Title
basic_string_view::starts_with Effects are incorrect
Status
c++20
Section
[string.view.ops]
Submitter
Marshall Clow

Created on 2017-11-29.00:00:00 last changed 45 months ago

Messages

Date: 2018-10-29.17:30:42

Proposed resolution:

This wording is relative to N4713.

  1. Change [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 [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 [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));

Date: 2018-10-29.00:00:00

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

Date: 2018-01-15.00:00:00

[ 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;

Date: 2017-12-13.00:00:00

[ 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 [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 [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 [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));

Date: 2017-11-29.00:00:00

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.

History
Date User Action Args
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2019-02-26 17:40:23adminsetstatus: voting -> wp
2019-01-21 04:50:04adminsetstatus: ready -> voting
2018-10-29 17:30:42adminsetmessages: + msg10170
2018-10-29 17:30:42adminsetstatus: open -> ready
2018-01-24 20:16:41adminsetmessages: + msg9628
2018-01-24 20:16:41adminsetstatus: ready -> open
2017-12-13 18:11:46adminsetmessages: + msg9587
2017-12-13 18:11:46adminsetstatus: new -> ready
2017-12-02 13:38:19adminsetmessages: + msg9575
2017-11-29 00:00:00admincreate