-
Notifications
You must be signed in to change notification settings - Fork 13
/
writer.go
108 lines (83 loc) · 1.72 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package mahonia
import (
"io"
"unicode/utf8"
)
// Writer implements character-set encoding for an io.Writer object.
type Writer struct {
wr io.Writer
encode Encoder
inbuf []byte
outbuf []byte
}
// NewWriter creates a new Writer that uses the receiver to encode text.
func (e Encoder) NewWriter(wr io.Writer) *Writer {
w := new(Writer)
w.wr = wr
w.encode = e
return w
}
// Write encodes and writes the data from p.
func (w *Writer) Write(p []byte) (n int, err error) {
n = len(p)
if len(w.inbuf) > 0 {
w.inbuf = append(w.inbuf, p...)
p = w.inbuf
}
if len(w.outbuf) < len(p) {
w.outbuf = make([]byte, len(p)+10)
}
outpos := 0
for len(p) > 0 {
rune, size := utf8.DecodeRune(p)
if rune == 0xfffd && !utf8.FullRune(p) {
break
}
p = p[size:]
retry:
size, status := w.encode(w.outbuf[outpos:], rune)
if status == NO_ROOM {
newDest := make([]byte, len(w.outbuf)*2)
copy(newDest, w.outbuf)
w.outbuf = newDest
goto retry
}
if status == STATE_ONLY {
outpos += size
goto retry
}
outpos += size
}
w.inbuf = w.inbuf[:0]
if len(p) > 0 {
w.inbuf = append(w.inbuf, p...)
}
n1, err := w.wr.Write(w.outbuf[0:outpos])
if err != nil && n1 < n {
n = n1
}
return
}
func (w *Writer) WriteRune(c rune) (size int, err error) {
if len(w.inbuf) > 0 {
// There are leftover bytes, a partial UTF-8 sequence.
w.inbuf = w.inbuf[:0]
w.WriteRune(0xfffd)
}
if w.outbuf == nil {
w.outbuf = make([]byte, 16)
}
outpos := 0
retry:
size, status := w.encode(w.outbuf[outpos:], c)
if status == NO_ROOM {
w.outbuf = make([]byte, len(w.outbuf)*2)
goto retry
}
if status == STATE_ONLY {
outpos += size
goto retry
}
outpos += size
return w.wr.Write(w.outbuf[0:outpos])
}