-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (46 loc) · 984 Bytes
/
util.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
package meep
import (
"bufio"
"io"
)
var (
tab = []byte{'\t'}
br = []byte{'\n'}
)
func indenter(w io.Writer) io.Writer {
return &rediscipliner{
edgeDfn: bufio.ScanLines,
wr: w,
prefix: tab,
suffix: br,
}
}
var _ io.Writer = &rediscipliner{}
type rediscipliner struct {
edgeDfn bufio.SplitFunc
wr io.Writer
prefix []byte
suffix []byte
rem []byte
}
func (red *rediscipliner) Write(b []byte) (int, error) {
red.rem = append(red.rem, b...)
n := 0
//fmt.Printf("\n>>>>>\n")
for len(red.rem) > 0 { // if loop until the buffer is exhausted, or another cond breaks out
adv, tok, err := red.edgeDfn(red.rem, false)
//fmt.Printf(">>>\n\tbuf: %q\n\ttok: %q\n\tadv: %d\n", red.rem, tok, adv)
if err != nil {
return n, err
}
if adv == 0 { // when we no longer have a full chunk, return
return n, nil
}
red.wr.Write(red.prefix)
red.wr.Write(tok)
red.wr.Write(red.suffix)
n += adv
red.rem = red.rem[adv:]
}
return -1, nil
}