rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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.
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]
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*.
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
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
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
template<typename T>
auto c4::to_chars (substr buf, fmt::integral_< T > fmt) -> typename std::enable_if< std::is_unsigned< T >::value, size_t >::type
 format an integer unsigned type
template<typename T>
auto c4::to_chars (substr buf, fmt::integral_padded_< T > fmt) -> typename std::enable_if< std::is_unsigned< T >::value, size_t >::type
 format an integer unsigned type, pad with zeroes
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
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
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

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/32]

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

Definition at line 2342 of file charconv.hpp.

2342{ return write_dec(buf, v); }
size_t write_dec(substr buf, T v) noexcept
write an integer to a string in decimal format.
Definition charconv.hpp:714

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

◆ to_chars() [2/32]

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); }

◆ to_chars() [3/32]

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); }

◆ to_chars() [4/32]

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); }

◆ to_chars() [5/32]

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) noexcept
convert an integral signed decimal to a string.

◆ to_chars() [6/32]

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

Definition at line 2347 of file charconv.hpp.

2347{ return itoa(buf, v); }

◆ to_chars() [7/32]

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

Definition at line 2348 of file charconv.hpp.

2348{ return itoa(buf, v); }

◆ to_chars() [8/32]

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

Definition at line 2349 of file charconv.hpp.

2349{ return itoa(buf, v); }

◆ to_chars() [9/32]

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.

◆ to_chars() [10/32]

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.

◆ to_chars() [11/32]

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); }

◆ to_chars() [12/32]

template<class T>
auto c4::to_chars ( substr s,
T * v )->typenamestd::enable_if<!std::is_same< T, char >::value &&!std::is_same< T, constchar >::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}

◆ 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 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 buf, uint8_t v) noexcept
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring left_of(size_t pos) const noexcept
return [0, pos[ .
Definition substr.hpp:556

Referenced by to_chars_sub().

◆ to_chars() [13/32]

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}

◆ to_chars() [14/32]

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}
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ to_chars() [15/32]

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}
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
Definition substr.hpp:493

◆ to_chars() [16/32]

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}

◆ to_chars() [17/32]

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}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356

◆ to_chars() [18/32]

template<class CharPtr>
auto c4::to_chars ( substr buf,
CharPtr v )->typenamestd::enable_if< std::is_same< CharPtr, char * >::value||std::is_same< CharPtr, constchar * >::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}

◆ to_chars() [19/32]

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

Definition at line 2689 of file charconv.hpp.

2690{
2691 return 0;
2692}

◆ to_chars() [20/32]

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}

◆ to_chars() [21/32]

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

format an integer signed type

Definition at line 288 of file format.hpp.

290{
291 return itoa(buf, fmt.val, fmt.radix);
292}

◆ to_chars() [22/32]

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

format an integer signed 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}

◆ to_chars() [23/32]

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

format an integer unsigned type

Definition at line 305 of file format.hpp.

307{
308 return utoa(buf, fmt.val, fmt.radix);
309}
size_t utoa(substr buf, T v) noexcept
convert an integral unsigned decimal to a string.

◆ to_chars() [24/32]

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

format an integer unsigned type, pad with zeroes

Definition at line 313 of file format.hpp.

315{
316 return utoa(buf, fmt.val, fmt.radix, fmt.num_digits);
317}

◆ to_chars() [25/32]

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); }

◆ to_chars() [26/32]

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); }

◆ to_chars() [27/32]

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}
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361

◆ to_chars() [28/32]

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}
auto fill(C val) -> typename std::enable_if< !std::is_const< U >::value, void >::type
fill the entire contents with the given val
Definition substr.hpp:2153
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
Definition substr.hpp:529
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:502

◆ to_chars() [29/32]

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}

◆ to_chars() [30/32]

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}

◆ to_chars() [31/32]

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.
Definition base64.cpp:361

◆ to_chars() [32/32]

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}