mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 12:48:14 +00:00
Vectorize strings.prefix_length.
Also add `strings.common_prefix`.
This commit is contained in:
@@ -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]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user