Documentation
¶
Index ¶
- Constants
- func BOMBytesFor(charset string) []byte
- func BOMSize(charset string) int
- func Canonical(name string) (string, bool)
- func Count() int
- func Decode(data []byte, charset string) (string, error)
- func DetectionCandidates() []string
- func Encode(content string, enc encoding.Encoding, charset string) ([]byte, error)
- func FormatCandidates(ranked []Candidate) string
- func Get(name string) (encoding.Encoding, bool)
- func IsUTF8(name string) bool
- func SameCharset(a, b string) bool
- func SetDetectionCandidates(names []string) error
- type Candidate
- type DetectionResult
- type EncodingListItem
- type UnsupportedError
- type UnsupportedRune
Constants ¶
const ( ChunkSize = 128 * 1024 // 128KB chunks for detection SmallFileThreshold = 128 * 1024 // Files smaller than this are read entirely HighConfidenceThreshold = 80 // Confidence level to stop sampling early MinConfidenceThreshold = 50 // Minimum confidence to trust detection )
Variables ¶
This section is empty.
Functions ¶
func BOMBytesFor ¶
BOMBytesFor returns the BOM bytes for charset, or nil if unsupported.
func DetectionCandidates ¶
func DetectionCandidates() []string
DetectionCandidates returns the pinned set, nil when detection is unrestricted.
func FormatCandidates ¶
FormatCandidates renders a ranked list as "windows-1251 (62%), koi8-r (55%)".
func SameCharset ¶
SameCharset compares through the registry, so "ascii" and "utf-8" are one answer.
func SetDetectionCandidates ¶
SetDetectionCandidates restricts detection to these encodings; an empty list clears the restriction.
Types ¶
type Candidate ¶
type Candidate struct {
Charset string
Confidence int
Supported bool // in the registry, so usable as an encoding parameter
}
Candidate is one guess at what the bytes are, with the detector's confidence.
func CandidatesFromFile ¶
CandidatesFromFile ranks over the bytes the detector saw: all in "full" mode, samples otherwise.
func CandidatesFromSample ¶
CandidatesFromSample ranks over the samples DetectSample reads: one bounded pass.
func SupportedAlternatives ¶
SupportedAlternatives keeps what a caller could retry with: in the registry, and not `exclude`.
type DetectionResult ¶
func Detect ¶
func Detect(data []byte) DetectionResult
func DetectBOM ¶
func DetectBOM(data []byte) (DetectionResult, bool)
DetectBOM checks for Unicode BOMs; UTF-32 goes before UTF-16 (shared prefixes).
func DetectFromFile ¶
func DetectFromFile(path string, mode string) (DetectionResult, error)
DetectFromFile detects encoding via streaming I/O; modes: sample (~384KB), chunked, full.
func DetectSample ¶
func DetectSample(data []byte) (DetectionResult, bool)
DetectSample samples beginning, middle and end; reports whether to trust it. TODO: make private once grep and convert_encoding stream instead of buffering.
func (DetectionResult) Conclusive ¶
func (d DetectionResult) Conclusive() bool
Conclusive reports whether the result settles which encoding to use. "ascii" never does: it fits every encoding here.
type EncodingListItem ¶
type EncodingListItem struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Aliases []string `json:"aliases"`
Description string `json:"description"`
}
func ListEncodings ¶
func ListEncodings() []EncodingListItem
type UnsupportedError ¶
type UnsupportedError struct {
Charset string
Runes []UnsupportedRune // capped at maxReportedRunes
Total int // may exceed len(Runes)
}
UnsupportedError says which characters charset cannot represent, and where.
func (*UnsupportedError) Error ¶
func (e *UnsupportedError) Error() string