mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-14 05:53:59 +00:00
Add support for negative indices in arrays. Negative indices count from the end of an array, so -4 is the fourth index from the end
darcs-hash:20060604093532-ac50b-6e22b2f2ccfe94375fe4c32e41ccec08ca501744.gz
This commit is contained in:
parent
6fd69bdda8
commit
e7e3e8ee74
2 changed files with 23 additions and 9 deletions
|
@ -177,7 +177,8 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
|
||||||
*/
|
*/
|
||||||
static int parse_index( array_list_t *indexes,
|
static int parse_index( array_list_t *indexes,
|
||||||
const wchar_t *src,
|
const wchar_t *src,
|
||||||
const wchar_t *name )
|
const wchar_t *name,
|
||||||
|
int var_count )
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
|
@ -224,13 +225,20 @@ static int parse_index( array_list_t *indexes,
|
||||||
{
|
{
|
||||||
wchar_t *end;
|
wchar_t *end;
|
||||||
long l_ind = wcstol(src, &end, 10);
|
long l_ind = wcstol(src, &end, 10);
|
||||||
|
int *ind;
|
||||||
|
|
||||||
if (end == src)
|
if (end == src)
|
||||||
{
|
{
|
||||||
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
|
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( l_ind < 0 )
|
||||||
|
{
|
||||||
|
l_ind = var_count+l_ind+1;
|
||||||
|
}
|
||||||
|
|
||||||
int *ind = (int *) calloc(1, sizeof(int));
|
ind = (int *) calloc(1, sizeof(int));
|
||||||
*ind = (int) l_ind;
|
*ind = (int) l_ind;
|
||||||
al_push(indexes, ind);
|
al_push(indexes, ind);
|
||||||
src = end;
|
src = end;
|
||||||
|
@ -643,13 +651,17 @@ int builtin_set( wchar_t **argv )
|
||||||
int idx_count, val_count;
|
int idx_count, val_count;
|
||||||
array_list_t values;
|
array_list_t values;
|
||||||
array_list_t indexes;
|
array_list_t indexes;
|
||||||
|
array_list_t result;
|
||||||
|
|
||||||
al_init(&values);
|
al_init(&values);
|
||||||
al_init(&indexes);
|
al_init(&indexes);
|
||||||
|
al_init(&result);
|
||||||
|
|
||||||
|
tokenize_variable_array( env_get(dest), &result );
|
||||||
|
|
||||||
for( ; woptind<argc; woptind++ )
|
for( ; woptind<argc; woptind++ )
|
||||||
{
|
{
|
||||||
if( !parse_index( &indexes, argv[woptind], dest ) )
|
if( !parse_index( &indexes, argv[woptind], dest, al_get_count( &result ) ) )
|
||||||
{
|
{
|
||||||
builtin_print_help( argv[0], sb_err );
|
builtin_print_help( argv[0], sb_err );
|
||||||
retcode = 1;
|
retcode = 1;
|
||||||
|
@ -682,10 +694,6 @@ int builtin_set( wchar_t **argv )
|
||||||
Slice indexes have been calculated, do the actual work
|
Slice indexes have been calculated, do the actual work
|
||||||
*/
|
*/
|
||||||
|
|
||||||
array_list_t result;
|
|
||||||
al_init(&result);
|
|
||||||
|
|
||||||
tokenize_variable_array( env_get(dest), &result );
|
|
||||||
if( erase )
|
if( erase )
|
||||||
{
|
{
|
||||||
erase_values(&result, &indexes);
|
erase_values(&result, &indexes);
|
||||||
|
@ -712,10 +720,11 @@ int builtin_set( wchar_t **argv )
|
||||||
al_destroy( &value );
|
al_destroy( &value );
|
||||||
|
|
||||||
}
|
}
|
||||||
al_foreach( &result, (void (*)(const void *))&free );
|
|
||||||
al_destroy( &result );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
al_foreach( &result, (void (*)(const void *))&free );
|
||||||
|
al_destroy( &result );
|
||||||
|
|
||||||
al_foreach( &indexes, (void (*)(const void *))&free );
|
al_foreach( &indexes, (void (*)(const void *))&free );
|
||||||
al_destroy(&indexes);
|
al_destroy(&indexes);
|
||||||
al_destroy(&values);
|
al_destroy(&values);
|
||||||
|
|
5
expand.c
5
expand.c
|
@ -815,6 +815,11 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx )
|
||||||
for( j=0; j<al_get_count( var_idx_list ); j++)
|
for( j=0; j<al_get_count( var_idx_list ); j++)
|
||||||
{
|
{
|
||||||
long tmp = (long)al_get( var_idx_list, j );
|
long tmp = (long)al_get( var_idx_list, j );
|
||||||
|
if( tmp < 0 )
|
||||||
|
{
|
||||||
|
tmp = al_get_count( &var_item_list)+tmp+1;
|
||||||
|
}
|
||||||
|
|
||||||
if( tmp < 1 || tmp > al_get_count( &var_item_list ) )
|
if( tmp < 1 || tmp > al_get_count( &var_item_list ) )
|
||||||
{
|
{
|
||||||
error( SYNTAX_ERROR,
|
error( SYNTAX_ERROR,
|
||||||
|
|
Loading…
Reference in a new issue