1
0
Fork 0
forked from AbleOS/ableos
ableos/sysdata/libraries/stn/src/string.hb

162 lines
2.8 KiB
Plaintext
Raw Normal View History

.{log, memory} := @use("stn")
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-09-17 09:52:27 -05:00
reverse := fn(s: ^u8): void {
j := s + @inline(length, s) - 1
2024-10-25 10:37:38 -05:00
temp := @as(u8, 0)
loop if s < j {
temp = *s;
*s = *j;
*j = temp
s += 1
2024-09-17 09:52:27 -05:00
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
}
2024-11-23 10:51:39 -06:00
}
clear := fn(ptr: ^u8): void {
loop if *ptr == 0 break else {
*ptr = 0
ptr += 1
}
}
split_buffer := memory.dangling(u8)
split_once := fn(haystack: ^u8, needle: @Any()): ?^u8 {
T := @TypeOf(needle)
if T == ^u8 {
if *needle == 0 return null
2024-11-23 10:51:39 -06:00
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
}
2024-11-23 10:51:39 -06:00
}
}
haystack += 1
2024-11-23 10:51:39 -06:00
}
} else if T == u8 {
loop if *haystack == needle return haystack else if *haystack == 0 return null else haystack += 1
} else {
@error("Type of needle must be string or char.\0")
2024-11-23 10:51:39 -06:00
}
}
SplitIter := fn($T: type): type {
return struct {
str: ^u8,
needle: T,
done: bool,
2024-11-23 10:51:39 -06:00
next := fn(self: ^Self): ?^u8 {
if self.done | *self.str == 0 {
return null
}
2024-11-23 10:51:39 -06:00
next0 := split_once(self.str, self.needle)
2024-11-23 10:51:39 -06:00
if next0 == null {
self.done = true
return self.str
}
s := self.str
if T == ^u8 {
self.str = next0 + length(self.needle)
} else if T == u8 {
self.str = next0 + 1
}
2024-11-23 10:51:39 -06:00
return s
}
2024-11-23 10:51:39 -06:00
}
}
split := fn(iter: ^u8, needle: @Any()): SplitIter(@TypeOf(needle)) {
T := @TypeOf(needle)
if T != ^u8 & T != u8 {
@error("Type of needle must be string or char.\0")
2024-11-23 10:51:39 -06:00
}
return .(iter, needle, false)
2024-11-23 10:51:39 -06:00
}
find_once := fn(haystack: ^u8, needle: @Any()): ?uint {
2024-11-23 10:51:39 -06:00
return @bitcast(@inline(split_once, haystack, needle) - haystack)
}
count := fn(haystack: ^u8, needle: @Any()): uint {
T := @TypeOf(needle)
if T != ^u8 & T != u8 {
@error("Type of needle must be string or char.\0")
}
2024-11-23 10:51:39 -06:00
c := 0
if T == u8 {
loop if *haystack == needle {
c += 1
haystack += 1
} else if *haystack == 0 return c else haystack += 1
} else if T == ^u8 {
if *needle == 0 return 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
}
2024-11-23 10:51:39 -06:00
}
}
haystack += 1
2024-11-23 10:51:39 -06:00
}
}
}
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
2024-07-20 12:54:58 -05:00
}