-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
112 lines (95 loc) · 1.83 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
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
package tdmarkup
import (
"fmt"
"strings"
)
const EOF = rune(0)
func NewScanner(text string) *Scanner {
return &Scanner{runes: []rune(text)}
}
type Scanner struct {
runes []rune
pos int
}
// Current position
func (s Scanner) Position() int { return s.pos }
// Length in runes
func (s Scanner) Length() int { return len(s.runes) }
// Change current position
func (s *Scanner) Rewind(i int) {
if i < 0 || i > s.Length() {
panic(fmt.Errorf("index must be between 0 and %d, %d given", s.Length(), i))
}
s.pos = i
}
// Diff
func (s Scanner) Since(i int) int {
if s.pos < i {
panic(fmt.Errorf("index must be between less than %d, %d given", s.pos, i))
}
return s.pos - i
}
// Number of runes
func (s Scanner) Rest() int {
return s.Length() - s.pos
}
// Previous rune
func (s Scanner) Prev() rune {
if s.pos < 2 {
return EOF
}
return s.runes[s.pos-2]
}
// Current rune: FIXME: strange logic, but it works
func (s Scanner) Current() rune {
if s.pos < 1 {
return EOF
}
return s.runes[s.pos-1]
}
// Returns next rune without rewind
func (s Scanner) Next() rune {
if s.pos >= s.Length() {
return EOF
}
return s.runes[s.pos]
}
// Returns next rune and rewind current position
func (s *Scanner) TakeNext() rune {
ch := s.Next()
if ch != EOF {
s.pos++
}
return ch
}
func (s *MarkupScanner) ScanUntil(cl []rune) string {
start := s.Position()
var b strings.Builder
//b.Grow(s.Length() - start) // slower!
switch len(cl) {
case 0:
panic("invalid close operator length: 0")
case 1:
r := cl[0]
for s.Rest() > 0 {
b.WriteRune(s.TakeNext())
if s.Current() == r {
return b.String()
}
}
s.Rewind(start)
return ""
default:
if s.Rest() < len(cl) {
return ""
}
for _, r := range cl {
if s.Next() != r {
s.Rewind(start)
return ""
}
b.WriteRune(s.TakeNext())
}
}
return b.String()
}