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

builtin: use 0 instead of \0 rune when setting C style terminators; use C.memcpy in []string{}.join("")

This commit is contained in:
Delyan Angelov 2021-04-13 11:29:33 +03:00
parent 1b924fcf41
commit a1121d0eb0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 25 additions and 30 deletions

View File

@ -533,7 +533,7 @@ pub fn (b []byte) hex() string {
}
}
unsafe {
hex[dst_i] = `\0`
hex[dst_i] = 0
return tos(hex, dst_i)
}
}

View File

@ -30,7 +30,7 @@ pub fn (nn int) str1() string {
buf[max - len - 1] = `-`
len++
}
buf[max] = `\0`
buf[max] = 0
return tos(buf + max - len, len)
}
*/
@ -77,7 +77,7 @@ fn (nn int) str_l(max int) string {
}
mut index := max
unsafe {
buf[index--] = `\0`
buf[index--] = 0
}
for n > 0 {
n1 := int(n / 100)
@ -143,7 +143,7 @@ pub fn (nn u32) str() string {
mut buf := unsafe { malloc(max + 1) }
mut index := max
unsafe {
buf[index--] = `\0`
buf[index--] = 0
}
for n > 0 {
n1 := n / u32(100)
@ -189,7 +189,7 @@ pub fn (nn i64) str() string {
}
mut index := max
unsafe {
buf[index--] = `\0`
buf[index--] = 0
}
for n > 0 {
n1 := n / i64(100)
@ -231,7 +231,7 @@ pub fn (nn u64) str() string {
mut buf := vcalloc(max + 1)
mut index := max
unsafe {
buf[index--] = `\0`
buf[index--] = 0
}
for n > 0 {
n1 := n / 100
@ -279,7 +279,7 @@ pub fn (n int) hex1() string {
fn u64_to_hex(nn u64, len byte) string {
mut n := nn
mut buf := [256]byte{}
buf[len] = `\0`
buf[len] = 0
mut i := 0
for i = len - 1; i >= 0; i-- {
d := byte(n & 0xF)
@ -295,7 +295,7 @@ fn u64_to_hex(nn u64, len byte) string {
fn u64_to_hex_no_leading_zeros(nn u64, len byte) string {
mut n := nn
mut buf := [256]byte{}
buf[len] = `\0`
buf[len] = 0
mut i := 0
for i = len - 1; i >= 0; i-- {
d := byte(n & 0xF)
@ -452,7 +452,7 @@ pub fn (b byte) ascii_str() string {
}
unsafe {
str.str[0] = b
str.str[1] = `\0`
str.str[1] = 0
}
// println(str)
return str

View File

@ -34,7 +34,7 @@ fn (s string) add(a string) string {
for j in 0 .. a.len {
res[s.len + j] = a[j]
}
res[new_len] = `\0` // V strings are not null terminated, but just in case
res[new_len] = 0 // V strings are not null terminated, but just in case
return res
}
@ -145,6 +145,6 @@ pub fn (a string) clone() string {
str: malloc(a.len + 1)
}
mem_copy(b.str, a.str, a.len)
b[a.len] = `\0`
b[a.len] = 0
return b
}

View File

@ -257,7 +257,7 @@ pub fn (a string) clone() string {
}
unsafe {
C.memcpy(b.str, a.str, a.len)
b.str[a.len] = `\0`
b.str[a.len] = 0
}
return b
}
@ -339,7 +339,7 @@ pub fn (s string) replace(rep string, with string) string {
}
}
unsafe {
b[new_len] = `\0`
b[new_len] = 0
return tos(b, new_len)
}
}
@ -419,7 +419,7 @@ pub fn (s string) replace_each(vals []string) string {
return s.clone()
}
idxs.sort2()
mut b := unsafe { malloc(new_len + 1) } // add a \0 just in case
mut b := unsafe { malloc(new_len + 1) } // add space for 0 terminator
// Fill the new string
mut idx_pos := 0
mut cur_idx := idxs[idx_pos]
@ -451,7 +451,7 @@ pub fn (s string) replace_each(vals []string) string {
}
}
unsafe {
b[new_len] = `\0`
b[new_len] = 0
return tos(b, new_len)
}
}
@ -576,7 +576,7 @@ pub fn (s string) add(a string) string {
}
}
unsafe {
res.str[new_len] = `\0` // V strings are not null terminated, but just in case
res.str[new_len] = 0 // V strings are not null terminated, but just in case
}
return res
}
@ -726,7 +726,7 @@ pub fn (s string) substr(start int, end int) string {
}
}
unsafe {
res.str[len] = `\0`
res.str[len] = 0
}
/*
res := string {
@ -1644,26 +1644,21 @@ pub fn (a []string) join(del string) string {
len: len
}
mut idx := 0
// Go thru every string and copy its every char one by one
for i, val in a {
for j in 0 .. val.len {
unsafe {
res.str[idx] = val.str[j]
}
idx++
unsafe {
C.memcpy(res.str + idx, val.str, val.len)
idx+=val.len
}
// Add del if it's not last
if i != a.len - 1 {
for k in 0 .. del.len {
unsafe {
res.str[idx] = del.str[k]
}
idx++
if i != a.len-1 {
unsafe {
C.memcpy(res.str + idx, del.str, del.len)
idx+=del.len
}
}
}
unsafe {
res.str[res.len] = `\0`
res.str[res.len] = 0
}
return res
}