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:
axel 2006-06-04 19:35:32 +10:00
parent 6fd69bdda8
commit e7e3e8ee74
2 changed files with 23 additions and 9 deletions

View file

@ -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,
const wchar_t *src,
const wchar_t *name )
const wchar_t *name,
int var_count )
{
int len;
@ -224,13 +225,20 @@ static int parse_index( array_list_t *indexes,
{
wchar_t *end;
long l_ind = wcstol(src, &end, 10);
int *ind;
if (end == src)
{
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
return 0;
}
int *ind = (int *) calloc(1, sizeof(int));
if( l_ind < 0 )
{
l_ind = var_count+l_ind+1;
}
ind = (int *) calloc(1, sizeof(int));
*ind = (int) l_ind;
al_push(indexes, ind);
src = end;
@ -643,13 +651,17 @@ int builtin_set( wchar_t **argv )
int idx_count, val_count;
array_list_t values;
array_list_t indexes;
array_list_t result;
al_init(&values);
al_init(&indexes);
al_init(&result);
tokenize_variable_array( env_get(dest), &result );
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 );
retcode = 1;
@ -682,10 +694,6 @@ int builtin_set( wchar_t **argv )
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 )
{
erase_values(&result, &indexes);
@ -712,9 +720,10 @@ int builtin_set( wchar_t **argv )
al_destroy( &value );
}
}
al_foreach( &result, (void (*)(const void *))&free );
al_destroy( &result );
}
al_foreach( &indexes, (void (*)(const void *))&free );
al_destroy(&indexes);

View file

@ -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++)
{
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 ) )
{
error( SYNTAX_ERROR,