Diff for /parser3/src/classes/string.C between versions 1.220 and 1.229

version 1.220, 2015/10/07 22:25:40 version 1.229, 2016/09/21 15:35:10
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 92  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 205  static void _pos(Request& r, MethodParam Line 202  static void _pos(Request& r, MethodParam
         if(params.count()>1){          if(params.count()>1){
                 offset=params.as_int(1, "n must be int", r);                  offset=params.as_int(1, "n must be int", r);
                 if(offset<0)                  if(offset<0)
                         throw Exception(PARSER_RUNTIME,                          throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", offset);
                                 0,   
                                 "n(%d) must be >=0", offset);  
         }          }
   
         r.write_no_lang(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), (size_t)offset)));          r.write_no_lang(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), (size_t)offset)));
 }  }
   
 static void split_list(MethodParams& params, int paramIndex,  static void split_list(MethodParams& params, int paramIndex, const String& string, ArrayString& result) {
                        const String& string,   
                        ArrayString& result) {  
         Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");          Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
           string.split(result, 0, delim_value.as_string());
         size_t pos_after=0;  
         string.split(result, pos_after, delim_value.as_string());  
 }  }
   
 #define SPLIT_LEFT 0x0001  #define SPLIT_LEFT 0x0001
Line 629  static void _save(Request& r, MethodPara Line 620  static void _save(Request& r, MethodPara
                         // ^file.save[filespec;$.charset[] $.append(true)]                          // ^file.save[filespec;$.charset[] $.append(true)]
                         int valid_options=0;                          int valid_options=0;
                         if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){                          if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
                                 asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER));                                  asked_charset=&pa_charsets.get(vcharset_name->as_string());
                                 valid_options++;                                  valid_options++;
                         }                          }
                         if(Value* vappend=options->get(MODE_APPEND)){                          if(Value* vappend=options->get(MODE_APPEND)){
Line 772  static void _unescape(Request& r, Method Line 763  static void _unescape(Request& r, Method
                 if(HashStringValue* options=params.as_hash(2)) {                  if(HashStringValue* options=params.as_hash(2)) {
                         int valid_options=0;                          int valid_options=0;
                         if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){                          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));                                  from_charset=&pa_charsets.get(vcharset_name->as_string());
                                 valid_options++;                                  valid_options++;
                         }                          }
                         if(valid_options!=options->count())                          if(valid_options!=options->count())
Line 785  static void _unescape(Request& r, Method Line 776  static void _unescape(Request& r, Method
         } else if(mode==UNESCAPE_MODE_URI){          } else if(mode==UNESCAPE_MODE_URI){
                 mode_js=false;                  mode_js=false;
         } else {          } else {
                 throw Exception(PARSER_RUNTIME, &mode, "is invalid mode, must be either '"UNESCAPE_MODE_JS"' or '"UNESCAPE_MODE_URI"'");                  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);          const char* unescaped=unescape_chars(src.cstr(), src.length(), from_charset, mode_js);
         if(*unescaped){          if(*unescaped){
                 const String* result=new String(Charset::transcode(String::Body(unescaped), *from_charset, r.charsets.source()), String::L_TAINTED);                  const String* result=new String(Charset::transcode(unescaped, *from_charset, r.charsets.source()), String::L_TAINTED);
                 r.write_assign_lang(*result);                  r.write_assign_lang(*result);
         }          }
 }  }
   
   static void _contains(Request& r, MethodParams& params) {
           // empty or whitespace string is hash compatible
           GET_SELF(r, VString).get_element(params.as_string(0, "key must be string"));
           // ignoring result as it allways null
           r.write_no_lang(VBool::get(false));
   }
   
 // constructor  // constructor
   
 MString::MString(): Methoded("string") {  MString::MString(): Methoded("string") {
Line 880  MString::MString(): Methoded("string") { Line 878  MString::MString(): Methoded("string") {
   
         // ^string:unescape[js|uri;escaped;$.charset[...]]          // ^string:unescape[js|uri;escaped;$.charset[...]]
         add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3);          add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3);
   
           // ^string.contains[key] for hash compatibility
           add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
 }  }

Removed from v.1.220  
changed lines
  Added in v.1.229


E-mail: