1
0
Fork 0
forked from AbleOS/ableos
ableos/sysdata/libraries/stn/src/string.hb

71 lines
1,015 B
Plaintext
Raw Normal View History

2024-11-03 16:31:53 -06:00
length := fn(ptr: ^u8): uint {
2024-11-03 16:31:53 -06:00
len := 0
loop if *(ptr + len) == 0 return len else len += 1
}
2024-09-17 09:52:27 -05:00
2024-10-25 10:37:38 -05:00
display_int := fn(num: int, p: ^u8, radix: uint): ^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-25 10:37:38 -05:00
digit := num % @bitcast(radix)
2024-10-20 07:31:44 -05:00
if digit < 10 {
2024-10-25 10:37:38 -05:00
*ptr = @intcast(digit) + 48
2024-10-20 07:31:44 -05:00
} else {
2024-10-25 10:37:38 -05:00
*ptr = @intcast(digit) + 55
2024-10-20 07:31:44 -05:00
}
2024-09-17 09:52:27 -05:00
ptr += 1
2024-10-25 10:37:38 -05:00
num /= @bitcast(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-10-25 10:37:38 -05:00
2024-09-17 09:52:27 -05:00
reverse := fn(s: ^u8): void {
2024-11-03 16:31:53 -06:00
i := 0
2024-10-25 10:37:38 -05:00
j := @inline(length, s) - 1
temp := @as(u8, 0)
2024-09-17 09:52:27 -05:00
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
}