ableos/sysdata/libraries/stn/src/string.hb
2024-10-20 13:31:44 +01:00

71 lines
993 B
Plaintext

length := fn(ptr: ^u8): uint {
len := @as(uint, 0)
loop if *(ptr + len) == 0 break else len += 1
return len
}
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 {
digit := num % radix
if digit < 10 {
*ptr = digit + 48
} else {
*ptr = digit + 55
}
ptr += 1
num /= radix
}
}
if negative {
*ptr = 45
ptr += 1
};
*ptr = 0
@inline(reverse, digits_start)
return p
}
reverse := fn(s: ^u8): void {
len := @inline(length, s)
i := 0
j := len - 1
temp := 0
loop if i >= j break else {
temp = *(s + i);
*(s + i) = *(s + j);
*(s + j) = temp
i += 1
j -= 1
}
return
}