Skip to content

Commit

Permalink
[NFC][libc++] Formats granularized charconv.
Browse files Browse the repository at this point in the history
Reviewed By: #libc, Mordante

Differential Revision: https://reviews.llvm.org/D146153
  • Loading branch information
mordante committed Mar 16, 2023
1 parent b0f9206 commit 866fbb8
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 432 deletions.
31 changes: 9 additions & 22 deletions libcxx/include/__charconv/chars_format.h
Expand Up @@ -21,35 +21,22 @@ _LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17

enum class _LIBCPP_ENUM_VIS chars_format
{
scientific = 0x1,
fixed = 0x2,
hex = 0x4,
general = fixed | scientific
};

inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format
operator~(chars_format __x) {
enum class _LIBCPP_ENUM_VIS chars_format { scientific = 0x1, fixed = 0x2, hex = 0x4, general = fixed | scientific };

inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator~(chars_format __x) {
return chars_format(~std::__to_underlying(__x));
}

inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format
operator&(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) &
std::__to_underlying(__y));
inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator&(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) & std::__to_underlying(__y));
}

inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format
operator|(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) |
std::__to_underlying(__y));
inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator|(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) | std::__to_underlying(__y));
}

inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format
operator^(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) ^
std::__to_underlying(__y));
inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator^(chars_format __x, chars_format __y) {
return chars_format(std::__to_underlying(__x) ^ std::__to_underlying(__y));
}

inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 chars_format&
Expand Down
293 changes: 128 additions & 165 deletions libcxx/include/__charconv/from_chars_integral.h
Expand Up @@ -38,150 +38,125 @@ from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;

template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
{
using __tl = numeric_limits<_Tp>;
decltype(std::__to_unsigned_like(__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 <= std::__complement(std::__to_unsigned_like(__tl::min())))
{
__x = std::__complement(__x);
std::copy_n(std::addressof(__x), 1, std::addressof(__value));
return __r;
}
__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {
using __tl = numeric_limits<_Tp>;
decltype(std::__to_unsigned_like(__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 <= std::__complement(std::__to_unsigned_like(__tl::min()))) {
__x = std::__complement(__x);
std::copy_n(std::addressof(__x), 1, std::addressof(__value));
return __r;
}
else
{
if (__x <= std::__to_unsigned_like(__tl::max()))
{
__value = __x;
return __r;
}
} else {
if (__x <= std::__to_unsigned_like(__tl::max())) {
__value = __x;
return __r;
}
}

return {__r.ptr, errc::result_out_of_range};
return {__r.ptr, errc::result_out_of_range};
}

template <typename _Tp>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
__in_pattern(_Tp __c)
{
return '0' <= __c && __c <= '9';
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __in_pattern(_Tp __c) {
return '0' <= __c && __c <= '9';
}

struct _LIBCPP_HIDDEN __in_pattern_result
{
bool __ok;
int __val;
struct _LIBCPP_HIDDEN __in_pattern_result {
bool __ok;
int __val;

explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
};

template <typename _Tp>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result
__in_pattern(_Tp __c, int __base)
{
if (__base <= 10)
return {'0' <= __c && __c < '0' + __base, __c - '0'};
else if (std::__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};
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result __in_pattern(_Tp __c, int __base) {
if (__base <= 10)
return {'0' <= __c && __c < '0' + __base, __c - '0'};
else if (std::__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_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
_Ts... __args)
{
auto __find_non_zero = [](_It __firstit, _It __lastit) {
for (; __firstit != __lastit; ++__firstit)
if (*__firstit != '0')
break;
return __firstit;
};

auto __p = __find_non_zero(__first, __last);
if (__p == __last || !std::__in_pattern(*__p, __args...))
{
if (__p == __first)
return {__first, errc::invalid_argument};
else
{
__value = 0;
return {__p, {}};
}
__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {
auto __find_non_zero = [](_It __firstit, _It __lastit) {
for (; __firstit != __lastit; ++__firstit)
if (*__firstit != '0')
break;
return __firstit;
};

auto __p = __find_non_zero(__first, __last);
if (__p == __last || !std::__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 (!std::__in_pattern(*__r.ptr, __args...))
break;
}
auto __r = __f(__p, __last, __value, __args...);
if (__r.ec == errc::result_out_of_range) {
for (; __r.ptr != __last; ++__r.ptr) {
if (!std::__in_pattern(*__r.ptr, __args...))
break;
}
}

return __r;
return __r;
}

template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI 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 std::__subject_seq_combinator(
__first, __last, __value,
[](const char* __f, const char* __l,
_Tp& __val) -> from_chars_result {
__output_type __a, __b;
auto __p = __tx::__read(__f, __l, __a, __b);
if (__p == __l || !std::__in_pattern(*__p))
{
__output_type __m = numeric_limits<_Tp>::max();
if (__m >= __a && __m - __a >= __b)
{
__val = __a + __b;
return {__p, {}};
}
}
return {__p, errc::result_out_of_range};
});
__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {
using __tx = __itoa::__traits<_Tp>;
using __output_type = typename __tx::type;

return std::__subject_seq_combinator(
__first, __last, __value, [](const char* __f, const char* __l, _Tp& __val) -> from_chars_result {
__output_type __a, __b;
auto __p = __tx::__read(__f, __l, __a, __b);
if (__p == __l || !std::__in_pattern(*__p)) {
__output_type __m = numeric_limits<_Tp>::max();
if (__m >= __a && __m - __a >= __b) {
__val = __a + __b;
return {__p, {}};
}
}
return {__p, errc::result_out_of_range};
});
}

template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
{
using __t = decltype(std::__to_unsigned_like(__value));
return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {
using __t = decltype(std::__to_unsigned_like(__value));
return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
}


/*
// Code used to generate __from_chars_log2f_lut.
#include <cmath>
#include <iostream>
#include <format>
#include <iostream>
int main() {
for (int i = 2; i <= 36; ++i)
Expand All @@ -197,75 +172,63 @@ inline constexpr float __from_chars_log2f_lut[35] = {

template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
int __base)
{
if (__base == 10)
return std::__from_chars_atoi(__first, __last, __value);

return std::__subject_seq_combinator(
__first, __last, __value,
[](const char* __p, const char* __lastp, _Tp& __val,
int __b) -> from_chars_result {
using __tl = numeric_limits<_Tp>;
// __base is always between 2 and 36 inclusive.
auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2];
_Tp __x = __in_pattern(*__p++, __b).__val, __y = 0;

for (int __i = 1; __p != __lastp; ++__i, ++__p)
{
if (auto __c = __in_pattern(*__p, __b))
{
if (__i < __digits - 1)
__x = __x * __b + __c.__val;
else
{
if (!__itoa::__mul_overflowed(__x, __b, __x))
++__p;
__y = __c.__val;
break;
}
}
else
break;
}
__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {
if (__base == 10)
return std::__from_chars_atoi(__first, __last, __value);

if (__p == __lastp || !__in_pattern(*__p, __b))
{
if (__tl::max() - __x >= __y)
{
__val = __x + __y;
return {__p, {}};
}
return std::__subject_seq_combinator(
__first,
__last,
__value,
[](const char* __p, const char* __lastp, _Tp& __val, int __b) -> from_chars_result {
using __tl = numeric_limits<_Tp>;
// __base is always between 2 and 36 inclusive.
auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2];
_Tp __x = __in_pattern(*__p++, __b).__val, __y = 0;

for (int __i = 1; __p != __lastp; ++__i, ++__p) {
if (auto __c = __in_pattern(*__p, __b)) {
if (__i < __digits - 1)
__x = __x * __b + __c.__val;
else {
if (!__itoa::__mul_overflowed(__x, __b, __x))
++__p;
__y = __c.__val;
break;
}
return {__p, errc::result_out_of_range};
},
__base);
} else
break;
}

if (__p == __lastp || !__in_pattern(*__p, __b)) {
if (__tl::max() - __x >= __y) {
__val = __x + __y;
return {__p, {}};
}
}
return {__p, errc::result_out_of_range};
},
__base);
}

template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
int __base)
{
using __t = decltype(std::__to_unsigned_like(__value));
return std::__sign_combinator(__first, __last, __value,
__from_chars_integral<__t>, __base);
__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {
using __t = decltype(std::__to_unsigned_like(__value));
return std::__sign_combinator(__first, __last, __value, __from_chars_integral<__t>, __base);
}

template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
from_chars(const char* __first, const char* __last, _Tp& __value)
{
return std::__from_chars_atoi(__first, __last, __value);
from_chars(const char* __first, const char* __last, _Tp& __value) {
return std::__from_chars_atoi(__first, __last, __value);
}

template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI 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 std::__from_chars_integral(__first, __last, __value, __base);
from_chars(const char* __first, const char* __last, _Tp& __value, int __base) {
_LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
return std::__from_chars_integral(__first, __last, __value, __base);
}
#endif // _LIBCPP_STD_VER >= 17

Expand Down

0 comments on commit 866fbb8

Please sign in to comment.