String Prefix and Suffix Checking

This paper proposes to add member functions starts_with and
ends_with to class templates basic_string and
basic_string_view. These functions check, whether or not a string
starts with a given prefix or ends with a given suffix, respectively.

Checking, whether or not a given string starts with a
given prefix (or ends with a given suffix) is a common task. In fact, standard
libraries of many other programming languages include routines for performing
such checks, for example:

Also, some C++ libraries (other than the standard library) that implement a
string type include such methods. For example, Qt library has classes
QString [3] and QStringRef (analogous to
std::string_view) which have startsWith and
endsWith member functions.

These functions are widely used. For example, the source code of a recent
version of Qt (excluding third-party components) has 1193 occurrences of
startsWith and 953 occurrences of endsWith. Other examples
include Webkit (304 occurrences of startsWith and 142 occurrences of
endsWith) and LLVM (class StringRef, 113 matches for
StartsWith and 38 matches for EndsWith).

The basic_string_view class template included starts_with
and ends_with up to revision 3 of the proposal (N3609
[4]). These two member functions were removed after LEWG
discussion in Bristol. The main concerns were:

This proposal adds member functions starts_with and
ends_with to class templates basic_string and
basic_string_view. Another considered option was to add free functions
to namespace std, but adding member functions is consistent with the existing
API for compare. Besides, as the original proposal [5]
mentioned, the order of parameters of a free function is
ambiguous (starts_with(string, prefix) vs starts_with(prefix,
string)).

Overload for const charT* is not included, because
basic_string_view has a non-explicit constuctor, that takes such
pointer. Likewise, overload for basic_string is not included, because
basic_string has a non-explicit conversion operator to
basic_string_view.