-
Notifications
You must be signed in to change notification settings - Fork 1
/
normalizer.go
215 lines (189 loc) · 5.25 KB
/
normalizer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package gitserver
import (
"bufio"
"bytes"
"io"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/saintfish/chardet"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/encoding/unicode/utf32"
"golang.org/x/text/transform"
)
var (
// UTF8BOM is the UTF-8 Byte order mark.
UTF8BOM = []byte{0xEF, 0xBB, 0xBF}
// UTF16LEBOM is the UTF-16 (LE) Byte order mark.
UTF16LEBOM = []byte{0xFF, 0xFE}
// UTF16BEBOM is the UTF-16 (BE) Byte order mark.
UTF16BEBOM = []byte{0xFE, 0xFF}
// UTF32LEBOM is the UTF-16 (LE) Byte order mark.
UTF32LEBOM = []byte{0xFF, 0xFE, 0x00, 0x00}
// UTF32BEBOM is the UTF-16 (BE) Byte order mark.
UTF32BEBOM = []byte{0x00, 0x00, 0xFE, 0xFF}
)
func removeBOM(r io.Reader) (io.Reader, bool, error) {
br := bufio.NewReader(r)
bom, err := br.Peek(utf8.UTFMax)
if err != nil && err != io.EOF {
return nil, false, errors.Wrap(
err,
"failed to inspect the first few bytes",
)
}
if bytes.HasPrefix(bom, UTF32LEBOM) {
return transform.NewReader(
br,
utf32.UTF32(utf32.LittleEndian, utf32.UseBOM).NewDecoder(),
), true, nil
} else if bytes.HasPrefix(bom, UTF32BEBOM) {
return transform.NewReader(
br,
utf32.UTF32(utf32.BigEndian, utf32.UseBOM).NewDecoder(),
), true, nil
} else if bytes.HasPrefix(bom, UTF16LEBOM) {
return transform.NewReader(
br,
unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder(),
), true, nil
} else if bytes.HasPrefix(bom, UTF16BEBOM) {
return transform.NewReader(
br,
unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewDecoder(),
), true, nil
} else if bytes.HasPrefix(bom, UTF8BOM) {
if _, err := br.Discard(len(UTF8BOM)); err != nil {
return nil, false, errors.Wrap(
err,
"failed to consume the UTF-8 byte order mark",
)
}
return br, true, nil
}
return br, false, nil
}
// ConvertMarkdownToUTF8 performs a best-effort detection of the encoding of
// the supplied reader and returns a Reader that is UTF-8 encoded.
func ConvertMarkdownToUTF8(r io.Reader) (io.Reader, error) {
br, removed, err := removeBOM(r)
if err != nil {
// removeBOM already wrapped the error correctly.
return nil, err
}
if removed {
return NewLineEndingNormalizer(br), nil
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, br); err != nil {
return nil, err
}
bytesReader := bytes.NewReader(buf.Bytes())
// Is it already valid UTF-8?
if utf8.Valid(buf.Bytes()) {
return NewLineEndingNormalizer(bytesReader), nil
}
// There was no BOM and it wasn't valid UTF-8, so we'll need to detect the
// encoding in another way.
detector := chardet.NewTextDetector()
if result, err := detector.DetectBest(buf.Bytes()); err == nil {
enc, err := htmlindex.Get(result.Charset)
if err == nil {
return NewLineEndingNormalizer(
transform.NewReader(bytesReader, unicode.BOMOverride(enc.NewDecoder())),
), nil
}
}
return NewLineEndingNormalizer(bytesReader), nil
}
// NormalizeCase performs a best-effort conversion to UTF-8 and normalizes the
// end-of-line characters.
func NormalizeCase(r io.Reader) (io.Reader, error) {
br, _, err := removeBOM(r)
if err != nil {
// removeBOM already wrapped the error correctly.
return nil, err
}
return NewLineEndingNormalizer(br), nil
}
// LineEndingNormalizer is an io.Reader that trims trailing whitespace and converts line endings to \n.
type LineEndingNormalizer struct {
buf bytes.Buffer
outBuf bytes.Buffer
r io.RuneScanner
empty bool
endsWithNewline bool
eof bool
}
// NewLineEndingNormalizer returns a LineEndingNormalizer from the provided io.Reader.
func NewLineEndingNormalizer(rd io.Reader) *LineEndingNormalizer {
var br io.RuneScanner
var ok bool
if br, ok = rd.(io.RuneScanner); !ok {
br = bufio.NewReader(rd)
}
return &LineEndingNormalizer{
r: br,
empty: true,
}
}
// Read implements io.Reader.
func (n *LineEndingNormalizer) Read(p []byte) (int, error) {
if n.eof {
return 0, io.EOF
}
for n.outBuf.Len() == 0 {
r, _, err := n.r.ReadRune()
if err != nil {
if err == io.EOF {
n.eof = true
// Unix non-empty files always end with a newline character.
if !n.empty && !n.endsWithNewline {
return utf8.EncodeRune(p, '\n'), nil
}
}
return 0, err
}
n.empty = false
switch r {
case '\r':
// Treat CRLF or an unaccompanied CR as an LF
nextR, _, err := n.r.ReadRune()
if err == nil {
if nextR != '\n' {
n.r.UnreadRune()
}
} else if err != nil {
if err == io.EOF {
n.eof = true
// Unix files always end with a newline character.
if !n.endsWithNewline {
return utf8.EncodeRune(p, '\n'), nil
}
}
return 0, err
}
fallthrough
case '\n':
// Discard any contents in the buffer, effectively trimming trailing
// whitespace.
n.endsWithNewline = true
n.buf.Reset()
n.outBuf.WriteRune('\n')
case ' ', '\t', '\v', '\f', 0x85, 0xA0:
// Accumulate all whitespace characters in the buffer.
n.endsWithNewline = false
n.buf.WriteRune(r)
default:
// A printable character. Write the buffer, reset it, and then write
// the character.
n.endsWithNewline = false
if n.buf.Len() > 0 {
io.Copy(&n.outBuf, &n.buf)
n.buf.Reset()
}
n.outBuf.WriteRune(r)
}
}
return n.outBuf.Read(p)
}