v2 / vlib / builtin / reuse.v
45 lines · 43 sloc · 2.54 KB · a5ece523e211184eace20b51396a21ffbf4b661e
Raw
1module builtin
2
3// reuse_data_as_string provides a way to treat the memory of a []u8 `buffer` as a string value.
4// It does not allocate or copy the memory block for the `buffer`, but instead creates a string descriptor,
5// that will point to the same memory as the input.
6// The intended use of that function, is to allow calling string search methods (defined on string),
7// on []u8 values too, without having to copy/allocate by calling .bytestr() (that can be too slow and unnecessary in loops).
8// Note: unlike normal V strings, the return value *is not* guaranteed to have a terminating `0` byte,
9// since this function does not allocate or modify the input in any way. This is not a problem usually,
10// since V methods and functions do not require it, but be careful, if you want to pass that string to call a C. function,
11// that expects 0 termination. If you have to do it, make a `tmp := s.clone()` beforehand, and free the cloned `tmp` string
12// after you have called the C. function with it.
13// The .len field of the result value, will be the same as the buffer.len.
14// Note: avoid storing or returning that resulting string,
15// and avoid calling the fn with a complex expression (prefer using a temporary variable as an argument).
16@[unsafe]
17pub fn reuse_data_as_string(buffer []u8) string {
18 return string{
19 str: buffer.data
20 len: buffer.len
21 is_lit: 1 // prevent freeing the string, since its memory is owned by the input buffer
22 }
23}
24
25// reuse_string_as_data provides a way to treat the memory of a string `s`, as a []u8 buffer.
26// It does not allocate or copy the memory block for the string `s`, but instead creates an array descriptor,
27// that will point to the same memory as the input.
28// The intended use of that function, is to allow calling array methods (defined on []u8),
29// on string values too, without having to copy/allocate by calling .bytes() (that can be too slow and unnecessary in loops).
30// Note: since there are no allocations, the buffer *will not* contain the terminating `0` byte, that V strings have usually.
31// The .len field of the result value, will be the same as s.len .
32// Note: avoid storing or returning that resulting byte buffer,
33// and avoid calling the fn with a complex expression (prefer using a temporary variable as an argument).
34@[unsafe]
35pub fn reuse_string_as_data(s string) []u8 {
36 mut res := unsafe {
37 array{
38 data: s.str
39 len: s.len
40 element_size: 1
41 flags: .nogrow | .noshrink | .nofree // prevent freeing/resizing the array, since its memory is owned by the input string
42 }
43 }
44 return res
45}
46