io.go
917 字节
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
package interpol
import (
"bufio"
"io"
"unicode/utf8"
)
type runeWriter interface {
io.Writer
WriteRune(r rune) (int, error)
}
func templateReader(opts *Options) io.RuneReader {
if rr, ok := opts.Template.(io.RuneReader); ok {
return rr
}
return bufio.NewReaderSize(opts.Template, utf8.UTFMax)
}
func outputWriter(opts *Options) runeWriter {
if rw, ok := opts.Output.(runeWriter); ok {
return rw
}
return &simpleRuneWriter{w: opts.Output}
}
type simpleRuneWriter struct {
runeEncoder
w io.Writer
}
func (rw *simpleRuneWriter) Write(b []byte) (int, error) {
return rw.w.Write(b)
}
func (rw *simpleRuneWriter) WriteRune(r rune) (int, error) {
return rw.w.Write(rw.encode(r))
}
type runeEncoder struct {
b [utf8.UTFMax]byte
}
func (re *runeEncoder) encode(r rune) []byte {
if r < utf8.RuneSelf {
re.b[0] = byte(r)
return re.b[:1]
}
n := utf8.EncodeRune(re.b[:], r)
return re.b[:n]
}