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 // TODO FMT Remove this once we require compilers with proper C++20 support. 34 // If the compiler has no concepts support, the format header will be disabled. 35 // Without concepts support enable_if needs to be used and that too much effort 36 // to support compilers with partial C++20 support. 37 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) 38 39 namespace __format_spec { 40 41 template <class _CharT> 42 class _LIBCPP_TEMPLATE_VIS __parser_integer : public __parser_integral<_CharT> { 43 public: 44 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx) 45 -> decltype(__parse_ctx.begin()) { 46 auto __it = __parser_integral<_CharT>::__parse(__parse_ctx); 47 48 switch (this->__type) { 49 case _Flags::_Type::__default: 50 this->__type = _Flags::_Type::__decimal; 51 [[fallthrough]]; 52 53 case _Flags::_Type::__binary_lower_case: 54 case _Flags::_Type::__binary_upper_case: 55 case _Flags::_Type::__octal: 56 case _Flags::_Type::__decimal: 57 case _Flags::_Type::__hexadecimal_lower_case: 58 case _Flags::_Type::__hexadecimal_upper_case: 59 this->__handle_integer(); 60 break; 61 62 case _Flags::_Type::__char: 63 this->__handle_char(); 64 break; 65 66 default: 67 __throw_format_error("The format-spec type has a type not supported for " 68 "an integer argument"); 69 } 70 return __it; 71 } 72 }; 73 74 template <class _CharT> 75 using __formatter_integer = __formatter_integral<__parser_integer<_CharT>>; 76 77 } // namespace __format_spec 78 79 // [format.formatter.spec]/2.3 80 // For each charT, for each cv-unqualified arithmetic type ArithmeticT other 81 // than char, wchar_t, char8_t, char16_t, or char32_t, a specialization 82 83 // Signed integral types. 84 template <__formatter::__char_type _CharT> 85 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 86 formatter<signed char, _CharT> 87 : public __format_spec::__formatter_integer<_CharT> {}; 88 template <__formatter::__char_type _CharT> 89 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<short, _CharT> 90 : public __format_spec::__formatter_integer<_CharT> {}; 91 template <__formatter::__char_type _CharT> 92 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<int, _CharT> 93 : public __format_spec::__formatter_integer<_CharT> {}; 94 template <__formatter::__char_type _CharT> 95 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long, _CharT> 96 : public __format_spec::__formatter_integer<_CharT> {}; 97 template <__formatter::__char_type _CharT> 98 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 99 formatter<long long, _CharT> 100 : public __format_spec::__formatter_integer<_CharT> {}; 101 #ifndef _LIBCPP_HAS_NO_INT128 102 template <__formatter::__char_type _CharT> 103 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 104 formatter<__int128_t, _CharT> 105 : public __format_spec::__formatter_integer<_CharT> { 106 using _Base = __format_spec::__formatter_integer<_CharT>; 107 108 _LIBCPP_HIDE_FROM_ABI auto format(__int128_t __value, auto& __ctx) 109 -> decltype(__ctx.out()) { 110 // TODO FMT Implement full 128 bit support. 111 using _To = long long; 112 if (__value < numeric_limits<_To>::min() || 113 __value > numeric_limits<_To>::max()) 114 __throw_format_error("128-bit value is outside of implemented range"); 115 116 return _Base::format(static_cast<_To>(__value), __ctx); 117 } 118 }; 119 #endif 120 121 // Unsigned integral types. 122 template <__formatter::__char_type _CharT> 123 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 124 formatter<unsigned char, _CharT> 125 : public __format_spec::__formatter_integer<_CharT> {}; 126 template <__formatter::__char_type _CharT> 127 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 128 formatter<unsigned short, _CharT> 129 : public __format_spec::__formatter_integer<_CharT> {}; 130 template <__formatter::__char_type _CharT> 131 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 132 formatter<unsigned, _CharT> 133 : public __format_spec::__formatter_integer<_CharT> {}; 134 template <__formatter::__char_type _CharT> 135 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 136 formatter<unsigned long, _CharT> 137 : public __format_spec::__formatter_integer<_CharT> {}; 138 template <__formatter::__char_type _CharT> 139 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 140 formatter<unsigned long long, _CharT> 141 : public __format_spec::__formatter_integer<_CharT> {}; 142 #ifndef _LIBCPP_HAS_NO_INT128 143 template <__formatter::__char_type _CharT> 144 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT 145 formatter<__uint128_t, _CharT> 146 : public __format_spec::__formatter_integer<_CharT> { 147 using _Base = __format_spec::__formatter_integer<_CharT>; 148 149 _LIBCPP_HIDE_FROM_ABI auto format(__uint128_t __value, auto& __ctx) 150 -> decltype(__ctx.out()) { 151 // TODO FMT Implement full 128 bit support. 152 using _To = unsigned long long; 153 if (__value < numeric_limits<_To>::min() || 154 __value > numeric_limits<_To>::max()) 155 __throw_format_error("128-bit value is outside of implemented range"); 156 157 return _Base::format(static_cast<_To>(__value), __ctx); 158 } 159 }; 160 #endif 161 162 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) 163 164 #endif //_LIBCPP_STD_VER > 17 165 166 _LIBCPP_END_NAMESPACE_STD 167 168 _LIBCPP_POP_MACROS 169 170 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H 171