--- parser3/src/classes/array.C 2024/10/27 12:24:49 1.27 +++ parser3/src/classes/array.C 2024/10/27 17:50:59 1.30 @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.27 2024/10/27 12:24:49 moko Exp $"; +volatile const char * IDENT_ARRAY_C="$Id: array.C,v 1.30 2024/10/27 17:50:59 moko Exp $"; // class @@ -614,9 +614,10 @@ static void _create_or_append_or_push(Re int count=params.count(); if(array.count()){ - for(int i=0; i0) - array.clear(VArray::index(params.as_int(0, PARAM_INDEX, r))); - else + if(params.count()>0) { + if(params[0].is_string()) { + array.clear(VArray::index(*params[0].get_string())); + } else { + array.clear(VArray::index(params.as_int(0, PARAM_INDEX, r))); + } + } else array.clear(); array.invalidate(); } @@ -656,10 +661,10 @@ static void _pop(Request& r, MethodParam Value *result=array.pop(); if(result){ r.write(*result); + array.change_used(-1); } else { r.write(*VVoid::get()); } - array.invalidate(); } static void _contains(Request& r, MethodParams& params) { @@ -892,8 +897,6 @@ static void _at(Request& r, MethodParams ArrayValue& array=GET_SELF(r, VArray).array(); size_t count=array.used(); // not array.count() - int pos=0; - AtResultType result_type=AtResultTypeValue; if(params.count() > 1) { const String& stype=params.as_string(1, "type must be string"); @@ -905,18 +908,7 @@ static void _at(Request& r, MethodParams throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'"); } - Value& vwhence=params[0]; - if(vwhence.is_string()) { - const String& swhence=*vwhence.get_string(); - if(swhence == "last") - pos=count-1; - else if(swhence != "first") - throw Exception(PARSER_RUNTIME, &swhence, "whence must be 'first', 'last' or expression"); - } else { - pos=r.process(vwhence).as_int(); - if(pos < 0) - pos+=count; - } + int pos=params.as_index(0, count, r); if(count && pos >= 0 && (size_t)pos < count){ if(count == array.count()){ @@ -1093,9 +1085,9 @@ MArray::MArray(): Methoded(VARRAY_TYPE) // ^array.mid(p;n) add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); - // ^array.delete[index] + // ^array.delete(index) or ^array.delete[index] add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1); - // ^array.remove[index] + // ^array.remove(index) add_native_method("remove", Method::CT_DYNAMIC, _remove, 1, 1); // ^array.pop[] add_native_method("pop", Method::CT_DYNAMIC, _pop, 0, 0);