1#ifndef C4_CHARCONV_HPP_
2#define C4_CHARCONV_HPP_
8#include "c4/language.hpp"
15#include "c4/config.hpp"
17#include "c4/std/std_fwd.hpp"
18#include "c4/memory_util.hpp"
20#ifndef C4CORE_NO_FAST_FLOAT
23# if (C4_MSVC_VERSION >= C4_MSVC_VERSION_2019)
25# define C4CORE_HAVE_STD_TOCHARS 1
26# define C4CORE_HAVE_STD_FROMCHARS 0
27# define C4CORE_HAVE_FAST_FLOAT 1
29# define C4CORE_HAVE_STD_TOCHARS 0
30# define C4CORE_HAVE_STD_FROMCHARS 0
31# define C4CORE_HAVE_FAST_FLOAT 1
34# if __has_include(<charconv>)
36# define C4CORE_HAVE_STD_TOCHARS 1
37# define C4CORE_HAVE_STD_FROMCHARS 0
38# define C4CORE_HAVE_FAST_FLOAT 1
40# define C4CORE_HAVE_STD_TOCHARS 0
41# define C4CORE_HAVE_STD_FROMCHARS 0
42# define C4CORE_HAVE_FAST_FLOAT 1
46# define C4CORE_HAVE_STD_TOCHARS 0
47# define C4CORE_HAVE_STD_FROMCHARS 0
48# define C4CORE_HAVE_FAST_FLOAT 1
50# if C4CORE_HAVE_FAST_FLOAT
51# include "c4/ext/fast_float.hpp"
54# define C4CORE_HAVE_FAST_FLOAT 0
56# if (C4_MSVC_VERSION >= C4_MSVC_VERSION_2019)
58# define C4CORE_HAVE_STD_TOCHARS 1
59# define C4CORE_HAVE_STD_FROMCHARS 1
61# define C4CORE_HAVE_STD_TOCHARS 0
62# define C4CORE_HAVE_STD_FROMCHARS 0
65# if __has_include(<charconv>)
67# if defined(__cpp_lib_to_chars)
68# define C4CORE_HAVE_STD_TOCHARS 1
69# define C4CORE_HAVE_STD_FROMCHARS 1
71# define C4CORE_HAVE_STD_TOCHARS 0
72# define C4CORE_HAVE_STD_FROMCHARS 0
75# define C4CORE_HAVE_STD_TOCHARS 0
76# define C4CORE_HAVE_STD_FROMCHARS 0
80# define C4CORE_HAVE_STD_TOCHARS 0
81# define C4CORE_HAVE_STD_FROMCHARS 0
82# define C4CORE_HAVE_FAST_FLOAT 0
86#if !C4CORE_HAVE_STD_FROMCHARS
91#if defined(_MSC_VER) && !defined(__clang__)
93# pragma warning(disable: 4996)
94# if C4_MSVC_VERSION != C4_MSVC_VERSION_2017
95# pragma warning(disable: 4800)
97#elif defined(__clang__)
98# pragma clang diagnostic push
99# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
100# pragma clang diagnostic ignored "-Wformat-nonliteral"
101# pragma clang diagnostic ignored "-Wdouble-promotion"
102# pragma clang diagnostic ignored "-Wold-style-cast"
103#elif defined(__GNUC__)
104# pragma GCC diagnostic push
105# pragma GCC diagnostic ignored "-Wformat-nonliteral"
106# pragma GCC diagnostic ignored "-Wdouble-promotion"
107# pragma GCC diagnostic ignored "-Wuseless-cast"
108# pragma GCC diagnostic ignored "-Wold-style-cast"
111#if defined(__clang__)
112#define C4_NO_UBSAN_IOVRFLW __attribute__((no_sanitize("signed-integer-overflow")))
113#elif defined(__GNUC__)
115#define C4_NO_UBSAN_IOVRFLW __attribute__((no_sanitize("signed-integer-overflow")))
117#define C4_NO_UBSAN_IOVRFLW
120#define C4_NO_UBSAN_IOVRFLW
172#if C4CORE_HAVE_STD_TOCHARS
174typedef enum : std::underlying_type<std::chars_format>::type {
176 FTOA_FLOAT =
static_cast<std::underlying_type<std::chars_format>::type
>(std::chars_format::fixed),
178 FTOA_SCIENT =
static_cast<std::underlying_type<std::chars_format>::type
>(std::chars_format::scientific),
180 FTOA_FLEX =
static_cast<std::underlying_type<std::chars_format>::type
>(std::chars_format::general),
182 FTOA_HEXA =
static_cast<std::underlying_type<std::chars_format>::type
>(std::chars_format::hex),
203struct is_fixed_length
207 value_i = (std::is_integral<T>::value
208 && (std::is_same<T, int8_t>::value
209 || std::is_same<T, int16_t>::value
210 || std::is_same<T, int32_t>::value
211 || std::is_same<T, int64_t>::value)),
213 value_u = (std::is_integral<T>::value
214 && (std::is_same<T, uint8_t>::value
215 || std::is_same<T, uint16_t>::value
216 || std::is_same<T, uint32_t>::value
217 || std::is_same<T, uint64_t>::value)),
219 value = value_i || value_u,
229#if defined(_MSC_VER) && !defined(__clang__)
230# pragma warning(push)
231#elif defined(__clang__)
232# pragma clang diagnostic push
233#elif defined(__GNUC__)
234# pragma GCC diagnostic push
235# pragma GCC diagnostic ignored "-Wconversion"
237# pragma GCC diagnostic ignored "-Wnull-dereference"
257template<
size_t num_
bytes,
bool is_
signed>
struct charconv_digits_;
258template<
class T>
using charconv_digits = charconv_digits_<
sizeof(T), std::is_signed<T>::value>;
260template<>
struct charconv_digits_<1u, true>
263 maxdigits_bin = 1 + 2 + 8,
264 maxdigits_oct = 1 + 2 + 3,
265 maxdigits_dec = 1 + 3,
266 maxdigits_hex = 1 + 2 + 2,
267 maxdigits_bin_nopfx = 8,
268 maxdigits_oct_nopfx = 3,
269 maxdigits_dec_nopfx = 3,
270 maxdigits_hex_nopfx = 2,
273 static constexpr csubstr min_value_dec() noexcept {
return csubstr(
"128"); }
274 static constexpr csubstr min_value_hex() noexcept {
return csubstr(
"80"); }
275 static constexpr csubstr min_value_oct() noexcept {
return csubstr(
"200"); }
276 static constexpr csubstr min_value_bin() noexcept {
return csubstr(
"10000000"); }
277 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"127"); }
278 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 3) || (str.len == 3 && str.str[0] <=
'1')); }
280template<>
struct charconv_digits_<1u, false>
283 maxdigits_bin = 2 + 8,
284 maxdigits_oct = 2 + 3,
286 maxdigits_hex = 2 + 2,
287 maxdigits_bin_nopfx = 8,
288 maxdigits_oct_nopfx = 3,
289 maxdigits_dec_nopfx = 3,
290 maxdigits_hex_nopfx = 2,
292 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"255"); }
293 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 3) || (str.len == 3 && str.str[0] <=
'3')); }
295template<>
struct charconv_digits_<2u, true>
298 maxdigits_bin = 1 + 2 + 16,
299 maxdigits_oct = 1 + 2 + 6,
300 maxdigits_dec = 1 + 5,
301 maxdigits_hex = 1 + 2 + 4,
302 maxdigits_bin_nopfx = 16,
303 maxdigits_oct_nopfx = 6,
304 maxdigits_dec_nopfx = 5,
305 maxdigits_hex_nopfx = 4,
308 static constexpr csubstr min_value_dec() noexcept {
return csubstr(
"32768"); }
309 static constexpr csubstr min_value_hex() noexcept {
return csubstr(
"8000"); }
310 static constexpr csubstr min_value_oct() noexcept {
return csubstr(
"100000"); }
311 static constexpr csubstr min_value_bin() noexcept {
return csubstr(
"1000000000000000"); }
312 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"32767"); }
313 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !(str.len < 6); }
315template<>
struct charconv_digits_<2u, false>
318 maxdigits_bin = 2 + 16,
319 maxdigits_oct = 2 + 6,
321 maxdigits_hex = 2 + 4,
322 maxdigits_bin_nopfx = 16,
323 maxdigits_oct_nopfx = 6,
324 maxdigits_dec_nopfx = 6,
325 maxdigits_hex_nopfx = 4,
327 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"65535"); }
328 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 6) || (str.len == 6 && str.str[0] <=
'1')); }
330template<>
struct charconv_digits_<4u, true>
333 maxdigits_bin = 1 + 2 + 32,
334 maxdigits_oct = 1 + 2 + 11,
335 maxdigits_dec = 1 + 10,
336 maxdigits_hex = 1 + 2 + 8,
337 maxdigits_bin_nopfx = 32,
338 maxdigits_oct_nopfx = 11,
339 maxdigits_dec_nopfx = 10,
340 maxdigits_hex_nopfx = 8,
343 static constexpr csubstr min_value_dec() noexcept {
return csubstr(
"2147483648"); }
344 static constexpr csubstr min_value_hex() noexcept {
return csubstr(
"80000000"); }
345 static constexpr csubstr min_value_oct() noexcept {
return csubstr(
"20000000000"); }
346 static constexpr csubstr min_value_bin() noexcept {
return csubstr(
"10000000000000000000000000000000"); }
347 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"2147483647"); }
348 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 11) || (str.len == 11 && str.str[0] <=
'1')); }
350template<>
struct charconv_digits_<4u, false>
353 maxdigits_bin = 2 + 32,
354 maxdigits_oct = 2 + 11,
356 maxdigits_hex = 2 + 8,
357 maxdigits_bin_nopfx = 32,
358 maxdigits_oct_nopfx = 11,
359 maxdigits_dec_nopfx = 10,
360 maxdigits_hex_nopfx = 8,
362 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"4294967295"); }
363 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 11) || (str.len == 11 && str.str[0] <=
'3')); }
365template<>
struct charconv_digits_<8u, true>
368 maxdigits_bin = 1 + 2 + 64,
369 maxdigits_oct = 1 + 2 + 22,
370 maxdigits_dec = 1 + 19,
371 maxdigits_hex = 1 + 2 + 16,
372 maxdigits_bin_nopfx = 64,
373 maxdigits_oct_nopfx = 22,
374 maxdigits_dec_nopfx = 19,
375 maxdigits_hex_nopfx = 16,
377 static constexpr csubstr min_value_dec() noexcept {
return csubstr(
"9223372036854775808"); }
378 static constexpr csubstr min_value_hex() noexcept {
return csubstr(
"8000000000000000"); }
379 static constexpr csubstr min_value_oct() noexcept {
return csubstr(
"1000000000000000000000"); }
380 static constexpr csubstr min_value_bin() noexcept {
return csubstr(
"1000000000000000000000000000000000000000000000000000000000000000"); }
381 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"9223372036854775807"); }
382 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !(str.len < 22); }
384template<>
struct charconv_digits_<8u, false>
387 maxdigits_bin = 2 + 64,
388 maxdigits_oct = 2 + 22,
390 maxdigits_hex = 2 + 16,
391 maxdigits_bin_nopfx = 64,
392 maxdigits_oct_nopfx = 22,
393 maxdigits_dec_nopfx = 20,
394 maxdigits_hex_nopfx = 16,
396 static constexpr csubstr max_value_dec() noexcept {
return csubstr(
"18446744073709551615"); }
397 static constexpr bool is_oct_overflow(csubstr str)
noexcept {
return !((str.len < 22) || (str.len == 22 && str.str[0] <=
'1')); }
402#define c4append_(c) { if C4_LIKELY(pos < buf.len) { buf.str[pos++] = static_cast<char>(c); } else { ++pos; } }
425C4_CONSTEXPR14 C4_ALWAYS_INLINE
427 ->
typename std::enable_if<
sizeof(T) == 1u,
unsigned>::type
429 C4_STATIC_ASSERT(std::is_integral<T>::value);
431 return ((v >= 100) ? 3u : ((v >= 10) ? 2u : 1u));
436C4_CONSTEXPR14 C4_ALWAYS_INLINE
438 ->
typename std::enable_if<
sizeof(T) == 2u,
unsigned>::type
440 C4_STATIC_ASSERT(std::is_integral<T>::value);
442 return ((v >= 10000) ? 5u : (v >= 1000) ? 4u : (v >= 100) ? 3u : (v >= 10) ? 2u : 1u);
447C4_CONSTEXPR14 C4_ALWAYS_INLINE
449 ->
typename std::enable_if<
sizeof(T) == 4u,
unsigned>::type
451 C4_STATIC_ASSERT(std::is_integral<T>::value);
453 return ((v >= 1000000000) ? 10u : (v >= 100000000) ? 9u : (v >= 10000000) ? 8u :
454 (v >= 1000000) ? 7u : (v >= 100000) ? 6u : (v >= 10000) ? 5u :
455 (v >= 1000) ? 4u : (v >= 100) ? 3u : (v >= 10) ? 2u : 1u);
460C4_CONSTEXPR14 C4_ALWAYS_INLINE
462 ->
typename std::enable_if<
sizeof(T) == 8u,
unsigned>::type
466 C4_STATIC_ASSERT(std::is_integral<T>::value);
470 if(v >= 100000000000000)
472 if(v >= 100000000000000000)
474 if((
typename std::make_unsigned<T>::type)v >= 10000000000000000000u)
477 return (v >= 1000000000000000000) ? 19u : 18u;
479 else if(v >= 10000000000000000)
485 return(v >= 1000000000000000) ? 16u : 15u;
488 else if(v >= 1000000000000)
490 return (v >= 10000000000000) ? 14u : 13u;
492 else if(v >= 100000000000)
498 return(v >= 10000000000) ? 11u : 10u;
504 return (v >= 100000000) ? 9u : 8u;
505 else if(v >= 1000000)
508 return (v >= 100000) ? 6u : 5u;
512 return (v >= 1000) ? 4u : 3u;
516 return (v >= 10) ? 2u : 1u;
523C4_CONSTEXPR14 C4_ALWAYS_INLINE
unsigned digits_hex(T v)
noexcept
525 C4_STATIC_ASSERT(std::is_integral<T>::value);
527 return v ? 1u + (msb((
typename std::make_unsigned<T>::type)v) >> 2u) : 1u;
532C4_CONSTEXPR14 C4_ALWAYS_INLINE
unsigned digits_bin(T v)
noexcept
534 C4_STATIC_ASSERT(std::is_integral<T>::value);
536 return v ? 1u + msb((
typename std::make_unsigned<T>::type)v) : 1u;
541C4_CONSTEXPR14 C4_ALWAYS_INLINE
unsigned digits_oct(T v_)
noexcept
544 C4_STATIC_ASSERT(std::is_integral<T>::value);
546 using U =
typename std::conditional<
sizeof(T) <=
sizeof(
unsigned),
548 typename std::make_unsigned<T>::type>::type;
580C4_INLINE_CONSTEXPR
const char hexchars[] =
"0123456789abcdef";
581C4_INLINE_CONSTEXPR
const char digits0099[] =
582 "0001020304050607080910111213141516171819"
583 "2021222324252627282930313233343536373839"
584 "4041424344454647484950515253545556575859"
585 "6061626364656667686970717273747576777879"
586 "8081828384858687888990919293949596979899";
590C4_SUPPRESS_WARNING_GCC_PUSH
591C4_SUPPRESS_WARNING_GCC(
"-Warray-bounds")
592#if (defined(__GNUC__) && (__GNUC__ >= 7))
593C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
607C4_HOT C4_ALWAYS_INLINE
610 C4_STATIC_ASSERT(std::is_integral<T>::value);
612 C4_ASSERT(buf.len >= digits_v);
619 const auto num = (v - quo * T(100)) << 1u;
621 buf.str[--digits_v] = detail::digits0099[num + 1];
622 buf.str[--digits_v] = detail::digits0099[num];
626 C4_ASSERT(digits_v == 2);
627 const auto num = v << 1u;
628 buf.str[1] = detail::digits0099[num + 1];
629 buf.str[0] = detail::digits0099[num];
633 C4_ASSERT(digits_v == 1);
634 buf.str[0] = (char)(
'0' + v);
640C4_HOT C4_ALWAYS_INLINE
643 C4_STATIC_ASSERT(std::is_integral<T>::value);
645 C4_ASSERT(buf.len >= digits_v);
648 buf.str[--digits_v] = detail::hexchars[v & T(15)];
651 C4_ASSERT(digits_v == 0);
656C4_HOT C4_ALWAYS_INLINE
659 C4_STATIC_ASSERT(std::is_integral<T>::value);
661 C4_ASSERT(buf.len >= digits_v);
664 buf.str[--digits_v] = (char)(
'0' + (v & T(7)));
667 C4_ASSERT(digits_v == 0);
672C4_HOT C4_ALWAYS_INLINE
675 C4_STATIC_ASSERT(std::is_integral<T>::value);
677 C4_ASSERT(buf.len >= digits_v);
680 buf.str[--digits_v] = (char)(
'0' + (v & T(1)));
683 C4_ASSERT(digits_v == 0);
710 C4_STATIC_ASSERT(std::is_integral<T>::value);
713 if C4_LIKELY(buf.len >= digits)
729 C4_STATIC_ASSERT(std::is_integral<T>::value);
732 if C4_LIKELY(buf.len >= digits)
748 C4_STATIC_ASSERT(std::is_integral<T>::value);
751 if C4_LIKELY(buf.len >= digits)
767 C4_STATIC_ASSERT(std::is_integral<T>::value);
770 C4_ASSERT(digits > 0);
771 if C4_LIKELY(buf.len >= digits)
779template<
class U>
using NumberWriter = size_t (*)(
substr, U);
780template<
class T, NumberWriter<T> writer>
781size_t write_num_digits(
substr buf, T v,
size_t num_digits)
noexcept
783 C4_STATIC_ASSERT(std::is_integral<T>::value);
784 const size_t ret = writer(buf, v);
785 if(ret >= num_digits)
787 else if(ret >= buf.len || num_digits > buf.len)
789 C4_ASSERT(num_digits >= ret);
790 const size_t delta =
static_cast<size_t>(num_digits - ret);
791 C4_ASSERT(ret + delta <= buf.len);
793 memmove(buf.str + delta, buf.str, ret);
795 memset(buf.str,
'0', delta);
808 return detail::write_num_digits<T, &write_dec<T>>(buf, val, num_digits);
817 return detail::write_num_digits<T, &write_hex<T>>(buf, val, num_digits);
826 return detail::write_num_digits<T, &write_bin<T>>(buf, val, num_digits);
835 return detail::write_num_digits<T, &write_oct<T>>(buf, val, num_digits);
840C4_SUPPRESS_WARNING_GCC_POP
848C4_SUPPRESS_WARNING_MSVC_PUSH
849C4_SUPPRESS_WARNING_MSVC(4365)
870 C4_STATIC_ASSERT(std::is_integral<I>::value);
871 C4_ASSERT(!s.empty());
875 if C4_UNLIKELY(c <
'0' || c >
'9')
877 *v = ((*v) * I(10)) + (I(c) - I(
'0'));
898 C4_STATIC_ASSERT(std::is_integral<I>::value);
899 C4_ASSERT(!s.empty());
904 if(c >=
'0' && c <=
'9')
906 else if(c >=
'a' && c <=
'f')
907 cv = I(10) + (I(c) - I(
'a'));
908 else if(c >=
'A' && c <=
'F')
909 cv = I(10) + (I(c) - I(
'A'));
912 *v = ((*v) * I(16)) + cv;
933 C4_STATIC_ASSERT(std::is_integral<I>::value);
934 C4_ASSERT(!s.empty());
963 C4_STATIC_ASSERT(std::is_integral<I>::value);
964 C4_ASSERT(!s.empty());
968 if C4_UNLIKELY(c <
'0' || c >
'7')
970 *v = ((*v) * I(8)) + (I(c) - I(
'0'));
977C4_SUPPRESS_WARNING_MSVC_POP
984C4_SUPPRESS_WARNING_GCC_WITH_PUSH(
"-Wswitch-default")
988inline size_t _itoa2buf(
substr buf,
size_t pos,
csubstr val)
noexcept
990 C4_ASSERT(pos < buf.len);
991 C4_ASSERT(pos + val.len <= buf.len);
992 C4_ASSERT(val.len > 0);
993 memcpy(buf.str + pos, val.str, val.len);
994 return pos + val.len;
996inline size_t _itoa2bufwithdigits(
substr buf,
size_t pos,
size_t num_digits,
csubstr val)
noexcept
998 num_digits = num_digits > val.len ? num_digits - val.len : 0;
999 C4_ASSERT(num_digits + val.len <= buf.len);
1000 for(
size_t i = 0; i < num_digits; ++i)
1002 return detail::_itoa2buf(buf, pos, val);
1005C4_NO_INLINE
size_t _itoadec2buf(
substr buf)
noexcept
1007 using digits_type = detail::charconv_digits<I>;
1008 if C4_UNLIKELY(buf.len < digits_type::maxdigits_dec)
1009 return digits_type::maxdigits_dec;
1011 return detail::_itoa2buf(buf, 1, digits_type::min_value_dec());
1014C4_NO_INLINE
size_t _itoa2buf(
substr buf, I radix)
noexcept
1016 using digits_type = detail::charconv_digits<I>;
1018 if C4_LIKELY(buf.len > 0)
1019 buf.str[pos++] =
'-';
1023 if C4_UNLIKELY(buf.len < digits_type::maxdigits_dec)
1024 return digits_type::maxdigits_dec;
1025 pos =_itoa2buf(buf, pos, digits_type::min_value_dec());
1028 if C4_UNLIKELY(buf.len < digits_type::maxdigits_hex)
1029 return digits_type::maxdigits_hex;
1030 buf.str[pos++] =
'0';
1031 buf.str[pos++] =
'x';
1032 pos = _itoa2buf(buf, pos, digits_type::min_value_hex());
1035 if C4_UNLIKELY(buf.len < digits_type::maxdigits_bin)
1036 return digits_type::maxdigits_bin;
1037 buf.str[pos++] =
'0';
1038 buf.str[pos++] =
'b';
1039 pos = _itoa2buf(buf, pos, digits_type::min_value_bin());
1042 if C4_UNLIKELY(buf.len < digits_type::maxdigits_oct)
1043 return digits_type::maxdigits_oct;
1044 buf.str[pos++] =
'0';
1045 buf.str[pos++] =
'o';
1046 pos = _itoa2buf(buf, pos, digits_type::min_value_oct());
1052C4_NO_INLINE
size_t _itoa2buf(
substr buf, I radix,
size_t num_digits)
noexcept
1054 using digits_type = detail::charconv_digits<I>;
1056 size_t needed_digits = 0;
1057 if C4_LIKELY(buf.len > 0)
1058 buf.str[pos++] =
'-';
1063 needed_digits = num_digits+1 > digits_type::maxdigits_dec ? num_digits+1 : digits_type::maxdigits_dec;
1064 if C4_UNLIKELY(buf.len < needed_digits)
1065 return needed_digits;
1066 pos = _itoa2bufwithdigits(buf, pos, num_digits, digits_type::min_value_dec());
1070 needed_digits = num_digits+3 > digits_type::maxdigits_hex ? num_digits+3 : digits_type::maxdigits_hex;
1071 if C4_UNLIKELY(buf.len < needed_digits)
1072 return needed_digits;
1073 buf.str[pos++] =
'0';
1074 buf.str[pos++] =
'x';
1075 pos = _itoa2bufwithdigits(buf, pos, num_digits, digits_type::min_value_hex());
1079 needed_digits = num_digits+3 > digits_type::maxdigits_bin ? num_digits+3 : digits_type::maxdigits_bin;
1080 if C4_UNLIKELY(buf.len < needed_digits)
1081 return needed_digits;
1082 C4_ASSERT(buf.len >= digits_type::maxdigits_bin);
1083 buf.str[pos++] =
'0';
1084 buf.str[pos++] =
'b';
1085 pos = _itoa2bufwithdigits(buf, pos, num_digits, digits_type::min_value_bin());
1089 needed_digits = num_digits+3 > digits_type::maxdigits_oct ? num_digits+3 : digits_type::maxdigits_oct;
1090 if C4_UNLIKELY(buf.len < needed_digits)
1091 return needed_digits;
1092 C4_ASSERT(buf.len >= digits_type::maxdigits_oct);
1093 buf.str[pos++] =
'0';
1094 buf.str[pos++] =
'o';
1095 pos = _itoa2bufwithdigits(buf, pos, num_digits, digits_type::min_value_oct());
1116 C4_STATIC_ASSERT(std::is_signed<T>::value);
1124 if C4_LIKELY(v != std::numeric_limits<T>::min())
1128 if C4_LIKELY(buf.len >= digits + 1u)
1135 return detail::_itoadec2buf<T>(buf);
1148 C4_STATIC_ASSERT(std::is_signed<T>::value);
1149 C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1150 C4_SUPPRESS_WARNING_GCC_PUSH
1151 #if (defined(__GNUC__) && (__GNUC__ >= 7))
1152 C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
1156 if C4_LIKELY(v != std::numeric_limits<T>::min())
1162 if C4_LIKELY(buf.len > 0)
1166 unsigned digits = 0;
1171 if C4_LIKELY(buf.len >= pos + digits)
1176 if C4_LIKELY(buf.len >= pos + 2u + digits)
1178 buf.str[pos + 0] =
'0';
1179 buf.str[pos + 1] =
'x';
1186 if C4_LIKELY(buf.len >= pos + 2u + digits)
1188 buf.str[pos + 0] =
'0';
1189 buf.str[pos + 1] =
'b';
1196 if C4_LIKELY(buf.len >= pos + 2u + digits)
1198 buf.str[pos + 0] =
'0';
1199 buf.str[pos + 1] =
'o';
1205 return pos + digits;
1207 C4_SUPPRESS_WARNING_GCC_POP
1210 return detail::_itoa2buf<T>(buf, radix);
1223C4_ALWAYS_INLINE
size_t itoa(
substr buf, T v, T radix,
size_t num_digits)
noexcept
1225 C4_STATIC_ASSERT(std::is_signed<T>::value);
1226 C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1227 C4_SUPPRESS_WARNING_GCC_PUSH
1228 #if (defined(__GNUC__) && (__GNUC__ >= 7))
1229 C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
1233 if C4_LIKELY(v != std::numeric_limits<T>::min())
1239 if C4_LIKELY(buf.len > 0)
1243 unsigned total_digits = 0;
1248 total_digits = pos + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1249 if C4_LIKELY(buf.len >= total_digits)
1254 total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1255 if C4_LIKELY(buf.len >= total_digits)
1257 buf.str[pos + 0] =
'0';
1258 buf.str[pos + 1] =
'x';
1259 write_hex(buf.sub(pos + 2), v, num_digits);
1264 total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1265 if C4_LIKELY(buf.len >= total_digits)
1267 buf.str[pos + 0] =
'0';
1268 buf.str[pos + 1] =
'b';
1269 write_bin(buf.sub(pos + 2), v, num_digits);
1274 total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1275 if C4_LIKELY(buf.len >= total_digits)
1277 buf.str[pos + 0] =
'0';
1278 buf.str[pos + 1] =
'o';
1279 write_oct(buf.sub(pos + 2), v, num_digits);
1283 return total_digits;
1285 C4_SUPPRESS_WARNING_GCC_POP
1288 return detail::_itoa2buf<T>(buf, radix, num_digits);
1311 C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1326 C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1327 C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1328 unsigned digits = 0;
1333 if C4_LIKELY(buf.len >= digits)
1338 if C4_LIKELY(buf.len >= digits+2u)
1348 if C4_LIKELY(buf.len >= digits+2u)
1358 if C4_LIKELY(buf.len >= digits+2u)
1379C4_ALWAYS_INLINE
size_t utoa(
substr buf, T v, T radix,
size_t num_digits)
noexcept
1381 C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1382 C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1383 unsigned total_digits = 0;
1388 total_digits = (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1389 if C4_LIKELY(buf.len >= total_digits)
1394 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1395 if C4_LIKELY(buf.len >= total_digits)
1404 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1405 if C4_LIKELY(buf.len >= total_digits)
1414 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1415 if C4_LIKELY(buf.len >= total_digits)
1423 return total_digits;
1425C4_SUPPRESS_WARNING_GCC_POP
1462 C4_STATIC_ASSERT(std::is_integral<T>::value);
1463 C4_STATIC_ASSERT(std::is_signed<T>::value);
1465 if C4_UNLIKELY(str.len == 0)
1473 if(str.str[0] ==
'-')
1475 if C4_UNLIKELY(str.len == ++start)
1480 bool parsed_ok =
true;
1481 if(str.str[start] !=
'0')
1483 parsed_ok =
read_dec(str.sub(start), v);
1485 else if(str.len > start + 1)
1488 const char pfx = str.str[start + 1];
1489 if(pfx ==
'x' || pfx ==
'X')
1490 parsed_ok = str.len > start + 2 &&
read_hex(str.sub(start + 2), v);
1491 else if(pfx ==
'b' || pfx ==
'B')
1492 parsed_ok = str.len > start + 2 &&
read_bin(str.sub(start + 2), v);
1493 else if(pfx ==
'o' || pfx ==
'O')
1494 parsed_ok = str.len > start + 2 &&
read_oct(str.sub(start + 2), v);
1496 parsed_ok =
read_dec(str.sub(start + 1), v);
1500 parsed_ok =
read_dec(str.sub(start), v);
1502 if C4_LIKELY(parsed_ok)
1518 if(trimmed.
len &&
atoi(trimmed, v))
1519 return static_cast<size_t>(trimmed.
end() - str.
begin());
1554 C4_STATIC_ASSERT(std::is_integral<T>::value);
1556 if C4_UNLIKELY(str.len == 0 || str.front() ==
'-')
1559 bool parsed_ok =
true;
1560 if(str.str[0] !=
'0')
1568 const char pfx = str.str[1];
1569 if(pfx ==
'x' || pfx ==
'X')
1570 parsed_ok = str.len > 2 &&
read_hex(str.sub(2), v);
1571 else if(pfx ==
'b' || pfx ==
'B')
1572 parsed_ok = str.len > 2 &&
read_bin(str.sub(2), v);
1573 else if(pfx ==
'o' || pfx ==
'O')
1574 parsed_ok = str.len > 2 &&
read_oct(str.sub(2), v);
1597 if(trimmed.
len &&
atou(trimmed, v))
1598 return static_cast<size_t>(trimmed.
end() - str.
begin());
1605#if defined(_MSC_VER) && !defined(__clang__)
1606# pragma warning(pop)
1607#elif defined(__clang__)
1608# pragma clang diagnostic pop
1609#elif defined(__GNUC__)
1610# pragma GCC diagnostic pop
1622 if(str.len != limit.len)
1623 return str.len > limit.len;
1624 for(
size_t i = 0; i < limit.len; ++i)
1626 if(str.str[i] < limit.str[i])
1628 else if(str.str[i] > limit.str[i])
1649 ->
typename std::enable_if<std::is_unsigned<T>::value,
bool>::type
1651 C4_STATIC_ASSERT(std::is_integral<T>::value);
1653 if C4_UNLIKELY(str.len == 0)
1657 else if(str.str[0] ==
'0')
1666 size_t fno = str.first_not_of(
'0', 2);
1669 return !(str.len <= fno + (
sizeof(T) * 2));
1674 size_t fno = str.first_not_of(
'0', 2);
1677 return !(str.len <= fno +(
sizeof(T) * 8));
1682 size_t fno = str.first_not_of(
'0', 2);
1685 return detail::charconv_digits<T>::is_oct_overflow(str.sub(fno));
1689 size_t fno = str.first_not_of(
'0', 1);
1692 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::max_value_dec());
1696 else if C4_UNLIKELY(str.str[0] ==
'-')
1702 return detail::check_overflow(str, detail::charconv_digits<T>::max_value_dec());
1716 ->
typename std::enable_if<std::is_signed<T>::value,
bool>::type
1718 C4_STATIC_ASSERT(std::is_integral<T>::value);
1719 if C4_UNLIKELY(str.len == 0)
1721 if(str.str[0] ==
'-')
1723 if(str.str[1] ==
'0')
1732 size_t fno = str.first_not_of(
'0', 3);
1735 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::min_value_hex());
1740 size_t fno = str.first_not_of(
'0', 3);
1743 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::min_value_bin());
1748 size_t fno = str.first_not_of(
'0', 3);
1751 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::min_value_oct());
1755 size_t fno = str.first_not_of(
'0', 2);
1758 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::min_value_dec());
1764 return detail::check_overflow(str.sub(1), detail::charconv_digits<T>::min_value_dec());
1767 else if(str.str[0] ==
'0')
1776 size_t fno = str.first_not_of(
'0', 2);
1779 const size_t len = str.len - fno;
1780 return !((len <
sizeof (T) * 2) || (len ==
sizeof(T) * 2 && str.str[fno] <=
'7'));
1785 size_t fno = str.first_not_of(
'0', 2);
1788 return !(str.len <= fno + (
sizeof(T) * 8 - 1));
1793 size_t fno = str.first_not_of(
'0', 2);
1796 return detail::charconv_digits<T>::is_oct_overflow(str.sub(fno));
1800 size_t fno = str.first_not_of(
'0', 1);
1803 return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::max_value_dec());
1809 return detail::check_overflow(str, detail::charconv_digits<T>::max_value_dec());
1824#if (!C4CORE_HAVE_STD_FROMCHARS)
1827void get_real_format_str(
char (& C4_RESTRICT fmt)[N],
int precision,
RealFormat_e formatting,
const char* length_modifier=
"")
1831 iret = snprintf(fmt,
sizeof(fmt),
"%%%s%c", length_modifier, formatting);
1832 else if(precision == 0)
1833 iret = snprintf(fmt,
sizeof(fmt),
"%%.%s%c", length_modifier, formatting);
1835 iret = snprintf(fmt,
sizeof(fmt),
"%%.%d%s%c", precision, length_modifier, formatting);
1836 C4_ASSERT(iret >= 2 &&
size_t(iret) <
sizeof(fmt));
1854size_t print_one(
substr str,
const char* full_fmt, T v)
1860 int iret = _snprintf(str.str, str.len, full_fmt, v);
1866 iret = snprintf(
nullptr, 0, full_fmt, v);
1867 C4_ASSERT(iret > 0);
1869 size_t ret = (size_t) iret;
1872 int iret = snprintf(str.str, str.len, full_fmt, v);
1873 C4_ASSERT(iret >= 0);
1874 size_t ret = (size_t) iret;
1883#if (!C4CORE_HAVE_STD_FROMCHARS) && (!C4CORE_HAVE_FAST_FLOAT)
1889inline size_t scan_one(
csubstr str,
const char *type_fmt, T *v)
1908 int iret = std::snprintf(fmt,
sizeof(fmt),
"%%" "%zu" "%s" "%%n", str.len, type_fmt);
1910 C4_ASSERT(iret >= 0 &&
size_t(iret) < C4_COUNTOF(fmt));
1914 iret = std::sscanf(str.str, fmt, v, &num_chars);
1917 C4_ASSERT(num_chars >= 0);
1918 return (
size_t)(num_chars);
1923#if C4CORE_HAVE_STD_TOCHARS
1927 std::to_chars_result result;
1931 if(buf.len >
size_t(2))
1939 result = std::to_chars(buf.str + pos, buf.str + buf.len, v, (std::chars_format)formatting);
1941 result = std::to_chars(buf.str + pos, buf.str + buf.len, v, (std::chars_format)formatting, precision);
1942 if(result.ec == std::errc())
1945 C4_ASSERT(result.ptr >= buf.str);
1946 ptrdiff_t delta = result.ptr - buf.str;
1947 return static_cast<size_t>(delta);
1949 C4_ASSERT(result.ec == std::errc::value_too_large);
1960 size_t ret =
static_cast<size_t>(std::numeric_limits<T>::max_digits10);
1961 return ret > buf.len ? ret : buf.len + 1;
1966#if C4CORE_HAVE_FAST_FLOAT
1968C4_ALWAYS_INLINE
bool scan_rhex(
csubstr s, T *C4_RESTRICT val)
noexcept
1979 for( ; pos < s.len; ++pos)
1981 const char c = s.str[pos];
1982 if(c >=
'0' && c <=
'9')
1983 *val = (*val * T(16)) + T(c -
'0');
1984 else if(c >=
'a' && c <=
'f')
1985 *val = (*val * T(16)) + T(c -
'a');
1986 else if(c >=
'A' && c <=
'F')
1987 *val = (*val * T(16)) + T(c -
'A');
1993 else if(c ==
'p' || c ==
'P')
2006 for(T digit = T(0.0625); pos < s.len; ++pos, digit /= T(16))
2008 const char c = s.str[pos];
2009 if(c >=
'0' && c <=
'9')
2010 *val += digit * T(c -
'0');
2011 else if(c >=
'a' && c <=
'f')
2012 *val += digit * T(c -
'a');
2013 else if(c >=
'A' && c <=
'F')
2014 *val += digit * T(c -
'A');
2015 else if(c ==
'p' || c ==
'P')
2028 if C4_LIKELY(pos < s.len)
2030 if(s.str[pos] ==
'+')
2032 if C4_LIKELY(pos < s.len)
2034 int16_t powval = {};
2035 if C4_LIKELY(
atoi(s.sub(pos), &powval))
2037 *val *= ipow<T, int16_t, 16>(powval);
2067#if C4CORE_HAVE_STD_TOCHARS
2068 return detail::rtoa(str, v, precision, formatting);
2071 detail::get_real_format_str(
fmt, precision, formatting,
"");
2072 return detail::print_one(str,
fmt, v);
2093#if C4CORE_HAVE_STD_TOCHARS
2094 return detail::rtoa(str, v, precision, formatting);
2097 detail::get_real_format_str(
fmt, precision, formatting,
"l");
2098 return detail::print_one(str,
fmt, v);
2115C4_ALWAYS_INLINE
bool atof(
csubstr str,
float * C4_RESTRICT v)
noexcept
2117 C4_ASSERT(str.len > 0);
2118 C4_ASSERT(str.triml(
" \r\t\n").len == str.len);
2119#if C4CORE_HAVE_FAST_FLOAT
2121 bool isneg = (str.str[0] ==
'-');
2122 csubstr rem = str.sub(isneg || str.str[0] ==
'+');
2123 if( ! (rem.
len >= 2 && (rem.
str[0] ==
'0' && (rem.
str[1] ==
'x' || rem.
str[1] ==
'X'))))
2125 fast_float::from_chars_result result;
2126 result = fast_float::from_chars(str.str, str.str + str.len, *v);
2127 return result.ec == std::errc();
2129 else if(detail::scan_rhex(rem.
sub(2), v))
2131 *v *= isneg ? -1.f : 1.f;
2135#elif C4CORE_HAVE_STD_FROMCHARS
2136 std::from_chars_result result;
2137 result = std::from_chars(str.str, str.str + str.len, *v);
2138 return result.ec == std::errc();
2140 csubstr rem = str.
sub(str.str[0] ==
'-' || str.str[0] ==
'+');
2141 if(!(rem.
len >= 2 && (rem.
str[0] ==
'0' && (rem.
str[1] ==
'x' || rem.
str[1] ==
'X'))))
2155 csubstr trimmed = str.first_real_span();
2156 if(trimmed.
len == 0)
2158 if(
atof(trimmed, v))
2159 return static_cast<size_t>(trimmed.
end() - str.begin());
2176C4_ALWAYS_INLINE
bool atod(
csubstr str,
double * C4_RESTRICT v)
noexcept
2178 C4_ASSERT(str.len > 0);
2179 C4_ASSERT(str.triml(
" \r\t\n").len == str.len);
2180#if C4CORE_HAVE_FAST_FLOAT
2182 bool isneg = (str.str[0] ==
'-');
2183 csubstr rem = str.sub(isneg || str.str[0] ==
'+');
2184 if( ! (rem.
len >= 2 && (rem.
str[0] ==
'0' && (rem.
str[1] ==
'x' || rem.
str[1] ==
'X'))))
2186 fast_float::from_chars_result result;
2188 result = fast_float::from_chars(str.str, str.str + str.len, *v);
2192 return result.ec == std::errc();
2194 else if(detail::scan_rhex(rem.
sub(2), v))
2196 *v *= isneg ? -1. : 1.;
2200#elif C4CORE_HAVE_STD_FROMCHARS
2201 std::from_chars_result result;
2202 result = std::from_chars(str.str, str.str + str.len, *v);
2203 return result.ec == std::errc();
2205 csubstr rem = str.
sub(str.str[0] ==
'-' || str.str[0] ==
'+');
2206 if(!(rem.
len >= 2 && (rem.
str[0] ==
'0' && (rem.
str[1] ==
'x' || rem.
str[1] ==
'X'))))
2220 csubstr trimmed = str.first_real_span();
2221 if(trimmed.
len == 0)
2223 if(
atod(trimmed, v))
2224 return static_cast<size_t>(trimmed.
end() - str.begin());
2239#define C4_IF_NOT_FIXED_LENGTH_I_(T, ty) typename std::enable_if<std:: is_signed<T>::value && !is_fixed_length<T>::value_i, ty>
2240#define C4_IF_NOT_FIXED_LENGTH_U_(T, ty) typename std::enable_if<std::is_unsigned<T>::value && !is_fixed_length<T>::value_u, ty>
2261C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint8_t v, uint8_t radix)
noexcept {
return utoa(s, v, radix); }
2262C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint16_t v, uint16_t radix)
noexcept {
return utoa(s, v, radix); }
2263C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint32_t v, uint32_t radix)
noexcept {
return utoa(s, v, radix); }
2264C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint64_t v, uint64_t radix)
noexcept {
return utoa(s, v, radix); }
2265C4_ALWAYS_INLINE
size_t xtoa(
substr s, int8_t v, int8_t radix)
noexcept {
return itoa(s, v, radix); }
2266C4_ALWAYS_INLINE
size_t xtoa(
substr s, int16_t v, int16_t radix)
noexcept {
return itoa(s, v, radix); }
2267C4_ALWAYS_INLINE
size_t xtoa(
substr s, int32_t v, int32_t radix)
noexcept {
return itoa(s, v, radix); }
2268C4_ALWAYS_INLINE
size_t xtoa(
substr s, int64_t v, int64_t radix)
noexcept {
return itoa(s, v, radix); }
2270C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint8_t v, uint8_t radix,
size_t num_digits)
noexcept {
return utoa(s, v, radix, num_digits); }
2271C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint16_t v, uint16_t radix,
size_t num_digits)
noexcept {
return utoa(s, v, radix, num_digits); }
2272C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint32_t v, uint32_t radix,
size_t num_digits)
noexcept {
return utoa(s, v, radix, num_digits); }
2273C4_ALWAYS_INLINE
size_t xtoa(
substr s, uint64_t v, uint64_t radix,
size_t num_digits)
noexcept {
return utoa(s, v, radix, num_digits); }
2274C4_ALWAYS_INLINE
size_t xtoa(
substr s, int8_t v, int8_t radix,
size_t num_digits)
noexcept {
return itoa(s, v, radix, num_digits); }
2275C4_ALWAYS_INLINE
size_t xtoa(
substr s, int16_t v, int16_t radix,
size_t num_digits)
noexcept {
return itoa(s, v, radix, num_digits); }
2276C4_ALWAYS_INLINE
size_t xtoa(
substr s, int32_t v, int32_t radix,
size_t num_digits)
noexcept {
return itoa(s, v, radix, num_digits); }
2277C4_ALWAYS_INLINE
size_t xtoa(
substr s, int64_t v, int64_t radix,
size_t num_digits)
noexcept {
return itoa(s, v, radix, num_digits); }
2282template <
class T> C4_ALWAYS_INLINE
auto xtoa(
substr buf, T v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_I_(T,
size_t)::type { return
itoa(buf, v); }
2283template <
class T> C4_ALWAYS_INLINE
auto xtoa(substr buf, T v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_U_(T,
size_t)::type { return write_dec(buf, v); }
2285C4_ALWAYS_INLINE
size_t xtoa(
substr s, T *v)
noexcept {
return itoa(s, (intptr_t)v, (intptr_t)16); }
2296C4_ALWAYS_INLINE
bool atox(
csubstr s, uint8_t *C4_RESTRICT v)
noexcept {
return atou(s, v); }
2297C4_ALWAYS_INLINE
bool atox(
csubstr s, uint16_t *C4_RESTRICT v)
noexcept {
return atou(s, v); }
2298C4_ALWAYS_INLINE
bool atox(
csubstr s, uint32_t *C4_RESTRICT v)
noexcept {
return atou(s, v); }
2299C4_ALWAYS_INLINE
bool atox(
csubstr s, uint64_t *C4_RESTRICT v)
noexcept {
return atou(s, v); }
2300C4_ALWAYS_INLINE
bool atox(
csubstr s, int8_t *C4_RESTRICT v)
noexcept {
return atoi(s, v); }
2301C4_ALWAYS_INLINE
bool atox(
csubstr s, int16_t *C4_RESTRICT v)
noexcept {
return atoi(s, v); }
2302C4_ALWAYS_INLINE
bool atox(
csubstr s, int32_t *C4_RESTRICT v)
noexcept {
return atoi(s, v); }
2303C4_ALWAYS_INLINE
bool atox(
csubstr s, int64_t *C4_RESTRICT v)
noexcept {
return atoi(s, v); }
2304C4_ALWAYS_INLINE
bool atox(
csubstr s,
float *C4_RESTRICT v)
noexcept {
return atof(s, v); }
2305C4_ALWAYS_INLINE
bool atox(
csubstr s,
double *C4_RESTRICT v)
noexcept {
return atod(s, v); }
2307template <
class T> C4_ALWAYS_INLINE
auto atox(
csubstr buf, T *C4_RESTRICT v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_I_(T,
bool)::type { return
atoi(buf, v); }
2308template <
class T> C4_ALWAYS_INLINE
auto atox(csubstr buf, T *C4_RESTRICT v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_U_(T,
bool)::type { return atou(buf, v); }
2310C4_ALWAYS_INLINE
bool atox(
csubstr s, T **v)
noexcept { intptr_t tmp;
bool ret =
atox(s, &tmp);
if(ret) { *v = (T*)tmp; }
return ret; }
2347template <
class T> C4_ALWAYS_INLINE
auto to_chars(
substr buf, T v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_I_(T,
size_t)::type { return
itoa(buf, v); }
2348template <
class T> C4_ALWAYS_INLINE
auto to_chars(substr buf, T v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_U_(T,
size_t)::type { return write_dec(buf, v); }
2351 ->
typename std::enable_if<!std::is_same<T, char>::value &&
2352 !std::is_same<T, const char>::value,
2355 return itoa(s, (intptr_t)v, (intptr_t)16);
2388template <
class T> C4_ALWAYS_INLINE
auto from_chars(
csubstr buf, T *C4_RESTRICT v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_I_(T,
bool)::type { return
atoi(buf, v); }
2389template <
class T> C4_ALWAYS_INLINE
auto from_chars(csubstr buf, T *C4_RESTRICT v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_U_(T,
bool)::type { return atou(buf, v); }
2391C4_ALWAYS_INLINE
bool from_chars(
csubstr buf, T **v)
noexcept { intptr_t tmp;
bool ret =
from_chars(buf, &tmp);
if(ret) { *v = (T*)tmp; }
return ret; }
2417template <
class T> C4_ALWAYS_INLINE
auto from_chars_first(csubstr buf, T *C4_RESTRICT v)
noexcept -> C4_IF_NOT_FIXED_LENGTH_U_(T,
size_t)::type { return atou_first(buf, v); }
2425#undef C4_IF_NOT_FIXED_LENGTH_I_
2426#undef C4_IF_NOT_FIXED_LENGTH_U_
2444 return buf.left_of(sz <= buf.len ? sz : buf.len);
2465 if(buf.str[0] ==
'0')
2467 *v =
false;
return true;
2469 else if(buf.str[0] ==
'1')
2471 *v =
true;
return true;
2475 if(((buf.str[0] ==
't') && (0 == memcmp(buf.str + 1,
"rue", 3)))
2477 ((buf.str[0] ==
'T') && (0 == memcmp(buf.str + 1,
"rue", 3) ||
2478 0 == memcmp(buf.str + 1,
"RUE", 3))))
2480 *v =
true;
return true;
2484 if(((buf.str[0] ==
'f') && (0 == memcmp(buf.str + 1,
"alse", 4)))
2486 ((buf.str[0] ==
'F') && (0 == memcmp(buf.str + 1,
"alse", 4) ||
2487 0 == memcmp(buf.str + 1,
"ALSE", 4))))
2489 *v =
false;
return true;
2506 csubstr trimmed = buf.first_non_empty_span();
2521 C4_XASSERT(buf.str);
2535 C4_XASSERT(buf.str);
2556 C4_ASSERT(!buf.overlaps(v));
2557 size_t len = buf.len < v.len ? buf.len : v.len;
2563 C4_ASSERT(buf.str !=
nullptr);
2564 C4_ASSERT(v.str !=
nullptr);
2565 memcpy(buf.str, v.str, len);
2580 csubstr trimmed = buf.first_non_empty_span();
2581 if(trimmed.
len == 0)
2584 return static_cast<size_t>(trimmed.
end() - buf.begin());
2594 C4_ASSERT(!buf.overlaps(v));
2595 size_t len = buf.len < v.len ? buf.len : v.len;
2601 C4_ASSERT(buf.str !=
nullptr);
2602 C4_ASSERT(v.str !=
nullptr);
2603 memcpy(buf.str, v.str, len);
2611 C4_ASSERT(!buf.overlaps(*v));
2613 if(v->len >= buf.len)
2620 C4_ASSERT(buf.str !=
nullptr);
2621 C4_ASSERT(v->str !=
nullptr);
2622 memcpy(v->str, buf.str, buf.len);
2633 csubstr trimmed = buf.first_non_empty_span();
2635 if C4_UNLIKELY(trimmed.
len == 0)
2637 size_t len = trimmed.
len > v->len ? v->len : trimmed.
len;
2643 C4_ASSERT(buf.str !=
nullptr);
2644 C4_ASSERT(v->str !=
nullptr);
2645 memcpy(v->str, trimmed.
str, len);
2647 if C4_UNLIKELY(trimmed.
len > v->len)
2649 return static_cast<size_t>(trimmed.
end() - buf.begin());
2669template<
class CharPtr>
2671 ->
typename std::enable_if<std::is_same<CharPtr, char*>::value
2673 std::is_same<CharPtr, const char*>::value,
size_t>::type
2697#if defined(_MSC_VER) && !defined(__clang__)
2698# pragma warning(pop)
2699#elif defined(__clang__)
2700# pragma clang diagnostic pop
2701#elif defined(__GNUC__)
2702# pragma GCC diagnostic pop
#define C4_NO_UBSAN_IOVRFLW
bool atod(csubstr str, double *v) noexcept
Convert a string to a double precision real number.
size_t atod_first(csubstr str, double *v) noexcept
Convert a string to a double precision real number.
size_t atof_first(csubstr str, float *v) noexcept
Convert a string to a single precision real number.
bool atof(csubstr str, float *v) noexcept
Convert a string to a single precision real number.
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
size_t atoi_first(csubstr str, T *v)
Select the next range of characters in the string that can be parsed as a signed integral value,...
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
size_t atou_first(csubstr str, T *v)
Select the next range of characters in the string that can be parsed as an unsigned integral value,...
bool atox(csubstr s, uint8_t *v) noexcept
@ FTOA_FLEX
print the real number in flexible format (like g)
@ FTOA_SCIENT
print the real number in scientific format (like e)
@ FTOA_FLOAT
print the real number in floating point format (like f)
@ FTOA_HEXA
print the real number in hexadecimal format (like a)
auto digits_dec(T v) noexcept -> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
size_t dtoa(substr str, double v, int precision=-1, RealFormat_e formatting=FTOA_FLEX) noexcept
Convert a double-precision real number to string.
size_t from_chars_first(csubstr buf, uint8_t *v) noexcept
bool from_chars(csubstr buf, uint8_t *v) noexcept
size_t ftoa(substr str, float v, int precision=-1, RealFormat_e formatting=FTOA_FLEX) noexcept
Convert a single-precision real number to string.
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.
auto overflows(csubstr str) noexcept -> typename std::enable_if< std::is_unsigned< T >::value, bool >::type
Test if the following string would overflow when converted to associated integral types; this functio...
bool read_bin(csubstr s, I *v) noexcept
read a binary integer from a string.
bool read_oct(csubstr s, I *v) noexcept
read an octal integer from a string.
bool read_hex(csubstr s, I *v) noexcept
read an hexadecimal integer from a string.
bool read_dec(csubstr s, I *v) noexcept
read a decimal integer from a string.
size_t to_chars(ryml::substr buf, vec2< T > v)
bool from_chars(ryml::csubstr buf, vec2< T > *v)
basic_substring< char > substr
a mutable string view
basic_substring< const char > csubstr
an immutable string view
substr to_chars_sub(substr buf, T const &v) noexcept
call to_chars() and return a substr consisting of the written portion of the input buffer.
size_t utoa(substr buf, T v) noexcept
convert an integral unsigned decimal to a string.
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
size_t write_hex(substr buf, T v) noexcept
write an integer to a string in hexadecimal format.
size_t write_dec(substr buf, T v) noexcept
write an integer to a string in decimal format.
size_t write_bin(substr buf, T v) noexcept
write an integer to a string in binary format.
size_t write_oct(substr buf, T v) noexcept
write an integer to a string in octal format.
size_t xtoa(substr s, uint8_t v) noexcept
basic_substring first_uint_span() const
get the first span which can be interpreted as an unsigned integer
basic_substring first_int_span() const
get the first span which can be interpreted as a signed integer
size_t len
the length of the substring
iterator begin() noexcept
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
basic_substring sub(size_t first) const noexcept
return [first,len[
C * str
a restricted pointer to the first character of the substring