buffer.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FORMAT_BUFFER_H
  10. #define _LIBCPP___FORMAT_BUFFER_H
  11. #include <__algorithm/copy_n.h>
  12. #include <__algorithm/fill_n.h>
  13. #include <__algorithm/max.h>
  14. #include <__algorithm/min.h>
  15. #include <__algorithm/ranges_copy_n.h>
  16. #include <__algorithm/transform.h>
  17. #include <__algorithm/unwrap_iter.h>
  18. #include <__concepts/same_as.h>
  19. #include <__config>
  20. #include <__format/concepts.h>
  21. #include <__format/enable_insertable.h>
  22. #include <__format/format_to_n_result.h>
  23. #include <__iterator/back_insert_iterator.h>
  24. #include <__iterator/concepts.h>
  25. #include <__iterator/incrementable_traits.h>
  26. #include <__iterator/iterator_traits.h>
  27. #include <__iterator/wrap_iter.h>
  28. #include <__utility/move.h>
  29. #include <cstddef>
  30. #include <string_view>
  31. #include <type_traits>
  32. #include <vector>
  33. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  34. # pragma GCC system_header
  35. #endif
  36. _LIBCPP_PUSH_MACROS
  37. #include <__undef_macros>
  38. _LIBCPP_BEGIN_NAMESPACE_STD
  39. #if _LIBCPP_STD_VER > 17
  40. namespace __format {
  41. /// A "buffer" that handles writing to the proper iterator.
  42. ///
  43. /// This helper is used together with the @ref back_insert_iterator to offer
  44. /// type-erasure for the formatting functions. This reduces the number to
  45. /// template instantiations.
  46. template <__fmt_char_type _CharT>
  47. class _LIBCPP_TEMPLATE_VIS __output_buffer {
  48. public:
  49. using value_type = _CharT;
  50. template <class _Tp>
  51. _LIBCPP_HIDE_FROM_ABI explicit __output_buffer(_CharT* __ptr, size_t __capacity, _Tp* __obj)
  52. : __ptr_(__ptr),
  53. __capacity_(__capacity),
  54. __flush_([](_CharT* __p, size_t __n, void* __o) { static_cast<_Tp*>(__o)->__flush(__p, __n); }),
  55. __obj_(__obj) {}
  56. _LIBCPP_HIDE_FROM_ABI void __reset(_CharT* __ptr, size_t __capacity) {
  57. __ptr_ = __ptr;
  58. __capacity_ = __capacity;
  59. }
  60. _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return std::back_insert_iterator{*this}; }
  61. // Used in std::back_insert_iterator.
  62. _LIBCPP_HIDE_FROM_ABI void push_back(_CharT __c) {
  63. __ptr_[__size_++] = __c;
  64. // Profiling showed flushing after adding is more efficient than flushing
  65. // when entering the function.
  66. if (__size_ == __capacity_)
  67. __flush();
  68. }
  69. /// Copies the input __str to the buffer.
  70. ///
  71. /// Since some of the input is generated by std::to_chars, there needs to be a
  72. /// conversion when _CharT is wchar_t.
  73. template <__fmt_char_type _InCharT>
  74. _LIBCPP_HIDE_FROM_ABI void __copy(basic_string_view<_InCharT> __str) {
  75. // When the underlying iterator is a simple iterator the __capacity_ is
  76. // infinite. For a string or container back_inserter it isn't. This means
  77. // adding a large string the the buffer can cause some overhead. In that
  78. // case a better approach could be:
  79. // - flush the buffer
  80. // - container.append(__str.begin(), __str.end());
  81. // The same holds true for the fill.
  82. // For transform it might be slightly harder, however the use case for
  83. // transform is slightly less common; it converts hexadecimal values to
  84. // upper case. For integral these strings are short.
  85. // TODO FMT Look at the improvements above.
  86. size_t __n = __str.size();
  87. __flush_on_overflow(__n);
  88. if (__n <= __capacity_) {
  89. _VSTD::copy_n(__str.data(), __n, _VSTD::addressof(__ptr_[__size_]));
  90. __size_ += __n;
  91. return;
  92. }
  93. // The output doesn't fit in the internal buffer.
  94. // Copy the data in "__capacity_" sized chunks.
  95. _LIBCPP_ASSERT(__size_ == 0, "the buffer should be flushed by __flush_on_overflow");
  96. const _InCharT* __first = __str.data();
  97. do {
  98. size_t __chunk = _VSTD::min(__n, __capacity_);
  99. _VSTD::copy_n(__first, __chunk, _VSTD::addressof(__ptr_[__size_]));
  100. __size_ = __chunk;
  101. __first += __chunk;
  102. __n -= __chunk;
  103. __flush();
  104. } while (__n);
  105. }
  106. /// A std::transform wrapper.
  107. ///
  108. /// Like @ref __copy it may need to do type conversion.
  109. template <__fmt_char_type _InCharT, class _UnaryOperation>
  110. _LIBCPP_HIDE_FROM_ABI void __transform(const _InCharT* __first, const _InCharT* __last, _UnaryOperation __operation) {
  111. _LIBCPP_ASSERT(__first <= __last, "not a valid range");
  112. size_t __n = static_cast<size_t>(__last - __first);
  113. __flush_on_overflow(__n);
  114. if (__n <= __capacity_) {
  115. _VSTD::transform(__first, __last, _VSTD::addressof(__ptr_[__size_]), _VSTD::move(__operation));
  116. __size_ += __n;
  117. return;
  118. }
  119. // The output doesn't fit in the internal buffer.
  120. // Transform the data in "__capacity_" sized chunks.
  121. _LIBCPP_ASSERT(__size_ == 0, "the buffer should be flushed by __flush_on_overflow");
  122. do {
  123. size_t __chunk = _VSTD::min(__n, __capacity_);
  124. _VSTD::transform(__first, __first + __chunk, _VSTD::addressof(__ptr_[__size_]), __operation);
  125. __size_ = __chunk;
  126. __first += __chunk;
  127. __n -= __chunk;
  128. __flush();
  129. } while (__n);
  130. }
  131. /// A \c fill_n wrapper.
  132. _LIBCPP_HIDE_FROM_ABI void __fill(size_t __n, _CharT __value) {
  133. __flush_on_overflow(__n);
  134. if (__n <= __capacity_) {
  135. _VSTD::fill_n(_VSTD::addressof(__ptr_[__size_]), __n, __value);
  136. __size_ += __n;
  137. return;
  138. }
  139. // The output doesn't fit in the internal buffer.
  140. // Fill the buffer in "__capacity_" sized chunks.
  141. _LIBCPP_ASSERT(__size_ == 0, "the buffer should be flushed by __flush_on_overflow");
  142. do {
  143. size_t __chunk = _VSTD::min(__n, __capacity_);
  144. _VSTD::fill_n(_VSTD::addressof(__ptr_[__size_]), __chunk, __value);
  145. __size_ = __chunk;
  146. __n -= __chunk;
  147. __flush();
  148. } while (__n);
  149. }
  150. _LIBCPP_HIDE_FROM_ABI void __flush() {
  151. __flush_(__ptr_, __size_, __obj_);
  152. __size_ = 0;
  153. }
  154. private:
  155. _CharT* __ptr_;
  156. size_t __capacity_;
  157. size_t __size_{0};
  158. void (*__flush_)(_CharT*, size_t, void*);
  159. void* __obj_;
  160. /// Flushes the buffer when the output operation would overflow the buffer.
  161. ///
  162. /// A simple approach for the overflow detection would be something along the
  163. /// lines:
  164. /// \code
  165. /// // The internal buffer is large enough.
  166. /// if (__n <= __capacity_) {
  167. /// // Flush when we really would overflow.
  168. /// if (__size_ + __n >= __capacity_)
  169. /// __flush();
  170. /// ...
  171. /// }
  172. /// \endcode
  173. ///
  174. /// This approach works for all cases but one:
  175. /// A __format_to_n_buffer_base where \ref __enable_direct_output is true.
  176. /// In that case the \ref __capacity_ of the buffer changes during the first
  177. /// \ref __flush. During that operation the output buffer switches from its
  178. /// __writer_ to its __storage_. The \ref __capacity_ of the former depends
  179. /// on the value of n, of the latter is a fixed size. For example:
  180. /// - a format_to_n call with a 10'000 char buffer,
  181. /// - the buffer is filled with 9'500 chars,
  182. /// - adding 1'000 elements would overflow the buffer so the buffer gets
  183. /// changed and the \ref __capacity_ decreases from 10'000 to
  184. /// __buffer_size (256 at the time of writing).
  185. ///
  186. /// This means that the \ref __flush for this class may need to copy a part of
  187. /// the internal buffer to the proper output. In this example there will be
  188. /// 500 characters that need this copy operation.
  189. ///
  190. /// Note it would be more efficient to write 500 chars directly and then swap
  191. /// the buffers. This would make the code more complex and \ref format_to_n is
  192. /// not the most common use case. Therefore the optimization isn't done.
  193. _LIBCPP_HIDE_FROM_ABI void __flush_on_overflow(size_t __n) {
  194. if (__size_ + __n >= __capacity_)
  195. __flush();
  196. }
  197. };
  198. /// A storage using an internal buffer.
  199. ///
  200. /// This storage is used when writing a single element to the output iterator
  201. /// is expensive.
  202. template <__fmt_char_type _CharT>
  203. class _LIBCPP_TEMPLATE_VIS __internal_storage {
  204. public:
  205. _LIBCPP_HIDE_FROM_ABI _CharT* __begin() { return __buffer_; }
  206. static constexpr size_t __buffer_size = 256 / sizeof(_CharT);
  207. private:
  208. _CharT __buffer_[__buffer_size];
  209. };
  210. /// A storage writing directly to the storage.
  211. ///
  212. /// This requires the storage to be a contiguous buffer of \a _CharT.
  213. /// Since the output is directly written to the underlying storage this class
  214. /// is just an empty class.
  215. template <__fmt_char_type _CharT>
  216. class _LIBCPP_TEMPLATE_VIS __direct_storage {};
  217. template <class _OutIt, class _CharT>
  218. concept __enable_direct_output = __fmt_char_type<_CharT> &&
  219. (same_as<_OutIt, _CharT*>
  220. #ifndef _LIBCPP_ENABLE_DEBUG_MODE
  221. || same_as<_OutIt, __wrap_iter<_CharT*>>
  222. #endif
  223. );
  224. /// Write policy for directly writing to the underlying output.
  225. template <class _OutIt, __fmt_char_type _CharT>
  226. class _LIBCPP_TEMPLATE_VIS __writer_direct {
  227. public:
  228. _LIBCPP_HIDE_FROM_ABI explicit __writer_direct(_OutIt __out_it)
  229. : __out_it_(__out_it) {}
  230. _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() { return __out_it_; }
  231. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT*, size_t __n) {
  232. // _OutIt can be a __wrap_iter<CharT*>. Therefore the original iterator
  233. // is adjusted.
  234. __out_it_ += __n;
  235. }
  236. private:
  237. _OutIt __out_it_;
  238. };
  239. /// Write policy for copying the buffer to the output.
  240. template <class _OutIt, __fmt_char_type _CharT>
  241. class _LIBCPP_TEMPLATE_VIS __writer_iterator {
  242. public:
  243. _LIBCPP_HIDE_FROM_ABI explicit __writer_iterator(_OutIt __out_it)
  244. : __out_it_{_VSTD::move(__out_it)} {}
  245. _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() && { return std::move(__out_it_); }
  246. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
  247. __out_it_ = std::ranges::copy_n(__ptr, __n, std::move(__out_it_)).out;
  248. }
  249. private:
  250. _OutIt __out_it_;
  251. };
  252. /// Concept to see whether a \a _Container is insertable.
  253. ///
  254. /// The concept is used to validate whether multiple calls to a
  255. /// \ref back_insert_iterator can be replace by a call to \c _Container::insert.
  256. ///
  257. /// \note a \a _Container needs to opt-in to the concept by specializing
  258. /// \ref __enable_insertable.
  259. template <class _Container>
  260. concept __insertable =
  261. __enable_insertable<_Container> && __fmt_char_type<typename _Container::value_type> &&
  262. requires(_Container& __t, add_pointer_t<typename _Container::value_type> __first,
  263. add_pointer_t<typename _Container::value_type> __last) { __t.insert(__t.end(), __first, __last); };
  264. /// Extract the container type of a \ref back_insert_iterator.
  265. template <class _It>
  266. struct _LIBCPP_TEMPLATE_VIS __back_insert_iterator_container {
  267. using type = void;
  268. };
  269. template <__insertable _Container>
  270. struct _LIBCPP_TEMPLATE_VIS __back_insert_iterator_container<back_insert_iterator<_Container>> {
  271. using type = _Container;
  272. };
  273. /// Write policy for inserting the buffer in a container.
  274. template <class _Container>
  275. class _LIBCPP_TEMPLATE_VIS __writer_container {
  276. public:
  277. using _CharT = typename _Container::value_type;
  278. _LIBCPP_HIDE_FROM_ABI explicit __writer_container(back_insert_iterator<_Container> __out_it)
  279. : __container_{__out_it.__get_container()} {}
  280. _LIBCPP_HIDE_FROM_ABI auto __out_it() { return std::back_inserter(*__container_); }
  281. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
  282. __container_->insert(__container_->end(), __ptr, __ptr + __n);
  283. }
  284. private:
  285. _Container* __container_;
  286. };
  287. /// Selects the type of the writer used for the output iterator.
  288. template <class _OutIt, class _CharT>
  289. class _LIBCPP_TEMPLATE_VIS __writer_selector {
  290. using _Container = typename __back_insert_iterator_container<_OutIt>::type;
  291. public:
  292. using type = conditional_t<!same_as<_Container, void>, __writer_container<_Container>,
  293. conditional_t<__enable_direct_output<_OutIt, _CharT>, __writer_direct<_OutIt, _CharT>,
  294. __writer_iterator<_OutIt, _CharT>>>;
  295. };
  296. /// The generic formatting buffer.
  297. template <class _OutIt, __fmt_char_type _CharT>
  298. requires(output_iterator<_OutIt, const _CharT&>) class _LIBCPP_TEMPLATE_VIS
  299. __format_buffer {
  300. using _Storage =
  301. conditional_t<__enable_direct_output<_OutIt, _CharT>,
  302. __direct_storage<_CharT>, __internal_storage<_CharT>>;
  303. public:
  304. _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it)
  305. requires(same_as<_Storage, __internal_storage<_CharT>>)
  306. : __output_(__storage_.__begin(), __storage_.__buffer_size, this), __writer_(_VSTD::move(__out_it)) {}
  307. _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it) requires(
  308. same_as<_Storage, __direct_storage<_CharT>>)
  309. : __output_(_VSTD::__unwrap_iter(__out_it), size_t(-1), this),
  310. __writer_(_VSTD::move(__out_it)) {}
  311. _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return __output_.__make_output_iterator(); }
  312. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) { __writer_.__flush(__ptr, __n); }
  313. _LIBCPP_HIDE_FROM_ABI _OutIt __out_it() && {
  314. __output_.__flush();
  315. return _VSTD::move(__writer_).__out_it();
  316. }
  317. private:
  318. _LIBCPP_NO_UNIQUE_ADDRESS _Storage __storage_;
  319. __output_buffer<_CharT> __output_;
  320. typename __writer_selector<_OutIt, _CharT>::type __writer_;
  321. };
  322. /// A buffer that counts the number of insertions.
  323. ///
  324. /// Since \ref formatted_size only needs to know the size, the output itself is
  325. /// discarded.
  326. template <__fmt_char_type _CharT>
  327. class _LIBCPP_TEMPLATE_VIS __formatted_size_buffer {
  328. public:
  329. _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return __output_.__make_output_iterator(); }
  330. _LIBCPP_HIDE_FROM_ABI void __flush(const _CharT*, size_t __n) { __size_ += __n; }
  331. _LIBCPP_HIDE_FROM_ABI size_t __result() && {
  332. __output_.__flush();
  333. return __size_;
  334. }
  335. private:
  336. __internal_storage<_CharT> __storage_;
  337. __output_buffer<_CharT> __output_{__storage_.__begin(), __storage_.__buffer_size, this};
  338. size_t __size_{0};
  339. };
  340. /// The base of a buffer that counts and limits the number of insertions.
  341. template <class _OutIt, __fmt_char_type _CharT, bool>
  342. requires(output_iterator<_OutIt, const _CharT&>)
  343. struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base {
  344. using _Size = iter_difference_t<_OutIt>;
  345. public:
  346. _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __max_size)
  347. : __writer_(_VSTD::move(__out_it)), __max_size_(_VSTD::max(_Size(0), __max_size)) {}
  348. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
  349. if (_Size(__size_) <= __max_size_)
  350. __writer_.__flush(__ptr, _VSTD::min(_Size(__n), __max_size_ - __size_));
  351. __size_ += __n;
  352. }
  353. protected:
  354. __internal_storage<_CharT> __storage_;
  355. __output_buffer<_CharT> __output_{__storage_.__begin(), __storage_.__buffer_size, this};
  356. typename __writer_selector<_OutIt, _CharT>::type __writer_;
  357. _Size __max_size_;
  358. _Size __size_{0};
  359. };
  360. /// The base of a buffer that counts and limits the number of insertions.
  361. ///
  362. /// This version is used when \c __enable_direct_output<_OutIt, _CharT> == true.
  363. ///
  364. /// This class limits the size available to the direct writer so it will not
  365. /// exceed the maximum number of code units.
  366. template <class _OutIt, __fmt_char_type _CharT>
  367. requires(output_iterator<_OutIt, const _CharT&>)
  368. class _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base<_OutIt, _CharT, true> {
  369. using _Size = iter_difference_t<_OutIt>;
  370. public:
  371. _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __max_size)
  372. : __output_(_VSTD::__unwrap_iter(__out_it), __max_size, this),
  373. __writer_(_VSTD::move(__out_it)),
  374. __max_size_(__max_size) {
  375. if (__max_size <= 0) [[unlikely]]
  376. __output_.__reset(__storage_.__begin(), __storage_.__buffer_size);
  377. }
  378. _LIBCPP_HIDE_FROM_ABI void __flush(_CharT* __ptr, size_t __n) {
  379. // A __flush to the direct writer happens in the following occasions:
  380. // - The format function has written the maximum number of allowed code
  381. // units. At this point it's no longer valid to write to this writer. So
  382. // switch to the internal storage. This internal storage doesn't need to
  383. // be written anywhere so the __flush for that storage writes no output.
  384. // - Like above, but the next "mass write" operation would overflow the
  385. // buffer. In that case the buffer is pre-emptively switched. The still
  386. // valid code units will be written separately.
  387. // - The format_to_n function is finished. In this case there's no need to
  388. // switch the buffer, but for simplicity the buffers are still switched.
  389. // When the __max_size <= 0 the constructor already switched the buffers.
  390. if (__size_ == 0 && __ptr != __storage_.__begin()) {
  391. __writer_.__flush(__ptr, __n);
  392. __output_.__reset(__storage_.__begin(), __storage_.__buffer_size);
  393. } else if (__size_ < __max_size_) {
  394. // Copies a part of the internal buffer to the output up to n characters.
  395. // See __output_buffer<_CharT>::__flush_on_overflow for more information.
  396. _Size __s = _VSTD::min(_Size(__n), __max_size_ - __size_);
  397. std::copy_n(__ptr, __s, __writer_.__out_it());
  398. __writer_.__flush(__ptr, __s);
  399. }
  400. __size_ += __n;
  401. }
  402. protected:
  403. __internal_storage<_CharT> __storage_;
  404. __output_buffer<_CharT> __output_;
  405. __writer_direct<_OutIt, _CharT> __writer_;
  406. _Size __max_size_;
  407. _Size __size_{0};
  408. };
  409. /// The buffer that counts and limits the number of insertions.
  410. template <class _OutIt, __fmt_char_type _CharT>
  411. requires(output_iterator<_OutIt, const _CharT&>)
  412. struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer final
  413. : public __format_to_n_buffer_base< _OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>> {
  414. using _Base = __format_to_n_buffer_base<_OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>>;
  415. using _Size = iter_difference_t<_OutIt>;
  416. public:
  417. _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer(_OutIt __out_it, _Size __max_size)
  418. : _Base(_VSTD::move(__out_it), __max_size) {}
  419. _LIBCPP_HIDE_FROM_ABI auto __make_output_iterator() { return this->__output_.__make_output_iterator(); }
  420. _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __result() && {
  421. this->__output_.__flush();
  422. return {_VSTD::move(this->__writer_).__out_it(), this->__size_};
  423. }
  424. };
  425. // A dynamically growing buffer intended to be used for retargeting a context.
  426. //
  427. // P2286 Formatting ranges adds range formatting support. It allows the user to
  428. // specify the minimum width for the entire formatted range. The width of the
  429. // range is not known until the range is formatted. Formatting is done to an
  430. // output_iterator so there's no guarantee it would be possible to add the fill
  431. // to the front of the output. Instead the range is formatted to a temporary
  432. // buffer and that buffer is formatted as a string.
  433. //
  434. // There is an issue with that approach, the format context used in
  435. // std::formatter<T>::format contains the output iterator used as part of its
  436. // type. So using this output iterator means there needs to be a new format
  437. // context and the format arguments need to be retargeted to the new context.
  438. // This retargeting is done by a basic_format_context specialized for the
  439. // __iterator of this container.
  440. template <__fmt_char_type _CharT>
  441. class _LIBCPP_TEMPLATE_VIS __retarget_buffer {
  442. public:
  443. using value_type = _CharT;
  444. struct __iterator {
  445. using difference_type = ptrdiff_t;
  446. _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(__retarget_buffer& __buffer)
  447. : __buffer_(std::addressof(__buffer)) {}
  448. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator=(const _CharT& __c) {
  449. __buffer_->push_back(__c);
  450. return *this;
  451. }
  452. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator=(_CharT&& __c) {
  453. __buffer_->push_back(__c);
  454. return *this;
  455. }
  456. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator*() { return *this; }
  457. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() { return *this; }
  458. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) { return *this; }
  459. __retarget_buffer* __buffer_;
  460. };
  461. _LIBCPP_HIDE_FROM_ABI explicit __retarget_buffer(size_t __size_hint) { __buffer_.reserve(__size_hint); }
  462. _LIBCPP_HIDE_FROM_ABI __iterator __make_output_iterator() { return __iterator{*this}; }
  463. _LIBCPP_HIDE_FROM_ABI void push_back(_CharT __c) { __buffer_.push_back(__c); }
  464. template <__fmt_char_type _InCharT>
  465. _LIBCPP_HIDE_FROM_ABI void __copy(basic_string_view<_InCharT> __str) {
  466. __buffer_.insert(__buffer_.end(), __str.begin(), __str.end());
  467. }
  468. template <__fmt_char_type _InCharT, class _UnaryOperation>
  469. _LIBCPP_HIDE_FROM_ABI void __transform(const _InCharT* __first, const _InCharT* __last, _UnaryOperation __operation) {
  470. _LIBCPP_ASSERT(__first <= __last, "not a valid range");
  471. std::transform(__first, __last, std::back_inserter(__buffer_), std::move(__operation));
  472. }
  473. _LIBCPP_HIDE_FROM_ABI void __fill(size_t __n, _CharT __value) { __buffer_.insert(__buffer_.end(), __n, __value); }
  474. _LIBCPP_HIDE_FROM_ABI basic_string_view<_CharT> __view() { return {__buffer_.data(), __buffer_.size()}; }
  475. private:
  476. // Use vector instead of string to avoid adding zeros after every append
  477. // operation. The buffer is exposed as a string_view and not as a c-string.
  478. vector<_CharT> __buffer_;
  479. };
  480. } // namespace __format
  481. #endif //_LIBCPP_STD_VER > 17
  482. _LIBCPP_END_NAMESPACE_STD
  483. _LIBCPP_POP_MACROS
  484. #endif // _LIBCPP___FORMAT_BUFFER_H