mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: speed up string methods with vmemcpy instead of for
loop for copying data (#18211)
This commit is contained in:
parent
aded6088e9
commit
9d0a1d8496
@ -1051,12 +1051,8 @@ pub fn (s string) substr(start int, end int) string {
|
||||
str: unsafe { malloc_noscan(len + 1) }
|
||||
len: len
|
||||
}
|
||||
for i in 0 .. len {
|
||||
unsafe {
|
||||
res.str[i] = s.str[start + i]
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
vmemcpy(res.str, s.str + start, len)
|
||||
res.str[len] = 0
|
||||
}
|
||||
return res
|
||||
@ -1077,12 +1073,8 @@ pub fn (s string) substr_with_check(start int, end int) !string {
|
||||
str: unsafe { malloc_noscan(len + 1) }
|
||||
len: len
|
||||
}
|
||||
for i in 0 .. len {
|
||||
unsafe {
|
||||
res.str[i] = s.str[start + i]
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
vmemcpy(res.str, s.str + start, len)
|
||||
res.str[len] = 0
|
||||
}
|
||||
return res
|
||||
@ -1114,14 +1106,7 @@ pub fn (s string) substr_ni(_start int, _end int) string {
|
||||
}
|
||||
|
||||
if start > s.len || end < start {
|
||||
mut res := string{
|
||||
str: unsafe { malloc_noscan(1) }
|
||||
len: 0
|
||||
}
|
||||
unsafe {
|
||||
res.str[0] = 0
|
||||
}
|
||||
return res
|
||||
return ''
|
||||
}
|
||||
|
||||
len := end - start
|
||||
@ -1131,12 +1116,8 @@ pub fn (s string) substr_ni(_start int, _end int) string {
|
||||
str: unsafe { malloc_noscan(len + 1) }
|
||||
len: len
|
||||
}
|
||||
for i in 0 .. len {
|
||||
unsafe {
|
||||
res.str[i] = s.str[start + i]
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
vmemcpy(res.str, s.str + start, len)
|
||||
res.str[len] = 0
|
||||
}
|
||||
return res
|
||||
|
Loading…
Reference in New Issue
Block a user