mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-07 18:21:16 -03:00
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
This commit is contained in:
25
expand.cpp
25
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
|
Parse an array slicing specification
|
||||||
*/
|
*/
|
||||||
static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector<long> &idx )
|
static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector<long> &idx, int size=-1 )
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
wchar_t *end;
|
wchar_t *end;
|
||||||
|
|
||||||
int pos = 1;
|
int pos = 1;
|
||||||
@@ -769,9 +767,17 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector<long>
|
|||||||
}
|
}
|
||||||
pos = end-in;
|
pos = end-in;
|
||||||
|
|
||||||
// debug( 0, L"Push range idx %d %d", tmp, tmp1 );
|
if ( size>-1 ) {
|
||||||
idx.push_back(tmp);
|
// debug( 0, L"Push range idx %d %d", tmp, tmp1 );
|
||||||
// idx.push_back(tmp2);
|
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<i1 ? -1 : 1 ;
|
||||||
|
for (long jjj = i1; jjj*direction <= i2*direction; jjj+=direction) {
|
||||||
|
// debug(0, L"Expand range [subst]: %i\n", jjj);
|
||||||
|
idx.push_back( jjj );
|
||||||
|
}
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1204,7 +1210,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector
|
|||||||
std::vector<long> slice_idx;
|
std::vector<long> slice_idx;
|
||||||
wchar_t *slice_end;
|
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" );
|
parser.error( SYNTAX_ERROR, -1, L"Invalid index value" );
|
||||||
return 0;
|
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++ )
|
for( i=0; i < slice_idx.size(); i++ )
|
||||||
{
|
{
|
||||||
long idx = slice_idx.at(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() )
|
if( idx < 1 || (size_t)idx > sub_res.size() )
|
||||||
{
|
{
|
||||||
parser.error( SYNTAX_ERROR, -1, L"Invalid index value" );
|
parser.error( SYNTAX_ERROR, -1, L"Invalid index value" );
|
||||||
|
|||||||
Reference in New Issue
Block a user