Skip to content

Commit 154049c

Browse files
committed
StringName Dictionary keys
also added 'is_string()' method to Variant and refactored many String type comparisons to use it instead
1 parent 40b378e commit 154049c

34 files changed

+65
-82
lines changed

core/object/object.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::Cal
605605
return Variant();
606606
}
607607

608-
if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
608+
if (!p_args[0]->is_string()) {
609609
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
610610
r_error.argument = 0;
611611
r_error.expected = Variant::STRING_NAME;
@@ -624,7 +624,7 @@ Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Call
624624
return Variant();
625625
}
626626

627-
if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
627+
if (!p_args[0]->is_string()) {
628628
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
629629
r_error.argument = 0;
630630
r_error.expected = Variant::STRING_NAME;
@@ -720,7 +720,7 @@ Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
720720
*r_valid = false;
721721
}
722722

723-
if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
723+
if (p_key.is_string()) {
724724
return get(p_key, r_valid);
725725
}
726726
return Variant();
@@ -730,7 +730,7 @@ void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid)
730730
if (r_valid) {
731731
*r_valid = false;
732732
}
733-
if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
733+
if (p_key.is_string()) {
734734
return set(p_key, p_value, r_valid);
735735
}
736736
}
@@ -1096,7 +1096,7 @@ Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::Cal
10961096
ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
10971097
}
10981098

1099-
if (unlikely(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING)) {
1099+
if (unlikely(!p_args[0]->is_string())) {
11001100
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
11011101
r_error.argument = 0;
11021102
r_error.expected = Variant::STRING_NAME;

core/variant/dictionary.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,15 @@ Variant Dictionary::get_value_at_index(int p_index) const {
8181

8282
Variant &Dictionary::operator[](const Variant &p_key) {
8383
if (unlikely(_p->read_only)) {
84-
if (p_key.get_type() == Variant::STRING_NAME) {
85-
const StringName *sn = VariantInternal::get_string_name(&p_key);
86-
const String &key = sn->operator String();
87-
if (likely(_p->variant_map.has(key))) {
88-
*_p->read_only = _p->variant_map[key];
89-
} else {
90-
*_p->read_only = Variant();
91-
}
92-
} else if (likely(_p->variant_map.has(p_key))) {
84+
if (likely(_p->variant_map.has(p_key))) {
9385
*_p->read_only = _p->variant_map[p_key];
9486
} else {
9587
*_p->read_only = Variant();
9688
}
9789

9890
return *_p->read_only;
9991
} else {
100-
if (p_key.get_type() == Variant::STRING_NAME) {
101-
const StringName *sn = VariantInternal::get_string_name(&p_key);
102-
return _p->variant_map[sn->operator String()];
103-
} else {
104-
return _p->variant_map[p_key];
105-
}
92+
return _p->variant_map[p_key];
10693
}
10794
}
10895

core/variant/variant_construct.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ template <typename T>
232232
class VariantConstructorFromString {
233233
public:
234234
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
235-
if (p_args[0]->get_type() != Variant::STRING) {
235+
if (!p_args[0]->is_string()) {
236236
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
237237
r_error.argument = 0;
238238
r_error.expected = Variant::STRING;
239239
return;
240240
}
241241

242242
VariantTypeChanger<T>::change(&r_ret);
243-
const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]);
243+
const String src_str = *p_args[0];
244244

245245
if (r_ret.get_type() == Variant::Type::INT) {
246246
r_ret = src_str.to_int();
@@ -417,7 +417,7 @@ class VariantConstructorTypedArray {
417417
return;
418418
}
419419

420-
if (p_args[2]->get_type() != Variant::STRING_NAME) {
420+
if (!p_args[2]->is_string()) {
421421
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
422422
r_error.argument = 2;
423423
r_error.expected = Variant::STRING_NAME;
@@ -426,8 +426,7 @@ class VariantConstructorTypedArray {
426426

427427
const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
428428
const uint32_t type = p_args[1]->operator uint32_t();
429-
const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
430-
r_ret = Array(base_arr, type, class_name, *p_args[3]);
429+
r_ret = Array(base_arr, type, *p_args[2], *p_args[3]);
431430
}
432431

433432
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {

core/variant/variant_setget.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,8 @@ void Variant::get_property_list(List<PropertyInfo> *p_list) const {
12881288
List<Variant> keys;
12891289
dic->get_key_list(&keys);
12901290
for (const Variant &E : keys) {
1291-
if (E.get_type() == Variant::STRING) {
1292-
p_list->push_back(PropertyInfo(Variant::STRING, E));
1291+
if (E.is_string()) {
1292+
p_list->push_back(PropertyInfo(dic->get_valid(E).get_type(), E));
12931293
}
12941294
}
12951295
} else if (type == OBJECT) {

editor/debugger/editor_debugger_inspector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
151151
Variant &var = property.second;
152152

153153
if (pinfo.type == Variant::OBJECT) {
154-
if (var.get_type() == Variant::STRING) {
154+
if (var.is_string()) {
155155
String path = var;
156156
if (path.contains("::")) {
157157
// built-in resource

editor/editor_build_profile.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ void EditorBuildProfileManager::_class_list_item_selected() {
649649
}
650650

651651
Variant md = item->get_metadata(0);
652-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
652+
if (md.is_string()) {
653653
description_bit->parse_symbol("class|" + md.operator String() + "|");
654654
} else if (md.get_type() == Variant::INT) {
655655
String build_option_description = EditorBuildProfile::get_build_option_description(EditorBuildProfile::BuildOption((int)md));
@@ -670,7 +670,7 @@ void EditorBuildProfileManager::_class_list_item_edited() {
670670
bool checked = item->is_checked(0);
671671

672672
Variant md = item->get_metadata(0);
673-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
673+
if (md.is_string()) {
674674
String class_selected = md;
675675
edited->set_disable_class(class_selected, !checked);
676676
_update_edited_profile();
@@ -691,7 +691,7 @@ void EditorBuildProfileManager::_class_list_item_collapsed(Object *p_item) {
691691
}
692692

693693
Variant md = item->get_metadata(0);
694-
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
694+
if (!md.is_string()) {
695695
return;
696696
}
697697

@@ -706,7 +706,7 @@ void EditorBuildProfileManager::_update_edited_profile() {
706706

707707
if (class_list->get_selected()) {
708708
Variant md = class_list->get_selected()->get_metadata(0);
709-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
709+
if (md.is_string()) {
710710
class_selected = md;
711711
} else if (md.get_type() == Variant::INT) {
712712
build_option_selected = md;

editor/editor_feature_profile.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ void EditorFeatureProfileManager::_class_list_item_selected() {
558558
}
559559

560560
Variant md = item->get_metadata(0);
561-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
561+
if (md.is_string()) {
562562
description_bit->parse_symbol("class|" + md.operator String() + "|");
563563
} else if (md.get_type() == Variant::INT) {
564564
String feature_description = EditorFeatureProfile::get_feature_description(EditorFeatureProfile::Feature((int)md));
@@ -643,7 +643,7 @@ void EditorFeatureProfileManager::_class_list_item_edited() {
643643
bool checked = item->is_checked(0);
644644

645645
Variant md = item->get_metadata(0);
646-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
646+
if (md.is_string()) {
647647
String class_selected = md;
648648
edited->set_disable_class(class_selected, !checked);
649649
_save_and_update();
@@ -666,7 +666,7 @@ void EditorFeatureProfileManager::_class_list_item_collapsed(Object *p_item) {
666666
}
667667

668668
Variant md = item->get_metadata(0);
669-
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
669+
if (!md.is_string()) {
670670
return;
671671
}
672672

@@ -686,7 +686,7 @@ void EditorFeatureProfileManager::_property_item_edited() {
686686
}
687687

688688
Variant md = class_item->get_metadata(0);
689-
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
689+
if (!md.is_string()) {
690690
return;
691691
}
692692

@@ -699,7 +699,7 @@ void EditorFeatureProfileManager::_property_item_edited() {
699699
bool checked = item->is_checked(0);
700700

701701
md = item->get_metadata(0);
702-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
702+
if (md.is_string()) {
703703
String property_selected = md;
704704
edited->set_disable_class_property(class_name, property_selected, !checked);
705705
_save_and_update();
@@ -732,7 +732,7 @@ void EditorFeatureProfileManager::_update_selected_profile() {
732732

733733
if (class_list->get_selected()) {
734734
Variant md = class_list->get_selected()->get_metadata(0);
735-
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
735+
if (md.is_string()) {
736736
class_selected = md;
737737
} else if (md.get_type() == Variant::INT) {
738738
feature_selected = md;

editor/editor_undo_redo_manager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcoun
177177
return;
178178
}
179179

180-
if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
180+
if (!p_args[1]->is_string()) {
181181
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
182182
r_error.argument = 1;
183183
r_error.expected = Variant::STRING_NAME;
@@ -206,7 +206,7 @@ void EditorUndoRedoManager::_add_undo_method(const Variant **p_args, int p_argco
206206
return;
207207
}
208208

209-
if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
209+
if (!p_args[1]->is_string()) {
210210
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
211211
r_error.argument = 1;
212212
r_error.expected = Variant::STRING_NAME;

editor/plugins/visual_shader_editor_plugin.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,7 @@ void VisualShaderEditor::_update_options_menu() {
22012201
if (input.is_valid()) {
22022202
input->set_shader_mode(visual_shader->get_mode());
22032203
input->set_shader_type(visual_shader->get_shader_type());
2204-
if (!add_options[i].ops.is_empty() && add_options[i].ops[0].get_type() == Variant::STRING) {
2204+
if (!add_options[i].ops.is_empty() && add_options[i].ops[0].is_string()) {
22052205
input->set_input_name((String)add_options[i].ops[0]);
22062206
}
22072207
}
@@ -3281,7 +3281,7 @@ void VisualShaderEditor::_setup_node(VisualShaderNode *p_node, const Vector<Vari
32813281
VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(p_node);
32823282

32833283
if (input) {
3284-
ERR_FAIL_COND(p_ops[0].get_type() != Variant::STRING);
3284+
ERR_FAIL_COND(!p_ops[0].is_string());
32853285
input->set_input_name((String)p_ops[0]);
32863286
return;
32873287
}

modules/gdscript/editor/gdscript_translation_parser_plugin.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Ve
7171

7272
bool GDScriptEditorTranslationParserPlugin::_is_constant_string(const GDScriptParser::ExpressionNode *p_expression) {
7373
ERR_FAIL_NULL_V(p_expression, false);
74-
return p_expression->is_constant && (p_expression->reduced_value.get_type() == Variant::STRING || p_expression->reduced_value.get_type() == Variant::STRING_NAME);
74+
return p_expression->is_constant && p_expression->reduced_value.is_string();
7575
}
7676

7777
void GDScriptEditorTranslationParserPlugin::_traverse_class(const GDScriptParser::ClassNode *p_class) {

modules/gdscript/gdscript_editor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context,
19971997
}
19981998

19991999
// Look for valid indexing in other types
2000-
if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
2000+
if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) {
20012001
StringName id = index.value;
20022002
found = _guess_identifier_type_from_base(c, base, id, r_type);
20032003
} else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {

modules/gdscript/gdscript_utility_functions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ struct GDScriptUtilityFunctionsDefinitions {
232232

233233
static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
234234
VALIDATE_ARG_COUNT(1);
235-
if (p_args[0]->get_type() != Variant::STRING) {
235+
if (!p_args[0]->is_string()) {
236236
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
237237
r_error.argument = 0;
238238
r_error.expected = Variant::STRING;

modules/gdscript/language_server/gdscript_text_document.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
309309
params.load(p_params["data"]);
310310
symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);
311311

312-
} else if (data.get_type() == Variant::STRING) {
312+
} else if (data.is_string()) {
313313
String query = data;
314314

315315
Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);

modules/gdscript/language_server/godot_lsp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ struct CompletionItem {
10641064
}
10651065
if (p_dict.has("documentation")) {
10661066
Variant doc = p_dict["documentation"];
1067-
if (doc.get_type() == Variant::STRING) {
1067+
if (doc.is_string()) {
10681068
documentation.value = doc;
10691069
} else if (doc.get_type() == Variant::DICTIONARY) {
10701070
Dictionary v = doc;

modules/gdscript/tests/scripts/analyzer/warnings/confusable_capture_reassignment.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ GDTEST_OK
1515
>> Line: 16
1616
>> CONFUSABLE_CAPTURE_REASSIGNMENT
1717
>> Reassigning lambda capture does not modify the outer local variable "array_assign".
18-
lambda 2 2 12 (2, 0) [2] [2] { "x": 2 }
19-
outer 2 1 1 (1, 0) [1] [2] { "x": 2 }
18+
lambda 2 2 12 (2, 0) [2] [2] { &"x": 2 }
19+
outer 2 1 1 (1, 0) [1] [2] { &"x": 2 }
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDTEST_OK
2-
{ "a": 1, "b": 2, "with spaces": 3, "2": 4 }
2+
{ &"a": 1, &"b": 2, &"with spaces": 3, &"2": 4 }
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDTEST_OK
2-
{ "hello": { "world": { "is": "beautiful" } } }
2+
{ "hello": { &"world": { "is": "beautiful" } } }

modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ func test():
77
stringname_dict[&"abc"] = 24
88

99
print("String key is TYPE_STRING: ", typeof(string_dict.keys()[0]) == TYPE_STRING)
10-
print("StringName key is TYPE_STRING: ", typeof(stringname_dict.keys()[0]) == TYPE_STRING)
10+
print("StringName key is TYPE_STRING_NAME: ", typeof(stringname_dict.keys()[0]) == TYPE_STRING_NAME)
1111

1212
print("StringName gets String: ", string_dict.get(&"abc"))
1313
print("String gets StringName: ", stringname_dict.get("abc"))
1414

1515
stringname_dict[&"abc"] = 42
16-
# They compare equal because StringName keys are converted to String.
16+
# They compare equal because StringName keys are considered equivalent to String keys.
1717
print("String Dictionary == StringName Dictionary: ", string_dict == stringname_dict)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GDTEST_OK
22
String key is TYPE_STRING: true
3-
StringName key is TYPE_STRING: true
3+
StringName key is TYPE_STRING_NAME: true
44
StringName gets String: 42
55
String gets StringName: 24
66
String Dictionary == StringName Dictionary: true

modules/multiplayer/scene_rpc_interface.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void SceneRPCInterface::_parse_rpc_config(const Variant &p_config, bool p_for_no
8282
Array names = config.keys();
8383
names.sort(); // Ensure ID order
8484
for (int i = 0; i < names.size(); i++) {
85-
ERR_CONTINUE(names[i].get_type() != Variant::STRING && names[i].get_type() != Variant::STRING_NAME);
85+
ERR_CONTINUE(!names[i].is_string());
8686
String name = names[i].operator String();
8787
ERR_CONTINUE(config[name].get_type() != Variant::DICTIONARY);
8888
ERR_CONTINUE(!config[name].operator Dictionary().has("rpc_mode"));

modules/regex/regex.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int RegExMatch::_find(const Variant &p_name) const {
5454
return -1;
5555
}
5656
return i;
57-
} else if (p_name.get_type() == Variant::STRING || p_name.get_type() == Variant::STRING_NAME) {
58-
HashMap<String, int>::ConstIterator found = names.find((String)p_name);
57+
} else if (p_name.is_string()) {
58+
HashMap<String, int>::ConstIterator found = names.find(p_name);
5959
if (found) {
6060
return found->value;
6161
}

modules/text_server_adv/text_server_adv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5902,7 +5902,7 @@ _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source
59025902
int32_t value = values[i];
59035903
if (value >= 0) {
59045904
hb_feature_t feature;
5905-
if (keys[i].get_type() == Variant::STRING) {
5905+
if (keys[i].is_string()) {
59065906
feature.tag = _name_to_tag(keys[i]);
59075907
} else {
59085908
feature.tag = keys[i];

platform/android/java_class_wrapper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool JavaClass::_call_method(JavaObject *p_instance, const StringName &p_method,
9797
}
9898
} break;
9999
case ARG_TYPE_STRING: {
100-
if (p_args[i]->get_type() != Variant::STRING) {
100+
if (!p_args[i]->is_string()) {
101101
arg_expected = Variant::STRING;
102102
}
103103
} break;

platform/web/api/api.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argc
9999
r_error.expected = 1;
100100
return Ref<JavaScriptObject>();
101101
}
102-
if (p_args[0]->get_type() != Variant::STRING) {
102+
if (!p_args[0]->is_string()) {
103103
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
104104
r_error.argument = 0;
105105
r_error.expected = Variant::STRING;

platform/web/javascript_bridge_singleton.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argc
304304
r_error.expected = 1;
305305
return Ref<JavaScriptObject>();
306306
}
307-
if (p_args[0]->get_type() != Variant::STRING) {
307+
if (!p_args[0]->is_string()) {
308308
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
309309
r_error.argument = 0;
310310
r_error.expected = Variant::STRING;

0 commit comments

Comments
 (0)