Merge pull request #9410 from williamd1k0/advanced-string-format

Advanced string format (2.1)
This commit is contained in:
Rémi Verschelde 2017-06-27 20:52:27 +02:00 committed by GitHub
commit e3f9653353
5 changed files with 156 additions and 12 deletions

View File

@ -39,14 +39,40 @@ struct _DictionaryVariantHash {
struct DictionaryPrivate { struct DictionaryPrivate {
struct Data {
Variant variant;
int order;
};
SafeRefCount refcount; SafeRefCount refcount;
HashMap<Variant, Variant, _DictionaryVariantHash> variant_map; HashMap<Variant, Data, _DictionaryVariantHash> variant_map;
int counter;
bool shared; bool shared;
}; };
struct DictionaryPrivateSort {
bool operator()(const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *A, const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *B) const {
return A->data.order < B->data.order;
}
};
void Dictionary::get_key_list(List<Variant> *p_keys) const { void Dictionary::get_key_list(List<Variant> *p_keys) const {
_p->variant_map.get_key_list(p_keys); if (_p->variant_map.empty())
return;
int count = _p->variant_map.size();
const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
_p->variant_map.get_key_value_ptr_array(pairs);
SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
sort.sort(pairs, count);
for (int i = 0; i < count; i++) {
p_keys->push_back(pairs[i]->key);
}
} }
void Dictionary::_copy_on_write() const { void Dictionary::_copy_on_write() const {
@ -67,29 +93,47 @@ Variant &Dictionary::operator[](const Variant &p_key) {
_copy_on_write(); _copy_on_write();
return _p->variant_map[p_key]; DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
if (!v) {
DictionaryPrivate::Data d;
d.order = _p->counter++;
_p->variant_map[p_key] = d;
v = _p->variant_map.getptr(p_key);
}
return v->variant;
} }
const Variant &Dictionary::operator[](const Variant &p_key) const { const Variant &Dictionary::operator[](const Variant &p_key) const {
return _p->variant_map[p_key]; return _p->variant_map[p_key].variant;
} }
const Variant *Dictionary::getptr(const Variant &p_key) const { const Variant *Dictionary::getptr(const Variant &p_key) const {
return _p->variant_map.getptr(p_key); const DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
if (!v)
return NULL;
else
return &v->variant;
} }
Variant *Dictionary::getptr(const Variant &p_key) { Variant *Dictionary::getptr(const Variant &p_key) {
_copy_on_write(); _copy_on_write();
return _p->variant_map.getptr(p_key); DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
if (!v)
return NULL;
else
return &v->variant;
} }
Variant Dictionary::get_valid(const Variant &p_key) const { Variant Dictionary::get_valid(const Variant &p_key) const {
const Variant *v = getptr(p_key); DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
if (!v) if (!v)
return Variant(); return Variant();
return *v; else
return v->variant;
} }
int Dictionary::size() const { int Dictionary::size() const {
@ -145,6 +189,7 @@ void Dictionary::clear() {
_copy_on_write(); _copy_on_write();
_p->variant_map.clear(); _p->variant_map.clear();
_p->counter = 0;
} }
bool Dictionary::is_shared() const { bool Dictionary::is_shared() const {
@ -192,11 +237,20 @@ Array Dictionary::values() const {
Array varr; Array varr;
varr.resize(size()); varr.resize(size());
const Variant *key = NULL; if (_p->variant_map.empty())
int i = 0; return varr;
while ((key = next(key))) {
varr[i++] = _p->variant_map[*key]; int count = _p->variant_map.size();
const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
_p->variant_map.get_key_value_ptr_array(pairs);
SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
sort.sort(pairs, count);
for (int i = 0; i < count; i++) {
varr[i] = pairs[i]->data.variant;
} }
return varr; return varr;
} }
@ -239,6 +293,7 @@ Dictionary::Dictionary(bool p_shared) {
_p = memnew(DictionaryPrivate); _p = memnew(DictionaryPrivate);
_p->refcount.init(); _p->refcount.init();
_p->counter = 0;
_p->shared = p_shared; _p->shared = p_shared;
} }
Dictionary::~Dictionary() { Dictionary::~Dictionary() {

View File

@ -570,6 +570,20 @@ public:
hash_table_power = 0; hash_table_power = 0;
} }
void get_key_value_ptr_array(const Pair **p_pairs) const {
if (!hash_table)
return;
for (int i = 0; i < (1 << hash_table_power); i++) {
Entry *e = hash_table[i];
while (e) {
*p_pairs = &e->pair;
p_pairs++;
e = e->next;
}
}
}
void get_key_list(List<TKey> *p_keys) const { void get_key_list(List<TKey> *p_keys) const {
if (!hash_table) if (!hash_table)
return; return;

View File

@ -2788,6 +2788,78 @@ bool String::matchn(const String &p_wildcard) const {
return _wildcard_match(p_wildcard.c_str(), c_str(), false); return _wildcard_match(p_wildcard.c_str(), c_str(), false);
} }
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 < values_arr.size(); i++) {
String i_as_str = String::num_int64(i);
if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
Array value_arr = values_arr[i];
if (value_arr.size() == 2) {
Variant v_key = value_arr[0];
String key;
key = v_key.get_construct_string();
if (key.left(1) == "\"" && key.right(key.length() - 1) == "\"") {
key = key.substr(1, key.length() - 2);
}
Variant v_val = value_arr[1];
String val;
val = v_val.get_construct_string();
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("STRING.format Inner Array size != 2 ").ascii().get_data());
}
} else { //Array structure ["RobotGuy","Logis","rookie"]
Variant v_val = values_arr[i];
String val;
val = v_val.get_construct_string();
if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
val = val.substr(1, val.length() - 2);
}
new_string = new_string.replacen(placeholder.replace("_", i_as_str), val);
}
}
} else if (values.get_type() == Variant::DICTIONARY) {
Dictionary d = values;
List<Variant> keys;
d.get_key_list(&keys);
for (List<Variant>::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 String::replace(String p_key, String p_with) const {
String new_string; String new_string;

View File

@ -120,6 +120,7 @@ public:
bool is_subsequence_ofi(const String &p_string) const; bool is_subsequence_ofi(const String &p_string) const;
Vector<String> bigrams() const; Vector<String> bigrams() const;
float similarity(const String &p_string) 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_first(String p_key, String p_with) const;
String replace(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; String replacen(String p_key, String p_with) const;

View File

@ -241,6 +241,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(String, is_subsequence_ofi); VCALL_LOCALMEM1R(String, is_subsequence_ofi);
VCALL_LOCALMEM0R(String, bigrams); VCALL_LOCALMEM0R(String, bigrams);
VCALL_LOCALMEM1R(String, similarity); VCALL_LOCALMEM1R(String, similarity);
VCALL_LOCALMEM2R(String, format);
VCALL_LOCALMEM2R(String, replace); VCALL_LOCALMEM2R(String, replace);
VCALL_LOCALMEM2R(String, replacen); VCALL_LOCALMEM2R(String, replacen);
VCALL_LOCALMEM2R(String, insert); VCALL_LOCALMEM2R(String, insert);
@ -1323,6 +1324,7 @@ void register_variant_methods() {
ADDFUNC0(STRING, STRING_ARRAY, String, bigrams, varray()); ADDFUNC0(STRING, STRING_ARRAY, String, bigrams, varray());
ADDFUNC1(STRING, REAL, String, similarity, STRING, "text", 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, replace, STRING, "what", STRING, "forwhat", varray());
ADDFUNC2(STRING, STRING, String, replacen, 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()); ADDFUNC2(STRING, STRING, String, insert, INT, "pos", STRING, "what", varray());