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_fwd.h>
17 #include <__format/format_parse_context.h>
18 #include <__format/formatter.h>
19 #include <__format/formatter_integral.h>
20 #include <__format/formatter_output.h>
21 #include <__format/parser_std_format_spec.h>
22 #include <__type_traits/make_32_64_or_128_bit.h>
23 #include <type_traits>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29     _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #if _LIBCPP_STD_VER > 17
32 
33     template <__formatter::__char_type _CharT>
34     struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT __formatter_integer {
35 
36 public:
37   _LIBCPP_HIDE_FROM_ABI constexpr auto
38   parse(basic_format_parse_context<_CharT>& __parse_ctx) -> decltype(__parse_ctx.begin()) {
39     auto __result = __parser_.__parse(__parse_ctx, __format_spec::__fields_integral);
40     __format_spec::__process_parsed_integer(__parser_);
41     return __result;
42   }
43 
44   template <integral _Tp>
45   _LIBCPP_HIDE_FROM_ABI auto format(_Tp __value, auto& __ctx) const -> decltype(__ctx.out()) {
46     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
47 
48     if (__specs.__std_.__type_ == __format_spec::__type::__char)
49       return __formatter::__format_char(__value, __ctx.out(), __specs);
50 
51     using _Type = __make_32_64_or_128_bit_t<_Tp>;
52     static_assert(!is_same<_Type, void>::value, "unsupported integral type used in __formatter_integer::__format");
53 
54     // Reduce the number of instantiation of the integer formatter
55     return __formatter::__format_integer(static_cast<_Type>(__value), __ctx, __specs);
56   }
57 
58   __format_spec::__parser<_CharT> __parser_;
59 };
60 
61 // Signed integral types.
62 template <__formatter::__char_type _CharT>
63 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<signed char, _CharT>
64     : public __formatter_integer<_CharT> {};
65 template <__formatter::__char_type _CharT>
66 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<short, _CharT> : public __formatter_integer<_CharT> {
67 };
68 template <__formatter::__char_type _CharT>
69 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<int, _CharT> : public __formatter_integer<_CharT> {};
70 template <__formatter::__char_type _CharT>
71 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long, _CharT> : public __formatter_integer<_CharT> {};
72 template <__formatter::__char_type _CharT>
73 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long long, _CharT>
74     : public __formatter_integer<_CharT> {};
75 #  ifndef _LIBCPP_HAS_NO_INT128
76 template <__formatter::__char_type _CharT>
77 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__int128_t, _CharT>
78     : public __formatter_integer<_CharT> {};
79 #  endif
80 
81 // Unsigned integral types.
82 template <__formatter::__char_type _CharT>
83 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned char, _CharT>
84     : public __formatter_integer<_CharT> {};
85 template <__formatter::__char_type _CharT>
86 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned short, _CharT>
87     : public __formatter_integer<_CharT> {};
88 template <__formatter::__char_type _CharT>
89 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned, _CharT>
90     : public __formatter_integer<_CharT> {};
91 template <__formatter::__char_type _CharT>
92 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned long, _CharT>
93     : public __formatter_integer<_CharT> {};
94 template <__formatter::__char_type _CharT>
95 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned long long, _CharT>
96     : public __formatter_integer<_CharT> {};
97 #  ifndef _LIBCPP_HAS_NO_INT128
98 template <__formatter::__char_type _CharT>
99 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__uint128_t, _CharT>
100     : public __formatter_integer<_CharT> {};
101 #  endif
102 
103 #endif //_LIBCPP_STD_VER > 17
104 
105 _LIBCPP_END_NAMESPACE_STD
106 
107 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H
108