-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.go
61 lines (52 loc) · 1.03 KB
/
decode.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
package base256
import (
"bufio"
"io"
)
// DecodeString returns the bytes represented by the base256 string s.
// Non-base256 runes are skipped silently.
func DecodeString(s string) []byte {
src := []rune(s)
dst := []byte{}
for i := 0; i < len(src); i++ {
decoded, ok := dectab[src[i]]
if !ok {
continue
}
dst = append(dst, decoded)
}
return dst
}
type decoder struct {
reader *bufio.Reader
err error
}
// NewDecoder constructs a new base256 stream decoder. Data read from the
// returned reader is base256 decoded from r.
// Non-base256 runes are skipped silently.
func NewDecoder(r io.Reader) io.Reader {
return &decoder{
reader: bufio.NewReader(r),
}
}
func (d *decoder) Read(c []byte) (int, error) {
if d.err != nil {
return 0, d.err
}
// the least count of runes to read is the length of c
var i int
for i = 0; i < len(c); {
r, _, err := d.reader.ReadRune()
if err != nil {
d.err = err
return i, err
}
decoded, ok := dectab[r]
if !ok {
continue
}
c[i] = decoded
i++
}
return i, nil
}