-
Notifications
You must be signed in to change notification settings - Fork 47
/
reader.go
67 lines (57 loc) · 1.29 KB
/
reader.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
package docx
import "io"
// Reader is a very basic io.Reader implementation which is capable of returning the current position.
type Reader struct {
str string
i int64
length int64
prevRune int64 // index of the previously read rune or -1
}
// NewReader returns a new Reader given a string source.
func NewReader(s string) *Reader {
return &Reader{
str: s,
i: 0,
length: int64(len(s)),
prevRune: -1,
}
}
// String implements the Stringer interface.
func (r *Reader) String() string {
return r.str
}
// Len returns the current length of the stream which has been read.
func (r *Reader) Len() int {
if r.i >= r.length {
return 0
}
return int(r.length - r.i)
}
// Size returns the size of the string to read.
func (r *Reader) Size() int64 {
return r.length
}
// Pos returns the current position which the reader is at.
func (r *Reader) Pos() int64 {
return r.i
}
// Read implements the io.Reader interface.
func (r *Reader) Read(b []byte) (int, error) {
if r.i >= r.length {
return 0, io.EOF
}
r.prevRune = -1
b[0] = r.str[r.i]
r.i += 1
return 1, nil
}
// ReadByte implements hte io.ByteReader interface.
func (r *Reader) ReadByte() (byte, error) {
r.prevRune = -1
if r.i >= int64(len(r.str)) {
return 0, io.EOF
}
b := r.str[r.i]
r.i++
return b, nil
}