<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 17: Bad bool parsing</title>
<meta property="og:title" content="Issue 17: Bad bool parsing">
<meta property="og:description" content="C++ library issue. Status: TC1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue17.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#TC1">TC1</a> status.</em></p>
<h3 id="17"><a href="lwg-defects.html#17">17</a>. Bad bool parsing</h3>
<p><b>Section:</b> 28.3.4.3.2.3 <a href="https://wg21.link/facet.num.get.virtuals">[facet.num.get.virtuals]</a> <b>Status:</b> <a href="lwg-active.html#TC1">TC1</a>
 <b>Submitter:</b> Nathan Myers <b>Opened:</b> 1998-08-06 <b>Last modified:</b> 2016-08-09</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#facet.num.get.virtuals">active issues</a> in [facet.num.get.virtuals].</p>
<p><b>View all other</b> <a href="lwg-index.html#facet.num.get.virtuals">issues</a> in [facet.num.get.virtuals].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#TC1">TC1</a> status.</p>
<p><b>Discussion:</b></p>
<p>This section describes the process of parsing a text boolean value from the input
stream. It does not say it recognizes either of the sequences &quot;true&quot; or
&quot;false&quot; and returns the corresponding bool value; instead, it says it recognizes
only one of those sequences, and chooses which according to the received value of a
reference argument intended for returning the result, and reports an error if the other
sequence is found. (!) Furthermore, it claims to get the names from the ctype&lt;&gt;
facet rather than the numpunct&lt;&gt; facet, and it examines the &quot;boolalpha&quot;
flag wrongly; it doesn't define the value &quot;loc&quot;; and finally, it computes
wrongly whether to use numeric or &quot;alpha&quot; parsing.<br/>
<br/>
I believe the correct algorithm is &quot;as if&quot;: </p>

<pre>  // in, err, val, and str are arguments.
  err = 0;
  const numpunct&lt;charT&gt;&amp; np = use_facet&lt;numpunct&lt;charT&gt; &gt;(str.getloc());
  const string_type t = np.truename(), f = np.falsename();
  bool tm = true, fm = true;
  size_t pos = 0;
  while (tm &amp;&amp; pos &lt; t.size() || fm &amp;&amp; pos &lt; f.size()) {
    if (in == end) { err = str.eofbit; }
    bool matched = false;
    if (tm &amp;&amp; pos &lt; t.size()) {
      if (!err &amp;&amp; t[pos] == *in) matched = true;
      else tm = false;
    }
    if (fm &amp;&amp; pos &lt; f.size()) {
      if (!err &amp;&amp; f[pos] == *in) matched = true;
      else fm = false;
    }
    if (matched) { ++in; ++pos; }
    if (pos &gt; t.size()) tm = false;
    if (pos &gt; f.size()) fm = false;
  }
  if (tm == fm || pos == 0) { err |= str.failbit; }
  else                      { val = tm; }
  return in;</pre>

<p>Notice this works reasonably when the candidate strings are both empty, or equal, or
when one is a substring of the other. The proposed text below captures the logic of the
code above.</p>


<p id="res-17"><b>Proposed resolution:</b></p>
<p>In 28.3.4.3.2.3 <a href="https://wg21.link/facet.num.get.virtuals">[facet.num.get.virtuals]</a>, in the first line of paragraph 14,
change &quot;&amp;&amp;&quot; to &quot;&amp;&quot;.</p>

<p>Then, replace paragraphs 15 and 16 as follows:</p>

<blockquote>

  <p>Otherwise target sequences are determined &quot;as if&quot; by
  calling the members <code>falsename()</code> and
  <code>truename()</code> of the facet obtained by
  <code>use_facet&lt;numpunct&lt;charT&gt;&nbsp;&gt;(str.getloc())</code>.  
  Successive characters in the range <code>[in,end)</code> (see
  [lib.sequence.reqmts]) are obtained and matched against
  corresponding positions in the target sequences only as necessary to
  identify a unique match. The input iterator <code>in</code> is
  compared to <code>end</code> only when necessary to obtain a
  character. If and only if a target sequence is uniquely matched,
  <code>val</code> is set to the corresponding value.</p>

</blockquote>

<blockquote>
  <p>The <code>in</code> iterator is always left pointing one position beyond the last character
  successfully matched. If <code>val</code> is set, then err is set to <code>str.goodbit</code>; or to
  <code>str.eofbit</code> if, when seeking another character to match, it is found that
  <code>(in==end)</code>. If <code>val</code> is not set, then <i>err</i> is set to <code>str.failbit</code>; or to
  <code>(str.failbit|str.eofbit)</code>if
  the reason for the failure was that <code>(in==end)</code>. [Example: for targets
  <code>true</code>:&quot;a&quot; and <code>false</code>:&quot;abb&quot;, the input sequence &quot;a&quot; yields
  <code>val==true</code> and <code>err==str.eofbit</code>; the input sequence &quot;abc&quot; yields
  <code>err=str.failbit</code>, with <code>in</code> ending at the 'c' element. For targets
  <code>true</code>:&quot;1&quot;
  and <code>false</code>:&quot;0&quot;, the input sequence &quot;1&quot; yields <code>val==true</code>
  and <code>err=str.goodbit</code>. For empty targets (&quot;&quot;), any input sequence yields
  <code>err==str.failbit</code>. --end example]</p>
</blockquote>





</body>
</html>
