Document number: | N3570 |
Date: | 2013-03-14 |
Project: | Programming Language C++ |
Reply-to: | Beman Dawes <bdawes at acm dot org> |
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.
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 Boost component that has been shipping for several years. The declarations for the proposed functions can go in a new header or an existing header.
The proposed wording below assumes the target is C++1y and places the function declarations in <iomanip>.
C++ standard library stream I/O for strings that contain embedded spaces can produce unexpected results. For example,
std::stringstream ss; std::string original = "foolish me"; std::string round_trip; ss << original; ss >> round_trip; std::cout << original; // outputs: foolish me std::cout << round_trip; // outputs: foolish assert(original == round_trip); // assert will fire
The proposed quoted
stream I/O manipulator places delimiters, defaulted
to
double-quote ("
), around strings on output, and strips off
the delimiters on input. This ensures strings with embedded white space round-trip as
desired. For example,
std::stringstream ss; std::string original = "foolish me"; std::string round_trip; ss << quoted(original); ss >> quoted(round_trip); std::cout << original; // outputs: foolish me std::cout << round_trip; // outputs: foolish me assert(original == round_trip); // assert will not fire
If the string contains the delimiter character, on output that character will
be preceded by an escape character, default to backslash (\
), as will the escape character itself:
std::cout << quoted("She said \"Hi!\""); // outputs: "She said \"Hi!\""
N3570 - Revision 1 (pre-Bristol mailing)
N3431 Initial paper (pre-Portland mailing)
Change 27.7.1 Overview [iostream.format.overview], "Header <iomanip> synopsis" as indicated:
namespace std { // types T1, T2, ... are unspecified implementation types T1 resetiosflags(ios_base::fmtflags mask); T2 setiosflags (ios_base::fmtflags mask); T3 setbase(int base); template<charT> T4 setfill(charT c); T5 setprecision(int n); T6 setw(int n); template <class moneyT> T7 get_money(moneyT& mon, bool intl = false); template <class moneyT> T8 put_money(const moneyT& mon, bool intl = false); template <class charT> T9 get_time(struct tm* tmb, const charT* fmt); template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt); template <class charT, class traits, class Allocator> T11 quoted(const std::basic_string<charT, traits, Allocator>& s, charT delim='"', charT escape='\\'); template <class charT> T12 quoted(const charT* s, charT delim='"', charT escape='\\'); template <class charT, class traits, class Allocator> T13 quoted(std::basic_string<charT, traits, Allocator>& s, charT delim='"', charT escape='\\'); }
After 27.7.5 Extended manipulators [ext.manip], add a new sub-section:
27.7.6 Quoted manipulators [quoted.manip]
[Note: 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. --end note]
template <class charT, class traits, class Allocator> unspecified quoted(const std::basic_string<charT, traits, Allocator>& s, charT delim='"', charT escape='\\'); template <class charT, class traits, class Allocator> unspecified quoted(const charT* s, charT delim='"', charT escape='\\');Returns: An object of unspecified type such that if
out
is an instance ofbasic_ostream<charT,
,
traits>s
is an instance of a type convertible tostd::basic_string<charT, traits, Allocator>
, orconst char*
, respectively, anddelim
andescape
are instances ofcharT
, then the expressionout << quoted(s, delim, escape)
behaves as if it inserts the following characters intoout
using character inserter function templates ([ostream.inserters.character]), which may throwios_base::failure
([ios::failure]):
delim
.- Each character in
s
. If the character to be output is equal toescape
ordelim
, as determined byoperator==
, first outputescape
.delim
.The expression
out << quoted(s, delim, escape)
shall have typebasic_ostream<charT, traits>&
and valueout
.template <class charT, class traits, class Allocator> unspecified quoted(const std::basic_string<charT, traits, Allocator>& s, charT delim='"', charT escape='\\');Returns: An object of unspecified type such that if
in
is an instance ofbasic_istream<charT,
,
traits>s
is an instance ofbasic_string<charT, traits, Allocator>
, anddelim
andescape
are instances of typescharT
, then the expressionin >> quoted(s, delim, escape)
behaves as if it extracts the following characters fromin
using basic_istream::operator>> ([istream::extractors]) which may throwios_base::failure
([ios::failure]):
- If the first character extracted is equal to
delim
, as determined byoperator==
, then:
- Turn off the
skipws
flag.string.clear()
Until an unescaped
delim
character is reached oris.not_good()
, extract characters fromin
and append them tos
, except that if anescape
is reached, ignore it and append the next character tos
.- Discard the final
delim
character.- Restore the
skipws
flag to its original value.- Otherwise,
os >> s
.The expression
in >> quoted(s, delim, escape)
shall have typebasic_istream<charT, traits>&
and valuein
.
The quoted()
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.