-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbuf.go
68 lines (54 loc) · 1.45 KB
/
buf.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
package main
import (
"io/ioutil"
"os"
"strings"
"unicode/utf8"
)
type Buffer []rune
func NewBuffer() Buffer { return []rune{} }
func (b *Buffer) Insert(q0 int, r []rune) {
if q0 > (len(*b)) {
panic("internal error: buffer.Insert: Out of range insertion")
}
(*b) = append((*b)[:q0], append(r, (*b)[q0:]...)...)
}
func (b *Buffer) Delete(q0, q1 int) {
if q0 > (len(*b)) || q1 > (len(*b)) {
panic("internal error: buffer.Delete: Out-of-range Delete")
}
copy((*b)[q0:], (*b)[q1:])
(*b) = (*b)[:(len(*b))-(q1-q0)] // Reslice to length
}
func (b *Buffer) Load(q0 int, fd *os.File) (n int, h FileHash, hasNulls bool, err error) {
// TODO(flux): Innefficient to load the file, then copy into the slice,
// but I need the UTF-8 interpretation. I could fix this by using a
// UTF-8 -> []rune reader on top of the os.File instead.
d, err := ioutil.ReadAll(fd)
if err != nil {
warning(nil, "read error in Buffer.Load")
}
s := string(d)
s = strings.Replace(s, "\000", "", -1)
hasNulls = len(s) != len(d)
runes := []rune(s)
(*b).Insert(q0, runes)
return (len(runes)), calcFileHash(d), hasNulls, err
}
func (b *Buffer) Read(q0 int, r []rune) (int, error) {
n := copy(r, (*b)[q0:])
return n, nil
}
func (b *Buffer) ReadC(q int) rune { return (*b)[q] }
func (b *Buffer) Close() {
(*b).Reset()
}
func (b *Buffer) Reset() {
(*b) = (*b)[0:0]
}
func (b *Buffer) Nc() int {
return len(*b)
}
func fbufalloc() []rune {
return make([]rune, BUFSIZE/utf8.UTFMax)
}