-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
70 lines (54 loc) · 1.06 KB
/
reader.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
62
63
64
65
66
67
68
69
70
package ngx
import "unsafe"
type Reader interface {
Len() int
Bytes() []byte
String() string
NewBytes() []byte
NewString() string
}
type BytesReader struct {
buf []byte
}
func NewBytesReader(b []byte) *BytesReader {
return &BytesReader{b}
}
func (r *BytesReader) Len() int {
return len(r.buf)
}
func (r *BytesReader) Bytes() []byte {
return r.buf
}
func (r *BytesReader) String() string {
return *(*string)(unsafe.Pointer(&r.buf))
}
func (r *BytesReader) NewBytes() []byte {
buf := make([]byte, r.Len())
copy(buf, r.buf)
return buf
}
func (r *BytesReader) NewString() string {
return string(r.buf)
}
type StringReader struct {
buf string
cap int
}
func NewStringReader(s string) *StringReader {
return &StringReader{s, len(s)}
}
func (r *StringReader) Len() int {
return len(r.buf)
}
func (r *StringReader) Bytes() []byte {
return *(*[]byte)(unsafe.Pointer(r))
}
func (r *StringReader) String() string {
return r.buf
}
func (r *StringReader) NewBytes() []byte {
return []byte(r.buf)
}
func (r *StringReader) NewString() string {
return string(r.buf)
}