-
Notifications
You must be signed in to change notification settings - Fork 2
/
scan.go
63 lines (53 loc) · 1.08 KB
/
scan.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
package semver
import "unicode"
// An extremely simple tokenizing helper that only handles ASCII strings.
type simpleASCIIScanner struct {
source string
length int
pos int
}
const (
scannerEOF int8 = -1
scannerNonASCII int8 = -2
)
func noTerminator(rune) bool {
return false
}
func newSimpleASCIIScanner(source string) simpleASCIIScanner {
return simpleASCIIScanner{source: source, length: len(source)}
}
func (s *simpleASCIIScanner) eof() bool {
return s.pos >= s.length
}
func (s *simpleASCIIScanner) peek() int8 {
if s.pos >= s.length {
return scannerEOF
}
var ch uint8 = s.source[s.pos]
if ch == 0 || ch > unicode.MaxASCII {
return scannerNonASCII
}
return int8(ch)
}
func (s *simpleASCIIScanner) next() int8 {
ch := s.peek()
if ch > 0 {
s.pos++
}
return ch
}
func (s *simpleASCIIScanner) readUntil(terminatorFn func(rune) bool) (substring string, terminatedBy int8) {
startPos := s.pos
var ch int8
for {
ch = s.next()
if ch < 0 || terminatorFn(rune(ch)) {
break
}
}
endPos := s.pos
if ch > 0 {
endPos--
}
return s.source[startPos:endPos], ch
}