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

170 lines
3.1 KiB
Plaintext
Raw Normal View History

.{log, memory} := @use("stn")
2024-12-22 13:48:12 -06:00
reverse := fn(str: []u8): void {
if str.len == 0 return;
j := str.len - 1
i := 0
2024-10-25 10:37:38 -05:00
temp := @as(u8, 0)
2024-12-22 13:48:12 -06:00
loop if i < j {
temp = str[i]
str[i] = str[j]
str[j] = temp
i += 1
2024-09-17 09:52:27 -05:00
j -= 1
} else return
}
2024-12-22 13:48:12 -06:00
$equals := fn(lhs: []u8, rhs: []u8): bool {
return lhs.ptr == rhs.ptr & lhs.len == rhs.len
2024-11-23 10:51:39 -06:00
}
2024-12-22 13:48:12 -06:00
clear := fn(str: []u8): void {
i := 0
loop if i == str.len break else {
str[i] = 0
i += 1
2024-11-23 10:51:39 -06:00
}
}
2024-12-22 13:48:12 -06:00
split_once := fn(haystack: []u8, needle: @Any()): ?[]u8 {
T := @TypeOf(needle)
2024-12-22 13:48:12 -06:00
if T == []u8 {
if needle.len == 0 return null
i := 0
loop if i == haystack.len return null else {
if haystack[i] == needle[0] {
h := i
n := 0
loop {
n += 1
h += 1
2024-12-22 13:48:12 -06:00
if needle[n] == 0 {
return haystack[h..]
} else if haystack[h] == 0 | haystack[h] != needle[n] {
break
}
2024-11-23 10:51:39 -06:00
}
}
2024-12-22 13:48:12 -06:00
i += 1
2024-11-23 10:51:39 -06:00
}
} else if T == u8 {
2024-12-22 13:48:12 -06:00
i := 0
loop if haystack[i] == needle return haystack[i..] else if i == haystack.len return null else i += 1
} else {
2024-12-22 13:48:12 -06:00
@error("Type of needle must be []u8 or u8.")
2024-11-23 10:51:39 -06:00
}
}
SplitIter := fn($T: type): type {
return struct {
2024-12-22 13:48:12 -06:00
str: []u8,
needle: T,
done: bool,
2024-11-23 10:51:39 -06:00
2024-12-22 13:48:12 -06:00
next := fn(self: ^Self): ?[]u8 {
if self.done return null;
if @TypeOf(self.needle) == u8 {
i := 0
loop if i == self.str.len {
self.done = true
return self.str
} else if self.str[i] == self.needle {
result := self.str[..i]
self.str = self.str[i + 1..]
return result
} else {
i += 1
}
} else if @TypeOf(self.needle) == []u8 {
if self.needle.len == 0 return null
i := 0
loop if i == self.str.len {
self.done = true
return self.str
} else if self.str[i] == self.needle[0] {
h := i
n := 0
loop {
n += 1
h += 1
if n == self.needle.len {
result := self.str[..i]
self.str = self.str[h..]
return result
} else if h == self.str.len | self.str[h] != self.needle[n] {
break
}
}
}
i += 1
}
2024-11-23 10:51:39 -06:00
2024-12-22 13:48:12 -06:00
self.done = true
return self.str
}
2024-11-23 10:51:39 -06:00
}
}
2024-12-22 13:48:12 -06:00
split := fn(iter: []u8, needle: @Any()): SplitIter(@TypeOf(needle)) {
T := @TypeOf(needle)
2024-12-22 13:48:12 -06:00
if T != []u8 & T != u8 {
@error("Type of needle must be []u8 or u8.")
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)
}
2024-12-22 13:48:12 -06:00
count := fn(haystack: []u8, needle: @Any()): uint {
T := @TypeOf(needle)
2024-11-23 10:51:39 -06:00
c := 0
if T == u8 {
2024-12-22 13:48:12 -06:00
i := 0
loop if haystack[i] == needle {
c += 1
2024-12-22 13:48:12 -06:00
i += 1
} else if i == haystack.len return c else i += 1
} else if T == []u8 {
i := 0
loop if i == haystack.len return c else {
if haystack[i] == needle[0] {
h := i
n := 0
loop {
n += 1
h += 1
2024-12-22 13:48:12 -06:00
if n == needle.len {
c += 1
2024-12-22 13:48:12 -06:00
i = h - 1
break
2024-12-22 13:48:12 -06:00
} else if h == haystack.len | haystack[h] != needle[n] {
break
}
2024-11-23 10:51:39 -06:00
}
}
2024-12-22 13:48:12 -06:00
i += 1
2024-11-23 10:51:39 -06:00
}
2024-12-22 13:48:12 -06:00
} else {
@error("Type of needle must be []u8 or u8.")
2024-11-23 10:51:39 -06:00
}
}
2024-12-22 13:48:12 -06:00
left_trim := fn(str: []u8, sub: []u8): []u8 {
i := 0
if str[0] == sub[0] {
loop if i == sub.len {
return str[i..str.len]
} else if str[i] != sub[i] | i == str.len {
2024-11-23 10:51:39 -06:00
return str
} else {
2024-12-22 13:48:12 -06:00
i += 1
2024-11-23 10:51:39 -06:00
}
}
2024-07-20 12:54:58 -05:00
}