--- parser3/src/classes/array.C 2024/10/20 13:20:57 1.19 +++ parser3/src/classes/array.C 2024/10/23 16:41:11 1.22 @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.19 2024/10/20 13:20:57 moko Exp $"; +volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.22 2024/10/23 16:41:11 moko Exp $"; // class @@ -544,7 +544,7 @@ static void _right(Request& r, MethodPar if(sn>0){ size_t used=GET_SELF(r, VArray).array().used(); - if(sn= 0 && (size_t)pos < count){ - switch(result_type) { - case AtResultTypeKey: - { - for(ArrayValue::Iterator i(array); i; i.next() ){ - if(i.value() && !(pos--)){ + if(count == array.count()){ + switch(result_type) { + case AtResultTypeKey: + r.write(*new VString(*new String(pa_uitoa(pos), String::L_TAINTED))); + break; + case AtResultTypeValue: + r.write(*array[pos]); + break; + case AtResultTypeHash: + r.write(SingleElementHash(pa_uitoa(pos), array[pos])); + break; + } + } else { + for(ArrayValue::Iterator i(array); i; i.next() ){ + if(i.value() && !(pos--)){ + switch(result_type) { + case AtResultTypeKey: r.write(*new VString(*new String(i.key(), String::L_TAINTED))); break; - } - } - break; - } - case AtResultTypeValue: - { - for(ArrayValue::Iterator i(array); i; i.next() ) - if(i.value() &&!(pos--)){ + case AtResultTypeValue: r.write(*i.value()); break; - } - break; - } - case AtResultTypeHash: - { - for(ArrayValue::Iterator i(array); i; i.next() ) - if(i.value() &&!(pos--)){ + case AtResultTypeHash: r.write(SingleElementHash(i.key(), i.value())); break; - } + } break; } + } } } } @@ -1034,6 +1034,10 @@ static void _reverse(Request& r, MethodP r.write(result); } +static void _compact(Request& r, MethodParams&) { + GET_SELF(r, VArray).array().compact(); +} + // constructor @@ -1093,6 +1097,9 @@ MArray::MArray(): Methoded(VARRAY_TYPE) // ^array.reverse[] add_native_method("reverse", Method::CT_DYNAMIC, _reverse, 0, 0); + // ^array.compact[] + add_native_method("compact", Method::CT_DYNAMIC, _compact, 0, 0); + // ^array._at[first|last[;'key'|'value'|'hash']] // ^array._at([-+]offset)[['key'|'value'|'hash']] add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2);