rapidyaml 0.14.0
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)

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

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

Definition at line 2341 of file charconv.hpp.

2341{ 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:715

◆ to_chars() [2/30]

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

Definition at line 2342 of file charconv.hpp.

2342{ return write_dec(buf, v); }

◆ to_chars() [3/30]

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

Definition at line 2343 of file charconv.hpp.

2343{ return write_dec(buf, v); }

◆ to_chars() [4/30]

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

Definition at line 2344 of file charconv.hpp.

2344{ return write_dec(buf, v); }

◆ to_chars() [5/30]

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

Definition at line 2345 of file charconv.hpp.

2345{ return itoa(buf, v); }
size_t itoa(substr buf, T v) noexcept
convert an integral signed decimal to a string.

◆ to_chars() [6/30]

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

Definition at line 2346 of file charconv.hpp.

2346{ return itoa(buf, v); }

◆ to_chars() [7/30]

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

Definition at line 2347 of file charconv.hpp.

2347{ return itoa(buf, v); }

◆ to_chars() [8/30]

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

Definition at line 2348 of file charconv.hpp.

2348{ return itoa(buf, v); }

◆ to_chars() [9/30]

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

Definition at line 2349 of file charconv.hpp.

2349{ 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/30]

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

Definition at line 2350 of file charconv.hpp.

2350{ 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/30]

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

Definition at line 2352 of file charconv.hpp.

2352{ return itoa(buf, v); }

◆ to_chars() [12/30]

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

2359{
2360 return itoa(s, (intptr_t)v, (intptr_t)16);
2361}

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

2447{
2448 size_t sz = to_chars(buf, v);
2449 return buf.left_of(sz <= buf.len ? sz : buf.len);
2450}
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:557

◆ to_chars() [13/30]

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

Definition at line 2458 of file charconv.hpp.

2459{
2460 int val = v;
2461 return to_chars(buf, val);
2462}

◆ to_chars() [14/30]

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

Definition at line 2522 of file charconv.hpp.

2523{
2524 if(buf.len > 0)
2525 {
2526 C4_XASSERT(buf.str);
2527 buf.str[0] = v;
2528 }
2529 return 1;
2530}
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ to_chars() [15/30]

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

Definition at line 2559 of file charconv.hpp.

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

◆ to_chars() [16/30]

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

Definition at line 2597 of file charconv.hpp.

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

◆ to_chars() [17/30]

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

2664{
2665 return to_chars(buf, csubstr{v, N-1});
2666}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357

◆ to_chars() [18/30]

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

2679{
2680 return v ? to_chars(buf, csubstr{v, strlen(v)}) : 0;
2681}

◆ to_chars() [19/30]

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

Definition at line 2688 of file charconv.hpp.

2689{
2690 return 0;
2691}

◆ to_chars() [20/30]

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

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

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

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

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

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

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

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

write a variable in raw binary format, using memcpy

◆ to_chars() [28/30]

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:2154
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
Definition substr.hpp:530
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503

◆ to_chars() [29/30]

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

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}