rapidyaml  0.10.0
parse and emit YAML, and do it fast
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

◆ utoa() [1/3]

template<class T >
size_t c4::utoa ( substr  buf,
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 1303 of file charconv.hpp.

1304 {
1305  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1306  // write_dec() does the buffer length check, so no need to check here
1307  return write_dec(buf, v);
1308 }
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:800

References c4::write_dec().

Referenced by c4::to_chars(), and c4::xtoa().

◆ utoa() [2/3]

template<class T >
size_t c4::utoa ( substr  buf,
v,
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 1318 of file charconv.hpp.

1319 {
1320  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1321  C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1322  unsigned digits = 0;
1323  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1324  {
1325  case T(10):
1326  digits = digits_dec(v);
1327  if(C4_LIKELY(buf.len >= digits))
1328  write_dec_unchecked(buf, v, digits);
1329  break;
1330  case T(16):
1331  digits = digits_hex(v);
1332  if(C4_LIKELY(buf.len >= digits+2u))
1333  {
1334  buf.str[0] = '0';
1335  buf.str[1] = 'x';
1336  write_hex_unchecked(buf.sub(2), v, digits);
1337  }
1338  digits += 2u;
1339  break;
1340  case T(2):
1341  digits = digits_bin(v);
1342  if(C4_LIKELY(buf.len >= digits+2u))
1343  {
1344  buf.str[0] = '0';
1345  buf.str[1] = 'b';
1346  write_bin_unchecked(buf.sub(2), v, digits);
1347  }
1348  digits += 2u;
1349  break;
1350  case T(8):
1351  digits = digits_oct(v);
1352  if(C4_LIKELY(buf.len >= digits+2u))
1353  {
1354  buf.str[0] = '0';
1355  buf.str[1] = 'o';
1356  write_oct_unchecked(buf.sub(2), v, digits);
1357  }
1358  digits += 2u;
1359  break;
1360  }
1361  return digits;
1362 }
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
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:535
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:517
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:526
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:667
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:602
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:635
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:651

References c4::digits_bin(), c4::digits_dec(), c4::digits_hex(), c4::digits_oct(), c4::write_bin_unchecked(), c4::write_dec_unchecked(), c4::write_hex_unchecked(), and c4::write_oct_unchecked().

◆ utoa() [3/3]

template<class T >
size_t c4::utoa ( substr  buf,
v,
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 1373 of file charconv.hpp.

1374 {
1375  C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1376  C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1377  unsigned total_digits = 0;
1378  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1379  {
1380  case T(10):
1381  total_digits = digits_dec(v);
1382  total_digits = (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1383  if(C4_LIKELY(buf.len >= total_digits))
1384  write_dec(buf, v, num_digits);
1385  break;
1386  case T(16):
1387  total_digits = digits_hex(v);
1388  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1389  if(C4_LIKELY(buf.len >= total_digits))
1390  {
1391  buf.str[0] = '0';
1392  buf.str[1] = 'x';
1393  write_hex(buf.sub(2), v, num_digits);
1394  }
1395  break;
1396  case T(2):
1397  total_digits = digits_bin(v);
1398  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1399  if(C4_LIKELY(buf.len >= total_digits))
1400  {
1401  buf.str[0] = '0';
1402  buf.str[1] = 'b';
1403  write_bin(buf.sub(2), v, num_digits);
1404  }
1405  break;
1406  case T(8):
1407  total_digits = digits_oct(v);
1408  total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1409  if(C4_LIKELY(buf.len >= total_digits))
1410  {
1411  buf.str[0] = '0';
1412  buf.str[1] = 'o';
1413  write_oct(buf.sub(2), v, num_digits);
1414  }
1415  break;
1416  }
1417  return total_digits;
1418 }
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:809
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:818
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:827

References c4::digits_bin(), c4::digits_dec(), c4::digits_hex(), c4::digits_oct(), c4::write_bin(), c4::write_dec(), c4::write_hex(), and c4::write_oct().