PR libstdc++/86008 add std::quoted support for string_view
[official-gcc.git] / libstdc++-v3 / include / bits / quoted_string.h
blobf6134da5a432c4b8f7efdc5bd6e274e5b83063e0
1 // Helpers for quoted stream manipulators -*- C++ -*-
3 // Copyright (C) 2013-2018 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.
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.
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.
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/>.
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}
30 #ifndef _GLIBCXX_QUOTED_STRING_H
31 #define _GLIBCXX_QUOTED_STRING_H 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
37 #else
38 #include <sstream>
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 namespace __detail {
45 /**
46 * @brief Struct for delimited strings.
48 template<typename _String, typename _CharT>
49 struct _Quoted_string
51 static_assert(is_reference<_String>::value
52 || is_pointer<_String>::value,
53 "String type must be pointer or reference");
55 _Quoted_string(_String __str, _CharT __del, _CharT __esc)
56 : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
57 { }
59 _Quoted_string&
60 operator=(_Quoted_string&) = delete;
62 _String _M_string;
63 _CharT _M_delim;
64 _CharT _M_escape;
67 #if __cplusplus >= 201703L
68 template<typename _CharT, typename _Traits>
69 struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT>
71 _Quoted_string(basic_string_view<_CharT, _Traits> __str,
72 _CharT __del, _CharT __esc)
73 : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
74 { }
76 _Quoted_string&
77 operator=(_Quoted_string&) = delete;
79 basic_string_view<_CharT, _Traits> _M_string;
80 _CharT _M_delim;
81 _CharT _M_escape;
83 #endif // C++17
85 /**
86 * @brief Inserter for quoted strings.
88 * _GLIBCXX_RESOLVE_LIB_DEFECTS
89 * DR 2344 quoted()'s interaction with padding is unclear
91 template<typename _CharT, typename _Traits>
92 std::basic_ostream<_CharT, _Traits>&
93 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
94 const _Quoted_string<const _CharT*, _CharT>& __str)
96 std::basic_ostringstream<_CharT, _Traits> __ostr;
97 __ostr << __str._M_delim;
98 for (const _CharT* __c = __str._M_string; *__c; ++__c)
100 if (*__c == __str._M_delim || *__c == __str._M_escape)
101 __ostr << __str._M_escape;
102 __ostr << *__c;
104 __ostr << __str._M_delim;
106 return __os << __ostr.str();
110 * @brief Inserter for quoted strings.
112 * _GLIBCXX_RESOLVE_LIB_DEFECTS
113 * DR 2344 quoted()'s interaction with padding is unclear
115 template<typename _CharT, typename _Traits, typename _String>
116 std::basic_ostream<_CharT, _Traits>&
117 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
118 const _Quoted_string<_String, _CharT>& __str)
120 std::basic_ostringstream<_CharT, _Traits> __ostr;
121 __ostr << __str._M_delim;
122 for (auto __c : __str._M_string)
124 if (__c == __str._M_delim || __c == __str._M_escape)
125 __ostr << __str._M_escape;
126 __ostr << __c;
128 __ostr << __str._M_delim;
130 return __os << __ostr.str();
134 * @brief Extractor for delimited strings.
135 * The left and right delimiters can be different.
137 template<typename _CharT, typename _Traits, typename _Alloc>
138 std::basic_istream<_CharT, _Traits>&
139 operator>>(std::basic_istream<_CharT, _Traits>& __is,
140 const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&,
141 _CharT>& __str)
143 _CharT __c;
144 __is >> __c;
145 if (!__is.good())
146 return __is;
147 if (__c != __str._M_delim)
149 __is.unget();
150 __is >> __str._M_string;
151 return __is;
153 __str._M_string.clear();
154 std::ios_base::fmtflags __flags
155 = __is.flags(__is.flags() & ~std::ios_base::skipws);
158 __is >> __c;
159 if (!__is.good())
160 break;
161 if (__c == __str._M_escape)
163 __is >> __c;
164 if (!__is.good())
165 break;
167 else if (__c == __str._M_delim)
168 break;
169 __str._M_string += __c;
171 while (true);
172 __is.setf(__flags);
174 return __is;
176 } // namespace __detail
178 _GLIBCXX_END_NAMESPACE_VERSION
179 } // namespace std
181 #endif // C++11
182 #endif /* _GLIBCXX_QUOTED_STRING_H */