Vectorize strings.prefix_length.

Also add `strings.common_prefix`.
This commit is contained in:
Jeroen van Rijn
2025-05-31 20:24:21 +02:00
parent d52aa3f2c2
commit 890e923051
5 changed files with 298 additions and 18 deletions
+50 -1
View File
@@ -1,9 +1,10 @@
package test_core_strings
import "base:runtime"
import "core:mem"
import "core:strings"
import "core:testing"
import "base:runtime"
import "core:unicode/utf8"
@test
test_index_any_small_string_not_found :: proc(t: ^testing.T) {
@@ -218,3 +219,51 @@ test_builder_to_cstring :: proc(t: ^testing.T) {
testing.expect(t, err == .Out_Of_Memory)
}
}
@test
test_prefix_length :: proc(t: ^testing.T) {
prefix_length :: proc "contextless" (a, b: string) -> (n: int) {
_len := min(len(a), len(b))
// Scan for matches including partial codepoints.
#no_bounds_check for n < _len && a[n] == b[n] {
n += 1
}
// Now scan to ignore partial codepoints.
if n > 0 {
s := a[:n]
n = 0
for {
r0, w := utf8.decode_rune(s[n:])
if r0 != utf8.RUNE_ERROR {
n += w
} else {
break
}
}
}
return
}
cases := [][2]string{
{"Hellope, there!", "Hellope, world!"},
{"Hellope, there!", "Foozle"},
{"Hellope, there!", "Hell"},
{"Hellope! 🦉", "Hellope! 🦉"},
}
for v in cases {
p_scalar := prefix_length(v[0], v[1])
p_simd := strings.prefix_length(v[0], v[1])
testing.expect_value(t, p_simd, p_scalar)
s := v[0]
for len(s) > 0 {
p_scalar = prefix_length(v[0], s)
p_simd = strings.prefix_length(v[0], s)
testing.expect_value(t, p_simd, p_scalar)
s = s[:len(s) - 1]
}
}
}