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