1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string: add s.strip_margin_custom/1, instead of passing varargs to s.strip_margin()

The reason for adding s.strip_margin_custom/1 is that
passing varargs interfere with the current implementation of the
builtin module caching.
This commit is contained in:
Delyan Angelov 2020-04-11 11:06:03 +03:00
parent c64e447749
commit 6433c23a34
2 changed files with 11 additions and 20 deletions

View File

@ -1286,24 +1286,15 @@ pub fn (s string) repeat(count int) string {
// Hello there,
// this is a string,
// Everything before the first | is removed
pub fn (s string) strip_margin(del ...byte) string {
mut sep := `|`
if del.len >= 1 {
// This is a workaround. We can't directly index a var_args array.
// Only care about the first one, ignore the rest if more
for d in del {
// The delimiter is not allowed to be white-space. Will use default
if d.is_space() {
eprintln("Warning: `strip_margin` cannot use white-space as a delimiter")
eprintln(" Defaulting to `|`")
} else {
sep = d
}
break
}
if del.len != 1 {
eprintln("Warning: `strip_margin` only uses the first argument given")
}
pub fn (s string) strip_margin() string {
return s.strip_margin_custom(`|`)
}
pub fn (s string) strip_margin_custom(del byte) string {
mut sep := del
if sep.is_space() {
eprintln("Warning: `strip_margin` cannot use white-space as a delimiter")
eprintln(" Defaulting to `|`")
sep = `|`
}
// don't know how much space the resulting string will be, but the max it
// can be is this big

View File

@ -42,7 +42,7 @@ fn test_strip_margins_alternate_delim() {
].join('\n')
alternate_delimiter_stripped := 'This has a different delim,
#but that is ok
#because everything works'.strip_margin(`#`)
#because everything works'.strip_margin_custom(`#`)
assert alternate_delimiter_stripped == alternate_delimiter
}
@ -99,7 +99,7 @@ fn test_strip_margins_space_delimiter() {
'revert back to default behavior.',
].join('\n')
space_delimiter_stripped := 'Using a white-space char will
|revert back to default behavior.'.strip_margin(`\n`)
|revert back to default behavior.'.strip_margin_custom(`\n`)
assert space_delimiter == space_delimiter_stripped
}