-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
117 lines (97 loc) · 2.49 KB
/
buffer.go
File metadata and controls
117 lines (97 loc) · 2.49 KB
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
package main
import (
"bytes"
"errors"
"io"
"strings"
)
// Buffer is a simple implenetation of the buffer that stores data in a []byte.
type Buffer struct {
filename string
data []byte
}
// NewBuffer constructs a new ByteBuffer object containing data.
func NewBuffer() Buffer {
return Buffer{}
}
// Filename returns the buffers filename as a byte slice.
func (buffer *Buffer) Filename() string {
return buffer.filename
}
// SetFilename ses the filename of that will be used when the buffer is saved.
func (buffer *Buffer) SetFilename(filename string) {
buffer.filename = filename
}
// NewReader returns a new Reader reading from the buffer contents.
func (buffer *Buffer) NewReader() io.Reader {
b := bytes.NewReader(buffer.data)
return b
}
// SetData sets the data of the buffer.
func (buffer *Buffer) SetData(data []byte) {
buffer.ReadData(bytes.NewReader(data))
}
// SetDataString sets the data of the buffer.
func (buffer *Buffer) SetDataString(data string) {
buffer.ReadData(strings.NewReader(data))
}
// ReadData reads the data from the given stream into the buffer replacing anything already there.
func (buffer *Buffer) ReadData(data io.Reader) {
b := new(bytes.Buffer)
b.ReadFrom(data)
buffer.setData(b.Bytes())
}
func (buffer *Buffer) setData(data []byte) {
buffer.data = data
}
// GetLine returns the requested line as a string.
func (buffer *Buffer) GetLine(lineNum int) (string, error) {
lines, _ := buffer.GetLines(lineNum, lineNum)
if len(lines) == 1 {
return lines[0], nil
}
return "", errors.New("Not found")
}
// GetLines returns the requested range of lines.
func (buffer *Buffer) GetLines(first, last int) ([]string, error) {
if first > last {
return []string{}, errors.New("Invalid range, first > last.")
}
lineNum := 1
pos := 0
grabbing := false
lines := []string{}
for {
if lineNum >= first {
grabbing = true
}
if grabbing {
line, err := untillNewLineOrEnd(buffer.data[pos:])
if err == nil {
lines = append(lines, line)
}
}
nextLine := bytesUntillNextNewline(buffer.data[pos:]) + 1
if nextLine == 0 {
lineNum = last
}
if lineNum == last {
return lines, nil
}
pos += nextLine
lineNum++
}
}
func untillNewLineOrEnd(data []byte) (string, error) {
endOfLine := bytesUntillNextNewline(data)
if endOfLine == -1 {
if len(data) == 0 {
return "", errors.New("Not found")
}
return string(data), nil
}
return string(data[:endOfLine]), nil
}
func bytesUntillNextNewline(data []byte) int {
return bytes.IndexByte(data, '\n')
}