diff options
author | maxfl <gmaxfl@gmail.com> | 2012-07-07 10:57:28 +0800 |
---|---|---|
committer | maxfl <gmaxfl@gmail.com> | 2012-07-07 10:57:28 +0800 |
commit | 0a5e7be129644e9f53deb7051e812098c1f4e518 (patch) | |
tree | bfe36296f366f5d9546a601b5ab4005ccb34d6b9 /builtin_set.cpp | |
parent | b23d65b0144e61afee0b0fdd841c6c746b2e885d (diff) |
Add index ranges
Builtin 'set' now can set variable index ranges:
set test[1..3] a b c #works
set test[-1..-3] a b c #works if variable have enough elements
set test[2..-2] a b c #works
set test[1..3 -1..-2] a b c b b #works
Expand now can parse index ranges. But not handle for now.
TODO:
* Add variable substitution index ranges: echo $PATH[-1..1]
* Add command substitution index range: echo (seq 10)[-1..-4]
* Add process substitution indexes and ranges: echo %vim[-1]
Diffstat (limited to 'builtin_set.cpp')
-rw-r--r-- | builtin_set.cpp | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/builtin_set.cpp b/builtin_set.cpp index c1bb65cc..11fe2ab9 100644 --- a/builtin_set.cpp +++ b/builtin_set.cpp @@ -236,11 +236,33 @@ static int parse_index( std::vector<long> &indexes, l_ind = var_count+l_ind+1; } - indexes.push_back( l_ind ); src = end; - count++; - while (iswspace(*src)) src++; - } + if ( *src==L'.' && *(src+1)==L'.' ){ + src+=2; + long l_ind2 = wcstol( src, &end, 10 ); + if( end==src || errno ) + { + return 1; + } + src = end; + + if( l_ind2 < 0 ) + { + l_ind2 = var_count+l_ind2+1; + } + int direction = l_ind2<l_ind ? -1 : 1 ; + for (long jjj = l_ind; jjj*direction <= l_ind2*direction; jjj+=direction) { + // debug(0, L"Expand range [set]: %i\n", jjj); + indexes.push_back( jjj ); + count++; + } + } + else { + indexes.push_back( l_ind ); + count++; + } + while (iswspace(*src)) src++; + } return count; } |