--- parser3/src/classes/hash.C 2009/07/06 08:49:49 1.97 +++ parser3/src/classes/hash.C 2015/03/12 08:18:18 1.121 @@ -1,12 +1,10 @@ /** @file Parser: @b hash parser class. - Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HASH_C="$Date: 2009/07/06 08:49:49 $"; - #include "classes.h" #include "pa_vmethod_frame.h" @@ -18,26 +16,22 @@ static const char * const IDENT_HASH_C=" #include "pa_vbool.h" #include "pa_vmethod_frame.h" +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.121 2015/03/12 08:18:18 misha Exp $"; + // class class MHash: public Methoded { public: // VStateless_class - Value* create_new_value(Pool&, HashStringValue*) { return new VHash(); } + Value* create_new_value(Pool&) { return new VHash(); } public: MHash(); -public: // Methoded - bool used_directly() { return true; } }; // global variable DECLARE_CLASS_VAR(hash, new MHash, 0); -// externs - -extern String cycle_data_name; - // methods #ifndef DOXYGEN @@ -74,9 +68,9 @@ public: empty(0) { } - bool add_column(SQL_Error& error, const char* str, size_t length) { + bool add_column(SQL_Error& error, const char* str, size_t ) { try { - columns+=new String(str, String::L_TAINTED, length); + columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); return false; } catch(...) { error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); @@ -113,9 +107,9 @@ public: return false; } - bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) { + bool add_row_cell(SQL_Error& error, const char *str, size_t ) { try { - String& cell=*new String(ptr, String::L_TAINTED, length); + 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) { @@ -197,31 +191,31 @@ VBool Hash_sql_event_handlers::only_one_ static void _create_or_add(Request& r, MethodParams& params) { if(params.count()) { - Value& vsrc=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* src=vsrc.get_hash()) { - VHash& self=GET_SELF(r, VHash); - HashStringValue* self_hash=&(self.hash()); - if(src==self_hash) // same: doing nothing + Value& vsrc=params.as_no_junction(0, PARAM_MUST_BE_HASH); + VHash& self=GET_SELF(r, VHash); + HashStringValue* src_hash; + HashStringValue* self_hash=&(self.hash()); + + if(VHash* src=static_cast(vsrc.as(VHASH_TYPE))) { + src_hash=&(src->hash_ro()); + + if(src_hash==self_hash) // same: doing nothing return; - src->for_each(copy_all_overwrite_to, self_hash); - if(VHash* vhash_src=static_cast(vsrc.as(VHASH_TYPE, false))) - { - if(Value* vdefault=vhash_src->get_default()) - { - if(vdefault->is_defined()) - { - self.set_default(vdefault); - } - } - } + if(Value* vdefault=src->get_default()) + if(vdefault->is_defined()) + self.set_default(vdefault); + } else { + src_hash=vsrc.get_hash(); } + + if(src_hash) + src_hash->for_each(copy_all_overwrite_to, self_hash); } } static void _sub(Request& r, MethodParams& params) { - Value& vsrc=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* src=vsrc.get_hash()) { + if(HashStringValue* src=params.as_hash(0, "param")) { HashStringValue* self=&(GET_SELF(r, VHash).hash()); if(src==self) { // same: clearing self->clear(); @@ -232,17 +226,16 @@ static void _sub(Request& r, MethodParam } static void copy_all_dontoverwrite_to( - HashStringValue::key_type key, - HashStringValue::value_type value, - HashStringValue* dest) { + HashStringValue::key_type key, + HashStringValue::value_type value, + HashStringValue* dest) { dest->put_dont_replace(key, value); } static void _union(Request& r, MethodParams& params) { // dest = copy of self Value& result=*new VHash(GET_SELF(r, VHash).hash()); // dest += b - Value& vsrc=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* src=vsrc.get_hash()) + if(HashStringValue* src=params.as_hash(0, "param")) src->for_each(copy_all_dontoverwrite_to, result.get_hash()); // return result @@ -256,17 +249,16 @@ struct Copy_intersection_to_info { }; #endif static void copy_intersection_to( - HashStringValue::key_type key, - HashStringValue::value_type value, - Copy_intersection_to_info *info) { + HashStringValue::key_type key, + HashStringValue::value_type value, + Copy_intersection_to_info *info) { if(info->b->get(key)) info->dest->put_dont_replace(key, value); } static void _intersection(Request& r, MethodParams& params) { Value& result=*new VHash; // dest += b - Value& vb=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* b=vb.get_hash()) { + if(HashStringValue* b=params.as_hash(0, "param")) { Copy_intersection_to_info info={b, result.get_hash()}; GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info); } @@ -276,18 +268,23 @@ static void _intersection(Request& r, Me } static bool intersects( - HashStringValue::key_type key, - HashStringValue::value_type /*value*/, - HashStringValue* b) { + HashStringValue::key_type key, + HashStringValue::value_type /*value*/, + HashStringValue* b) { return b->get(key)!=0; } static void _intersects(Request& r, MethodParams& params) { bool result=false; - Value& vb=params.as_no_junction(0, "param must be hash"); - if(HashStringValue* b=vb.get_hash()) - 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)); @@ -312,40 +309,32 @@ static void _sql(Request& r, MethodParam ulong offset=0; bool distinct=false; Table2hash_value_type value_type=C_HASH; - if(params.count()>1) { - Value& voptions=params.as_no_junction(1, "options must be hash, not code"); - if(voptions.is_defined() && !voptions.is_string()) - if(HashStringValue* options=voptions.get_hash()) { - int valid_options=0; - if(Value* vbind=options->get(sql_bind_name)) { - valid_options++; - bind=vbind->get_hash(); - } - if(Value* vlimit=options->get(sql_limit_name)) { - valid_options++; - limit=(ulong)r.process_to_value(*vlimit).as_double(); - } - if(Value* voffset=options->get(sql_offset_name)) { - valid_options++; - offset=(ulong)r.process_to_value(*voffset).as_double(); - } - if(Value* vdistinct=options->get(sql_distinct_name)) { - valid_options++; - distinct=r.process_to_value(*vdistinct).as_bool(); - } - if(Value* vvalue_type=options->get(sql_value_type_name)) { - valid_options++; - value_type=get_value_type(r.process_to_value(*vvalue_type)); - } - if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, - 0, - "called with invalid option"); - } else - throw Exception(PARSER_RUNTIME, - 0, - "options must be hash"); - } + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1, "sql options")) { + int valid_options=0; + if(Value* vbind=options->get(sql_bind_name)) { + valid_options++; + bind=vbind->get_hash(); + } + if(Value* vlimit=options->get(sql_limit_name)) { + valid_options++; + limit=(ulong)r.process_to_value(*vlimit).as_double(); + } + if(Value* voffset=options->get(sql_offset_name)) { + valid_options++; + offset=(ulong)r.process_to_value(*voffset).as_double(); + } + if(Value* vdistinct=options->get(sql_distinct_name)) { + valid_options++; + distinct=r.process_to_value(*vdistinct).as_bool(); + } + if(Value* vvalue_type=options->get(sql_value_type_name)) { + valid_options++; + value_type=get_value_type(r.process_to_value(*vvalue_type)); + } + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } SQL_Driver::Placeholder* placeholders=0; uint placeholders_count=0; @@ -354,7 +343,7 @@ static void _sql(Request& r, MethodParam Temp_lang temp_lang(r, String::L_SQL); const String& statement_string=r.process_to_string(statement); - const char* statement_cstr=statement_string.cstr(String::L_UNSPECIFIED, r.connection()); + const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection()); HashStringValue& hash=GET_SELF(r, VHash).hash(); hash.clear(); @@ -376,10 +365,10 @@ static void _sql(Request& r, MethodParam } static void keys_collector( - HashStringValue::key_type key, - HashStringValue::value_type, - Table *table) { - Table::element_type row(new ArrayString); + HashStringValue::key_type key, + HashStringValue::value_type, + Table *table) { + Table::element_type row(new ArrayString(1)); *row+=new String(key, String::L_TAINTED); *table+=row; } @@ -390,26 +379,28 @@ static void _keys(Request& r, MethodPara else keys_column_name=new String("key"); - Table::columns_type columns(new ArrayString); + Table::columns_type columns(new ArrayString(1)); *columns+=keys_column_name; Table* table=new Table(columns); - GET_SELF(r, VHash).hash().for_each(keys_collector, table); + GET_SELF(r, VHash).hash_ro().for_each(keys_collector, table); r.write_no_lang(*new VTable(table)); } static void _count(Request& r, MethodParams&) { - r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count())); + r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash_ro().count())); } 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) { - bool result=GET_SELF(r, VHash).hash().contains(params.as_string(0, "key must be string")); + bool result=GET_SELF(r, VHash).hash_ro().contains(params.as_string(0, "key must be string")); r.write_no_lang(VBool::get(result)); } @@ -432,10 +423,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(var_context, *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(var_context, *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); @@ -457,8 +448,7 @@ static bool one_foreach_cycle( } } static void _foreach(Request& r, MethodParams& params) { - Temp_hash_value - cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r); + InCycle temp(r); 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"); @@ -474,11 +464,109 @@ static void _foreach(Request& r, MethodP }; VHash& self=GET_SELF(r, VHash); - HashStringValue& hash=self.hash(); + HashStringValue& hash=self.hash_ro(); VHash_lock lock(self); 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; +} + +static void _at(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHash).hash_ro(); + 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_to_value can be called) even more. + AtResultType result_type=AtResultTypeValue; + if(params.count() > 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()) { + 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_to_value(vwhence).as_int(); + if(pos < 0) + pos+=count; + } + + if(count && pos >= 0 && (size_t)pos < count){ + switch(result_type) { + case AtResultTypeKey: + { + 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 { + 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: + { + 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()); + break; + } + } + break; + } + case AtResultTypeHash: + { + 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 { + for(HashStringValue::Iterator i(hash); i; i.next(), pos-- ) + if(!pos){ + r.write_no_lang(SingleElementHash(i.key(), i.value())); + break; + } + } + break; + } + } + } +} + // constructor MHash::MHash(): Methoded("hash") @@ -497,7 +585,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); @@ -515,4 +603,8 @@ MHash::MHash(): Methoded("hash") // ^hash.foreach[key;value]{code}[delim] add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 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); }