forked from AbleOS/ableos
121 lines
1.8 KiB
Plaintext
121 lines
1.8 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 {
|
|
ptr := p
|
|
negative := num < 0
|
|
if negative {
|
|
num = -num
|
|
}
|
|
|
|
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
|
|
if num == 0 {
|
|
*ptr = 48
|
|
ptr += 1
|
|
} else {
|
|
loop if num == 0 break else {
|
|
digit := num % @bitcast(radix)
|
|
if digit < 10 {
|
|
*ptr = @intcast(digit) + 48
|
|
} else {
|
|
*ptr = @intcast(digit) + 55
|
|
}
|
|
ptr += 1
|
|
num /= @bitcast(radix)
|
|
}
|
|
}
|
|
|
|
if negative {
|
|
*ptr = 45
|
|
ptr += 1
|
|
};
|
|
|
|
*ptr = 0
|
|
|
|
@inline(reverse, digits_start)
|
|
|
|
return p
|
|
}
|
|
|
|
reverse := fn(s: ^u8): void {
|
|
i := 0
|
|
j := @inline(length, s) - 1
|
|
temp := @as(u8, 0)
|
|
loop if i >= j break else {
|
|
temp = *(s + i);
|
|
*(s + i) = *(s + j);
|
|
*(s + j) = temp
|
|
i += 1
|
|
j -= 1
|
|
}
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
} |