--- parser3/src/classes/hash.C 2021/01/13 22:21:29 1.149 +++ parser3/src/classes/hash.C 2021/12/21 14:24:54 1.154 @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.149 2021/01/13 22:21:29 moko Exp $"; +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.154 2021/12/21 14:24:54 moko Exp $"; // class @@ -622,6 +622,7 @@ static void _at(Request& r, MethodParams extern String table_reverse_name; static void _select(Request& r, MethodParams& params) { + InCycle temp(r); const String* key_var_name=¶ms.as_string(0, "key-var name must be string"); const String* value_var_name=¶ms.as_string(1, "value-var name must be string"); Value& vcondition=params.as_expression(2, "condition must be number, bool or expression"); @@ -658,6 +659,7 @@ static void _select(Request& r, MethodPa HashStringValue& result_hash=*new HashStringValue(); if(limit>0){ + #ifdef HASH_ORDER if(reverse){ for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){ @@ -666,7 +668,12 @@ static void _select(Request& r, MethodPa if(value_var_name) r.put_element(caller, *value_var_name, i.value()); - if(r.process(vcondition).as_bool()){ + bool condition=r.process(vcondition).as_bool(); + + if(r.check_skip_break()) + break; + + if(condition){ result_hash.put(i.key(), i.value()); if(!--limit) break; @@ -682,7 +689,12 @@ static void _select(Request& r, MethodPa if(value_var_name) r.put_element(caller, *value_var_name, i.value()); - if(r.process(vcondition).as_bool()){ + bool condition=r.process(vcondition).as_bool(); + + if(r.check_skip_break()) + break; + + if(condition){ result_hash.put(i.key(), i.value()); if(!--limit) break; @@ -719,6 +731,24 @@ static void _reverse(Request& r, MethodP r.write(result); } + +static void _rename(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHashBase).hash(); + + if(params.count()>1){ + const String& key_from=params.as_string(0, "from key must be string"); + const String& key_to=params.as_string(1, "to key must be string"); + + hash.rename(key_from, key_to); + } else { + HashStringValue* names=params.as_hash(0,"single parameter"); + + for(HashStringValue::Iterator i(*names); i; i.next()) + hash.rename(i.key(), i.value()->as_string()); + } +} + + // constructor MHash::MHash(): Methoded("hash") @@ -770,6 +800,10 @@ MHash::MHash(): Methoded("hash") // ^hash._at([-+]offset)[['key'|'value'|'hash']] add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2); + // ^hash.rename[from;to] + // ^hash.rename[ $.from[to] ... ] + add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2); + #ifdef FEATURE_GET_ELEMENT4CALL // aliases without "_" add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1);