From 6433c23a34b941fa851de40f6c9e68bb88776e2c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 11 Apr 2020 11:06:03 +0300 Subject: [PATCH] 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. --- vlib/builtin/string.v | 27 +++++++++---------------- vlib/builtin/string_strip_margin_test.v | 4 ++-- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 2bc4ebf575..8469696739 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -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 diff --git a/vlib/builtin/string_strip_margin_test.v b/vlib/builtin/string_strip_margin_test.v index 5d532e7c2d..04729a65da 100644 --- a/vlib/builtin/string_strip_margin_test.v +++ b/vlib/builtin/string_strip_margin_test.v @@ -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 }