--- parser3/src/classes/string.C 2026/01/07 15:03:18 1.266 +++ parser3/src/classes/string.C 2026/11/03 12:25:52 1.269 @@ -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.266 2026/01/07 15:03:18 moko Exp $"; +volatile const char * IDENT_STRING_C="$Id: string.C,v 1.269 2026/11/03 12:25:52 moko Exp $"; // class @@ -158,12 +158,8 @@ static void _left(Request& r, MethodPara static void _right(Request& r, MethodParams& params) { int sn=params.as_int(0, "n must be int", r); - if(sn>0){ - size_t n=sn; - const String& string=GET_SELF(r, VString).string(); - size_t length=string.length(r.charsets.source()); - r.write(n0) + r.write(GET_SELF(r, VString).string().right(r.charsets.source(), sn)); } static void _mid(Request& r, MethodParams& params) { @@ -173,20 +169,16 @@ static void _mid(Request& r, MethodParam if(begin<0) throw Exception(PARSER_RUNTIME, 0, "p(%d) must be >=0", begin); - size_t end; - size_t length=0; + size_t end=string.length(); // to the end of the string (the length in bytes is not less than in characters) 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); 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) { @@ -203,6 +195,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; @@ -844,8 +851,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]