builtin: add trailing `0` to reversed string (#10248)
parent
bd467f94ab
commit
15557161cc
|
@ -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 ` `
|
||||
|
|
Loading…
Reference in New Issue