-
Notifications
You must be signed in to change notification settings - Fork 20
/
input.go
44 lines (37 loc) · 854 Bytes
/
input.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
package excellent
import (
"bufio"
)
const eof rune = rune(0)
// provides functionality for reading and unreading from a buffered source
type xinput struct {
base *bufio.Reader
unreadRunes []rune
unreadCount int
}
func newInput(base *bufio.Reader) *xinput {
return &xinput{
base: base,
unreadRunes: make([]rune, 4),
}
}
// gets the next rune or EOF if we are at the end of the string
func (r *xinput) read() rune {
// first see if we have any unread runes to return
if r.unreadCount > 0 {
ch := r.unreadRunes[r.unreadCount-1]
r.unreadCount--
return ch
}
// otherwise, read the next run
ch, _, err := r.base.ReadRune()
if err != nil {
return eof
}
return ch
}
// pops the passed in rune as the next rune to be returned
func (r *xinput) unread(ch rune) {
r.unreadRunes[r.unreadCount] = ch
r.unreadCount++
}