From b35b430047f1c1a7a3bcc3b6eda3f92bd36a67c0 Mon Sep 17 00:00:00 2001 From: koniifer Date: Sun, 20 Oct 2024 13:31:44 +0100 Subject: [PATCH] display_int radices --- sysdata/libraries/stn/src/string.hb | 37 ++++++++++++++++++++++++----- sysdata/programs/tests/src/main.hb | 2 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/sysdata/libraries/stn/src/string.hb b/sysdata/libraries/stn/src/string.hb index 679c180..c84ac55 100644 --- a/sysdata/libraries/stn/src/string.hb +++ b/sysdata/libraries/stn/src/string.hb @@ -4,32 +4,57 @@ length := fn(ptr: ^u8): uint { return len } -// WTFFF is wrong with display_int -display_int := fn(num: int, p: ^u8): ^u8 { +display_int := fn(num: int, p: ^u8, radix: int): ^u8 { ptr := p negative := num < 0 if negative { num = -num } + + if radix == 2 { + *ptr = 48 + ptr += 1; + *ptr = 98 + ptr += 1 + } else if radix == 16 { + *ptr = 48 + ptr += 1; + *ptr = 120 + ptr += 1 + } else if radix == 8 { + *ptr = 48 + ptr += 1; + *ptr = 111 + ptr += 1 + } + digits_start := ptr if num == 0 { *ptr = 48 ptr += 1 } else { loop if num == 0 break else { - *ptr = num % 10 + 48 + digit := num % radix + if digit < 10 { + *ptr = digit + 48 + } else { + *ptr = digit + 55 + } ptr += 1 - num /= 10 + num /= radix } } + if negative { *ptr = 45 ptr += 1 }; + *ptr = 0 - @inline(reverse, p) + + @inline(reverse, digits_start) + return p } - reverse := fn(s: ^u8): void { len := @inline(length, s) i := 0 diff --git a/sysdata/programs/tests/src/main.hb b/sysdata/programs/tests/src/main.hb index e89eafb..b2eee5e 100644 --- a/sysdata/programs/tests/src/main.hb +++ b/sysdata/programs/tests/src/main.hb @@ -12,7 +12,7 @@ main := fn(): int { buf := "\0\0\0\0" x := 0 loop if x == 255 break else { - log.info(string.display_int(x, buf)) + log.info(string.display_int(x, buf, 10)) x += 1 } return 0