sstream.tcc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // String based streams -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/sstream.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{sstream}
  23. */
  24. //
  25. // ISO C++ 14882: 27.7 String-based streams
  26. //
  27. #ifndef _SSTREAM_TCC
  28. #define _SSTREAM_TCC 1
  29. #pragma GCC system_header
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. template <class _CharT, class _Traits, class _Alloc>
  34. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  35. basic_stringbuf<_CharT, _Traits, _Alloc>::
  36. pbackfail(int_type __c)
  37. {
  38. int_type __ret = traits_type::eof();
  39. if (this->eback() < this->gptr())
  40. {
  41. // Try to put back __c into input sequence in one of three ways.
  42. // Order these tests done in is unspecified by the standard.
  43. const bool __testeof = traits_type::eq_int_type(__c, __ret);
  44. if (!__testeof)
  45. {
  46. const bool __testeq = traits_type::eq(traits_type::
  47. to_char_type(__c),
  48. this->gptr()[-1]);
  49. const bool __testout = this->_M_mode & ios_base::out;
  50. if (__testeq || __testout)
  51. {
  52. this->gbump(-1);
  53. if (!__testeq)
  54. *this->gptr() = traits_type::to_char_type(__c);
  55. __ret = __c;
  56. }
  57. }
  58. else
  59. {
  60. this->gbump(-1);
  61. __ret = traits_type::not_eof(__c);
  62. }
  63. }
  64. return __ret;
  65. }
  66. template <class _CharT, class _Traits, class _Alloc>
  67. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  68. basic_stringbuf<_CharT, _Traits, _Alloc>::
  69. overflow(int_type __c)
  70. {
  71. const bool __testout = this->_M_mode & ios_base::out;
  72. if (__builtin_expect(!__testout, false))
  73. return traits_type::eof();
  74. const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
  75. if (__builtin_expect(__testeof, false))
  76. return traits_type::not_eof(__c);
  77. const __size_type __capacity = _M_string.capacity();
  78. #if _GLIBCXX_USE_CXX11_ABI
  79. if (size_t(this->epptr() - this->pbase()) < __capacity)
  80. {
  81. // There is additional capacity in _M_string that can be used.
  82. char_type* __base = const_cast<char_type*>(_M_string.data());
  83. _M_pbump(__base, __base + __capacity, this->pptr() - this->pbase());
  84. if (_M_mode & ios_base::in)
  85. {
  86. const __size_type __nget = this->gptr() - this->eback();
  87. const __size_type __eget = this->egptr() - this->eback();
  88. this->setg(__base, __base + __nget, __base + __eget + 1);
  89. }
  90. *this->pptr() = traits_type::to_char_type(__c);
  91. this->pbump(1);
  92. return __c;
  93. }
  94. #endif
  95. const __size_type __max_size = _M_string.max_size();
  96. const bool __testput = this->pptr() < this->epptr();
  97. if (__builtin_expect(!__testput && __capacity == __max_size, false))
  98. return traits_type::eof();
  99. // Try to append __c into output sequence in one of two ways.
  100. // Order these tests done in is unspecified by the standard.
  101. const char_type __conv = traits_type::to_char_type(__c);
  102. if (!__testput)
  103. {
  104. // NB: Start ostringstream buffers at 512 chars. This is an
  105. // experimental value (pronounced "arbitrary" in some of the
  106. // hipper English-speaking countries), and can be changed to
  107. // suit particular needs.
  108. //
  109. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  110. // 169. Bad efficiency of overflow() mandated
  111. // 432. stringbuf::overflow() makes only one write position
  112. // available
  113. const __size_type __opt_len = std::max(__size_type(2 * __capacity),
  114. __size_type(512));
  115. const __size_type __len = std::min(__opt_len, __max_size);
  116. __string_type __tmp(_M_string.get_allocator());
  117. __tmp.reserve(__len);
  118. if (this->pbase())
  119. __tmp.assign(this->pbase(), this->epptr() - this->pbase());
  120. __tmp.push_back(__conv);
  121. _M_string.swap(__tmp);
  122. _M_sync(const_cast<char_type*>(_M_string.data()),
  123. this->gptr() - this->eback(), this->pptr() - this->pbase());
  124. }
  125. else
  126. *this->pptr() = __conv;
  127. this->pbump(1);
  128. return __c;
  129. }
  130. template <class _CharT, class _Traits, class _Alloc>
  131. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  132. basic_stringbuf<_CharT, _Traits, _Alloc>::
  133. underflow()
  134. {
  135. int_type __ret = traits_type::eof();
  136. const bool __testin = this->_M_mode & ios_base::in;
  137. if (__testin)
  138. {
  139. // Update egptr() to match the actual string end.
  140. _M_update_egptr();
  141. if (this->gptr() < this->egptr())
  142. __ret = traits_type::to_int_type(*this->gptr());
  143. }
  144. return __ret;
  145. }
  146. template <class _CharT, class _Traits, class _Alloc>
  147. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  148. basic_stringbuf<_CharT, _Traits, _Alloc>::
  149. seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
  150. {
  151. pos_type __ret = pos_type(off_type(-1));
  152. bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  153. bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  154. const bool __testboth = __testin && __testout && __way != ios_base::cur;
  155. __testin &= !(__mode & ios_base::out);
  156. __testout &= !(__mode & ios_base::in);
  157. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  158. // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
  159. const char_type* __beg = __testin ? this->eback() : this->pbase();
  160. if ((__beg || !__off) && (__testin || __testout || __testboth))
  161. {
  162. _M_update_egptr();
  163. off_type __newoffi = __off;
  164. off_type __newoffo = __newoffi;
  165. if (__way == ios_base::cur)
  166. {
  167. __newoffi += this->gptr() - __beg;
  168. __newoffo += this->pptr() - __beg;
  169. }
  170. else if (__way == ios_base::end)
  171. __newoffo = __newoffi += this->egptr() - __beg;
  172. if ((__testin || __testboth)
  173. && __newoffi >= 0
  174. && this->egptr() - __beg >= __newoffi)
  175. {
  176. this->setg(this->eback(), this->eback() + __newoffi,
  177. this->egptr());
  178. __ret = pos_type(__newoffi);
  179. }
  180. if ((__testout || __testboth)
  181. && __newoffo >= 0
  182. && this->egptr() - __beg >= __newoffo)
  183. {
  184. _M_pbump(this->pbase(), this->epptr(), __newoffo);
  185. __ret = pos_type(__newoffo);
  186. }
  187. }
  188. return __ret;
  189. }
  190. template <class _CharT, class _Traits, class _Alloc>
  191. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  192. basic_stringbuf<_CharT, _Traits, _Alloc>::
  193. seekpos(pos_type __sp, ios_base::openmode __mode)
  194. {
  195. pos_type __ret = pos_type(off_type(-1));
  196. const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  197. const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  198. const char_type* __beg = __testin ? this->eback() : this->pbase();
  199. if ((__beg || !off_type(__sp)) && (__testin || __testout))
  200. {
  201. _M_update_egptr();
  202. const off_type __pos(__sp);
  203. const bool __testpos = (0 <= __pos
  204. && __pos <= this->egptr() - __beg);
  205. if (__testpos)
  206. {
  207. if (__testin)
  208. this->setg(this->eback(), this->eback() + __pos,
  209. this->egptr());
  210. if (__testout)
  211. _M_pbump(this->pbase(), this->epptr(), __pos);
  212. __ret = __sp;
  213. }
  214. }
  215. return __ret;
  216. }
  217. template <class _CharT, class _Traits, class _Alloc>
  218. void
  219. basic_stringbuf<_CharT, _Traits, _Alloc>::
  220. _M_sync(char_type* __base, __size_type __i, __size_type __o)
  221. {
  222. const bool __testin = _M_mode & ios_base::in;
  223. const bool __testout = _M_mode & ios_base::out;
  224. char_type* __endg = __base + _M_string.size();
  225. char_type* __endp = __base + _M_string.capacity();
  226. if (__base != _M_string.data())
  227. {
  228. // setbuf: __i == size of buffer area (_M_string.size() == 0).
  229. __endg += __i;
  230. __i = 0;
  231. __endp = __endg;
  232. }
  233. if (__testin)
  234. this->setg(__base, __base + __i, __endg);
  235. if (__testout)
  236. {
  237. _M_pbump(__base, __endp, __o);
  238. // egptr() always tracks the string end. When !__testin,
  239. // for the correct functioning of the streambuf inlines
  240. // the other get area pointers are identical.
  241. if (!__testin)
  242. this->setg(__endg, __endg, __endg);
  243. }
  244. }
  245. template <class _CharT, class _Traits, class _Alloc>
  246. void
  247. basic_stringbuf<_CharT, _Traits, _Alloc>::
  248. _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
  249. {
  250. this->setp(__pbeg, __pend);
  251. while (__off > __gnu_cxx::__numeric_traits<int>::__max)
  252. {
  253. this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
  254. __off -= __gnu_cxx::__numeric_traits<int>::__max;
  255. }
  256. this->pbump(__off);
  257. }
  258. // Inhibit implicit instantiations for required instantiations,
  259. // which are defined via explicit instantiations elsewhere.
  260. #if _GLIBCXX_EXTERN_TEMPLATE
  261. extern template class basic_stringbuf<char>;
  262. extern template class basic_istringstream<char>;
  263. extern template class basic_ostringstream<char>;
  264. extern template class basic_stringstream<char>;
  265. #ifdef _GLIBCXX_USE_WCHAR_T
  266. extern template class basic_stringbuf<wchar_t>;
  267. extern template class basic_istringstream<wchar_t>;
  268. extern template class basic_ostringstream<wchar_t>;
  269. extern template class basic_stringstream<wchar_t>;
  270. #endif
  271. #endif
  272. _GLIBCXX_END_NAMESPACE_VERSION
  273. } // namespace std
  274. #endif