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 <__availability>
14 #include <__concepts/arithmetic.h>
15 #include <__config>
16 #include <__format/format_error.h> // TODO FMT Remove after adding 128-bit support
17 #include <__format/format_fwd.h>
18 #include <__format/format_parse_context.h>
19 #include <__format/formatter.h>
20 #include <__format/formatter_integral.h>
21 #include <__format/formatter_output.h>
22 #include <__format/parser_std_format_spec.h>
23 #include <limits> // TODO FMT Remove after adding 128-bit support
24 #include <type_traits>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_PUSH_MACROS // TODO FMT Remove after adding 128-bit support
31 #include <__undef_macros>
32 
33     _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 #if _LIBCPP_STD_VER > 17
36 
37     template <__formatter::__char_type _CharT>
38     struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT __formatter_integer {
39 
40 public:
41   _LIBCPP_HIDE_FROM_ABI constexpr auto
42   parse(basic_format_parse_context<_CharT>& __parse_ctx) -> decltype(__parse_ctx.begin()) {
43     auto __result = __parser_.__parse(__parse_ctx, __format_spec::__fields_integral);
44     __format_spec::__process_parsed_integer(__parser_);
45     return __result;
46   }
47 
48   template <integral _Tp>
49   _LIBCPP_HIDE_FROM_ABI auto format(_Tp __value, auto& __ctx) const -> decltype(__ctx.out()) {
50     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
51 
52     if (__specs.__std_.__type_ == __format_spec::__type::__char)
53       return __formatter::__format_char(__value, __ctx.out(), __specs);
54 
55     using _Type = __make_32_64_or_128_bit_t<_Tp>;
56     static_assert(!is_same<_Type, void>::value, "unsupported integral type used in __formatter_integer::__format");
57 
58     // Reduce the number of instantiation of the integer formatter
59     return __formatter::__format_integer(static_cast<_Type>(__value), __ctx, __specs);
60   }
61 
62   __format_spec::__parser<_CharT> __parser_;
63 };
64 
65 // Signed integral types.
66 template <__formatter::__char_type _CharT>
67 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<signed char, _CharT>
68     : public __formatter_integer<_CharT> {};
69 template <__formatter::__char_type _CharT>
70 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<short, _CharT> : public __formatter_integer<_CharT> {
71 };
72 template <__formatter::__char_type _CharT>
73 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<int, _CharT> : public __formatter_integer<_CharT> {};
74 template <__formatter::__char_type _CharT>
75 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long, _CharT> : public __formatter_integer<_CharT> {};
76 template <__formatter::__char_type _CharT>
77 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long long, _CharT>
78     : public __formatter_integer<_CharT> {};
79 #  ifndef _LIBCPP_HAS_NO_INT128
80 template <__formatter::__char_type _CharT>
81 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__int128_t, _CharT>
82     : public __formatter_integer<_CharT> {
83   using _Base = __formatter_integer<_CharT>;
84 
85   _LIBCPP_HIDE_FROM_ABI auto format(__int128_t __value, auto& __ctx) const -> decltype(__ctx.out()) {
86     // TODO FMT Implement full 128 bit support.
87     using _To = long long;
88     if (__value < numeric_limits<_To>::min() || __value > numeric_limits<_To>::max())
89       std::__throw_format_error("128-bit value is outside of implemented range");
90 
91     return _Base::format(static_cast<_To>(__value), __ctx);
92   }
93 };
94 #  endif
95 
96 // Unsigned integral types.
97 template <__formatter::__char_type _CharT>
98 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned char, _CharT>
99     : public __formatter_integer<_CharT> {};
100 template <__formatter::__char_type _CharT>
101 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned short, _CharT>
102     : public __formatter_integer<_CharT> {};
103 template <__formatter::__char_type _CharT>
104 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned, _CharT>
105     : public __formatter_integer<_CharT> {};
106 template <__formatter::__char_type _CharT>
107 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned long, _CharT>
108     : public __formatter_integer<_CharT> {};
109 template <__formatter::__char_type _CharT>
110 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned long long, _CharT>
111     : public __formatter_integer<_CharT> {};
112 #  ifndef _LIBCPP_HAS_NO_INT128
113 template <__formatter::__char_type _CharT>
114 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__uint128_t, _CharT>
115     : public __formatter_integer<_CharT> {
116   using _Base = __formatter_integer<_CharT>;
117 
118   _LIBCPP_HIDE_FROM_ABI auto format(__uint128_t __value, auto& __ctx) const -> decltype(__ctx.out()) {
119     // TODO FMT Implement full 128 bit support.
120     using _To = unsigned long long;
121     if (__value < numeric_limits<_To>::min() || __value > numeric_limits<_To>::max())
122       std::__throw_format_error("128-bit value is outside of implemented range");
123 
124     return _Base::format(static_cast<_To>(__value), __ctx);
125   }
126 };
127 #  endif
128 
129 #endif //_LIBCPP_STD_VER > 17
130 
131 _LIBCPP_END_NAMESPACE_STD
132 
133 _LIBCPP_POP_MACROS
134 
135 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H
136