<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3040: basic_string_view::starts_with Effects are incorrect</title>
<meta property="og:title" content="Issue 3040: basic_string_view::starts_with Effects are incorrect">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3040.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="3040"><a href="lwg-defects.html#3040">3040</a>. <code>basic_string_view::starts_with</code> <i>Effects</i> are incorrect</h3>
<p><b>Section:</b> 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Marshall Clow <b>Opened:</b> 2017-11-29 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#string.view.ops">issues</a> in [string.view.ops].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The effects of <code>starts_with</code> are described as equivalent to <code>return compare(0, npos, x) == 0</code>.
<p/>
This is incorrect, because it returns <code>false</code> when you check to see if any sequence begins with the empty sequence. 
(There are other failure cases, but that one's easy)
<p/>
As a drive-by fix, we can make the <i>Effects:</i> for <code>starts_with</code> and <code>ends_with</code> clearer.
<p/>
Those are the second and proposed third changes, and they are not required.
</p>

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


<strong>Previous resolution: [SUPERSEDED]</strong>

<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4713">N4713</a>.</p>

<ol>
<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p20 as indicated:</p>

<blockquote>
<pre>constexpr bool starts_with(basic_string_view x) const noexcept;</pre>
<blockquote>
<p>
-20- <i>Effects:</i> Equivalent to: <code>return <ins>size() &gt;= x.size() &amp;&amp;</ins> 
compare(0, <del>npos</del><ins>x.size()</ins>, x) == 0;</code>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p21 as indicated:</p>
<blockquote>
<pre>constexpr bool starts_with(charT x) const noexcept;</pre>
<blockquote>
<p>
-21- <i>Effects:</i> Equivalent to: <code>return <ins>!empty() &amp;&amp; 
traits::eq(front(), x)</ins><del>starts_with(basic_string_view(&amp;x, 1))</del>;</code>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p24 as indicated:</p>

<blockquote>
<pre>constexpr bool ends_with(charT x) const noexcept;</pre>
<blockquote>
<p>
-24- <i>Effects:</i> Equivalent to: <code>return <ins>!empty() &amp;&amp; traits::eq(back(), 
x)</ins><del>ends_with(basic_string_view(&amp;x, 1))</del>;</code>
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

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

<p>
The currently suggested wording has:
</p>
<blockquote><p>
<i>Effects:</i> Equivalent to: <code>return size() &gt;= x.size() &amp;&amp; compare(0, x.size(), x) == 0;</code>
</p></blockquote>
<p>
but <code>compare()</code> already does the <code>size() &gt;= x.size()</code> check.
<p/>
It seems like it should say:
</p>
<blockquote><p>
<i>Effects:</i> Equivalent to: <code>return substr(0, x.size()) == x;</code>
</p></blockquote>

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




<p id="res-3040"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4713">N4713</a>.</p>

<ol>
<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p20 as indicated:</p>

<blockquote>
<pre>constexpr bool starts_with(basic_string_view x) const noexcept;</pre>
<blockquote>
<p>
-20- <i>Effects:</i> Equivalent to: <code>return <ins>substr(0, x.size()) == x</ins><del>compare(0, 
npos, x) == 0</del>;</code>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p21 as indicated:</p>
<blockquote>
<pre>constexpr bool starts_with(charT x) const noexcept;</pre>
<blockquote>
<p>
-21- <i>Effects:</i> Equivalent to: <code>return <ins>!empty() &amp;&amp; 
traits::eq(front(), x)</ins><del>starts_with(basic_string_view(&amp;x, 1))</del>;</code>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a> p24 as indicated:</p>

<blockquote>
<pre>constexpr bool ends_with(charT x) const noexcept;</pre>
<blockquote>
<p>
-24- <i>Effects:</i> Equivalent to: <code>return <ins>!empty() &amp;&amp; traits::eq(back(), 
x)</ins><del>ends_with(basic_string_view(&amp;x, 1))</del>;</code>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
