From c0e996acf8226e44da995d0e91dc26c4903e4427 Mon Sep 17 00:00:00 2001 From: maxfl Date: Sat, 7 Jul 2012 22:01:28 +0800 Subject: [PATCH] Implemented index ranges for command substitution Now the following code works: > echo (seq 10)[-1..1] With output: 10 9 8 7 6 5 4 3 2 1 --- expand.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/expand.cpp b/expand.cpp index 7212d7952..ab75a40ab 100644 --- a/expand.cpp +++ b/expand.cpp @@ -727,10 +727,8 @@ 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 ) +static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx, int size=-1 ) { - - wchar_t *end; int pos = 1; @@ -769,9 +767,17 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector } pos = end-in; - // debug( 0, L"Push range idx %d %d", tmp, tmp1 ); - idx.push_back(tmp); - // idx.push_back(tmp2); + 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 slice_idx; wchar_t *slice_end; - if( parse_slice( tail_begin, &slice_end, slice_idx ) ) + if( parse_slice( tail_begin, &slice_end, slice_idx, sub_res.size() ) ) { parser.error( SYNTAX_ERROR, -1, L"Invalid index value" ); return 0; @@ -1216,11 +1222,6 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector for( i=0; i < slice_idx.size(); i++ ) { long idx = slice_idx.at(i); - if( idx < 0 ) - { - idx = sub_res.size() + idx + 1; - } - if( idx < 1 || (size_t)idx > sub_res.size() ) { parser.error( SYNTAX_ERROR, -1, L"Invalid index value" );