streambuf_iterator.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // Streambuf iterators
  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/streambuf_iterator.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{iterator}
  23. */
  24. #ifndef _STREAMBUF_ITERATOR_H
  25. #define _STREAMBUF_ITERATOR_H 1
  26. #pragma GCC system_header
  27. #include <streambuf>
  28. #include <debug/debug.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. /**
  33. * @addtogroup iterators
  34. * @{
  35. */
  36. // Ignore warnings about std::iterator.
  37. #pragma GCC diagnostic push
  38. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  39. // 24.5.3 Template class istreambuf_iterator
  40. /// Provides input iterator semantics for streambufs.
  41. template<typename _CharT, typename _Traits>
  42. class istreambuf_iterator
  43. : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
  44. _CharT*, _CharT>
  45. {
  46. public:
  47. // Types:
  48. ///@{
  49. /// Public typedefs
  50. #if __cplusplus < 201103L
  51. typedef _CharT& reference; // Changed to _CharT by LWG 445
  52. #elif __cplusplus > 201703L
  53. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  54. // 3188. istreambuf_iterator::pointer should not be unspecified
  55. using pointer = void;
  56. #endif
  57. typedef _CharT char_type;
  58. typedef _Traits traits_type;
  59. typedef typename _Traits::int_type int_type;
  60. typedef basic_streambuf<_CharT, _Traits> streambuf_type;
  61. typedef basic_istream<_CharT, _Traits> istream_type;
  62. ///@}
  63. template<typename _CharT2>
  64. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  65. ostreambuf_iterator<_CharT2> >::__type
  66. copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
  67. ostreambuf_iterator<_CharT2>);
  68. template<bool _IsMove, typename _CharT2>
  69. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  70. _CharT2*>::__type
  71. __copy_move_a2(istreambuf_iterator<_CharT2>,
  72. istreambuf_iterator<_CharT2>, _CharT2*);
  73. template<typename _CharT2, typename _Size>
  74. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  75. _CharT2*>::__type
  76. __copy_n_a(istreambuf_iterator<_CharT2>, _Size, _CharT2*, bool);
  77. template<typename _CharT2>
  78. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  79. istreambuf_iterator<_CharT2> >::__type
  80. find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
  81. const _CharT2&);
  82. template<typename _CharT2, typename _Distance>
  83. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  84. void>::__type
  85. advance(istreambuf_iterator<_CharT2>&, _Distance);
  86. private:
  87. // 24.5.3 istreambuf_iterator
  88. // p 1
  89. // If the end of stream is reached (streambuf_type::sgetc()
  90. // returns traits_type::eof()), the iterator becomes equal to
  91. // the "end of stream" iterator value.
  92. // NB: This implementation assumes the "end of stream" value
  93. // is EOF, or -1.
  94. mutable streambuf_type* _M_sbuf;
  95. int_type _M_c;
  96. public:
  97. /// Construct end of input stream iterator.
  98. _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
  99. : _M_sbuf(0), _M_c(traits_type::eof()) { }
  100. #if __cplusplus > 201703L && __cpp_lib_concepts
  101. constexpr istreambuf_iterator(default_sentinel_t) noexcept
  102. : istreambuf_iterator() { }
  103. #endif
  104. #if __cplusplus >= 201103L
  105. istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
  106. ~istreambuf_iterator() = default;
  107. #endif
  108. /// Construct start of input stream iterator.
  109. istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
  110. : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
  111. /// Construct start of streambuf iterator.
  112. istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
  113. : _M_sbuf(__s), _M_c(traits_type::eof()) { }
  114. #if __cplusplus >= 201103L
  115. istreambuf_iterator&
  116. operator=(const istreambuf_iterator&) noexcept = default;
  117. #endif
  118. /// Return the current character pointed to by iterator. This returns
  119. /// streambuf.sgetc(). It cannot be assigned. NB: The result of
  120. /// operator*() on an end of stream is undefined.
  121. _GLIBCXX_NODISCARD
  122. char_type
  123. operator*() const
  124. {
  125. int_type __c = _M_get();
  126. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  127. // Dereferencing a past-the-end istreambuf_iterator is a
  128. // libstdc++ extension
  129. __glibcxx_requires_cond(!_S_is_eof(__c),
  130. _M_message(__gnu_debug::__msg_deref_istreambuf)
  131. ._M_iterator(*this));
  132. #endif
  133. return traits_type::to_char_type(__c);
  134. }
  135. /// Advance the iterator. Calls streambuf.sbumpc().
  136. istreambuf_iterator&
  137. operator++()
  138. {
  139. __glibcxx_requires_cond(_M_sbuf &&
  140. (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
  141. _M_message(__gnu_debug::__msg_inc_istreambuf)
  142. ._M_iterator(*this));
  143. _M_sbuf->sbumpc();
  144. _M_c = traits_type::eof();
  145. return *this;
  146. }
  147. /// Advance the iterator. Calls streambuf.sbumpc().
  148. istreambuf_iterator
  149. operator++(int)
  150. {
  151. __glibcxx_requires_cond(_M_sbuf &&
  152. (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
  153. _M_message(__gnu_debug::__msg_inc_istreambuf)
  154. ._M_iterator(*this));
  155. istreambuf_iterator __old = *this;
  156. __old._M_c = _M_sbuf->sbumpc();
  157. _M_c = traits_type::eof();
  158. return __old;
  159. }
  160. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  161. // 110 istreambuf_iterator::equal not const
  162. // NB: there is also number 111 (NAD) relevant to this function.
  163. /// Return true both iterators are end or both are not end.
  164. _GLIBCXX_NODISCARD
  165. bool
  166. equal(const istreambuf_iterator& __b) const
  167. { return _M_at_eof() == __b._M_at_eof(); }
  168. private:
  169. int_type
  170. _M_get() const
  171. {
  172. int_type __ret = _M_c;
  173. if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
  174. _M_sbuf = 0;
  175. return __ret;
  176. }
  177. bool
  178. _M_at_eof() const
  179. { return _S_is_eof(_M_get()); }
  180. static bool
  181. _S_is_eof(int_type __c)
  182. {
  183. const int_type __eof = traits_type::eof();
  184. return traits_type::eq_int_type(__c, __eof);
  185. }
  186. #if __cplusplus > 201703L && __cpp_lib_concepts
  187. [[nodiscard]]
  188. friend bool
  189. operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
  190. { return __i._M_at_eof(); }
  191. #endif
  192. };
  193. template<typename _CharT, typename _Traits>
  194. _GLIBCXX_NODISCARD
  195. inline bool
  196. operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
  197. const istreambuf_iterator<_CharT, _Traits>& __b)
  198. { return __a.equal(__b); }
  199. #if __cpp_impl_three_way_comparison < 201907L
  200. template<typename _CharT, typename _Traits>
  201. _GLIBCXX_NODISCARD
  202. inline bool
  203. operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
  204. const istreambuf_iterator<_CharT, _Traits>& __b)
  205. { return !__a.equal(__b); }
  206. #endif
  207. /// Provides output iterator semantics for streambufs.
  208. template<typename _CharT, typename _Traits>
  209. class ostreambuf_iterator
  210. : public iterator<output_iterator_tag, void, void, void, void>
  211. {
  212. public:
  213. // Types:
  214. ///@{
  215. /// Public typedefs
  216. #if __cplusplus > 201703L
  217. using difference_type = ptrdiff_t;
  218. #endif
  219. typedef _CharT char_type;
  220. typedef _Traits traits_type;
  221. typedef basic_streambuf<_CharT, _Traits> streambuf_type;
  222. typedef basic_ostream<_CharT, _Traits> ostream_type;
  223. ///@}
  224. template<typename _CharT2>
  225. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  226. ostreambuf_iterator<_CharT2> >::__type
  227. copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
  228. ostreambuf_iterator<_CharT2>);
  229. private:
  230. streambuf_type* _M_sbuf;
  231. bool _M_failed;
  232. public:
  233. #if __cplusplus > 201703L
  234. constexpr
  235. ostreambuf_iterator() noexcept
  236. : _M_sbuf(nullptr), _M_failed(true) { }
  237. #endif
  238. /// Construct output iterator from ostream.
  239. ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
  240. : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
  241. /// Construct output iterator from streambuf.
  242. ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
  243. : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
  244. /// Write character to streambuf. Calls streambuf.sputc().
  245. ostreambuf_iterator&
  246. operator=(_CharT __c)
  247. {
  248. if (!_M_failed &&
  249. _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
  250. _M_failed = true;
  251. return *this;
  252. }
  253. /// Return *this.
  254. _GLIBCXX_NODISCARD
  255. ostreambuf_iterator&
  256. operator*()
  257. { return *this; }
  258. /// Return *this.
  259. ostreambuf_iterator&
  260. operator++(int)
  261. { return *this; }
  262. /// Return *this.
  263. ostreambuf_iterator&
  264. operator++()
  265. { return *this; }
  266. /// Return true if previous operator=() failed.
  267. _GLIBCXX_NODISCARD
  268. bool
  269. failed() const _GLIBCXX_USE_NOEXCEPT
  270. { return _M_failed; }
  271. ostreambuf_iterator&
  272. _M_put(const _CharT* __ws, streamsize __len)
  273. {
  274. if (__builtin_expect(!_M_failed, true)
  275. && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
  276. false))
  277. _M_failed = true;
  278. return *this;
  279. }
  280. };
  281. #pragma GCC diagnostic pop
  282. // Overloads for streambuf iterators.
  283. template<typename _CharT>
  284. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  285. ostreambuf_iterator<_CharT> >::__type
  286. copy(istreambuf_iterator<_CharT> __first,
  287. istreambuf_iterator<_CharT> __last,
  288. ostreambuf_iterator<_CharT> __result)
  289. {
  290. if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
  291. {
  292. bool __ineof;
  293. __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
  294. if (!__ineof)
  295. __result._M_failed = true;
  296. }
  297. return __result;
  298. }
  299. template<bool _IsMove, typename _CharT>
  300. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  301. ostreambuf_iterator<_CharT> >::__type
  302. __copy_move_a2(_CharT* __first, _CharT* __last,
  303. ostreambuf_iterator<_CharT> __result)
  304. {
  305. const streamsize __num = __last - __first;
  306. if (__num > 0)
  307. __result._M_put(__first, __num);
  308. return __result;
  309. }
  310. template<bool _IsMove, typename _CharT>
  311. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  312. ostreambuf_iterator<_CharT> >::__type
  313. __copy_move_a2(const _CharT* __first, const _CharT* __last,
  314. ostreambuf_iterator<_CharT> __result)
  315. {
  316. const streamsize __num = __last - __first;
  317. if (__num > 0)
  318. __result._M_put(__first, __num);
  319. return __result;
  320. }
  321. template<bool _IsMove, typename _CharT>
  322. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  323. _CharT*>::__type
  324. __copy_move_a2(istreambuf_iterator<_CharT> __first,
  325. istreambuf_iterator<_CharT> __last, _CharT* __result)
  326. {
  327. typedef istreambuf_iterator<_CharT> __is_iterator_type;
  328. typedef typename __is_iterator_type::traits_type traits_type;
  329. typedef typename __is_iterator_type::streambuf_type streambuf_type;
  330. typedef typename traits_type::int_type int_type;
  331. if (__first._M_sbuf && !__last._M_sbuf)
  332. {
  333. streambuf_type* __sb = __first._M_sbuf;
  334. int_type __c = __sb->sgetc();
  335. while (!traits_type::eq_int_type(__c, traits_type::eof()))
  336. {
  337. const streamsize __n = __sb->egptr() - __sb->gptr();
  338. if (__n > 1)
  339. {
  340. traits_type::copy(__result, __sb->gptr(), __n);
  341. __sb->__safe_gbump(__n);
  342. __result += __n;
  343. __c = __sb->underflow();
  344. }
  345. else
  346. {
  347. *__result++ = traits_type::to_char_type(__c);
  348. __c = __sb->snextc();
  349. }
  350. }
  351. }
  352. return __result;
  353. }
  354. template<typename _CharT, typename _Size>
  355. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  356. _CharT*>::__type
  357. __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result,
  358. bool __strict __attribute__((__unused__)))
  359. {
  360. if (__n == 0)
  361. return __result;
  362. __glibcxx_requires_cond(__it._M_sbuf,
  363. _M_message(__gnu_debug::__msg_inc_istreambuf)
  364. ._M_iterator(__it));
  365. _CharT* __beg = __result;
  366. __result += __it._M_sbuf->sgetn(__beg, __n);
  367. __glibcxx_requires_cond(!__strict || __result - __beg == __n,
  368. _M_message(__gnu_debug::__msg_inc_istreambuf)
  369. ._M_iterator(__it));
  370. return __result;
  371. }
  372. template<typename _CharT>
  373. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  374. istreambuf_iterator<_CharT> >::__type
  375. find(istreambuf_iterator<_CharT> __first,
  376. istreambuf_iterator<_CharT> __last, const _CharT& __val)
  377. {
  378. typedef istreambuf_iterator<_CharT> __is_iterator_type;
  379. typedef typename __is_iterator_type::traits_type traits_type;
  380. typedef typename __is_iterator_type::streambuf_type streambuf_type;
  381. typedef typename traits_type::int_type int_type;
  382. const int_type __eof = traits_type::eof();
  383. if (__first._M_sbuf && !__last._M_sbuf)
  384. {
  385. const int_type __ival = traits_type::to_int_type(__val);
  386. streambuf_type* __sb = __first._M_sbuf;
  387. int_type __c = __sb->sgetc();
  388. while (!traits_type::eq_int_type(__c, __eof)
  389. && !traits_type::eq_int_type(__c, __ival))
  390. {
  391. streamsize __n = __sb->egptr() - __sb->gptr();
  392. if (__n > 1)
  393. {
  394. const _CharT* __p = traits_type::find(__sb->gptr(),
  395. __n, __val);
  396. if (__p)
  397. __n = __p - __sb->gptr();
  398. __sb->__safe_gbump(__n);
  399. __c = __sb->sgetc();
  400. }
  401. else
  402. __c = __sb->snextc();
  403. }
  404. __first._M_c = __eof;
  405. }
  406. return __first;
  407. }
  408. template<typename _CharT, typename _Distance>
  409. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  410. void>::__type
  411. advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
  412. {
  413. if (__n == 0)
  414. return;
  415. __glibcxx_assert(__n > 0);
  416. __glibcxx_requires_cond(!__i._M_at_eof(),
  417. _M_message(__gnu_debug::__msg_inc_istreambuf)
  418. ._M_iterator(__i));
  419. typedef istreambuf_iterator<_CharT> __is_iterator_type;
  420. typedef typename __is_iterator_type::traits_type traits_type;
  421. typedef typename __is_iterator_type::streambuf_type streambuf_type;
  422. typedef typename traits_type::int_type int_type;
  423. const int_type __eof = traits_type::eof();
  424. streambuf_type* __sb = __i._M_sbuf;
  425. while (__n > 0)
  426. {
  427. streamsize __size = __sb->egptr() - __sb->gptr();
  428. if (__size > __n)
  429. {
  430. __sb->__safe_gbump(__n);
  431. break;
  432. }
  433. __sb->__safe_gbump(__size);
  434. __n -= __size;
  435. if (traits_type::eq_int_type(__sb->underflow(), __eof))
  436. {
  437. __glibcxx_requires_cond(__n == 0,
  438. _M_message(__gnu_debug::__msg_inc_istreambuf)
  439. ._M_iterator(__i));
  440. break;
  441. }
  442. }
  443. __i._M_c = __eof;
  444. }
  445. /// @} group iterators
  446. _GLIBCXX_END_NAMESPACE_VERSION
  447. } // namespace
  448. #endif