Skip to content

Commit

Permalink
builtin: add s.trim_space_left/0 and s.trim_space_right/0 methods (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 authored Jul 21, 2024
1 parent 0ebf184 commit 69bc4be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 14 additions & 0 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions vlib/builtin/string_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions vlib/flag/flag_to.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 << ''
Expand Down

0 comments on commit 69bc4be

Please sign in to comment.