216 lines
3.3 KiB
Plaintext
216 lines
3.3 KiB
Plaintext
length := fn(ptr: ^u8): uint {
|
|
len := 0
|
|
loop if *(ptr + len) == 0 return len else len += 1
|
|
}
|
|
|
|
display_int := fn(num: int, p: ^u8, radix: uint): ^u8 {
|
|
is_negative := num < 0
|
|
if is_negative num = -num
|
|
|
|
ptr := p
|
|
|
|
if num == 0 {
|
|
*ptr = 0x30;
|
|
*(ptr + 1) = 0
|
|
return p
|
|
}
|
|
|
|
loop if num == 0 break else {
|
|
remainder := num % @bitcast(radix)
|
|
num /= @bitcast(radix);
|
|
*ptr = @intcast(remainder + 0x30)
|
|
if remainder > 9 {
|
|
*ptr = @intcast(remainder - 10 + 0x41)
|
|
}
|
|
ptr += 1
|
|
}
|
|
|
|
if is_negative {
|
|
*ptr = 0x2D
|
|
ptr += 1
|
|
}
|
|
// ! it gets broked when you do this ??
|
|
// *ptr = 0
|
|
|
|
@inline(reverse, p)
|
|
return p
|
|
}
|
|
|
|
reverse := fn(s: ^u8): void {
|
|
j := s + @inline(length, s) - 1
|
|
temp := @as(u8, 0)
|
|
loop if s < j {
|
|
temp = *s;
|
|
*s = *j;
|
|
*j = temp
|
|
s += 1
|
|
j -= 1
|
|
} else return
|
|
}
|
|
|
|
equals := fn(lhs: ^u8, rhs: ^u8): bool {
|
|
if lhs == rhs {
|
|
return true
|
|
}
|
|
loop if *lhs != *rhs {
|
|
return false
|
|
} else if *lhs == 0 {
|
|
return true
|
|
} else {
|
|
lhs += 1
|
|
rhs += 1
|
|
}
|
|
}
|
|
|
|
clear := fn(ptr: ^u8): void {
|
|
loop if *ptr == 0 break else {
|
|
*ptr = 0
|
|
ptr += 1
|
|
}
|
|
}
|
|
|
|
split_once := fn(haystack: ^u8, needle: u8): ?^u8 {
|
|
loop if *haystack == needle return haystack else if *haystack == 0 return null else haystack += 1
|
|
}
|
|
|
|
split_once_str := fn(haystack: ^u8, needle: ^u8): ?^u8 {
|
|
if *needle == 0 return null
|
|
|
|
loop if *haystack == 0 return null else {
|
|
if *haystack == *needle {
|
|
h := haystack
|
|
n := needle
|
|
|
|
loop {
|
|
n += 1
|
|
h += 1
|
|
if *n == 0 {
|
|
return haystack
|
|
} else if *h == 0 | *h != *n {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
haystack += 1
|
|
}
|
|
}
|
|
|
|
Split := struct {
|
|
str: ^u8,
|
|
needle: u8,
|
|
done: bool,
|
|
}
|
|
|
|
split := fn(iter: ^u8, needle: u8): Split {
|
|
return .(
|
|
iter,
|
|
needle,
|
|
false,
|
|
)
|
|
}
|
|
|
|
iter_split := fn(iter: ^Split): ?^u8 {
|
|
if iter.done | *iter.str == 0 {
|
|
return null
|
|
}
|
|
|
|
next := split_once(iter.str + 1, iter.needle)
|
|
|
|
if next == null {
|
|
iter.done = true
|
|
return iter.str
|
|
}
|
|
s := iter.str
|
|
iter.str = next + 1
|
|
|
|
return s
|
|
}
|
|
|
|
SplitStr := struct {
|
|
str: ^u8,
|
|
needle: ^u8,
|
|
done: bool,
|
|
}
|
|
|
|
split_str := fn(iter: ^u8, needle: ^u8): SplitStr {
|
|
return .(
|
|
iter,
|
|
needle,
|
|
false,
|
|
)
|
|
}
|
|
|
|
iter_split_str := fn(iter: ^SplitStr): ?^u8 {
|
|
if iter.done | *iter.str == 0 {
|
|
return null
|
|
}
|
|
|
|
next := split_once_str(iter.str, iter.needle)
|
|
|
|
if next == null {
|
|
iter.done = true
|
|
return iter.str
|
|
}
|
|
|
|
s := iter.str
|
|
|
|
iter.str = next + length(iter.needle)
|
|
|
|
return s
|
|
}
|
|
|
|
find_once := fn(haystack: ^u8, needle: u8): ?uint {
|
|
return @bitcast(@inline(split_once, haystack, needle) - haystack)
|
|
}
|
|
|
|
find_once_str := fn(haystack: ^u8, needle: ^u8): ?uint {
|
|
return @bitcast(@inline(split_once_str, haystack, needle) - haystack)
|
|
}
|
|
|
|
count := fn(haystack: ^u8, needle: ^u8): uint {
|
|
c := 0
|
|
loop if *haystack == needle {
|
|
c += 1
|
|
haystack += 1
|
|
} else if *haystack == 0 return c else haystack += 1
|
|
}
|
|
|
|
count_str := fn(haystack: ^u8, needle: ^u8): uint {
|
|
if *needle == 0 return 0
|
|
c := 0
|
|
loop if *haystack == 0 return c else {
|
|
if *haystack == *needle {
|
|
h := haystack
|
|
n := needle
|
|
|
|
loop {
|
|
n += 1
|
|
h += 1
|
|
if *n == 0 {
|
|
c += 1
|
|
break
|
|
} else if *h == 0 | *h != *n {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
haystack += 1
|
|
}
|
|
}
|
|
|
|
left_trim := fn(str: ^u8, sub: ^u8): ^u8 {
|
|
original := str
|
|
if *str == *sub {
|
|
loop if *sub == 0 {
|
|
return str
|
|
} else if *str != *sub {
|
|
return original
|
|
} else if *str == 0 {
|
|
return original
|
|
} else {
|
|
str += 1
|
|
sub += 1
|
|
}
|
|
}
|
|
return str
|
|
} |