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