--- parser3/src/classes/string.C 2001/05/21 16:38:46 1.53 +++ parser3/src/classes/string.C 2001/07/20 09:40:46 1.64 @@ -4,9 +4,8 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: string.C,v 1.53 2001/05/21 16:38:46 parser Exp $ */ +static const char *RCSId="$Id: string.C,v 1.64 2001/07/20 09:40:46 parser Exp $"; #include "classes.h" #include "pa_request.h" @@ -24,9 +23,6 @@ // class class MString : public Methoded { -public: // VStateless_class - Value *create_new_value(Pool& pool) { return new(pool) VString(pool); } - public: MString(Pool& pool); public: // Methoded @@ -59,12 +55,16 @@ static void _double(Request& r, const St /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& fmt=params->get_junction(0, "fmt must be code"); + Value& fmt=params->as_junction(0, "fmt must be code"); Temp_lang temp_lang(r, String::UL_PASS_APPENDED); char *buf=format(pool, r.self->as_double(), r.process(fmt).as_string().cstr()); - - r.write_no_lang(String(pool, buf)); + + String result(pool); + result.APPEND_CLEAN(buf, 0, + method_name.origin().file, + method_name.origin().line); + r.write_no_lang(result); } static void _left(Request& r, const String&, MethodParams *params) { @@ -98,7 +98,7 @@ static void _mid(Request& r, const Strin static void _pos(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& substr=params->get_no_junction(0, "substr must not be code"); + Value& substr=params->as_no_junction(0, "substr must not be code"); const String& string=*static_cast(r.self)->get_string(); r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string()))); @@ -107,7 +107,7 @@ static void _pos(Request& r, const Strin static void split_list(Request& r, const String& method_name, MethodParams *params, const String& string, Array& result) { - Value& delim_value=params->get_no_junction(0, "delimiter must not be code"); + Value& delim_value=params->as_no_junction(0, "delimiter must not be code"); string.split(result, 0, delim_value.as_string()); } @@ -116,13 +116,20 @@ static void _lsplit(Request& r, const St Pool& pool=r.pool(); const String& string=*static_cast(r.self)->get_string(); - Array& row=*new(pool) Array(pool); - split_list(r, method_name, params, string, row); + Array pieces(pool); + split_list(r, method_name, params, string, pieces); - Table& table=*new(pool) Table(pool, &string, - 0/*nameless*/, 1/*row preallocate(and only)*/); - table+=&row; + Array& columns=*new(pool) Array(pool); + columns+=new(pool) String(pool, "piece"); + Table& table=*new(pool) Table(pool, &string, + &columns, pieces.size()); + int size=pieces.quick_size(); + for(int i=0; i(r.self)->get_string(); - Array list(pool); - split_list(r, method_name, params, string, list); + Array pieces(pool); + split_list(r, method_name, params, string, pieces); - Array& row=*new(pool) Array(pool); - for(int i=list.size(); --i>=0; ) - row+=list.get(i); + Array& columns=*new(pool) Array(pool); + columns+=new(pool) String(pool, "piece"); Table& table=*new(pool) Table(pool, &string, - 0/*nameless*/, 1/*row preallocate(and only)*/); - table+=&row; + &columns, pieces.size()); + for(int i=pieces.size(); --i>=0; ) { + Array& row=*new(pool) Array(pool); + row+=pieces.get(i); + table+=&row; + } r.write_no_lang(*new(pool) VTable(pool, &table)); } @@ -194,31 +204,31 @@ static void _match(Request& r, const Str Pool& pool=r.pool(); const String& src=*static_cast(r.self)->get_string(); - Value& regexp=params->get_no_junction(0, "regexp must not be code"); + Value& regexp=params->as_no_junction(0, "regexp must not be code"); const String *options= params->size()>1? - ¶ms->get_no_junction(1, "options must not be code").as_string():0; + ¶ms->as_no_junction(1, "options must not be code").as_string():0; Value *result; Temp_lang temp_lang(r, String::UL_PASS_APPENDED); Table *table; if(params->size()<3) { // search - if(src.match(r.pcre_tables, + bool was_global; + bool matched=src.match(r.pcre_tables, &method_name, regexp.as_string(), options, &table, - search_action, 0)) { - // matched - if(table->columns()->size()==3 && // just matched[3=pre/match/post], no substrings - table->size()==1) // just one row, not /g_lobal search - result=new(pool) VBool(pool, true); - else // table of pre/match/post+substrings - result=new(pool) VTable(pool, table); - } else // not matched [not global] - result=new(pool) VBool(pool, false); + search_action, 0, + &was_global); + // matched + // not (just matched[3=pre/match/post], no substrings) or Global search + if(table->columns()->size()>3 || was_global) + result=new(pool) VTable(pool, table); // table of pre/match/post+substrings + else + result=new(pool) VBool(pool, matched); } else { // replace - Value& replacement_code=params->get_junction(2, "replacement code must be code"); + Value& replacement_code=params->as_junction(2, "replacement code must be code"); String& dest=*new(pool) String(pool); Replace_action_info replace_action_info={ @@ -253,7 +263,7 @@ static void _lower(Request& r, const Str change_case(r, method_name, params, String::CC_LOWER); } -String& sql_result_string(Request& r, const String& method_name, MethodParams *params) { +const String* sql_result_string(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); if(!r.connection) @@ -261,13 +271,9 @@ String& sql_result_string(Request& r, co &method_name, "without connect"); - Value& statement=params->get_junction(0, "statement must be code"); + Value& statement=params->as_junction(0, "statement must be code"); - ulong offset=0; - if(params->size()>1) { - Value& offset_code=params->get_junction(1, "offset must be expression"); - offset=(ulong)r.process(offset_code).as_double(); - } + ulong offset=(ulong)(params->size()>2?params->as_int(2, r):0); Temp_lang temp_lang(r, String::UL_SQL); const String& statement_string=r.process(statement).as_string(); @@ -294,16 +300,19 @@ String& sql_result_string(Request& r, co if(sql_column_count!=1) PTHROW(0, 0, &statement_string, - "result must contain only one column"); + "result must contain exactly one column"); + + if(!sql_row_count) + return 0; // no lines, should return second param[default value] - if(sql_row_count!=1) + if(sql_row_count>1) PTHROW(0, 0, &statement_string, - "result must contain only one row"); + "result must not contain more then one row"); - String& result=*new(pool) String(pool); + String *result=new(pool) String(pool); SQL_Driver::Cell& cell=sql_rows[0][0]; - result.APPEND_TAINTED( + result->APPEND_TAINTED( (const char *)cell.ptr, cell.size, statement_cstr, 0); @@ -313,7 +322,15 @@ String& sql_result_string(Request& r, co static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VString& result=*new(pool) VString(sql_result_string(r, method_name, params)); + const String *string=sql_result_string(r, method_name, params); + if(!string) { + Value& default_code=params->as_junction(1, "default result must code"); + Value& processed_code=r.process(default_code); + string=processed_code.get_string(); + if(!string) + string=empty_string; + } + VString& result=*new(pool) VString(*string); result.set_name(method_name); r.write_assign_lang(result); } @@ -360,9 +377,9 @@ MString::MString(Pool& apool) : Methoded // ^string.tolower[] add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0); - // ^string:sql[query] - // ^string:sql[query](offset) - add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); + // ^string:sql[query]{default} + // ^string:sql[query]{default}(offset) + add_native_method("sql", Method::CT_STATIC, _sql, 2, 3); } // global variable