From 2b679882642868557ae4483e8128853511c12aa1 Mon Sep 17 00:00:00 2001 From: Andy C Date: Wed, 26 Jun 2024 10:33:56 -0400 Subject: [PATCH] [osh] Change spec to allow ${arr[@]::}, implement it in OSH 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 --- osh/word_parse.py | 9 +++++---- spec/var-op-slice.test.sh | 24 +++++++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/osh/word_parse.py b/osh/word_parse.py index 88bc791e68..694d1c39ca 100644 --- a/osh/word_parse.py +++ b/osh/word_parse.py @@ -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} diff --git a/spec/var-op-slice.test.sh b/spec/var-op-slice.test.sh index dd33424c2a..57ec455cf9 100644 --- a/spec/var-op-slice.test.sh +++ b/spec/var-op-slice.test.sh @@ -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