From 456c0250b024578d06664b8a401e25a637d4cb8d Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 16 Mar 2020 22:46:09 +0800 Subject: [PATCH] string: fix strip_margin --- vlib/builtin/string.v | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 90fa0f7efe..b18372a61a 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -244,9 +244,10 @@ pub fn (s string) replace_each(vals []string) string { } // We need to remember both the position in the string, // and which rep/with pair it refers to. - idxs << RepIndex{ + idxs << RepIndex { idx:idx -val_idx:rep_i} + val_idx:rep_i + } idx++ new_len += with.len - rep.len } @@ -1272,7 +1273,7 @@ pub fn (s string) repeat(count int) string { // before a delimeter. by default `|` is used. // Note: the delimiter has to be a byte at this time. That means surrounding // the value in ``. -// +// // Example: // st := 'Hello there, // |this is a string, @@ -1306,14 +1307,15 @@ pub fn (s string) strip_margin(del ...byte) string { mut count := 0 for i := 0; i < s.len; i++ { if (s[i] in [`\n`, `\r`]) { - $if windows { - ret[count] = `\r` - ret[count+1] = `\n` - count += 2 - } $else { - ret[count] = s[i] + ret[count] = s[i] + count++ + // CRLF + if s[i] == `\r` && i < s.len - 1 && s[i+1] == `\n` { + ret[count] = s[i+1] count++ + i++ } + for s[i] != sep { i++ if i >= s.len {