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_FORMATTER_INTEGRAL_H
11 #define _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
12 
13 #include <__concepts/arithmetic.h>
14 #include <__concepts/same_as.h>
15 #include <__config>
16 #include <__format/format_error.h>
17 #include <__format/formatter.h> // for __char_type TODO FMT Move the concept?
18 #include <__format/formatter_output.h>
19 #include <__format/parser_std_format_spec.h>
20 #include <__utility/unreachable.h>
21 #include <charconv>
22 #include <limits>
23 #include <string>
24 
25 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
26 #  include <locale>
27 #endif
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #  pragma GCC system_header
31 #endif
32 
33 _LIBCPP_PUSH_MACROS
34 #include <__undef_macros>
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 #if _LIBCPP_STD_VER > 17
39 
40 namespace __formatter {
41 
42 //
43 // Generic
44 //
45 
46 _LIBCPP_HIDE_FROM_ABI inline char* __insert_sign(char* __buf, bool __negative, __format_spec::__sign __sign) {
47   if (__negative)
48     *__buf++ = '-';
49   else
50     switch (__sign) {
51     case __format_spec::__sign::__default:
52     case __format_spec::__sign::__minus:
53       // No sign added.
54       break;
55     case __format_spec::__sign::__plus:
56       *__buf++ = '+';
57       break;
58     case __format_spec::__sign::__space:
59       *__buf++ = ' ';
60       break;
61     }
62 
63   return __buf;
64 }
65 
66 /**
67  * Determines the required grouping based on the size of the input.
68  *
69  * The grouping's last element will be repeated. For simplicity this repeating
70  * is unwrapped based on the length of the input. (When the input is short some
71  * groups are not processed.)
72  *
73  * @returns The size of the groups to write. This means the number of
74  * separator characters written is size() - 1.
75  *
76  * @note Since zero-sized groups cause issues they are silently ignored.
77  *
78  * @note The grouping field of the locale is always a @c std::string,
79  * regardless whether the @c std::numpunct's type is @c char or @c wchar_t.
80  */
81 _LIBCPP_HIDE_FROM_ABI inline string __determine_grouping(ptrdiff_t __size, const string& __grouping) {
82   _LIBCPP_ASSERT(!__grouping.empty() && __size > __grouping[0],
83                  "The slow grouping formatting is used while there will be no "
84                  "separators written");
85   string __r;
86   auto __end = __grouping.end() - 1;
87   auto __ptr = __grouping.begin();
88 
89   while (true) {
90     __size -= *__ptr;
91     if (__size > 0)
92       __r.push_back(*__ptr);
93     else {
94       // __size <= 0 so the value pushed will be <= *__ptr.
95       __r.push_back(*__ptr + __size);
96       return __r;
97     }
98 
99     // Proceed to the next group.
100     if (__ptr != __end) {
101       do {
102         ++__ptr;
103         // Skip grouping with a width of 0.
104       } while (*__ptr == 0 && __ptr != __end);
105     }
106   }
107 
108   __libcpp_unreachable();
109 }
110 
111 //
112 // Char
113 //
114 
115 template <__formatter::__char_type _CharT>
116 _LIBCPP_HIDE_FROM_ABI auto __format_char(
117     integral auto __value,
118     output_iterator<const _CharT&> auto __out_it,
119     __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
120   using _Tp = decltype(__value);
121   if constexpr (!same_as<_CharT, _Tp>) {
122     // cmp_less and cmp_greater can't be used for character types.
123     if constexpr (signed_integral<_CharT> == signed_integral<_Tp>) {
124       if (__value < numeric_limits<_CharT>::min() || __value > numeric_limits<_CharT>::max())
125         std::__throw_format_error("Integral value outside the range of the char type");
126     } else if constexpr (signed_integral<_CharT>) {
127       // _CharT is signed _Tp is unsigned
128       if (__value > static_cast<make_unsigned_t<_CharT>>(numeric_limits<_CharT>::max()))
129         std::__throw_format_error("Integral value outside the range of the char type");
130     } else {
131       // _CharT is unsigned _Tp is signed
132       if (__value < 0 || static_cast<make_unsigned_t<_Tp>>(__value) > numeric_limits<_CharT>::max())
133         std::__throw_format_error("Integral value outside the range of the char type");
134     }
135   }
136 
137   const auto __c = static_cast<_CharT>(__value);
138   return __formatter::__write(_VSTD::addressof(__c), _VSTD::addressof(__c) + 1, _VSTD::move(__out_it), __specs);
139 }
140 
141 //
142 // Integer
143 //
144 
145 /** Wrapper around @ref to_chars, returning the output pointer. */
146 template <integral _Tp>
147 _LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, int __base) {
148   // TODO FMT Evaluate code overhead due to not calling the internal function
149   // directly. (Should be zero overhead.)
150   to_chars_result __r = _VSTD::to_chars(__first, __last, __value, __base);
151   _LIBCPP_ASSERT(__r.ec == errc(0), "Internal buffer too small");
152   return __r.ptr;
153 }
154 
155 /**
156  * Helper to determine the buffer size to output a integer in Base @em x.
157  *
158  * There are several overloads for the supported bases. The function uses the
159  * base as template argument so it can be used in a constant expression.
160  */
161 template <unsigned_integral _Tp, size_t _Base>
162 consteval size_t __buffer_size() noexcept
163   requires(_Base == 2)
164 {
165   return numeric_limits<_Tp>::digits // The number of binary digits.
166        + 2                           // Reserve space for the '0[Bb]' prefix.
167        + 1;                          // Reserve space for the sign.
168 }
169 
170 template <unsigned_integral _Tp, size_t _Base>
171 consteval size_t __buffer_size() noexcept
172   requires(_Base == 8)
173 {
174   return numeric_limits<_Tp>::digits // The number of binary digits.
175            / 3                       // Adjust to octal.
176        + 1                           // Turn floor to ceil.
177        + 1                           // Reserve space for the '0' prefix.
178        + 1;                          // Reserve space for the sign.
179 }
180 
181 template <unsigned_integral _Tp, size_t _Base>
182 consteval size_t __buffer_size() noexcept
183   requires(_Base == 10)
184 {
185   return numeric_limits<_Tp>::digits10 // The floored value.
186        + 1                             // Turn floor to ceil.
187        + 1;                            // Reserve space for the sign.
188 }
189 
190 template <unsigned_integral _Tp, size_t _Base>
191 consteval size_t __buffer_size() noexcept
192   requires(_Base == 16)
193 {
194   return numeric_limits<_Tp>::digits // The number of binary digits.
195            / 4                       // Adjust to hexadecimal.
196        + 2                           // Reserve space for the '0[Xx]' prefix.
197        + 1;                          // Reserve space for the sign.
198 }
199 
200 template <unsigned_integral _Tp, class _CharT>
201 _LIBCPP_HIDE_FROM_ABI auto __format_integer(
202     _Tp __value,
203     auto& __ctx,
204     __format_spec::__parsed_specifications<_CharT> __specs,
205     bool __negative,
206     char* __begin,
207     char* __end,
208     const char* __prefix,
209     int __base) -> decltype(__ctx.out()) {
210   char* __first = __formatter::__insert_sign(__begin, __negative, __specs.__std_.__sign_);
211   if (__specs.__std_.__alternate_form_ && __prefix)
212     while (*__prefix)
213       *__first++ = *__prefix++;
214 
215   char* __last = __formatter::__to_buffer(__first, __end, __value, __base);
216 
217 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
218   if (__specs.__std_.__locale_specific_form_) {
219     const auto& __np  = use_facet<numpunct<_CharT>>(__ctx.locale());
220     string __grouping = __np.grouping();
221     ptrdiff_t __size  = __last - __first;
222     // Writing the grouped form has more overhead than the normal output
223     // routines. If there will be no separators written the locale-specific
224     // form is identical to the normal routine. Test whether to grouped form
225     // is required.
226     if (!__grouping.empty() && __size > __grouping[0])
227       return __formatter::__write_using_decimal_separators(
228           __ctx.out(),
229           __begin,
230           __first,
231           __last,
232           __formatter::__determine_grouping(__size, __grouping),
233           __np.thousands_sep(),
234           __specs);
235   }
236 #  endif
237   auto __out_it = __ctx.out();
238   if (__specs.__alignment_ != __format_spec::__alignment::__zero_padding)
239     __first = __begin;
240   else {
241     // __buf contains [sign][prefix]data
242     //                              ^ location of __first
243     // The zero padding is done like:
244     // - Write [sign][prefix]
245     // - Write data right aligned with '0' as fill character.
246     __out_it             = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
247     __specs.__alignment_ = __format_spec::__alignment::__right;
248     __specs.__fill_      = _CharT('0');
249     int32_t __size       = __first - __begin;
250 
251     __specs.__width_ -= _VSTD::min(__size, __specs.__width_);
252   }
253 
254   if (__specs.__std_.__type_ != __format_spec::__type::__hexadecimal_upper_case) [[likely]]
255     return __formatter::__write(__first, __last, __ctx.out(), __specs);
256 
257   return __formatter::__write_transformed(__first, __last, __ctx.out(), __specs, __formatter::__hex_to_upper);
258 }
259 
260 template <unsigned_integral _Tp, class _CharT>
261 _LIBCPP_HIDE_FROM_ABI auto __format_integer(
262     _Tp __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs, bool __negative = false)
263     -> decltype(__ctx.out()) {
264   switch (__specs.__std_.__type_) {
265   case __format_spec::__type::__binary_lower_case: {
266     array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
267     return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0b", 2);
268   }
269   case __format_spec::__type::__binary_upper_case: {
270     array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
271     return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0B", 2);
272   }
273   case __format_spec::__type::__octal: {
274     // Octal is special; if __value == 0 there's no prefix.
275     array<char, __formatter::__buffer_size<decltype(__value), 8>()> __array;
276     return __formatter::__format_integer(
277         __value, __ctx, __specs, __negative, __array.begin(), __array.end(), __value != 0 ? "0" : nullptr, 8);
278   }
279   case __format_spec::__type::__default:
280   case __format_spec::__type::__decimal: {
281     array<char, __formatter::__buffer_size<decltype(__value), 10>()> __array;
282     return __formatter::__format_integer(
283         __value, __ctx, __specs, __negative, __array.begin(), __array.end(), nullptr, 10);
284   }
285   case __format_spec::__type::__hexadecimal_lower_case: {
286     array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
287     return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0x", 16);
288   }
289   case __format_spec::__type::__hexadecimal_upper_case: {
290     array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
291     return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0X", 16);
292   }
293   default:
294     _LIBCPP_ASSERT(false, "The parse function should have validated the type");
295     __libcpp_unreachable();
296   }
297 }
298 
299 template <signed_integral _Tp, class _CharT>
300 _LIBCPP_HIDE_FROM_ABI auto
301 __format_integer(_Tp __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs)
302     -> decltype(__ctx.out()) {
303   // Depending on the std-format-spec string the sign and the value
304   // might not be outputted together:
305   // - alternate form may insert a prefix string.
306   // - zero-padding may insert additional '0' characters.
307   // Therefore the value is processed as a positive unsigned value.
308   // The function @ref __insert_sign will a '-' when the value was negative.
309   auto __r        = std::__to_unsigned_like(__value);
310   bool __negative = __value < 0;
311   if (__negative)
312     __r = __complement(__r);
313 
314   return __formatter::__format_integer(__r, __ctx, __specs, __negative);
315 }
316 
317 //
318 // Formatter arithmetic (bool)
319 //
320 
321 template <class _CharT>
322 struct _LIBCPP_TEMPLATE_VIS __bool_strings;
323 
324 template <>
325 struct _LIBCPP_TEMPLATE_VIS __bool_strings<char> {
326   static constexpr string_view __true{"true"};
327   static constexpr string_view __false{"false"};
328 };
329 
330 #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
331 template <>
332 struct _LIBCPP_TEMPLATE_VIS __bool_strings<wchar_t> {
333   static constexpr wstring_view __true{L"true"};
334   static constexpr wstring_view __false{L"false"};
335 };
336 #  endif
337 
338 template <class _CharT>
339 _LIBCPP_HIDE_FROM_ABI auto
340 __format_bool(bool __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs)
341     -> decltype(__ctx.out()) {
342 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
343   if (__specs.__std_.__locale_specific_form_) {
344     const auto& __np           = use_facet<numpunct<_CharT>>(__ctx.locale());
345     basic_string<_CharT> __str = __value ? __np.truename() : __np.falsename();
346     return __formatter::__write_string_no_precision(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
347   }
348 #  endif
349   basic_string_view<_CharT> __str =
350       __value ? __formatter::__bool_strings<_CharT>::__true : __formatter::__bool_strings<_CharT>::__false;
351   return __formatter::__write(__str.begin(), __str.end(), __ctx.out(), __specs);
352 }
353 
354 } // namespace __formatter
355 
356 #endif //_LIBCPP_STD_VER > 17
357 
358 _LIBCPP_END_NAMESPACE_STD
359 
360 _LIBCPP_POP_MACROS
361 
362 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
363