From 769c2459d5c201d98c9e96c868dae94e4c7ec1a2 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 20 Mar 2019 18:13:23 +0000 Subject: [PATCH] Make to_chars/from_chars work back to C++11. This means that we can use them to implement to_string as well. Reviewed as https://reviews.llvm.org/D59598. llvm-svn: 356585 --- libcxx/include/charconv | 34 +++++++++++----------- .../charconv.from.chars/integral.bool.fail.cpp | 2 +- .../charconv/charconv.from.chars/integral.pass.cpp | 2 +- .../charconv.to.chars/integral.bool.fail.cpp | 2 +- .../charconv/charconv.to.chars/integral.pass.cpp | 2 +- libcxx/test/support/charconv_test_helpers.h | 13 +++++---- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/libcxx/include/charconv b/libcxx/include/charconv index ec9baa8..4e84ef6 100644 --- a/libcxx/include/charconv +++ b/libcxx/include/charconv @@ -96,7 +96,7 @@ _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer); _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer); } -#if _LIBCPP_STD_VER > 11 +#ifndef _LIBCPP_CXX03_LANG enum class _LIBCPP_ENUM_VIS chars_format { @@ -124,7 +124,7 @@ void from_chars(const char*, const char*, bool, int = 10) = delete; namespace __itoa { -static constexpr uint64_t __pow10_64[] = { +static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = { UINT64_C(0), UINT64_C(10), UINT64_C(100), @@ -147,7 +147,7 @@ static constexpr uint64_t __pow10_64[] = { UINT64_C(10000000000000000000), }; -static constexpr uint32_t __pow10_32[] = { +static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = { UINT32_C(0), UINT32_C(10), UINT32_C(100), UINT32_C(1000), UINT32_C(10000), UINT32_C(100000), UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000), @@ -172,7 +172,7 @@ struct _LIBCPP_HIDDEN __traits_base return __u64toa(__v, __p); } - static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_64; } + static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; } }; template @@ -194,7 +194,7 @@ struct _LIBCPP_HIDDEN return __u32toa(__v, __p); } - static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_32; } + static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; } }; template @@ -239,7 +239,7 @@ __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) template struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> { - static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; + static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1; using __traits_base<_Tp>::__pow; using typename __traits_base<_Tp>::type; @@ -285,10 +285,10 @@ __complement(_Tp __x) } template -inline _LIBCPP_INLINE_VISIBILITY auto +inline _LIBCPP_INLINE_VISIBILITY typename make_unsigned<_Tp>::type __to_unsigned(_Tp __x) { - return static_cast>(__x); + return static_cast::type>(__x); } template @@ -379,14 +379,14 @@ __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, } } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY to_chars_result to_chars(char* __first, char* __last, _Tp __value) { return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>()); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY to_chars_result to_chars(char* __first, char* __last, _Tp __value, int __base) { @@ -501,7 +501,7 @@ __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, return __r; } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) { @@ -527,7 +527,7 @@ __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) }); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) { @@ -535,7 +535,7 @@ __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result __from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) @@ -582,7 +582,7 @@ __from_chars_integral(const char* __first, const char* __last, _Tp& __value, __base); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result __from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) @@ -592,14 +592,14 @@ __from_chars_integral(const char* __first, const char* __last, _Tp& __value, __from_chars_integral<__t>, __base); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result from_chars(const char* __first, const char* __last, _Tp& __value) { return __from_chars_atoi(__first, __last, __value); } -template ::value, int> = 0> +template ::value, int>::type = 0> inline _LIBCPP_INLINE_VISIBILITY from_chars_result from_chars(const char* __first, const char* __last, _Tp& __value, int __base) { @@ -607,7 +607,7 @@ from_chars(const char* __first, const char* __last, _Tp& __value, int __base) return __from_chars_integral(__first, __last, __value, __base); } -#endif // _LIBCPP_STD_VER > 11 +#endif // _LIBCPP_CXX03_LANG _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp b/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp index 2e744cc..ef4c236 100644 --- a/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp +++ b/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -// UNSUPPORTED: c++98, c++03, c++11 +// UNSUPPORTED: c++98, c++03 // // In diff --git a/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp b/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp index d750cb4..1b8fa71 100644 --- a/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp +++ b/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -// UNSUPPORTED: c++98, c++03, c++11 +// UNSUPPORTED: c++98, c++03 // XFAIL: with_system_cxx_lib=macosx10.14 // XFAIL: with_system_cxx_lib=macosx10.13 diff --git a/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp b/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp index 5c947d1..7d8da57 100644 --- a/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp +++ b/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -// UNSUPPORTED: c++98, c++03, c++11 +// UNSUPPORTED: c++98, c++03 // // In diff --git a/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp b/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp index c034151..f0bea97 100644 --- a/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp +++ b/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -// UNSUPPORTED: c++98, c++03, c++11 +// UNSUPPORTED: c++98, c++03 // XFAIL: with_system_cxx_lib=macosx10.14 // XFAIL: with_system_cxx_lib=macosx10.13 diff --git a/libcxx/test/support/charconv_test_helpers.h b/libcxx/test/support/charconv_test_helpers.h index dc704a9..f30956e 100644 --- a/libcxx/test/support/charconv_test_helpers.h +++ b/libcxx/test/support/charconv_test_helpers.h @@ -17,8 +17,8 @@ #include "test_macros.h" -#if TEST_STD_VER <= 11 -#error This file requires C++14 +#if TEST_STD_VER < 11 +#error This file requires C++11 #endif using std::false_type; @@ -56,14 +56,14 @@ template > constexpr bool _fits_in(T v, false_type, true_type /* T signed */, false_type /* X unsigned*/) { - return 0 <= v && std::make_unsigned_t(v) <= (xl::max)(); + return 0 <= v && typename std::make_unsigned::type(v) <= (xl::max)(); } template > constexpr bool _fits_in(T v, false_type, false_type /* T unsigned */, ...) { - return v <= std::make_unsigned_t((xl::max)()); + return v <= typename std::make_unsigned::type((xl::max)()); } template @@ -119,7 +119,7 @@ struct to_chars_test_base } private: - static auto fromchars(char const* p, char const* ep, int base, true_type) + static long long fromchars(char const* p, char const* ep, int base, true_type) { char* last; auto r = strtoll(p, &last, base); @@ -128,7 +128,7 @@ private: return r; } - static auto fromchars(char const* p, char const* ep, int base, false_type) + static unsigned long long fromchars(char const* p, char const* ep, int base, false_type) { char* last; auto r = strtoull(p, &last, base); @@ -138,6 +138,7 @@ private: } static auto fromchars(char const* p, char const* ep, int base = 10) + -> decltype(fromchars(p, ep, base, std::is_signed())) { return fromchars(p, ep, base, std::is_signed()); } -- 2.7.4