-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.go
66 lines (57 loc) · 905 Bytes
/
writer.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
package mustache
import (
"bufio"
"bytes"
"io"
)
type writer struct {
hasText bool
hasTag bool
w io.Writer
b *bufio.Writer
}
func newWriter(w io.Writer) *writer {
return &writer{
hasText: false,
hasTag: false,
w: w,
b: bufio.NewWriter(w),
}
}
func (w *writer) text() {
w.hasText = true
}
func (w *writer) tag() {
w.hasTag = true
}
func (w *writer) reset() {
w.hasTag = false
w.hasText = false
}
func (w *writer) flush() error {
defer w.reset()
if w.hasTag && !w.hasText {
w.b.Reset(w.w)
return nil
}
return w.b.Flush()
}
func (w *writer) write(r rune) error {
_, err := w.b.WriteRune(r)
if err != nil {
return err
}
if r == '\n' {
return w.flush()
}
return nil
}
func (w *writer) Write(b []byte) (int, error) {
for i, r := range bytes.Runes(b) {
err := w.write(r)
if err != nil {
return i, err
}
}
return len(b), nil
}