commit 07759c39a999a00756834fc00a3f5f2360ebe5b5
parent eff709028d3aea77de55bbf8743a27cf9a3207c3
Author: Ember Sawady <ecs@d2evs.net>
Date: Wed, 6 Sep 2023 08:11:37 +0000
rt: replace ztos with u64tos
Signed-off-by: Ember Sawady <ecs@d2evs.net>
Diffstat:
5 files changed, 27 insertions(+), 45 deletions(-)
diff --git a/rt/u64tos.ha b/rt/u64tos.ha
@@ -0,0 +1,19 @@
+fn u64tos(u: u64) (*const u8, size) = {
+ static let buf: [20]u8 = [0...]; // len("18446744073709551615")
+ let sl = buf[..0];
+ if (u == 0) {
+ static append(sl, '0');
+ };
+ for (u > 0) {
+ static append(sl, (u % 10): u8 + '0');
+ u /= 10;
+ };
+ for (let s = 0z, e = len(sl) - 1; s < e) {
+ let tmp = sl[s];
+ sl[s] = sl[e];
+ sl[e] = tmp;
+ s += 1;
+ e -= 1;
+ };
+ return (sl: *[*]u8: *const u8, len(sl));
+};
diff --git a/rt/unknown_errno.ha b/rt/unknown_errno.ha
@@ -4,12 +4,12 @@ fn unknown_errno(err: errno) str = {
buf[..len(s)] = s;
if (err < 0) {
buf[len(s)] = '-';
- const s2 = &ztos(-err: size): *string;
+ const s2 = &u64tos(-err: u64): *string;
buf[len(s) + 1..s2.length + len(s) + 1] = s2.data[..s2.length];
buf[s2.length + len(s) + 1] = ']';
return *(&buf[..s2.length + len(s) + 2]: *str);
} else {
- const s2 = &ztos(err: size): *string;
+ const s2 = &u64tos(err: u64): *string;
buf[len(s)..s2.length + len(s)] = s2.data[..s2.length];
buf[s2.length + len(s)] = ']';
return *(&buf[..s2.length + len(s) + 1]: *str);
diff --git a/rt/ztos.ha b/rt/ztos.ha
@@ -1,37 +0,0 @@
-fn bytes_reverse(b: []u8) void = {
- if (len(b) == 0) {
- return;
- };
- for (let s = 0z, e = len(b) - 1; s < e) {
- let x = b[s];
- b[s] = b[e];
- b[e] = x;
- s += 1;
- e -= 1;
- };
-};
-
-fn ztos(u: size) const str = {
- static let buf: [20]u8 = [0...];
- buf = [0...];
-
- let s = struct {
- b: *[*]u8 = &buf,
- l: size = 0,
- c: size = 0,
- };
-
- if (u == 0) {
- s.b[s.l] = '0';
- s.l += 1;
- };
-
- for (u > 0) {
- s.b[s.l] = '0' + (u % 10): u8;
- s.l += 1;
- u /= 10;
- };
-
- bytes_reverse(s.b[..s.l]);
- return *(&s: *str);
-};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -44,7 +44,7 @@ gensrcs_rt() {
memset.ha \
strcmp.ha \
unknown_errno.ha \
- ztos.ha \
+ u64tos.ha \
$*
gen_srcs -pfreebsd rt \
+freebsd/platform_abort.ha \
@@ -70,7 +70,7 @@ gensrcs_rt() {
memset.ha \
strcmp.ha \
unknown_errno.ha \
- ztos.ha \
+ u64tos.ha \
$*
}
diff --git a/stdlib.mk b/stdlib.mk
@@ -28,7 +28,7 @@ stdlib_rt_linux_srcs = \
$(STDLIB)/rt/memset.ha \
$(STDLIB)/rt/strcmp.ha \
$(STDLIB)/rt/unknown_errno.ha \
- $(STDLIB)/rt/ztos.ha \
+ $(STDLIB)/rt/u64tos.ha \
$(STDLIB)/rt/abort.ha \
$(STDLIB)/rt/start.ha
@@ -57,7 +57,7 @@ stdlib_rt_freebsd_srcs = \
$(STDLIB)/rt/memset.ha \
$(STDLIB)/rt/strcmp.ha \
$(STDLIB)/rt/unknown_errno.ha \
- $(STDLIB)/rt/ztos.ha \
+ $(STDLIB)/rt/u64tos.ha \
$(STDLIB)/rt/abort.ha \
$(STDLIB)/rt/start.ha
@@ -2470,7 +2470,7 @@ testlib_rt_linux_srcs = \
$(STDLIB)/rt/memset.ha \
$(STDLIB)/rt/strcmp.ha \
$(STDLIB)/rt/unknown_errno.ha \
- $(STDLIB)/rt/ztos.ha \
+ $(STDLIB)/rt/u64tos.ha \
$(STDLIB)/rt/abort+test.ha \
$(STDLIB)/rt/start+test.ha \
$(STDLIB)/rt/+test/signal_test.ha
@@ -2500,7 +2500,7 @@ testlib_rt_freebsd_srcs = \
$(STDLIB)/rt/memset.ha \
$(STDLIB)/rt/strcmp.ha \
$(STDLIB)/rt/unknown_errno.ha \
- $(STDLIB)/rt/ztos.ha \
+ $(STDLIB)/rt/u64tos.ha \
$(STDLIB)/rt/abort+test.ha \
$(STDLIB)/rt/start+test.ha \
$(STDLIB)/rt/+test/signal_test.ha