<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2217: operator==(sub_match, string) slices on embedded '\0's</title>
<meta property="og:title" content="Issue 2217: operator==(sub_match, string) slices on embedded '\0's">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2217.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++17">C++17</a> status.</em></p>
<h3 id="2217"><a href="lwg-defects.html#2217">2217</a>. <code>operator==(sub_match, string)</code> slices on embedded <code>'\0'</code>s</h3>
<p><b>Section:</b> 28.6.8.3 <a href="https://wg21.link/re.submatch.op">[re.submatch.op]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Jeffrey Yasskin <b>Opened:</b> 2012-11-26 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#re.submatch.op">issues</a> in [re.submatch.op].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>

<blockquote><pre>
template &lt;class BiIter, class ST, class SA&gt;
  bool operator==(
    const basic_string&lt;
      typename iterator_traits&lt;BiIter&gt;::value_type, ST, SA&gt;&amp; lhs,
    const sub_match&lt;BiIter&gt;&amp; rhs);
</pre></blockquote>
<p>
is specified as:
</p>
<blockquote><p>
<i>Returns</i>: <code>rhs.compare(lhs.c_str()) == 0</code>.
</p></blockquote>
<p>
This is odd because <code>sub_match::compare(basic_string)</code> is defined to
honor embedded <code>'\0'</code> characters. This could allow a <code>sub_match</code> to <code>==</code> or
<code>!=</code> a <code>std::string</code> unexpectedly.
</p>

<p><i>[Daniel:]</i></p>

<p>
This wording change was done intentionally as of LWG <a href="lwg-defects.html#1181" title="Invalid sub_match comparison operators (Status: C++11)">1181</a><sup><a href="https://cplusplus.github.io/LWG/issue1181" title="Latest snapshot">(i)</a></sup>, but the here mentioned slicing
effect was not considered at that time. It seems best to use another overload of compare to fix this problem:
</p>
<blockquote><p>
<i>Returns</i>: <code>rhs.str().compare(0, rhs.length(), lhs.data(), lhs.size()) == 0</code>.
</p></blockquote>
<p>
or
</p>
<blockquote><p>
<i>Returns</i>: <code>rhs.compare(sub_match&lt;BiIter&gt;::string_type(lhs.data(), lhs.size())) == 0</code>.
</p></blockquote>

<p><i>[2013-10-17: Daniel provides concrete wording]</i></p>


<p>
The original wording was suggested to reduce the need to allocate memory during comparisons. The specification would be
very much easier, if <code>sub_match</code> would provide an additional <code>compare</code> overload of the form:
</p>
<blockquote><pre>
int compare(const value_type* s, size_t n) const;
</pre></blockquote>
<p>
But given the fact that currently <em>all</em> of <code>basic_string</code>'s <code>compare</code> overloads are defined in terms
of temporary string constructions, the following proposed wording does follow the same string-construction route as 
<code>basic_string</code> does (where needed to fix the embedded zeros issue) and to hope that existing implementations
ignore to interpret this semantics in the literal sense.
<p/>
I decided to use the second replacement form
</p>
<blockquote><pre>
<i>Returns</i>: <code>rhs.compare(sub_match&lt;BiIter&gt;::string_type(lhs.data(), lhs.size())) == 0</code>.
</pre></blockquote>
<p>
because it already reflects the existing style used in 28.6.8.3 <a href="https://wg21.link/re.submatch.op">[re.submatch.op]</a> p31.
</p>


<p><i>[2014-02-15 post-Issaquah session : move to Tentatively Ready]</i></p>




<p id="res-2217"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3691.</p>

<ol>
<li><p>Change 28.6.8.3 <a href="https://wg21.link/re.submatch.op">[re.submatch.op]</a> as indicated:</p>

<blockquote><pre>
template &lt;class BiIter, class ST, class SA&gt;
  bool operator==(
    const basic_string&lt;
      typename iterator_traits&lt;BiIter&gt;::value_type, ST, SA&gt;&amp; lhs,
    const sub_match&lt;BiIter&gt;&amp; rhs);
</pre><blockquote>
<p>
-7- <i>Returns:</i> <code>rhs.compare(<del>lhs.c_str()</del><ins>typename sub_match&lt;BiIter&gt;::string_type(lhs.data(), lhs.size())</ins>) == 0</code>.
</p>
</blockquote></blockquote>
<p>
[&hellip;]
</p>
<blockquote><pre>
template &lt;class BiIter, class ST, class SA&gt;
  bool operator&lt;(
    const basic_string&lt;
      typename iterator_traits&lt;BiIter&gt;::value_type, ST, SA&gt;&amp; lhs,
    const sub_match&lt;BiIter&gt;&amp; rhs);
</pre><blockquote>
<p>
-9- <i>Returns:</i> <code>rhs.compare(<del>lhs.c_str()</del><ins>typename sub_match&lt;BiIter&gt;::string_type(lhs.data(), lhs.size())</ins>) &gt; 0</code>.
</p>
</blockquote></blockquote>
<p>
[&hellip;]
</p>
<blockquote><pre>
template &lt;class BiIter, class ST, class SA&gt;
  bool operator==(const sub_match&lt;BiIter&gt;&amp; lhs,
                  const basic_string&lt;
                    typename iterator_traits&lt;BiIter&gt;::value_type, ST, SA>&amp; rhs);
</pre><blockquote>
<p>
-13- <i>Returns:</i> <code>lhs.compare(<del>rhs.c_str()</del><ins>typename sub_match&lt;BiIter&gt;::string_type(rhs.data(), rhs.size())</ins>) == 0</code>.
</p>
</blockquote></blockquote>
<p>
[&hellip;]
</p>
<blockquote><pre>
template &lt;class BiIter, class ST, class SA&gt;
  bool operator&lt;(const sub_match&lt;BiIter&gt;&amp; lhs,
                 const basic_string&lt;
                   typename iterator_traits&lt;BiIter&gt;::value_type, ST, SA>&amp; rhs);
</pre><blockquote>
<p>
-15- <i>Returns:</i> <code>lhs.compare(<del>rhs.c_str()</del><ins>typename sub_match&lt;BiIter&gt;::string_type(rhs.data(), rhs.size())</ins>) &lt; 0</code>.
</p>
</blockquote></blockquote>

<blockquote><pre>
</pre><blockquote>
<p>
</p>
</blockquote></blockquote>

<blockquote><pre>
</pre><blockquote>
<p>
</p>
</blockquote></blockquote>

<blockquote><pre>
</pre><blockquote>
<p>
</p>
</blockquote></blockquote>

<blockquote><pre>
</pre><blockquote>
<p>
</p>
</blockquote></blockquote>
</li>
</ol>






</body>
</html>
