Documentation
¶
Overview ¶
Package xmlenc decides the character encoding of an XML document and decodes it to UTF-8 as a stream.
The decision follows the XML 1.0 specification (section 4.3.3 and appendix F): a byte order mark, then the first bytes of the XML declaration where they show UTF-16 without a mark, then the encoding the declaration names, then UTF-8. A document that declares no encoding and is not UTF-8 is read in the encoding recognised from its bytes where one is certain (go-fast-html's detection), and refused otherwise.
Index ¶
Constants ¶
const SampleSize = 1 << 20
SampleSize is how much of the start of the input Sniff wants: the XML declaration, and enough text to tell UTF-8 or recognise an encoding.
Variables ¶
var ErrUnsupported = errors.New("encoding not read")
ErrUnsupported is returned, wrapped, for an encoding that is not read.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader decodes a stream to UTF-8. Input read as UTF-8 is passed through as it is.
func NewReader ¶
NewReader returns a reader of r, which starts after the bytes Sniff said to skip, that gives UTF-8.
func (*Reader) Invalid ¶
Invalid returns the byte sequences so far that were not a character in the encoding and became U+FFFD. Invalid UTF-8 passed through is not counted here.
func (*Reader) Map ¶
Map replaces offsets in the decoded text, sorted in increasing order, by the offsets in the source of the characters that start at or cover them; an offset at the end of what was decoded gives the end of the source read. src is the source, the text starting at offset base. Only what is needed is read again, a decoded chunk at a time, in one pass. Input read as UTF-8 needs no map: its offsets are kept.
type Result ¶
type Result struct {
// Enc is the encoding.
Enc charset.Encoding
// UTF8 reports that the input is read as UTF-8: the bytes are used as
// they are, and offsets in the text are offsets in the file.
UTF8 bool
// Skip is the bytes at the start that are not text (a byte order
// mark).
Skip int
// Declared is the encoding the XML declaration names, lowercased and
// held to 64 bytes, or empty.
Declared string
// Detected reports that nothing declared the encoding and the input is
// not UTF-8: Enc was recognised from the bytes.
Detected bool
}
Result is the decision.
func Sniff ¶
Sniff decides the encoding from sample, the first SampleSize bytes of the input (all of it if shorter).