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 
10 #ifndef _LIBCPP___FORMAT_BUFFER_H
11 #define _LIBCPP___FORMAT_BUFFER_H
12 
13 #include <__algorithm/copy_n.h>
14 #include <__algorithm/max.h>
15 #include <__algorithm/min.h>
16 #include <__algorithm/unwrap_iter.h>
17 #include <__config>
18 #include <__format/enable_insertable.h>
19 #include <__format/format_to_n_result.h>
20 #include <__format/formatter.h> // for __char_type TODO FMT Move the concept?
21 #include <__iterator/back_insert_iterator.h>
22 #include <__iterator/concepts.h>
23 #include <__iterator/incrementable_traits.h>
24 #include <__iterator/iterator_traits.h>
25 #include <__iterator/wrap_iter.h>
26 #include <__utility/move.h>
27 #include <concepts>
28 #include <cstddef>
29 #include <type_traits>
30 
31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32 #  pragma GCC system_header
33 #endif
34 
35 _LIBCPP_PUSH_MACROS
36 #include <__undef_macros>
37 
38 _LIBCPP_BEGIN_NAMESPACE_STD
39 
40 #if _LIBCPP_STD_VER > 17
41 
42 namespace __format {
43 
44 /// A "buffer" that handles writing to the proper iterator.
45 ///
46 /// This helper is used together with the @ref back_insert_iterator to offer
47 /// type-erasure for the formatting functions. This reduces the number to
48 /// template instantiations.
49 template <__formatter::__char_type _CharT>
50 class _LIBCPP_TEMPLATE_VIS __output_buffer {
51 public:
52   using value_type = _CharT;
53 
54   template <class _Tp>
55   _LIBCPP_HIDE_FROM_ABI explicit __output_buffer(_CharT* __ptr,
56                                                  size_t __capacity, _Tp* __obj)
57       : __ptr_(__ptr), __capacity_(__capacity),
58         __flush_([](_CharT* __p, size_t __size, void* __o) {
59           static_cast<_Tp*>(__o)->flush(__p, __size);
60         }),
61         __obj_(__obj) {}
62 
63   _LIBCPP_HIDE_FROM_ABI void reset(_CharT* __ptr, size_t __capacity) {
64     __ptr_ = __ptr;
65     __capacity_ = __capacity;
66   }
67 
68   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() {
69     return back_insert_iterator{*this};
70   }
71 
72   // TODO FMT It would be nice to have an overload taking a
73   // basic_string_view<_CharT> and append it directly.
74   _LIBCPP_HIDE_FROM_ABI void push_back(_CharT __c) {
75     __ptr_[__size_++] = __c;
76 
77     // Profiling showed flushing after adding is more efficient than flushing
78     // when entering the function.
79     if (__size_ == __capacity_)
80       flush();
81   }
82 
83   _LIBCPP_HIDE_FROM_ABI void flush() {
84     __flush_(__ptr_, __size_, __obj_);
85     __size_ = 0;
86   }
87 
88 private:
89   _CharT* __ptr_;
90   size_t __capacity_;
91   size_t __size_{0};
92   void (*__flush_)(_CharT*, size_t, void*);
93   void* __obj_;
94 };
95 
96 /// A storage using an internal buffer.
97 ///
98 /// This storage is used when writing a single element to the output iterator
99 /// is expensive.
100 template <__formatter::__char_type _CharT>
101 class _LIBCPP_TEMPLATE_VIS __internal_storage {
102 public:
103   _LIBCPP_HIDE_FROM_ABI _CharT* begin() { return __buffer_; }
104   _LIBCPP_HIDE_FROM_ABI size_t capacity() { return __buffer_size_; }
105 
106 private:
107   static constexpr size_t __buffer_size_ = 256 / sizeof(_CharT);
108   _CharT __buffer_[__buffer_size_];
109 };
110 
111 /// A storage writing directly to the storage.
112 ///
113 /// This requires the storage to be a contiguous buffer of \a _CharT.
114 /// Since the output is directly written to the underlying storage this class
115 /// is just an empty class.
116 template <__formatter::__char_type _CharT>
117 class _LIBCPP_TEMPLATE_VIS __direct_storage {};
118 
119 template <class _OutIt, class _CharT>
120 concept __enable_direct_output = __formatter::__char_type<_CharT> &&
121     (same_as<_OutIt, _CharT*>
122 #if _LIBCPP_DEBUG_LEVEL < 2
123      || same_as<_OutIt, __wrap_iter<_CharT*>>
124 #endif
125     );
126 
127 /// Write policy for directly writing to the underlying output.
128 template <class _OutIt, __formatter::__char_type _CharT>
129 class _LIBCPP_TEMPLATE_VIS __writer_direct {
130 public:
131   _LIBCPP_HIDE_FROM_ABI explicit __writer_direct(_OutIt __out_it)
132       : __out_it_(__out_it) {}
133 
134   _LIBCPP_HIDE_FROM_ABI auto out() { return __out_it_; }
135 
136   _LIBCPP_HIDE_FROM_ABI void flush(_CharT*, size_t __size) {
137     // _OutIt can be a __wrap_iter<CharT*>. Therefore the original iterator
138     // is adjusted.
139     __out_it_ += __size;
140   }
141 
142 private:
143   _OutIt __out_it_;
144 };
145 
146 /// Write policy for copying the buffer to the output.
147 template <class _OutIt, __formatter::__char_type _CharT>
148 class _LIBCPP_TEMPLATE_VIS __writer_iterator {
149 public:
150   _LIBCPP_HIDE_FROM_ABI explicit __writer_iterator(_OutIt __out_it)
151       : __out_it_{_VSTD::move(__out_it)} {}
152 
153   _LIBCPP_HIDE_FROM_ABI auto out() { return __out_it_; }
154 
155   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
156     __out_it_ = _VSTD::copy_n(__ptr, __size, _VSTD::move(__out_it_));
157   }
158 
159 private:
160   _OutIt __out_it_;
161 };
162 
163 /// Concept to see whether a \a _Container is insertable.
164 ///
165 /// The concept is used to validate whether multiple calls to a
166 /// \ref back_insert_iterator can be replace by a call to \c _Container::insert.
167 ///
168 /// \note a \a _Container needs to opt-in to the concept by specializing
169 /// \ref __enable_insertable.
170 template <class _Container>
171 concept __insertable =
172     __enable_insertable<_Container> && __formatter::__char_type<typename _Container::value_type> &&
173     requires(_Container& __t, add_pointer_t<typename _Container::value_type> __first,
174              add_pointer_t<typename _Container::value_type> __last) { __t.insert(__t.end(), __first, __last); };
175 
176 /// Extract the container type of a \ref back_insert_iterator.
177 template <class _It>
178 struct _LIBCPP_TEMPLATE_VIS __back_insert_iterator_container {
179   using type = void;
180 };
181 
182 template <__insertable _Container>
183 struct _LIBCPP_TEMPLATE_VIS __back_insert_iterator_container<back_insert_iterator<_Container>> {
184   using type = _Container;
185 };
186 
187 /// Write policy for inserting the buffer in a container.
188 template <class _Container>
189 class _LIBCPP_TEMPLATE_VIS __writer_container {
190 public:
191   using _CharT = typename _Container::value_type;
192 
193   _LIBCPP_HIDE_FROM_ABI explicit __writer_container(back_insert_iterator<_Container> __out_it)
194       : __container_{__out_it.__get_container()} {}
195 
196   _LIBCPP_HIDE_FROM_ABI auto out() { return back_inserter(*__container_); }
197 
198   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
199     __container_->insert(__container_->end(), __ptr, __ptr + __size);
200   }
201 
202 private:
203   _Container* __container_;
204 };
205 
206 /// Selects the type of the writer used for the output iterator.
207 template <class _OutIt, class _CharT>
208 class _LIBCPP_TEMPLATE_VIS __writer_selector {
209   using _Container = typename __back_insert_iterator_container<_OutIt>::type;
210 
211 public:
212   using type = conditional_t<!same_as<_Container, void>, __writer_container<_Container>,
213                              conditional_t<__enable_direct_output<_OutIt, _CharT>, __writer_direct<_OutIt, _CharT>,
214                                            __writer_iterator<_OutIt, _CharT>>>;
215 };
216 
217 /// The generic formatting buffer.
218 template <class _OutIt, __formatter::__char_type _CharT>
219 requires(output_iterator<_OutIt, const _CharT&>) class _LIBCPP_TEMPLATE_VIS
220     __format_buffer {
221   using _Storage =
222       conditional_t<__enable_direct_output<_OutIt, _CharT>,
223                     __direct_storage<_CharT>, __internal_storage<_CharT>>;
224 
225 public:
226   _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it) requires(
227       same_as<_Storage, __internal_storage<_CharT>>)
228       : __output_(__storage_.begin(), __storage_.capacity(), this),
229         __writer_(_VSTD::move(__out_it)) {}
230 
231   _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it) requires(
232       same_as<_Storage, __direct_storage<_CharT>>)
233       : __output_(_VSTD::__unwrap_iter(__out_it), size_t(-1), this),
234         __writer_(_VSTD::move(__out_it)) {}
235 
236   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() {
237     return __output_.make_output_iterator();
238   }
239 
240   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
241     __writer_.flush(__ptr, __size);
242   }
243 
244   _LIBCPP_HIDE_FROM_ABI _OutIt out() && {
245     __output_.flush();
246     return _VSTD::move(__writer_).out();
247   }
248 
249 private:
250   _LIBCPP_NO_UNIQUE_ADDRESS _Storage __storage_;
251   __output_buffer<_CharT> __output_;
252   typename __writer_selector<_OutIt, _CharT>::type __writer_;
253 };
254 
255 /// A buffer that counts the number of insertions.
256 ///
257 /// Since \ref formatted_size only needs to know the size, the output itself is
258 /// discarded.
259 template <__formatter::__char_type _CharT>
260 class _LIBCPP_TEMPLATE_VIS __formatted_size_buffer {
261 public:
262   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return __output_.make_output_iterator(); }
263 
264   _LIBCPP_HIDE_FROM_ABI void flush(const _CharT*, size_t __size) { __size_ += __size; }
265 
266   _LIBCPP_HIDE_FROM_ABI size_t result() && {
267     __output_.flush();
268     return __size_;
269   }
270 
271 private:
272   __internal_storage<_CharT> __storage_;
273   __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.capacity(), this};
274   size_t __size_{0};
275 };
276 
277 /// The base of a buffer that counts and limits the number of insertions.
278 template <class _OutIt, __formatter::__char_type _CharT, bool>
279   requires(output_iterator<_OutIt, const _CharT&>)
280 struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base {
281   using _Size = iter_difference_t<_OutIt>;
282 
283 public:
284   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __n)
285       : __writer_(_VSTD::move(__out_it)), __n_(_VSTD::max(_Size(0), __n)) {}
286 
287   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
288     if (_Size(__size_) <= __n_)
289       __writer_.flush(__ptr, _VSTD::min(_Size(__size), __n_ - __size_));
290     __size_ += __size;
291   }
292 
293 protected:
294   __internal_storage<_CharT> __storage_;
295   __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.capacity(), this};
296   typename __writer_selector<_OutIt, _CharT>::type __writer_;
297 
298   _Size __n_;
299   _Size __size_{0};
300 };
301 
302 /// The base of a buffer that counts and limits the number of insertions.
303 ///
304 /// This version is used when \c __enable_direct_output<_OutIt, _CharT> == true.
305 ///
306 /// This class limits the size available the the direct writer so it will not
307 /// exceed the maximum number of code units.
308 template <class _OutIt, __formatter::__char_type _CharT>
309   requires(output_iterator<_OutIt, const _CharT&>)
310 class _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base<_OutIt, _CharT, true> {
311   using _Size = iter_difference_t<_OutIt>;
312 
313 public:
314   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __n)
315       : __output_(_VSTD::__unwrap_iter(__out_it), __n, this), __writer_(_VSTD::move(__out_it)) {
316     if (__n <= 0) [[unlikely]]
317       __output_.reset(__storage_.begin(), __storage_.capacity());
318   }
319 
320   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
321     // A flush to the direct writer happens in two occasions:
322     // - The format function has written the maximum number of allowed code
323     //   units. At this point it's no longer valid to write to this writer. So
324     //   switch to the internal storage. This internal storage doesn't need to
325     //   be written anywhere so the flush for that storage writes no output.
326     // - The format_to_n function is finished. In this case there's no need to
327     //   switch the buffer, but for simplicity the buffers are still switched.
328     // When the __n <= 0 the constructor already switched the buffers.
329     if (__size_ == 0 && __ptr != __storage_.begin()) {
330       __writer_.flush(__ptr, __size);
331       __output_.reset(__storage_.begin(), __storage_.capacity());
332     }
333 
334     __size_ += __size;
335   }
336 
337 protected:
338   __internal_storage<_CharT> __storage_;
339   __output_buffer<_CharT> __output_;
340   __writer_direct<_OutIt, _CharT> __writer_;
341 
342   _Size __size_{0};
343 };
344 
345 /// The buffer that counts and limits the number of insertions.
346 template <class _OutIt, __formatter::__char_type _CharT>
347   requires(output_iterator<_OutIt, const _CharT&>)
348 struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer final
349     : public __format_to_n_buffer_base< _OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>> {
350   using _Base = __format_to_n_buffer_base<_OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>>;
351   using _Size = iter_difference_t<_OutIt>;
352 
353 public:
354   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer(_OutIt __out_it, _Size __n) : _Base(_VSTD::move(__out_it), __n) {}
355   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return this->__output_.make_output_iterator(); }
356 
357   _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> result() && {
358     this->__output_.flush();
359     return {_VSTD::move(this->__writer_).out(), this->__size_};
360   }
361 };
362 } // namespace __format
363 
364 #endif //_LIBCPP_STD_VER > 17
365 
366 _LIBCPP_END_NAMESPACE_STD
367 
368 _LIBCPP_POP_MACROS
369 
370 #endif // _LIBCPP___FORMAT_BUFFER_H
371