2024-10-20 06:11:29 -05: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-07-16 16:42:49 -05:00
|
|
|
}
|
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-07-16 16:42:49 -05:00
|
|
|
}
|
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-11-06 19:55:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
equals := fn(lhs: ^u8, rhs: ^u8): bool {
|
|
|
|
if lhs == rhs {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
loop if *(lhs + i) != *(rhs + i) {
|
|
|
|
return false
|
|
|
|
} else if *lhs == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
i += 1
|
|
|
|
}
|
2024-11-06 20:22:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
contains := fn(haystack: ^u8, needle: ^u8): bool {
|
|
|
|
haystack_len := @inline(length, haystack)
|
|
|
|
needle_len := @inline(length, needle)
|
|
|
|
|
|
|
|
if needle_len == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if haystack_len < needle_len {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
max_start := haystack_len - needle_len
|
|
|
|
|
|
|
|
pos := 0
|
|
|
|
loop if pos > max_start break else {
|
|
|
|
is_match := true
|
|
|
|
offset := 0
|
|
|
|
|
|
|
|
loop if offset >= needle_len break else {
|
|
|
|
if *(haystack + pos + offset) != *(needle + offset) {
|
|
|
|
is_match = false
|
|
|
|
}
|
|
|
|
if is_match == false {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
offset += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_match {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
pos += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2024-07-20 12:54:58 -05:00
|
|
|
}
|