--- parser3/src/classes/hash.C 2009/08/08 13:30:20 1.103 +++ parser3/src/classes/hash.C 2010/08/04 15:11:44 1.105 @@ -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/08/04 15:11:44 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -334,13 +334,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 +470,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 && pos < count){ + if(pos == 0) + r.write_assign_lang(*hash.first_value()); + else if(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 +542,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); }