This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++20 status.

3040. basic_string_view::starts_with Effects are incorrect

Section: 23.3.3.8 [string.view.ops] Status: C++20 Submitter: Marshall Clow Opened: 2017-11-29 Last modified: 2021-02-25

Priority: 0

View all other issues in [string.view.ops].

View all issues with C++20 status.

Discussion:

The effects of starts_with are described as equivalent to return compare(0, npos, x) == 0.

This is incorrect, because it returns false when you check to see if any sequence begins with the empty sequence. (There are other failure cases, but that one's easy)

As a drive-by fix, we can make the Effects: for starts_with and ends_with clearer.

Those are the second and proposed third changes, and they are not required.

[ 2017-12-13 Moved to Tentatively Ready after 8 positive votes for P0 on c++std-lib. ]

Previous resolution: [SUPERSEDED]

This wording is relative to N4713.

  1. Change 23.3.3.8 [string.view.ops] p20 as indicated:

    constexpr bool starts_with(basic_string_view x) const noexcept;

    -20- Effects: Equivalent to: return size() >= x.size() && compare(0, nposx.size(), x) == 0;

  2. Change 23.3.3.8 [string.view.ops] p21 as indicated:

    constexpr bool starts_with(charT x) const noexcept;

    -21- Effects: Equivalent to: return !empty() && traits::eq(front(), x)starts_with(basic_string_view(&x, 1));

  3. Change 23.3.3.8 [string.view.ops] p24 as indicated:

    constexpr bool ends_with(charT x) const noexcept;

    -24- Effects: Equivalent to: return !empty() && traits::eq(back(), x)ends_with(basic_string_view(&x, 1));

[2018-01-23, Reopening due to a comment of Billy Robert O'Neal III requesting a change of the proposed wording]

The currently suggested wording has:

Effects: Equivalent to: return size() >= x.size() && compare(0, x.size(), x) == 0;

but compare() already does the size() >= x.size() check.

It seems like it should say:

Effects: Equivalent to: return substr(0, x.size()) == x;

[ 2018-10-29 Moved to Tentatively Ready after 5 positive votes for P0 on c++std-lib. ]

Proposed resolution:

This wording is relative to N4713.

  1. Change 23.3.3.8 [string.view.ops] p20 as indicated:

    constexpr bool starts_with(basic_string_view x) const noexcept;

    -20- Effects: Equivalent to: return substr(0, x.size()) == xcompare(0, npos, x) == 0;

  2. Change 23.3.3.8 [string.view.ops] p21 as indicated:

    constexpr bool starts_with(charT x) const noexcept;

    -21- Effects: Equivalent to: return !empty() && traits::eq(front(), x)starts_with(basic_string_view(&x, 1));

  3. Change 23.3.3.8 [string.view.ops] p24 as indicated:

    constexpr bool ends_with(charT x) const noexcept;

    -24- Effects: Equivalent to: return !empty() && traits::eq(back(), x)ends_with(basic_string_view(&x, 1));