...
This commit is contained in:
@@ -32,8 +32,7 @@ context : Context = ---;
|
||||
// --- Slice & string allocation ---
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
p : s64 = xx context.allocator.ctx;
|
||||
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
s : string = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -42,8 +41,7 @@ cstring :: (size: s64) -> string {
|
||||
}
|
||||
|
||||
alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
p : s64 = xx context.allocator.ctx;
|
||||
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
|
||||
memset(raw, 0, count * size_of(T));
|
||||
s : []T = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -55,19 +53,16 @@ int_to_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
neg := n < 0;
|
||||
v := if neg then 0 - n else n;
|
||||
tmp := v;
|
||||
len := 0;
|
||||
while tmp > 0 { len += 1; tmp = tmp / 10; }
|
||||
total := if neg then len + 1 else len;
|
||||
buf := cstring(total);
|
||||
i := total - 1;
|
||||
// Single pass: fill digits backwards into temp string, then substr
|
||||
tmp := cstring(20);
|
||||
i := 19;
|
||||
while v > 0 {
|
||||
buf[i] = (v % 10) + 48;
|
||||
tmp[i] = (v % 10) + 48;
|
||||
v = v / 10;
|
||||
i -= 1;
|
||||
}
|
||||
if neg { buf[0] = 45; }
|
||||
buf;
|
||||
if neg { tmp[i] = 45; i -= 1; }
|
||||
substr(tmp, i + 1, 20 - i - 1);
|
||||
}
|
||||
|
||||
bool_to_string :: (b: bool) -> string {
|
||||
@@ -100,6 +95,17 @@ float_to_string :: (f: f64) -> string {
|
||||
buf;
|
||||
}
|
||||
|
||||
hex_group :: (buf: string, offset: s64, val: s64) {
|
||||
i := offset + 3;
|
||||
v := val;
|
||||
while i >= offset {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
int_to_hex_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
|
||||
@@ -117,42 +123,11 @@ int_to_hex_string :: (n: s64) -> string {
|
||||
if g3 < 0 { g3 = g3 + 65536; }
|
||||
|
||||
buf := cstring(16);
|
||||
// Group 3: digits 0-3 (bits 48-63)
|
||||
i := 3;
|
||||
v := g3;
|
||||
while i >= 0 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 2: digits 4-7 (bits 32-47)
|
||||
i = 7;
|
||||
v = g2;
|
||||
while i >= 4 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 1: digits 8-11 (bits 16-31)
|
||||
i = 11;
|
||||
v = g1;
|
||||
while i >= 8 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 0: digits 12-15 (bits 0-15)
|
||||
i = 15;
|
||||
v = g0;
|
||||
while i >= 12 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
hex_group(buf, 0, g3);
|
||||
hex_group(buf, 4, g2);
|
||||
hex_group(buf, 8, g1);
|
||||
hex_group(buf, 12, g0);
|
||||
|
||||
// Skip leading zeros (keep at least 1 digit)
|
||||
start := 0;
|
||||
while start < 15 {
|
||||
|
||||
Reference in New Issue
Block a user