P0457R0 String Prefix and Suffix Checking

Checking, whether or not a given string starts with a given prefix (or ends with a given suffix) is a common task.

This proposal adds two member functions

  • starts_with
  • ends_with

to class templates

  • std::basic_string
  • std::basic_string_view

Implemented in standard libraries of many other programming languages as a member of string/string-like classes. For example:

  • Python
  • Java, Scala
  • .NET langauges
  • Rust
  • Swift

Implemented in some C++ libraries:

  • Qt (QString, QStringRef)
  • boost
    • original string_ref (string_view)
    • boost::algorithm::starts_with free function

Used in various codebases

Name starts_with ends_with
Qt 1193 953
Webkit 304 142
LLVM 113 38

Discussion in Bristol

Alan was concerned about "findXXX" and starts_with/end_with functions, and suggested that they be free functions.

Sam said that he was in favor of having starts_with/end_with as member functions.

Dietmar suggested that starts_with/end_with should be in a separate proposal, and are independent of string_view. He also said that we are not designing a new interface here, we need to be more or less compatible with std::string.

Design decisions

  • Member function vs. free function
  • Overload set
    • string_view
    • charT