Index: .gitignore =================================================================== --- .gitignore +++ .gitignore @@ -4,6 +4,11 @@ # C extensions *.so +*.core + +# Vim +*~ +.*.swp # Distribution / packaging .Python Index: include/charconv =================================================================== --- /dev/null +++ include/charconv @@ -0,0 +1,633 @@ +// -*- C++ -*- +//===------------------------------ charconv ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_CHARCONV +#define _LIBCPP_CHARCONV + +/* + charconv synopsis + +namespace std { + + // floating-point format for primitive numerical conversion + enum class chars_format { + scientific = unspecified, + fixed = unspecified, + hex = unspecified, + general = fixed | scientific + }; + + // 23.20.2, primitive numerical output conversion + struct to_chars_result { + char* ptr; + errc ec; + }; + + to_chars_result to_chars(char* first, char* last, see below value, + int base = 10); + + to_chars_result to_chars(char* first, char* last, float value); + to_chars_result to_chars(char* first, char* last, double value); + to_chars_result to_chars(char* first, char* last, long double value); + + to_chars_result to_chars(char* first, char* last, float value, + chars_format fmt); + to_chars_result to_chars(char* first, char* last, double value, + chars_format fmt); + to_chars_result to_chars(char* first, char* last, long double value, + chars_format fmt); + + to_chars_result to_chars(char* first, char* last, float value, + chars_format fmt, int precision); + to_chars_result to_chars(char* first, char* last, double value, + chars_format fmt, int precision); + to_chars_result to_chars(char* first, char* last, long double value, + chars_format fmt, int precision); + + // 23.20.3, primitive numerical input conversion + struct from_chars_result { + const char* ptr; + errc ec; + }; + + from_chars_result from_chars(const char* first, const char* last, + see below& value, int base = 10); + + from_chars_result from_chars(const char* first, const char* last, + float& value, + chars_format fmt = chars_format::general); + from_chars_result from_chars(const char* first, const char* last, + double& value, + chars_format fmt = chars_format::general); + from_chars_result from_chars(const char* first, const char* last, + long double& value, + chars_format fmt = chars_format::general); + +} // namespace std + +*/ + +#include <__errc> +#include +#include +#include +#include +#include + +#include <__debug> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +enum class _LIBCPP_ENUM_VIS chars_format +{ + scientific = 0x1, + fixed = 0x2, + hex = 0x4, + general = fixed | scientific +}; + +struct _LIBCPP_TYPE_VIS to_chars_result +{ + char* ptr; + errc ec; +}; + +struct _LIBCPP_TYPE_VIS from_chars_result +{ + const char* ptr; + errc ec; +}; + +void to_chars(char*, char*, bool, int = 10) = delete; +void from_chars(const char*, const char*, bool, int = 10) = delete; + +namespace __itoa +{ + +static constexpr uint64_t __pow10_64[] = { + UINT64_C(0), + UINT64_C(10), + UINT64_C(100), + UINT64_C(1000), + UINT64_C(10000), + UINT64_C(100000), + UINT64_C(1000000), + UINT64_C(10000000), + UINT64_C(100000000), + UINT64_C(1000000000), + UINT64_C(10000000000), + UINT64_C(100000000000), + UINT64_C(1000000000000), + UINT64_C(10000000000000), + UINT64_C(100000000000000), + UINT64_C(1000000000000000), + UINT64_C(10000000000000000), + UINT64_C(100000000000000000), + UINT64_C(1000000000000000000), + UINT64_C(10000000000000000000), +}; + +static 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), + UINT32_C(1000000000), +}; + +_LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer); +_LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer); + +template +struct _LIBCPP_HIDDEN __traits_base +{ + using type = uint64_t; + +#if !defined(_LIBCPP_COMPILER_MSVC) + static _LIBCPP_INLINE_VISIBILITY auto width(_Tp __v) -> int + { + auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12; + return __t - (__v < __pow10_64[__t]) + 1; + } +#endif + + static _LIBCPP_INLINE_VISIBILITY auto convert(_Tp __v, char* __p) -> char* + { + return __u64toa(__v, __p); + } + + static _LIBCPP_INLINE_VISIBILITY auto pow() -> type const (&)[20] + { + return __pow10_64; + } +}; + +template +struct _LIBCPP_HIDDEN + __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))> +{ + using type = uint32_t; + +#if !defined(_LIBCPP_COMPILER_MSVC) + static _LIBCPP_INLINE_VISIBILITY auto width(_Tp __v) -> int + { + auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12; + return __t - (__v < __pow10_32[__t]) + 1; + } +#endif + + static _LIBCPP_INLINE_VISIBILITY auto convert(_Tp __v, char* __p) -> char* + { + return __u32toa(__v, __p); + } + + static _LIBCPP_INLINE_VISIBILITY auto pow() -> type const (&)[10] + { + return __pow10_32; + } +}; + +template +inline _LIBCPP_INLINE_VISIBILITY bool +__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) +{ + auto __c = __a * __b; + __r = __c; + return __c > (numeric_limits::max)(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY bool +__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) +{ + auto __c = __a * __b; + __r = __c; + return __c > (numeric_limits::max)(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY bool +__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) +{ + static_assert(is_unsigned<_Tp>::value, ""); +#if !defined(_LIBCPP_COMPILER_MSVC) + return __builtin_mul_overflow(__a, __b, &__r); +#else + bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a; + __r = __a * __b; + return __did; +#endif +} + +template +inline _LIBCPP_INLINE_VISIBILITY bool +__mul_overflowed(_Tp __a, _Up __b, _Tp& __r) +{ + return __mul_overflowed(__a, static_cast<_Tp>(__b), __r); +} + +template +struct _LIBCPP_HIDDEN traits : __traits_base<_Tp> +{ + static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; + using __traits_base<_Tp>::pow; + using typename __traits_base<_Tp>::type; + + // precondition: at least one non-zero character available + static _LIBCPP_INLINE_VISIBILITY auto + read(char const* __p, char const* __ep, type& __a, type& __b) -> char const* + { + type __cprod[digits]; + int __j = digits - 1; + int __i = digits; + do + { + if (!('0' <= *__p && *__p <= '9')) + break; + __cprod[--__i] = *__p++ - '0'; + } while (__p != __ep && __i != 0); + + __a = inner_product(__cprod + __i + 1, __cprod + __j, pow() + 1, + __cprod[__i]); + if (__mul_overflowed(__cprod[__j], pow()[__j - __i], __b)) + --__p; + return __p; + } + + template + static _LIBCPP_INLINE_VISIBILITY auto + inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) -> _Up + { + for (; __first1 < __last1; ++__first1, ++__first2) + __init = __init + *__first1 * *__first2; + return __init; + } +}; + +} // namespace __itoa + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__complement(_Tp __x) -> _Tp +{ + static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); + return _Tp(~__x + 1); +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__to_unsigned(_Tp __x) -> typename make_unsigned<_Tp>::type +{ + return static_cast::type>(__x); +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) + -> to_chars_result +{ + auto __x = __to_unsigned(__value); + if (__value < 0 && __first != __last) + { + *__first++ = '-'; + __x = __complement(__x); + } + + return __to_chars_itoa(__first, __last, __x, false_type()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) + -> to_chars_result +{ + using __tx = __itoa::traits<_Tp>; + auto __diff = __last - __first; + +#if !defined(_LIBCPP_COMPILER_MSVC) + if (__tx::digits <= __diff || __tx::width(__value) <= __diff) + return {__tx::convert(__value, __first), {}}; + else + return {__last, errc::value_too_large}; +#else + if (__tx::digits <= __diff) + return {__tx::convert(__value, __first), {}}; + else + { + char __buf[__tx::digits]; + auto __p = __tx::convert(__value, __buf); + auto __len = __p - __buf; + if (__len <= __diff) + { + memcpy(__first, __buf, __len); + return {__first + __len, {}}; + } + else + return {__last, errc::value_too_large}; + } +#endif +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, + true_type) -> to_chars_result +{ + auto __x = __to_unsigned(__value); + if (__value < 0 && __first != __last) + { + *__first++ = '-'; + __x = __complement(__x); + } + + return __to_chars_integral(__first, __last, __x, __base, false_type()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, + false_type) -> to_chars_result +{ + if (__base == 10) + return __to_chars_itoa(__first, __last, __value, false_type()); + + auto __gen_digit = [](_Tp __c) { + return "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; + }; + + auto __p = __last; + while (__p != __first) + { + auto __c = __value % __base; + __value /= __base; + *--__p = __gen_digit(__c); + if (__value == 0) + break; + } + + auto __len = __last - __p; + if (__value != 0 || !__len) + return {__last, errc::value_too_large}; + else + { + memmove(__first, __p, __len); + return {__first + __len, {}}; + } +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +to_chars(char* __first, char* __last, _Tp __value) -> to_chars_result +{ + return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>()); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +to_chars(char* __first, char* __last, _Tp __value, int __base) + -> to_chars_result +{ + _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); + return __to_chars_integral(__first, __last, __value, __base, + is_signed<_Tp>()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) + -> from_chars_result +{ + using __tl = numeric_limits<_Tp>; + decltype(__to_unsigned(__value)) __x; + + bool __neg = (__first != __last && *__first == '-'); + auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); + switch (__r.ec) + { + case errc::invalid_argument: + return {__first, __r.ec}; + case errc::result_out_of_range: + return __r; + default: + break; + } + + if (__neg) + { + if (__x <= __complement(__to_unsigned(__tl::min()))) + { + __x = __complement(__x); + memcpy(&__value, &__x, sizeof(__x)); + return __r; + } + } + else + { + if (__x <= (__tl::max)()) + { + __value = __x; + return __r; + } + } + + return {__r.ptr, errc::result_out_of_range}; +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__in_pattern(_Tp __c) -> bool +{ + return '0' <= __c && __c <= '9'; +} + +struct _LIBCPP_HIDDEN __in_pattern_result +{ + bool __ok; + int __val; + + explicit _LIBCPP_ALWAYS_INLINE operator bool() const { return __ok; } +}; + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__in_pattern(_Tp __c, int __base) -> __in_pattern_result +{ + if (__base <= 10) + return {'0' <= __c && __c < '0' + __base, __c - '0'}; + else if (__in_pattern(__c)) + return {true, __c - '0'}; + else if ('a' <= __c && __c < 'a' + __base - 10) + return {true, __c - 'a' + 10}; + else + return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; +} + +template +inline _LIBCPP_INLINE_VISIBILITY auto +__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, + _Ts... __args) -> from_chars_result +{ + auto __find_non_zero = [](_It __first, _It __last) -> _It { + for (; __first != __last; ++__first) + if (*__first != '0') + break; + return __first; + }; + + auto __p = __find_non_zero(__first, __last); + if (__p == __last || !__in_pattern(*__p, __args...)) + { + if (__p == __first) + return {__first, errc::invalid_argument}; + else + { + __value = 0; + return {__p, {}}; + } + } + + auto __r = __f(__p, __last, __value, __args...); + // ...you just can't let them approve anything that wasn't implemented + if (__r.ec == errc::result_out_of_range) + { + for (; __r.ptr != __last; ++__r.ptr) + { + if (!__in_pattern(*__r.ptr, __args...)) + break; + } + } + + return __r; +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) + -> from_chars_result +{ + using __tx = __itoa::traits<_Tp>; + using __output_type = typename __tx::type; + + return __subject_seq_combinator( + __first, __last, __value, + [](const char* __first, const char* __last, + _Tp& __value) -> from_chars_result { + __output_type __a, __b; + auto __p = __tx::read(__first, __last, __a, __b); + if (__p == __last || !__in_pattern(*__p)) + { + __output_type __m = (numeric_limits<_Tp>::max)(); + if (__m >= __a && __m - __a >= __b) + { + __value = __a + __b; + return {__p, {}}; + } + } + return {__p, errc::result_out_of_range}; + }); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) + -> from_chars_result +{ + using __t = decltype(__to_unsigned(__value)); + return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +__from_chars_integral(const char* __first, const char* __last, _Tp& __value, + int __base) -> from_chars_result +{ + if (__base == 10) + return __from_chars_atoi(__first, __last, __value); + + return __subject_seq_combinator( + __first, __last, __value, + [](const char* __p, const char* __last, _Tp& __value, + int __base) -> from_chars_result { + using __tl = numeric_limits<_Tp>; + auto __digits = __tl::digits / log2f(float(__base)); + _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0; + + for (int __i = 1; __p != __last; ++__i, ++__p) + { + if (auto __c = __in_pattern(*__p, __base)) + { + if (__i < __digits - 1) + __a = __a * __base + __c.__val; + else + { + if (!__itoa::__mul_overflowed(__a, __base, __a)) + ++__p; + __b = __c.__val; + break; + } + } + else + break; + } + + if (__p == __last || !__in_pattern(*__p, __base)) + { + if ((__tl::max)() - __a >= __b) + { + __value = __a + __b; + return {__p, {}}; + } + } + return {__p, errc::result_out_of_range}; + }, + __base); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +__from_chars_integral(const char* __first, const char* __last, _Tp& __value, + int __base) -> from_chars_result +{ + using __t = decltype(__to_unsigned(__value)); + return __sign_combinator(__first, __last, __value, + __from_chars_integral<__t>, __base); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +from_chars(const char* __first, const char* __last, _Tp& __value) + -> from_chars_result +{ + return __from_chars_atoi(__first, __last, __value); +} + +template ::value, int>::type = 0> +inline _LIBCPP_INLINE_VISIBILITY auto +from_chars(const char* __first, const char* __last, _Tp& __value, int __base) + -> from_chars_result +{ + _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); + return __from_chars_integral(__first, __last, __value, __base); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_CHARCONV Index: src/charconv.cpp =================================================================== --- /dev/null +++ src/charconv.cpp @@ -0,0 +1,230 @@ +//===------------------------- charconv.cpp -------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "charconv" +#include + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __itoa +{ + +static constexpr char cDigitsLut[200] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', + '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2', + '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', + '7', '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0', '5', '1', '5', + '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', + '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', + '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', + '7', '9', '8', '9', '9'}; + +#define APPEND1(i) \ + do \ + { \ + *buffer++ = '0' + static_cast(i); \ + } while (0) + +#define APPEND2(i) \ + do \ + { \ + memcpy(buffer, &cDigitsLut[(i)*2], 2); \ + buffer += 2; \ + } while (0) + +#define APPEND3(i) \ + do \ + { \ + APPEND1((i) / 100); \ + APPEND2((i) % 100); \ + } while (0) + +#define APPEND4(i) \ + do \ + { \ + APPEND2((i) / 100); \ + APPEND2((i) % 100); \ + } while (0) + +char* +__u32toa(uint32_t value, char* buffer) +{ + if (value < 10000) + { + if (value < 100) + { + if (value < 10) + APPEND1(value); + else + APPEND2(value); + } + else + { + if (value < 1000) + APPEND3(value); + else + APPEND4(value); + } + } + else if (value < 100000000) + { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + if (value < 1000000) + { + if (value < 100000) + APPEND1(b); + else + APPEND2(b); + } + else + { + if (value < 10000000) + APPEND3(b); + else + APPEND4(b); + } + + APPEND4(c); + } + else + { + // value = aabbbbcccc in decimal + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a < 10) + APPEND1(a); + else + APPEND2(a); + + APPEND4(value / 10000); + APPEND4(value % 10000); + } + + return buffer; +} + +char* +__u64toa(uint64_t value, char* buffer) +{ + if (value < 100000000) + { + uint32_t v = static_cast(value); + if (v < 10000) + { + if (v < 100) + { + if (v < 10) + APPEND1(v); + else + APPEND2(v); + } + else + { + if (v < 1000) + APPEND3(v); + else + APPEND4(v); + } + } + else + { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + if (v < 1000000) + { + if (v < 100000) + APPEND1(b); + else + APPEND2(b); + } + else + { + if (v < 10000000) + APPEND3(b); + else + APPEND4(b); + } + + APPEND4(c); + } + } + else if (value < 10000000000000000) + { + const uint32_t v0 = static_cast(value / 100000000); + const uint32_t v1 = static_cast(value % 100000000); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + if (v0 < 1000000) + { + if (v0 < 100000) + APPEND1(b0); + else + APPEND2(b0); + } + else + { + if (v0 < 10000000) + APPEND3(b0); + else + APPEND4(b0); + } + + APPEND4(c0); + APPEND4(v1 / 10000); + APPEND4(v1 % 10000); + } + else + { + const uint32_t a = + static_cast(value / 10000000000000000); // 1 to 1844 + value %= 10000000000000000; + + if (a < 100) + { + if (a < 10) + APPEND1(a); + else + APPEND2(a); + } + else + { + if (a < 1000) + APPEND3(a); + else + APPEND4(a); + } + + const uint32_t v0 = static_cast(value / 100000000); + const uint32_t v1 = static_cast(value % 100000000); + APPEND4(v0 / 10000); + APPEND4(v0 % 10000); + APPEND4(v1 / 10000); + APPEND4(v1 % 10000); + } + + return buffer; +} + +} // namespace __itoa + +_LIBCPP_END_NAMESPACE_STD Index: test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp =================================================================== --- /dev/null +++ test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 +// + +// In +// +// from_chars_result from_chars(const char* first, const char* last, +// Integral& value, int base = 10) +// +// Integral cannot be bool. + +#include + +int main() +{ + using std::from_chars; + char buf[] = "01001"; + bool lv; + + from_chars(buf, buf + sizeof(buf), lv); // expected-error {{call to deleted function}} + from_chars(buf, buf + sizeof(buf), lv, 16); // expected-error {{call to deleted function}} +} Index: test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp =================================================================== --- /dev/null +++ test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp @@ -0,0 +1,172 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 +// + +// from_chars_result from_chars(const char* first, const char* last, +// Integral& value, int base = 10) + +#include "charconv_test_helpers.h" + +template +struct test_basics : roundtrip_test_base +{ + using roundtrip_test_base::test; + + void operator()() + { + test(0); + test(42); + test(32768); + test(0, 10); + test(42, 10); + test(32768, 10); + test(0xf, 16); + test(0xdeadbeaf, 16); + test(0755, 8); + + for (int b = 2; b < 37; ++b) + { + using xl = std::numeric_limits; + + test(1, b); + test(xl::lowest(), b); + test((xl::max)(), b); + test((xl::max)() / 2, b); + } + + using std::from_chars; + std::from_chars_result r; + T x; + + { + char s[] = "001x"; + + // the expected form of the subject sequence is a sequence of + // letters and digits representing an integer with the radix + // specified by base (C11 7.22.1.4/3) + r = from_chars(s, s + sizeof(s), x); + assert(r.ec == std::errc{}); + assert(r.ptr == s + 3); + assert(x == 1); + } + + { + char s[] = "0X7BAtSGHDkEIXZg "; + + // The letters from a (or A) through z (or Z) are ascribed the + // values 10 through 35; (C11 7.22.1.4/3) + r = from_chars(s, s + sizeof(s), x, 36); + assert(r.ec == std::errc::result_out_of_range); + // The member ptr of the return value points to the first character + // not matching the pattern + assert(r.ptr == s + sizeof(s) - 2); + assert(x == 1); + + // no "0x" or "0X" prefix shall appear if the value of base is 16 + r = from_chars(s, s + sizeof(s), x, 16); + assert(r.ec == std::errc{}); + assert(r.ptr == s + 1); + assert(x == 0); + + // only letters and digits whose ascribed values are less than that + // of base are permitted. (C11 7.22.1.4/3) + r = from_chars(s + 2, s + sizeof(s), x, 12); + // If the parsed value is not in the range representable by the type + // of value, + if (!fits_in(1150)) + { + // value is unmodified and + assert(x == 0); + // the member ec of the return value is equal to + // errc::result_out_of_range + assert(r.ec == std::errc::result_out_of_range); + } + else + { + // Otherwise, value is set to the parsed value, + assert(x == 1150); + // and the member ec is value-initialized. + assert(r.ec == std::errc{}); + } + assert(r.ptr == s + 5); + } + } +}; + +template +struct test_signed : roundtrip_test_base +{ + using roundtrip_test_base::test; + + void operator()() + { + test(-1); + test(-12); + test(-1, 10); + test(-12, 10); + test(-21734634, 10); + test(-2647, 2); + test(-0xcc1, 16); + + for (int b = 2; b < 37; ++b) + { + using xl = std::numeric_limits; + + test(0, b); + test(xl::lowest(), b); + test((xl::max)(), b); + } + + using std::from_chars; + std::from_chars_result r; + T x; + + { + // If the pattern allows for an optional sign, + char s[] = " - 9+12"; + // but the string has no digit characters following the sign, + r = from_chars(s + 1, s + sizeof(s), x); + // no characters match the pattern. + assert(r.ptr == s + 1); + assert(r.ec == std::errc::invalid_argument); + + // a minus sign is the only sign that may appear + r = from_chars(s + 3, s + sizeof(s), x); + assert(r.ec == std::errc{}); + // The member ptr of the return value points to the first character + // not matching the pattern, + assert(r.ptr == s + 4); + assert(x == 9); + + r = from_chars(s + 5, s + 7, x); + assert(r.ec == std::errc{}); + // or has the value last if all characters match. + assert(r.ptr == s + 7); + assert(x == 12); + + // If no characters match the pattern, + r = from_chars(s + 4, s + sizeof(s), x); + // value is unmodified, + assert(x == 12); + // the member ptr of the return value is first and + assert(r.ptr == s + 4); + // the member ec is equal to errc::invalid_argument. + assert(r.ec == std::errc::invalid_argument); + } + } +}; + +int +main() +{ + run(integrals); + run(all_signed); +} Index: test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp =================================================================== --- /dev/null +++ test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 +// + +// In +// +// to_chars_result to_chars(char* first, char* last, Integral value, +// int base = 10) +// +// Integral cannot be bool. + +#include + +int main() +{ + using std::to_chars; + char buf[10]; + bool lv = true; + + to_chars(buf, buf + sizeof(buf), false); // expected-error {{call to deleted function}} + to_chars(buf, buf + sizeof(buf), lv, 16); // expected-error {{call to deleted function}} +} Index: test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp =================================================================== --- /dev/null +++ test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 +// + +// to_chars_result to_chars(char* first, char* last, Integral value, +// int base = 10) + +#include "charconv_test_helpers.h" + +template +struct test_basics : to_chars_test_base +{ + using to_chars_test_base::test; + using to_chars_test_base::test_value; + + void operator()() + { + test(0, "0"); + test(42, "42"); + test(32768, "32768"); + test(0, "0", 10); + test(42, "42", 10); + test(32768, "32768", 10); + test(0xf, "f", 16); + test(0xdeadbeaf, "deadbeaf", 16); + test(0755, "755", 8); + + for (int b = 2; b < 37; ++b) + { + using xl = std::numeric_limits; + + test_value(1, b); + test_value(xl::lowest(), b); + test_value((xl::max)(), b); + test_value((xl::max)() / 2, b); + } + } +}; + +template +struct test_signed : to_chars_test_base +{ + using to_chars_test_base::test; + using to_chars_test_base::test_value; + + void operator()() + { + test(-1, "-1"); + test(-12, "-12"); + test(-1, "-1", 10); + test(-12, "-12", 10); + test(-21734634, "-21734634", 10); + test(-2647, "-101001010111", 2); + test(-0xcc1, "-cc1", 16); + + for (int b = 2; b < 37; ++b) + { + using xl = std::numeric_limits; + + test_value(0, b); + test_value(xl::lowest(), b); + test_value((xl::max)(), b); + } + } +}; + +int +main() +{ + run(integrals); + run(all_signed); +} Index: test/support/charconv_test_helpers.h =================================================================== --- /dev/null +++ test/support/charconv_test_helpers.h @@ -0,0 +1,233 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_CHARCONV_TEST_HELPERS_H +#define SUPPORT_CHARCONV_TEST_HELPERS_H + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +using std::false_type; +using std::true_type; + +template +constexpr auto +is_non_narrowing(From a) -> decltype(To{a}, true_type()) +{ + return {}; +} + +template +constexpr auto +is_non_narrowing(...) -> false_type +{ + return {}; +} + +template +constexpr bool +_fits_in(T, true_type /* non-narrowing*/, ...) +{ + return true; +} + +template > +constexpr bool +_fits_in(T v, false_type, true_type /* T signed*/, true_type /* X signed */) +{ + return xl::lowest() <= v && v <= (xl::max)(); +} + +template > +constexpr bool +_fits_in(T v, false_type, true_type /* T signed */, false_type /* X unsigned*/) +{ + 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 <= typename std::make_unsigned::type((xl::max)()); +} + +template +constexpr bool +fits_in(T v) +{ + return _fits_in(v, is_non_narrowing(v), std::is_signed(), + std::is_signed()); +} + +template +struct to_chars_test_base +{ + template + void test(T v, char const (&expect)[N], Ts... args) + { + using std::to_chars; + std::to_chars_result r; + + constexpr size_t len = N - 1; + static_assert(len > 0, "expected output won't be empty"); + + if (!fits_in(v)) + return; + + r = to_chars(buf, buf + len - 1, X(v), args...); + assert(r.ptr == buf + len - 1); + assert(r.ec == std::errc::value_too_large); + + r = to_chars(buf, buf + sizeof(buf), X(v), args...); + assert(r.ptr == buf + len); + assert(r.ec == std::errc{}); + assert(memcmp(buf, expect, len) == 0); + } + + template + void test_value(X v, Ts... args) + { + using std::to_chars; + std::to_chars_result r; + + r = to_chars(buf, buf + sizeof(buf), v, args...); + assert(r.ec == std::errc{}); + *r.ptr = '\0'; + + auto a = fromchars(buf, r.ptr, args...); + assert(v == a); + + auto ep = r.ptr - 1; + r = to_chars(buf, ep, v, args...); + assert(r.ptr == ep); + assert(r.ec == std::errc::value_too_large); + } + +private: + using max_t = typename std::conditional::value, long long, + unsigned long long>::type; + + static auto fromchars(char const* p, char const* ep, int base, true_type) + -> long long + { + char* last; + auto r = strtoll(p, &last, base); + assert(last == ep); + + return r; + } + + static auto fromchars(char const* p, char const* ep, int base, false_type) + -> unsigned long long + { + char* last; + auto r = strtoull(p, &last, base); + assert(last == ep); + + return r; + } + + static auto fromchars(char const* p, char const* ep, int base = 10) -> max_t + { + return fromchars(p, ep, base, std::is_signed()); + } + + char buf[100]; +}; + +template +struct roundtrip_test_base +{ + template + void test(T v, Ts... args) + { + using std::from_chars; + using std::to_chars; + std::from_chars_result r2; + std::to_chars_result r; + X x = 0xc; + + if (fits_in(v)) + { + r = to_chars(buf, buf + sizeof(buf), v, args...); + assert(r.ec == std::errc{}); + + r2 = from_chars(buf, r.ptr, x, args...); + assert(r2.ptr == r.ptr); + assert(x == X(v)); + } + else + { + r = to_chars(buf, buf + sizeof(buf), v, args...); + assert(r.ec == std::errc{}); + + r2 = from_chars(buf, r.ptr, x, args...); + + if (std::is_signed::value && v < 0 && std::is_unsigned::value) + { + assert(x == 0xc); + assert(r2.ptr == buf); + assert(r.ec == std::errc::invalid_argument); + } + else + { + assert(x == 0xc); + assert(r2.ptr == r.ptr); + assert(r2.ec == std::errc::result_out_of_range); + } + } + } + +private: + char buf[100]; +}; + +template +struct type_list +{ +}; + +template +struct type_concat; + +template +struct type_concat, type_list> +{ + using type = type_list; +}; + +template +using concat_t = typename type_concat::type; + +template +constexpr auto concat(L1, L2) -> concat_t +{ + return {}; +} + +auto all_signed = type_list(); +auto all_unsigned = type_list(); +auto integrals = concat(all_signed, all_unsigned); + +template