rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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 >
auto c4::to_chars (substr buf, fmt::integral_< T > fmt) -> typename std::enable_if< std::is_signed< T >::value, size_t >::type
 format an integer signed type More...
 
template<typename T >
auto c4::to_chars (substr buf, fmt::integral_padded_< T > fmt) -> typename std::enable_if< std::is_signed< T >::value, size_t >::type
 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)
 
template<typename CharOrConstChar >
size_t c4::to_chars (substr buf, fmt::detail::base64_wrapper_< CharOrConstChar > const &b)
 write a variable or buffer in base64 format More...
 
template<class Container , typename CharOrConstChar >
size_t c4::to_chars (substr buf, fmt::detail::base64_container_wrapper_< Container, CharOrConstChar > const &b)
 write a container 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

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

Definition at line 2342 of file charconv.hpp.

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

2342 { 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:812
size_t c4::to_chars ( substr  buf,
uint16_t  v 
)
inlinenoexcept

Definition at line 2343 of file charconv.hpp.

2343 { 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:812
size_t c4::to_chars ( substr  buf,
uint32_t  v 
)
inlinenoexcept

Definition at line 2344 of file charconv.hpp.

2344 { 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:812
size_t c4::to_chars ( substr  buf,
uint64_t  v 
)
inlinenoexcept

Definition at line 2345 of file charconv.hpp.

2345 { 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:812
size_t c4::to_chars ( substr  buf,
int8_t  v 
)
inlinenoexcept

Definition at line 2346 of file charconv.hpp.

2346 { 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:1229
size_t c4::to_chars ( substr  buf,
int16_t  v 
)
inlinenoexcept

Definition at line 2347 of file charconv.hpp.

2347 { 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:1229
size_t c4::to_chars ( substr  buf,
int32_t  v 
)
inlinenoexcept

Definition at line 2348 of file charconv.hpp.

2348 { 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:1229
size_t c4::to_chars ( substr  buf,
int64_t  v 
)
inlinenoexcept

Definition at line 2349 of file charconv.hpp.

2349 { 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:1229
size_t c4::to_chars ( substr  buf,
float  v 
)
inlinenoexcept

Definition at line 2350 of file charconv.hpp.

2350 { 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:2071
size_t c4::to_chars ( substr  buf,
double  v 
)
inlinenoexcept

Definition at line 2351 of file charconv.hpp.

2351 { 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:2097
template<class T >
auto c4::to_chars ( substr  buf,
T  v 
) -> size_t ::type
inlinenoexcept

Definition at line 2353 of file charconv.hpp.

2353 { 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:1229
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 2356 of file charconv.hpp.

2360 {
2361  return itoa(s, (intptr_t)v, (intptr_t)16);
2362 }
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:1229
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 2447 of file charconv.hpp.

2448 {
2449  size_t sz = to_chars(buf, v);
2450  return buf.left_of(sz <= buf.len ? sz : buf.len);
2451 }
size_t to_chars(substr, std::nullptr_t) noexcept
Definition: charconv.hpp:2689
size_t c4::to_chars ( substr  buf,
bool  v 
)
inlinenoexcept

Definition at line 2459 of file charconv.hpp.

2460 {
2461  int val = v;
2462  return to_chars(buf, val);
2463 }
size_t to_chars(substr, std::nullptr_t) noexcept
Definition: charconv.hpp:2689
size_t c4::to_chars ( substr  buf,
char  v 
)
inlinenoexcept

Definition at line 2523 of file charconv.hpp.

2524 {
2525  if(buf.len > 0)
2526  {
2527  C4_XASSERT(buf.str);
2528  buf.str[0] = v;
2529  }
2530  return 1;
2531 }
size_t c4::to_chars ( substr  buf,
csubstr  v 
)
inlinenoexcept

Definition at line 2560 of file charconv.hpp.

2561 {
2562  C4_ASSERT(!buf.overlaps(v));
2563  size_t len = buf.len < v.len ? buf.len : v.len;
2564  // calling memcpy with null strings is undefined behavior
2565  // and will wreak havoc in calling code's branches.
2566  // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2567  if(len)
2568  {
2569  C4_ASSERT(buf.str != nullptr);
2570  C4_ASSERT(v.str != nullptr);
2571  memcpy(buf.str, v.str, len);
2572  }
2573  return v.len;
2574 }
size_t c4::to_chars ( substr  buf,
substr  v 
)
inlinenoexcept

Definition at line 2598 of file charconv.hpp.

2599 {
2600  C4_ASSERT(!buf.overlaps(v));
2601  size_t len = buf.len < v.len ? buf.len : v.len;
2602  // calling memcpy zero len is undefined behavior
2603  // and will wreak havoc in calling code's branches.
2604  // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2605  if(len)
2606  {
2607  C4_ASSERT(buf.str != nullptr);
2608  C4_ASSERT(v.str != nullptr);
2609  memcpy(buf.str, v.str, len);
2610  }
2611  return v.len;
2612 }
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 2664 of file charconv.hpp.

2665 {
2666  return to_chars(buf, csubstr{v, N-1});
2667 }
size_t to_chars(substr, std::nullptr_t) noexcept
Definition: charconv.hpp:2689
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
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 2676 of file charconv.hpp.

2680 {
2681  return v ? to_chars(buf, csubstr{v, strlen(v)}) : 0;
2682 }
size_t to_chars(substr, std::nullptr_t) noexcept
Definition: charconv.hpp:2689
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
size_t c4::to_chars ( substr  ,
std::nullptr_t   
)
inlinenoexcept

Definition at line 2689 of file charconv.hpp.

2690 {
2691  return 0;
2692 }
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:592
template<typename T >
auto c4::to_chars ( substr  buf,
fmt::integral_< T >  fmt 
) -> typename std::enable_if<std::is_signed<T>::value, size_t>::type
inline

format an integer signed type

format an integer unsigned type

Definition at line 288 of file format.hpp.

290 {
291  return itoa(buf, fmt.val, fmt.radix);
292 }
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.
Definition: charconv.hpp:1120
template<typename T >
auto c4::to_chars ( substr  buf,
fmt::integral_padded_< T >  fmt 
) -> typename std::enable_if<std::is_signed<T>::value, size_t>::type
inline

format an integer signed type, pad with zeroes

format an integer unsigned type, pad with zeroes

Definition at line 296 of file format.hpp.

298 {
299  return itoa(buf, fmt.val, fmt.radix, fmt.num_digits);
300 }
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.
Definition: charconv.hpp:1120
size_t c4::to_chars ( substr  buf,
fmt::real_< float >  fmt 
)
inline

Definition at line 374 of file format.hpp.

374 { return ftoa(buf, fmt.val, fmt.precision, fmt.fmt); }
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:2071
size_t c4::to_chars ( substr  buf,
fmt::real_< double >  fmt 
)
inline

Definition at line 376 of file format.hpp.

376 { return dtoa(buf, fmt.val, fmt.precision, fmt.fmt); }
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:2097
size_t c4::to_chars ( substr  buf,
fmt::const_raw_wrapper  r 
)

write a variable in raw binary format, using memcpy

Definition at line 18 of file format.cpp.

19 {
20  void * vptr = buf.str;
21  size_t space = buf.len;
22  char * ptr = (char*) std::align(r.alignment, r.len, vptr, space);
23  if(ptr == nullptr)
24  {
25  // if it was not possible to align, return a conservative estimate
26  // of the required space
27  return r.alignment + r.len;
28  }
29  C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
30  size_t sz = static_cast<size_t>(ptr - buf.str) + r.len;
31  if(sz <= buf.len)
32  {
33  memcpy(ptr, r.buf, r.len);
34  }
35  return sz;
36 }
template<class T >
size_t c4::to_chars ( substr  buf,
fmt::left_< T > const &  align 
)

Definition at line 567 of file format.hpp.

568 {
569  size_t ret = to_chars(buf, align.val);
570  if(ret >= buf.len || ret >= align.width)
571  return ret > align.width ? ret : align.width;
572  buf.first(align.width).sub(ret).fill(align.padchar);
573  return align.width;
574 }
size_t to_chars(substr buf, fmt::center_< T > const &align)
Definition: format.hpp:592
template<class T >
size_t c4::to_chars ( substr  buf,
fmt::right_< T > const &  align 
)

Definition at line 578 of file format.hpp.

579 {
580  size_t ret = to_chars(buf, align.val);
581  if(ret >= buf.len || ret >= align.width)
582  return ret > align.width ? ret : align.width;
583  size_t rem = align.width - ret;
584  if(ret)
585  memmove(buf.str + rem, buf.str, ret);
586  buf.first(rem).fill(align.padchar);
587  return align.width;
588 }
size_t to_chars(substr buf, fmt::center_< T > const &align)
Definition: format.hpp:592
template<class T >
size_t c4::to_chars ( substr  buf,
fmt::center_< T > const &  align 
)

Definition at line 592 of file format.hpp.

593 {
594  size_t ret = to_chars(buf, align.val);
595  if(ret >= buf.len || ret >= align.width)
596  return ret > align.width ? ret : align.width;
597  size_t first = (align.width - ret) / 2u;
598  if(ret)
599  memmove(buf.str + first, buf.str, ret);
600  buf.first(first).fill(align.padchar);
601  buf.sub(first + ret).fill(align.padchar);
602  return align.width;
603 }
size_t to_chars(substr buf, fmt::center_< T > const &align)
Definition: format.hpp:592
template<typename CharOrConstChar >
size_t c4::to_chars ( substr  buf,
fmt::detail::base64_wrapper_< CharOrConstChar > const &  b 
)
inline

write a variable or buffer in base64 format

Definition at line 238 of file format_base64.hpp.

239 {
240  size_t reqsize = base64_encode(buf.str, buf.len, b.data.buf, b.data.len);
241  if(b.required_size)
242  *b.required_size = reqsize;
243  return reqsize;
244 }
size_t base64_encode(char *encoded, size_t encoded_sz, void const *data, size_t data_sz)
base64-encode binary data.
template<class Container , typename CharOrConstChar >
size_t c4::to_chars ( substr  buf,
fmt::detail::base64_container_wrapper_< Container, CharOrConstChar > const &  b 
)

write a container in base64 format

Definition at line 248 of file format_base64.hpp.

249 {
250  cblob data = b.data();
251  size_t reqsize = base64_encode(buf.str, buf.len, data.buf, data.len);
252  if(b.required_size)
253  *b.required_size = reqsize;
254  return reqsize;
255 }
size_t base64_encode(char *encoded, size_t encoded_sz, void const *data, size_t data_sz)
base64-encode binary data.