xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/formatter_integer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric // -*- C++ -*-
2349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
3349cc55cSDimitry Andric //
4349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7349cc55cSDimitry Andric //
8349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
9349cc55cSDimitry Andric 
10349cc55cSDimitry Andric #ifndef _LIBCPP___FORMAT_FORMATTER_INTEGER_H
11349cc55cSDimitry Andric #define _LIBCPP___FORMAT_FORMATTER_INTEGER_H
12349cc55cSDimitry Andric 
1381ad6265SDimitry Andric #include <__concepts/arithmetic.h>
14349cc55cSDimitry Andric #include <__config>
15bdd1243dSDimitry Andric #include <__format/concepts.h>
1681ad6265SDimitry Andric #include <__format/format_parse_context.h>
17349cc55cSDimitry Andric #include <__format/formatter.h>
18349cc55cSDimitry Andric #include <__format/formatter_integral.h>
1981ad6265SDimitry Andric #include <__format/formatter_output.h>
20349cc55cSDimitry Andric #include <__format/parser_std_format_spec.h>
2106c3fb27SDimitry Andric #include <__type_traits/is_void.h>
22972a253aSDimitry Andric #include <__type_traits/make_32_64_or_128_bit.h>
23349cc55cSDimitry Andric 
24349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25349cc55cSDimitry Andric #  pragma GCC system_header
26349cc55cSDimitry Andric #endif
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
29349cc55cSDimitry Andric 
3006c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
31349cc55cSDimitry Andric 
32bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
3306c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __formatter_integer {
34349cc55cSDimitry Andric public:
3506c3fb27SDimitry Andric   template <class _ParseContext>
3606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
3706c3fb27SDimitry Andric     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral);
3806c3fb27SDimitry Andric     __format_spec::__process_parsed_integer(__parser_, "an integer");
3981ad6265SDimitry Andric     return __result;
40349cc55cSDimitry Andric   }
4181ad6265SDimitry Andric 
4206c3fb27SDimitry Andric   template <integral _Tp, class _FormatContext>
4306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const {
4481ad6265SDimitry Andric     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
4581ad6265SDimitry Andric 
4681ad6265SDimitry Andric     if (__specs.__std_.__type_ == __format_spec::__type::__char)
4781ad6265SDimitry Andric       return __formatter::__format_char(__value, __ctx.out(), __specs);
4881ad6265SDimitry Andric 
4981ad6265SDimitry Andric     using _Type = __make_32_64_or_128_bit_t<_Tp>;
5006c3fb27SDimitry Andric     static_assert(!is_void<_Type>::value, "unsupported integral type used in __formatter_integer::__format");
5181ad6265SDimitry Andric 
5281ad6265SDimitry Andric     // Reduce the number of instantiation of the integer formatter
5381ad6265SDimitry Andric     return __formatter::__format_integer(static_cast<_Type>(__value), __ctx, __specs);
54349cc55cSDimitry Andric   }
5581ad6265SDimitry Andric 
5681ad6265SDimitry Andric   __format_spec::__parser<_CharT> __parser_;
57349cc55cSDimitry Andric };
58349cc55cSDimitry Andric 
59349cc55cSDimitry Andric // Signed integral types.
60bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
61*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<signed char, _CharT> : public __formatter_integer<_CharT> {};
62bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
63*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<short, _CharT> : public __formatter_integer<_CharT> {};
64bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
6506c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<int, _CharT> : public __formatter_integer<_CharT> {};
66bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
6706c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<long, _CharT> : public __formatter_integer<_CharT> {};
68bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
69*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<long long, _CharT> : public __formatter_integer<_CharT> {};
70349cc55cSDimitry Andric #  ifndef _LIBCPP_HAS_NO_INT128
71bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
72*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<__int128_t, _CharT> : public __formatter_integer<_CharT> {};
73349cc55cSDimitry Andric #  endif
74349cc55cSDimitry Andric 
75349cc55cSDimitry Andric // Unsigned integral types.
76bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
77*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<unsigned char, _CharT> : public __formatter_integer<_CharT> {};
78bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
79*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<unsigned short, _CharT> : public __formatter_integer<_CharT> {};
80bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
81*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<unsigned, _CharT> : public __formatter_integer<_CharT> {};
82bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
83*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long, _CharT> : public __formatter_integer<_CharT> {};
84bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
85*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<unsigned long long, _CharT> : public __formatter_integer<_CharT> {};
86349cc55cSDimitry Andric #  ifndef _LIBCPP_HAS_NO_INT128
87bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
88*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<__uint128_t, _CharT> : public __formatter_integer<_CharT> {};
89349cc55cSDimitry Andric #  endif
90349cc55cSDimitry Andric 
9106c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 20
92349cc55cSDimitry Andric 
93349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD
94349cc55cSDimitry Andric 
95349cc55cSDimitry Andric #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H
96