--- parser3/src/classes/string.C 2025/10/05 19:41:27 1.263 +++ 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-2024 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.263 2025/10/05 19:41:27 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; @@ -847,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]