v2 / vlib / builtin / string_charptr_byteptr_helpers.v
102 lines · 93 sloc · 2.72 KB · 110811940a8ecbc4d2abcdc29ae288cb78c89d8a
Raw
1module builtin
2
3// byteptr.vbytes() - makes a V []u8 structure from a C style memory buffer. Note: the data is reused, NOT copied!
4@[reused; unsafe]
5pub fn (data byteptr) vbytes(len int) []u8 {
6 return unsafe { voidptr(data).vbytes(len) }
7}
8
9// vstring converts a C style string to a V string. Note: the string data is reused, NOT copied.
10// strings returned from this function will be normal V strings beside that (i.e. they would be
11// freed by V's -autofree mechanism, when they are no longer used).
12@[reused; unsafe]
13pub fn (bp byteptr) vstring() string {
14 return string{
15 str: bp
16 len: unsafe { vstrlen(bp) }
17 }
18}
19
20// vstring_with_len converts a C style string to a V string.
21// Note: the string data is reused, NOT copied.
22@[reused; unsafe]
23pub fn (bp byteptr) vstring_with_len(len int) string {
24 return string{
25 str: bp
26 len: len
27 is_lit: 0
28 }
29}
30
31// vstring converts C char* to V string.
32// Note: the string data is reused, NOT copied.
33@[reused; unsafe]
34pub fn (cp charptr) vstring() string {
35 return string{
36 str: byteptr(cp)
37 len: unsafe { vstrlen_char(cp) }
38 is_lit: 0
39 }
40}
41
42// vstring_with_len converts C char* to V string.
43// Note: the string data is reused, NOT copied.
44@[reused; unsafe]
45pub fn (cp charptr) vstring_with_len(len int) string {
46 return string{
47 str: byteptr(cp)
48 len: len
49 is_lit: 0
50 }
51}
52
53// vstring_literal converts a C style string to a V string.
54// Note: the string data is reused, NOT copied.
55// NB2: unlike vstring, vstring_literal will mark the string
56// as a literal, so it will not be freed by autofree.
57// This is suitable for readonly strings, C string literals etc,
58// that can be read by the V program, but that should not be
59// managed by it, for example `os.args` is implemented using it.
60@[reused; unsafe]
61pub fn (bp byteptr) vstring_literal() string {
62 return string{
63 str: bp
64 len: unsafe { vstrlen(bp) }
65 is_lit: 1
66 }
67}
68
69// vstring_with_len converts a C style string to a V string.
70// Note: the string data is reused, NOT copied.
71@[reused; unsafe]
72pub fn (bp byteptr) vstring_literal_with_len(len int) string {
73 return string{
74 str: bp
75 len: len
76 is_lit: 1
77 }
78}
79
80// vstring_literal converts C char* to V string.
81// See also vstring_literal defined on byteptr for more details.
82// Note: the string data is reused, NOT copied.
83@[reused; unsafe]
84pub fn (cp charptr) vstring_literal() string {
85 return string{
86 str: byteptr(cp)
87 len: unsafe { vstrlen_char(cp) }
88 is_lit: 1
89 }
90}
91
92// vstring_literal_with_len converts C char* to V string.
93// See also vstring_literal_with_len defined on byteptr.
94// Note: the string data is reused, NOT copied.
95@[reused; unsafe]
96pub fn (cp charptr) vstring_literal_with_len(len int) string {
97 return string{
98 str: byteptr(cp)
99 len: len
100 is_lit: 1
101 }
102}
103