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

48 lines
742 B
Plaintext
Raw Normal View History

2024-07-07 08:35:07 -05:00
length := fn(ptr: ^u8): int {
2024-07-19 08:53:45 -05:00
len := 0
loop if *(ptr + len) == 0 break else len += 1
2024-07-19 08:53:45 -05:00
return len
}
2024-09-16 20:45:00 -05:00
2024-09-16 15:56:52 -05:00
// WTFFF is wrong with display_int
display_int := fn(num: int, p: ^u8): ^u8 {
ptr := p
negative := num < 0
if negative {
2024-09-16 20:45:00 -05:00
num = -num
}
2024-09-16 15:56:52 -05:00
if num == 0 {
*ptr = 48
ptr += 1
} else {
loop if num == 0 break else {
*ptr = num % 10 + 48
ptr += 1
num /= 10
}
2024-09-16 20:45:00 -05:00
}
2024-09-16 15:56:52 -05:00
if negative {
*ptr = 45
ptr += 1
};
*ptr = 0
@inline(reverse, p)
2024-07-19 08:53:45 -05:00
return p
}
2024-09-16 15:56:52 -05:00
reverse := fn(s: ^u8): void {
//reverse a string, don't remove digits
len := 0
loop if *(s + len) == 0 break else len += 1
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
}