minor changes

This commit is contained in:
Able 2024-09-16 20:45:00 -05:00
parent 48027196b1
commit 3173b63c93

View file

@ -3,8 +3,6 @@ length := fn(ptr: ^u8): int {
loop if *(ptr + len) == 0 break else len += 1 loop if *(ptr + len) == 0 break else len += 1
return len return len
} }
// WTFFF is wrong with display_int
display_int := fn(num: int, p: ^u8): ^u8 { display_int := fn(num: int, p: ^u8): ^u8 {
ptr := p ptr := p
negative := num < 0 negative := num < 0
@ -12,37 +10,42 @@ display_int := fn(num: int, p: ^u8): ^u8 {
num = -num num = -num
} }
if num == 0 { if num == 0 {
*ptr = 48 *p = 48;
ptr += 1 *(p + 1) = 0
} else { return p
}
neg := false
if num < 0 {
neg = true
num = -num
}
loop if num == 0 break else { loop if num == 0 break else {
*ptr = num % 10 + 48 *(p + i) = num % 10 + 48
ptr += 1
num /= 10 num /= 10
i += 1
} }
if neg {
*(p + i) = 45
i += 1
} }
if negative {
*ptr = 45 reverse(p, i)
ptr += 1
};
*ptr = 0
@inline(reverse, p)
return p return p
} }
reverse := fn(s: ^u8): void { reverse := fn(p: ^u8, len: int): void {
//reverse a string, don't remove digits start := 0
len := 0 end := len - 1
loop if *(s + len) == 0 break else len += 1 loop if start >= end break else {
i := 0 temp := *(p + start);
j := len - 1 *(p + start) = *(p + end);
temp := 0 *(p + end) = temp
loop if i >= j break else { start += 1
temp = *(s + i); end -= 1
*(s + i) = *(s + j);
*(s + j) = temp
i += 1
j -= 1
} }
return return
} }