Created
June 29, 2023 06:27
-
-
Save yukithm/b79961622bc05af1dcc961ff33ba8a85 to your computer and use it in GitHub Desktop.
BOM aware Unicode Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bomreader | |
import ( | |
"bufio" | |
"bytes" | |
"io" | |
"golang.org/x/text/encoding/unicode" | |
"golang.org/x/text/transform" | |
) | |
type Encoding interface { | |
String() string | |
BOM() []byte | |
} | |
type encoding struct { | |
name string | |
bom []byte | |
} | |
func (e encoding) String() string { | |
return e.name | |
} | |
func (e encoding) BOM() []byte { | |
return e.bom | |
} | |
var ( | |
UTF8 = encoding{name: "UTF-8", bom: []byte{0xef, 0xbb, 0xbf}} | |
UTF16BE = encoding{name: "UTF-16BE", bom: []byte{0xfe, 0xff}} | |
UTF16LE = encoding{name: "UTF-16LE", bom: []byte{0xff, 0xfe}} | |
NoBOM = encoding{name: "No BOM", bom: []byte{}} | |
) | |
func NewBomReader(r io.Reader) (io.Reader, Encoding, error) { | |
br := bufio.NewReader(r) | |
mark, err := br.Peek(3) | |
if err != nil { | |
return nil, nil, err | |
} | |
if bytes.HasPrefix(mark, UTF8.bom) { | |
return transform.NewReader(br, unicode.UTF8BOM.NewDecoder()), UTF8, nil | |
} else if bytes.HasPrefix(mark, UTF16BE.bom) { | |
return transform.NewReader(br, unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewDecoder()), UTF16BE, nil | |
} else if bytes.HasPrefix(mark, UTF16LE.bom) { | |
return transform.NewReader(br, unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()), UTF16LE, nil | |
} | |
return br, NoBOM, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A better solution:
See: https://pkg.go.dev/golang.org/x/text/encoding/unicode#BOMOverride
A complete example: