--- parser3/src/classes/string.C 2003/11/20 16:34:23 1.129 +++ parser3/src/classes/string.C 2004/03/01 08:54:16 1.136 @@ -1,11 +1,11 @@ /** @file Parser: @b string parser class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2003/11/20 16:34:23 $"; +static const char * const IDENT_STRING_C="$Date: 2004/03/01 08:54:16 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -36,6 +36,9 @@ DECLARE_CLASS_VAR(string, new MString, 0 // defines for statics #define MATCH_VAR_NAME "match" +#define TRIM_START_OPTION "start" +#define TRIM_BOTH_OPTION "both" +#define TRIM_END_OPTION "end" // statics @@ -157,7 +160,7 @@ static int split_options(const String* o {"r", "R", SPLIT_RIGHT, SPLIT_LEFT}, {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL}, {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL}, - {0} + {0, 0, 0, 0} }; int result=0; @@ -203,7 +206,7 @@ static Table& split_horizontal(ArrayStri Table& table=*new Table(Table::columns_type(0) /* nameless */); Table::element_type row(new ArrayString(pieces.count())); if(right) { // right - for(size_t i=pieces.count(); --i>=0; ) + for(int i=pieces.count(); --i>=0; ) *row+=pieces[i]; } else { // left for(Array_iterator i(pieces); i.has_next(); ) @@ -310,12 +313,13 @@ static void _match(Request& r, MethodPar String result; VTable* vtable=new VTable; - Replace_action_info info={0}; - info.request=&r; - info.src=&src; - info.dest=&result; - info.vtable=vtable; - info.replacement_code=&replacement_code; + Replace_action_info info={ + &r, + &src, + &result, + vtable, + &replacement_code + }; Temp_value_element temp_match_var( *replacement_code.get_junction()->method_frame, match_var_name, vtable); @@ -400,15 +404,13 @@ const String* sql_result_string(Request& if(params.count()>1) { Value& voptions=params.as_no_junction(1, "options must be hash, not code"); if(!voptions.is_string()) - if(options=voptions.get_hash()) { + if((options=voptions.get_hash())) { if(Value* vlimit=options->get(sql_limit_name)) limit=(ulong)r.process_to_value(*vlimit).as_double(); if(Value* voffset=options->get(sql_offset_name)) offset=(ulong)r.process_to_value(*voffset).as_double(); - if(default_code=options->get(sql_default_name)) { - if(Junction* default_junction=default_code->get_junction()) - ;//default_junction->change_context(statement.get_junction()); - else + if((default_code=options->get(sql_default_name))) { + if(!default_code->get_junction()) throw Exception("parser.runtime", 0, "default option must be code"); @@ -495,6 +497,38 @@ static void _normalize(Request& r, Metho r.write_assign_lang(src); } +static void _trim(Request& r, MethodParams& params) { + const String& src=GET_SELF(r, VString).string(); + + String::Trim_kind kind=String::TRIM_BOTH; + const char* chars=0; + if(params.count()>0) { + const String& skind=params.as_string(0, + "'where' must be string"); + if(skind==TRIM_START_OPTION) + kind=String::TRIM_START; + else if(skind==TRIM_END_OPTION) + kind=String::TRIM_END; + else if(skind==TRIM_BOTH_OPTION) + kind=String::TRIM_BOTH; + else + throw Exception("parser.runtime", + &skind, + "'kind' should be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION); + + if(params.count()>1) { + const String& schars=params.as_string(1, "'chars' must be string"); + if(!schars.length()) + throw Exception("parser.runtime", + 0, + "'chars' must not be empty"); + chars=schars.cstr(); + } + } + + r.write_assign_lang(src.trim(kind, chars)); +} + // constructor MString::MString(): Methoded("string") { @@ -551,4 +585,7 @@ MString::MString(): Methoded("string") { // ^string.normalize[] add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0); + + // ^string.trim[[start|both|end][;chars]] + add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2); }