Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to construct a NodePath from a StringName #72702

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/string/node_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ NodePath NodePath::simplified() const {
return np;
}

NodePath NodePath::from_string_name(const StringName &p_string_name) {
String string = String(p_string_name);
const int size = string.size();
if (size == 0) {
return NodePath();
}
// Check if p_string_name contains a slash or colon. If so, we need to parse it.
const char32_t *chars = string.ptr();
for (int i = 0; i < size; i++) {
if (unlikely(chars[i] == '/' || chars[i] == ':')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me think it'd be interesting to see if we should add a findmc to complement findmk, and perhaps a contains_any or similar, there's many places that look for "is any of these present" and it'd be a good feature, just to think about I might look at an implementation later

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that require constructing a Vector<char32_t>, so less efficient than as it is now?

But in general, that seems like a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For two arguments I'd tend to agree

return NodePath(string);
}
}
// If there is no colon or slash, the desired NodePath has one StringName.
// Therefore we can avoid all the String parsing and StringName re-creation.
Vector<StringName> path;
path.push_back(p_string_name);
NodePath ret = NodePath(path, false);
ret.data->concatenated_path = p_string_name;
return ret;
}

NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
if (p_path.size() == 0 && !p_absolute) {
return;
Expand Down
2 changes: 2 additions & 0 deletions core/string/node_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class NodePath {
void simplify();
NodePath simplified() const;

static NodePath from_string_name(const StringName &p_string_name);

NodePath(const Vector<StringName> &p_path, bool p_absolute);
NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute);
NodePath(const NodePath &p_path);
Expand Down
4 changes: 4 additions & 0 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
case NODE_PATH: {
static const Type valid[] = {
STRING,
STRING_NAME,
NIL
};

Expand Down Expand Up @@ -733,6 +734,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type
case NODE_PATH: {
static const Type valid[] = {
STRING,
STRING_NAME,
NIL
};

Expand Down Expand Up @@ -2129,6 +2131,8 @@ Variant::operator NodePath() const {
return *reinterpret_cast<const NodePath *>(_data._mem);
} else if (type == STRING) {
return NodePath(operator String());
} else if (type == STRING_NAME) {
return NodePath::from_string_name(operator StringName());
} else {
return NodePath();
}
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,7 @@ static void _register_variant_builtin_methods_misc() {
bind_method(NodePath, slice, sarray("begin", "end"), varray(INT_MAX));
bind_method(NodePath, get_as_property_path, sarray(), varray());
bind_method(NodePath, is_empty, sarray(), varray());
bind_static_method(NodePath, from_string_name, sarray("string_name"), varray());

/* Callable */

Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void Variant::_register_variant_constructors() {
add_constructor<VariantConstructNoArgs<NodePath>>(sarray());
add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from"));
add_constructor<VariantConstructor<NodePath, String>>(sarray("from"));
add_constructor<VariantConstructor<NodePath, StringName>>(sarray("from"));

add_constructor<VariantConstructNoArgs<::RID>>(sarray());
add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from"));
Expand Down
14 changes: 14 additions & 0 deletions doc/classes/NodePath.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,22 @@
[b]Note:[/b] In GDScript, it's also possible to convert a constant string into a node path by prefixing it with [code]^[/code]. [code]^"path/to/node"[/code] is equivalent to [code]NodePath("path/to/node")[/code].
</description>
</constructor>
<constructor name="NodePath">
<return type="NodePath" />
<param index="0" name="from" type="StringName" />
<description>
Constructs a NodePath from a [StringName]. This method will be faster than constructing from a String if the StringName does not contain any slashes or colons.
AThousandShips marked this conversation as resolved.
Show resolved Hide resolved
</description>
</constructor>
</constructors>
<methods>
<method name="from_string_name" qualifiers="static">
<return type="NodePath" />
<param index="0" name="string_name" type="StringName" />
<description>
Constructs a NodePath from a [StringName]. This method will be faster than constructing from a String if the StringName does not contain any slashes or colons.
</description>
</method>
<method name="get_as_property_path" qualifiers="const">
<return type="NodePath" />
<description>
Expand Down
69 changes: 69 additions & 0 deletions tests/core/string/test_node_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,75 @@ TEST_CASE("[NodePath] Empty path") {
"The node path should be considered empty.");
}

TEST_CASE("[NodePath] From StringName") {
const StringName sname = StringName("Path2D");

// Test the case where the StringName has no slash or colon and uses the StringName directly.
const NodePath node_path_sname = NodePath::from_string_name(sname);
const NodePath node_path_string = NodePath("Path2D");

CHECK_MESSAGE(
!node_path_sname.is_absolute(),
"The StringName version should not be absolute.");

CHECK_MESSAGE(
!node_path_string.is_absolute(),
"The String version should not be absolute.");

CHECK_MESSAGE(
node_path_sname.get_name(0) == sname,
"The StringName version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_string.get_name(0) == sname,
"The String version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_sname.get_concatenated_names() == sname,
"The StringName version should give the expected StringName.");

CHECK_MESSAGE(
node_path_string.get_concatenated_names() == sname,
"The String version should give the expected StringName.");

CHECK_MESSAGE(
node_path_string.get_concatenated_names() == node_path_sname.get_concatenated_names(),
"The String and StringName versions should give the same result.");

// Test the case where the StringName has a slash or colon and uses String parsing.
const NodePath node_path_abs_sname = NodePath::from_string_name("/Path2D");
const NodePath node_path_abs_string = NodePath("/Path2D");

CHECK_MESSAGE(
node_path_abs_sname.is_absolute(),
"The StringName version should be absolute.");

CHECK_MESSAGE(
node_path_abs_string.is_absolute(),
"The String version should be absolute.");

CHECK_MESSAGE(
node_path_abs_sname.get_name(0) == sname,
"The StringName version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_abs_string.get_name(0) == sname,
"The String version should have the expected StringName as the first name.");

// Absolute paths do not include a leading slash in the concatenated names.
CHECK_MESSAGE(
node_path_abs_sname.get_concatenated_names() == sname,
"The StringName version should give the expected StringName.");

CHECK_MESSAGE(
node_path_abs_string.get_concatenated_names() == sname,
"The String version should give the expected StringName.");

CHECK_MESSAGE(
node_path_abs_string.get_concatenated_names() == node_path_abs_sname.get_concatenated_names(),
"The String and StringName versions should give the same result.");
}

TEST_CASE("[NodePath] Slice") {
const NodePath node_path_relative = NodePath("Parent/Child:prop");
const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
Expand Down
Loading