rapidyaml  0.12.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 1302 of file charconv.hpp.

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

References c4::write_dec().

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

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

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

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

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().