xmlenc

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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

func NewReader(r io.Reader, res Result) *Reader

NewReader returns a reader of r, which starts after the bytes Sniff said to skip, that gives UTF-8.

func (*Reader) Invalid

func (d *Reader) Invalid() int

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

func (d *Reader) Map(src io.ReaderAt, base int64, offs []int64) error

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.

func (*Reader) Read

func (d *Reader) Read(p []byte) (int, error)

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

func Sniff(sample []byte) (Result, error)

Sniff decides the encoding from sample, the first SampleSize bytes of the input (all of it if shorter).

func SniffAs

func SniffAs(sample []byte, enc charset.Encoding) (Result, error)

SniffAs is Sniff with the encoding given from outside the input: enc is used whatever the XML declaration names or the bytes suggest. A byte order mark still wins (as go-fast-html's Charset), and a UTF-32 one is refused.

func (Result) Name

func (r Result) Name() string

Name returns the name of the encoding.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL