v / vlib / builtin
Raw file | 84 loc (76 sloc) | 2.99 KB | Latest commit hash 8f8a18615
1module builtin
2
3// vstrlen returns the V length of the C string `s` (0 terminator is not counted).
4// The C string is expected to be a &byte pointer.
5[inline; unsafe]
6pub fn vstrlen(s &byte) int {
7 return unsafe { C.strlen(&char(s)) }
8}
9
10// vstrlen_char returns the V length of the C string `s` (0 terminator is not counted).
11// The C string is expected to be a &char pointer.
12[inline; unsafe]
13pub fn vstrlen_char(s &char) int {
14 return unsafe { C.strlen(s) }
15}
16
17// vmemcpy copies n bytes from memory area src to memory area dest.
18// The memory areas *MUST NOT OVERLAP*. Use vmemmove, if the memory
19// areas do overlap. vmemcpy returns a pointer to `dest`.
20[inline; unsafe]
21pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
22 $if trace_vmemcpy ? {
23 C.fprintf(C.stderr, c'vmemcpy dest: %p src: %p n: %6ld\n', dest, const_src, n)
24 }
25 unsafe {
26 return C.memcpy(dest, const_src, n)
27 }
28}
29
30// vmemmove copies n bytes from memory area `src` to memory area `dest`.
31// The memory areas *MAY* overlap: copying takes place as though the bytes
32// in `src` are first copied into a temporary array that does not overlap
33// `src` or `dest`, and the bytes are then copied from the temporary array
34// to `dest`. vmemmove returns a pointer to `dest`.
35[inline; unsafe]
36pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
37 $if trace_vmemmove ? {
38 C.fprintf(C.stderr, c'vmemmove dest: %p src: %p n: %6ld\n', dest, const_src, n)
39 }
40 unsafe {
41 return C.memmove(dest, const_src, n)
42 }
43}
44
45// vmemcmp compares the first n bytes (each interpreted as unsigned char)
46// of the memory areas s1 and s2. It returns an integer less than, equal to,
47// or greater than zero, if the first n bytes of s1 is found, respectively,
48// to be less than, to match, or be greater than the first n bytes of s2.
49// For a nonzero return value, the sign is determined by the sign of the
50// difference between the first pair of bytes (interpreted as unsigned char)
51// that differ in s1 and s2.
52// If n is zero, the return value is zero.
53// Do NOT use vmemcmp to compare security critical data, such as cryptographic
54// secrets, because the required CPU time depends on the number of equal bytes.
55// You should use a function that performs comparisons in constant time for
56// this.
57[inline; unsafe]
58pub fn vmemcmp(const_s1 voidptr, const_s2 voidptr, n isize) int {
59 $if trace_vmemcmp ? {
60 C.fprintf(C.stderr, c'vmemcmp s1: %p s2: %p n: %6ld\n', const_s1, const_s2, n)
61 }
62 unsafe {
63 return C.memcmp(const_s1, const_s2, n)
64 }
65}
66
67// vmemset fills the first `n` bytes of the memory area pointed to by `s`,
68// with the constant byte `c`. It returns a pointer to the memory area `s`.
69[inline; unsafe]
70pub fn vmemset(s voidptr, c int, n isize) voidptr {
71 $if trace_vmemset ? {
72 C.fprintf(C.stderr, c'vmemset s: %p c: %d n: %6ld\n', s, c, n)
73 }
74 unsafe {
75 return C.memset(s, c, n)
76 }
77}
78
79type FnSortCB = fn (const_a voidptr, const_b voidptr) int
80
81[inline; unsafe]
82fn vqsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
83 C.qsort(base, nmemb, size, voidptr(sort_cb))
84}