|
|
| version 1.26, 2024/10/26 18:53:36 | version 1.30, 2024/10/27 17:50:59 |
|---|---|
| Line 608 static void _count(Request& r, MethodPar | Line 608 static void _count(Request& r, MethodPar |
| r.write(*new VInt(array.used())); | r.write(*new VInt(array.used())); |
| } | } |
| static void _create_or_append(Request& r, MethodParams& params) { | static void _create_or_append_or_push(Request& r, MethodParams& params) { |
| ArrayValue& array=GET_SELF(r, VArray).array(); | ArrayValue& array=GET_SELF(r, VArray).array(); |
| int count=params.count(); | int count=params.count(); |
| if(array.count()){ | if(array.count()){ |
| for(int i=0; i<count; i++) | for(int i=0; i<count; i++){ |
| array+=&r.process(params[i]); | array+=&r.process(params[i]); |
| array.invalidate(); | array.change_used(+1); // after each element, since an exception can occur |
| } | |
| } else { | } else { |
| for(int i=0; i<count; i++) | for(int i=0; i<count; i++) |
| array+=&r.process(params[i]); | array+=&r.process(params[i]); |
| Line 632 static void _insert(Request& r, MethodPa | Line 633 static void _insert(Request& r, MethodPa |
| for(int i=1; i<count; i++){ | for(int i=1; i<count; i++){ |
| array.insert(index++, &r.process(params[i])); | array.insert(index++, &r.process(params[i])); |
| array.change_used(+1); // after each element, since an exception can occur | |
| } | } |
| array.invalidate(); | |
| } | } |
| static void _delete(Request& r, MethodParams& params) { | static void _delete(Request& r, MethodParams& params) { |
| ArrayValue& array=GET_SELF(r, VArray).array(); | ArrayValue& array=GET_SELF(r, VArray).array(); |
| if(params.count()>0) | if(params.count()>0) { |
| array.clear(VArray::index(params.as_int(0, PARAM_INDEX, r))); | if(params[0].is_string()) { |
| else | array.clear(VArray::index(*params[0].get_string())); |
| } else { | |
| array.clear(VArray::index(params.as_int(0, PARAM_INDEX, r))); | |
| } | |
| } else | |
| array.clear(); | array.clear(); |
| array.invalidate(); | array.invalidate(); |
| } | } |
| Line 651 static void _remove(Request& r, MethodPa | Line 656 static void _remove(Request& r, MethodPa |
| array.invalidate(); | array.invalidate(); |
| } | } |
| static void _pop(Request& r, MethodParams& params) { | |
| ArrayValue& array=GET_SELF(r, VArray).array(); | |
| Value *result=array.pop(); | |
| if(result){ | |
| r.write(*result); | |
| array.change_used(-1); | |
| } else { | |
| r.write(*VVoid::get()); | |
| } | |
| } | |
| static void _contains(Request& r, MethodParams& params) { | static void _contains(Request& r, MethodParams& params) { |
| VArray& self=GET_SELF(r, VArray); | VArray& self=GET_SELF(r, VArray); |
| bool result=self.contains(VArray::index(params.as_int(0, PARAM_INDEX, r))); | bool result=self.contains(VArray::index(params.as_int(0, PARAM_INDEX, r))); |
| Line 881 static void _at(Request& r, MethodParams | Line 897 static void _at(Request& r, MethodParams |
| ArrayValue& array=GET_SELF(r, VArray).array(); | ArrayValue& array=GET_SELF(r, VArray).array(); |
| size_t count=array.used(); // not array.count() | size_t count=array.used(); // not array.count() |
| int pos=0; | |
| AtResultType result_type=AtResultTypeValue; | AtResultType result_type=AtResultTypeValue; |
| if(params.count() > 1) { | if(params.count() > 1) { |
| const String& stype=params.as_string(1, "type must be string"); | const String& stype=params.as_string(1, "type must be string"); |
| Line 894 static void _at(Request& r, MethodParams | Line 908 static void _at(Request& r, MethodParams |
| throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'"); | throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'"); |
| } | } |
| Value& vwhence=params[0]; | int pos=params.as_index(0, count, r); |
| 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; | |
| } | |
| if(count && pos >= 0 && (size_t)pos < count){ | if(count && pos >= 0 && (size_t)pos < count){ |
| if(count == array.count()){ | if(count == array.count()){ |
| Line 1065 MArray::MArray(): Methoded(VARRAY_TYPE) | Line 1068 MArray::MArray(): Methoded(VARRAY_TYPE) |
| // ^array.join[join_from[;options]] | // ^array.join[join_from[;options]] |
| add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2); | add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2); |
| // ^array::create[value;value] | // ^array::create[value[;value...]] |
| add_native_method("create", Method::CT_DYNAMIC, _create_or_append, 0, 10000); | add_native_method("create", Method::CT_DYNAMIC, _create_or_append_or_push, 0, 10000); |
| // ^array.append[value;value] | // ^array.append[value[;value...]] |
| add_native_method("append", Method::CT_DYNAMIC, _create_or_append, 1, 10000); | add_native_method("append", Method::CT_DYNAMIC, _create_or_append_or_push, 1, 10000); |
| // ^array.insert[index;value...] | // ^array.push[value[;value...]] |
| add_native_method("push", Method::CT_DYNAMIC, _create_or_append_or_push, 1, 10000); | |
| // ^array.insert[index;value[;value...]] | |
| add_native_method("insert", Method::CT_DYNAMIC, _insert, 2, 10000); | add_native_method("insert", Method::CT_DYNAMIC, _insert, 2, 10000); |
| // ^array.left(n) | // ^array.left(n) |
| Line 1080 MArray::MArray(): Methoded(VARRAY_TYPE) | Line 1085 MArray::MArray(): Methoded(VARRAY_TYPE) |
| // ^array.mid(p;n) | // ^array.mid(p;n) |
| add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); | 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); | 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); | add_native_method("remove", Method::CT_DYNAMIC, _remove, 1, 1); |
| // ^array.pop[] | |
| add_native_method("pop", Method::CT_DYNAMIC, _pop, 0, 0); | |
| // ^array.contains[index] | // ^array.contains[index] |
| add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1); | add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1); |