--- parser3/src/classes/string.C 2024/10/27 17:50:59 1.259 +++ parser3/src/classes/string.C 2026/11/01 14:41:57 1.268 @@ -1,7 +1,7 @@ /** @file 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 , Alexandr Petrosian */ @@ -22,7 +22,7 @@ #include "pa_vregex.h" #include "pa_charsets.h" -volatile const char * IDENT_STRING_C="$Id: string.C,v 1.259 2024/10/27 17:50:59 moko Exp $"; +volatile const char * IDENT_STRING_C="$Id: string.C,v 1.268 2026/11/01 14:41:57 moko Exp $"; // class @@ -65,31 +65,28 @@ static const String match_var_name(MATCH // methods 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)); } static void _int(Request& r, MethodParams& params) { const String& self_string=GET_SELF(r, VString).string(); - int converted; if(self_string.is_empty()) { 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 throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified"); } else { try { - converted=self_string.as_int(); + r.write(*new VInt(self_string.as_wint())); } catch(...) { // convert problem 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 rethrow; // we have a problem when no default } } - - r.write(*new VInt(converted)); } static void _double(Request& r, MethodParams& params) { @@ -206,6 +203,21 @@ static void _pos(Request& r, MethodParam 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 { const String& src; ArrayString &result; @@ -641,7 +653,7 @@ 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(); String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); @@ -690,25 +702,19 @@ static void _trim(Request& r, MethodPara Base64Options base64_encode_options(Request& r, HashStringValue* options) { Base64Options result; if(options) { - int valid_options=0; for(HashStringValue::Iterator i(*options); i; i.next()) { String::Body key=i.key(); Value* value=i.value(); if(key == "pad") { result.pad=r.process(*value).as_bool(); - valid_options++; } else if(key == "wrap") { result.wrap=r.process(*value).as_bool(); - valid_options++; } else if(key == "url-safe") { if(r.process(*value).as_bool()) 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; } @@ -716,25 +722,19 @@ Base64Options base64_encode_options(Requ Base64Options base64_decode_options(Request& r, HashStringValue* options) { Base64Options result; if(options) { - int valid_options=0; for(HashStringValue::Iterator i(*options); i; i.next() ) { String::Body key=i.key(); Value* value=i.value(); if(key == "pad") { result.pad=r.process(*value).as_bool(); - valid_options++; } else if(key == "strict") { result.strict=r.process(*value).as_bool(); - valid_options++; } else if(key == "url-safe") { if(r.process(*value).as_bool()) 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; } @@ -792,7 +792,7 @@ static void _js_unescape(Request& r, Met } 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); Charset* from_charset=&r.charsets.client(); @@ -859,8 +859,13 @@ MString::MString(): Methoded("string") { add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2); // ^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("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][options]