builtin: optimise the common case of s.contains("x") add s.contains_byte(x) (#17702)

This commit is contained in:
Delyan Angelov 2023-03-19 00:10:13 +02:00 committed by GitHub
parent 14148f3e52
commit 3793bf1c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 10 deletions

View file

@ -1287,12 +1287,12 @@ pub fn (s string) index_after(p string, start int) int {
return -1
}
// index_byte returns the index of byte `c` if found in the string.
// index_byte returns -1 if the byte can not be found.
// index_u8 returns the index of byte `c` if found in the string.
// index_u8 returns -1 if the byte can not be found.
[direct_array_access]
pub fn (s string) index_u8(c u8) int {
for i in 0 .. s.len {
if unsafe { s.str[i] } == c {
for i, b in s {
if b == c {
return i
}
}
@ -1348,22 +1348,33 @@ pub fn (s string) count(substr string) int {
return 0 // TODO can never get here - v doesn't know that
}
// contains_u8 returns `true` if the string contains the byte value `x`.
// See also: [`string.index_u8`](#string.index_u8) , to get the index of the byte as well.
pub fn (s string) contains_u8(x u8) bool {
for c in s {
if x == c {
return true
}
}
return false
}
// contains returns `true` if the string contains `substr`.
// See also: [`string.index`](#string.index)
pub fn (s string) contains(substr string) bool {
if substr.len == 0 {
return true
}
if s.index_(substr) == -1 {
return false
if substr.len == 1 {
return s.contains_u8(unsafe { substr.str[0] })
}
return true
return s.index_(substr) != -1
}
// contains_any returns `true` if the string contains any chars in `chars`.
pub fn (s string) contains_any(chars string) bool {
for c in chars {
if s.contains(c.ascii_str()) {
if s.contains_u8(c) {
return true
}
}