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:
parent
f59c9133da
commit
f099f90f50
@ -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() {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user