rapidyaml 0.14.0
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 1316 of file charconv.hpp.

1317{
1318 C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1319 // write_dec() does the buffer length check, so no need to check here
1320 return write_dec(buf, v);
1321}
size_t write_dec(substr buf, T v) noexcept
write an integer to a string in decimal format.
Definition charconv.hpp:715

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

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

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

1387{
1388 C4_STATIC_ASSERT(std::is_unsigned<T>::value);
1389 C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
1390 unsigned total_digits = 0;
1391 switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1392 {
1393 case T(10):
1394 total_digits = digits_dec(v);
1395 total_digits = (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1396 if(C4_LIKELY(buf.len >= total_digits))
1397 write_dec(buf, v, num_digits);
1398 break;
1399 case T(16):
1400 total_digits = digits_hex(v);
1401 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1402 if(C4_LIKELY(buf.len >= total_digits))
1403 {
1404 buf.str[0] = '0';
1405 buf.str[1] = 'x';
1406 write_hex(buf.sub(2), v, num_digits);
1407 }
1408 break;
1409 case T(2):
1410 total_digits = digits_bin(v);
1411 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1412 if(C4_LIKELY(buf.len >= total_digits))
1413 {
1414 buf.str[0] = '0';
1415 buf.str[1] = 'b';
1416 write_bin(buf.sub(2), v, num_digits);
1417 }
1418 break;
1419 case T(8):
1420 total_digits = digits_oct(v);
1421 total_digits = 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1422 if(C4_LIKELY(buf.len >= total_digits))
1423 {
1424 buf.str[0] = '0';
1425 buf.str[1] = 'o';
1426 write_oct(buf.sub(2), v, num_digits);
1427 }
1428 break;
1429 }
1430 return total_digits;
1431}