rapidyaml  0.13.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 >
auto c4::to_chars (substr s, T *v) noexcept -> typename std::enable_if<!std::is_same< T, char >::value &&!std::is_same< T, const char >::value, size_t >::type
 
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
 (1) overload for char(&)[N] and const char(&)[N] More...
 
template<class CharPtr >
auto c4::to_chars (substr buf, CharPtr v) noexcept -> typename std::enable_if< std::is_same< CharPtr, char * >::value||std::is_same< CharPtr, const char * >::value, size_t >::type
 (2) overload for char* and const char*. More...
 
size_t c4::to_chars (substr, std::nullptr_t) noexcept
 
size_t c4::to_chars (substr buf, fmt::boolalpha_ 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)
 
template<class T >
size_t c4::to_chars (substr buf, fmt::center_< 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/29]

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

Definition at line 2327 of file charconv.hpp.

2327 { 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:799

◆ to_chars() [2/29]

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

Definition at line 2328 of file charconv.hpp.

2328 { return write_dec(buf, v); }

◆ to_chars() [3/29]

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

Definition at line 2329 of file charconv.hpp.

2329 { return write_dec(buf, v); }

◆ to_chars() [4/29]

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

Definition at line 2330 of file charconv.hpp.

2330 { return write_dec(buf, v); }

◆ to_chars() [5/29]

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

Definition at line 2331 of file charconv.hpp.

2331 { 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:1216

◆ to_chars() [6/29]

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

Definition at line 2332 of file charconv.hpp.

2332 { return itoa(buf, v); }

◆ to_chars() [7/29]

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

Definition at line 2333 of file charconv.hpp.

2333 { return itoa(buf, v); }

◆ to_chars() [8/29]

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

Definition at line 2334 of file charconv.hpp.

2334 { return itoa(buf, v); }

◆ to_chars() [9/29]

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

Definition at line 2335 of file charconv.hpp.

2335 { 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:2056

◆ to_chars() [10/29]

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

Definition at line 2336 of file charconv.hpp.

2336 { 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:2082

◆ to_chars() [11/29]

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

Definition at line 2338 of file charconv.hpp.

2338 { return itoa(buf, v); }

◆ to_chars() [12/29]

template<class T >
auto c4::to_chars ( substr  s,
T *  v 
) -> typename std::enable_if<!std::is_same<T, char>::value && !std::is_same<T, const char>::value, size_t>::type
inlinenoexcept

Definition at line 2341 of file charconv.hpp.

2345 {
2346  return itoa(s, (intptr_t)v, (intptr_t)16);
2347 }

◆ 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 2432 of file charconv.hpp.

2433 {
2434  size_t sz = to_chars(buf, v);
2435  return buf.left_of(sz <= buf.len ? sz : buf.len);
2436 }
size_t to_chars(substr, std::nullptr_t) noexcept
Definition: charconv.hpp:2674

◆ to_chars() [13/29]

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

Definition at line 2444 of file charconv.hpp.

2445 {
2446  int val = v;
2447  return to_chars(buf, val);
2448 }

◆ to_chars() [14/29]

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

Definition at line 2508 of file charconv.hpp.

2509 {
2510  if(buf.len > 0)
2511  {
2512  C4_XASSERT(buf.str);
2513  buf.str[0] = v;
2514  }
2515  return 1;
2516 }

◆ to_chars() [15/29]

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

Definition at line 2545 of file charconv.hpp.

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

◆ to_chars() [16/29]

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

Definition at line 2583 of file charconv.hpp.

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

◆ to_chars() [17/29]

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

(1) overload for char(&)[N] and const char(&)[N]

Definition at line 2649 of file charconv.hpp.

2650 {
2651  return to_chars(buf, csubstr{v, N-1});
2652 }

◆ to_chars() [18/29]

template<class CharPtr >
auto c4::to_chars ( substr  buf,
CharPtr  v 
) -> typename std::enable_if<std::is_same<CharPtr, char*>::value || std::is_same<CharPtr, const char*>::value, size_t>::type
noexcept

(2) overload for char* and const char*.

Must be zero-terminated!

Warning
will call strlen()
Note
this overload uses SFINAE to prevent it from overriding the array overload
See also
For a more detailed explanation on why the plain pointer overloads cannot coexist with the array overloads, see http://cplusplus.bordoon.com/specializeForCharacterArrays.html

Definition at line 2661 of file charconv.hpp.

2665 {
2666  return v ? to_chars(buf, csubstr{v, strlen(v)}) : 0;
2667 }

◆ to_chars() [19/29]

size_t c4::to_chars ( substr  ,
std::nullptr_t   
)
inlinenoexcept

Definition at line 2674 of file charconv.hpp.

2675 {
2676  return 0;
2677 }

◆ to_chars() [20/29]

size_t c4::to_chars ( substr  buf,
fmt::boolalpha_  fmt 
)
inline

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

Definition at line 84 of file format.hpp.

85 {
86  return to_chars(buf, fmt.val ? "true" : "false");
87 }
size_t to_chars(substr buf, fmt::center_< T > const &align)
Definition: format.hpp:596

◆ to_chars() [21/29]

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 290 of file format.hpp.

291 {
292  return itoa(buf, fmt.val, fmt.radix);
293 }
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.
Definition: charconv.hpp:1107

◆ to_chars() [22/29]

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 299 of file format.hpp.

300 {
301  return itoa(buf, fmt.val, fmt.radix, fmt.num_digits);
302 }

◆ to_chars() [23/29]

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

Definition at line 378 of file format.hpp.

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

◆ to_chars() [24/29]

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

Definition at line 380 of file format.hpp.

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

◆ to_chars() [25/29]

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

write a variable in raw binary format, using memcpy

◆ to_chars() [26/29]

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

Definition at line 571 of file format.hpp.

572 {
573  size_t ret = to_chars(buf, align.val);
574  if(ret >= buf.len || ret >= align.width)
575  return ret > align.width ? ret : align.width;
576  buf.first(align.width).sub(ret).fill(align.padchar);
577  return align.width;
578 }

◆ to_chars() [27/29]

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

Definition at line 582 of file format.hpp.

583 {
584  size_t ret = to_chars(buf, align.val);
585  if(ret >= buf.len || ret >= align.width)
586  return ret > align.width ? ret : align.width;
587  size_t rem = align.width - ret;
588  if(ret)
589  memmove(buf.str + rem, buf.str, ret);
590  buf.first(rem).fill(align.padchar);
591  return align.width;
592 }

◆ to_chars() [28/29]

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

Definition at line 596 of file format.hpp.

597 {
598  size_t ret = to_chars(buf, align.val);
599  if(ret >= buf.len || ret >= align.width)
600  return ret > align.width ? ret : align.width;
601  size_t first = (align.width - ret) / 2u;
602  if(ret)
603  memmove(buf.str + first, buf.str, ret);
604  buf.first(first).fill(align.padchar);
605  buf.sub(first + ret).fill(align.padchar);
606  return align.width;
607 }

◆ to_chars() [29/29]

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.