-
Suggestion
-
Resolution: Unresolved
-
P2: Important
-
None
-
None
-
None
We often have code that does the following operation:
if (str.startsWith(x)) {
doSomethingWith(str.first(x.size()));
str.slice(x.size());
}
ie. it checks for a prefix, x, and consumes it before the next step. Ditto endsWith() and last()/chop().
This works fine in a world where everything is UTF-16. It even still works if you throw L1 into the mix, because L1 is a fixed-width encoding and those Unicode characters that require more than one UTF-16 code point never match L1, so lengths are still consistent between L1 and UTF-16.
But if one of str xor x is UTF-8, this code breaks apart.
This is an example of a function that throws away useful (and hard to calculate) information without returning it. In the implementation of startsWith, the equivalent end of x in *this is well-known. But instead of returning this information as a prefix string-view, we throw it away.
So add a function that returns this information, and is otherwise a drop-in replacement for startsWith()/endsWith(). That means the new function returns an optional-like structure that, if engaged, contains the subset of *this that matched as a view.
Acceptance criteria:
- decide whether to extend the existing beginsWith()/endsWith() or to add a new function
- add functionality to all strings and views that have startsWith()/endsWith()
- document it
- test it
- port some users in Qt
- (say at least one of prefix/suffix for each of the classes that get the new functionality
- split from
-
QTBUG-135629 String View Additions
-
- Reported
-