Skip to content

Commit

Permalink
builtin: improve performance of string.starts_with/1 and `string.en…
Browse files Browse the repository at this point in the history
…ds_with/1`, when compiled with tcc (vlang#22620)
  • Loading branch information
enghitalo authored Oct 22, 2024
1 parent 4e13ddf commit b5795dc
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -1499,27 +1499,21 @@ pub fn (s string) contains_any_substr(substrs []string) bool {
pub fn (s string) starts_with(p string) bool {
if p.len > s.len {
return false
} else if unsafe { vmemcmp(s.str, p.str, p.len) == 0 } {
return true
}
for i in 0 .. p.len {
if unsafe { s.str[i] != p.str[i] } {
return false
}
}
return true
return false
}

// ends_with returns `true` if the string ends with `p`.
@[direct_array_access]
pub fn (s string) ends_with(p string) bool {
if p.len > s.len {
return false
} else if unsafe { vmemcmp(s.str + s.len - p.len, p.str, p.len) == 0 } {
return true
}
for i in 0 .. p.len {
if unsafe { p.str[i] != s.str[s.len - p.len + i] } {
return false
}
}
return true
return false
}

// to_lower returns the string in all lowercase characters.
Expand Down

0 comments on commit b5795dc

Please sign in to comment.