Skip to content

Commit

Permalink
fixed issue with handling of process substitution described in issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
vsevolodkulaga committed Jun 1, 2017
1 parent 0dc1f38 commit d0da271
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
15 changes: 9 additions & 6 deletions minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ def isInsideGroupWhereWhitespacesCannotBeTruncated(self):


def minify(src):
# first remove all comments
# first: remove all comments
it = BashFileIterator(src)
src = "" # result
for ch in it.charactersGenerator():
if not it.isInsideComment():
src += ch

# second remove empty strings, strip lines and truncate spaces (replace groups of whitespaces by single space)
# secondly: remove empty strings, strip lines and truncate spaces (replace groups of whitespaces by single space)
it = BashFileIterator(src)
src = "" # result
emptyLine = True # means that no characters has been printed in current line so far
Expand Down Expand Up @@ -289,7 +289,7 @@ def minify(src):
previousSpacePrinted = False
emptyLine = False

# third get rid of newlines
# thirdly: get rid of newlines
it = BashFileIterator(src)
src = "" # result
for ch in it.charactersGenerator():
Expand All @@ -316,15 +316,18 @@ def minify(src):
elif it.getNextCharacter() != "" and it.getPreviousCharacter() != ";":
src += ";"

# finally remove spaces around semicolons and pipes
# finally: remove spaces around semicolons and pipes and other delimiters
it = BashFileIterator(src)
src = "" # result
other_delimiters = ('|', '&', ';', '<', '>', '(', ')') # characters that may not be surrounded by whitespaces
for ch in it.charactersGenerator():
if it.isInsideGroupWhereWhitespacesCannotBeTruncated():
src += ch
elif ch in (' ', '\t') and (it.getPreviousCharacter() in other_delimiters or
it.getNextCharacter() in other_delimiters):
elif ch in (' ', '\t') \
and (it.getPreviousCharacter() in other_delimiters or
it.getNextCharacter() in other_delimiters) \
and it.getNextCharacters(2) not in ('<(', '>('): # process substitution
# see test t_process_substitution.sh for details
continue
else:
src += ch
Expand Down
1 change: 1 addition & 0 deletions tests/minified_t_process_substitution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cat< <(which wc);cat <(which wc);seq 1 10|tee >(echo "# of lines:" `wc -l` 1>&2)|awk '{print $1*2}'
10 changes: 10 additions & 0 deletions tests/t_process_substitution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# for details see
# https://superuser.com/questions/1059781/what-exactly-is-in-bash-and-in-zsh
# https://en.wikipedia.org/wiki/Process_substitution

cat < <(which wc)

cat <(which wc)

seq 1 10 | tee >(echo "# of lines:" `wc -l` 1>&2) | awk '{print $1*2}'

0 comments on commit d0da271

Please sign in to comment.