string: fix trim_prefix and trim_suffix methods (#5572)

pull/5586/head
nyaascii 2020-06-30 15:44:53 +02:00 committed by GitHub
parent af56b01a41
commit f10d2bb75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -921,14 +921,14 @@ pub fn (s string) trim_right(cutset string) string {
pub fn (s string) trim_prefix(str string) string {
if s.starts_with(str) {
return s.replace(str, "")
return s[str.len..]
}
return s
}
pub fn (s string) trim_suffix(str string) string {
if s.ends_with(str) {
return s.replace(str, "")
return s[..s.len-str.len]
}
return s
}

View File

@ -669,6 +669,14 @@ fn test_trim_prefix() {
assert s.trim_prefix('V ') == 'Programming Language'
assert s.trim_prefix('V Programming ') == 'Language'
assert s.trim_prefix('Language') == s
s2 := 'TestTestTest'
assert s2.trim_prefix('Test') == 'TestTest'
assert s2.trim_prefix('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_prefix('123') == 'Test123Test'
assert s3.trim_prefix('123Test') == '123Test'
}
fn test_trim_suffix() {
@ -676,6 +684,14 @@ fn test_trim_suffix() {
assert s.trim_suffix(' Language') == 'V Programming'
assert s.trim_suffix(' Programming Language') == 'V'
assert s.trim_suffix('V') == s
s2 := 'TestTestTest'
assert s2.trim_suffix('Test') == 'TestTest'
assert s2.trim_suffix('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_suffix('123') == s3
assert s3.trim_suffix('123Test') == '123Test'
}
fn test_raw() {