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