--- parser3/src/classes/string.C 2005/11/22 11:21:35 1.147 +++ parser3/src/classes/string.C 2007/04/20 10:19:24 1.152 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2005/11/22 11:21:35 $"; +static const char * const IDENT_STRING_C="$Date: 2007/04/20 10:19:24 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -88,6 +88,25 @@ static void _double(Request& r, MethodPa 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) { Value& fmt_maybe_code=params[0]; @@ -317,18 +336,19 @@ static void _match(Request& r, MethodPar Temp_lang temp_lang(r, String::L_PASS_APPENDED); const String& src=GET_SELF(r, VString).string(); - bool just_matched; + int matches_count=0; if(params.count()<3) { // search Table* table=src.match(r.charsets.source(), regexp.as_string(), options, search_action, 0, - just_matched); - Value* result; - if(table) - result=new VTable(table); // table of pre/match/post+substrings - else - result=new VBool(just_matched); - r.write_assign_lang(*result); + matches_count); + // r.write_assign_lang(*new VTable(table)); + if(table){ + r.write_assign_lang(*new VTable(table)); + } else { + r.write_assign_lang(*new VInt(matches_count)); + } + } else { // replace Value& replacement_code=params.as_junction(2, "replacement param must be code"); @@ -347,7 +367,7 @@ static void _match(Request& r, MethodPar src.match(r.charsets.source(), r.process_to_string(regexp), options, replace_action, &info, - just_matched); + matches_count); r.write_assign_lang(result); } } @@ -446,10 +466,6 @@ const String* sql_result_string(Request& } if((default_code=options->get(sql_default_name))) { valid_options++; - if(!default_code->get_junction()) - throw Exception("parser.runtime", - 0, - "default option must be code"); } if(valid_options!=options->count()) throw Exception("parser.runtime", @@ -591,15 +607,15 @@ static void _base64(Request& r, MethodPa if(params.count()) { // decode 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; pa_base64_decode(cstr, strlen(cstr), decoded_cstr, decoded_size); if(decoded_cstr && decoded_size) - r.write_assign_lang(*new String(static_cast(decoded_cstr), decoded_size, true)); + r.write_assign_lang(*new String(decoded_cstr, decoded_size, true)); } else { // encode - VString& vself=GET_SELF(r, VString); - const char* cstr=vself.string().cstr(); + VString& self=GET_SELF(r, VString); + const char* cstr=self.string().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*/)); } @@ -617,6 +633,9 @@ MString::MString(): Methoded("string") { // ^string.double[] // ^string.double(default) 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} add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);