rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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

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

Definition at line 614 of file charconv.hpp.

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

615 {
616  C4_STATIC_ASSERT(std::is_integral<T>::value);
617  C4_ASSERT(v >= 0);
618  C4_ASSERT(buf.len >= digits_v);
619  C4_XASSERT(digits_v == digits_dec(v));
620  // in bm_xtoa: checkoncelog_singlediv_write2
621  while(v >= T(100))
622  {
623  T quo = v;
624  quo /= T(100);
625  const auto num = (v - quo * T(100)) << 1u; // NOLINT
626  v = quo;
627  buf.str[--digits_v] = detail::digits0099[num + 1];
628  buf.str[--digits_v] = detail::digits0099[num];
629  }
630  if(v >= T(10))
631  {
632  C4_ASSERT(digits_v == 2);
633  const auto num = v << 1u;
634  buf.str[1] = detail::digits0099[num + 1];
635  buf.str[0] = detail::digits0099[num];
636  }
637  else
638  {
639  C4_ASSERT(digits_v == 1);
640  buf.str[0] = (char)('0' + v);
641  }
642 }
auto digits_dec(T v) noexcept-> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:432
template<class T >
void c4::write_hex_unchecked ( substr  buf,
T  v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 647 of file charconv.hpp.

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

648 {
649  C4_STATIC_ASSERT(std::is_integral<T>::value);
650  C4_ASSERT(v >= 0);
651  C4_ASSERT(buf.len >= digits_v);
652  C4_XASSERT(digits_v == digits_hex(v));
653  do {
654  buf.str[--digits_v] = detail::hexchars[v & T(15)];
655  v >>= 4;
656  } while(v);
657  C4_ASSERT(digits_v == 0);
658 }
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:529
template<class T >
void c4::write_oct_unchecked ( substr  buf,
T  v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 663 of file charconv.hpp.

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

664 {
665  C4_STATIC_ASSERT(std::is_integral<T>::value);
666  C4_ASSERT(v >= 0);
667  C4_ASSERT(buf.len >= digits_v);
668  C4_XASSERT(digits_v == digits_oct(v));
669  do {
670  buf.str[--digits_v] = (char)('0' + (v & T(7)));
671  v >>= 3;
672  } while(v);
673  C4_ASSERT(digits_v == 0);
674 }
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:547
template<class T >
void c4::write_bin_unchecked ( substr  buf,
T  v,
unsigned  digits_v 
)
inlinenoexcept

Definition at line 679 of file charconv.hpp.

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

680 {
681  C4_STATIC_ASSERT(std::is_integral<T>::value);
682  C4_ASSERT(v >= 0);
683  C4_ASSERT(buf.len >= digits_v);
684  C4_XASSERT(digits_v == digits_bin(v));
685  do {
686  buf.str[--digits_v] = (char)('0' + (v & T(1)));
687  v >>= 1;
688  } while(v);
689  C4_ASSERT(digits_v == 0);
690 }
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:538