|
|
| version 1.217, 2015/05/16 22:24:38 | version 1.224, 2016/03/31 21:46:20 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b string parser class. | Parser: @b string parser class. |
| Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 31 public: | Line 31 public: |
| // global variable | // global variable |
| DECLARE_CLASS_VAR(string, new MString, 0); | DECLARE_CLASS_VAR(string, new MString); |
| // void class, inherited from string and thus should be inited afterwards | // void class, inherited from string and thus should be inited afterwards |
| Line 42 public: | Line 42 public: |
| // void global variable should be after string global variable | // void global variable should be after string global variable |
| DECLARE_CLASS_VAR(void, new MVoid, 0); | DECLARE_CLASS_VAR(void, new MVoid); |
| // defines for statics | // defines for statics |
| Line 53 DECLARE_CLASS_VAR(void, new MVoid, 0); | Line 53 DECLARE_CLASS_VAR(void, new MVoid, 0); |
| #define MODE_APPEND "append" | #define MODE_APPEND "append" |
| #define UNESCAPE_MODE_JS "js" | |
| #define UNESCAPE_MODE_URI "uri" | |
| // statics | // statics |
| static const String match_var_name(MATCH_VAR_NAME); | static const String match_var_name(MATCH_VAR_NAME); |
| Line 89 static void _int(Request& r, MethodParam | Line 92 static void _int(Request& r, MethodParam |
| static void _double(Request& r, MethodParams& params) { | static void _double(Request& r, MethodParams& params) { |
| const String& self_string=GET_SELF(r, VString).string(); | const String& self_string=GET_SELF(r, VString).string(); |
| double converted; | |
| if(self_string.is_empty()) { | if(self_string.is_empty()) { |
| if(params.count()>0) | if(params.count()>0) |
| converted=params.as_double(0, "default must be double", r); // (default) | r.write_no_lang(*new VDouble(params.as_double(0, "default must be double", 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_double(); | r.write_no_lang(*new VDouble(self_string.as_double())); |
| } catch(...) { // convert problem | } catch(...) { // convert problem |
| if(params.count()>0) | if(params.count()>0) |
| converted=params.as_double(0, "default must be double", r); // (default) | r.write_no_lang(*new VDouble(params.as_double(0, "default must be double", r))); // (default) |
| else | else |
| rethrow; // we have a problem when no default | rethrow; // we have a problem when no default |
| } | } |
| } | } |
| r.write_no_lang(*new VDouble(converted)); | |
| } | } |
| static void _bool(Request& r, MethodParams& params) { | static void _bool(Request& r, MethodParams& params) { |
| Line 678 static void _trim(Request& r, MethodPara | Line 678 static void _trim(Request& r, MethodPara |
| kind=String::TRIM_START; | kind=String::TRIM_START; |
| else if(skind==TRIM_END_OPTION || skind=="end") | else if(skind==TRIM_END_OPTION || skind=="end") |
| kind=String::TRIM_END; | kind=String::TRIM_END; |
| else if(params_count==1) | |
| chars=skind.cstr(); | |
| else | else |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &skind, | &skind, |
| Line 695 static void _trim(Request& r, MethodPara | Line 697 static void _trim(Request& r, MethodPara |
| } | } |
| static void _base64(Request& r, MethodParams& params) { | static void _base64(Request& r, MethodParams& params) { |
| if(params.count()) { | if(&r.get_self() == string_class) { |
| // decode: ^string:base64[encoded[;$.strict(true|false)]] | // decode: ^string:base64[encoded[;$.strict(true|false)]] |
| const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr(); | const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : ""; |
| char* decoded=0; | char* decoded=0; |
| size_t length=0; | size_t length=0; |
| Line 734 static void _base64(Request& r, MethodPa | Line 736 static void _base64(Request& r, MethodPa |
| } | } |
| static void _idna(Request& r, MethodParams& params) { | static void _idna(Request& r, MethodParams& params) { |
| if(params.count()) { | if(&r.get_self() == string_class) { |
| // decode: ^string:idna[encoded] | // decode: ^string:idna[encoded] |
| const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr(); | const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : ""; |
| r.write_assign_lang(*new String(pa_idna_decode(cstr, r.charsets.source()), String::L_TAINTED)); | r.write_assign_lang(*new String(pa_idna_decode(cstr, r.charsets.source()), String::L_TAINTED)); |
| } else { | } else { |
| // encode: ^str.idna[] | // encode: ^str.idna[] |
| Line 746 static void _idna(Request& r, MethodPara | Line 748 static void _idna(Request& r, MethodPara |
| } | } |
| } | } |
| static void _escape(Request& r, MethodParams&){ | static void _js_escape(Request& r, MethodParams&){ |
| const String& src=GET_SELF(r, VString).string(); | const String& src=GET_SELF(r, VString).string(); |
| r.write_assign_lang(src.escape(r.charsets.source())); | r.write_assign_lang(src.escape(r.charsets.source())); |
| } | } |
| static void _unescape(Request& r, MethodParams& params){ | static void _js_unescape(Request& r, MethodParams& params){ |
| const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING); | const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING); |
| if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true)) | if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true)) |
| r.write_assign_lang(*new String(result, String::L_TAINTED)); | r.write_assign_lang(*new String(result, String::L_TAINTED)); |
| } | } |
| static void _unescape(Request& r, MethodParams& params){ | |
| const String& mode=params.as_string(0, MODE_MUST_NOT_BE_CODE); | |
| const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING); | |
| Charset* from_charset=&r.charsets.client(); | |
| if(params.count() > 2) | |
| if(HashStringValue* options=params.as_hash(2)) { | |
| int valid_options=0; | |
| if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){ | |
| from_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER)); | |
| valid_options++; | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | |
| bool mode_js; | |
| if(mode==UNESCAPE_MODE_JS){ | |
| mode_js=true; | |
| } else if(mode==UNESCAPE_MODE_URI){ | |
| mode_js=false; | |
| } else { | |
| throw Exception(PARSER_RUNTIME, &mode, "is invalid mode, must be either '"UNESCAPE_MODE_JS"' or '"UNESCAPE_MODE_URI"'"); | |
| } | |
| const char* unescaped=unescape_chars(src.cstr(), src.length(), from_charset, mode_js); | |
| if(*unescaped){ | |
| const String* result=new String(Charset::transcode(unescaped, *from_charset, r.charsets.source()), String::L_TAINTED); | |
| r.write_assign_lang(*result); | |
| } | |
| } | |
| // constructor | // constructor |
| MString::MString(): Methoded("string") { | MString::MString(): Methoded("string") { |
| Line 835 MString::MString(): Methoded("string") { | Line 870 MString::MString(): Methoded("string") { |
| add_native_method("idna", Method::CT_ANY, _idna, 0, 1); | add_native_method("idna", Method::CT_ANY, _idna, 0, 1); |
| // ^string.js-escape[] | // ^string.js-escape[] |
| add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0); | add_native_method("js-escape", Method::CT_DYNAMIC, _js_escape, 0, 0); |
| // ^string:js-unescape[escaped%uXXXXstring] | // ^string:js-unescape[escaped%uXXXXstring] |
| add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1); | add_native_method("js-unescape", Method::CT_STATIC, _js_unescape, 1, 1); |
| } | |
| // ^string:unescape[js|uri;escaped;$.charset[...]] | |
| add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3); | |
| } |