From 79d4d56fe56cb4c3a60f56e0128a5674d11f8447 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Fri, 6 Sep 2019 20:22:37 +1000 Subject: [PATCH] string: implement is_space in V & make trim_space use trim --- vlib/builtin/string.v | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 232206406b..32d82594ed 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -562,7 +562,7 @@ pub fn (a []string) to_c() voidptr { */ 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 { @@ -570,26 +570,11 @@ pub fn (c byte) is_space() bool { } pub fn (s string) trim_space() string { - if s == '' { - 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 + return s.trim(' \n\t\v\f\r') } pub fn (s string) trim(cutset string) string { - if s.len == 0 || cutset.len == 0 { + if s.len < 1 || cutset.len < 1 { return s } 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 { - if s.len == 0 || cutset.len == 0 { + if s.len < 1 || cutset.len < 1 { return s } 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 { - if s.len == 0 || cutset.len == 0 { + if s.len < 1 || cutset.len < 1 { return s } cs_arr := cutset.bytes()