Skip to content

Commit

Permalink
std::strings: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 26, 2024
1 parent 05f0dcc commit 1dd66f1
Showing 1 changed file with 3 additions and 32 deletions.
35 changes: 3 additions & 32 deletions std/strings/strings.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down

0 comments on commit 1dd66f1

Please sign in to comment.