string: implement is_space in V & make trim_space use trim

pull/1880/head
joe-conigliaro 2019-09-06 20:22:37 +10:00 committed by Alexander Medvednikov
parent 8a77d4482c
commit 79d4d56fe5
1 changed files with 5 additions and 20 deletions

View File

@ -562,7 +562,7 @@ pub fn (a []string) to_c() voidptr {
*/ */
fn is_space(c byte) bool { fn is_space(c byte) bool {
return C.isspace(c) return c in [` `,`\n`,`\t`,`\v`,`\f`,`\r`]
} }
pub fn (c byte) is_space() bool { pub fn (c byte) is_space() bool {
@ -570,26 +570,11 @@ pub fn (c byte) is_space() bool {
} }
pub fn (s string) trim_space() string { pub fn (s string) trim_space() string {
if s == '' { return s.trim(' \n\t\v\f\r')
return ''
}
mut i := 0
for i < s.len && is_space(s[i]) {
i++
}
mut end := s.len - 1
for end >= 0 && is_space(s[end]) {
end--
}
if i > end + 1 {
return s
}
res := s.substr(i, end + 1)
return res
} }
pub fn (s string) trim(cutset string) string { pub fn (s string) trim(cutset string) string {
if s.len == 0 || cutset.len == 0 { if s.len < 1 || cutset.len < 1 {
return s return s
} }
cs_arr := cutset.bytes() cs_arr := cutset.bytes()
@ -614,7 +599,7 @@ pub fn (s string) trim(cutset string) string {
} }
pub fn (s string) trim_left(cutset string) string { pub fn (s string) trim_left(cutset string) string {
if s.len == 0 || cutset.len == 0 { if s.len < 1 || cutset.len < 1 {
return s return s
} }
cs_arr := cutset.bytes() cs_arr := cutset.bytes()
@ -626,7 +611,7 @@ pub fn (s string) trim_left(cutset string) string {
} }
pub fn (s string) trim_right(cutset string) string { pub fn (s string) trim_right(cutset string) string {
if s.len == 0 || cutset.len == 0 { if s.len < 1 || cutset.len < 1 {
return s return s
} }
cs_arr := cutset.bytes() cs_arr := cutset.bytes()