|
|
| version 1.259, 2024/10/27 17:50:59 | version 1.269, 2026/11/03 12:25:52 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b string parser class. | Parser: @b string parser class. |
| Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| Line 65 static const String match_var_name(MATCH | Line 65 static const String match_var_name(MATCH |
| // methods | // methods |
| static void _length(Request& r, MethodParams&) { | static void _length(Request& r, MethodParams&) { |
| double result=GET_SELF(r, VString).string().length(r.charsets.source()); | double result=(double)GET_SELF(r, VString).string().length(r.charsets.source()); |
| r.write(*new VDouble(result)); | r.write(*new VDouble(result)); |
| } | } |
| static void _int(Request& r, MethodParams& params) { | static void _int(Request& r, MethodParams& params) { |
| const String& self_string=GET_SELF(r, VString).string(); | const String& self_string=GET_SELF(r, VString).string(); |
| int converted; | |
| if(self_string.is_empty()) { | if(self_string.is_empty()) { |
| if(params.count()>0) | if(params.count()>0) |
| converted=params.as_int(0, "default must be int", r); // (default) | r.write(*new VInt(params.as_wint(0, "default must be int", r))); // (default) |
| else | else |
| throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified"); | throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified"); |
| } else { | } else { |
| try { | try { |
| converted=self_string.as_int(); | r.write(*new VInt(self_string.as_wint())); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(params.count()>0) | if(params.count()>0) |
| converted=params.as_int(0, "default must be int", r); // (default) | r.write(*new VInt(params.as_wint(0, "default must be int", r))); // (default) |
| else | else |
| rethrow; // we have a problem when no default | rethrow; // we have a problem when no default |
| } | } |
| } | } |
| r.write(*new VInt(converted)); | |
| } | } |
| static void _double(Request& r, MethodParams& params) { | static void _double(Request& r, MethodParams& params) { |
| Line 161 static void _left(Request& r, MethodPara | Line 158 static void _left(Request& r, MethodPara |
| static void _right(Request& r, MethodParams& params) { | static void _right(Request& r, MethodParams& params) { |
| int sn=params.as_int(0, "n must be int", r); | int sn=params.as_int(0, "n must be int", r); |
| if(sn>0){ | if(sn>0) |
| size_t n=sn; | r.write(GET_SELF(r, VString).string().right(r.charsets.source(), sn)); |
| const String& string=GET_SELF(r, VString).string(); | |
| size_t length=string.length(r.charsets.source()); | |
| r.write(n<length ? string.mid(r.charsets.source(), length-n, length, length) : string); | |
| } | |
| } | } |
| static void _mid(Request& r, MethodParams& params) { | static void _mid(Request& r, MethodParams& params) { |
| Line 176 static void _mid(Request& r, MethodParam | Line 169 static void _mid(Request& r, MethodParam |
| if(begin<0) | if(begin<0) |
| throw Exception(PARSER_RUNTIME, 0, "p(%d) must be >=0", begin); | throw Exception(PARSER_RUNTIME, 0, "p(%d) must be >=0", begin); |
| size_t end; | size_t end=string.length(); // to the end of the string (the length in bytes is not less than in characters) |
| size_t length=0; | |
| if(params.count()>1) { | if(params.count()>1) { |
| int n=params.as_int(1, "n must be int", r); | int n=params.as_int(1, "n must be int", r); |
| if(n<0) | if(n<0) |
| throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n); | throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n); |
| end=begin+n; | end=begin+n; |
| } else { | |
| length=string.length(r.charsets.source()); | |
| end=length; | |
| } | } |
| r.write(string.mid(r.charsets.source(), begin, end, length)); | r.write(string.mid(r.charsets.source(), begin, end)); |
| } | } |
| static void _pos(Request& r, MethodParams& params) { | static void _pos(Request& r, MethodParams& params) { |
| Line 206 static void _pos(Request& r, MethodParam | Line 195 static void _pos(Request& r, MethodParam |
| r.write(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), offset))); | r.write(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), offset))); |
| } | } |
| static void _rpos(Request& r, MethodParams& params) { | |
| Value& substr=params.as_no_junction(0, "substr must not be code"); | |
| const String& string=GET_SELF(r, VString).string(); | |
| size_t offset=string.length(); // from the very end | |
| if(params.count()>1){ | |
| int n=params.as_int(1, "n must be int", r); | |
| if(n<0) | |
| throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n); | |
| offset=n; | |
| } | |
| r.write(*new VInt((int)string.rpos(r.charsets.source(), substr.as_string(), offset))); | |
| } | |
| struct Split_action_info { | struct Split_action_info { |
| const String& src; | const String& src; |
| ArrayString &result; | ArrayString &result; |
| Line 641 static void _save(Request& r, MethodPara | Line 645 static void _save(Request& r, MethodPara |
| } | } |
| } | } |
| const String& file_name=params.as_string(file_name_index, FILE_NAME_MUST_BE_STRING); | const String& file_name=params.as_file_name(file_name_index); |
| const String& src=GET_SELF(r, VString).string(); | const String& src=GET_SELF(r, VString).string(); |
| String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); | String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); |
| Line 690 static void _trim(Request& r, MethodPara | Line 694 static void _trim(Request& r, MethodPara |
| Base64Options base64_encode_options(Request& r, HashStringValue* options) { | Base64Options base64_encode_options(Request& r, HashStringValue* options) { |
| Base64Options result; | Base64Options result; |
| if(options) { | if(options) { |
| int valid_options=0; | |
| for(HashStringValue::Iterator i(*options); i; i.next()) { | for(HashStringValue::Iterator i(*options); i; i.next()) { |
| String::Body key=i.key(); | String::Body key=i.key(); |
| Value* value=i.value(); | Value* value=i.value(); |
| if(key == "pad") { | if(key == "pad") { |
| result.pad=r.process(*value).as_bool(); | result.pad=r.process(*value).as_bool(); |
| valid_options++; | |
| } else if(key == "wrap") { | } else if(key == "wrap") { |
| result.wrap=r.process(*value).as_bool(); | result.wrap=r.process(*value).as_bool(); |
| valid_options++; | |
| } else if(key == "url-safe") { | } else if(key == "url-safe") { |
| if(r.process(*value).as_bool()) | if(r.process(*value).as_bool()) |
| result.set_url_safe_abc(); | result.set_url_safe_abc(); |
| valid_options++; | } else |
| } | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| if(valid_options != options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | } |
| return result; | return result; |
| } | } |
| Line 716 Base64Options base64_encode_options(Requ | Line 714 Base64Options base64_encode_options(Requ |
| Base64Options base64_decode_options(Request& r, HashStringValue* options) { | Base64Options base64_decode_options(Request& r, HashStringValue* options) { |
| Base64Options result; | Base64Options result; |
| if(options) { | if(options) { |
| int valid_options=0; | |
| for(HashStringValue::Iterator i(*options); i; i.next() ) { | for(HashStringValue::Iterator i(*options); i; i.next() ) { |
| String::Body key=i.key(); | String::Body key=i.key(); |
| Value* value=i.value(); | Value* value=i.value(); |
| if(key == "pad") { | if(key == "pad") { |
| result.pad=r.process(*value).as_bool(); | result.pad=r.process(*value).as_bool(); |
| valid_options++; | |
| } else if(key == "strict") { | } else if(key == "strict") { |
| result.strict=r.process(*value).as_bool(); | result.strict=r.process(*value).as_bool(); |
| valid_options++; | |
| } else if(key == "url-safe") { | } else if(key == "url-safe") { |
| if(r.process(*value).as_bool()) | if(r.process(*value).as_bool()) |
| result.set_url_safe_abc(); | result.set_url_safe_abc(); |
| valid_options++; | } else |
| } | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| if(valid_options != options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | } |
| return result; | return result; |
| } | } |
| Line 792 static void _js_unescape(Request& r, Met | Line 784 static void _js_unescape(Request& r, Met |
| } | } |
| static void _unescape(Request& r, MethodParams& params){ | static void _unescape(Request& r, MethodParams& params){ |
| const String& mode=params.as_string(0, MODE_MUST_NOT_BE_CODE); | const String& mode=params.as_string(0, MODE_MUST_BE_STRING); |
| const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING); | const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING); |
| Charset* from_charset=&r.charsets.client(); | Charset* from_charset=&r.charsets.client(); |
| Line 859 MString::MString(): Methoded("string") { | Line 851 MString::MString(): Methoded("string") { |
| add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); | add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); |
| // ^string.pos[substr] | // ^string.pos[substr] |
| // ^string.pos[substr](n) | // ^string.pos[substr](n) n is the position from which to search |
| add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2); | add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2); |
| add_native_method("lpos", Method::CT_DYNAMIC, _pos, 1, 2); | |
| // ^string.rpos[substr] | |
| // ^string.rpos[substr](n) n is the maximum position of the substr start, not substr end | |
| add_native_method("rpos", Method::CT_DYNAMIC, _rpos, 1, 2); | |
| // ^string.split[delim] | // ^string.split[delim] |
| // ^string.split[delim][options] | // ^string.split[delim][options] |