Clean up some uses of String::substr
#102427
Open
+124
−124
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.
Cases where the end position is either equvalent to the default or past the end of the string.
Cases:
X.substr(Y, X.length())
(which is always equivalent or beyond the end)X.substr(Y, X.length() - Y)
,X.substr(Y, X.size() - (Y + 1))
(which are always equivalent)X.substr(Y, X.size())
orX.substr(Y, X.size() - Y)
(which is always beyond the end)X.substr(Y, -1)
(which is the default)For context,
X.substr(Y, -1)
is equivalent toX.substr(Y, X.length() - Y)
, and if the second argument is greater thanX.length() - Y
it is clamped, so equivalent to the default again. Note thatX.size()
is alwaysX.length() + 1
unlessX.length() == 0
(in which casesubstr
returns empty)This communicates this intent more clearly, and avoids unnecessary operations, and avoids errors due to copying the code or minor changes down the line.
Didn't try to identify cases equivalent to this where the second argument was a variable.