From b5795dcd7669a1d45a3064a98c226e281c585d62 Mon Sep 17 00:00:00 2001 From: Hitalo Souza Date: Tue, 22 Oct 2024 10:02:47 -0400 Subject: [PATCH] builtin: improve performance of `string.starts_with/1` and `string.ends_with/1`, when compiled with tcc (#22620) --- vlib/builtin/string.v | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 77e747f81498f5..da676cf976bed9 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1499,13 +1499,10 @@ 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`. @@ -1513,13 +1510,10 @@ pub fn (s string) starts_with(p string) bool { 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.