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

Expose String.resize() #79177

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,7 @@ static void _register_variant_builtin_methods() {
bind_string_method(to_utf32_buffer, sarray(), varray());
bind_string_method(hex_decode, sarray(), varray());
bind_string_method(to_wchar_buffer, sarray(), varray());
bind_method(String, resize, sarray("size"), varray());

bind_static_method(String, num_scientific, sarray("number"), varray());
bind_static_method(String, num, sarray("number", "decimals"), varray(-1));
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,13 @@
Replaces all [b]case-insensitive[/b] occurrences of [param what] inside the string with the given [param forwhat].
</description>
</method>
<method name="resize">
<return type="int" />
<param index="0" name="size" type="int" />
<description>
Resizes this string to the new size. The size should be one more than desired string length (in order to accommodate the null terminator).
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this make the String data mutable?

In most programming languages, Strings are immutable. I think this is true for all String methods in GDScript except for this one. Wouldn't it be better to return a copy that's resized?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In GDScript, you can already do:

var s = "abc"
s[2] = 3
print(s) # will print "ab3"

This was one of the points that @bruvzg brought up on my original PR (which aimed expose this only to GDExtension and not GDScript).

Wouldn't it be better to return a copy that's resized?

Unfortunately, this would defeat the purpose of having access to resize() from GDExtension. The specific use-case that triggered the quest to expose resize was to avoid an unnecessary copy in text_server_adv when compiling it as a GDExtension.

</description>
</method>
<method name="rfind" qualifiers="const">
<return type="int" />
<param index="0" name="what" type="String" />
Expand Down