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

View File

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