--- parser3/src/classes/hash.C 2024/09/28 19:42:41 1.164 +++ parser3/src/classes/hash.C 2025/05/26 01:56:54 1.172 @@ -1,7 +1,7 @@ /** @file Parser: @b hash parser class. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ @@ -11,13 +11,14 @@ #include "pa_request.h" #include "pa_charsets.h" #include "pa_vhash.h" +#include "pa_varray.h" #include "pa_vvoid.h" #include "pa_sql_connection.h" #include "pa_vtable.h" #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.164 2024/09/28 19:42:41 moko Exp $"; +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.172 2025/05/26 01:56:54 moko Exp $"; // class @@ -197,7 +198,13 @@ static void _create_or_add(Request& r, M HashStringValue* self_hash=&(self.hash()); HashStringValue* src_hash; - if(VHashBase* src=dynamic_cast(&vsrc)) { + if(VArray* src=dynamic_cast(&vsrc)) { + for(ArrayValue::Iterator i(src->array()); i; i.next()){ + if(i.value()) + self_hash->put(String::Body::uitoa(i.index()), i.value()); + } + return; + } else if(VHashBase* src=dynamic_cast(&vsrc)) { src_hash=&(src->hash()); if(src_hash==self_hash) // same: doing nothing @@ -206,6 +213,7 @@ static void _create_or_add(Request& r, M if(Value* vdefault=src->get_default()) self.set_default(vdefault); } else { + // allows $h[^hash::create[non-blank string]], thus as_hash("param") is more correct, but is not backward compatible src_hash=vsrc.get_hash(); } @@ -425,7 +433,7 @@ static void _foreach(Request& r, MethodP bool need_delim=false;; for(HashStringValue::Iterator i(hash); i; i.next()){ if(key_var_name){ - VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); + VString* vkey=new VString(i.key()); r.put_element(caller, *key_var_name, vkey); } @@ -451,7 +459,7 @@ static void _foreach(Request& r, MethodP } else { for(HashStringValue::Iterator i(hash); i; i.next()){ if(key_var_name){ - VString* vkey=new VString(*new String(i.key(), String::L_TAINTED)); + VString* vkey=new VString(i.key()); r.put_element(caller, *key_var_name, vkey); } @@ -472,7 +480,7 @@ enum AtResultType { AtResultTypeHash = 2 }; -inline Value& SingleElementHash(String::Body akey, Value* avalue) { +static Value& SingleElementHash(String::Body akey, Value* avalue) { Value& result=*new VHash; result.put_element(*new String(akey, String::L_TAINTED), avalue); return result; @@ -523,7 +531,7 @@ static void _sort(Request& r, MethodPara for(HashStringValue::Iterator i(hash); i; i.next(), pos++ ){ if(key_var) - r.put_element(*context, *key_var, new VString(*new String(i.key(), String::L_TAINTED))); + r.put_element(*context, *key_var, new VString(i.key())); if(value_var) r.put_element(*context, *value_var, i.value()); @@ -564,8 +572,6 @@ static void _at(Request& r, MethodParams HashStringValue& hash=GET_SELF(r, VHashBase).hash(); size_t count=hash.count(); - int pos=0; - // misha@ // I do not like that type is checked before whence. // But I do not like the idea to move it after whence (where process can be called) even more. @@ -580,20 +586,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){ switch(result_type) { @@ -601,15 +594,15 @@ static void _at(Request& r, MethodParams { #ifdef HASH_ORDER if(pos == 0) { - r.write(*new VString(*new String(hash.first_key(), String::L_TAINTED))); + r.write(*new VString(hash.first_key())); } else if((size_t)pos == count-1) { - r.write(*new VString(*new String(hash.last_key(), String::L_TAINTED))); + r.write(*new VString(hash.last_key())); } else #endif { for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) if(!pos){ - r.write(*new VString(*new String(i.key(), String::L_TAINTED))); + r.write(*new VString(i.key())); break; } } @@ -655,6 +648,33 @@ static void _at(Request& r, MethodParams } } +static void _set(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHashBase).hash(); + size_t count=hash.count(); + + int pos=params.as_index(0, count, r); + + if(count && pos >= 0 && (size_t)pos < count){ +#ifdef HASH_ORDER + if((size_t)pos == count-1) { + hash.last_pair()->value=&r.process(params[1]); + return; + } else +#endif + { + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + i.pair()->value=&r.process(params[1]); + return; + } + } + } + + if(count) + throw Exception(PARSER_RUNTIME, 0, "index '%d' is out of range 0..%d", pos, count-1); + throw Exception(PARSER_RUNTIME, 0, "index '%d' is out of range: hash is empty", pos); +} + extern String table_reverse_name; static void _select(Request& r, MethodParams& params) { @@ -700,7 +720,7 @@ static void _select(Request& r, MethodPa if(reverse){ for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){ if(key_var_name) - r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED))); + r.put_element(caller, *key_var_name, new VString(i.key())); if(value_var_name) r.put_element(caller, *value_var_name, i.value()); @@ -721,7 +741,7 @@ static void _select(Request& r, MethodPa #endif for(HashStringValue::Iterator i(source_hash); i; i.next() ){ if(key_var_name) - r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED))); + r.put_element(caller, *key_var_name, new VString(i.key())); if(value_var_name) r.put_element(caller, *value_var_name, i.value()); @@ -747,7 +767,7 @@ static void _select(Request& r, MethodPa r.write(*result); } -static void _reverse(Request& r, MethodParams& params) { +static void _reverse(Request& r, MethodParams&) { VHashBase& self=GET_SELF(r, VHashBase); HashStringValue& source_hash=self.hash(); HashStringValue& result_hash=*new HashStringValue(); @@ -785,6 +805,35 @@ static void _rename(Request& r, MethodPa } +static void _array(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHashBase).hash(); + + VArray& result=*new VArray; + ArrayValue& array=result.array(); + + if(params.count() > 0) { + const String& smode=params.as_string(0, "mode must be string"); + if(smode == "keys"){ + for(HashStringValue::Iterator i(hash); i; i.next()){ + array+=new VString(i.key()); + } + } else if(smode == "values"){ + for(HashStringValue::Iterator i(hash); i; i.next()){ + array+=i.value(); + } + } else { + throw Exception(PARSER_RUNTIME, &smode, "mode must be 'keys' or 'values'"); + } + } else { + for(HashStringValue::Iterator i(hash); i; i.next()){ + array.put(VArray::index(i.key()), i.value()); + } + } + + r.write(result); +} + + // constructor MHash::MHash(): Methoded("hash") @@ -833,13 +882,20 @@ MHash::MHash(): Methoded("hash") add_native_method("reverse", Method::CT_DYNAMIC, _reverse, 0, 0); // ^hash._at[first|last[;'key'|'value'|'hash']] - // ^hash._at([-+]offset)[['key'|'value'|'hash']] + // ^hash._at([-+]index)[['key'|'value'|'hash']] add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2); + // ^hash.set[first|last;value] + // ^hash.set([-+]index)[value] + add_native_method("set", Method::CT_DYNAMIC, _set, 2, 2); + // ^hash.rename[from;to] // ^hash.rename[ $.from[to] ... ] add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2); + // ^hash.array[[keys|values]] + add_native_method("array", Method::CT_DYNAMIC, _array, 0, 1); + #ifdef FEATURE_GET_ELEMENT4CALL // aliases without "_" add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1);