Skip to content

Commit

Permalink
[osh] Change spec to allow ${arr[@]::}, implement it in OSH
Browse files Browse the repository at this point in the history
The the missing length is interpreted as zero.

Even though

- It appears undocumented
- zsh doesn't agree

This came up on PR #725 a few years ago, for ble.sh

Discussion:

    https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/.24.7Barr.5B.40.5D.3A.3A.7D.20in.20bash.20-.20is.20it.20documented.3F
  • Loading branch information
Andy C committed Jun 26, 2024
1 parent 1b0993c commit 2b67988
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 5 additions & 4 deletions osh/word_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,11 @@ def _ReadSliceVarOp(self):
if self.token_type != Id.Arith_RBrace:
length = self._ReadArithExpr(Id.Arith_RBrace)
else:
p_die('Use explicit slice length of zero',
self.cur_token)
# bash behavior
# length = arith_expr.EmptyZero # ${a:1:} or ${a::}
# quirky bash behavior:
# ${a:1:} or ${a::} means zero length
# but ${a:1} or ${a:} means length N
length = arith_expr.EmptyZero

return suffix_op.Slice(begin, length)

elif cur_id == Id.Arith_RBrace: # ${a:1} or ${@:1}
Expand Down
24 changes: 17 additions & 7 deletions spec/var-op-slice.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,32 @@ $SH -c 's=123; argv.py space ${s: }'
['space', '123']
## END

#### don't agree with ${array[@]::} has implicit length of zero!
#### ${array[@]::} has implicit length of zero - for ble.sh

# https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/.24.7Barr.5B.40.5D.3A.3A.7D.20in.20bash.20-.20is.20it.20documented.3F

array=(1 2 3)
argv.py ${array[@]::}
argv.py ${array[@]:0:}

echo

set -- 1 2 3
#argv.py ${@:}
argv.py ${@::}
argv.py ${@:0:}

## status: 1
## stdout-json: ""
## status: 0
## STDOUT:

## OK osh status: 2
## status: 0
## STDOUT:
[]
[]

## N-I bash status: 0
## N-I bash STDOUT:
[]
[]
## END

## OK mksh/zsh status: 1
## OK mksh/zsh STDOUT:
## END

0 comments on commit 2b67988

Please sign in to comment.