forked from nazarifard/fastape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
65 lines (57 loc) · 1.15 KB
/
Copy pathstring.go
File metadata and controls
65 lines (57 loc) · 1.15 KB
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
62
63
64
65
package fastape
import "unsafe"
type StringTape struct {
len LenTape
}
func (t StringTape) Sizeof(s string) int {
if s == "" {
return 1
}
strLen := len(s)
return t.len.Sizeof(strLen) + strLen //8
}
func (t StringTape) Roll(s string, bs []byte) (n int, err error) {
if len(bs) == 0 {
return 0, ErrNoSpaceLeft
}
if s == "" {
bs[0] = 0
return 1, nil //OK
}
strLen := len(s)
n, err = t.len.Roll(strLen, bs)
if err != nil {
return 0, err
}
n += copy(bs[n:], s)
return n, err
}
func (t StringTape) Unroll(bs []byte, ps *string) (n int, err error) {
if ps == nil {
return 0, ErrNilPtr
}
if bs[0] == 0 {
*ps = ""
return 1, nil //OK
}
var strLen int
n, err = t.len.Unroll(bs, &strLen)
if err != nil {
return 0, err
}
*ps = b2s(bs[n : n+strLen]) //todo more check
return n + strLen, err
}
func b2s(b []byte) string {
return unsafe.String(&b[0], len(b))
}
// https://github.com/golang/go/issues/53003#issuecomment-1145241692
// type StringHeader struct {
// Data *byte
// Len int
// }
// func s2b(s string) []byte {
// header := (*StringHeader)(unsafe.Pointer(&s))
// bytes := *(*[]byte)(unsafe.Pointer(header))
// return bytes
// }