-
Notifications
You must be signed in to change notification settings - Fork 255
/
linecache.go
60 lines (52 loc) · 945 Bytes
/
linecache.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
package bufreader
import (
"sync"
)
type LineCache struct {
lines []string
lock *sync.RWMutex
}
func NewLineCache() *LineCache {
return &LineCache{
lines: make([]string, 0, 16),
lock: new(sync.RWMutex),
}
}
func (l *LineCache) Size() int {
l.lock.RLock()
defer l.lock.RUnlock()
return len(l.lines)
}
func (l *LineCache) Set(r []string) {
l.lock.Lock()
defer l.lock.Unlock()
l.lines = r
}
func (l *LineCache) Append(r string) {
l.lock.Lock()
defer l.lock.Unlock()
l.lines = append(l.lines, r)
}
func (l *LineCache) TotalLen() int {
l.lock.RLock()
defer l.lock.RUnlock()
var ret int
for _, v := range l.lines {
ret += len(v)
}
return ret
}
func (l *LineCache) Combine() []byte {
if l.Size() <= 0 {
return make([]byte, 0)
}
n := l.TotalLen()
xb := make([]byte, n)
l.lock.RLock()
defer l.lock.RUnlock()
bp := copy(xb, l.lines[0])
for _, s := range l.lines[1:] {
bp += copy(xb[bp:], s)
}
return xb
}