libstdc++
quoted_string.h
Go to the documentation of this file.
1// Helpers for quoted stream manipulators -*- C++ -*-
2
3// Copyright (C) 2013-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/quoted_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{iomanip}
28 */
29
30#ifndef _GLIBCXX_QUOTED_STRING_H
31#define _GLIBCXX_QUOTED_STRING_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus < 201103L
38# include <bits/c++0x_warning.h>
39#else
40#include <sstream>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 namespace __detail {
47 /**
48 * @brief Struct for delimited strings.
49 */
50 template<typename _String, typename _CharT>
51 struct _Quoted_string
52 {
55 "String type must be pointer or reference");
56
57 _Quoted_string(_String __str, _CharT __del, _CharT __esc)
58 : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
59 { }
60
61 _Quoted_string&
62 operator=(_Quoted_string&) = delete;
63
64 _String _M_string;
65 _CharT _M_delim;
66 _CharT _M_escape;
67 };
68
69#if __cplusplus >= 201703L
70 template<typename _CharT, typename _Traits>
71 struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT>
72 {
74 _CharT __del, _CharT __esc)
75 : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
76 { }
77
78 _Quoted_string&
79 operator=(_Quoted_string&) = delete;
80
82 _CharT _M_delim;
83 _CharT _M_escape;
84 };
85#endif // C++17
86
87 /**
88 * @brief Inserter for quoted strings.
89 *
90 * @headerfile iomanip
91 */
92 template<typename _CharT, typename _Traits>
95 const _Quoted_string<const _CharT*, _CharT>& __str)
96 {
97 // _GLIBCXX_RESOLVE_LIB_DEFECTS
98 // DR 2344 quoted()'s interaction with padding is unclear
100 __ostr << __str._M_delim;
101 for (const _CharT* __c = __str._M_string; *__c; ++__c)
102 {
103 if (*__c == __str._M_delim || *__c == __str._M_escape)
104 __ostr << __str._M_escape;
105 __ostr << *__c;
106 }
107 __ostr << __str._M_delim;
108
109 return __os << __ostr.str();
110 }
111
112 /**
113 * @brief Inserter for quoted strings.
114 *
115 * @headerfile iomanip
116 */
117 template<typename _CharT, typename _Traits, typename _String>
118 std::basic_ostream<_CharT, _Traits>&
119 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
121 {
122 // _GLIBCXX_RESOLVE_LIB_DEFECTS
123 // DR 2344 quoted()'s interaction with padding is unclear
124 std::basic_ostringstream<_CharT, _Traits> __ostr;
125 __ostr << __str._M_delim;
126 for (auto __c : __str._M_string)
127 {
128 if (__c == __str._M_delim || __c == __str._M_escape)
129 __ostr << __str._M_escape;
130 __ostr << __c;
131 }
132 __ostr << __str._M_delim;
133
134 return __os << __ostr.str();
135 }
136
137 /**
138 * @brief Extractor for delimited strings.
139 * The left and right delimiters can be different.
140 *
141 * @headerfile iomanip
142 */
143 template<typename _CharT, typename _Traits, typename _Alloc>
144 std::basic_istream<_CharT, _Traits>&
147 _CharT>& __str)
148 {
149 _CharT __c;
150 __is >> __c;
151 if (!__is.good())
152 return __is;
153 if (__c != __str._M_delim)
154 {
155 __is.unget();
156 __is >> __str._M_string;
157 return __is;
158 }
159 __str._M_string.clear();
161 = __is.flags(__is.flags() & ~std::ios_base::skipws);
162 do
163 {
164 __is >> __c;
165 if (!__is.good())
166 break;
167 if (__c == __str._M_escape)
168 {
169 __is >> __c;
170 if (!__is.good())
171 break;
172 }
173 else if (__c == __str._M_delim)
174 break;
175 __str._M_string += __c;
176 }
177 while (true);
178 __is.setf(__flags);
179
180 return __is;
181 }
182 } // namespace __detail
183
184_GLIBCXX_END_NAMESPACE_VERSION
185} // namespace std
186
187#endif // C++11
188#endif /* _GLIBCXX_QUOTED_STRING_H */
class __attribute((__abi_tag__("cxx11"))) failure typedef _Ios_Fmtflags fmtflags
These are thrown to indicate problems with io.
Definition ios_base.h:292
ISO C++ entities toplevel namespace is std.
Implementation details not part of the namespace std interface.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, const _Quoted_string< basic_string< _CharT, _Traits, _Alloc > &, _CharT > &__str)
Extractor for delimited strings. The left and right delimiters can be different.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const _Quoted_string< const _CharT *, _CharT > &__str)
Inserter for quoted strings.
void clear(iostate __state=goodbit)
[Re]sets the error state.
Definition basic_ios.tcc:46
bool good() const
Fast error checking.
Definition basic_ios.h:190
Template class basic_istream.
Definition istream:63
__istream_type & unget()
Unextracting the previous character.
Definition istream.tcc:806
Template class basic_ostream.
Definition ostream.h:67
Controlling output for std::string.
Definition sstream:872
__string_type str() const &
Copying out the string buffer.
Definition sstream:1059
A non-owning reference to a string.
Definition string_view:109
is_pointer
Definition type_traits:572
is_reference
Definition type_traits:732
Managing sequences of characters and character-like objects.
fmtflags setf(fmtflags __fmtfl)
Setting new format flags.
Definition ios_base.h:721
static const fmtflags skipws
Skips leading white space before certain input operations.
Definition ios_base.h:423
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
Struct for delimited strings.