File: bom.go

package info (click to toggle)
golang-github-ssor-bom 0.0~git20170718.0.6386211-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 64 kB
  • sloc: makefile: 2
file content (34 lines) | stat: -rw-r--r-- 640 bytes parent folder | download | duplicates (2)
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
package bom

import (
	"bytes"
	"io"
	"io/ioutil"
)

const (
	bom0 = 0xef
	bom1 = 0xbb
	bom2 = 0xbf
)

// CleanBom 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 CleanBom(b []byte) []byte {
	if len(b) >= 3 &&
		b[0] == bom0 &&
		b[1] == bom1 &&
		b[2] == bom2 {
		return b[3:]
	}
	return b
}

// NewReaderWithoutBom returns an io.Reader that will skip over initial UTF-8 byte order marks.
func NewReaderWithoutBom(r io.Reader) (io.Reader, error) {
	bs, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}
	return bytes.NewReader(CleanBom(bs)), nil
}