-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.go
55 lines (43 loc) · 889 Bytes
/
parser.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
package lib
import (
"bufio"
"fmt"
"strconv"
"strings"
)
type InfoParser struct {
*bufio.Reader
}
func NewInfoParser(s string) *InfoParser {
return &InfoParser{bufio.NewReader(strings.NewReader(s))}
}
func (ip *InfoParser) Expect(s string) error {
buf := make([]byte, len(s))
if _, err := ip.Read(buf); err != nil {
return err
}
if sbuf := string(buf); sbuf != s {
return fmt.Errorf("expected value %q found %q", s, sbuf)
}
return nil
}
func (ip *InfoParser) ReadUntil(delim byte) (string, error) {
v, err := ip.ReadBytes(delim)
switch len(v) {
case 0:
return string(v), err
case 1:
if v[0] == delim {
return "", err
}
return string(v), err
}
return string(v[:len(v)-1]), err
}
func (ip *InfoParser) ReadFloat(delim byte) (float64, error) {
s, err := ip.ReadUntil(delim)
if err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}