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 <__config> 15 #include <__format/format_error.h> 16 #include <__format/format_fwd.h> 17 #include <__format/formatter.h> 18 #include <__format/formatter_integral.h> 19 #include <__format/parser_std_format_spec.h> 20 #include <limits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_PUSH_MACROS 27 #include <__undef_macros> 28 29 _LIBCPP_BEGIN_NAMESPACE_STD 30 31 #if _LIBCPP_STD_VER > 17 32 33 namespace __format_spec { 34 35 template <class _CharT> 36 class _LIBCPP_TEMPLATE_VIS __parser_integer : public __parser_integral<_CharT> { 37 public: 38 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx) 39 -> decltype(__parse_ctx.begin()) { 40 auto __it = __parser_integral<_CharT>::__parse(__parse_ctx); 41 42 switch (this->__type) { 43 case _Flags::_Type::__default: 44 this->__type = _Flags::_Type::__decimal; 45 [[fallthrough]]; 46 47 case _Flags::_Type::__binary_lower_case: 48 case _Flags::_Type::__binary_upper_case: 49 case _Flags::_Type::__octal: 50 case _Flags::_Type::__decimal: 51 case _Flags::_Type::__hexadecimal_lower_case: 52 case _Flags::_Type::__hexadecimal_upper_case: 53 this->__handle_integer(); 54 break; 55 56 case _Flags::_Type::__char: 57 this->__handle_char(); 58 break; 59 60 default: 61 __throw_format_error("The format-spec type has a type not supported for " 62 "an integer argument"); 63 } 64 return __it; 65 } 66 }; 67 68 template <class _CharT> 69 using __formatter_integer = __formatter_integral<__parser_integer<_CharT>>; 70 71 } // namespace __format_spec 72 73 // [format.formatter.spec]/2.3 74 // For each charT, for each cv-unqualified arithmetic type ArithmeticT other 75 // than char, wchar_t, char8_t, char16_t, or char32_t, a specialization 76 77 // Signed integral types. 78 template <__formatter::__char_type _CharT> 79 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 80 formatter<signed char, _CharT> 81 : public __format_spec::__formatter_integer<_CharT> {}; 82 template <__formatter::__char_type _CharT> 83 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<short, _CharT> 84 : public __format_spec::__formatter_integer<_CharT> {}; 85 template <__formatter::__char_type _CharT> 86 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<int, _CharT> 87 : public __format_spec::__formatter_integer<_CharT> {}; 88 template <__formatter::__char_type _CharT> 89 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long, _CharT> 90 : public __format_spec::__formatter_integer<_CharT> {}; 91 template <__formatter::__char_type _CharT> 92 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 93 formatter<long long, _CharT> 94 : public __format_spec::__formatter_integer<_CharT> {}; 95 #ifndef _LIBCPP_HAS_NO_INT128 96 template <__formatter::__char_type _CharT> 97 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 98 formatter<__int128_t, _CharT> 99 : public __format_spec::__formatter_integer<_CharT> { 100 using _Base = __format_spec::__formatter_integer<_CharT>; 101 102 _LIBCPP_HIDE_FROM_ABI auto format(__int128_t __value, auto& __ctx) 103 -> decltype(__ctx.out()) { 104 // TODO FMT Implement full 128 bit support. 105 using _To = long long; 106 if (__value < numeric_limits<_To>::min() || 107 __value > numeric_limits<_To>::max()) 108 __throw_format_error("128-bit value is outside of implemented range"); 109 110 return _Base::format(static_cast<_To>(__value), __ctx); 111 } 112 }; 113 #endif 114 115 // Unsigned integral types. 116 template <__formatter::__char_type _CharT> 117 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 118 formatter<unsigned char, _CharT> 119 : public __format_spec::__formatter_integer<_CharT> {}; 120 template <__formatter::__char_type _CharT> 121 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 122 formatter<unsigned short, _CharT> 123 : public __format_spec::__formatter_integer<_CharT> {}; 124 template <__formatter::__char_type _CharT> 125 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 126 formatter<unsigned, _CharT> 127 : public __format_spec::__formatter_integer<_CharT> {}; 128 template <__formatter::__char_type _CharT> 129 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 130 formatter<unsigned long, _CharT> 131 : public __format_spec::__formatter_integer<_CharT> {}; 132 template <__formatter::__char_type _CharT> 133 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 134 formatter<unsigned long long, _CharT> 135 : public __format_spec::__formatter_integer<_CharT> {}; 136 #ifndef _LIBCPP_HAS_NO_INT128 137 template <__formatter::__char_type _CharT> 138 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 139 formatter<__uint128_t, _CharT> 140 : public __format_spec::__formatter_integer<_CharT> { 141 using _Base = __format_spec::__formatter_integer<_CharT>; 142 143 _LIBCPP_HIDE_FROM_ABI auto format(__uint128_t __value, auto& __ctx) 144 -> decltype(__ctx.out()) { 145 // TODO FMT Implement full 128 bit support. 146 using _To = unsigned long long; 147 if (__value < numeric_limits<_To>::min() || 148 __value > numeric_limits<_To>::max()) 149 __throw_format_error("128-bit value is outside of implemented range"); 150 151 return _Base::format(static_cast<_To>(__value), __ctx); 152 } 153 }; 154 #endif 155 156 #endif //_LIBCPP_STD_VER > 17 157 158 _LIBCPP_END_NAMESPACE_STD 159 160 _LIBCPP_POP_MACROS 161 162 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H 163