sstream.tcc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // String based streams -*- C++ -*-
  2. // Copyright (C) 1997-2015 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. const __size_type __max_size = _M_string.max_size();
  79. const bool __testput = this->pptr() < this->epptr();
  80. if (__builtin_expect(!__testput && __capacity == __max_size, false))
  81. return traits_type::eof();
  82. // Try to append __c into output sequence in one of two ways.
  83. // Order these tests done in is unspecified by the standard.
  84. const char_type __conv = traits_type::to_char_type(__c);
  85. if (!__testput)
  86. {
  87. // NB: Start ostringstream buffers at 512 chars. This is an
  88. // experimental value (pronounced "arbitrary" in some of the
  89. // hipper English-speaking countries), and can be changed to
  90. // suit particular needs.
  91. //
  92. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  93. // 169. Bad efficiency of overflow() mandated
  94. // 432. stringbuf::overflow() makes only one write position
  95. // available
  96. const __size_type __opt_len = std::max(__size_type(2 * __capacity),
  97. __size_type(512));
  98. const __size_type __len = std::min(__opt_len, __max_size);
  99. __string_type __tmp;
  100. __tmp.reserve(__len);
  101. if (this->pbase())
  102. __tmp.assign(this->pbase(), this->epptr() - this->pbase());
  103. __tmp.push_back(__conv);
  104. _M_string.swap(__tmp);
  105. _M_sync(const_cast<char_type*>(_M_string.data()),
  106. this->gptr() - this->eback(), this->pptr() - this->pbase());
  107. }
  108. else
  109. *this->pptr() = __conv;
  110. this->pbump(1);
  111. return __c;
  112. }
  113. template <class _CharT, class _Traits, class _Alloc>
  114. typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
  115. basic_stringbuf<_CharT, _Traits, _Alloc>::
  116. underflow()
  117. {
  118. int_type __ret = traits_type::eof();
  119. const bool __testin = this->_M_mode & ios_base::in;
  120. if (__testin)
  121. {
  122. // Update egptr() to match the actual string end.
  123. _M_update_egptr();
  124. if (this->gptr() < this->egptr())
  125. __ret = traits_type::to_int_type(*this->gptr());
  126. }
  127. return __ret;
  128. }
  129. template <class _CharT, class _Traits, class _Alloc>
  130. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  131. basic_stringbuf<_CharT, _Traits, _Alloc>::
  132. seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
  133. {
  134. pos_type __ret = pos_type(off_type(-1));
  135. bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  136. bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  137. const bool __testboth = __testin && __testout && __way != ios_base::cur;
  138. __testin &= !(__mode & ios_base::out);
  139. __testout &= !(__mode & ios_base::in);
  140. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  141. // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
  142. const char_type* __beg = __testin ? this->eback() : this->pbase();
  143. if ((__beg || !__off) && (__testin || __testout || __testboth))
  144. {
  145. _M_update_egptr();
  146. off_type __newoffi = __off;
  147. off_type __newoffo = __newoffi;
  148. if (__way == ios_base::cur)
  149. {
  150. __newoffi += this->gptr() - __beg;
  151. __newoffo += this->pptr() - __beg;
  152. }
  153. else if (__way == ios_base::end)
  154. __newoffo = __newoffi += this->egptr() - __beg;
  155. if ((__testin || __testboth)
  156. && __newoffi >= 0
  157. && this->egptr() - __beg >= __newoffi)
  158. {
  159. this->setg(this->eback(), this->eback() + __newoffi,
  160. this->egptr());
  161. __ret = pos_type(__newoffi);
  162. }
  163. if ((__testout || __testboth)
  164. && __newoffo >= 0
  165. && this->egptr() - __beg >= __newoffo)
  166. {
  167. _M_pbump(this->pbase(), this->epptr(), __newoffo);
  168. __ret = pos_type(__newoffo);
  169. }
  170. }
  171. return __ret;
  172. }
  173. template <class _CharT, class _Traits, class _Alloc>
  174. typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
  175. basic_stringbuf<_CharT, _Traits, _Alloc>::
  176. seekpos(pos_type __sp, ios_base::openmode __mode)
  177. {
  178. pos_type __ret = pos_type(off_type(-1));
  179. const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
  180. const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
  181. const char_type* __beg = __testin ? this->eback() : this->pbase();
  182. if ((__beg || !off_type(__sp)) && (__testin || __testout))
  183. {
  184. _M_update_egptr();
  185. const off_type __pos(__sp);
  186. const bool __testpos = (0 <= __pos
  187. && __pos <= this->egptr() - __beg);
  188. if (__testpos)
  189. {
  190. if (__testin)
  191. this->setg(this->eback(), this->eback() + __pos,
  192. this->egptr());
  193. if (__testout)
  194. _M_pbump(this->pbase(), this->epptr(), __pos);
  195. __ret = __sp;
  196. }
  197. }
  198. return __ret;
  199. }
  200. template <class _CharT, class _Traits, class _Alloc>
  201. void
  202. basic_stringbuf<_CharT, _Traits, _Alloc>::
  203. _M_sync(char_type* __base, __size_type __i, __size_type __o)
  204. {
  205. const bool __testin = _M_mode & ios_base::in;
  206. const bool __testout = _M_mode & ios_base::out;
  207. char_type* __endg = __base + _M_string.size();
  208. char_type* __endp = __base + _M_string.capacity();
  209. if (__base != _M_string.data())
  210. {
  211. // setbuf: __i == size of buffer area (_M_string.size() == 0).
  212. __endg += __i;
  213. __i = 0;
  214. __endp = __endg;
  215. }
  216. if (__testin)
  217. this->setg(__base, __base + __i, __endg);
  218. if (__testout)
  219. {
  220. _M_pbump(__base, __endp, __o);
  221. // egptr() always tracks the string end. When !__testin,
  222. // for the correct functioning of the streambuf inlines
  223. // the other get area pointers are identical.
  224. if (!__testin)
  225. this->setg(__endg, __endg, __endg);
  226. }
  227. }
  228. template <class _CharT, class _Traits, class _Alloc>
  229. void
  230. basic_stringbuf<_CharT, _Traits, _Alloc>::
  231. _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
  232. {
  233. this->setp(__pbeg, __pend);
  234. while (__off > __gnu_cxx::__numeric_traits<int>::__max)
  235. {
  236. this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
  237. __off -= __gnu_cxx::__numeric_traits<int>::__max;
  238. }
  239. this->pbump(__off);
  240. }
  241. // Inhibit implicit instantiations for required instantiations,
  242. // which are defined via explicit instantiations elsewhere.
  243. #if _GLIBCXX_EXTERN_TEMPLATE
  244. extern template class basic_stringbuf<char>;
  245. extern template class basic_istringstream<char>;
  246. extern template class basic_ostringstream<char>;
  247. extern template class basic_stringstream<char>;
  248. #ifdef _GLIBCXX_USE_WCHAR_T
  249. extern template class basic_stringbuf<wchar_t>;
  250. extern template class basic_istringstream<wchar_t>;
  251. extern template class basic_ostringstream<wchar_t>;
  252. extern template class basic_stringstream<wchar_t>;
  253. #endif
  254. #endif
  255. _GLIBCXX_END_NAMESPACE_VERSION
  256. } // namespace std
  257. #endif