Warning: This file is not a C or C++ file. It does not have highlighting.
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_INTEGER_H |
11 | #define _LIBCPP___FORMAT_FORMATTER_INTEGER_H |
12 | |
13 | #include <__concepts/arithmetic.h> |
14 | #include <__config> |
15 | #include <__format/concepts.h> |
16 | #include <__format/format_parse_context.h> |
17 | #include <__format/formatter.h> |
18 | #include <__format/formatter_integral.h> |
19 | #include <__format/formatter_output.h> |
20 | #include <__format/parser_std_format_spec.h> |
21 | #include <__type_traits/is_void.h> |
22 | #include <__type_traits/make_32_64_or_128_bit.h> |
23 | |
24 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
25 | # pragma GCC system_header |
26 | #endif |
27 | |
28 | _LIBCPP_BEGIN_NAMESPACE_STD |
29 | |
30 | #if _LIBCPP_STD_VER >= 20 |
31 | |
32 | template <__fmt_char_type _CharT> |
33 | struct __formatter_integer { |
34 | public: |
35 | template <class _ParseContext> |
36 | _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { |
37 | typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral); |
38 | __format_spec::__process_parsed_integer(__parser_, "an integer"); |
39 | return __result; |
40 | } |
41 | |
42 | template <integral _Tp, class _FormatContext> |
43 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const { |
44 | __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx); |
45 | |
46 | if (__specs.__std_.__type_ == __format_spec::__type::__char) |
47 | return __formatter::__format_char(__value, __ctx.out(), __specs); |
48 | |
49 | using _Type = __make_32_64_or_128_bit_t<_Tp>; |
50 | static_assert(!is_void<_Type>::value, "unsupported integral type used in __formatter_integer::__format"); |
51 | |
52 | // Reduce the number of instantiation of the integer formatter |
53 | return __formatter::__format_integer(static_cast<_Type>(__value), __ctx, __specs); |
54 | } |
55 | |
56 | __format_spec::__parser<_CharT> __parser_; |
57 | }; |
58 | |
59 | // Signed integral types. |
60 | template <__fmt_char_type _CharT> |
61 | struct formatter<signed char, _CharT> : public __formatter_integer<_CharT> {}; |
62 | template <__fmt_char_type _CharT> |
63 | struct formatter<short, _CharT> : public __formatter_integer<_CharT> {}; |
64 | template <__fmt_char_type _CharT> |
65 | struct formatter<int, _CharT> : public __formatter_integer<_CharT> {}; |
66 | template <__fmt_char_type _CharT> |
67 | struct formatter<long, _CharT> : public __formatter_integer<_CharT> {}; |
68 | template <__fmt_char_type _CharT> |
69 | struct formatter<long long, _CharT> : public __formatter_integer<_CharT> {}; |
70 | # if _LIBCPP_HAS_INT128 |
71 | template <__fmt_char_type _CharT> |
72 | struct formatter<__int128_t, _CharT> : public __formatter_integer<_CharT> {}; |
73 | # endif |
74 | |
75 | // Unsigned integral types. |
76 | template <__fmt_char_type _CharT> |
77 | struct formatter<unsigned char, _CharT> : public __formatter_integer<_CharT> {}; |
78 | template <__fmt_char_type _CharT> |
79 | struct formatter<unsigned short, _CharT> : public __formatter_integer<_CharT> {}; |
80 | template <__fmt_char_type _CharT> |
81 | struct formatter<unsigned, _CharT> : public __formatter_integer<_CharT> {}; |
82 | template <__fmt_char_type _CharT> |
83 | struct formatter<unsigned long, _CharT> : public __formatter_integer<_CharT> {}; |
84 | template <__fmt_char_type _CharT> |
85 | struct formatter<unsigned long long, _CharT> : public __formatter_integer<_CharT> {}; |
86 | # if _LIBCPP_HAS_INT128 |
87 | template <__fmt_char_type _CharT> |
88 | struct formatter<__uint128_t, _CharT> : public __formatter_integer<_CharT> {}; |
89 | # endif |
90 | |
91 | # if _LIBCPP_STD_VER >= 23 |
92 | template <> |
93 | inline constexpr bool enable_nonlocking_formatter_optimization<signed char> = true; |
94 | template <> |
95 | inline constexpr bool enable_nonlocking_formatter_optimization<short> = true; |
96 | template <> |
97 | inline constexpr bool enable_nonlocking_formatter_optimization<int> = true; |
98 | template <> |
99 | inline constexpr bool enable_nonlocking_formatter_optimization<long> = true; |
100 | template <> |
101 | inline constexpr bool enable_nonlocking_formatter_optimization<long long> = true; |
102 | # if _LIBCPP_HAS_INT128 |
103 | template <> |
104 | inline constexpr bool enable_nonlocking_formatter_optimization<__int128_t> = true; |
105 | # endif |
106 | |
107 | template <> |
108 | inline constexpr bool enable_nonlocking_formatter_optimization<unsigned char> = true; |
109 | template <> |
110 | inline constexpr bool enable_nonlocking_formatter_optimization<unsigned short> = true; |
111 | template <> |
112 | inline constexpr bool enable_nonlocking_formatter_optimization<unsigned> = true; |
113 | template <> |
114 | inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long> = true; |
115 | template <> |
116 | inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long long> = true; |
117 | # if _LIBCPP_HAS_INT128 |
118 | template <> |
119 | inline constexpr bool enable_nonlocking_formatter_optimization<__uint128_t> = true; |
120 | # endif |
121 | # endif // _LIBCPP_STD_VER >= 23 |
122 | #endif // _LIBCPP_STD_VER >= 20 |
123 | |
124 | _LIBCPP_END_NAMESPACE_STD |
125 | |
126 | #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H |
127 |
Warning: This file is not a C or C++ file. It does not have highlighting.