summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhihao Yuan <zy@miator.net>2018-08-01 02:38:30 +0000
committerZhihao Yuan <zy@miator.net>2018-08-01 02:38:30 +0000
commitdc38e97601dd5b8c4a9a520c11a03698242d916b (patch)
treeb808f2af738e57dd4837a520b1980d8b9083ae1b
parent6952d1478ddd5a1870079d01f1a0e1eea5b09a1a (diff)
[libc++][C++17] Elementary string conversions for integral types
Summary: Major QoI considerations: - The facility is backported to C++14, same as libstdc++. - Efforts have been made to minimize the header dependencies. - The design is friendly to the uses of MSVC intrinsics (`__emulu`, `_umul128`, `_BitScanForward`, `_BitScanForward64`) but not implemented; future contributions are welcome. Thanks to Milo Yip for contributing the implementation of `__u64toa` and `__u32toa`. References: https://wg21.link/p0067r5 https://wg21.link/p0682r1 Reviewers: mclow.lists, EricWF Reviewed By: mclow.lists Subscribers: ldionne, Quuxplusone, christof, mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D41458 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@338479 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/CMakeLists.txt1
-rw-r--r--include/charconv610
-rw-r--r--include/module.modulemap4
-rw-r--r--src/charconv.cpp233
-rw-r--r--test/libcxx/double_include.sh.cpp1
-rw-r--r--test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp30
-rw-r--r--test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp181
-rw-r--r--test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp30
-rw-r--r--test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp80
-rw-r--r--test/support/charconv_test_helpers.h232
10 files changed, 1402 insertions, 0 deletions
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index a45b80160..d9def18d7 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -32,6 +32,7 @@ set(files
cerrno
cfenv
cfloat
+ charconv
chrono
cinttypes
ciso646
diff --git a/include/charconv b/include/charconv
new file mode 100644
index 000000000..7cb790e1b
--- /dev/null
+++ b/include/charconv
@@ -0,0 +1,610 @@
+// -*- 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 <type_traits>
+#include <limits>
+#include <stdint.h>
+#include <string.h>
+#include <math.h>
+
+#include <__debug>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 11
+
+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 <typename _Tp, typename = void>
+struct _LIBCPP_HIDDEN __traits_base
+{
+ using type = uint64_t;
+
+#if !defined(_LIBCPP_COMPILER_MSVC)
+ static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
+ {
+ auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12;
+ return __t - (__v < __pow10_64[__t]) + 1;
+ }
+#endif
+
+ static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
+ {
+ return __u64toa(__v, __p);
+ }
+
+ static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_64; }
+};
+
+template <typename _Tp>
+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 int __width(_Tp __v)
+ {
+ auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
+ return __t - (__v < __pow10_32[__t]) + 1;
+ }
+#endif
+
+ static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
+ {
+ return __u32toa(__v, __p);
+ }
+
+ static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_32; }
+};
+
+template <typename _Tp>
+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<unsigned char>::max)();
+}
+
+template <typename _Tp>
+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<unsigned short>::max)();
+}
+
+template <typename _Tp>
+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 <typename _Tp, typename _Up>
+inline _LIBCPP_INLINE_VISIBILITY bool
+__mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
+{
+ return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
+}
+
+template <typename _Tp>
+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 char const*
+ __read(char const* __p, char const* __ep, type& __a, type& __b)
+ {
+ 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 <typename _It1, typename _It2, class _Up>
+ static _LIBCPP_INLINE_VISIBILITY _Up
+ __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
+ {
+ for (; __first1 < __last1; ++__first1, ++__first2)
+ __init = __init + *__first1 * *__first2;
+ return __init;
+ }
+};
+
+} // namespace __itoa
+
+template <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY _Tp
+__complement(_Tp __x)
+{
+ static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
+ return _Tp(~__x + 1);
+}
+
+template <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY auto
+__to_unsigned(_Tp __x)
+{
+ return static_cast<make_unsigned_t<_Tp>>(__x);
+}
+
+template <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY to_chars_result
+__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
+{
+ auto __x = __to_unsigned(__value);
+ if (__value < 0 && __first != __last)
+ {
+ *__first++ = '-';
+ __x = __complement(__x);
+ }
+
+ return __to_chars_itoa(__first, __last, __x, false_type());
+}
+
+template <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY to_chars_result
+__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
+{
+ 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 <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY to_chars_result
+__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
+ true_type)
+{
+ 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 <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY to_chars_result
+__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
+ false_type)
+{
+ if (__base == 10)
+ return __to_chars_itoa(__first, __last, __value, false_type());
+
+ auto __p = __last;
+ while (__p != __first)
+ {
+ auto __c = __value % __base;
+ __value /= __base;
+ *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__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 <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 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 <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY to_chars_result
+to_chars(char* __first, char* __last, _Tp __value, int __base)
+{
+ _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
+ return __to_chars_integral(__first, __last, __value, __base,
+ is_signed<_Tp>());
+}
+
+template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
+{
+ 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 <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY bool
+__in_pattern(_Tp __c)
+{
+ return '0' <= __c && __c <= '9';
+}
+
+struct _LIBCPP_HIDDEN __in_pattern_result
+{
+ bool __ok;
+ int __val;
+
+ explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
+};
+
+template <typename _Tp>
+inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
+__in_pattern(_Tp __c, int __base)
+{
+ 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 <typename _It, typename _Tp, typename _Fn, typename... _Ts>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
+ _Ts... __args)
+{
+ auto __find_non_zero = [](_It __first, _It __last) {
+ 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...);
+ if (__r.ec == errc::result_out_of_range)
+ {
+ for (; __r.ptr != __last; ++__r.ptr)
+ {
+ if (!__in_pattern(*__r.ptr, __args...))
+ break;
+ }
+ }
+
+ return __r;
+}
+
+template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
+{
+ 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 <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
+{
+ using __t = decltype(__to_unsigned(__value));
+ return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
+}
+
+template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
+ int __base)
+{
+ 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 <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
+ int __base)
+{
+ using __t = decltype(__to_unsigned(__value));
+ return __sign_combinator(__first, __last, __value,
+ __from_chars_integral<__t>, __base);
+}
+
+template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 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 <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
+inline _LIBCPP_INLINE_VISIBILITY from_chars_result
+from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
+{
+ _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
+ return __from_chars_integral(__first, __last, __value, __base);
+}
+
+#endif // _LIBCPP_STD_VER > 11
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_CHARCONV
diff --git a/include/module.modulemap b/include/module.modulemap
index 3b66390fb..089505586 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap
@@ -235,6 +235,10 @@ module std [system] {
export *
}
// No submodule for cassert. It fundamentally needs repeated, textual inclusion.
+ module charconv {
+ header "charconv"
+ export *
+ }
module chrono {
header "chrono"
export *
diff --git a/src/charconv.cpp b/src/charconv.cpp
new file mode 100644
index 000000000..ec241db74
--- /dev/null
+++ b/src/charconv.cpp
@@ -0,0 +1,233 @@
+//===------------------------- 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 <string.h>
+
+_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'};
+
+template <typename T>
+inline _LIBCPP_INLINE_VISIBILITY char*
+append1(char* buffer, T i)
+{
+ *buffer = '0' + static_cast<char>(i);
+ return buffer + 1;
+}
+
+template <typename T>
+inline _LIBCPP_INLINE_VISIBILITY char*
+append2(char* buffer, T i)
+{
+ memcpy(buffer, &cDigitsLut[(i)*2], 2);
+ return buffer + 2;
+}
+
+template <typename T>
+inline _LIBCPP_INLINE_VISIBILITY char*
+append3(char* buffer, T i)
+{
+ return append2(append1(buffer, (i) / 100), (i) % 100);
+}
+
+template <typename T>
+inline _LIBCPP_INLINE_VISIBILITY char*
+append4(char* buffer, T i)
+{
+ return append2(append2(buffer, (i) / 100), (i) % 100);
+}
+
+char*
+__u32toa(uint32_t value, char* buffer)
+{
+ if (value < 10000)
+ {
+ if (value < 100)
+ {
+ if (value < 10)
+ buffer = append1(buffer, value);
+ else
+ buffer = append2(buffer, value);
+ }
+ else
+ {
+ if (value < 1000)
+ buffer = append3(buffer, value);
+ else
+ buffer = append4(buffer, 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)
+ buffer = append1(buffer, b);
+ else
+ buffer = append2(buffer, b);
+ }
+ else
+ {
+ if (value < 10000000)
+ buffer = append3(buffer, b);
+ else
+ buffer = append4(buffer, b);
+ }
+
+ buffer = append4(buffer, c);
+ }
+ else
+ {
+ // value = aabbbbcccc in decimal
+ const uint32_t a = value / 100000000; // 1 to 42
+ value %= 100000000;
+
+ if (a < 10)
+ buffer = append1(buffer, a);
+ else
+ buffer = append2(buffer, a);
+
+ buffer = append4(buffer, value / 10000);
+ buffer = append4(buffer, value % 10000);
+ }
+
+ return buffer;
+}
+
+char*
+__u64toa(uint64_t value, char* buffer)
+{
+ if (value < 100000000)
+ {
+ uint32_t v = static_cast<uint32_t>(value);
+ if (v < 10000)
+ {
+ if (v < 100)
+ {
+ if (v < 10)
+ buffer = append1(buffer, v);
+ else
+ buffer = append2(buffer, v);
+ }
+ else
+ {
+ if (v < 1000)
+ buffer = append3(buffer, v);
+ else
+ buffer = append4(buffer, v);
+ }
+ }
+ else
+ {
+ // value = bbbbcccc
+ const uint32_t b = v / 10000;
+ const uint32_t c = v % 10000;
+
+ if (v < 1000000)
+ {
+ if (v < 100000)
+ buffer = append1(buffer, b);
+ else
+ buffer = append2(buffer, b);
+ }
+ else
+ {
+ if (v < 10000000)
+ buffer = append3(buffer, b);
+ else
+ buffer = append4(buffer, b);
+ }
+
+ buffer = append4(buffer, c);
+ }
+ }
+ else if (value < 10000000000000000)
+ {
+ const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
+ const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
+
+ const uint32_t b0 = v0 / 10000;
+ const uint32_t c0 = v0 % 10000;
+
+ if (v0 < 1000000)
+ {
+ if (v0 < 100000)
+ buffer = append1(buffer, b0);
+ else
+ buffer = append2(buffer, b0);
+ }
+ else
+ {
+ if (v0 < 10000000)
+ buffer = append3(buffer, b0);
+ else
+ buffer = append4(buffer, b0);
+ }
+
+ buffer = append4(buffer, c0);
+ buffer = append4(buffer, v1 / 10000);
+ buffer = append4(buffer, v1 % 10000);
+ }
+ else
+ {
+ const uint32_t a =
+ static_cast<uint32_t>(value / 10000000000000000); // 1 to 1844
+ value %= 10000000000000000;
+
+ if (a < 100)
+ {
+ if (a < 10)
+ buffer = append1(buffer, a);
+ else
+ buffer = append2(buffer, a);
+ }
+ else
+ {
+ if (a < 1000)
+ buffer = append3(buffer, a);
+ else
+ buffer = append4(buffer, a);
+ }
+
+ const uint32_t v0 = static_cast<uint32_t>(value / 100000000);
+ const uint32_t v1 = static_cast<uint32_t>(value % 100000000);
+ buffer = append4(buffer, v0 / 10000);
+ buffer = append4(buffer, v0 % 10000);
+ buffer = append4(buffer, v1 / 10000);
+ buffer = append4(buffer, v1 % 10000);
+ }
+
+ return buffer;
+}
+
+} // namespace __itoa
+
+_LIBCPP_END_NAMESPACE_STD
diff --git a/test/libcxx/double_include.sh.cpp b/test/libcxx/double_include.sh.cpp
index c98cc8065..8d08db4fd 100644
--- a/test/libcxx/double_include.sh.cpp
+++ b/test/libcxx/double_include.sh.cpp
@@ -34,6 +34,7 @@
#include <cerrno>
#include <cfenv>
#include <cfloat>
+#include <charconv>
#include <chrono>
#include <cinttypes>
#include <ciso646>
diff --git a/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp b/test/std/utilities/charconv/charconv.from.chars/integral.bool.fail.cpp
new file mode 100644
index 000000000..d21b638d0
--- /dev/null
+++ b/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, c++11
+// <charconv>
+
+// In
+//
+// from_chars_result from_chars(const char* first, const char* last,
+// Integral& value, int base = 10)
+//
+// Integral cannot be bool.
+
+#include <charconv>
+
+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}}
+}
diff --git a/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp b/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp
new file mode 100644
index 000000000..e5a93b497
--- /dev/null
+++ b/test/std/utilities/charconv/charconv.from.chars/integral.pass.cpp
@@ -0,0 +1,181 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, c++11
+// <charconv>
+
+// from_chars_result from_chars(const char* first, const char* last,
+// Integral& value, int base = 10)
+
+#include "charconv_test_helpers.h"
+
+template <typename T>
+struct test_basics : roundtrip_test_base<T>
+{
+ using roundtrip_test_base<T>::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<T>;
+
+ 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<T>(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 <typename T>
+struct test_signed : roundtrip_test_base<T>
+{
+ using roundtrip_test_base<T>::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<T>;
+
+ 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,
+ // but the string has no digit characters following the sign,
+ char s[] = "- 9+12";
+ r = from_chars(s, s + sizeof(s), x);
+ // no characters match the pattern.
+ assert(r.ptr == s);
+ assert(r.ec == std::errc::invalid_argument);
+ }
+
+ {
+ char s[] = "9+12";
+ r = from_chars(s, 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 + 1);
+ assert(x == 9);
+ }
+
+ {
+ char s[] = "12";
+ r = from_chars(s, s + 2, x);
+ assert(r.ec == std::errc{});
+ // or has the value last if all characters match.
+ assert(r.ptr == s + 2);
+ assert(x == 12);
+ }
+
+ {
+ // '-' is the only sign that may appear
+ char s[] = "+30";
+ // If no characters match the pattern,
+ r = from_chars(s, s + sizeof(s), x);
+ // value is unmodified,
+ assert(x == 12);
+ // the member ptr of the return value is first and
+ assert(r.ptr == s);
+ // the member ec is equal to errc::invalid_argument.
+ assert(r.ec == std::errc::invalid_argument);
+ }
+ }
+};
+
+int
+main()
+{
+ run<test_basics>(integrals);
+ run<test_signed>(all_signed);
+}
diff --git a/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp b/test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp
new file mode 100644
index 000000000..e3d702a3a
--- /dev/null
+++ b/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, c++11
+// <charconv>
+
+// In
+//
+// to_chars_result to_chars(char* first, char* last, Integral value,
+// int base = 10)
+//
+// Integral cannot be bool.
+
+#include <charconv>
+
+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}}
+}
diff --git a/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp b/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp
new file mode 100644
index 000000000..50ca5b190
--- /dev/null
+++ b/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, c++11
+// <charconv>
+
+// to_chars_result to_chars(char* first, char* last, Integral value,
+// int base = 10)
+
+#include "charconv_test_helpers.h"
+
+template <typename T>
+struct test_basics : to_chars_test_base<T>
+{
+ using to_chars_test_base<T>::test;
+ using to_chars_test_base<T>::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<T>;
+
+ test_value(1, b);
+ test_value(xl::lowest(), b);
+ test_value((xl::max)(), b);
+ test_value((xl::max)() / 2, b);
+ }
+ }
+};
+
+template <typename T>
+struct test_signed : to_chars_test_base<T>
+{
+ using to_chars_test_base<T>::test;
+ using to_chars_test_base<T>::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<T>;
+
+ test_value(0, b);
+ test_value(xl::lowest(), b);
+ test_value((xl::max)(), b);
+ }
+ }
+};
+
+int
+main()
+{
+ run<test_basics>(integrals);
+ run<test_signed>(all_signed);
+}
diff --git a/test/support/charconv_test_helpers.h b/test/support/charconv_test_helpers.h
new file mode 100644
index 000000000..df1b00a67
--- /dev/null
+++ b/test/support/charconv_test_helpers.h
@@ -0,0 +1,232 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <charconv>
+#include <cassert>
+#include <limits>
+#include <string.h>
+#include <stdlib.h>
+
+#include "test_macros.h"
+
+#if TEST_STD_VER <= 11
+#error This file requires C++14
+#endif
+
+using std::false_type;
+using std::true_type;
+
+template <typename To, typename From>
+constexpr auto
+is_non_narrowing(From a) -> decltype(To{a}, true_type())
+{
+ return {};
+}
+
+template <typename To>
+constexpr auto
+is_non_narrowing(...) -> false_type
+{
+ return {};
+}
+
+template <typename X, typename T>
+constexpr bool
+_fits_in(T, true_type /* non-narrowing*/, ...)
+{
+ return true;
+}
+
+template <typename X, typename T, typename xl = std::numeric_limits<X>>
+constexpr bool
+_fits_in(T v, false_type, true_type /* T signed*/, true_type /* X signed */)
+{
+ return xl::lowest() <= v && v <= (xl::max)();
+}
+
+template <typename X, typename T, typename xl = std::numeric_limits<X>>
+constexpr bool
+_fits_in(T v, false_type, true_type /* T signed */, false_type /* X unsigned*/)
+{
+ return 0 <= v && std::make_unsigned_t<T>(v) <= (xl::max)();
+}
+
+template <typename X, typename T, typename xl = std::numeric_limits<X>>
+constexpr bool
+_fits_in(T v, false_type, false_type /* T unsigned */, ...)
+{
+ return v <= std::make_unsigned_t<X>((xl::max)());
+}
+
+template <typename X, typename T>
+constexpr bool
+fits_in(T v)
+{
+ return _fits_in<X>(v, is_non_narrowing<X>(v), std::is_signed<T>(),
+ std::is_signed<X>());
+}
+
+template <typename X>
+struct to_chars_test_base
+{
+ template <typename T, size_t N, typename... Ts>
+ 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<X>(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 <typename... Ts>
+ 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:
+ static auto fromchars(char const* p, char const* ep, int base, true_type)
+ {
+ 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)
+ {
+ 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)
+ {
+ return fromchars(p, ep, base, std::is_signed<X>());
+ }
+
+ char buf[100];
+};
+
+template <typename X>
+struct roundtrip_test_base
+{
+ template <typename T, typename... Ts>
+ 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<X>(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<T>::value && v < 0 && std::is_unsigned<X>::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 <typename... T>
+struct type_list
+{
+};
+
+template <typename L1, typename L2>
+struct type_concat;
+
+template <typename... Xs, typename... Ys>
+struct type_concat<type_list<Xs...>, type_list<Ys...>>
+{
+ using type = type_list<Xs..., Ys...>;
+};
+
+template <typename L1, typename L2>
+using concat_t = typename type_concat<L1, L2>::type;
+
+template <typename L1, typename L2>
+constexpr auto concat(L1, L2) -> concat_t<L1, L2>
+{
+ return {};
+}
+
+auto all_signed = type_list<char, signed char, short, int, long, long long>();
+auto all_unsigned = type_list<unsigned char, unsigned short, unsigned int,
+ unsigned long, unsigned long long>();
+auto integrals = concat(all_signed, all_unsigned);
+
+template <template <typename> class Fn, typename... Ts>
+void
+run(type_list<Ts...>)
+{
+ int ls[sizeof...(Ts)] = {(Fn<Ts>{}(), 0)...};
+ (void)ls;
+}
+
+#endif // SUPPORT_CHARCONV_TEST_HELPERS_H