diff --git a/expand.cpp b/expand.cpp index ab75a40ab..c370725a3 100644 --- a/expand.cpp +++ b/expand.cpp @@ -727,7 +727,7 @@ void expand_variable_error( parser_t &parser, const wchar_t *token, int token_po /** Parse an array slicing specification */ -static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx, int size=-1 ) +static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx, int size ) { wchar_t *end; @@ -757,6 +757,7 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector } // debug( 0, L"Push idx %d", tmp ); + long i1 = tmp>-1 ? tmp : size+tmp+1; pos = end-in; if ( in[pos]==L'.' && in[pos+1]==L'.' ){ pos+=2; @@ -767,23 +768,19 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector } pos = end-in; - if ( size>-1 ) { - // debug( 0, L"Push range idx %d %d", tmp, tmp1 ); - long i1 = tmp>-1 ? tmp : size+tmp+1; - long i2 = tmp1>-1 ? tmp1 : size+tmp1+1; - // debug( 0, L"Push range idx %d %d", i1, i2 ); - short direction = i2-1 ? tmp1 : size+tmp1+1; + // debug( 0, L"Push range idx %d %d", i1, i2 ); + short direction = i2 sub_res.size() ) { - parser.error( SYNTAX_ERROR, -1, L"Invalid index value" ); + parser.error( SYNTAX_ERROR, + -1, + ARRAY_BOUNDS_ERR ); return 0; } - idx = idx-1; sub_res2.push_back(sub_res.at(idx)); diff --git a/tests/test8.err b/tests/test8.err new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test8.in b/tests/test8.in new file mode 100644 index 000000000..e03d9d05b --- /dev/null +++ b/tests/test8.in @@ -0,0 +1,21 @@ +# Test index ranges + +# Test variable expand +set test (seq 10) +echo $test[1..10] # normal range +echo $test[10..1] # inverted range +echo $test[2..5 8..6] # several ranges +echo $test[-1..-2] # range with negative limits +echo $test[-1..1] # range with mixed limits + +# Test variable set +set test1 $test +set test1[-1..1] $test # reverse variable +echo $echo $test1 +set test1[2..4 -2..-4] $test1[4..2 -4..-2] +echo $test1 + +# Test command substitution +echo (seq 5)[-1..1] +echo (seq 10)[3..5 -2..2] + diff --git a/tests/test8.out b/tests/test8.out new file mode 100644 index 000000000..e118e572b --- /dev/null +++ b/tests/test8.out @@ -0,0 +1,9 @@ +1 2 3 4 5 6 7 8 9 10 +10 9 8 7 6 5 4 3 2 1 +2 3 4 5 8 7 6 +10 9 +10 9 8 7 6 5 4 3 2 1 +10 9 8 7 6 5 4 3 2 1 +10 7 8 9 6 5 2 3 4 1 +5 4 3 2 1 +3 4 5 9 8 7 6 5 4 3 2 diff --git a/tests/test8.status b/tests/test8.status new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/tests/test8.status @@ -0,0 +1 @@ +0 diff --git a/tests/top.out b/tests/top.out index 5614a6619..0c7b1103a 100644 --- a/tests/top.out +++ b/tests/top.out @@ -6,3 +6,4 @@ File test4.in tested ok File test5.in tested ok File test6.in tested ok File test7.in tested ok +File test8.in tested ok