builtin.string: minor fixes in join() (#9952)

pull/9975/head
Mark 2021-05-02 17:31:47 +01:00 committed by GitHub
parent 581fe375cc
commit feb60674b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 7 deletions

View File

@ -1606,17 +1606,17 @@ pub fn (s string) after_char(dot byte) string {
return s[pos + 1..]
}
// join joins a string array into a string using `del` delimiter.
// join joins a string array into a string using `sep` separator.
// Example: assert ['Hello','V'].join(' ') == 'Hello V'
pub fn (a []string) join(del string) string {
pub fn (a []string) join(sep string) string {
if a.len == 0 {
return ''
}
mut len := 0
for val in a {
len += val.len + del.len
len += val.len + sep.len
}
len -= del.len
len -= sep.len
// Allocate enough memory
mut res := string{
str: unsafe { malloc(len + 1) }
@ -1628,11 +1628,11 @@ pub fn (a []string) join(del string) string {
C.memcpy(res.str + idx, val.str, val.len)
idx += val.len
}
// Add del if it's not last
// Add sep if it's not last
if i != a.len - 1 {
unsafe {
C.memcpy(res.str + idx, del.str, del.len)
idx += del.len
C.memcpy(res.str + idx, sep.str, sep.len)
idx += sep.len
}
}
}