diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-16 23:21:34 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-16 23:21:34 +0200 |
commit | 7fee0fc28809042a2ecbc03f2e1b5b569073982b (patch) | |
tree | 8058579d05f55fc3c36567b68d3ef0314cd72ed0 /src/badlib.h | |
parent | 357f02dc5f1f2ad99210faf13c2b1fd7dff6e669 (diff) | |
download | bdl-7fee0fc28809042a2ecbc03f2e1b5b569073982b.tar.gz bdl-7fee0fc28809042a2ecbc03f2e1b5b569073982b.zip |
Add support for parsing long long unsigned integers if using hex
Diffstat (limited to 'src/badlib.h')
-rw-r--r-- | src/badlib.h | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/src/badlib.h b/src/badlib.h index d334fa7..6432562 100644 --- a/src/badlib.h +++ b/src/badlib.h | |||
@@ -470,13 +470,13 @@ str_from_int(sz num, Arena *a) { | |||
470 | } | 470 | } |
471 | 471 | ||
472 | Str | 472 | Str |
473 | str_from_hex(sz num, sz zeroes, Arena *a) { | 473 | str_from_hex(u64 num, sz zeroes, Arena *a) { |
474 | char char_map[] = "0123456789abcdef"; | 474 | char char_map[] = "0123456789abcdef"; |
475 | u8 tmp[64]; | 475 | u8 tmp[64]; |
476 | zeroes = MIN((sz)sizeof(ptrsize) * 2, zeroes); | 476 | zeroes = MIN((sz)sizeof(ptrsize) * 2, zeroes); |
477 | u8 *end = tmp + sizeof(tmp); | 477 | u8 *end = tmp + sizeof(tmp); |
478 | u8 *beg = end; | 478 | u8 *beg = end; |
479 | sz t = num > 0 ? num : -num; | 479 | u64 t = num; |
480 | do { | 480 | do { |
481 | *--beg = char_map[t % 16]; | 481 | *--beg = char_map[t % 16]; |
482 | } while (t /= 16); | 482 | } while (t /= 16); |
@@ -634,6 +634,57 @@ str_to_int(Str s) { | |||
634 | return num; | 634 | return num; |
635 | } | 635 | } |
636 | 636 | ||
637 | u64 | ||
638 | str_to_uint(Str s) { | ||
639 | u64 num = 0; | ||
640 | if (str_has_prefix(s, cstr("0b"))) { | ||
641 | // Binary number. | ||
642 | s = str_remove_prefix(s, cstr("0b")); | ||
643 | while (s.size) { | ||
644 | char c = str_next(&s); | ||
645 | if (c == '_') { | ||
646 | continue; | ||
647 | } | ||
648 | assert(c == '0' || c == '1'); | ||
649 | num = num * 2 + (c - '0'); | ||
650 | } | ||
651 | } else if (str_has_prefix(s, cstr("0x"))) { | ||
652 | // Hex number. | ||
653 | s = str_remove_prefix(s, cstr("0x")); | ||
654 | while (s.size) { | ||
655 | char c = str_next(&s); | ||
656 | if (c == '_') { | ||
657 | continue; | ||
658 | } | ||
659 | assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || | ||
660 | (c >= 'A' && c <= 'F')); | ||
661 | if (c >= '0' && c <= '9') { | ||
662 | num = num * 16 + (c - '0'); | ||
663 | } else if (c >= 'a' && c <= 'f') { | ||
664 | num = num * 16 + (c - 'a' + 10); | ||
665 | } else if (c >= 'A' && c <= 'F') { | ||
666 | num = num * 16 + (c - 'A' + 10); | ||
667 | } | ||
668 | } | ||
669 | } else { | ||
670 | // Decimal number. | ||
671 | char c = str_peek(s); | ||
672 | assert(c != '-'); | ||
673 | if (c == '+') { | ||
674 | str_next(&s); | ||
675 | } | ||
676 | while (s.size) { | ||
677 | char c = str_next(&s); | ||
678 | if (c == '_') { | ||
679 | continue; | ||
680 | } | ||
681 | assert(c >= '0' && c <= '9'); | ||
682 | num = num * 10 + (c - '0'); | ||
683 | } | ||
684 | } | ||
685 | return num; | ||
686 | } | ||
687 | |||
637 | f64 | 688 | f64 |
638 | str_to_float(Str s) { | 689 | str_to_float(Str s) { |
639 | char c = str_peek(s); | 690 | char c = str_peek(s); |