forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
61 lines (53 loc) · 2 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package noarch
// Strlen returns the length of a string.
//
// The length of a C string is determined by the terminating null-character: A
// C string is as long as the number of characters between the beginning of the
// string and the terminating null character (without including the terminating
// null character itself).
func Strlen(a []byte) int {
// TODO: The transpiler should have a syntax that means this proxy function
// does not need to exist.
return len(CStringToString(a))
}
// Strcpy copies the C string pointed by source into the array pointed by
// destination, including the terminating null character (and stopping at that
// point).
//
// To avoid overflows, the size of the array pointed by destination shall be
// long enough to contain the same C string as source (including the terminating
// null character), and should not overlap in memory with source.
func Strcpy(dest, src []byte) []byte {
for i, c := range src {
dest[i] = c
// We only need to copy until the first NULL byte. Make sure we also
// include that NULL byte on the end.
if c == 0 {
break
}
}
return dest
}
// Strncpy copies the first num characters of source to destination. If the end
// of the source C string (which is signaled by a null-character) is found
// before num characters have been copied, destination is padded with zeros
// until a total of num characters have been written to it.
//
// No null-character is implicitly appended at the end of destination if source
// is longer than num. Thus, in this case, destination shall not be considered a
// null terminated C string (reading it as such would overflow).
//
// destination and source shall not overlap (see memmove for a safer alternative
// when overlapping).
func Strncpy(dest, src []byte, len int) []byte {
// Copy up to the len or first NULL bytes - whichever comes first.
i := 0
for ; i < len && src[i] != 0; i++ {
dest[i] = src[i]
}
// The rest of the dest will be padded with zeros to the len.
for ; i < len; i++ {
dest[i] = 0
}
return dest
}