mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Update strings.prefix_length to handle partial UTF-8 runes.
This commit is contained in:
@@ -225,14 +225,23 @@ equal_fold :: proc(u, v: string) -> bool {
|
|||||||
*/
|
*/
|
||||||
prefix_length :: proc(a, b: string) -> (n: int) {
|
prefix_length :: proc(a, b: string) -> (n: int) {
|
||||||
_len := min(len(a), len(b))
|
_len := min(len(a), len(b))
|
||||||
idx := 0
|
|
||||||
|
|
||||||
#no_bounds_check for idx < _len && a[idx] == b[idx] {
|
// Scan for matches including partial codepoints.
|
||||||
idx += 1
|
#no_bounds_check for n < _len && a[n] == b[n] {
|
||||||
|
n += 1
|
||||||
|
}
|
||||||
|
|
||||||
if a[idx] & 128 != 128 {
|
// Now scan to ignore partial codepoints.
|
||||||
// new codepoint or end of multi-byte codepoint, update match length
|
if n > 0 {
|
||||||
n = idx
|
s := a[:n]
|
||||||
|
n = 0
|
||||||
|
for {
|
||||||
|
r0, w := utf8.decode_rune(s[n:])
|
||||||
|
if r0 != utf8.RUNE_ERROR {
|
||||||
|
n += w
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user