diff --git a/std/strings/strings.jule b/std/strings/strings.jule index 0fd589f17..ec989d635 100644 --- a/std/strings/strings.jule +++ b/std/strings/strings.jule @@ -22,43 +22,14 @@ fn Repeat(s: str, mut n: int): str { ret ss.Str() } -fn hasPrefix(&s: str, &sub: str, mut start: int): bool { - if len(sub) == 0 || len(s)-start < len(sub) { - ret false - } - - mut i := 0 - for i < len(sub); i, start = i + 1, start + 1 { - if s[start] != sub[i] { - ret false - } - } - ret true -} - // Reports string has prefix as specified substring or not. fn HasPrefix(s: str, sub: str): bool { - ret hasPrefix(s, sub, 0) -} - -fn hasSuffix(&s: str, &sub: str, mut start: int): bool { - if len(sub) == 0 || len(s)-start < len(sub) { - ret false - } - - start = len(s) - start - mut i := 0 - for i < len(sub); i++ { - if s[start-i-1] != sub[len(sub)-i-1] { - ret false - } - } - ret true + ret len(s) >= len(sub) && s[:len(sub)] == sub } // Reports string has suffix as specified substring or not. fn HasSuffix(s: str, sub: str): bool { - ret hasSuffix(s, sub, 0) + ret len(s) >= len(sub) && s[len(s)-len(sub):] == sub } // Returns index of first matched item with specified substring, @@ -70,7 +41,7 @@ fn FindAt(s: str, sub: str, mut i: int): int { ret -1 } for i < len(s); i++ { - if hasPrefix(s, sub, i) { + if HasPrefix(s[i:], sub) { ret i } }