rapidyaml  0.7.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 1304 of file charconv.hpp.

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

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

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

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

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

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