Skip to content

Commit

Permalink
Deprecate usage of nested Arrays as key-value pairs in String.format()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickeon committed Jun 30, 2024
1 parent 4ab8fb8 commit 8ec14c2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3946,10 +3946,12 @@ String String::format(const Variant &values, const String &placeholder) const {
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]]
#ifndef DISABLE_DEPRECATED
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) {
WARN_DEPRECATED_MSG("Arrays structured as key-value pairs in String.format() are deprecated. Consider using a Dictionary instead.");
Variant v_key = value_arr[0];
String key = v_key;

Expand All @@ -3960,15 +3962,18 @@ String String::format(const Variant &values, const String &placeholder) const {
} 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 = v_val;
continue;
}
#endif // DISABLE_DEPRECATED

if (placeholder.contains("_")) {
new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
} else {
new_string = new_string.replace_first(placeholder, val);
}
// Array structure ["RobotGuy", "Logis", "rookie"].
Variant v_val = values_arr[i];
String val = v_val;

if (placeholder.contains("_")) {
new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
} else {
new_string = new_string.replace_first(placeholder, val);
}
}
} else if (values.get_type() == Variant::DICTIONARY) {
Expand Down

0 comments on commit 8ec14c2

Please sign in to comment.