-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffer.go
executable file
·59 lines (49 loc) · 1.19 KB
/
buffer.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
package pool
import "unsafe"
//copy from std lib
//Use simple []byte instead of bytes.Buffer to avoid large dependency.
type Buffer []byte
func (b *Buffer) Write(p []byte) {
*b = append(*b, p...)
}
func (b *Buffer) WriteString(s string) {
*b = append(*b, s...)
}
func (b *Buffer) WriteByte(c byte) {
*b = append(*b, c)
}
func (b Buffer) Len() int {
return len(b)
}
func (b Buffer) String() string {
//copy from strings.Builder
return *(*string)(unsafe.Pointer(&b))
}
// https://segmentfault.com/a/1190000005006351
// func (b *Buffer) NewfromString(s string) []byte {
// x := (*[2]uintptr)(unsafe.Pointer(&s))
// h := [3]uintptr{x[0], x[1], x[1]}
// return *(*[]byte)(unsafe.Pointer(&h))
// }
// Join concatenates the elements of s to create a new byte slice. The separator
// sep is placed between elements in the resulting slice.
func Join(s []Buffer, sep []byte) Buffer {
if len(s) == 0 {
return Buffer{}
}
if len(s) == 1 {
// Just return a copy.
return append(Buffer{}, s[0]...)
}
n := len(sep) * (len(s) - 1)
for _, v := range s {
n += len(v)
}
b := make(Buffer, n)
bp := copy(b, s[0])
for _, v := range s[1:] {
bp += copy(b[bp:], sep)
bp += copy(b[bp:], v)
}
return b
}