|
|
| version 1.127, 2003/09/29 10:09:35 | version 1.136, 2004/03/01 08:54:16 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b string parser class. | 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 <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_STRING_C="$Date$"; | static const char * const IDENT_STRING_C="$Date$"; |
| #include "classes.h" | #include "classes.h" |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| Line 36 DECLARE_CLASS_VAR(string, new MString, 0 | Line 36 DECLARE_CLASS_VAR(string, new MString, 0 |
| // defines for statics | // defines for statics |
| #define MATCH_VAR_NAME "match" | #define MATCH_VAR_NAME "match" |
| #define TRIM_START_OPTION "start" | |
| #define TRIM_BOTH_OPTION "both" | |
| #define TRIM_END_OPTION "end" | |
| // statics | // statics |
| Line 132 static void _pos(Request& r, MethodParam | Line 135 static void _pos(Request& r, MethodParam |
| r.write_assign_lang(*new VInt((int)string.pos(substr.as_string()))); | r.write_assign_lang(*new VInt((int)string.pos(substr.as_string()))); |
| } | } |
| static void split_list(Request& r, | static void split_list(MethodParams& params, int paramIndex, |
| MethodParams& params, int paramIndex, | |
| const String& string, | const String& string, |
| ArrayString& result) { | ArrayString& result) { |
| Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code"); | Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code"); |
| Line 158 static int split_options(const String* o | Line 160 static int split_options(const String* o |
| {"r", "R", SPLIT_RIGHT, SPLIT_LEFT}, | {"r", "R", SPLIT_RIGHT, SPLIT_LEFT}, |
| {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL}, | {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL}, |
| {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL}, | {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL}, |
| {0} | {0, 0, 0, 0} |
| }; | }; |
| int result=0; | int result=0; |
| Line 177 static int split_options(const String* o | Line 179 static int split_options(const String* o |
| return result; | return result; |
| } | } |
| static Table& split_vertical(Request& r, ArrayString& pieces, bool right) { | static Table& split_vertical(ArrayString& pieces, bool right) { |
| Table::columns_type columns(new ArrayString); | Table::columns_type columns(new ArrayString); |
| *columns+=new String("piece"); | *columns+=new String("piece"); |
| Line 201 static Table& split_vertical(Request& r, | Line 202 static Table& split_vertical(Request& r, |
| return table; | return table; |
| } | } |
| static Table& split_horizontal(Request& r, ArrayString& pieces, bool right) { | static Table& split_horizontal(ArrayString& pieces, bool right) { |
| Table& table=*new Table(Table::columns_type(0) /* nameless */); | Table& table=*new Table(Table::columns_type(0) /* nameless */); |
| Table::element_type row(new ArrayString(pieces.count())); | Table::element_type row(new ArrayString(pieces.count())); |
| if(right) { // right | if(right) { // right |
| for(size_t i=pieces.count(); --i>=0; ) | for(int i=pieces.count(); --i>=0; ) |
| *row+=pieces[i]; | *row+=pieces[i]; |
| } else { // left | } else { // left |
| for(Array_iterator<const String*> i(pieces); i.has_next(); ) | for(Array_iterator<const String*> i(pieces); i.has_next(); ) |
| Line 221 static void split_with_options(Request& | Line 222 static void split_with_options(Request& |
| const String& string=GET_SELF(r, VString).string(); | const String& string=GET_SELF(r, VString).string(); |
| ArrayString pieces; | ArrayString pieces; |
| split_list(r, params, 0, string, pieces); | split_list(params, 0, string, pieces); |
| if(!bits) { | if(!bits) { |
| const String* options=0; | const String* options=0; |
| Line 233 static void split_with_options(Request& | Line 234 static void split_with_options(Request& |
| bool right=(bits & SPLIT_RIGHT) != 0; | bool right=(bits & SPLIT_RIGHT) != 0; |
| bool horizontal=(bits & SPLIT_HORIZONTAL) !=0; | bool horizontal=(bits & SPLIT_HORIZONTAL) !=0; |
| Table& table=horizontal?split_horizontal(r, pieces, right) | Table& table=horizontal?split_horizontal(pieces, right) |
| :split_vertical(r, pieces, right); | :split_vertical(pieces, right); |
| r.write_no_lang(*new VTable(&table)); | r.write_no_lang(*new VTable(&table)); |
| } | } |
| Line 312 static void _match(Request& r, MethodPar | Line 313 static void _match(Request& r, MethodPar |
| String result; | String result; |
| VTable* vtable=new VTable; | VTable* vtable=new VTable; |
| Replace_action_info info={0}; | Replace_action_info info={ |
| info.request=&r; | &r, |
| info.src=&src; | &src, |
| info.dest=&result; | &result, |
| info.vtable=vtable; | vtable, |
| info.replacement_code=&replacement_code; | &replacement_code |
| }; | |
| Temp_value_element temp_match_var( | Temp_value_element temp_match_var( |
| *replacement_code.get_junction()->method_frame, | *replacement_code.get_junction()->method_frame, |
| match_var_name, vtable); | match_var_name, vtable); |
| Line 329 static void _match(Request& r, MethodPar | Line 331 static void _match(Request& r, MethodPar |
| } | } |
| } | } |
| static void change_case(Request& r, MethodParams& params, | static void change_case(Request& r, MethodParams&, |
| String::Change_case_kind kind) { | String::Change_case_kind kind) { |
| const String& src=GET_SELF(r, VString).string(); | const String& src=GET_SELF(r, VString).string(); |
| Line 357 public: | Line 359 public: |
| got_cell(false), | got_cell(false), |
| result(*new String) {} | result(*new String) {} |
| bool add_column(SQL_Error& error, const char* str, size_t /*length*/) { | bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) { |
| if(got_column) { | if(got_column) { |
| error=SQL_Error("parser.runtime", | error=SQL_Error("parser.runtime", |
| //statement_string, | //statement_string, |
| Line 402 const String* sql_result_string(Request& | Line 404 const String* sql_result_string(Request& |
| if(params.count()>1) { | if(params.count()>1) { |
| Value& voptions=params.as_no_junction(1, "options must be hash, not code"); | Value& voptions=params.as_no_junction(1, "options must be hash, not code"); |
| if(!voptions.is_string()) | if(!voptions.is_string()) |
| if(options=voptions.get_hash()) { | if((options=voptions.get_hash())) { |
| if(Value* vlimit=options->get(sql_limit_name)) | if(Value* vlimit=options->get(sql_limit_name)) |
| limit=(ulong)r.process_to_value(*vlimit).as_double(); | limit=(ulong)r.process_to_value(*vlimit).as_double(); |
| if(Value* voffset=options->get(sql_offset_name)) | if(Value* voffset=options->get(sql_offset_name)) |
| offset=(ulong)r.process_to_value(*voffset).as_double(); | offset=(ulong)r.process_to_value(*voffset).as_double(); |
| if(default_code=options->get(sql_default_name)) { | if((default_code=options->get(sql_default_name))) { |
| if(Junction* default_junction=default_code->get_junction()) | if(!default_code->get_junction()) |
| ;//default_junction->change_context(statement.get_junction()); | |
| else | |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| 0, | 0, |
| "default option must be code"); | "default option must be code"); |
| Line 497 static void _normalize(Request& r, Metho | Line 497 static void _normalize(Request& r, Metho |
| r.write_assign_lang(src); | 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 | // constructor |
| MString::MString(): Methoded("string") { | MString::MString(): Methoded("string") { |
| Line 553 MString::MString(): Methoded("string") { | Line 585 MString::MString(): Methoded("string") { |
| // ^string.normalize[] | // ^string.normalize[] |
| add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0); | 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); | |
| } | } |