Skip to content

Commit

Permalink
StringName Dictionary keys
Browse files Browse the repository at this point in the history
also added 'is_string()' method to Variant
and refactored many String type comparisons to use it instead
  • Loading branch information
rune-scape committed Aug 6, 2024
1 parent 3978628 commit e7b5348
Show file tree
Hide file tree
Showing 32 changed files with 60 additions and 77 deletions.
10 changes: 5 additions & 5 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::Cal
return Variant();
}

if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING_NAME;
Expand All @@ -621,7 +621,7 @@ Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Call
return Variant();
}

if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING_NAME;
Expand Down Expand Up @@ -717,7 +717,7 @@ Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
*r_valid = false;
}

if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
if (p_key.is_string()) {
return get(p_key, r_valid);
}
return Variant();
Expand All @@ -727,7 +727,7 @@ void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid)
if (r_valid) {
*r_valid = false;
}
if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
if (p_key.is_string()) {
return set(p_key, p_value, r_valid);
}
}
Expand Down Expand Up @@ -1093,7 +1093,7 @@ Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::Cal
ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
}

if (unlikely(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING)) {
if (unlikely(!p_args[0]->is_string())) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING_NAME;
Expand Down
17 changes: 2 additions & 15 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,15 @@ Variant Dictionary::get_value_at_index(int p_index) const {

Variant &Dictionary::operator[](const Variant &p_key) {
if (unlikely(_p->read_only)) {
if (p_key.get_type() == Variant::STRING_NAME) {
const StringName *sn = VariantInternal::get_string_name(&p_key);
const String &key = sn->operator String();
if (likely(_p->variant_map.has(key))) {
*_p->read_only = _p->variant_map[key];
} else {
*_p->read_only = Variant();
}
} else if (likely(_p->variant_map.has(p_key))) {
if (likely(_p->variant_map.has(p_key))) {
*_p->read_only = _p->variant_map[p_key];
} else {
*_p->read_only = Variant();
}

return *_p->read_only;
} else {
if (p_key.get_type() == Variant::STRING_NAME) {
const StringName *sn = VariantInternal::get_string_name(&p_key);
return _p->variant_map[sn->operator String()];
} else {
return _p->variant_map[p_key];
}
return _p->variant_map[p_key];
}
}

Expand Down
9 changes: 4 additions & 5 deletions core/variant/variant_construct.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ template <typename T>
class VariantConstructorFromString {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return;
}

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

if (r_ret.get_type() == Variant::Type::INT) {
r_ret = src_str.to_int();
Expand Down Expand Up @@ -417,7 +417,7 @@ class VariantConstructorTypedArray {
return;
}

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

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

static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
Expand Down
4 changes: 2 additions & 2 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,8 @@ void Variant::get_property_list(List<PropertyInfo> *p_list) const {
List<Variant> keys;
dic->get_key_list(&keys);
for (const Variant &E : keys) {
if (E.get_type() == Variant::STRING) {
p_list->push_back(PropertyInfo(Variant::STRING, E));
if (E.is_string()) {
p_list->push_back(PropertyInfo(dic->get_valid(E).get_type(), E));
}
}
} else if (type == OBJECT) {
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_debugger_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
Variant &var = property.second;

if (pinfo.type == Variant::OBJECT) {
if (var.get_type() == Variant::STRING) {
if (var.is_string()) {
String path = var;
if (path.contains("::")) {
// built-in resource
Expand Down
8 changes: 4 additions & 4 deletions editor/editor_build_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ void EditorBuildProfileManager::_class_list_item_selected() {
}

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

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

Variant md = item->get_metadata(0);
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
if (!md.is_string()) {
return;
}

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

if (class_list->get_selected()) {
Variant md = class_list->get_selected()->get_metadata(0);
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
if (md.is_string()) {
class_selected = md;
} else if (md.get_type() == Variant::INT) {
build_option_selected = md;
Expand Down
12 changes: 6 additions & 6 deletions editor/editor_feature_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void EditorFeatureProfileManager::_class_list_item_selected() {
}

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

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

Variant md = item->get_metadata(0);
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
if (!md.is_string()) {
return;
}

Expand All @@ -686,7 +686,7 @@ void EditorFeatureProfileManager::_property_item_edited() {
}

Variant md = class_item->get_metadata(0);
if (md.get_type() != Variant::STRING && md.get_type() != Variant::STRING_NAME) {
if (!md.is_string()) {
return;
}

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

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

if (class_list->get_selected()) {
Variant md = class_list->get_selected()->get_metadata(0);
if (md.get_type() == Variant::STRING || md.get_type() == Variant::STRING_NAME) {
if (md.is_string()) {
class_selected = md;
} else if (md.get_type() == Variant::INT) {
feature_selected = md;
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_undo_redo_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcoun
return;
}

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

if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
if (!p_args[1]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ void VisualShaderEditor::_update_options_menu() {
if (input.is_valid()) {
input->set_shader_mode(visual_shader->get_mode());
input->set_shader_type(visual_shader->get_shader_type());
if (!add_options[i].ops.is_empty() && add_options[i].ops[0].get_type() == Variant::STRING) {
if (!add_options[i].ops.is_empty() && add_options[i].ops[0].is_string()) {
input->set_input_name((String)add_options[i].ops[0]);
}
}
Expand Down Expand Up @@ -3174,7 +3174,7 @@ void VisualShaderEditor::_setup_node(VisualShaderNode *p_node, const Vector<Vari
VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(p_node);

if (input) {
ERR_FAIL_COND(p_ops[0].get_type() != Variant::STRING);
ERR_FAIL_COND(!p_ops[0].is_string());
input->set_input_name((String)p_ops[0]);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context,
}

// Look for valid indexing in other types
if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) {
StringName id = index.value;
found = _guess_identifier_type_from_base(c, base, id, r_type);
} else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript_utility_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ struct GDScriptUtilityFunctionsDefinitions {

static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1);
if (p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
params.load(p_params["data"]);
symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function);

} else if (data.get_type() == Variant::STRING) {
} else if (data.is_string()) {
String query = data;

Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ GDTEST_OK
>> Line: 16
>> CONFUSABLE_CAPTURE_REASSIGNMENT
>> Reassigning lambda capture does not modify the outer local variable "array_assign".
lambda 2 2 12 (2, 0) [2] [2] { "x": 2 }
outer 2 1 1 (1, 0) [1] [2] { "x": 2 }
lambda 2 2 12 (2, 0) [2] [2] { &"x": 2 }
outer 2 1 1 (1, 0) [1] [2] { &"x": 2 }
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GDTEST_OK
{ "a": 1, "b": 2, "with spaces": 3, "2": 4 }
{ &"a": 1, &"b": 2, &"with spaces": 3, &"2": 4 }
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GDTEST_OK
{ "hello": { "world": { "is": "beautiful" } } }
{ "hello": { &"world": { "is": "beautiful" } } }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func test():
stringname_dict[&"abc"] = 24

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

print("StringName gets String: ", string_dict.get(&"abc"))
print("String gets StringName: ", stringname_dict.get("abc"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GDTEST_OK
String key is TYPE_STRING: true
StringName key is TYPE_STRING: true
StringName key is TYPE_STRING_NAME: true
StringName gets String: 42
String gets StringName: 24
String Dictionary == StringName Dictionary: true
2 changes: 1 addition & 1 deletion modules/multiplayer/scene_rpc_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void SceneRPCInterface::_parse_rpc_config(const Variant &p_config, bool p_for_no
Array names = config.keys();
names.sort(); // Ensure ID order
for (int i = 0; i < names.size(); i++) {
ERR_CONTINUE(names[i].get_type() != Variant::STRING && names[i].get_type() != Variant::STRING_NAME);
ERR_CONTINUE(!names[i].is_string());
String name = names[i].operator String();
ERR_CONTINUE(config[name].get_type() != Variant::DICTIONARY);
ERR_CONTINUE(!config[name].operator Dictionary().has("rpc_mode"));
Expand Down
4 changes: 2 additions & 2 deletions modules/regex/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ int RegExMatch::_find(const Variant &p_name) const {
return -1;
}
return i;
} else if (p_name.get_type() == Variant::STRING || p_name.get_type() == Variant::STRING_NAME) {
HashMap<String, int>::ConstIterator found = names.find((String)p_name);
} else if (p_name.is_string()) {
HashMap<String, int>::ConstIterator found = names.find(p_name);
if (found) {
return found->value;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5871,7 +5871,7 @@ _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source
int32_t value = values[i];
if (value >= 0) {
hb_feature_t feature;
if (keys[i].get_type() == Variant::STRING) {
if (keys[i].is_string()) {
feature.tag = _name_to_tag(keys[i]);
} else {
feature.tag = keys[i];
Expand Down
2 changes: 1 addition & 1 deletion platform/android/java_class_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool JavaClass::_call_method(JavaObject *p_instance, const StringName &p_method,
}
} break;
case ARG_TYPE_STRING: {
if (p_args[i]->get_type() != Variant::STRING) {
if (!p_args[i]->is_string()) {
arg_expected = Variant::STRING;
}
} break;
Expand Down
2 changes: 1 addition & 1 deletion platform/web/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argc
r_error.expected = 1;
return Ref<JavaScriptObject>();
}
if (p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
Expand Down
2 changes: 1 addition & 1 deletion platform/web/javascript_bridge_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argc
r_error.expected = 1;
return Ref<JavaScriptObject>();
}
if (p_args[0]->get_type() != Variant::STRING) {
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
Expand Down
4 changes: 2 additions & 2 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,13 @@ bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const
return drop_override;
}

return is_editable() && p_data.get_type() == Variant::STRING;
return is_editable() && p_data.is_string();
}

void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
Control::drop_data(p_point, p_data);

if (p_data.get_type() == Variant::STRING && is_editable()) {
if (p_data.is_string() && is_editable()) {
set_caret_at_pixel_pos(p_point.x);
int caret_column_tmp = caret_column;
bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end;
Expand Down
Loading

0 comments on commit e7b5348

Please sign in to comment.