bom.go 640 B

12345678910111213141516171819202122232425262728293031323334
  1. package bom
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. )
  7. const (
  8. bom0 = 0xef
  9. bom1 = 0xbb
  10. bom2 = 0xbf
  11. )
  12. // CleanBom returns b with the 3 byte BOM stripped off the front if it is present.
  13. // If the BOM is not present, then b is returned.
  14. func CleanBom(b []byte) []byte {
  15. if len(b) >= 3 &&
  16. b[0] == bom0 &&
  17. b[1] == bom1 &&
  18. b[2] == bom2 {
  19. return b[3:]
  20. }
  21. return b
  22. }
  23. // NewReaderWithoutBom returns an io.Reader that will skip over initial UTF-8 byte order marks.
  24. func NewReaderWithoutBom(r io.Reader) (io.Reader, error) {
  25. bs, err := ioutil.ReadAll(r)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return bytes.NewReader(CleanBom(bs)), nil
  30. }