rapidyaml  0.7.2
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 608 of file charconv.hpp.

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

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

642 {
643  C4_STATIC_ASSERT(std::is_integral<T>::value);
644  C4_ASSERT(v >= 0);
645  C4_ASSERT(buf.len >= digits_v);
646  C4_XASSERT(digits_v == digits_hex(v));
647  do {
648  buf.str[--digits_v] = detail::hexchars[v & T(15)];
649  v >>= 4;
650  } while(v);
651  C4_ASSERT(digits_v == 0);
652 }
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:524

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

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

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

674 {
675  C4_STATIC_ASSERT(std::is_integral<T>::value);
676  C4_ASSERT(v >= 0);
677  C4_ASSERT(buf.len >= digits_v);
678  C4_XASSERT(digits_v == digits_bin(v));
679  do {
680  buf.str[--digits_v] = (char)('0' + (v & T(1)));
681  v >>= 1;
682  } while(v);
683  C4_ASSERT(digits_v == 0);
684 }
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:533

References c4::digits_bin().

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