Path: blob/main/contrib/llvm-project/libcxx/include/__charconv/from_chars_integral.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H10#define _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H1112#include <__algorithm/copy_n.h>13#include <__assert>14#include <__charconv/from_chars_result.h>15#include <__charconv/traits.h>16#include <__config>17#include <__memory/addressof.h>18#include <__system_error/errc.h>19#include <__type_traits/enable_if.h>20#include <__type_traits/integral_constant.h>21#include <__type_traits/is_integral.h>22#include <__type_traits/is_unsigned.h>23#include <__type_traits/make_unsigned.h>24#include <limits>2526#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)27# pragma GCC system_header28#endif2930_LIBCPP_PUSH_MACROS31#include <__undef_macros>3233_LIBCPP_BEGIN_NAMESPACE_STD3435#if _LIBCPP_STD_VER >= 173637from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;3839template <typename _It, typename _Tp, typename _Fn, typename... _Ts>40inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result41__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {42using __tl = numeric_limits<_Tp>;43decltype(std::__to_unsigned_like(__value)) __x;4445bool __neg = (__first != __last && *__first == '-');46auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);47switch (__r.ec) {48case errc::invalid_argument:49return {__first, __r.ec};50case errc::result_out_of_range:51return __r;52default:53break;54}5556if (__neg) {57if (__x <= std::__complement(std::__to_unsigned_like(__tl::min()))) {58__x = std::__complement(__x);59std::copy_n(std::addressof(__x), 1, std::addressof(__value));60return __r;61}62} else {63if (__x <= std::__to_unsigned_like(__tl::max())) {64__value = __x;65return __r;66}67}6869return {__r.ptr, errc::result_out_of_range};70}7172template <typename _Tp>73inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __in_pattern(_Tp __c) {74return '0' <= __c && __c <= '9';75}7677struct _LIBCPP_HIDDEN __in_pattern_result {78bool __ok;79int __val;8081explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }82};8384template <typename _Tp>85inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result __in_pattern(_Tp __c, int __base) {86if (__base <= 10)87return {'0' <= __c && __c < '0' + __base, __c - '0'};88else if (std::__in_pattern(__c))89return {true, __c - '0'};90else if ('a' <= __c && __c < 'a' + __base - 10)91return {true, __c - 'a' + 10};92else93return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};94}9596template <typename _It, typename _Tp, typename _Fn, typename... _Ts>97inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result98__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {99auto __find_non_zero = [](_It __firstit, _It __lastit) {100for (; __firstit != __lastit; ++__firstit)101if (*__firstit != '0')102break;103return __firstit;104};105106auto __p = __find_non_zero(__first, __last);107if (__p == __last || !std::__in_pattern(*__p, __args...)) {108if (__p == __first)109return {__first, errc::invalid_argument};110else {111__value = 0;112return {__p, {}};113}114}115116auto __r = __f(__p, __last, __value, __args...);117if (__r.ec == errc::result_out_of_range) {118for (; __r.ptr != __last; ++__r.ptr) {119if (!std::__in_pattern(*__r.ptr, __args...))120break;121}122}123124return __r;125}126127template <typename _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>128inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result129__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {130using __tx = __itoa::__traits<_Tp>;131using __output_type = typename __tx::type;132133return std::__subject_seq_combinator(134__first, __last, __value, [](const char* __f, const char* __l, _Tp& __val) -> from_chars_result {135__output_type __a, __b;136auto __p = __tx::__read(__f, __l, __a, __b);137if (__p == __l || !std::__in_pattern(*__p)) {138__output_type __m = numeric_limits<_Tp>::max();139if (__m >= __a && __m - __a >= __b) {140__val = __a + __b;141return {__p, {}};142}143}144return {__p, errc::result_out_of_range};145});146}147148template <typename _Tp, __enable_if_t<is_signed<_Tp>::value, int> = 0>149inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result150__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {151using __t = decltype(std::__to_unsigned_like(__value));152return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);153}154155/*156// Code used to generate __from_chars_log2f_lut.157#include <cmath>158#include <format>159#include <iostream>160161int main() {162for (int i = 2; i <= 36; ++i)163std::cout << std::format("{},\n", log2f(i));164}165*/166/// log2f table for bases [2, 36].167inline constexpr float __from_chars_log2f_lut[35] = {1681, 1.5849625, 2, 2.321928, 2.5849626, 2.807355, 3, 3.169925, 3.321928,1693.4594316, 3.5849626, 3.7004397, 3.807355, 3.9068906, 4, 4.087463, 4.169925, 4.2479277,1704.321928, 4.3923173, 4.4594316, 4.523562, 4.5849624, 4.643856, 4.70044, 4.7548876, 4.807355,1714.857981, 4.9068904, 4.9541965, 5, 5.044394, 5.087463, 5.129283, 5.169925};172173template <typename _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>174inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result175__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {176if (__base == 10)177return std::__from_chars_atoi(__first, __last, __value);178179return std::__subject_seq_combinator(180__first,181__last,182__value,183[](const char* __p, const char* __lastp, _Tp& __val, int __b) -> from_chars_result {184using __tl = numeric_limits<_Tp>;185// __base is always between 2 and 36 inclusive.186auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2];187_Tp __x = __in_pattern(*__p++, __b).__val, __y = 0;188189for (int __i = 1; __p != __lastp; ++__i, ++__p) {190if (auto __c = __in_pattern(*__p, __b)) {191if (__i < __digits - 1)192__x = __x * __b + __c.__val;193else {194if (!__itoa::__mul_overflowed(__x, __b, __x))195++__p;196__y = __c.__val;197break;198}199} else200break;201}202203if (__p == __lastp || !__in_pattern(*__p, __b)) {204if (__tl::max() - __x >= __y) {205__val = __x + __y;206return {__p, {}};207}208}209return {__p, errc::result_out_of_range};210},211__base);212}213214template <typename _Tp, __enable_if_t<is_signed<_Tp>::value, int> = 0>215inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result216__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {217using __t = decltype(std::__to_unsigned_like(__value));218return std::__sign_combinator(__first, __last, __value, __from_chars_integral<__t>, __base);219}220221template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>222inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result223from_chars(const char* __first, const char* __last, _Tp& __value) {224return std::__from_chars_atoi(__first, __last, __value);225}226227template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>228inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result229from_chars(const char* __first, const char* __last, _Tp& __value, int __base) {230_LIBCPP_ASSERT_UNCATEGORIZED(2 <= __base && __base <= 36, "base not in [2, 36]");231return std::__from_chars_integral(__first, __last, __value, __base);232}233#endif // _LIBCPP_STD_VER >= 17234235_LIBCPP_END_NAMESPACE_STD236237_LIBCPP_POP_MACROS238239#endif // _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H240241242