builtin: fix string.trim() (fix #13021) (#22205)

This commit is contained in:
yuyi 2024-09-13 12:27:40 +08:00 committed by GitHub
parent 5cf4b5ed4f
commit 6eae77b61e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 2 deletions

View File

@ -1720,8 +1720,11 @@ pub fn (s string) trim(cutset string) string {
if s == '' || cutset == '' {
return s.clone()
}
left, right := s.trim_indexes(cutset)
return s.substr(left, right)
if cutset.len_utf8() == cutset.len {
return s.trim_chars(cutset)
} else {
return s.trim_runes(cutset)
}
}
// trim_indexes gets the new start and end indices of a string when any of the characters given in `cutset` were stripped from the start and end of the string. Should be used as an input to `substr()`. If the string contains only the characters in `cutset`, both values returned are zero.
@ -1754,6 +1757,64 @@ pub fn (s string) trim_indexes(cutset string) (int, int) {
return pos_left, pos_right + 1
}
@[direct_array_access]
fn (s string) trim_chars(cutset string) string {
mut pos_left := 0
mut pos_right := s.len - 1
mut cs_match := true
for pos_left <= s.len && pos_right >= -1 && cs_match {
cs_match = false
for cs in cutset {
if s[pos_left] == cs {
pos_left++
cs_match = true
break
}
}
for cs in cutset {
if s[pos_right] == cs {
pos_right--
cs_match = true
break
}
}
if pos_left > pos_right {
return ''
}
}
return s.substr(pos_left, pos_right + 1)
}
@[direct_array_access]
fn (s string) trim_runes(cutset string) string {
s_runes := s.runes()
c_runes := cutset.runes()
mut pos_left := 0
mut pos_right := s_runes.len - 1
mut cs_match := true
for pos_left <= s_runes.len && pos_right >= -1 && cs_match {
cs_match = false
for cs in c_runes {
if s_runes[pos_left] == cs {
pos_left++
cs_match = true
break
}
}
for cs in c_runes {
if s_runes[pos_right] == cs {
pos_right--
cs_match = true
break
}
}
if pos_left > pos_right {
return ''
}
}
return s_runes[pos_left..pos_right + 1].string()
}
// trim_left strips any of the characters given in `cutset` from the left of the string.
// Example: assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'
@[direct_array_access]

View File

@ -921,6 +921,7 @@ fn test_trim() {
assert 'banana'.trim('bna') == ''
assert 'abc'.trim('ac') == 'b'
assert 'aaabccc'.trim('ac') == 'b'
assert ''.trim('') == ''
}
fn test_trim_indexes() {