libstdc++: quoted_string.h Source File

libstdc++

quoted_string.h
Go to the documentation of this file.
1// Helpers for quoted stream manipulators -*- C++ -*-
2
3// Copyright (C) 2013-2026 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 {
53 static_assert(is_reference<_String>::value
54 || is_pointer<_String>::value,
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 // Friends for ADL with module std.
65 template<typename _CharT2, typename _Traits>
66 friend std::basic_ostream<_CharT2, _Traits>&
67 operator<<(std::basic_ostream<_CharT2, _Traits>& __os,
68 const _Quoted_string<const _CharT2*, _CharT2>& __str);
69
70 template<typename _CharT2, typename _Traits, typename _String2>
71 friend std::basic_ostream<_CharT2, _Traits>&
72 operator<<(std::basic_ostream<_CharT2, _Traits>& __os,
73 const _Quoted_string<_String2, _CharT2>& __str);
74
75 template<typename _CharT2, typename _Traits, typename _Alloc>
76 friend std::basic_istream<_CharT2, _Traits>&
77 operator>>(std::basic_istream<_CharT2, _Traits>& __is,
78 const _Quoted_string<basic_string<_CharT2, _Traits, _Alloc>&,
79 _CharT2>& __str);
80
81 _String _M_string;
82 _CharT _M_delim;
83 _CharT _M_escape;
84 };
85
86#if __cplusplus >= 201703L
87 template<typename _CharT, typename _Traits>
88 struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT>
89 {
90 _Quoted_string(basic_string_view<_CharT, _Traits> __str,
91 _CharT __del, _CharT __esc)
92 : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
93 { }
94
95 _Quoted_string&
96 operator=(_Quoted_string&) = delete;
97
98 // Friend for ADL with module std.
99 template<typename _CharT2, typename _Traits2, typename _String2>
100 friend std::basic_ostream<_CharT2, _Traits2>&
101 operator<<(std::basic_ostream<_CharT2, _Traits2>& __os,
102 const _Quoted_string<_String2, _CharT2>& __str);
103
104 basic_string_view<_CharT, _Traits> _M_string;
105 _CharT _M_delim;
106 _CharT _M_escape;
107 };
108#endif // C++17
109
110 /**
111 * @brief Inserter for quoted strings.
112 *
113 * @headerfile iomanip
114 */
115 template<typename _CharT, typename _Traits>
116 std::basic_ostream<_CharT, _Traits>&
117 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
118 const _Quoted_string<const _CharT*, _CharT>& __str)
119 {
120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
121 // DR 2344 quoted()'s interaction with padding is unclear
122 std::basic_ostringstream<_CharT, _Traits> __ostr;
123 __ostr << __str._M_delim;
124 for (const _CharT* __c = __str._M_string; *__c; ++__c)
125 {
126 if (*__c == __str._M_delim || *__c == __str._M_escape)
127 __ostr << __str._M_escape;
128 __ostr << *__c;
129 }
130 __ostr << __str._M_delim;
131
132 return __os << __ostr.str();
133 }
134
135 /**
136 * @brief Inserter for quoted strings.
137 *
138 * @headerfile iomanip
139 */
140 template<typename _CharT, typename _Traits, typename _String>
141 std::basic_ostream<_CharT, _Traits>&
142 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
143 const _Quoted_string<_String, _CharT>& __str)
144 {
145 // _GLIBCXX_RESOLVE_LIB_DEFECTS
146 // DR 2344 quoted()'s interaction with padding is unclear
147 std::basic_ostringstream<_CharT, _Traits> __ostr;
148 __ostr << __str._M_delim;
149 for (auto __c : __str._M_string)
150 {
151 if (__c == __str._M_delim || __c == __str._M_escape)
152 __ostr << __str._M_escape;
153 __ostr << __c;
154 }
155 __ostr << __str._M_delim;
156
157 return __os << __ostr.str();
158 }
159
160 /**
161 * @brief Extractor for delimited strings.
162 * The left and right delimiters can be different.
163 *
164 * @headerfile iomanip
165 */
166 template<typename _CharT, typename _Traits, typename _Alloc>
167 std::basic_istream<_CharT, _Traits>&
168 operator>>(std::basic_istream<_CharT, _Traits>& __is,
169 const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&,
170 _CharT>& __str)
171 {
172 _CharT __c;
173 __is >> __c;
174 if (!__is.good())
175 return __is;
176 if (__c != __str._M_delim)
177 {
178 __is.unget();
179 __is >> __str._M_string;
180 return __is;
181 }
182 __str._M_string.clear();
183 std::ios_base::fmtflags __flags
184 = __is.flags(__is.flags() & ~std::ios_base::skipws);
185 do
186 {
187 __is >> __c;
188 if (!__is.good())
189 break;
190 if (__c == __str._M_escape)
191 {
192 __is >> __c;
193 if (!__is.good())
194 break;
195 }
196 else if (__c == __str._M_delim)
197 break;
198 __str._M_string += __c;
199 }
200 while (true);
201 __is.setf(__flags);
202
203 return __is;
204 }
205 } // namespace __detail
206
207_GLIBCXX_END_NAMESPACE_VERSION
208} // namespace std
209
210#endif // C++11
211#endif /* _GLIBCXX_QUOTED_STRING_H */
sstream
c++0x_warning.h
std
ISO C++ entities toplevel namespace is std.
std::operator<<
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1754
std::__detail
Implementation details not part of the namespace std interface.
std::__detail::operator>>
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.
Definition quoted_string.h:168
std::__detail::operator<<
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const _Quoted_string< const _CharT *, _CharT > &__str)
Inserter for quoted strings.
Definition quoted_string.h:117
std::basic_ios::clear
void clear(iostate __state=goodbit)
[Re]sets the error state.
Definition basic_ios.tcc:46
std::basic_ios::good
bool good() const
Fast error checking.
Definition basic_ios.h:191
std::basic_istream
Template class basic_istream.
Definition istream:67
std::basic_istream::unget
__istream_type & unget()
Unextracting the previous character.
Definition istream.tcc:806
std::basic_ostream
Template class basic_ostream.
Definition ostream.h:67
std::basic_ostringstream
Controlling output for std::string.
Definition sstream:875
std::basic_ostringstream::str
__string_type str() const &
Copying out the string buffer.
Definition sstream:1062
std::basic_string_view
A non-owning reference to a string.
Definition string_view:113
std::is_pointer
is_pointer
Definition type_traits:628
std::is_reference
is_reference
Definition type_traits:817
std::basic_string
Managing sequences of characters and character-like objects.
Definition basic_string.h:94
std::ios_base::setf
fmtflags setf(fmtflags __fmtfl)
Setting new format flags.
Definition ios_base.h:721
std::ios_base::skipws
static const fmtflags skipws
Skips leading white space before certain input operations.
Definition ios_base.h:423
std::ios_base::fmtflags
_Ios_Fmtflags fmtflags
This is a bitmask type.
Definition ios_base.h:378
std::ios_base::flags
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
std::__detail::_Quoted_string
Struct for delimited strings.
Definition quoted_string.h:52