hush: implement negative start in the ${v: -n[:m]} idiom
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
3234045d07
commit
5dad7bdc3b
5
shell/ash_test/ash-vars/var_bash6.right
Normal file
5
shell/ash_test/ash-vars/var_bash6.right
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Expected Actual
|
||||||
|
a*z : a*z
|
||||||
|
\z : \z
|
||||||
|
a1z a2z: a1z a2z
|
||||||
|
z : z
|
9
shell/ash_test/ash-vars/var_bash6.tests
Executable file
9
shell/ash_test/ash-vars/var_bash6.tests
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
# This testcase checks globbing correctness in ${v/a/b}
|
||||||
|
|
||||||
|
>a1z; >a2z;
|
||||||
|
echo 'Expected' 'Actual'
|
||||||
|
v='a bz'; echo 'a*z :' "${v/a*z/a*z}"
|
||||||
|
v='a bz'; echo '\z :' "${v/a*z/\z}"
|
||||||
|
v='a bz'; echo 'a1z a2z:' ${v/a*z/a*z}
|
||||||
|
v='a bz'; echo 'z :' ${v/a*z/\z}
|
||||||
|
rm a1z a2z
|
@ -5619,8 +5619,12 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
|
|||||||
goto arith_err;
|
goto arith_err;
|
||||||
debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len);
|
debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len);
|
||||||
if (len >= 0) { /* bash compat: len < 0 is illegal */
|
if (len >= 0) { /* bash compat: len < 0 is illegal */
|
||||||
if (beg < 0) /* bash compat */
|
if (beg < 0) {
|
||||||
beg = 0;
|
/* negative beg counts from the end */
|
||||||
|
beg = (arith_t)strlen(val) + beg;
|
||||||
|
if (beg < 0) /* ${v: -999999} is "" */
|
||||||
|
beg = len = 0;
|
||||||
|
}
|
||||||
debug_printf_varexp("from val:'%s'\n", val);
|
debug_printf_varexp("from val:'%s'\n", val);
|
||||||
if (len == 0 || !val || beg >= strlen(val)) {
|
if (len == 0 || !val || beg >= strlen(val)) {
|
||||||
arith_err:
|
arith_err:
|
||||||
|
6
shell/hush_test/hush-vars/var_bash1a.right
Normal file
6
shell/hush_test/hush-vars/var_bash1a.right
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
parameter 'abcdef'
|
||||||
|
varoffset2 'cdef'
|
||||||
|
varoffset-2 'ef'
|
||||||
|
literal '2' 'cdef'
|
||||||
|
literal '-2' 'abcdef'
|
||||||
|
literal ' -2' 'ef'
|
11
shell/hush_test/hush-vars/var_bash1a.tests
Executable file
11
shell/hush_test/hush-vars/var_bash1a.tests
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
parameter=abcdef
|
||||||
|
offset=2
|
||||||
|
noffset=-2
|
||||||
|
echo "parameter '${parameter}'"
|
||||||
|
echo "varoffset2 '${parameter:${offset}}'"
|
||||||
|
echo "varoffset-2 '${parameter:${noffset}}'"
|
||||||
|
echo "literal '2' '${parameter:2}'"
|
||||||
|
# This is not inrpreted as ${VAR:POS{:LEN}},
|
||||||
|
# but as ${VAR:=WORD} - if VAR is unset or null, substitute WORD
|
||||||
|
echo "literal '-2' '${parameter:-2}'"
|
||||||
|
echo "literal ' -2' '${parameter: -2}'"
|
Loading…
Reference in New Issue
Block a user