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