From d8129719c299dc9f5f46087798d1b220ca79aae8 Mon Sep 17 00:00:00 2001 From: AlexHolly Date: Tue, 1 Nov 2016 15:14:48 +0100 Subject: [PATCH] advanced string format --- core/ustring.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++- core/ustring.h | 1 + core/variant_call.cpp | 2 ++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index f9c10615b3..6ed4dfcf25 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2901,6 +2901,78 @@ bool String::matchn(const String& p_wildcard) const { } +String String::format(const Variant& values,String placeholder) const { + + String new_string = String( this->ptr() ); + + if( values.get_type() == Variant::ARRAY ) { + Array values_arr = values; + + for(int i=0;i keys; + d.get_key_list(&keys); + + for (List::Element *E=keys.front();E;E=E->next()) { + String key = E->get().get_construct_string(); + String val = d[E->get()].get_construct_string(); + + if( key.left(1)=="\"" && key.right(key.length()-1)=="\"" ) { + key = key.substr(1,key.length()-2); + } + + if( val.left(1)=="\"" && val.right(val.length()-1)=="\"" ) { + val = val.substr(1,val.length()-2); + } + + new_string = new_string.replacen( placeholder.replace("_", key ), val ); + } + }else{ + ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data()); + } + + return new_string; +} + String String::replace(String p_key,String p_with) const { String new_string; @@ -4161,4 +4233,3 @@ String RTR(const String& p_text) { return p_text; } - diff --git a/core/ustring.h b/core/ustring.h index 452f252857..841420487a 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -125,6 +125,7 @@ public: bool is_subsequence_ofi(const String& p_string) const; Vector bigrams() const; float similarity(const String& p_string) const; + String format(const Variant& values,String placeholder="{_}") const; String replace_first(String p_key,String p_with) const; String replace(String p_key,String p_with) const; String replacen(String p_key,String p_with) const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 9b6fa27cf4..231ed92b9f 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -251,6 +251,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM1R(String,is_subsequence_ofi); VCALL_LOCALMEM0R(String,bigrams); VCALL_LOCALMEM1R(String,similarity); + VCALL_LOCALMEM2R(String,format); VCALL_LOCALMEM2R(String,replace); VCALL_LOCALMEM2R(String,replacen); VCALL_LOCALMEM2R(String,insert); @@ -1388,6 +1389,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC0(STRING,STRING_ARRAY,String,bigrams,varray()); ADDFUNC1(STRING,REAL,String,similarity,STRING,"text",varray()); + ADDFUNC2(STRING,STRING,String,format,NIL,"values",STRING,"placeholder",varray("{_}")); ADDFUNC2(STRING,STRING,String,replace,STRING,"what",STRING,"forwhat",varray()); ADDFUNC2(STRING,STRING,String,replacen,STRING,"what",STRING,"forwhat",varray()); ADDFUNC2(STRING,STRING,String,insert,INT,"pos",STRING,"what",varray());