Skip to content

Commit

Permalink
Implement lowercase and uppercase, e.g. ${prev,,}
Browse files Browse the repository at this point in the history
Used by bash_completion.
  • Loading branch information
Andy Chu committed Sep 29, 2018
1 parent 281e0ac commit 19da39c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/libstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ def DoUnarySuffixOp(s, op, arg):
else:
return s

elif op.op_id == Id.VOp1_Comma: # Only lowercase the first letter
return s[0].lower() + s[1:]

elif op.op_id == Id.VOp1_DComma:
return s.lower()

elif op.op_id == Id.VOp1_Caret: # Only uppercase the first letter
return s[0].upper() + s[1:]

elif op.op_id == Id.VOp1_DCaret:
return s.upper()

else: # e.g. ^ ^^ , ,,
raise AssertionError(op.op_id)

Expand Down
5 changes: 5 additions & 0 deletions doc/architecture-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ This isn't re-parsing, but it's re-reading.
- ${s:1:2} -- offsets in code points
- ${x#?} and family (not yet implemented)

Where bash respects it:

- [[ a < b ]] and [ a '<' b ] for sorting
- ${foo,} and ${foo^} for lowercase / uppercase

## Parse-time and Runtime Pairs

- echo -e '\x00\n' and echo $'\x00\n' (shared in OSH)
Expand Down
26 changes: 26 additions & 0 deletions spec/var-op-other.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,29 @@ echo -${s:1:3}-
## N-I dash stdout-json: ""
## N-I bash status: 0
## N-I bash stdout-json: "slice\n-bcd-\n"

#### Lower Case with , and ,,
x='ABC DEF'
echo ${x,}
echo ${x,,}
## STDOUT:
aBC DEF
abc def
## END
## N-I dash/mksh/zsh stdout-json: ""
## N-I dash status: 2
## N-I mksh/zsh status: 1


#### Upper Case with ^ and ^^
x='abc def'
echo ${x^}
echo ${x^^}
## STDOUT:
Abc def
ABC DEF
## END
## N-I dash/mksh/zsh stdout-json: ""
## N-I dash status: 2
## N-I mksh/zsh status: 1

0 comments on commit 19da39c

Please sign in to comment.