libstdc++
sstream.tcc
Go to the documentation of this file.
1 // String based streams -*- C++ -*-
2 
3 // Copyright (C) 1997-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/sstream.tcc
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{sstream}
28  */
29 
30 //
31 // ISO C++ 14882: 27.7 String-based streams
32 //
33 
34 #ifndef _SSTREAM_TCC
35 #define _SSTREAM_TCC 1
36 
37 #ifdef _GLIBCXX_SYSHDR
38 #pragma GCC system_header
39 #endif
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47  template <class _CharT, class _Traits, class _Alloc>
48  typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
50  pbackfail(int_type __c)
51  {
52  int_type __ret = traits_type::eof();
53  if (this->eback() < this->gptr())
54  {
55  // Try to put back __c into input sequence in one of three ways.
56  // Order these tests done in is unspecified by the standard.
57  const bool __testeof = traits_type::eq_int_type(__c, __ret);
58  if (!__testeof)
59  {
60  const bool __testeq = traits_type::eq(traits_type::
61  to_char_type(__c),
62  this->gptr()[-1]);
63  const bool __testout = this->_M_mode & ios_base::out;
64  if (__testeq || __testout)
65  {
66  this->gbump(-1);
67  if (!__testeq)
68  *this->gptr() = traits_type::to_char_type(__c);
69  __ret = __c;
70  }
71  }
72  else
73  {
74  this->gbump(-1);
75  __ret = traits_type::not_eof(__c);
76  }
77  }
78  return __ret;
79  }
80 
81  template <class _CharT, class _Traits, class _Alloc>
82  typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
84  overflow(int_type __c)
85  {
86  const bool __testout = this->_M_mode & ios_base::out;
87  if (__builtin_expect(!__testout, false))
88  return traits_type::eof();
89 
90  const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
91  if (__builtin_expect(__testeof, false))
92  return traits_type::not_eof(__c);
93 
94  const __size_type __capacity = _M_string.capacity();
95 
96 #if _GLIBCXX_USE_CXX11_ABI
97  if (size_t(this->epptr() - this->pbase()) < __capacity)
98  {
99  // There is additional capacity in _M_string that can be used.
100  char_type* __base = const_cast<char_type*>(_M_string.data());
101  _M_pbump(__base, __base + __capacity, this->pptr() - this->pbase());
102  if (_M_mode & ios_base::in)
103  {
104  const __size_type __nget = this->gptr() - this->eback();
105  const __size_type __eget = this->egptr() - this->eback();
106  this->setg(__base, __base + __nget, __base + __eget + 1);
107  }
108  *this->pptr() = traits_type::to_char_type(__c);
109  this->pbump(1);
110  return __c;
111  }
112 #endif
113 
114  const __size_type __max_size = _M_string.max_size();
115  const bool __testput = this->pptr() < this->epptr();
116  if (__builtin_expect(!__testput && __capacity == __max_size, false))
117  return traits_type::eof();
118 
119  // Try to append __c into output sequence in one of two ways.
120  // Order these tests done in is unspecified by the standard.
121  const char_type __conv = traits_type::to_char_type(__c);
122  if (!__testput)
123  {
124  // NB: Start ostringstream buffers at 512 chars. This is an
125  // experimental value (pronounced "arbitrary" in some of the
126  // hipper English-speaking countries), and can be changed to
127  // suit particular needs.
128  //
129  // _GLIBCXX_RESOLVE_LIB_DEFECTS
130  // 169. Bad efficiency of overflow() mandated
131  // 432. stringbuf::overflow() makes only one write position
132  // available
133  const __size_type __opt_len = std::max(__size_type(2 * __capacity),
134  __size_type(512));
135  const __size_type __len = std::min(__opt_len, __max_size);
136  __string_type __tmp(_M_string.get_allocator());
137  __tmp.reserve(__len);
138  if (this->pbase())
139  __tmp.assign(this->pbase(), this->epptr() - this->pbase());
140  __tmp.push_back(__conv);
141  _M_string.swap(__tmp);
142  _M_sync(const_cast<char_type*>(_M_string.data()),
143  this->gptr() - this->eback(), this->pptr() - this->pbase());
144  }
145  else
146  *this->pptr() = __conv;
147  this->pbump(1);
148  return __c;
149  }
150 
151  template <class _CharT, class _Traits, class _Alloc>
152  typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
154  underflow()
155  {
156  int_type __ret = traits_type::eof();
157  const bool __testin = this->_M_mode & ios_base::in;
158  if (__testin)
159  {
160  // Update egptr() to match the actual string end.
161  _M_update_egptr();
162 
163  if (this->gptr() < this->egptr())
164  __ret = traits_type::to_int_type(*this->gptr());
165  }
166  return __ret;
167  }
168 
169  template <class _CharT, class _Traits, class _Alloc>
170  typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
172  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
173  {
174  pos_type __ret = pos_type(off_type(-1));
175  bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
176  bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
177  const bool __testboth = __testin && __testout && __way != ios_base::cur;
178  __testin &= !(__mode & ios_base::out);
179  __testout &= !(__mode & ios_base::in);
180 
181  // _GLIBCXX_RESOLVE_LIB_DEFECTS
182  // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
183  const char_type* __beg = __testin ? this->eback() : this->pbase();
184  if ((__beg || !__off) && (__testin || __testout || __testboth))
185  {
186  _M_update_egptr();
187 
188  off_type __newoffi = __off;
189  off_type __newoffo = __newoffi;
190  if (__way == ios_base::cur)
191  {
192  __newoffi += this->gptr() - __beg;
193  __newoffo += this->pptr() - __beg;
194  }
195  else if (__way == ios_base::end)
196  __newoffo = __newoffi += this->egptr() - __beg;
197 
198  if ((__testin || __testboth)
199  && __newoffi >= 0
200  && this->egptr() - __beg >= __newoffi)
201  {
202  this->setg(this->eback(), this->eback() + __newoffi,
203  this->egptr());
204  __ret = pos_type(__newoffi);
205  }
206  if ((__testout || __testboth)
207  && __newoffo >= 0
208  && this->egptr() - __beg >= __newoffo)
209  {
210  _M_pbump(this->pbase(), this->epptr(), __newoffo);
211  __ret = pos_type(__newoffo);
212  }
213  }
214  return __ret;
215  }
216 
217  template <class _CharT, class _Traits, class _Alloc>
218  typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
220  seekpos(pos_type __sp, ios_base::openmode __mode)
221  {
222  pos_type __ret = pos_type(off_type(-1));
223  const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
224  const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
225 
226  const char_type* __beg = __testin ? this->eback() : this->pbase();
227  if ((__beg || !off_type(__sp)) && (__testin || __testout))
228  {
229  _M_update_egptr();
230 
231  const off_type __pos(__sp);
232  const bool __testpos = (0 <= __pos
233  && __pos <= this->egptr() - __beg);
234  if (__testpos)
235  {
236  if (__testin)
237  this->setg(this->eback(), this->eback() + __pos,
238  this->egptr());
239  if (__testout)
240  _M_pbump(this->pbase(), this->epptr(), __pos);
241  __ret = __sp;
242  }
243  }
244  return __ret;
245  }
246 
247  template <class _CharT, class _Traits, class _Alloc>
248  void
250  _M_sync(char_type* __base, __size_type __i, __size_type __o)
251  {
252  const bool __testin = _M_mode & ios_base::in;
253  const bool __testout = _M_mode & ios_base::out;
254  char_type* __endg = __base + _M_string.size();
255  char_type* __endp = __base + _M_string.capacity();
256 
257  if (__base != _M_string.data())
258  {
259  // setbuf: __i == size of buffer area (_M_string.size() == 0).
260  __endg += __i;
261  __i = 0;
262  __endp = __endg;
263  }
264 
265  if (__testin)
266  this->setg(__base, __base + __i, __endg);
267  if (__testout)
268  {
269  _M_pbump(__base, __endp, __o);
270  // egptr() always tracks the string end. When !__testin,
271  // for the correct functioning of the streambuf inlines
272  // the other get area pointers are identical.
273  if (!__testin)
274  this->setg(__endg, __endg, __endg);
275  }
276  }
277 
278  template <class _CharT, class _Traits, class _Alloc>
279  void
280  basic_stringbuf<_CharT, _Traits, _Alloc>::
281  _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
282  {
283  this->setp(__pbeg, __pend);
284  while (__off > __gnu_cxx::__numeric_traits<int>::__max)
285  {
286  this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
287  __off -= __gnu_cxx::__numeric_traits<int>::__max;
288  }
289  this->pbump(__off);
290  }
291 
292  // Inhibit implicit instantiations for required instantiations,
293  // which are defined via explicit instantiations elsewhere.
294 #if _GLIBCXX_EXTERN_TEMPLATE
295  extern template class basic_stringbuf<char>;
296  extern template class basic_istringstream<char>;
297  extern template class basic_ostringstream<char>;
298  extern template class basic_stringstream<char>;
299 
300 #ifdef _GLIBCXX_USE_WCHAR_T
301  extern template class basic_stringbuf<wchar_t>;
302  extern template class basic_istringstream<wchar_t>;
303  extern template class basic_ostringstream<wchar_t>;
304  extern template class basic_stringstream<wchar_t>;
305 #endif
306 #endif
307 
308 _GLIBCXX_END_NAMESPACE_VERSION
309 } // namespace std
310 
311 #pragma GCC diagnostic pop
312 #endif
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:258
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:234
ISO C++ entities toplevel namespace is std.
constexpr _Iterator __base(_Iterator __it)
The actual work of input and output (for std::string).
Definition: sstream:82
virtual int_type underflow()
Fetches more data from the controlled sequence.
Definition: sstream.tcc:154
virtual pos_type seekpos(pos_type __sp, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition: sstream.tcc:220
virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition: sstream.tcc:172
virtual int_type overflow(int_type __c=traits_type::eof())
Consumes data from the buffer; writes to the controlled sequence.
Definition: sstream.tcc:84
virtual int_type pbackfail(int_type __c=traits_type::eof())
Tries to back up the input sequence.
Definition: sstream.tcc:50
void push_back(_CharT __c)
Append a single character.
Definition: cow_string.h:1437
void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
Definition: cow_string.h:3653
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition: cow_string.h:3391
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition: ios_base.h:529
static const seekdir end
Request a seek relative to the current end of the sequence.
Definition: ios_base.h:532
static const openmode in
Open for input. Default for ifstream and fstream.
Definition: ios_base.h:498
static const openmode out
Open for output. Default for ofstream and fstream.
Definition: ios_base.h:501