<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3892: Incorrect formatting of nested ranges and tuples</title>
<meta property="og:title" content="Issue 3892: Incorrect formatting of nested ranges and tuples">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3892.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#WP">WP</a> status.</em></p>
<h3 id="3892"><a href="lwg-defects.html#3892">3892</a>. Incorrect formatting of nested ranges and tuples</h3>
<p><b>Section:</b> 28.5.7.2 <a href="https://wg21.link/format.range.formatter">[format.range.formatter]</a>, 28.5.9 <a href="https://wg21.link/format.tuple">[format.tuple]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Victor Zverovich <b>Opened:</b> 2023-02-20 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#format.range.formatter">issues</a> in [format.range.formatter].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>formatter</code> specializations for ranges and tuples set debug format for underlying element formatters in their 
<code>parse</code> functions e.g. 28.5.7.2 <a href="https://wg21.link/format.range.formatter">[format.range.formatter]</a> p9:
</p>
<blockquote><pre>
template&lt;class ParseContext&gt;
  constexpr typename ParseContext::iterator
    parse(ParseContext&amp; ctx);
</pre>
<blockquote>
<p>
<i>Effects</i>: Parses the format specifier as a <i>range-format-spec</i> and stores the parsed specifiers in <code>*this</code>.
 The values of <code><i>opening-bracket_</i></code>, <code><i>closing-bracket_</i></code>, and <code><i>separator_</i></code> are modified 
 if and only if required by the <i>range-type</i> or the <code>n</code> option, if present. If:
</p>
<ol style="list-style-type: none">
<li><p>&mdash; the <i>range-type</i> is neither <code>s</code> nor <code>?s</code>,</p></li>
<li><p>&mdash; <code><i>underlying_</i>.set_debug_format()</code> is a valid expression, and</p></li>
<li><p>&mdash; there is no <i>range-underlying-spec</i>,</p></li>
</ol>
<p>
then calls <code><i>underlying_</i>.set_debug_format()</code>.
</p>
</blockquote>
</blockquote>
<p>
However, they don't say anything about calling <code>parse</code> functions of those formatters. As as result, 
formatting of nested ranges can be incorrect, e.g.
</p>
<blockquote><pre>
std::string s = std::format("{}", std::vector&lt;std::vector&lt;std::string&gt;&gt;{{"a, b", "c"}});
</pre></blockquote>
<p>
With the current specification <code>s</code> is <code>[[a, b, c]]</code> instead of <code>[["a, b", "c"]]</code>, i.e. strings 
in the output are not correctly escaped. The same is true for nested tuples and combinations of tuples and ranges.
<p/>
The fix approved by LEWG as part of <a href="https://wg21.link/P2733" title=" Fix handling of empty specifiers in std::format">P2733</a> (which was trying to address a different issue) is to 
always call <code>parse</code> <a href="https://github.com/cplusplus/papers/issues/1426#issuecomment-1420017182">for underlying formatter</a>. 
Additionally the standard should clarify that <i>format-spec</i> cannot start with <code>'}'</code> because that's the 
implicit assumption in range formatting and what happens when <i>format-spec</i> is not present.
</p>

<p><i>[2023-03-22; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4928" title=" Working Draft, Standard for Programming Language C++">N4928</a>.
</p>

<ol>

<li><p>Modify 28.5.2.1 <a href="https://wg21.link/format.string.general">[format.string.general]</a> p1 as indicated:</p>

<blockquote>
<dl>
<dt>[&hellip;]</dt>
<dt><i>format-specifier</i>:</dt>
<dd>: <i>format-spec</i></dd>
<dt><i>format-spec</i>:</dt>
<dd>as specified by the <code>formatter</code> specialization for the argument type<ins>; cannot start with <code>}</code></ins></dd>
</dl>
</blockquote>

</li>

<li><p>Modify 28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a> as indicated:</p>

<blockquote>
<p>
-3- Given character type <code>charT</code>, output iterator type <code>Out</code>, and formatting argument type <code>T</code>, in 
Table 74 [tab:formatter.basic] and Table 75 [tab:formatter]:
<p/>
[&hellip;]
<p/>
<code>pc.begin()</code> points to the beginning of the <i>format-spec</i> (28.5.2 <a href="https://wg21.link/format.string">[format.string]</a>) of the 
replacement field being formatted in the format string. If <i>format-spec</i> is <ins>not present or</ins> empty then 
either <code>pc.begin() == pc.end()</code> or <code>*pc.begin() == '}'</code>.
</p>
</blockquote>

</li>

<li><p>Modify 28.5.7.2 <a href="https://wg21.link/format.range.formatter">[format.range.formatter]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class ParseContext&gt;
  constexpr typename ParseContext::iterator
    parse(ParseContext&amp; ctx);
</pre>
<blockquote>
<p>
-9- <i>Effects</i>: Parses the format specifier<ins>s</ins> as a <i>range-format-spec</i> and stores the parsed specifiers in <code>*this</code>.
<ins>Calls <code><i>underlying_</i>.parse(ctx)</code> to parse <i>format-spec</i> in <i>range-format-spec</i> or, if the latter is not present, 
empty <i>format-spec</i>.</ins> The values of <code><i>opening-bracket_</i></code>, <code><i>closing-bracket_</i></code>, and <code><i>separator_</i></code> 
are modified if and only if required by the <i>range-type</i> or the <code>n</code> option, if present. If:
</p>
<ol style="list-style-type: none">
<li><p>(9.1) &mdash; the <i>range-type</i> is neither <code>s</code> nor <code>?s</code>,</p></li>
<li><p>(9.2) &mdash; <code><i>underlying_</i>.set_debug_format()</code> is a valid expression, and</p></li>
<li><p>(9.3) &mdash; there is no <i>range-underlying-spec</i>,</p></li>
</ol>
<p>
then calls <code><i>underlying_</i>.set_debug_format()</code>.
</p>
</blockquote>
</blockquote>

</li>

<li><p>Modify 28.5.9 <a href="https://wg21.link/format.tuple">[format.tuple]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class ParseContext&gt;
  constexpr typename ParseContext::iterator
    parse(ParseContext&amp; ctx);
</pre>
<blockquote>
<p>
-7- <i>Effects</i>: Parses the format specifier<ins>s</ins> as a <i>tuple-format-spec</i> and stores the parsed specifiers 
in <code>*this</code>. The values of <code><i>opening-bracket_</i></code>, <code><i>closing-bracket_</i></code>, and <code><i>separator_</i></code> 
are modified if and only if required by the <i>tuple-type</i>, if present. For each element <code><i>e</i></code> in 
<code><i>underlying_</i></code>, <ins>calls <code><i>e</i>.parse(ctx)</code> to parse empty <i>format-spec</i> and,</ins> 
if <code><i>e</i>.set_debug_format()</code> is a valid expression, calls <code><i>e</i>.set_debug_format()</code>.
</p>
</blockquote>
</blockquote>


</li>


</ol>
</blockquote>

<p><i>[Varna 2023-06-16; Jonathan provides tweaked wording]</i></p>

<p>
Add "an" in two places.
</p>

<p><i>[Varna 2023-06-16; Move to Ready]</i></p>

<p>
This would allow resolving LWG <a href="lwg-closed.html#3776" title="Avoid parsing format-spec if it is not present or empty (Status: NAD)">3776</a><sup><a href="https://cplusplus.github.io/LWG/issue3776" title="Latest snapshot">(i)</a></sup> as NAD.
</p>

<p><i>[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3892"><b>Proposed resolution:</b></p>

<p>
This wording is relative to <a href="https://wg21.link/N4928" title=" Working Draft, Standard for Programming Language C++">N4928</a>.
</p>

<ol>

<li><p>Modify 28.5.2.1 <a href="https://wg21.link/format.string.general">[format.string.general]</a> p1 as indicated:</p>

<blockquote>
<dl>
<dt>[&hellip;]</dt>
<dt><i>format-specifier</i>:</dt>
<dd>: <i>format-spec</i></dd>
<dt><i>format-spec</i>:</dt>
<dd>as specified by the <code>formatter</code> specialization for the argument type<ins>; cannot start with <code>}</code></ins></dd>
</dl>
</blockquote>

</li>

<li><p>Modify 28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a> as indicated:</p>

<blockquote>
<p>
-3- Given character type <code>charT</code>, output iterator type <code>Out</code>, and formatting argument type <code>T</code>, in 
Table 74 [tab:formatter.basic] and Table 75 [tab:formatter]:
<p/>
[&hellip;]
<p/>
<code>pc.begin()</code> points to the beginning of the <i>format-spec</i> (28.5.2 <a href="https://wg21.link/format.string">[format.string]</a>) of the 
replacement field being formatted in the format string. If <i>format-spec</i> is <ins>not present or</ins> empty then 
either <code>pc.begin() == pc.end()</code> or <code>*pc.begin() == '}'</code>.
</p>
</blockquote>

</li>

<li><p>Modify 28.5.7.2 <a href="https://wg21.link/format.range.formatter">[format.range.formatter]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class ParseContext&gt;
  constexpr typename ParseContext::iterator
    parse(ParseContext&amp; ctx);
</pre>
<blockquote>
<p>
-9- <i>Effects</i>: Parses the format specifier<ins>s</ins> as a <i>range-format-spec</i> and stores the parsed specifiers in <code>*this</code>.
<ins>Calls <code><i>underlying_</i>.parse(ctx)</code> to parse <i>format-spec</i> in <i>range-format-spec</i> or, if the latter is not present, 
an empty <i>format-spec</i>.</ins> The values of <code><i>opening-bracket_</i></code>, <code><i>closing-bracket_</i></code>, and <code><i>separator_</i></code> 
are modified if and only if required by the <i>range-type</i> or the <code>n</code> option, if present. If:
</p>
<ol style="list-style-type: none">
<li><p>(9.1) &mdash; the <i>range-type</i> is neither <code>s</code> nor <code>?s</code>,</p></li>
<li><p>(9.2) &mdash; <code><i>underlying_</i>.set_debug_format()</code> is a valid expression, and</p></li>
<li><p>(9.3) &mdash; there is no <i>range-underlying-spec</i>,</p></li>
</ol>
<p>
then calls <code><i>underlying_</i>.set_debug_format()</code>.
</p>
</blockquote>
</blockquote>

</li>

<li><p>Modify 28.5.9 <a href="https://wg21.link/format.tuple">[format.tuple]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class ParseContext&gt;
  constexpr typename ParseContext::iterator
    parse(ParseContext&amp; ctx);
</pre>
<blockquote>
<p>
-7- <i>Effects</i>: Parses the format specifier<ins>s</ins> as a <i>tuple-format-spec</i> and stores the parsed specifiers 
in <code>*this</code>. The values of <code><i>opening-bracket_</i></code>, <code><i>closing-bracket_</i></code>, and <code><i>separator_</i></code> 
are modified if and only if required by the <i>tuple-type</i>, if present. For each element <code><i>e</i></code> in 
<code><i>underlying_</i></code>, <ins>calls <code><i>e</i>.parse(ctx)</code> to parse an empty <i>format-spec</i> and,</ins> 
if <code><i>e</i>.set_debug_format()</code> is a valid expression, calls <code><i>e</i>.set_debug_format()</code>.
</p>
</blockquote>
</blockquote>


</li>


</ol>





</body>
</html>
