Diff for /parser3/src/classes/string.C between versions 1.147 and 1.154

version 1.147, 2005/11/22 11:21:35 version 1.154, 2007/08/20 10:02:51
Line 56  static void _int(Request& r, MethodParam Line 56  static void _int(Request& r, MethodParam
         int converted;          int converted;
         try {          try {
                 if(self_string.is_empty())                  if(self_string.is_empty())
                         throw Exception("parser.runtime",                          throw Exception(PARSER_RUNTIME,
                                 0,                                  0,
                                 "parameter is empty string, error converting");                                  "parameter is empty string, error converting");
                 converted=self_string.as_int();                  converted=self_string.as_int();
Line 74  static void _double(Request& r, MethodPa Line 74  static void _double(Request& r, MethodPa
         double converted;          double converted;
         try {          try {
                 if(self_string.is_empty())                  if(self_string.is_empty())
                         throw Exception("parser.runtime",                          throw Exception(PARSER_RUNTIME,
                                 0,                                  0,
                                 "parameter is empty string, error converting");                                  "parameter is empty string, error converting");
                 converted=self_string.as_double();                  converted=self_string.as_double();
Line 88  static void _double(Request& r, MethodPa Line 88  static void _double(Request& r, MethodPa
         r.write_no_lang(*new VDouble(converted));          r.write_no_lang(*new VDouble(converted));
 }  }
   
   static void _bool(Request& r, MethodParams& params) {
           const String& self_string=GET_SELF(r, VString).string();
           bool converted;
           try {
                   if(self_string.is_empty())
                           throw Exception(PARSER_RUNTIME,
                                   0,
                                   "parameter is empty string, error converting");
                   converted=self_string.as_bool();
           } catch(...) { // convert problem
                   if(params.count()>0)
                           converted=params.as_bool(0, "default must be bool", r); // (default)
                   else
                           rethrow; // we have a problem when no default
           }
   
           r.write_no_lang(*new VBool(converted));
   }
   
 /*not static*/void _string_format(Request& r, MethodParams& params) {  /*not static*/void _string_format(Request& r, MethodParams& params) {
   
         Value& fmt_maybe_code=params[0];          Value& fmt_maybe_code=params[0];
Line 102  static void _double(Request& r, MethodPa Line 121  static void _double(Request& r, MethodPa
 static void _left(Request& r, MethodParams& params) {  static void _left(Request& r, MethodParams& params) {
         ssize_t sn=params.as_int(0, "n must be int", r);          ssize_t sn=params.as_int(0, "n must be int", r);
         if(sn<0)          if(sn<0)
                 throw Exception("parser.runtime",                  throw Exception(PARSER_RUNTIME,
                         0,                           0, 
                         "n(%d) must be >=0", sn);                          "n(%d) must be >=0", sn);
         size_t n=(size_t)sn;          size_t n=(size_t)sn;
Line 114  static void _left(Request& r, MethodPara Line 133  static void _left(Request& r, MethodPara
 static void _right(Request& r, MethodParams& params) {  static void _right(Request& r, MethodParams& params) {
         ssize_t sn=(size_t)params.as_int(0, "n must be int", r);          ssize_t sn=(size_t)params.as_int(0, "n must be int", r);
         if(sn<0)          if(sn<0)
                 throw Exception("parser.runtime",                  throw Exception(PARSER_RUNTIME,
                         0,                           0, 
                         "n(%d) must be >=0", sn);                          "n(%d) must be >=0", sn);
         size_t n=(size_t)sn;          size_t n=(size_t)sn;
Line 130  static void _mid(Request& r, MethodParam Line 149  static void _mid(Request& r, MethodParam
   
         ssize_t sbegin=params.as_int(0, "p must be int", r);          ssize_t sbegin=params.as_int(0, "p must be int", r);
         if(sbegin<0)          if(sbegin<0)
                 throw Exception("parser.runtime",                  throw Exception(PARSER_RUNTIME,
                         0,                           0, 
                         "p(%d) must be >=0", sbegin);                          "p(%d) must be >=0", sbegin);
         size_t begin=(size_t)sbegin;          size_t begin=(size_t)sbegin;
Line 139  static void _mid(Request& r, MethodParam Line 158  static void _mid(Request& r, MethodParam
         if(params.count()>1) {          if(params.count()>1) {
                 ssize_t sn=params.as_int(1, "n must be int", r);                  ssize_t sn=params.as_int(1, "n must be int", r);
                 if(sn<0)                  if(sn<0)
                         throw Exception("parser.runtime",                          throw Exception(PARSER_RUNTIME,
                                 0,                                   0, 
                                 "n(%d) must be >=0", sn);                                  "n(%d) must be >=0", sn);
                 end=begin+(size_t)sn;                  end=begin+(size_t)sn;
Line 190  static int split_options(const String* o Line 209  static int split_options(const String* o
                         if(options->pos(o->keyL)!=STRING_NOT_FOUND                           if(options->pos(o->keyL)!=STRING_NOT_FOUND 
                                 || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {                                  || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
                                 if(result & o->checkBit)                                  if(result & o->checkBit)
                                         throw Exception("parser.runtime",                                          throw Exception(PARSER_RUNTIME,
                                                 options,                                                  options,
                                                 "conflicting split options");                                                  "conflicting split options");
                                 result |= o->setBit;                                  result |= o->setBit;
Line 317  static void _match(Request& r, MethodPar Line 336  static void _match(Request& r, MethodPar
   
         Temp_lang temp_lang(r, String::L_PASS_APPENDED);          Temp_lang temp_lang(r, String::L_PASS_APPENDED);
         const String& src=GET_SELF(r, VString).string();          const String& src=GET_SELF(r, VString).string();
         bool just_matched;          int matches_count=0;
         if(params.count()<3) { // search          if(params.count()<3) { // search
                 Table* table=src.match(r.charsets.source(),                  Table* table=src.match(r.charsets.source(),
                         regexp.as_string(), options,                          regexp.as_string(), options,
                         search_action, 0,                          search_action, 0,
                         just_matched);                          matches_count);
                 Value* result;                  // r.write_assign_lang(*new VTable(table));
                 if(table)                   if(table){
                         result=new VTable(table); // table of pre/match/post+substrings                  r.write_assign_lang(*new VTable(table));
                 else                   } else {
                         result=new VBool(just_matched);                          r.write_assign_lang(*new VInt(matches_count));
                 r.write_assign_lang(*result);                  }
   
         } else { // replace          } else { // replace
                 Value& replacement_code=params.as_junction(2, "replacement param must be code");                  Value& replacement_code=params.as_junction(2, "replacement param must be code");
   
Line 347  static void _match(Request& r, MethodPar Line 367  static void _match(Request& r, MethodPar
                 src.match(r.charsets.source(),                  src.match(r.charsets.source(),
                         r.process_to_string(regexp), options,                          r.process_to_string(regexp), options,
                         replace_action, &info,                          replace_action, &info,
                         just_matched);                          matches_count);
                 r.write_assign_lang(result);                  r.write_assign_lang(result);
         }          }
 }  }
Line 382  public: Line 402  public:
   
         bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {          bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
                 if(got_column) {                  if(got_column) {
                         error=SQL_Error("parser.runtime",                          error=SQL_Error(PARSER_RUNTIME,
                                 //statement_string,                                  //statement_string,
                                 "result must contain exactly one column");                                  "result must contain exactly one column");
                         return true;                          return true;
Line 394  public: Line 414  public:
         bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }          bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
         bool add_row_cell(SQL_Error& error, const char* str, size_t length) {          bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
                 if(got_cell) {                  if(got_cell) {
                         error=SQL_Error("parser.runtime",                          error=SQL_Error(PARSER_RUNTIME,
                                 //statement_string,                                  //statement_string,
                                 "result must not contain more then one row");                                  "result must not contain more then one row");
                         return true;                          return true;
Line 446  const String* sql_result_string(Request& Line 466  const String* sql_result_string(Request&
                                 }                                  }
                                 if((default_code=options->get(sql_default_name))) {                                  if((default_code=options->get(sql_default_name))) {
                                         valid_options++;                                          valid_options++;
                                         if(!default_code->get_junction())  
                                                 throw Exception("parser.runtime",  
                                                         0,  
                                                         "default option must be code");  
                                 }                                  }
                                 if(valid_options!=options->count())                                  if(valid_options!=options->count())
                                         throw Exception("parser.runtime",                                          throw Exception(PARSER_RUNTIME,
                                                 0,                                                  0,
                                                 "called with invalid option");                                                  "called with invalid option");
                         } else                          } else
                                 throw Exception("parser.runtime",                                  throw Exception(PARSER_RUNTIME,
                                         0,                                          0,
                                         "options must be hash");                                          "options must be hash");
         } else          } else
Line 497  static void _sql(Request& r, MethodParam Line 513  static void _sql(Request& r, MethodParam
                 if(default_code) {                  if(default_code) {
                         string=&r.process_to_string(*default_code);                          string=&r.process_to_string(*default_code);
                 } else                  } else
                         throw Exception("parser.runtime",                          throw Exception(PARSER_RUNTIME,
                                 0,                                  0,
                                 "produced no result, but no default option specified");                                  "produced no result, but no default option specified");
         }          }
Line 510  static void _replace(Request& r, MethodP Line 526  static void _replace(Request& r, MethodP
   
         Table* table=params.as_no_junction(0, "parameter must not be code").get_table();          Table* table=params.as_no_junction(0, "parameter must not be code").get_table();
         if(!table)          if(!table)
                 throw Exception("parser.runtime",                  throw Exception(PARSER_RUNTIME,
                         0,                          0,
                         "parameter must be table");                          "parameter must be table");
   
Line 519  static void _replace(Request& r, MethodP Line 535  static void _replace(Request& r, MethodP
 }  }
   
 static void _save(Request& r, MethodParams& params) {  static void _save(Request& r, MethodParams& params) {
         const String& file_name=params.as_string(params.count()-1,           const String& file_name=params.as_string(params.count()-1, FILE_NAME_MUST_BE_STRING);
                 "file name must be string");  
   
         const String& src=GET_SELF(r, VString).string();          const String& src=GET_SELF(r, VString).string();
   
Line 530  static void _save(Request& r, MethodPara Line 545  static void _save(Request& r, MethodPara
                 if(mode=="append")                  if(mode=="append")
                         do_append=true;                          do_append=true;
                 else                  else
                         throw Exception("parser.runtime",                          throw Exception(PARSER_RUNTIME,
                                 &mode,                                  &mode,
                                 "unknown mode, must be 'append'");                                  "unknown mode, must be 'append'");
         }                         }               
Line 563  static void _trim(Request& r, MethodPara Line 578  static void _trim(Request& r, MethodPara
                         else if(skind==TRIM_BOTH_OPTION)                          else if(skind==TRIM_BOTH_OPTION)
                                 kind=String::TRIM_BOTH;                                  kind=String::TRIM_BOTH;
                         else                          else
                                 throw Exception("parser.runtime",                                  throw Exception(PARSER_RUNTIME,
                                         &skind,                                          &skind,
                                         "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);                                          "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
   
Line 591  static void _base64(Request& r, MethodPa Line 606  static void _base64(Request& r, MethodPa
         if(params.count()) {          if(params.count()) {
                 // decode                  // decode
                 const char* cstr=params.as_string(0, "parameter must be string").cstr();                  const char* cstr=params.as_string(0, "parameter must be string").cstr();
                 void* decoded_cstr=0;                  char* decoded_cstr=0;
                 size_t decoded_size=0;                  size_t decoded_size=0;
                 pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size);                  pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size);
                 if(decoded_cstr && decoded_size)                  if(decoded_cstr && decoded_size)
                         r.write_assign_lang(*new String(static_cast<const char*>(decoded_cstr), decoded_size, true));                          r.write_assign_lang(*new String(decoded_cstr, decoded_size, true));
         } else {          } else {
                 // encode                   // encode 
                 VString& vself=GET_SELF(r, VString);                  VString& self=GET_SELF(r, VString);
                 const char* cstr=vself.string().cstr();                  const char* cstr=self.string().cstr();
                 const char* encoded=pa_base64_encode(cstr, strlen(cstr));                  const char* encoded=pa_base64_encode(cstr, strlen(cstr));
                 r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/));                  r.write_assign_lang(*new String(encoded, 0, true/*once ?param=base64(something) was needed*/));
         }          }
Line 617  MString::MString(): Methoded("string") { Line 632  MString::MString(): Methoded("string") {
         // ^string.double[]          // ^string.double[]
         // ^string.double(default)          // ^string.double(default)
         add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);          add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
           // ^void.bool[]
           // ^void.bool(default)
           add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
   
         // ^string.format{format}          // ^string.format{format}
         add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);          add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);

Removed from v.1.147  
changed lines
  Added in v.1.154


E-mail: