builtin: add trailing `0` to reversed string (#10248)

pull/10261/head
Uwe Krüger 2021-05-29 15:45:26 +02:00 committed by GitHub
parent bd467f94ab
commit 15557161cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -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 ` `