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,473 @@ +// -*- 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 <__debug> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +extern "C" float log2f(float); + +_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; + +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 __has_builtin(__builtin_clzll) + 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()); + + // XXX assuming ASCII-compatible basic character sets + auto __gen_digit = [](_Tp __c) { + return char(__c + (__c < 10 ? '0' : 'a' - 10)); + }; + + auto __reverse_buffer = [](char* __first, char* __last) { + for (; __first < --__last; ++__first) + _VSTD::iter_swap(__first, __last); + }; + + auto __p = __first; + while (__p != __last) + { + auto __c = __value % __base; + __value /= __base; + *__p++ = __gen_digit(__c); + if (__value == 0) + break; + } + + if (__value != 0 || __p == __first) + return {__p, errc::value_too_large}; + else + { + __reverse_buffer(__first, __p); + return {__p, {}}; + } +} + +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: include/support/itoa/itoa.h =================================================================== --- /dev/null +++ include/support/itoa/itoa.h @@ -0,0 +1,204 @@ +// -*- C++ -*- +//===--------------------- support/itoa/itoa.h ----------------------------===// +// +// 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_SUPPORT_ITOA_ITOA_H +#define _LIBCPP_SUPPORT_ITOA_ITOA_H + +#include <__config> +#include +#include +#include + +_LIBCPP_BEGIN_NAMESPACE_STD + +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), +}; + +#if __has_builtin(__builtin_clzll) + +inline _LIBCPP_INLINE_VISIBILITY auto +__u64digits10(uint64_t __x) -> int +{ + auto __t = (64 - __builtin_clzll(__x | 1)) * 1233 >> 12; + return __t - (__x < __pow10_64[__t]) + 1; +} + +inline _LIBCPP_INLINE_VISIBILITY auto +__u32digits10(uint32_t __x) -> int +{ + auto __t = (32 - __builtin_clz(__x | 1)) * 1233 >> 12; + return __t - (__x < __pow10_32[__t]) + 1; +} + +#endif + +extern _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer); +extern _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer); + +template +struct _LIBCPP_HIDDEN __traits +{ + static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; + using type = uint64_t; + +#if __has_builtin(__builtin_clzll) + static _LIBCPP_INLINE_VISIBILITY auto width(_Tp __v) -> int + { + return __u64digits10(__v); + } +#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<_Tp, decltype(void(uint32_t{declval<_Tp>()}))> +{ + static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; + using type = uint32_t; + +#if __has_builtin(__builtin_clzll) + static _LIBCPP_INLINE_VISIBILITY auto width(_Tp __v) -> int + { + return __u32digits10(__v); + } +#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 __has_builtin(__builtin_mul_overflow) + 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<_Tp> +{ + using __traits<_Tp>::digits; + using __traits<_Tp>::pow; + using typename __traits<_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 + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_SUPPORT_ITOA_ITOA_H Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -2,7 +2,7 @@ # Get sources # FIXME: Don't use glob here -file(GLOB LIBCXX_SOURCES ../src/*.cpp) +file(GLOB LIBCXX_SOURCES ../src/*.cpp ../src/support/itoa/*.cpp) if(WIN32) file(GLOB LIBCXX_WIN32_SOURCES ../src/support/win32/*.cpp) list(APPEND LIBCXX_SOURCES ${LIBCXX_WIN32_SOURCES}) Index: src/support/itoa/itoa.cpp =================================================================== --- /dev/null +++ src/support/itoa/itoa.cpp @@ -0,0 +1,296 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __itoa +{ + +static const 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'}; + +char* +__u32toa(uint32_t value, char* buffer) +{ + + if (value < 10000) + { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) + { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else + { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) + { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +char* +__u64toa(uint64_t value, char* buffer) +{ + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) + { + uint32_t v = static_cast(value); + if (v < 10000) + { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else + { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) + { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + if (value >= kTen8) + *buffer++ = cDigitsLut[d4 + 1]; + + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else + { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) + { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) + { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + 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); + LIBCPP_ASSERT(r.ec == std::errc{}); + LIBCPP_ASSERT(r.ptr == s + 3); + LIBCPP_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); + LIBCPP_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 + LIBCPP_ASSERT(r.ptr == s + sizeof(s) - 2); + LIBCPP_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); + LIBCPP_ASSERT(r.ec == std::errc{}); + LIBCPP_ASSERT(r.ptr == s + 1); + LIBCPP_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 + LIBCPP_ASSERT(x == 0); + // the member ec of the return value is equal to + // errc::result_out_of_range + LIBCPP_ASSERT(r.ec == std::errc::result_out_of_range); + } + else + { + // Otherwise, value is set to the parsed value, + LIBCPP_ASSERT(x == 1150); + // and the member ec is value-initialized. + LIBCPP_ASSERT(r.ec == std::errc{}); + } + LIBCPP_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. + LIBCPP_ASSERT(r.ptr == s + 1); + LIBCPP_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); + LIBCPP_ASSERT(r.ec == std::errc{}); + // The member ptr of the return value points to the first character + // not matching the pattern, + LIBCPP_ASSERT(r.ptr == s + 4); + LIBCPP_ASSERT(x == 9); + + r = from_chars(s + 5, s + 7, x); + LIBCPP_ASSERT(r.ec == std::errc{}); + // or has the value last if all characters match. + LIBCPP_ASSERT(r.ptr == s + 7); + LIBCPP_ASSERT(x == 12); + + // If no characters match the pattern, + r = from_chars(s + 4, s + sizeof(s), x); + // value is unmodified, + LIBCPP_ASSERT(x == 12); + // the member ptr of the return value is first and + LIBCPP_ASSERT(r.ptr == s + 4); + // the member ec is equal to errc::invalid_argument. + LIBCPP_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...); + LIBCPP_ASSERT(r.ptr == buf + len - 1); + LIBCPP_ASSERT(r.ec == std::errc::value_too_large); + + r = to_chars(buf, buf + sizeof(buf), X(v), args...); + LIBCPP_ASSERT(r.ptr == buf + len); + LIBCPP_ASSERT(r.ec == std::errc{}); + LIBCPP_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...); + LIBCPP_ASSERT(r.ec == std::errc{}); + *r.ptr = '\0'; + + auto a = fromchars(buf, r.ptr, args...); + LIBCPP_ASSERT(v == a); + + auto ep = r.ptr - 1; + r = to_chars(buf, ep, v, args...); + LIBCPP_ASSERT(r.ptr == ep); + LIBCPP_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); + LIBCPP_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); + LIBCPP_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...); + LIBCPP_ASSERT(r.ec == std::errc{}); + + r2 = from_chars(buf, r.ptr, x, args...); + LIBCPP_ASSERT(r2.ptr == r.ptr); + LIBCPP_ASSERT(x == X(v)); + } + else + { + r = to_chars(buf, buf + sizeof(buf), v, args...); + LIBCPP_ASSERT(r.ec == std::errc{}); + + r2 = from_chars(buf, r.ptr, x, args...); + + if (std::is_signed::value && v < 0 && std::is_unsigned::value) + { + LIBCPP_ASSERT(x == 0xc); + LIBCPP_ASSERT(r2.ptr == buf); + LIBCPP_ASSERT(r.ec == std::errc::invalid_argument); + } + else + { + LIBCPP_ASSERT(x == 0xc); + LIBCPP_ASSERT(r2.ptr == r.ptr); + LIBCPP_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