rapidyaml  0.13.0
parse and emit YAML, and do it fast
Write with known number of digits

Writes a value without checking the buffer length with regards to the required number of digits to encode the value. More...

Functions

template<class T >
void c4::write_dec_unchecked (substr buf, T v, unsigned digits_v) noexcept
 
template<class T >
void c4::write_hex_unchecked (substr buf, T v, unsigned digits_v) noexcept
 
template<class T >
void c4::write_oct_unchecked (substr buf, T v, unsigned digits_v) noexcept
 
template<class T >
void c4::write_bin_unchecked (substr buf, T v, unsigned digits_v) noexcept
 

Detailed Description

Writes a value without checking the buffer length with regards to the required number of digits to encode the value.

It is the responsibility of the caller to ensure that the provided number of digits is enough to write the given value. Notwithstanding the name, assertions are liberally performed, so this code is safe.

Function Documentation

◆ write_dec_unchecked()

template<class T >
void c4::write_dec_unchecked ( substr  buf,
v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 601 of file charconv.hpp.

602 {
603  C4_STATIC_ASSERT(std::is_integral<T>::value);
604  C4_ASSERT(v >= 0);
605  C4_ASSERT(buf.len >= digits_v);
606  C4_XASSERT(digits_v == digits_dec(v));
607  // in bm_xtoa: checkoncelog_singlediv_write2
608  while(v >= T(100))
609  {
610  T quo = v;
611  quo /= T(100);
612  const auto num = (v - quo * T(100)) << 1u; // NOLINT
613  v = quo;
614  buf.str[--digits_v] = detail::digits0099[num + 1];
615  buf.str[--digits_v] = detail::digits0099[num];
616  }
617  if(v >= T(10))
618  {
619  C4_ASSERT(digits_v == 2);
620  const auto num = v << 1u;
621  buf.str[1] = detail::digits0099[num + 1];
622  buf.str[0] = detail::digits0099[num];
623  }
624  else
625  {
626  C4_ASSERT(digits_v == 1);
627  buf.str[0] = (char)('0' + v);
628  }
629 }
auto digits_dec(T v) noexcept -> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:433

◆ write_hex_unchecked()

template<class T >
void c4::write_hex_unchecked ( substr  buf,
v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 634 of file charconv.hpp.

635 {
636  C4_STATIC_ASSERT(std::is_integral<T>::value);
637  C4_ASSERT(v >= 0);
638  C4_ASSERT(buf.len >= digits_v);
639  C4_XASSERT(digits_v == digits_hex(v));
640  do {
641  buf.str[--digits_v] = detail::hexchars[v & T(15)];
642  v >>= 4;
643  } while(v);
644  C4_ASSERT(digits_v == 0);
645 }
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:516

◆ write_oct_unchecked()

template<class T >
void c4::write_oct_unchecked ( substr  buf,
v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 650 of file charconv.hpp.

651 {
652  C4_STATIC_ASSERT(std::is_integral<T>::value);
653  C4_ASSERT(v >= 0);
654  C4_ASSERT(buf.len >= digits_v);
655  C4_XASSERT(digits_v == digits_oct(v));
656  do {
657  buf.str[--digits_v] = (char)('0' + (v & T(7)));
658  v >>= 3;
659  } while(v);
660  C4_ASSERT(digits_v == 0);
661 }
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:534

◆ write_bin_unchecked()

template<class T >
void c4::write_bin_unchecked ( substr  buf,
v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 666 of file charconv.hpp.

667 {
668  C4_STATIC_ASSERT(std::is_integral<T>::value);
669  C4_ASSERT(v >= 0);
670  C4_ASSERT(buf.len >= digits_v);
671  C4_XASSERT(digits_v == digits_bin(v));
672  do {
673  buf.str[--digits_v] = (char)('0' + (v & T(1)));
674  v >>= 1;
675  } while(v);
676  C4_ASSERT(digits_v == 0);
677 }
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:525