-
Notifications
You must be signed in to change notification settings - Fork 11
/
stringScanner.go
75 lines (64 loc) · 1.12 KB
/
stringScanner.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
package decimal
import (
"bytes"
"fmt"
"io"
"unicode"
)
type runeScanner interface {
io.Reader
io.RuneScanner
}
type scanner struct {
reader runeScanner
}
var _ fmt.ScanState = (*scanner)(nil)
func (s *scanner) ReadRune() (r rune, size int, err error) {
return s.reader.ReadRune()
}
func (s *scanner) UnreadRune() error {
return s.reader.UnreadRune()
}
func (s *scanner) SkipSpace() {
for {
ch, _, err := s.ReadRune()
if err != nil {
break
}
if !unicode.IsSpace(ch) {
if err := s.UnreadRune(); err != nil {
panic("s.UnreadRune() failed")
}
break
}
}
}
func (s *scanner) Token(skipSpace bool, f func(rune) bool) (token []byte, err error) {
if skipSpace {
s.SkipSpace()
}
var buf bytes.Buffer
for {
r, _, err := s.ReadRune()
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
if !f(r) {
if err := s.UnreadRune(); err != nil {
return nil, err
}
break
}
buf.WriteRune(r)
}
return buf.Bytes(), nil
}
func (s *scanner) Width() (wid int, ok bool) {
return 0, false
}
func (s *scanner) Read(buf []byte) (n int, err error) {
return s.reader.Read(buf)
}