File indexing completed on 2026-05-03 08:13:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___FORMAT_FORMATTER_INTEGER_H
0011 #define _LIBCPP___FORMAT_FORMATTER_INTEGER_H
0012
0013 #include <__concepts/arithmetic.h>
0014 #include <__config>
0015 #include <__format/concepts.h>
0016 #include <__format/format_parse_context.h>
0017 #include <__format/formatter.h>
0018 #include <__format/formatter_integral.h>
0019 #include <__format/formatter_output.h>
0020 #include <__format/parser_std_format_spec.h>
0021 #include <__type_traits/is_void.h>
0022 #include <__type_traits/make_32_64_or_128_bit.h>
0023
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 # pragma GCC system_header
0026 #endif
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 #if _LIBCPP_STD_VER >= 20
0031
0032 template <__fmt_char_type _CharT>
0033 struct _LIBCPP_TEMPLATE_VIS __formatter_integer {
0034 public:
0035 template <class _ParseContext>
0036 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
0037 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral);
0038 __format_spec::__process_parsed_integer(__parser_, "an integer");
0039 return __result;
0040 }
0041
0042 template <integral _Tp, class _FormatContext>
0043 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const {
0044 __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
0045
0046 if (__specs.__std_.__type_ == __format_spec::__type::__char)
0047 return __formatter::__format_char(__value, __ctx.out(), __specs);
0048
0049 using _Type = __make_32_64_or_128_bit_t<_Tp>;
0050 static_assert(!is_void<_Type>::value, "unsupported integral type used in __formatter_integer::__format");
0051
0052
0053 return __formatter::__format_integer(static_cast<_Type>(__value), __ctx, __specs);
0054 }
0055
0056 __format_spec::__parser<_CharT> __parser_;
0057 };
0058
0059
0060 template <__fmt_char_type _CharT>
0061 struct _LIBCPP_TEMPLATE_VIS formatter<signed char, _CharT> : public __formatter_integer<_CharT> {};
0062 template <__fmt_char_type _CharT>
0063 struct _LIBCPP_TEMPLATE_VIS formatter<short, _CharT> : public __formatter_integer<_CharT> {};
0064 template <__fmt_char_type _CharT>
0065 struct _LIBCPP_TEMPLATE_VIS formatter<int, _CharT> : public __formatter_integer<_CharT> {};
0066 template <__fmt_char_type _CharT>
0067 struct _LIBCPP_TEMPLATE_VIS formatter<long, _CharT> : public __formatter_integer<_CharT> {};
0068 template <__fmt_char_type _CharT>
0069 struct _LIBCPP_TEMPLATE_VIS formatter<long long, _CharT> : public __formatter_integer<_CharT> {};
0070 # if _LIBCPP_HAS_INT128
0071 template <__fmt_char_type _CharT>
0072 struct _LIBCPP_TEMPLATE_VIS formatter<__int128_t, _CharT> : public __formatter_integer<_CharT> {};
0073 # endif
0074
0075
0076 template <__fmt_char_type _CharT>
0077 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned char, _CharT> : public __formatter_integer<_CharT> {};
0078 template <__fmt_char_type _CharT>
0079 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned short, _CharT> : public __formatter_integer<_CharT> {};
0080 template <__fmt_char_type _CharT>
0081 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned, _CharT> : public __formatter_integer<_CharT> {};
0082 template <__fmt_char_type _CharT>
0083 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long, _CharT> : public __formatter_integer<_CharT> {};
0084 template <__fmt_char_type _CharT>
0085 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long long, _CharT> : public __formatter_integer<_CharT> {};
0086 # if _LIBCPP_HAS_INT128
0087 template <__fmt_char_type _CharT>
0088 struct _LIBCPP_TEMPLATE_VIS formatter<__uint128_t, _CharT> : public __formatter_integer<_CharT> {};
0089 # endif
0090
0091 # if _LIBCPP_STD_VER >= 23
0092 template <>
0093 inline constexpr bool enable_nonlocking_formatter_optimization<signed char> = true;
0094 template <>
0095 inline constexpr bool enable_nonlocking_formatter_optimization<short> = true;
0096 template <>
0097 inline constexpr bool enable_nonlocking_formatter_optimization<int> = true;
0098 template <>
0099 inline constexpr bool enable_nonlocking_formatter_optimization<long> = true;
0100 template <>
0101 inline constexpr bool enable_nonlocking_formatter_optimization<long long> = true;
0102 # if _LIBCPP_HAS_INT128
0103 template <>
0104 inline constexpr bool enable_nonlocking_formatter_optimization<__int128_t> = true;
0105 # endif
0106
0107 template <>
0108 inline constexpr bool enable_nonlocking_formatter_optimization<unsigned char> = true;
0109 template <>
0110 inline constexpr bool enable_nonlocking_formatter_optimization<unsigned short> = true;
0111 template <>
0112 inline constexpr bool enable_nonlocking_formatter_optimization<unsigned> = true;
0113 template <>
0114 inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long> = true;
0115 template <>
0116 inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long long> = true;
0117 # if _LIBCPP_HAS_INT128
0118 template <>
0119 inline constexpr bool enable_nonlocking_formatter_optimization<__uint128_t> = true;
0120 # endif
0121 # endif
0122 #endif
0123
0124 _LIBCPP_END_NAMESPACE_STD
0125
0126 #endif