rapidyaml  0.8.0
parse and emit YAML, and do it fast
to_chars: generalized chars to value

Convert the given value, writing into the string. More...

Functions

size_t c4::to_chars (substr buf, uint8_t v) noexcept
 
size_t c4::to_chars (substr buf, uint16_t v) noexcept
 
size_t c4::to_chars (substr buf, uint32_t v) noexcept
 
size_t c4::to_chars (substr buf, uint64_t v) noexcept
 
size_t c4::to_chars (substr buf, int8_t v) noexcept
 
size_t c4::to_chars (substr buf, int16_t v) noexcept
 
size_t c4::to_chars (substr buf, int32_t v) noexcept
 
size_t c4::to_chars (substr buf, int64_t v) noexcept
 
size_t c4::to_chars (substr buf, float v) noexcept
 
size_t c4::to_chars (substr buf, double v) noexcept
 
template<class T >
auto c4::to_chars (substr buf, T v) noexcept -> size_t ::type
 
template<class T >
size_t c4::to_chars (substr s, T *v) noexcept
 
template<class T >
substr c4::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. More...
 
size_t c4::to_chars (substr buf, bool v) noexcept
 
size_t c4::to_chars (substr buf, char v) noexcept
 
size_t c4::to_chars (substr buf, csubstr v) noexcept
 
size_t c4::to_chars (substr buf, substr v) noexcept
 
template<size_t N>
size_t c4::to_chars (substr buf, const char(&v)[N]) noexcept
 
size_t c4::to_chars (substr buf, const char *v) noexcept
 
template<class T >
size_t c4::to_chars (substr buf, fmt::boolalpha_< T > fmt)
 write a variable as an alphabetic boolean, ie as either true or false More...
 
template<typename T >
std::enable_if< std::is_signed< T >::value, size_t >::type c4::to_chars (substr buf, fmt::integral_< T > fmt)
 format an integer signed type More...
 
template<typename T >
std::enable_if< std::is_signed< T >::value, size_t >::type c4::to_chars (substr buf, fmt::integral_padded_< T > fmt)
 format an integer signed type, pad with zeroes More...
 
size_t c4::to_chars (substr buf, fmt::real_< float > fmt)
 
size_t c4::to_chars (substr buf, fmt::real_< double > fmt)
 
size_t c4::to_chars (substr buf, fmt::const_raw_wrapper r)
 write a variable in raw binary format, using memcpy More...
 
template<class T >
size_t c4::to_chars (substr buf, fmt::left_< T > const &align)
 
template<class T >
size_t c4::to_chars (substr buf, fmt::right_< T > const &align)
 
size_t c4::to_chars (substr buf, fmt::const_base64_wrapper b)
 write a variable in base64 format More...
 

Detailed Description

Convert the given value, writing into the string.

The resulting string will NOT be null-terminated. Return the number of characters needed. This function is safe to call when the string is too small - no writes will occur beyond the string's last character.

Dispatches to the most appropriate and efficient conversion function.

See also
write_dec, utoa: unsigned to chars, itoa: signed to chars, ftoa: float32 to chars, dtoa: float64 to chars
Warning
When serializing floating point values (float or double), be aware that because it uses defaults, to_chars() may cause a truncation of the precision. To enforce a particular precision, use for example c4::fmt::real, or call directly c4::ftoa or c4::dtoa.

Function Documentation

◆ to_chars() [1/27]

size_t c4::to_chars ( substr  buf,
uint8_t  v 
)
inlinenoexcept

Definition at line 2328 of file charconv.hpp.

2328 { return write_dec(buf, v); }
size_t write_dec(substr buf, T val, size_t num_digits) noexcept
same as c4::write_dec(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:800

References c4::write_dec().

Referenced by c4::cat(), c4::catsep(), c4::format(), and c4::to_chars().

◆ to_chars() [2/27]

size_t c4::to_chars ( substr  buf,
uint16_t  v 
)
inlinenoexcept

Definition at line 2329 of file charconv.hpp.

2329 { return write_dec(buf, v); }

References c4::write_dec().

◆ to_chars() [3/27]

size_t c4::to_chars ( substr  buf,
uint32_t  v 
)
inlinenoexcept

Definition at line 2330 of file charconv.hpp.

2330 { return write_dec(buf, v); }

References c4::write_dec().

◆ to_chars() [4/27]

size_t c4::to_chars ( substr  buf,
uint64_t  v 
)
inlinenoexcept

Definition at line 2331 of file charconv.hpp.

2331 { return write_dec(buf, v); }

References c4::write_dec().

◆ to_chars() [5/27]

size_t c4::to_chars ( substr  buf,
int8_t  v 
)
inlinenoexcept

Definition at line 2332 of file charconv.hpp.

2332 { return itoa(buf, v); }
size_t itoa(substr buf, T v, T radix, size_t num_digits) noexcept
same as c4::itoa(), but pad with zeroes on the left such that the resulting string is num_digits wide...
Definition: charconv.hpp:1217

References c4::itoa().

◆ to_chars() [6/27]

size_t c4::to_chars ( substr  buf,
int16_t  v 
)
inlinenoexcept

Definition at line 2333 of file charconv.hpp.

2333 { return itoa(buf, v); }

References c4::itoa().

◆ to_chars() [7/27]

size_t c4::to_chars ( substr  buf,
int32_t  v 
)
inlinenoexcept

Definition at line 2334 of file charconv.hpp.

2334 { return itoa(buf, v); }

References c4::itoa().

◆ to_chars() [8/27]

size_t c4::to_chars ( substr  buf,
int64_t  v 
)
inlinenoexcept

Definition at line 2335 of file charconv.hpp.

2335 { return itoa(buf, v); }

References c4::itoa().

◆ to_chars() [9/27]

size_t c4::to_chars ( substr  buf,
float  v 
)
inlinenoexcept

Definition at line 2336 of file charconv.hpp.

2336 { return ftoa(buf, v); }
size_t ftoa(substr str, float v, int precision=-1, RealFormat_e formatting=FTOA_FLEX) noexcept
Convert a single-precision real number to string.
Definition: charconv.hpp:2061

References c4::ftoa().

◆ to_chars() [10/27]

size_t c4::to_chars ( substr  buf,
double  v 
)
inlinenoexcept

Definition at line 2337 of file charconv.hpp.

2337 { return dtoa(buf, v); }
size_t dtoa(substr str, double v, int precision=-1, RealFormat_e formatting=FTOA_FLEX) noexcept
Convert a double-precision real number to string.
Definition: charconv.hpp:2087

References c4::dtoa().

◆ to_chars() [11/27]

template<class T >
auto c4::to_chars ( substr  buf,
v 
) -> size_t ::type
inlinenoexcept

Definition at line 2339 of file charconv.hpp.

2339 { return itoa(buf, v); }

◆ to_chars() [12/27]

template<class T >
size_t c4::to_chars ( substr  s,
T *  v 
)
inlinenoexcept

Definition at line 2342 of file charconv.hpp.

2342 { return itoa(s, (intptr_t)v, (intptr_t)16); }

References c4::itoa().

◆ to_chars_sub()

template<class T >
substr c4::to_chars_sub ( substr  buf,
T const &  v 
)
inlinenoexcept

call to_chars() and return a substr consisting of the written portion of the input buffer.

Ie, same as to_chars(), but return a substr instead of a size_t. Convert the given value to a string using to_chars(), and return the resulting string, up to and including the last written character.

See also
to_chars()

Definition at line 2427 of file charconv.hpp.

2428 {
2429  size_t sz = to_chars(buf, v);
2430  return buf.left_of(sz <= buf.len ? sz : buf.len);
2431 }
size_t to_chars(substr buf, const char *v) noexcept
Definition: charconv.hpp:2651

References c4::to_chars().

◆ to_chars() [13/27]

size_t c4::to_chars ( substr  buf,
bool  v 
)
inlinenoexcept

Definition at line 2439 of file charconv.hpp.

2440 {
2441  int val = v;
2442  return to_chars(buf, val);
2443 }

References c4::to_chars().

◆ to_chars() [14/27]

size_t c4::to_chars ( substr  buf,
char  v 
)
inlinenoexcept

Definition at line 2504 of file charconv.hpp.

2505 {
2506  if(buf.len > 0)
2507  {
2508  C4_XASSERT(buf.str);
2509  buf.str[0] = v;
2510  }
2511  return 1;
2512 }

◆ to_chars() [15/27]

size_t c4::to_chars ( substr  buf,
csubstr  v 
)
inlinenoexcept

Definition at line 2541 of file charconv.hpp.

2542 {
2543  C4_ASSERT(!buf.overlaps(v));
2544  size_t len = buf.len < v.len ? buf.len : v.len;
2545  // calling memcpy with null strings is undefined behavior
2546  // and will wreak havoc in calling code's branches.
2547  // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2548  if(len)
2549  {
2550  C4_ASSERT(buf.str != nullptr);
2551  C4_ASSERT(v.str != nullptr);
2552  memcpy(buf.str, v.str, len);
2553  }
2554  return v.len;
2555 }

◆ to_chars() [16/27]

size_t c4::to_chars ( substr  buf,
substr  v 
)
inlinenoexcept

Definition at line 2579 of file charconv.hpp.

2580 {
2581  C4_ASSERT(!buf.overlaps(v));
2582  size_t len = buf.len < v.len ? buf.len : v.len;
2583  // calling memcpy with null strings is undefined behavior
2584  // and will wreak havoc in calling code's branches.
2585  // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2586  if(len)
2587  {
2588  C4_ASSERT(buf.str != nullptr);
2589  C4_ASSERT(v.str != nullptr);
2590  memcpy(buf.str, v.str, len);
2591  }
2592  return v.len;
2593 }

◆ to_chars() [17/27]

template<size_t N>
size_t c4::to_chars ( substr  buf,
const char(&)  v[N] 
)
inlinenoexcept

Definition at line 2644 of file charconv.hpp.

2645 {
2646  csubstr sp(v);
2647  return to_chars(buf, sp);
2648 }

References c4::to_chars().

◆ to_chars() [18/27]

size_t c4::to_chars ( substr  buf,
const char *  v 
)
inlinenoexcept

Definition at line 2651 of file charconv.hpp.

2652 {
2653  return to_chars(buf, to_csubstr(v));
2654 }
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2186

References c4::to_csubstr().

Referenced by c4::to_chars(), and c4::to_chars_sub().

◆ to_chars() [19/27]

template<class T >
size_t c4::to_chars ( substr  buf,
fmt::boolalpha_< T >  fmt 
)
inline

write a variable as an alphabetic boolean, ie as either true or false

Definition at line 88 of file format.hpp.

89 {
90  return to_chars(buf, fmt.val ? "true" : "false");
91 }
size_t to_chars(substr buf, fmt::right_< T > const &align)
Definition: format.hpp:558

References c4::to_chars(), and c4::fmt::boolalpha_< T >::val.

◆ to_chars() [20/27]

template<typename T >
std::enable_if<std::is_signed<T>::value, size_t>::type c4::to_chars ( substr  buf,
fmt::integral_< T >  fmt 
)
inline

format an integer signed type

Definition at line 294 of file format.hpp.

295 {
296  return itoa(buf, fmt.val, fmt.radix);
297 }
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.
Definition: charconv.hpp:1108

References c4::itoa(), c4::fmt::integral_< T >::radix, and c4::fmt::integral_< T >::val.

◆ to_chars() [21/27]

template<typename T >
std::enable_if<std::is_signed<T>::value, size_t>::type c4::to_chars ( substr  buf,
fmt::integral_padded_< T >  fmt 
)
inline

format an integer signed type, pad with zeroes

Definition at line 303 of file format.hpp.

304 {
305  return itoa(buf, fmt.val, fmt.radix, fmt.num_digits);
306 }

References c4::itoa(), c4::fmt::integral_padded_< T >::num_digits, c4::fmt::integral_padded_< T >::radix, and c4::fmt::integral_padded_< T >::val.

◆ to_chars() [22/27]

size_t c4::to_chars ( substr  buf,
fmt::real_< float >  fmt 
)
inline

Definition at line 382 of file format.hpp.

382 { return ftoa(buf, fmt.val, fmt.precision, fmt.fmt); }

References c4::fmt::real_< T >::fmt, c4::ftoa(), c4::fmt::real_< T >::precision, and c4::fmt::real_< T >::val.

◆ to_chars() [23/27]

size_t c4::to_chars ( substr  buf,
fmt::real_< double >  fmt 
)
inline

Definition at line 384 of file format.hpp.

384 { return dtoa(buf, fmt.val, fmt.precision, fmt.fmt); }

References c4::dtoa(), c4::fmt::real_< T >::fmt, c4::fmt::real_< T >::precision, and c4::fmt::real_< T >::val.

◆ to_chars() [24/27]

size_t c4::to_chars ( substr  buf,
fmt::const_raw_wrapper  r 
)

write a variable in raw binary format, using memcpy

◆ to_chars() [25/27]

template<class T >
size_t c4::to_chars ( substr  buf,
fmt::left_< T > const &  align 
)

Definition at line 546 of file format.hpp.

547 {
548  size_t ret = to_chars(buf, align.val);
549  if(ret >= buf.len || ret >= align.width)
550  return ret > align.width ? ret : align.width;
551  buf.first(align.width).sub(ret).fill(align.pad);
552  to_chars(buf, align.val);
553  return align.width;
554 }

References c4::fmt::left_< T >::pad, c4::to_chars(), c4::fmt::left_< T >::val, and c4::fmt::left_< T >::width.

◆ to_chars() [26/27]

template<class T >
size_t c4::to_chars ( substr  buf,
fmt::right_< T > const &  align 
)

Definition at line 558 of file format.hpp.

559 {
560  size_t ret = to_chars(buf, align.val);
561  if(ret >= buf.len || ret >= align.width)
562  return ret > align.width ? ret : align.width;
563  size_t rem = static_cast<size_t>(align.width - ret);
564  buf.first(rem).fill(align.pad);
565  to_chars(buf.sub(rem), align.val);
566  return align.width;
567 }

References c4::fmt::right_< T >::pad, c4::to_chars(), c4::fmt::right_< T >::val, and c4::fmt::right_< T >::width.

◆ to_chars() [27/27]

size_t c4::to_chars ( substr  buf,
fmt::const_base64_wrapper  b 
)
inline

write a variable in base64 format

Definition at line 127 of file base64.hpp.

128 {
129  return base64_encode(buf, b.data);
130 }
size_t base64_encode(substr encoded, cblob data)
base64-encode binary data.

References c4::base64_encode(), and c4::fmt::base64_wrapper_< CharOrConstChar >::data.