blob: 93b811c6d91df47cc7dbfad7e461a4090772fadd (
plain)
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
|
// Package bom is used to clean up UTF-8 Byte Order Marks.
package bom
import (
"bufio"
"io"
)
const (
bom0 = 0xef
bom1 = 0xbb
bom2 = 0xbf
)
// Clean returns b with the 3 byte BOM stripped off the front if it is present.
// If the BOM is not present, then b is returned.
func Clean(b []byte) []byte {
if len(b) >= 3 &&
b[0] == bom0 &&
b[1] == bom1 &&
b[2] == bom2 {
return b[3:]
}
return b
}
// NewReader returns an io.Reader that will skip over initial UTF-8 byte order marks.
func NewReader(r io.Reader) io.Reader {
buf := bufio.NewReader(r)
b, err := buf.Peek(3)
if err != nil {
// not enough bytes
return buf
}
if b[0] == bom0 && b[1] == bom1 && b[2] == bom2 {
discardBytes(buf, 3)
}
return buf
}
|