<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  <title>&quot;quoted&quot; proposal</title>
  <meta name="generator" content="Microsoft FrontPage 5.0" />
<style type="text/css">
body  {
        font-family: sans-serif;
        margin: 1em;
        max-width : 7.5in;
      }

table { margin: 0.5em; }

pre   { background-color:#D7EEFF }

ins   { background-color:#CCFFCC }
del   { background-color:#FFCACA }

  </style>
</head>

<body>

  <table border="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
    <tr>
      <td height="23">Document number:&nbsp;&nbsp; </td>
      <td height="23">N3570</td>
    </tr>
    <tr>
      <td height="23">Date:</td>
      <td height="23">
      <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%Y-%m-%d" startspan -->2013-03-14<!--webbot bot="Timestamp" endspan i-checksum="12183" --></td>
    </tr>
    <tr>
      <td height="23">Project:</td>
      <td height="23">Programming Language C++</td>
    </tr>
    <tr>
      <td valign="top" height="22">Reply-to:</td>
      <td height="22">Beman Dawes &lt;bdawes at acm dot org&gt;</td>
    </tr>
  </table>


<h1>Quoted Strings Library Proposal (Revision 1)</h1>


<h2><a name="Introduction">Introduction</a></h2>


<p>Character strings enclosed in quotation marks are an element of numerous common data formats (e.g. 
XML, CSV), yet C++ standard library stream I/O offers no direct support. 
Furthermore, standard library stream I/O has 
a problem with embedded spaces in strings that can trip the unwary. The proposed solution provides 
direct support for quoted strings, avoids embedded spaces problems and is more 
efficient than likely user provided solutions.</p>


<p>The proposal is suitable for either C++1y or a standard library Technical Specification 
(TS). It 
is a pure addition that will 
break no existing standard-conforming user code. It is based on 
a <a href="http://www.boost.org/libs/io/doc/quoted_manip.html">Boost component</a> that has been shipping for several years. The declarations 
for the proposed functions can go in a new header or 
an existing header.</p>


<p>The proposed wording below assumes the target is C++1y and places the 
function declarations in &lt;iomanip&gt;.</p>


<h2><a name="Problem">Problem</a> description</h2>
<p>C++ standard library stream I/O for strings that contain embedded spaces 
can produce unexpected results. For example,</p>
<blockquote>
  <pre>std::stringstream ss;
std::string original = &quot;foolish me&quot;;
std::string round_trip;

ss &lt;&lt; original;
ss &gt;&gt; round_trip;

std::cout &lt;&lt; original;   // outputs: foolish me
std::cout &lt;&lt; round_trip; // outputs: foolish

assert(original == round_trip); // assert will fire</pre>
</blockquote>
<p>The proposed <code>quoted</code> stream I/O manipulator places delimiters, defaulted 
to 
double-quote (<code>&quot;</code>), around strings on output, and strips off 
the delimiters on input. This ensures strings with embedded white space round-trip as 
desired. For example,</p>
<blockquote>
  <pre>std::stringstream ss;
std::string original = &quot;foolish me&quot;;
std::string round_trip;

ss &lt;&lt; quoted(original);
ss &gt;&gt; quoted(round_trip);

std::cout &lt;&lt; original;     // outputs: foolish me
std::cout &lt;&lt; round_trip;   // outputs: foolish me

assert(original == round_trip); // assert will not fire</pre>
</blockquote>
<p>If the string contains the delimiter character, on output that character will 
be preceded by an escape character, default to backslash (<code>\</code>), as will the escape character itself:</p>
<blockquote>
  <pre>std::cout &lt;&lt; quoted(&quot;She said \&quot;Hi!\&quot;&quot;);  // outputs: &quot;She said \&quot;Hi!\&quot;&quot;</pre>
</blockquote>


<h2><a name="Revision-history">Revision history</a></h2>


<p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3570.html">
N3570</a> - Revision 1 
(pre-Bristol mailing)</p>
  <ul>
    <li>Remove unnecessary backslash from character literals in default arguments.</li>
    <li>Rewrite the proposed wording to follow the standard library's form for 
    iomanip functions. Note that this has the effect of making error handling 
    well-specified, as requested by the LWG at the Portland meeting.</li>
    <li>Editorial cleanup.</li>
  </ul>


  <p>
  <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3431.html">
  N3431</a> Initial paper (pre-Portland mailing)</p>
<h2><a name="Proposed-wording">Proposed wording</a></h2>
<p><i>Change 27.7.1 Overview [iostream.format.overview], &quot;Header &lt;iomanip&gt; 
synopsis&quot; as indicated:</i></p>
  <blockquote>
    <pre>namespace std {
// <i>types</i> T1, T2, ... <i>are unspecified implementation types</i>
<i>T1</i> resetiosflags(ios_base::fmtflags mask);
<i>T2</i> setiosflags (ios_base::fmtflags mask);
<i>T3</i> setbase(int base);
template&lt;charT&gt; <i>T4</i> setfill(charT c);
<i>T5</i> setprecision(int n);
<i>T6</i> setw(int n);
template &lt;class moneyT&gt; <i>T7</i> get_money(moneyT&amp; mon, bool intl = false);
template &lt;class moneyT&gt; <i>T8</i> put_money(const moneyT&amp; mon, bool intl = false);
template &lt;class charT&gt; <i>T9</i> get_time(struct tm* tmb, const charT* fmt);
template &lt;class charT&gt; <i>T10</i> put_time(const struct tm* tmb, const charT* fmt);

<ins>template &lt;class charT, class traits, class Allocator&gt;</ins>
  <ins><i>T11</i> quoted(const std::basic_string&lt;charT, traits, Allocator&gt;&amp; s,</ins>
             <ins>charT delim='&quot;', charT escape='\\');</ins>

<ins>template &lt;class charT&gt;</ins>
  <ins><i>T12</i> quoted(const charT* s, charT delim='&quot;', charT escape='\\');</ins>

<ins>template &lt;class charT, class traits, class Allocator&gt;</ins>
  <ins><i>T13</i> quoted(std::basic_string&lt;charT, traits, Allocator&gt;&amp; s,</ins>
             <ins>charT delim='&quot;', charT escape='\\');</ins>
}</pre>
  </blockquote>
<p><i>After 27.7.5 Extended manipulators [ext.manip], add a new sub-section:</i></p>
  <blockquote>
<p>27.7.6 Quoted manipulators [quoted.manip]</p>
<p>[<i>Note</i>: Quoted manipulators provide string insertion and extraction of 
quoted strings (for example, XML and CSV formats). Quoted manipulators are useful in ensuring 
that the content of a string with embedded spaces remains unchanged if inserted and then extracted 
via stream I/O.
<i>--end note</i>]</p>
    <pre>template &lt;class charT, class traits, class Allocator&gt;
<i>unspecified</i> quoted(const std::basic_string&lt;charT, traits, Allocator&gt;&amp; s,
                   charT delim='&quot;', charT escape='\\');
template &lt;class charT, class traits, class Allocator&gt;
<i>unspecified</i> quoted(const charT* s, charT delim='&quot;', charT escape='\\');</pre>
    <blockquote>
<p><i>Returns:</i> An object of unspecified type such that if <code>out</code> 
is an instance of <code>basic_ostream&lt;charT,<br>
traits&gt;</code>, <code>s</code> is an instance of a type convertible to <code>std::basic_string&lt;charT, traits, Allocator&gt;</code>, 
or <code>const char*</code>, respectively, and
<code>delim</code> and <code>escape</code> are instances of
<code>charT</code>, then the expression <code>out &lt;&lt; quoted(s, delim, escape)</code> 
behaves as if it inserts the following characters into <code>out</code> using character inserter function templates ([ostream.inserters.character]), 
which may throw <code>ios_base::failure</code> ([ios::failure]):</p>
  <ul>
    <li><code>delim</code>.</li>
    <li>Each character in <code>s</code>. If the character to be output is 
    equal to <code>escape</code> or <code>delim</code>, as determined by <code>
    operator==</code>, first output <code>escape</code>. </li>
    <li><code>delim</code>.</li>
  </ul>
<p>The expression <code>out &lt;&lt; quoted(s, delim, escape)</code> shall have type
<code>basic_ostream&lt;charT, traits&gt;&amp;</code> and value <code>out</code>.</p>
    </blockquote>
    <pre>template &lt;class charT, class traits, class Allocator&gt;
<i>unspecified</i> quoted(const std::basic_string&lt;charT, traits, Allocator&gt;&amp; s,
                   charT delim='&quot;', charT escape='\\');
</pre>
    <blockquote>
<p><i>Returns:</i> An object of unspecified type such that if <code>in</code> 
is an instance of <code>basic_istream&lt;charT,<br>
traits&gt;</code>, <code>s</code> is an instance of <code>basic_string&lt;charT, traits, Allocator&gt;</code>, 
and
<code>delim</code> and <code>escape</code> are instances of types
<code>charT</code>, then the expression <code>in &gt;&gt; quoted(s, delim, escape)</code> 
behaves as if it extracts the following characters from <code>in</code> 
using basic_istream::operator&gt;&gt; ([istream::extractors]) 
which may throw <code>ios_base::failure</code> ([ios::failure]):</p>
  <ul>
    <li>If the first character extracted is equal to <code>delim</code>, as determined by
    <code>operator==</code>, then:<ul>
      <li>Turn off the <code>skipws</code> flag.</li>
      <li><code>string.clear()</code></li>
      <li dir="ltr">
      <p dir="ltr">Until an unescaped <code>delim</code> character is reached or <code>
      is.not_good()</code>, extract 
      characters from <code>in</code> and append them to <code>s</code>, 
      except that if an <code>escape</code> is reached, ignore it and append the 
      next character to <code>s</code>.</li>
      <li>Discard the final <code>delim</code> character.</li>
      <li>Restore the <code>skipws</code> flag to its original value.</li>
    </ul>
    </li>
    <li>Otherwise, <code>os &gt;&gt; s</code>.</li>
  </ul>
<p>The expression <code>in &gt;&gt; quoted(s, delim, escape)</code> shall have type
<code>basic_istream&lt;charT, traits&gt;&amp;</code> and value <code>in</code>.</p>
    </blockquote>
  </blockquote>
  <h2><a name="Acknowledgements">Acknowledgements</a></h2>
<p>The <code>quoted()</code> stream manipulators emerged from discussions on the 
Boost developers mailing list. Participants included Beman Dawes, Rob Stewart, 
Alexander Lamaison, Eric Niebler, Vicente Botet, Andrey Semashev, Phil Richards, 
and Rob Murray. Eric Niebler's suggestions provided the basis for the name and 
form of the templates. </p>
<hr>

</body>
</html>