From 4c07ef05522dc90b66ef10fd4f8c1cbba35693c3 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 20 May 2026 23:38:03 +0900 Subject: [PATCH] =?UTF-8?q?lib/strings/dup:=20rt.malloc=20=E2=86=92=20allo?= =?UTF-8?q?c([],=20n)!=20slice=20form?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pilot for task #8 (runtime-N alloc API). `alloc([], n)!` yields a slice with cap=n, len=0; explicit `buf.len = s.len;` lifts the len before the fromutf8_unsafe reinterpret. Same shape as ref/hare/strings/dup.ha:15 modulo ww not yet having `append` (task #36) — open-coded byte loop in lieu of static-append. Verified 132/132 + 995_self_rebuild byte-identity. Pattern is the template for the next α-category sites (concat/join/lpad/rpad/...). --- lib/strings/strings.ww | 7 +++---- selfhost/cmd/w6c/main.combined.ww | 7 +++---- selfhost/cmd/wwdump/main.combined.ww | 7 +++---- selfhost/test/smoke.combined.ww | 7 +++---- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/strings/strings.ww b/lib/strings/strings.ww index 1c306dba..b004ad30 100644 --- a/lib/strings/strings.ww +++ b/lib/strings/strings.ww @@ -99,12 +99,11 @@ export fn dup(s: str) str = { r.ptr = nil; r.len = 0; if (s.len == 0) { return r; }; - let buf: *u8 = rt.malloc(s.len: u64): *u8; + let buf: []u8 = alloc([], s.len: u64)!; let i: i32 = 0; for (i < s.len) { buf[i] = s[i]; i += 1; }; - r.ptr = buf; - r.len = s.len; - return r; + buf.len = s.len; + return fromutf8_unsafe(buf); }; // dupall — fresh `[]str` whose elements are independent copies of diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e3ace8a2..c7f92ea3 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -1938,12 +1938,11 @@ export fn dup(s: str) str = { r.ptr = nil; r.len = 0; if (s.len == 0) { return r; }; - let buf: *u8 = rt.malloc(s.len: u64): *u8; + let buf: []u8 = alloc([], s.len: u64)!; let i: i32 = 0; for (i < s.len) { buf[i] = s[i]; i += 1; }; - r.ptr = buf; - r.len = s.len; - return r; + buf.len = s.len; + return fromutf8_unsafe(buf); }; // dupall — fresh `[]str` whose elements are independent copies of diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index d1334983..172596af 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -1938,12 +1938,11 @@ export fn dup(s: str) str = { r.ptr = nil; r.len = 0; if (s.len == 0) { return r; }; - let buf: *u8 = rt.malloc(s.len: u64): *u8; + let buf: []u8 = alloc([], s.len: u64)!; let i: i32 = 0; for (i < s.len) { buf[i] = s[i]; i += 1; }; - r.ptr = buf; - r.len = s.len; - return r; + buf.len = s.len; + return fromutf8_unsafe(buf); }; // dupall — fresh `[]str` whose elements are independent copies of diff --git a/selfhost/test/smoke.combined.ww b/selfhost/test/smoke.combined.ww index 3339fda9..e611545d 100644 --- a/selfhost/test/smoke.combined.ww +++ b/selfhost/test/smoke.combined.ww @@ -1828,12 +1828,11 @@ export fn dup(s: str) str = { r.ptr = nil; r.len = 0; if (s.len == 0) { return r; }; - let buf: *u8 = rt.malloc(s.len: u64): *u8; + let buf: []u8 = alloc([], s.len: u64)!; let i: i32 = 0; for (i < s.len) { buf[i] = s[i]; i += 1; }; - r.ptr = buf; - r.len = s.len; - return r; + buf.len = s.len; + return fromutf8_unsafe(buf); }; // dupall — fresh `[]str` whose elements are independent copies of