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

string: fix trim_right, add tests

This commit is contained in:
Major Taylor 2020-04-06 08:30:25 -04:00 committed by GitHub
parent f59c9133da
commit f099f90f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -857,7 +857,7 @@ pub fn (s string) trim_left(cutset string) string {
}
cs_arr := cutset.bytes()
mut pos := 0
for pos <= s.len && s[pos] in cs_arr {
for pos < s.len && s[pos] in cs_arr {
pos++
}
return s.right(pos)
@ -869,10 +869,10 @@ pub fn (s string) trim_right(cutset string) string {
}
cs_arr := cutset.bytes()
mut pos := s.len - 1
for pos >= -1 && s[pos] in cs_arr {
for pos >= 0 && s[pos] in cs_arr {
pos--
}
return s.left(pos + 1)
return if pos < 0 { '' } else { s.left(pos + 1) }
}
// fn print_cur_thread() {

View File

@ -438,6 +438,7 @@ fn test_trim_left() {
// test cutset
s = 'banana'
assert s.trim_left('ba') == 'nana'
assert s.trim_left('ban') == ''
}
fn test_trim_right() {
@ -448,6 +449,7 @@ fn test_trim_right() {
// test cutset
s = 'banana'
assert s.trim_right('na') == 'b'
assert s.trim_right('ban') == ''
}
fn test_all_before() {