builtin: optimise `sx == sy` in the case where strings have common prefixes

pull/10182/head
Delyan Angelov 2021-05-23 17:22:57 +03:00
parent e9fa53b0c1
commit 7f12bfa563
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 7 additions and 0 deletions

View File

@ -496,6 +496,7 @@ pub fn (s string) u64() u64 {
}
// eq implements the `s == a` (equal) operator.
[direct_array_access]
fn (s string) eq(a string) bool {
if s.str == 0 {
// should never happen
@ -504,6 +505,12 @@ fn (s string) eq(a string) bool {
if s.len != a.len {
return false
}
if s.len > 0 {
last_idx := s.len - 1
if s[last_idx] != a[last_idx] {
return false
}
}
unsafe {
return C.memcmp(s.str, a.str, a.len) == 0
}