ableos/sysdata/libraries/stn/src/string.hb

71 lines
993 B
Plaintext
Raw Normal View History

length := fn(ptr: ^u8): uint {
len := @as(uint, 0)
loop if *(ptr + len) == 0 break else len += 1
2024-07-19 08:53:45 -05:00
return len
}
2024-09-17 09:52:27 -05:00
2024-10-20 07:31:44 -05:00
display_int := fn(num: int, p: ^u8, radix: int): ^u8 {
2024-09-16 15:56:52 -05:00
ptr := p
negative := num < 0
if negative {
2024-09-16 20:45:00 -05:00
num = -num
}
2024-10-20 07:31:44 -05:00
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
2024-09-16 15:56:52 -05:00
if num == 0 {
2024-09-17 09:52:27 -05:00
*ptr = 48
ptr += 1
} else {
loop if num == 0 break else {
2024-10-20 07:31:44 -05:00
digit := num % radix
if digit < 10 {
*ptr = digit + 48
} else {
*ptr = digit + 55
}
2024-09-17 09:52:27 -05:00
ptr += 1
2024-10-20 07:31:44 -05:00
num /= radix
2024-09-17 09:52:27 -05:00
}
2024-09-16 20:45:00 -05:00
}
2024-10-20 07:31:44 -05:00
2024-09-17 09:52:27 -05:00
if negative {
*ptr = 45
ptr += 1
};
2024-10-20 07:31:44 -05:00
2024-09-17 09:52:27 -05:00
*ptr = 0
2024-10-20 07:31:44 -05:00
@inline(reverse, digits_start)
2024-07-19 08:53:45 -05:00
return p
}
2024-09-17 09:52:27 -05:00
reverse := fn(s: ^u8): void {
2024-10-20 06:36:34 -05:00
len := @inline(length, s)
2024-09-17 09:52:27 -05:00
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
2024-07-19 08:53:45 -05:00
}
return
2024-07-20 12:54:58 -05:00
}