diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 36ba5b09e520b3..1efbeef20c8b31 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1690,6 +1690,20 @@ pub fn (s string) trim_space() string { return s.trim(' \n\t\v\f\r') } +// trim_space_left strips any of ` `, `\n`, `\t`, `\v`, `\f`, `\r` from the start of the string. +// Example: assert ' Hello V '.trim_space_left() == 'Hello V ' +@[inline] +pub fn (s string) trim_space_left() string { + return s.trim_left(' \n\t\v\f\r') +} + +// trim_space_right strips any of ` `, `\n`, `\t`, `\v`, `\f`, `\r` from the end of the string. +// Example: assert ' Hello V '.trim_space_right() == ' Hello V' +@[inline] +pub fn (s string) trim_space_right() string { + return s.trim_right(' \n\t\v\f\r') +} + // trim strips any of the characters given in `cutset` from the start and end of the string. // Example: assert ' ffHello V ffff'.trim(' f') == 'Hello V' pub fn (s string) trim(cutset string) string { diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 95b7003392e555..4d0d190de4b846 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -596,6 +596,9 @@ fn test_is_int() { fn test_trim_space() { a := ' a ' assert a.trim_space() == 'a' + assert a.trim_space_left() == 'a ' + assert a.trim_space_right() == ' a' + code := ' fn main() { @@ -606,7 +609,19 @@ fn main() { code_clean := 'fn main() { println(2) }' + code_trim_right := ' + +fn main() { + println(2) +}' + code_trim_left := 'fn main() { + println(2) +} + +' assert code.trim_space() == code_clean + assert code.trim_space_right() == code_trim_right + assert code.trim_space_left() == code_trim_left } fn test_join() { diff --git a/vlib/flag/flag_to.v b/vlib/flag/flag_to.v index 2a1aee5ca02ce5..2e09fdc5819d9a 100644 --- a/vlib/flag/flag_to.v +++ b/vlib/flag/flag_to.v @@ -737,13 +737,13 @@ pub fn (fm FlagMapper) fields_docs(dc DocConfig) ![]string { diff := -flag_line_diff line := flag_line + ' '.repeat(diff) + keep_at_max(doc, desc_max).replace('\n', '\n${empty_padding}') - docs << line.trim_right(' \n\t\v\f\r') + docs << line.trim_space_right() } else { - docs << flag_line.trim_right(' \n\t\v\f\r') + docs << flag_line.trim_space_right() if doc != '' { line := empty_padding + keep_at_max(doc, desc_max).replace('\n', '\n${empty_padding}') - docs << line.trim_right(' \n\t\v\f\r') + docs << line.trim_space_right() } } if !dc.options.compact { @@ -760,12 +760,12 @@ pub fn (fm FlagMapper) fields_docs(dc DocConfig) ![]string { diff := -flag_line_diff line := indent_flags_padding + entry.trim(' ') + ' '.repeat(diff) + keep_at_max(doc, desc_max).replace('\n', '\n${empty_padding}') - docs << line.trim_right(' \n\t\v\f\r') + docs << line.trim_space_right() } else { docs << indent_flags_padding + entry.trim(' ') line := empty_padding + keep_at_max(doc, desc_max).replace('\n', '\n${empty_padding}') - docs << line.trim_right(' \n\t\v\f\r') + docs << line.trim_space_right() } if !dc.options.compact { docs << ''