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 
105   static constexpr size_t __buffer_size = 256 / sizeof(_CharT);
106 
107 private:
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 #ifndef _LIBCPP_ENABLE_DEBUG_MODE
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)
227     requires(same_as<_Storage, __internal_storage<_CharT>>)
228   : __output_(__storage_.begin(), __storage_.__buffer_size, this), __writer_(_VSTD::move(__out_it)) {}
229 
230   _LIBCPP_HIDE_FROM_ABI explicit __format_buffer(_OutIt __out_it) requires(
231       same_as<_Storage, __direct_storage<_CharT>>)
232       : __output_(_VSTD::__unwrap_iter(__out_it), size_t(-1), this),
233         __writer_(_VSTD::move(__out_it)) {}
234 
235   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() {
236     return __output_.make_output_iterator();
237   }
238 
239   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
240     __writer_.flush(__ptr, __size);
241   }
242 
243   _LIBCPP_HIDE_FROM_ABI _OutIt out() && {
244     __output_.flush();
245     return _VSTD::move(__writer_).out();
246   }
247 
248 private:
249   _LIBCPP_NO_UNIQUE_ADDRESS _Storage __storage_;
250   __output_buffer<_CharT> __output_;
251   typename __writer_selector<_OutIt, _CharT>::type __writer_;
252 };
253 
254 /// A buffer that counts the number of insertions.
255 ///
256 /// Since \ref formatted_size only needs to know the size, the output itself is
257 /// discarded.
258 template <__formatter::__char_type _CharT>
259 class _LIBCPP_TEMPLATE_VIS __formatted_size_buffer {
260 public:
261   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return __output_.make_output_iterator(); }
262 
263   _LIBCPP_HIDE_FROM_ABI void flush(const _CharT*, size_t __size) { __size_ += __size; }
264 
265   _LIBCPP_HIDE_FROM_ABI size_t result() && {
266     __output_.flush();
267     return __size_;
268   }
269 
270 private:
271   __internal_storage<_CharT> __storage_;
272   __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.__buffer_size, this};
273   size_t __size_{0};
274 };
275 
276 /// The base of a buffer that counts and limits the number of insertions.
277 template <class _OutIt, __formatter::__char_type _CharT, bool>
278   requires(output_iterator<_OutIt, const _CharT&>)
279 struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base {
280   using _Size = iter_difference_t<_OutIt>;
281 
282 public:
283   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __n)
284       : __writer_(_VSTD::move(__out_it)), __n_(_VSTD::max(_Size(0), __n)) {}
285 
286   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
287     if (_Size(__size_) <= __n_)
288       __writer_.flush(__ptr, _VSTD::min(_Size(__size), __n_ - __size_));
289     __size_ += __size;
290   }
291 
292 protected:
293   __internal_storage<_CharT> __storage_;
294   __output_buffer<_CharT> __output_{__storage_.begin(), __storage_.__buffer_size, this};
295   typename __writer_selector<_OutIt, _CharT>::type __writer_;
296 
297   _Size __n_;
298   _Size __size_{0};
299 };
300 
301 /// The base of a buffer that counts and limits the number of insertions.
302 ///
303 /// This version is used when \c __enable_direct_output<_OutIt, _CharT> == true.
304 ///
305 /// This class limits the size available the the direct writer so it will not
306 /// exceed the maximum number of code units.
307 template <class _OutIt, __formatter::__char_type _CharT>
308   requires(output_iterator<_OutIt, const _CharT&>)
309 class _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base<_OutIt, _CharT, true> {
310   using _Size = iter_difference_t<_OutIt>;
311 
312 public:
313   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer_base(_OutIt __out_it, _Size __n)
314       : __output_(_VSTD::__unwrap_iter(__out_it), __n, this), __writer_(_VSTD::move(__out_it)) {
315     if (__n <= 0) [[unlikely]]
316       __output_.reset(__storage_.begin(), __storage_.__buffer_size);
317   }
318 
319   _LIBCPP_HIDE_FROM_ABI void flush(_CharT* __ptr, size_t __size) {
320     // A flush to the direct writer happens in two occasions:
321     // - The format function has written the maximum number of allowed code
322     //   units. At this point it's no longer valid to write to this writer. So
323     //   switch to the internal storage. This internal storage doesn't need to
324     //   be written anywhere so the flush for that storage writes no output.
325     // - The format_to_n function is finished. In this case there's no need to
326     //   switch the buffer, but for simplicity the buffers are still switched.
327     // When the __n <= 0 the constructor already switched the buffers.
328     if (__size_ == 0 && __ptr != __storage_.begin()) {
329       __writer_.flush(__ptr, __size);
330       __output_.reset(__storage_.begin(), __storage_.__buffer_size);
331     }
332 
333     __size_ += __size;
334   }
335 
336 protected:
337   __internal_storage<_CharT> __storage_;
338   __output_buffer<_CharT> __output_;
339   __writer_direct<_OutIt, _CharT> __writer_;
340 
341   _Size __size_{0};
342 };
343 
344 /// The buffer that counts and limits the number of insertions.
345 template <class _OutIt, __formatter::__char_type _CharT>
346   requires(output_iterator<_OutIt, const _CharT&>)
347 struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer final
348     : public __format_to_n_buffer_base< _OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>> {
349   using _Base = __format_to_n_buffer_base<_OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>>;
350   using _Size = iter_difference_t<_OutIt>;
351 
352 public:
353   _LIBCPP_HIDE_FROM_ABI explicit __format_to_n_buffer(_OutIt __out_it, _Size __n) : _Base(_VSTD::move(__out_it), __n) {}
354   _LIBCPP_HIDE_FROM_ABI auto make_output_iterator() { return this->__output_.make_output_iterator(); }
355 
356   _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> result() && {
357     this->__output_.flush();
358     return {_VSTD::move(this->__writer_).out(), this->__size_};
359   }
360 };
361 } // namespace __format
362 
363 #endif //_LIBCPP_STD_VER > 17
364 
365 _LIBCPP_END_NAMESPACE_STD
366 
367 _LIBCPP_POP_MACROS
368 
369 #endif // _LIBCPP___FORMAT_BUFFER_H
370