--- parser3/src/classes/hash.C 2012/06/13 22:53:47 1.115 +++ parser3/src/classes/hash.C 2015/10/15 18:12:29 1.124 @@ -9,6 +9,7 @@ #include "pa_vmethod_frame.h" #include "pa_request.h" +#include "pa_charsets.h" #include "pa_vhash.h" #include "pa_vvoid.h" #include "pa_sql_connection.h" @@ -16,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.115 2012/06/13 22:53:47 moko Exp $"; +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.124 2015/10/15 18:12:29 moko Exp $"; // class @@ -107,9 +108,9 @@ public: return false; } - bool add_row_cell(SQL_Error& error, const char *ptr, size_t ) { + bool add_row_cell(SQL_Error& error, const char *str, size_t ) { try { - String& cell=*new String(ptr, String::L_TAINTED /* no length as 0x00 can be inside */); + const String& cell=str?*new String(str, String::L_TAINTED /* no length as 0x00 can be inside */):String::Empty; bool duplicate=false; if(one_bool_column) { @@ -277,8 +278,14 @@ static bool intersects( static void _intersects(Request& r, MethodParams& params) { bool result=false; - if(HashStringValue* b=params.as_hash(0, "param")) - result=GET_SELF(r, VHash).hash().first_that(intersects, b)!=0; + if(HashStringValue* b=params.as_hash(0, "param")) { + HashStringValue* self=&(GET_SELF(r, VHash).hash()); + if(b==self) { + r.write_no_lang(VBool::get(true)); + return; + } + result=self->first_that(intersects, b)!=0; + } // return result r.write_no_lang(VBool::get(result)); @@ -387,8 +394,10 @@ static void _count(Request& r, MethodPar } static void _delete(Request& r, MethodParams& params) { - - GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string")); + if(params.count()>0) + GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string")); + else + GET_SELF(r, VHash).hash().clear(); } static void _contains(Request& r, MethodParams& params) { @@ -415,10 +424,10 @@ static bool one_foreach_cycle( Value& var_context=*info->var_context; if(info->key_var_name){ VString* vkey=new VString(*new String(akey, String::L_TAINTED)); - var_context.put_element(*info->key_var_name, vkey, false); + info->r->put_element(var_context, *info->key_var_name, vkey); } if(info->value_var_name) - var_context.put_element(*info->value_var_name, avalue, false); + info->r->put_element(var_context, *info->value_var_name, avalue); if(info->delim_maybe_code){ // delimiter set StringOrValue sv_processed=info->r->process(*info->body_code); @@ -461,14 +470,126 @@ static void _foreach(Request& r, MethodP hash.first_that(one_foreach_cycle, &info); } +enum AtResultType { + AtResultTypeValue = 0, + AtResultTypeKey = 1, + AtResultTypeHash = 2 +}; + +inline Value& SingleElementHash(String::Body akey, Value* avalue) { + Value& result=*new VHash; + result.put_element(*new String(akey, String::L_TAINTED), avalue); + return result; +} + +#ifndef DOXYGEN +struct Hash_seq_item { + HashStringValue::Pair *hash_pair; + union { + const char *c_str; + double d; + } value; +}; +#endif +static int sort_cmp_string(const void *a, const void *b) { + return strcmp( + static_cast(a)->value.c_str, + static_cast(b)->value.c_str + ); +} +static int sort_cmp_double(const void *a, const void *b) { + double va=static_cast(a)->value.d; + double vb=static_cast(b)->value.d; + if(vavb) + return +1; + else + return 0; +} +static void _sort(Request& r, MethodParams& params){ +#ifdef HASH_ORDER + const String& key_var_name=params.as_string(0, "key-var name must be string"); + const String& value_var_name=params.as_string(1, "value-var name must be string"); + Value& key_maker=params.as_junction(2, "key-maker must be code"); + bool reverse=params.count()>3/*..[desc|asc|]*/? + reverse=params.as_no_junction(3, "order must not be code").as_string()=="desc": + false; // default=asc + + const String* key_var=key_var_name.is_empty()? 0 : &key_var_name; + const String* value_var=value_var_name.is_empty()? 0 : &value_var_name; + VMethodFrame* context=r.get_method_frame()->caller(); + + VHash& self=GET_SELF(r, VHash); + HashStringValue& hash=self.hash_ro(); + int count=hash.count(); + VHash_lock lock(self); + + Hash_seq_item* seq=new(PointerFreeGC) Hash_seq_item[count]; + int pos=0; + bool key_values_are_strings=true; + + 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))); + if(value_var) + r.put_element(*context, *value_var, i.value()); + + Value& value=r.process_to_value(key_maker); + if(pos==0) // determining key values type by first one + key_values_are_strings=value.is_string(); + + seq[pos].hash_pair=i.pair(); + if(key_values_are_strings) + seq[pos].value.c_str=value.as_string().cstr(); + else + seq[pos].value.d=value.as_expr_result().as_double(); + } + + // @todo: handle this elsewhere + if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings) + for(pos=0; pos=0; pos--) + hash.order_next(seq[pos].hash_pair); + else + for(pos=0; pos 1) { + const String& stype=params.as_string(1, "type must be string"); + if(stype == "key") + result_type=AtResultTypeKey; + else if(stype == "hash") + result_type=AtResultTypeHash; + else if(stype != "value") + throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'"); + } + Value& vwhence=*params.get(0); - if(vwhence.is_string()){ + if(vwhence.is_string()) { const String& swhence=*vwhence.get_string(); if(swhence == "last") pos=count-1; @@ -483,18 +604,63 @@ static void _at(Request& r, MethodParams } if(count && pos >= 0 && (size_t)pos < count){ - if(pos == 0) - r.write_assign_lang(*hash.first_value()); - else if((size_t)pos == count-1) - r.write_assign_lang(*hash.last_value()); - else - for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) - if(!pos){ - r.write_assign_lang(*i.value()); + switch(result_type) { + case AtResultTypeKey: + { +#ifdef HASH_ORDER + if(pos == 0) { + r.write_assign_lang(*new VString(*new String(hash.first_key(), String::L_TAINTED))); + } else if((size_t)pos == count-1) { + r.write_assign_lang(*new VString(*new String(hash.last_key(), String::L_TAINTED))); + } else +#endif + { + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + r.write_assign_lang(*new VString(*new String(i.key(), String::L_TAINTED))); + break; + } + } + break; + } + case AtResultTypeValue: + { +#ifdef HASH_ORDER + if(pos == 0) { + r.write_assign_lang(*hash.first_value()); + } else if((size_t)pos == count-1) { + r.write_assign_lang(*hash.last_value()); + } else +#endif + { + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + r.write_assign_lang(*i.value()); + break; + } + } + break; + } + case AtResultTypeHash: + { +#ifdef HASH_ORDER + if(pos == 0) { + r.write_no_lang(SingleElementHash(hash.first_key(), hash.first_value())); + } else if((size_t)pos == count-1) { + r.write_no_lang(SingleElementHash(hash.last_key(), hash.last_value())); + } else +#endif + { + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + r.write_no_lang(SingleElementHash(i.key(), i.value())); + break; + } + } break; } + } } - } // constructor @@ -515,7 +681,7 @@ MHash::MHash(): Methoded("hash") add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1); // ^a.delete[key] - add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1); + add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1); // ^a.contains[key] add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1); @@ -526,15 +692,27 @@ MHash::MHash(): Methoded("hash") add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); // ^hash._keys[[column name]] - add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1); + add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1); // ^hash._count[] - add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); + add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); // ^hash.foreach[key;value]{code}[delim] add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); - // ^hash._at[first|last] - // ^hash._at([-]offset) - add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1); + // ^hash.sort[key;value]{string-key-maker}[[asc|desc]] + // ^hash.sort[key;value](numeric-key-maker)[[asc|desc]] + add_native_method("sort", Method::CT_DYNAMIC, _sort, 2+1, 2+1+1); + + // ^hash._at[first|last[;'key'|'value'|'hash']] + // ^hash._at([-+]offset)[['key'|'value'|'hash']] + add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2); + +#ifdef FEATURE_GET_ELEMENT4CALL + // aliases without "_" + add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1); + add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0); + add_native_method("at", Method::CT_DYNAMIC, _at, 1, 2); +#endif + }