]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/sstream.tcc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / sstream.tcc
CommitLineData
725dc051
BK
1// String based streams -*- C++ -*-
2
7adcbafe 3// Copyright (C) 1997-2022 Free Software Foundation, Inc.
725dc051
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
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
748086b7
JJ
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/>.
725dc051 24
f910786b 25/** @file bits/sstream.tcc
0aa06b18 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{sstream}
0aa06b18
BK
28 */
29
725dc051
BK
30//
31// ISO C++ 14882: 27.7 String-based streams
32//
33
3d7c150e
BK
34#ifndef _SSTREAM_TCC
35#define _SSTREAM_TCC 1
725dc051 36
3b794528
BK
37#pragma GCC system_header
38
12ffa228
BK
39namespace std _GLIBCXX_VISIBILITY(default)
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 42
725dc051 43 template <class _CharT, class _Traits, class _Alloc>
ed6814f7 44 typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
725dc051
BK
45 basic_stringbuf<_CharT, _Traits, _Alloc>::
46 pbackfail(int_type __c)
47 {
48 int_type __ret = traits_type::eof();
1b170b55 49 if (this->eback() < this->gptr())
725dc051 50 {
62929dd9
PC
51 // Try to put back __c into input sequence in one of three ways.
52 // Order these tests done in is unspecified by the standard.
abccc4f6
PC
53 const bool __testeof = traits_type::eq_int_type(__c, __ret);
54 if (!__testeof)
55 {
56 const bool __testeq = traits_type::eq(traits_type::
57 to_char_type(__c),
58 this->gptr()[-1]);
59 const bool __testout = this->_M_mode & ios_base::out;
60 if (__testeq || __testout)
61 {
62 this->gbump(-1);
63 if (!__testeq)
64 *this->gptr() = traits_type::to_char_type(__c);
65 __ret = __c;
66 }
67 }
ca78f36a 68 else
725dc051 69 {
abccc4f6
PC
70 this->gbump(-1);
71 __ret = traits_type::not_eof(__c);
725dc051 72 }
725dc051
BK
73 }
74 return __ret;
75 }
ed6814f7 76
725dc051 77 template <class _CharT, class _Traits, class _Alloc>
ed6814f7 78 typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
725dc051
BK
79 basic_stringbuf<_CharT, _Traits, _Alloc>::
80 overflow(int_type __c)
81 {
5e77a357 82 const bool __testout = this->_M_mode & ios_base::out;
391cd095
PC
83 if (__builtin_expect(!__testout, false))
84 return traits_type::eof();
23cac885 85
42134429 86 const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
391cd095
PC
87 if (__builtin_expect(__testeof, false))
88 return traits_type::not_eof(__c);
89
9f9000d1 90 const __size_type __capacity = _M_string.capacity();
93ef155b
JW
91
92#if _GLIBCXX_USE_CXX11_ABI
2232b613 93 if (size_t(this->epptr() - this->pbase()) < __capacity)
93ef155b
JW
94 {
95 // There is additional capacity in _M_string that can be used.
96 char_type* __base = const_cast<char_type*>(_M_string.data());
97 _M_pbump(__base, __base + __capacity, this->pptr() - this->pbase());
98 if (_M_mode & ios_base::in)
99 {
100 const __size_type __nget = this->gptr() - this->eback();
101 const __size_type __eget = this->egptr() - this->eback();
102 this->setg(__base, __base + __nget, __base + __eget + 1);
103 }
104 *this->pptr() = traits_type::to_char_type(__c);
105 this->pbump(1);
106 return __c;
107 }
108#endif
109
9f9000d1 110 const __size_type __max_size = _M_string.max_size();
1b170b55 111 const bool __testput = this->pptr() < this->epptr();
9f9000d1 112 if (__builtin_expect(!__testput && __capacity == __max_size, false))
391cd095 113 return traits_type::eof();
725dc051
BK
114
115 // Try to append __c into output sequence in one of two ways.
116 // Order these tests done in is unspecified by the standard.
10d9600d 117 const char_type __conv = traits_type::to_char_type(__c);
23cac885 118 if (!__testput)
725dc051 119 {
10d9600d 120 // NB: Start ostringstream buffers at 512 chars. This is an
9f9000d1 121 // experimental value (pronounced "arbitrary" in some of the
28dac70a 122 // hipper English-speaking countries), and can be changed to
9f9000d1 123 // suit particular needs.
10d9600d
PC
124 //
125 // _GLIBCXX_RESOLVE_LIB_DEFECTS
126 // 169. Bad efficiency of overflow() mandated
127 // 432. stringbuf::overflow() makes only one write position
128 // available
9f9000d1
PC
129 const __size_type __opt_len = std::max(__size_type(2 * __capacity),
130 __size_type(512));
131 const __size_type __len = std::min(__opt_len, __max_size);
387c17d3 132 __string_type __tmp(_M_string.get_allocator());
74843551 133 __tmp.reserve(__len);
dbb8bfe8
PC
134 if (this->pbase())
135 __tmp.assign(this->pbase(), this->epptr() - this->pbase());
10d9600d 136 __tmp.push_back(__conv);
74843551 137 _M_string.swap(__tmp);
655d7821 138 _M_sync(const_cast<char_type*>(_M_string.data()),
42134429 139 this->gptr() - this->eback(), this->pptr() - this->pbase());
725dc051 140 }
10d9600d
PC
141 else
142 *this->pptr() = __conv;
143 this->pbump(1);
144 return __c;
725dc051
BK
145 }
146
1b170b55 147 template <class _CharT, class _Traits, class _Alloc>
ed6814f7 148 typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
1b170b55 149 basic_stringbuf<_CharT, _Traits, _Alloc>::
cd16e04b 150 underflow()
1b170b55
PC
151 {
152 int_type __ret = traits_type::eof();
153 const bool __testin = this->_M_mode & ios_base::in;
154 if (__testin)
155 {
156 // Update egptr() to match the actual string end.
157 _M_update_egptr();
983de0da 158
1b170b55 159 if (this->gptr() < this->egptr())
cd16e04b 160 __ret = traits_type::to_int_type(*this->gptr());
1b170b55
PC
161 }
162 return __ret;
163 }
164
725dc051 165 template <class _CharT, class _Traits, class _Alloc>
31bfa177 166 typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
725dc051
BK
167 basic_stringbuf<_CharT, _Traits, _Alloc>::
168 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
169 {
ed6814f7 170 pos_type __ret = pos_type(off_type(-1));
8fbc5ae7
MM
171 bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
172 bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
5e77a357 173 const bool __testboth = __testin && __testout && __way != ios_base::cur;
b988dfc5
BK
174 __testin &= !(__mode & ios_base::out);
175 __testout &= !(__mode & ios_base::in);
176
9c9e97bd
PC
177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
178 // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
983de0da 179 const char_type* __beg = __testin ? this->eback() : this->pbase();
9c9e97bd 180 if ((__beg || !__off) && (__testin || __testout || __testboth))
725dc051 181 {
1b170b55 182 _M_update_egptr();
725dc051 183
0a162bde
PC
184 off_type __newoffi = __off;
185 off_type __newoffo = __newoffi;
725dc051
BK
186 if (__way == ios_base::cur)
187 {
0a162bde
PC
188 __newoffi += this->gptr() - __beg;
189 __newoffo += this->pptr() - __beg;
725dc051
BK
190 }
191 else if (__way == ios_base::end)
0a162bde 192 __newoffo = __newoffi += this->egptr() - __beg;
725dc051 193
b988dfc5 194 if ((__testin || __testboth)
0a162bde
PC
195 && __newoffi >= 0
196 && this->egptr() - __beg >= __newoffi)
725dc051 197 {
1139a735
PC
198 this->setg(this->eback(), this->eback() + __newoffi,
199 this->egptr());
725dc051
BK
200 __ret = pos_type(__newoffi);
201 }
b988dfc5 202 if ((__testout || __testboth)
0a162bde
PC
203 && __newoffo >= 0
204 && this->egptr() - __beg >= __newoffo)
725dc051 205 {
1139a735 206 _M_pbump(this->pbase(), this->epptr(), __newoffo);
725dc051
BK
207 __ret = pos_type(__newoffo);
208 }
209 }
210 return __ret;
211 }
212
213 template <class _CharT, class _Traits, class _Alloc>
31bfa177 214 typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
725dc051
BK
215 basic_stringbuf<_CharT, _Traits, _Alloc>::
216 seekpos(pos_type __sp, ios_base::openmode __mode)
217 {
ed6814f7 218 pos_type __ret = pos_type(off_type(-1));
983de0da
PC
219 const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
220 const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
ed6814f7 221
983de0da 222 const char_type* __beg = __testin ? this->eback() : this->pbase();
bcf0248a 223 if ((__beg || !off_type(__sp)) && (__testin || __testout))
983de0da 224 {
1b170b55
PC
225 _M_update_egptr();
226
f67b6b7a 227 const off_type __pos(__sp);
10d9600d
PC
228 const bool __testpos = (0 <= __pos
229 && __pos <= this->egptr() - __beg);
f67b6b7a 230 if (__testpos)
b988dfc5 231 {
1b170b55 232 if (__testin)
1139a735
PC
233 this->setg(this->eback(), this->eback() + __pos,
234 this->egptr());
1b170b55 235 if (__testout)
1139a735 236 _M_pbump(this->pbase(), this->epptr(), __pos);
9d7bc6de 237 __ret = __sp;
b988dfc5 238 }
725dc051 239 }
725dc051
BK
240 return __ret;
241 }
a32e3c09 242
10d9600d
PC
243 template <class _CharT, class _Traits, class _Alloc>
244 void
245 basic_stringbuf<_CharT, _Traits, _Alloc>::
246 _M_sync(char_type* __base, __size_type __i, __size_type __o)
247 {
248 const bool __testin = _M_mode & ios_base::in;
249 const bool __testout = _M_mode & ios_base::out;
62448787
PC
250 char_type* __endg = __base + _M_string.size();
251 char_type* __endp = __base + _M_string.capacity();
252
253 if (__base != _M_string.data())
254 {
255 // setbuf: __i == size of buffer area (_M_string.size() == 0).
256 __endg += __i;
257 __i = 0;
258 __endp = __endg;
259 }
260
10d9600d 261 if (__testin)
62448787 262 this->setg(__base, __base + __i, __endg);
10d9600d
PC
263 if (__testout)
264 {
1139a735 265 _M_pbump(__base, __endp, __o);
10d9600d
PC
266 // egptr() always tracks the string end. When !__testin,
267 // for the correct functioning of the streambuf inlines
268 // the other get area pointers are identical.
269 if (!__testin)
62448787 270 this->setg(__endg, __endg, __endg);
10d9600d
PC
271 }
272 }
273
1139a735
PC
274 template <class _CharT, class _Traits, class _Alloc>
275 void
276 basic_stringbuf<_CharT, _Traits, _Alloc>::
277 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
278 {
279 this->setp(__pbeg, __pend);
280 while (__off > __gnu_cxx::__numeric_traits<int>::__max)
281 {
282 this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
283 __off -= __gnu_cxx::__numeric_traits<int>::__max;
284 }
285 this->pbump(__off);
286 }
287
a32e3c09 288 // Inhibit implicit instantiations for required instantiations,
ed6814f7 289 // which are defined via explicit instantiations elsewhere.
51b6facb 290#if _GLIBCXX_EXTERN_TEMPLATE
a32e3c09 291 extern template class basic_stringbuf<char>;
a32e3c09 292 extern template class basic_istringstream<char>;
a32e3c09 293 extern template class basic_ostringstream<char>;
a32e3c09 294 extern template class basic_stringstream<char>;
5112ae3a 295
3d7c150e 296#ifdef _GLIBCXX_USE_WCHAR_T
5112ae3a
BK
297 extern template class basic_stringbuf<wchar_t>;
298 extern template class basic_istringstream<wchar_t>;
299 extern template class basic_ostringstream<wchar_t>;
a32e3c09 300 extern template class basic_stringstream<wchar_t>;
5112ae3a 301#endif
51b6facb 302#endif
3cbc7af0 303
12ffa228 304_GLIBCXX_END_NAMESPACE_VERSION
ed4f96af 305} // namespace std
725dc051 306
6f48900c 307#endif