rapidyaml  0.16.0
parseandemitYAML,anddoitfast
utoa: unsigned to chars

Functions

template<class T >
size_t c4::utoa (substr buf, T v) noexcept
 convert an integral unsigned decimal to a string. More...
 
template<class T >
size_t c4::utoa (substr buf, T v, T radix) noexcept
 convert an integral unsigned integer to a string, using a specific radix. More...
 
template<class T >
size_t c4::utoa (substr buf, T v, T radix, size_t num_digits) noexcept
 same as c4::utoa(), but pad with zeroes on the left such that the resulting string is num_digits wide. More...
 

Detailed Description

Function Documentation

template<class T >
size_t c4::utoa ( substr  buf,
T  v 
)
inlinenoexcept

convert an integral unsigned decimal to a string.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1315 of file charconv.hpp.

Referenced by c4::xtoa().

1316 {
1317  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1318  // write_dec() does the buffer length check, so no need to check here
1319  return write_dec(buf, v);
1320 }
size_t write_dec(substr buf, T val, size_t num_digits) noexcept
same as c4::write_dec(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:812
template<class T >
size_t c4::utoa ( substr  buf,
T  v,
T  radix 
)
inlinenoexcept

convert an integral unsigned integer to a string, using a specific radix.

The radix must be 2, 8, 10 or 16.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1330 of file charconv.hpp.

1331 {
1332  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1333  C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1334  unsigned digits = 0;
1335  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1336  {
1337  case T(10):
1338  digits = digits_dec(v);
1339  if C4_LIKELY(buf.len >= digits)
1340  write_dec_unchecked(buf, v, digits);
1341  break;
1342  case T(16):
1343  digits = digits_hex(v);
1344  if C4_LIKELY(buf.len >= digits+2u)
1345  {
1346  buf.str[0] = '0';
1347  buf.str[1] = 'x';
1348  write_hex_unchecked(buf.sub(2), v, digits);
1349  }
1350  digits += 2u;
1351  break;
1352  case T(2):
1353  digits = digits_bin(v);
1354  if C4_LIKELY(buf.len >= digits+2u)
1355  {
1356  buf.str[0] = '0';
1357  buf.str[1] = 'b';
1358  write_bin_unchecked(buf.sub(2), v, digits);
1359  }
1360  digits += 2u;
1361  break;
1362  case T(8):
1363  digits = digits_oct(v);
1364  if C4_LIKELY(buf.len >= digits+2u)
1365  {
1366  buf.str[0] = '0';
1367  buf.str[1] = 'o';
1368  write_oct_unchecked(buf.sub(2), v, digits);
1369  }
1370  digits += 2u;
1371  break;
1372  }
1373  return digits;
1374 }
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:614
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:538
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:663
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
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:547
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:647
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:529
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:679
template<class T >
size_t c4::utoa ( substr  buf,
T  v,
T  radix,
size_t  num_digits 
)
inlinenoexcept

same as c4::utoa(), but pad with zeroes on the left such that the resulting string is num_digits wide.

The radix must be 2, 8, 10 or 16.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1385 of file charconv.hpp.

1386 {
1387  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1388  C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1389  unsigned total_digits = 0;
1390  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1391  {
1392  case T(10):
1393  total_digits = digits_dec(v);
1394  total_digits = (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1395  if C4_LIKELY(buf.len >= total_digits)
1396  write_dec(buf, v, num_digits);
1397  break;
1398  case T(16):
1399  total_digits = digits_hex(v);
1400  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1401  if C4_LIKELY(buf.len >= total_digits)
1402  {
1403  buf.str[0] = '0';
1404  buf.str[1] = 'x';
1405  write_hex(buf.sub(2), v, num_digits);
1406  }
1407  break;
1408  case T(2):
1409  total_digits = digits_bin(v);
1410  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1411  if C4_LIKELY(buf.len >= total_digits)
1412  {
1413  buf.str[0] = '0';
1414  buf.str[1] = 'b';
1415  write_bin(buf.sub(2), v, num_digits);
1416  }
1417  break;
1418  case T(8):
1419  total_digits = digits_oct(v);
1420  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1421  if C4_LIKELY(buf.len >= total_digits)
1422  {
1423  buf.str[0] = '0';
1424  buf.str[1] = 'o';
1425  write_oct(buf.sub(2), v, num_digits);
1426  }
1427  break;
1428  }
1429  return total_digits;
1430 }
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:538
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
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:547
size_t write_hex(substr buf, T val, size_t num_digits) noexcept
same as c4::write_hex(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:821
size_t write_dec(substr buf, T val, size_t num_digits) noexcept
same as c4::write_dec(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:812
size_t write_bin(substr buf, T val, size_t num_digits) noexcept
same as c4::write_bin(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:830
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:529
size_t write_oct(substr buf, T val, size_t num_digits) noexcept
same as c4::write_oct(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:839