hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit efeba759edc509df57f4136a7846433ce8685e0d
parent a451ac939c64cde8ed34e3755e043668b3bd4823
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 12 Feb 2021 10:41:10 -0500

strconv: add lowercase hex support

Diffstat:
Mstrconv/types.ha | 6+++++-
Mstrconv/utos.ha | 12+++++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/strconv/types.ha b/strconv/types.ha @@ -13,6 +13,10 @@ export type base = enum uint { OCT = 8, // Base 10, decimal DEC = 10, - // Base 16, hexadecimal + // Base 16, UPPERCASE hexadecimal + HEX_UPPER = 16, + // Alias for HEX_UPPER HEX = 16, + // Base 16, lowercase hexadecimal + HEX_LOWER = 17, }; diff --git a/strconv/utos.ha b/strconv/utos.ha @@ -6,10 +6,18 @@ use types; // duplicate the result. export fn u64tosb(u: u64, b: base) const str = { static assert(types::U64_MAX == 18446744073709551615); - static const lut: [_]rune = [ + static const lut_upper: [_]rune = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', ]; + static const lut_lower: [_]rune = [ + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', + ]; + let lut = if (b != base::HEX_LOWER) lut_upper else { + b = base::HEX_UPPER; + lut_lower; + }; static let buf: [21]u8 = [0...]; // 20 digits plus NUL buf = [0...]; @@ -96,6 +104,8 @@ export fn uptrtos(uptr: uintptr) const str = u64tos(uptr: u64); assert("1234567" == u64tosb(0o1234567, base::OCT)); assert("123456789" == u64tosb(123456789, base::DEC)); assert("123456789ABCDEF" == u64tosb(0x123456789ABCDEF, base::HEX)); + assert("123456789ABCDEF" == u64tosb(0x123456789ABCDEF, base::HEX_UPPER)); + assert("123456789abcdef" == u64tosb(0x123456789ABCDEF, base::HEX_LOWER)); }; @test fn utos() void = {