mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
builtin: add s.trim_space_left/0 and s.trim_space_right/0 methods (#21903)
This commit is contained in:
parent
0ebf184cb5
commit
69bc4be512
3 changed files with 34 additions and 5 deletions
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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 << ''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue