Rename String::size
to String::buffer_size
, to prevent misinterpretation. Use length()
instead where appropriate.
#105753
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #105710.
String::size
is error prone and misleading. I am proposing to rename it tobuffer_size
to make its purpose clearer.String::size
is luckily not exposed to GDExtension or GDScript.This PR is likely to both fix subtle bugs and cause regressions (though less likely than #105710). I recommend merging it soon (early in the 4.5 release cycle).
Explanation
I was investigating why #102369 failed, when I realized that empty strings have a dichotomous state:
_cowdata
is empty (_ptr == nullptr
)_cowdata
is allocated and_size
is 1. The only character is used for the trailing 0 (NUL
terminator).This peculiarity does not seem to be widely known. It is currently common to check for
String
emptiness like this:godot/drivers/gles3/shader_gles3.cpp
Line 179 in 931820d
This is incorrect because the
String
in question might be empty but allocated, whereuponsize()
would return 1. It should callif (p_version->uniforms.length())
instead (or better,if (!p_version->uniforms.is_empty())
).Additionally,
size
is currently often mistaken forlength
in the codebase. While making this PR, I found many subtle bugs of code that was iterating over the trailingNUL
character and (potentially) inserting it into strings or other data.By renaming to
buffer_size
, it's clearer that this should only be used when interacting with the buffer directly (such as when usingresize
and filling contents).Changes
Interface:
size()
is renamed tobuffer_size()
inustring.h
get_data()
is moved to the header to allow inlining.Callers:
size() - 1
is almost always replaced bylength()
if (size())
is replaced byif (length())
(could be!is_empty
but that's out of scope)size()
is replaced bylength()
if it is obviously more correctsize()
is replaced bybuffer_size()
in other cases.Addendum
We should resolve the dichotomy mentioned above. Empty
String
s should not allocate. Without the dichotomy, #102369 can also be merged for a pretty significant speed boost.This will unfortunately require a little bit of a compatibility breakage, so I wanted to do this PR first.