From 15557161ccb1c8f5c384c10af00e496fefc3f9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 29 May 2021 15:45:26 +0200 Subject: [PATCH] builtin: add trailing `0` to reversed string (#10248) --- vlib/builtin/string.v | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index fe1f1d1ac3..1b6e58a0c3 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1621,7 +1621,7 @@ pub fn (s string) reverse() string { return s.clone() } mut res := string{ - str: unsafe { malloc(s.len) } + str: unsafe { malloc(s.len + 1) } len: s.len } for i := s.len - 1; i >= 0; i-- { @@ -1629,6 +1629,9 @@ pub fn (s string) reverse() string { res.str[s.len - i - 1] = s[i] } } + unsafe { + res.str[res.len] = 0 + } return res } @@ -1681,11 +1684,11 @@ pub fn (s string) repeat(count int) string { } } } + new_len := s.len * count unsafe { - new_len := s.len * count ret[new_len] = 0 - return ret.vstring_with_len(new_len) } + return unsafe { ret.vstring_with_len(new_len) } } // fields returns a string array of the string split by `\t` and ` `