-
Notifications
You must be signed in to change notification settings - Fork 141
/
buffer_grep.go
141 lines (127 loc) · 3.02 KB
/
buffer_grep.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package the_platinum_searcher
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
)
type bufferGrep struct {
printer
pattern pattern
column bool
}
func (g bufferGrep) grep(path string, buf []byte) {
f, err := getFileHandler(path)
if err != nil {
log.Fatalf("open: %s\n", err)
}
defer f.Close()
identified := false
var encoding int
pattern := g.pattern.pattern
match := match{path: path}
offset, read := 0, 0
loop:
for {
n, err := f.Read(buf[offset:])
if err == io.EOF {
// Scan remain (For last line without new line.)
scan(&match, buf[:offset], pattern, read, encoding, g.column)
break
}
if err != nil {
panic(err)
}
cbuf := buf[0 : offset+n]
// detect encoding.
if !identified {
limit := n
if limit > 512 {
limit = 512
}
if f == os.Stdin {
// TODO: File type is fixed in ASCII because it can not determine the character code.
encoding = ASCII
} else {
encoding = detectEncoding(cbuf[:limit])
}
if encoding == ERROR || encoding == BINARY {
break
}
if r := newEncodeReader(bytes.NewReader(pattern), encoding); r != nil {
// encode pattern to shift-jis or euc-jp.
pattern, _ = ioutil.ReadAll(r)
}
identified = true
}
newLine := bytes.LastIndexByte(cbuf, '\n')
// fmt.Printf("offset: %d, newLine: %d\n", offset, newLine)
if newLine >= 0 {
c := scan(&match, cbuf[0:newLine], pattern, read, encoding, g.column)
// matchLines = append(matchLines, m...)
offset = len(cbuf[newLine+1:])
for i, _ := range cbuf[newLine+1:] {
buf[0+i] = cbuf[newLine+1+i]
}
read += c
} else {
grow := make([]byte, len(cbuf)*2)
copy(grow, buf)
buf = grow
offset = len(cbuf)
continue loop
}
}
g.printer.print(match)
}
var NewLineBytes = []byte{10}
func scanNewLine(buf []byte) int {
return bytes.Count(buf, NewLineBytes)
}
func scan(match *match, buf, pattern []byte, base, encoding int, column bool) int {
offset, newLineCount := 0, 0
for {
if offset > len(buf) {
break
}
cbuf := buf[offset:]
idx := bytes.Index(cbuf, pattern)
if idx == -1 {
newLineCount += scanNewLineCount(cbuf)
break
}
beforeNewLine := bytes.LastIndexByte(cbuf[:idx], '\n')
if beforeNewLine != -1 {
newLineCount += (scanNewLineCount(cbuf[:beforeNewLine]) + 1)
}
num := base + newLineCount + 1
afterNewLine := bytes.IndexByte(cbuf[idx+len(pattern):], '\n')
if afterNewLine == -1 {
afterNewLine = len(cbuf) - (idx + len(pattern))
} else {
newLineCount++
}
mbuf := cbuf[beforeNewLine+1 : idx+len(pattern)+afterNewLine]
line := make([]byte, len(mbuf))
copy(line, mbuf)
// decode bytes from shift-jis or euc-jp.
if r := newDecodeReader(bytes.NewReader(line), encoding); r != nil {
line, _ = ioutil.ReadAll(r)
}
c := 0
if column {
if beforeNewLine == -1 {
c = idx + 1
} else {
c = idx - beforeNewLine
}
}
match.add(num, c, string(line), true)
offset += idx + len(pattern) + afterNewLine + 1
}
return newLineCount + 1
}
func scanNewLineCount(buf []byte) int {
return bytes.Count(buf, NewLineBytes)
}