-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_literal_encoder.go
57 lines (48 loc) · 1.02 KB
/
string_literal_encoder.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
package bindata
import (
"encoding/hex"
"io"
)
type stringLiteralEncoder interface {
io.Writer
}
type hexStringLiteralEncoder struct {
writer io.Writer
shouldWriteTrailingByte bool
}
func newStringLiteralEncoder(writer io.Writer) stringLiteralEncoder {
return hex.NewEncoder(&hexStringLiteralEncoder{
writer: writer,
shouldWriteTrailingByte: false,
})
}
func (e *hexStringLiteralEncoder) Write(p []byte) (int, error) {
written := 0
if e.shouldWriteTrailingByte {
_, err := e.writer.Write([]byte{p[0]})
if err != nil {
return written, err
}
p = p[1:]
written++
}
idx := 0
for ; idx < len(p)-1; idx += 2 {
_, err := e.writer.Write([]byte{'\\', 'x', p[idx], p[idx+1]})
if err != nil {
return written, err
}
written += 2
}
if idx < len(p) {
_, err := e.writer.Write([]byte{'\\', 'x', p[idx]})
if err != nil {
return written, err
}
written++
e.shouldWriteTrailingByte = true
} else {
e.shouldWriteTrailingByte = false
}
return written, nil
}