rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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.
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.
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.

Detailed Description

Function Documentation

◆ utoa() [1/3]

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.

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 v) noexcept
write an integer to a string in decimal format.
Definition charconv.hpp:714

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

◆ utoa() [2/3]

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}
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
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition charconv.hpp:529
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition charconv.hpp:538
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition charconv.hpp:679
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition charconv.hpp:614
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition charconv.hpp:647
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition charconv.hpp:663
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:502
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ utoa() [3/3]

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}
size_t write_hex(substr buf, T v) noexcept
write an integer to a string in hexadecimal format.
Definition charconv.hpp:733
size_t write_bin(substr buf, T v) noexcept
write an integer to a string in binary format.
Definition charconv.hpp:771
size_t write_oct(substr buf, T v) noexcept
write an integer to a string in octal format.
Definition charconv.hpp:752