-
Notifications
You must be signed in to change notification settings - Fork 43
/
cstr.go
49 lines (44 loc) · 1.01 KB
/
cstr.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
package bbs
import "bytes"
//Cstr
//
//[]byte with C String property in that \0 is considered as the end of the bytes/string.
//It is used to convert from fixed-length bytes to string or []byte with no \0.
//
//Naming Cstr instead of CString is to avoid confusion with C.CString
//(C.CString is from string, and should be compatible with string, not with []byte)
//(We also have str(len/cpy/cmp) functions in C)
//
//See tests for more examples of how to use fixed-bytes with Cstr to get no-\0 string / []byte
type Cstr []byte
//CstrToString
//
//Only the bytes until \0 when converting to string.
//See tests for more examples.
//
//Params
// cstr
//
//Return
// string: string
func CstrToString(cstr Cstr) string {
theBytes := CstrToBytes(cstr)
return string(theBytes)
}
//CstrToBytes
//
//Only the bytes until \0.
//See tests for more examples.
//
//Params
// cstr
//
//Return
// []byte: bytes
func CstrToBytes(cstr Cstr) []byte {
len := bytes.IndexByte(cstr, 0x00)
if len == -1 {
return cstr
}
return cstr[:len]
}