builtin: speed up string.clone() by using C.memcpy (#5837)

pull/5895/head
Nick Treleaven 2020-07-20 15:44:35 +01:00 committed by GitHub
parent c93467bca5
commit fb76e02c59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 13 deletions

View File

@ -85,9 +85,6 @@ pub fn tos(s byteptr, len int) string {
}
pub fn tos_clone(s byteptr) string {
if s == 0 {
panic('tos: nil string')
}
return tos2(s).clone()
}
@ -131,13 +128,11 @@ fn (a string) clone_static() string {
pub fn (a string) clone() string {
mut b := string{
str: malloc(a.len + 1)
str: unsafe {malloc(a.len + 1)}
len: a.len
}
for i in 0..a.len {
b.str[i] = a.str[i]
}
unsafe {
C.memcpy(b.str, a.str, a.len)
b.str[a.len] = `\0`
}
return b
@ -152,12 +147,7 @@ pub fn (s string) cstr() byteptr {
// cstring_to_vstring creates a copy of cstr and turns it into a v string
pub fn cstring_to_vstring(cstr byteptr) string {
unsafe {
slen := C.strlen(charptr(cstr))
mut s := byteptr(memdup(cstr, slen + 1))
s[slen] = `\0`
return tos(s, slen)
}
return tos_clone(cstr)
}
pub fn (s string) replace_once(rep, with string) string {