xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/formatter_integer.h (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
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/concepts.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/is_void.h>
23 #include <__type_traits/make_32_64_or_128_bit.h>
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 >= 20
32 
33 template <__fmt_char_type _CharT>
34 struct _LIBCPP_TEMPLATE_VIS __formatter_integer {
35 public:
36   template <class _ParseContext>
37   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
38     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral);
39     __format_spec::__process_parsed_integer(__parser_, "an integer");
40     return __result;
41   }
42 
43   template <integral _Tp, class _FormatContext>
44   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const {
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_void<_Type>::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 <__fmt_char_type _CharT>
62 struct _LIBCPP_TEMPLATE_VIS formatter<signed char, _CharT> : public __formatter_integer<_CharT> {};
63 template <__fmt_char_type _CharT>
64 struct _LIBCPP_TEMPLATE_VIS formatter<short, _CharT> : public __formatter_integer<_CharT> {};
65 template <__fmt_char_type _CharT>
66 struct _LIBCPP_TEMPLATE_VIS formatter<int, _CharT> : public __formatter_integer<_CharT> {};
67 template <__fmt_char_type _CharT>
68 struct _LIBCPP_TEMPLATE_VIS formatter<long, _CharT> : public __formatter_integer<_CharT> {};
69 template <__fmt_char_type _CharT>
70 struct _LIBCPP_TEMPLATE_VIS formatter<long long, _CharT> : public __formatter_integer<_CharT> {};
71 #  ifndef _LIBCPP_HAS_NO_INT128
72 template <__fmt_char_type _CharT>
73 struct _LIBCPP_TEMPLATE_VIS formatter<__int128_t, _CharT> : public __formatter_integer<_CharT> {};
74 #  endif
75 
76 // Unsigned integral types.
77 template <__fmt_char_type _CharT>
78 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned char, _CharT> : public __formatter_integer<_CharT> {};
79 template <__fmt_char_type _CharT>
80 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned short, _CharT> : public __formatter_integer<_CharT> {};
81 template <__fmt_char_type _CharT>
82 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned, _CharT> : public __formatter_integer<_CharT> {};
83 template <__fmt_char_type _CharT>
84 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long, _CharT> : public __formatter_integer<_CharT> {};
85 template <__fmt_char_type _CharT>
86 struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long long, _CharT> : public __formatter_integer<_CharT> {};
87 #  ifndef _LIBCPP_HAS_NO_INT128
88 template <__fmt_char_type _CharT>
89 struct _LIBCPP_TEMPLATE_VIS formatter<__uint128_t, _CharT> : public __formatter_integer<_CharT> {};
90 #  endif
91 
92 #endif //_LIBCPP_STD_VER >= 20
93 
94 _LIBCPP_END_NAMESPACE_STD
95 
96 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H
97