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

vlib: add [unsafe] tag to more functions: tos, string_from_wide, strings.Builder: write_bytes, free (#8766)

This commit is contained in:
Nick Treleaven
2021-02-15 15:15:52 +00:00
committed by GitHub
parent 4bdbb0cfa8
commit 4a0367a63c
25 changed files with 82 additions and 60 deletions

View File

@@ -73,7 +73,7 @@ pub fn vstrlen(s byteptr) int {
// tos converts a C string to a V string.
// String data is reused, not copied.
//[unsafe]
[unsafe]
pub fn tos(s byteptr, len int) string {
// This should never happen.
if s == 0 {
@@ -86,12 +86,14 @@ pub fn tos(s byteptr, len int) string {
}
// tos_clone returns a copy of `s`.
[unsafe]
pub fn tos_clone(s byteptr) string {
return tos2(s).clone()
return unsafe { tos2(s) }.clone()
}
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
// Used only internally.
[unsafe]
pub fn tos2(s byteptr) string {
if s == 0 {
panic('tos2: nil string')
@@ -103,6 +105,7 @@ pub fn tos2(s byteptr) string {
}
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
[unsafe]
pub fn tos3(s charptr) string {
if s == 0 {
panic('tos3: nil string')
@@ -114,19 +117,21 @@ pub fn tos3(s charptr) string {
}
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
[unsafe]
pub fn tos4(s byteptr) string {
if s == 0 {
return ''
}
return tos2(s)
return unsafe { tos2(s) }
}
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
[unsafe]
pub fn tos5(s charptr) string {
if s == 0 {
return ''
}
return tos3(s)
return unsafe { tos3(s) }
}
[deprecated]
@@ -207,7 +212,7 @@ pub fn (s string) cstr() byteptr {
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
[unsafe]
pub fn cstring_to_vstring(cstr byteptr) string {
return tos_clone(cstr)
return unsafe { tos_clone(cstr) }
}
// replace_once replaces the first occurence of `rep` with the string passed in `with`.