-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
69 lines (59 loc) · 1.28 KB
/
scanner.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
package xml
import (
"bufio"
"bytes"
"fmt"
"unicode"
)
var (
tagStart = []byte{'<'}
tagEnd = []byte{'>'}
)
func split(data []byte, atEOF bool) (int, []byte, error) {
tmpData := bytes.TrimLeft(data, " \n\r\t")
diff := len(data) - len(tmpData)
data = tmpData
if atEOF && len(data) == 0 {
return 0, nil, nil
} else if atEOF && !isWhitespace(data) {
return len(data), data, bufio.ErrFinalToken
}
switch {
case bytes.HasPrefix(data, tagStart):
end := bytes.Index(data, tagEnd)
if end == -1 {
return 0, nil, nil
}
token := data[:end+1]
next := bytes.Index(data, tagStart)
if next == -1 {
return 0, nil, nil
} else if isWhitespace(data[end+1 : end+1+next]) {
return len(token) + diff + next, token, nil
}
return len(token) + diff, token, nil
default:
end := bytes.Index(data, tagStart)
if end == -1 {
return 0, nil, nil
}
token := data[:end]
return len(token) + diff, bytes.TrimSpace(token), nil
}
return 0, nil, fmt.Errorf("Not start of tag")
}
func isWhitespace(data []byte) bool {
for _, c := range bytes.Runes(data) {
if !unicode.IsSpace(c) {
return false
}
}
return true
}
func checkIfLast(data []byte) (int, error) {
ind := bytes.Index(data, []byte{'<'})
if ind == -1 {
return ind, bufio.ErrFinalToken
}
return ind, nil
}