-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.go
137 lines (113 loc) · 3.09 KB
/
bytes.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package tl
import (
"bytes"
"io"
"unsafe"
)
// Bytes wraps the behavior of Vec as Vec[byte] adding new methods on top.
type Bytes Vec[byte]
var (
_ io.WriterTo = Bytes{}
_ io.ReaderFrom = Bytes{}
)
// NewBytes returns a Bytes instance with size `size` and capacity `capacity`.
func NewBytes(size, capacity int) Bytes {
return BytesFrom(make([]byte, size, capacity))
}
// BytesFrom creates a Bytes instance from `bts` bytes.
func BytesFrom(bts []byte) Bytes {
return Bytes(bts)
}
// Slice returns a sliced Bytes using indexes starting from `low` and ending in `max`.
func (b Bytes) Slice(low, max int) Bytes {
if max <= 0 {
return b[low:]
}
return b[low:max]
}
// CopyFrom copies the bytes from `other` to `b`.
func (b *Bytes) CopyFrom(other Bytes) {
b.Reserve(other.Len())
copy(*b, other)
}
// WriteTo writes the bytes to an io.Writer.
//
// Implements the io.WriterTo interface.
func (b Bytes) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b)
return int64(n), err
}
// ReadFrom reads bytes from an io.Reader into `b`.
//
// This function does NOT append bytes, it uses the existing buffer.
//
// Implements the io.ReaderFrom interface.
func (b Bytes) ReadFrom(r io.Reader) (int64, error) {
n, err := r.Read(b)
return int64(n), err
}
// LimitReadFrom is like ReadFrom but it is limited to `n` bytes.
func (b *Bytes) LimitReadFrom(r io.Reader, n int) (int64, error) {
b.Reserve(n)
return b.Slice(0, n).ReadFrom(r)
}
// LimitWriteTo writes a limited amount of `n` bytes to an io.Writer.
func (b Bytes) LimitWriteTo(w io.Writer, n int) (int64, error) {
return b.Slice(0, Min(b.Len(), n)).WriteTo(w)
}
// Len returns the length of `b`.
//
// This function is equivalent to `len(b))`.
func (b Bytes) Len() int {
return len(b)
}
// Cap returns the capacity of `b`.
//
// This function is equivalent to `cap(b))`.
func (b Bytes) Cap() int {
return cap(b)
}
// String converts `b` to a string.
//
// This function produces an allocation. To avoid the allocation use UnsafeString.
func (b Bytes) String() string {
return string(b)
}
// UnsafeString converts `b` to a string without producing allocations.
func (b Bytes) UnsafeString() string {
return *(*string)(unsafe.Pointer(&b))
}
// IndexByte returns the index of `c` in `b`.
//
// This function is equivalent to bytes.IndexByte(b, c).
func (b Bytes) IndexByte(c byte) int {
return bytes.IndexByte(b, c)
}
// Index returns the index of `other` in `b`.
//
// This function is equivalent to bytes.Index(b, other).
func (b Bytes) Index(other Bytes) int {
return bytes.Index(b, other)
}
// Resize increases or decreases the size of Bytes.
func (b *Bytes) Resize(n int) {
vc := (*Vec[byte])(b)
vc.Resize(n)
}
// Reserve increases the size of Bytes if needed.
//
// The call doesn't produce any allocation if `n` < cap(b).
func (b *Bytes) Reserve(n int) {
vc := (*Vec[byte])(b)
vc.Reserve(n)
}
// Append appends bytes at the end of Bytes.
func (b *Bytes) Append(others ...byte) {
vc := (*Vec[byte])(b)
vc.Append(others...)
}
// Push pushes the `bts` to the beginning of Bytes.
func (b *Bytes) Push(bts ...byte) {
vc := (*Vec[byte])(b)
vc.Push(bts...)
}