1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string: optimize starts_with and ends_with

This commit is contained in:
yuyi 2020-02-19 22:18:09 +08:00 committed by GitHub
parent e4179c0008
commit 391da0ba07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -711,20 +711,27 @@ pub fn (s string) contains(p string) bool {
} }
pub fn (s string) starts_with(p string) bool { pub fn (s string) starts_with(p string) bool {
idx := s.index(p) or { if p.len > s.len {
return false return false
} }
return idx == 0 for i := 0; i < p.len; i++ {
if s[i] != p[i] {
return false
}
}
return true
} }
pub fn (s string) ends_with(p string) bool { pub fn (s string) ends_with(p string) bool {
if p.len > s.len { if p.len > s.len {
return false return false
} }
idx := s.last_index(p) or { for i := 0; i < p.len; i++ {
return false if p[i] != s[s.len - p.len + i] {
return false
}
} }
return idx == s.len - p.len return true
} }
// TODO only works with ASCII // TODO only works with ASCII