io.snprintf

This commit is contained in:
2026-06-05 09:47:59 +02:00
parent 803aee772e
commit be42267a59
5 changed files with 67 additions and 18 deletions

View File

@@ -283,6 +283,67 @@ func io.printf[..] : void
io.print_char(s[0])
s = s + 1
func io.snprintf[..] : i64
buf := _var_arg(0) as ptr
size := _var_arg(1) as i64
if size <= 0
return 0
s := _var_arg(2) as ptr
i := 3
n := 0
while s[0]
if s[0] == '%'
s = s + 1
if s[0] == 'd'
tmp := _stackalloc(21)
str.from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 'x'
tmp := _stackalloc(17)
str.hex_from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 's'
tmp_str := _var_arg(i) as str
tmp_len := str.len(tmp_str)
remaining := size - n - 1
mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 'c'
if n < size - 1
buf[n] = _var_arg(i) as u8
n = n + 1
i = i + 1
else if s[0] == '%'
if n < size - 1
buf[n] = '%'
n = n + 1
else if s[0] == 0
break
else
panic("io.snprintf: unrecognized format")
else
if n < size - 1
buf[n] = s[0]
n = n + 1
s = s + 1
if n < size
buf[n] = 0
else if size > 0
buf[size - 1] = 0
return n
func io.print_sized[x: ptr, size: i64] : void
_builtin_syscall(SYS_write, 1, x, size)