From a1121d0eb07ccaac260409d53c0666766c2c2785 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 13 Apr 2021 11:29:33 +0300 Subject: [PATCH] builtin: use 0 instead of `\0` rune when setting C style terminators; use C.memcpy in `[]string{}.join("")` --- vlib/builtin/array.v | 2 +- vlib/builtin/int.v | 16 ++++++------- vlib/builtin/linux_bare/string_bare.v | 4 ++-- vlib/builtin/string.v | 33 ++++++++++++--------------- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 681caf99ed..d40cfa2dd1 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -533,7 +533,7 @@ pub fn (b []byte) hex() string { } } unsafe { - hex[dst_i] = `\0` + hex[dst_i] = 0 return tos(hex, dst_i) } } diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index fbfdb59220..f497ed041b 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -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 diff --git a/vlib/builtin/linux_bare/string_bare.v b/vlib/builtin/linux_bare/string_bare.v index 06f95d429e..8f7edfcfb4 100644 --- a/vlib/builtin/linux_bare/string_bare.v +++ b/vlib/builtin/linux_bare/string_bare.v @@ -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 } diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 2142fadb0c..055a99ccc1 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -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 }