-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
ring.go
49 lines (38 loc) · 706 Bytes
/
ring.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 jsonx
import (
"strings"
)
type ring struct {
buf [70]byte
start, end int
}
func (r *ring) writeByte(b byte) {
r.buf[r.end] = b
r.end = (r.end + 1) % len(r.buf)
if r.end == r.start {
r.start = (r.start + 1) % len(r.buf)
}
}
func (r *ring) string() string {
var lines []byte
newlineCount := 0
for i := r.end - 1; ; i-- {
if i < 0 {
i = len(r.buf) - 1
}
if r.buf[i] == '\n' {
newlineCount++
if newlineCount == 2 {
break
}
}
lines = append(lines, r.buf[i])
if i == r.start {
break
}
}
for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 {
lines[i], lines[j] = lines[j], lines[i]
}
return strings.TrimRight(string(lines), "\t \n")
}