-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringParser.go
65 lines (58 loc) · 1.29 KB
/
stringParser.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
package bencode
import (
"io"
"strconv"
"strings"
)
// A bencode parser that uses a string as source
type stringParser struct {
bencode string
i int
}
// Returns the next byte
func (sp *stringParser) readByte() (byte, error) {
if sp.i >= len(sp.bencode) {
return 0, io.EOF
}
sp.i++
return sp.bencode[sp.i-1], nil
}
// Backtracks by 1 byte
//
// MUST only be called at most once immediately following a call to nextByte()
func (sp *stringParser) undoReadByte() {
sp.i--
if sp.i < 0 {
panic("stringParser: invalid undoReadByte")
}
}
// Reads a number until an end byte
func (sp *stringParser) readIntTo(separator byte) (int, error) {
i := sp.i
if i >= len(sp.bencode) {
return 0, io.ErrUnexpectedEOF
}
index := strings.IndexByte(sp.bencode[i:], separator)
if index == -1 {
return 0, io.ErrUnexpectedEOF
}
sp.i += index + 1
return strconv.Atoi(sp.bencode[i : i+index])
}
// Returns a string of a given length
func (sp *stringParser) readString(length int) (string, error) {
sp.i += length
if sp.i > len(sp.bencode) {
return "", io.ErrUnexpectedEOF
}
return sp.bencode[sp.i-length : sp.i], nil
}
// Returns a bencode decoder from a given string
func NewParserFromString(bencode string) *decoder {
return &decoder{
&stringParser{
bencode: bencode,
i: 0,
},
}
}