-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx_token_reader.go
94 lines (83 loc) · 1.54 KB
/
xlsx_token_reader.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
package xlsxcfg
import (
"bytes"
"strconv"
)
type tokenReader struct {
buf []byte
idx int
curr []byte
listIdx int
err error
}
func newTokenReader(src string) *tokenReader {
return &tokenReader{buf: []byte(src)}
}
func (r *tokenReader) HasNext() bool {
return r.idx < len(r.buf)
}
func (r *tokenReader) Next() bool {
r.curr = nil
r.listIdx = -1
if r.idx >= len(r.buf) {
return false
}
leftBuf := r.buf[r.idx:]
i := bytes.IndexByte(leftBuf, '.')
if i > 0 {
r.curr = leftBuf[:i]
r.idx += i + 1
} else {
r.curr = leftBuf[:]
r.idx = len(r.buf)
}
i = bytes.IndexByte(r.curr, '#')
if i > 0 {
idxBuf := r.curr[i+1:]
r.curr = r.curr[:i]
n, e := strconv.ParseInt(string(idxBuf), 10, 64)
if e != nil {
r.err = e
} else {
r.listIdx = int(n)
}
}
return true
}
func (r *tokenReader) Ident() string {
return string(r.curr)
}
func (r *tokenReader) ListIndex() int {
return r.listIdx
}
func (r *tokenReader) FullPrev() string {
i := bytes.LastIndex(r.buf[:r.idx], r.curr)
if i <= 0 {
return ""
}
return string(r.buf[:i-1])
}
func (r *tokenReader) FullIdent() string {
return string(bytes.TrimSuffix(r.buf[:r.idx], []byte{'.'}))
}
func (r *tokenReader) TypePath() string {
buf := make([]byte, r.idx+1)
copy(buf, r.buf[:r.idx])
for i := 0; i < len(buf); i++ {
if buf[i] != '#' {
continue
}
j := i + 1
for ; j < len(buf); j++ {
if buf[j] == '.' {
break
}
}
if j < len(buf) {
buf = append(buf[:i], buf[j:]...)
} else {
buf = buf[:i]
}
}
return string(bytes.Trim(buf, "\x00"))
}