--- parser3/src/classes/string.C 2009/04/28 04:53:52 1.178 +++ parser3/src/classes/string.C 2009/06/23 10:05:24 1.182 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2009/04/28 04:53:52 $"; +static const char * const IDENT_STRING_C="$Date: 2009/06/23 10:05:24 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -332,17 +332,18 @@ static void search_action(Table& table, #ifndef DOXYGEN struct Replace_action_info { - Request* request; - const String* src; String* dest; + Request* request; + const String* src; + String* dest; VTable* vtable; Value* replacement_code; }; #endif /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this static void replace_action(Table& table, ArrayString* row, - int prestart, int prefinish, - int poststart, int postfinish, - void *info) { + int prestart, int prefinish, + int poststart, int postfinish, + void *info) { Replace_action_info& ai=*static_cast(info); if(row) { // begin&middle // piece from last match['prestart'] to beginning of this match['prefinish'] @@ -353,10 +354,12 @@ static void replace_action(Table& table, table.put(0, row); else // begin table+=row; - { // execute 'replacement_code' in 'table' context - ai.vtable->set_table(table); - *ai.dest << ai.request->process_to_string(*ai.replacement_code); + { // execute 'replacement_code' in 'table' context + if(ai.replacement_code){ + ai.vtable->set_table(table); + *ai.dest << ai.request->process_to_string(*ai.replacement_code); + } } } else // end *ai.dest << ai.src->mid(poststart, postfinish); @@ -402,7 +405,21 @@ static void _match(Request& r, MethodPar } } else { // replace - Value& replacement_code=params.as_junction(2, "replacement param must be code"); + + Value* replacement_code=0; + bool is_junction=false; + + Value* replacement=¶ms[2]; + if(replacement->get_junction()){ + replacement_code=replacement; + is_junction=true; + } else if(replacement->is_string()){ + if(replacement->is_defined()) + replacement_code=replacement; + } else if(!replacement->is_void()) + throw Exception(PARSER_RUNTIME, + 0, + "replacement option should be junction or string"); String result; VTable* vtable=new VTable; @@ -411,16 +428,23 @@ static void _match(Request& r, MethodPar &src, &result, vtable, - &replacement_code + replacement_code }; - Temp_value_element temp_match_var( - *replacement_code.get_junction()->method_frame, - match_var_name, vtable); + + Temp_value_element* temp_match_var=0; + + if(is_junction) + temp_match_var=new Temp_value_element( + *replacement_code->get_junction()->method_frame, + match_var_name, vtable); src.match(vregex, replace_action, &info, matches_count); + if(temp_match_var) + delete temp_match_var; + r.write_assign_lang(result); } } @@ -644,7 +668,7 @@ static void _trim(Request& r, MethodPara } } - r.write_assign_lang(src.trim(kind, chars)); + r.write_assign_lang(src.trim(kind, chars, &r.charsets.source())); } static void _append(Request& r, MethodParams& params) { @@ -671,16 +695,15 @@ static void _base64(Request& r, MethodPa "Invalid \\x00 character found while decode to string. Decode it to file instead."); fix_line_breaks(decoded, length); - if(length){ - r.write_assign_lang(*new String(decoded, length, true/*tainted*/)); - } + if(length) + r.write_assign_lang(*new String(decoded, String::L_TAINTED)); } } else { // encode: ^str.base64[] 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/*tainted. once ?param=base64(something) was needed*/)); + r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/)); } }