-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunebuffer.go
More file actions
69 lines (59 loc) · 1.53 KB
/
runebuffer.go
File metadata and controls
69 lines (59 loc) · 1.53 KB
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 objregexp
import (
"unicode/utf8"
)
type runeBufferT struct {
input string
pos int
runeErrorCb func()
}
func (s *runeBufferT) Initialize(input string) {
s.input = input
s.pos = 0
}
// Return ok, rune, eof, and advances the pointer
// If not ok, this function calls runeErrorCb, via _peekNextRune
func (s *runeBufferT) getNextRune() (bool, rune, bool) {
ok, r, size, eof := s._peekNextRune(true)
if !ok {
return false, 0, false
}
if ok && !eof {
s.pos += size
}
return ok, r, eof
}
// Return ok, rune, eof, but does not advance the pointer
// on error, this does *not* call runeErrorCb.
func (s *runeBufferT) peekNextRune() (bool, rune, bool) {
ok, r, _, eof := s._peekNextRune(false)
return ok, r, eof
}
// Advances the pointer by 1 rune
// If not ok, this function calls runeErrorCb, via _peekNextRune
func (s *runeBufferT) consumeNextRune() (bool, bool) {
ok, _, size, eof := s._peekNextRune(true)
if ok && !eof {
s.pos += size
}
return ok, eof
}
// Internal helper for get/peek/consume- NextRune()
// If not ok, this function calls runeErrorCb
func (s *runeBufferT) _peekNextRune(useCb bool) (bool, rune, int, bool) {
// EOS?
if s.pos == len(s.input) {
return true, utf8.RuneError, 0, true
}
r, size := utf8.DecodeRuneInString(s.input[s.pos:])
if r == utf8.RuneError {
if useCb && s.runeErrorCb != nil {
s.runeErrorCb()
}
return false, utf8.RuneError, size, false
}
return true, r, size, false
}
func (s *runeBufferT) getStringSlice(start int, end int) string {
return string(s.input[start:end])
}