1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

View File

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