Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-138531

Add removePrefix()/removeSuffix() functions to string (views) (or extend begin/endsWith())

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: P2: Important 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

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            cnn Qt Core & Network
            mmutz Marc Mutz
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes