Index: pol-core/bscript/objmethods.h =================================================================== --- pol-core/bscript/objmethods.h (revision 625) +++ pol-core/bscript/objmethods.h (working copy) @@ -168,7 +168,8 @@ MTH_JOIN, MTH_FIND, MTH_UPPER, - MTH_LOWER + MTH_LOWER, + MTH_FORMAT }; Index: pol-core/bscript/parser.cpp =================================================================== --- pol-core/bscript/parser.cpp (revision 625) +++ pol-core/bscript/parser.cpp (working copy) @@ -631,7 +631,8 @@ { MTH_JOIN, "join"}, { MTH_FIND, "find"}, { MTH_UPPER, "upper"}, - { MTH_LOWER, "lower"} + { MTH_LOWER, "lower"}, + { MTH_FORMAT, "format"} }; int n_objmethods = sizeof object_methods / sizeof object_methods[0]; ObjMethod* getKnownObjMethod(const char* token) Index: pol-core/bscript/str.cpp =================================================================== --- pol-core/bscript/str.cpp (revision 625) +++ pol-core/bscript/str.cpp (working copy) @@ -614,7 +614,27 @@ } } +bool s_parse_int(int &i, string const &s) +{ + char c; + std::stringstream ss(s); + ss >> i; + if (ss.fail() || ss.get(c)) { + // not an integer + return false; + } + return true; +} +void s_trim(string &s) +{ + std::stringstream trimmer; + trimmer << s; + s.clear(); + trimmer >> s; +} + + BObjectImp* String::call_method( const char* methodname, Executor& ex ) { ObjMethod* objmethod = getKnownObjMethod(methodname); @@ -639,11 +659,11 @@ return new BError("string.find(Search, [Start]) takes only two parameters"); if (ex.numParams() < 1) return new BError("string.find(Search, [Start]) takes at least one parameter"); - const char *s = ex.paramAsString(0); - int d = 0; - if (ex.numParams() == 2) - d = ex.paramAsLong(1); - int posn = find(d ? (d - 1) : 0, s) + 1; + const char *s = ex.paramAsString(0); + int d = 0; + if (ex.numParams() == 2) + d = ex.paramAsLong(1); + int posn = find(d ? (d - 1) : 0, s) + 1; return new BLong (posn); } case MTH_UPPER: @@ -699,6 +719,100 @@ else return new BError( "string.join(array) requires a parameter." ); } + case MTH_FORMAT: + { + if(ex.numParams() > 0) { + BObject* args = ex.getParamObj( 0 ); + string s = this->getStringRep(); // string itself + std::stringstream result; + + unsigned int tag_start_pos; + unsigned int tag_stop_pos; + unsigned int current_s_pos=0; + + unsigned int next_param_idx = 0; + int tag_param_idx; + + while((tag_start_pos = s.find("{", current_s_pos))!=string::npos) { + if((tag_stop_pos=s.find("}", tag_start_pos)) != string::npos) { + + string s_repl_by; + string s_tag_body = s.substr(tag_start_pos+1, (tag_stop_pos - tag_start_pos)-1); + + // trim the tag of whitespaces + s_trim( s_tag_body ); + + // parsing .this_part + int tag_dot_pos = s_tag_body.find(".", 0); + string s_tag_prop_name; + + // dot is found within the tag + if(tag_dot_pos!=string::npos) { + s_tag_prop_name = s_tag_body.substr(tag_dot_pos+1, string::npos); // + s_tag_body = s_tag_body.substr(0, tag_dot_pos); // remove property from the tag + } + + //--end of property parsing + + //cout << "property: '" << s_tag_suffix << "' tag: '" << s_tag_body << "'"; + + // if not a number then just use next idx in line + if(s_parse_int(tag_param_idx, s_tag_body)) { + tag_param_idx -= 1; // sinse POL indexes are 1-based + } else { + tag_param_idx = next_param_idx; + if(tag_dot_pos==string::npos) { // there is no dot in the tag + // the whole tag becomes prop_name if no dot was found + s_tag_prop_name = s_tag_body; + } + } + + result << s.substr(current_s_pos, tag_start_pos - current_s_pos); + + if( ex.numParams() > tag_param_idx ) { + BObjectImp *imp = ex.getParamImp(tag_param_idx); + if(s_tag_prop_name.length() > 0) { // accesing object member + /* + if(s_tag_suffix.find("@", 0)==0) { + UObject* uobj; + + if(getUObjectParam(ex, tag_param_idx, uobj)) { + + } else { + s_repl_by = ""; + } + } + */ + BObjectRef obj_member = imp->get_member(s_tag_prop_name.c_str()); + s_repl_by = obj_member->impptr()->getStringRep(); + } else { + s_repl_by = imp->getStringRep(); + } + } + else { + std::ostringstream os; + os << ""; + s_repl_by = os.str(); + } + + current_s_pos = tag_stop_pos + 1; + result << s_repl_by; + ++next_param_idx; + } else { + break; + } + } + + if( current_s_pos < s.length() ) { + result << s.substr( current_s_pos, string::npos ); + } + + return new String( result.str() ); + + } else { + return new BError( "string.format() requires a parameter." ); + } + } default: return NULL; }