rapidyaml  0.10.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 602 of file charconv.hpp.

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

References c4::digits_dec().

Referenced by c4::itoa(), c4::utoa(), and c4::write_dec().

◆ write_hex_unchecked()

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

Definition at line 635 of file charconv.hpp.

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

References c4::digits_hex().

Referenced by c4::itoa(), c4::utoa(), and c4::write_hex().

◆ write_oct_unchecked()

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

Definition at line 651 of file charconv.hpp.

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

References c4::digits_oct().

Referenced by c4::itoa(), c4::utoa(), and c4::write_oct().

◆ write_bin_unchecked()

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

Definition at line 667 of file charconv.hpp.

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

References c4::digits_bin().

Referenced by c4::itoa(), c4::utoa(), and c4::write_bin().