mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
make trim use cutset like trim_right/trim_left
This commit is contained in:

committed by
Alexander Medvednikov

parent
3db50f724b
commit
02fc7e14cd
@ -590,30 +590,38 @@ pub fn (s string) trim_space() string {
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn (s string) trim(c byte) string {
|
||||
if s == '' {
|
||||
return ''
|
||||
pub fn (s string) trim(cutset string) string {
|
||||
if s.len == 0 || cutset.len == 0 {
|
||||
return s
|
||||
}
|
||||
mut i := 0
|
||||
for i < s.len && c == s[i] {
|
||||
i++
|
||||
cs_arr := cutset.bytes()
|
||||
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
|
||||
if s[pos_left] in cs_arr {
|
||||
pos_left++
|
||||
cs_match = true
|
||||
}
|
||||
if s[pos_right] in cs_arr {
|
||||
pos_right--
|
||||
cs_match = true
|
||||
}
|
||||
if pos_left > pos_right {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
mut res := s.right(i)
|
||||
mut end := res.len - 1
|
||||
for end >= 0 && c == res[end] {
|
||||
end--
|
||||
}
|
||||
res = res.left(end + 1)
|
||||
return res
|
||||
return s.substr(pos_left, pos_right+1)
|
||||
}
|
||||
|
||||
pub fn (s string) trim_left(cutset string) string {
|
||||
if s.len == 0 || cutset.len == 0 {
|
||||
return s
|
||||
}
|
||||
mut pos := 0
|
||||
cs_arr := cutset.bytes()
|
||||
for s[pos] in cs_arr {
|
||||
mut pos := 0
|
||||
for pos <= s.len && s[pos] in cs_arr {
|
||||
pos++
|
||||
}
|
||||
return s.right(pos)
|
||||
@ -623,9 +631,9 @@ pub fn (s string) trim_right(cutset string) string {
|
||||
if s.len == 0 || cutset.len == 0 {
|
||||
return s
|
||||
}
|
||||
mut pos := s.len - 1
|
||||
cs_arr := cutset.bytes()
|
||||
for s[pos] in cs_arr {
|
||||
mut pos := s.len - 1
|
||||
for pos >= -1 && s[pos] in cs_arr {
|
||||
pos--
|
||||
}
|
||||
return s.left(pos+1)
|
||||
|
Reference in New Issue
Block a user