--- parser3/src/classes/hash.C 2009/08/08 13:30:20 1.103 +++ parser3/src/classes/hash.C 2010/10/21 15:06:27 1.108 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HASH_C="$Date: 2009/08/08 13:30:20 $"; +static const char * const IDENT_HASH_C="$Date: 2010/10/21 15:06:27 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -26,8 +26,6 @@ public: // VStateless_class public: MHash(); -public: // Methoded - bool used_directly() { return true; } }; // global variable @@ -72,7 +70,7 @@ public: bool add_column(SQL_Error& error, const char* str, size_t length) { 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"); @@ -111,7 +109,7 @@ public: bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) { try { - String& cell=*new String(ptr, String::L_TAINTED, length); + String& cell=*new String(ptr, String::L_TAINTED /* no length as 0x00 can be inside */); bool duplicate=false; if(one_bool_column) { @@ -334,13 +332,9 @@ static void _sql(Request& r, MethodParam 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"); + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } else - throw Exception(PARSER_RUNTIME, - 0, - "options must be hash"); + throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH); } SQL_Driver::Placeholder* placeholders=0; @@ -474,6 +468,42 @@ static void _foreach(Request& r, MethodP hash.first_that(one_foreach_cycle, &info); } +static void _at(Request& r, MethodParams& params) { + HashStringValue& hash=GET_SELF(r, VHash).hash(); + size_t count=hash.count(); + + int pos=0; + + 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){ + 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; + } + } + +} + // constructor MHash::MHash(): Methoded("hash") @@ -510,4 +540,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] + // ^hash._at([-]offset) + add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1); }