Documentation
¶
Overview ¶
Package encsniff detects common non-UTF-8 text encodings from the head of a file or byte slice, using only byte-perfect signatures — no heuristics.
It returns one of four actions: UseAsIs (clean UTF-8/ASCII), StripBom (UTF-8 BOM that should be silently skipped), Warn (a named non-UTF-8 encoding the caller should surface along with an iconv hint), or WarnUnknown (bytes that are provably not UTF-8, encoding unnameable).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IconvCommand ¶
IconvCommand composes an iconv command that converts path from enc to UTF-8, writing to a sibling file with .utf8 inserted before the extension. It returns the empty string for encodings that need no conversion.
func IconvGuessCommand ¶ added in v0.2.0
IconvGuessCommand composes a suggested conversion for a file that is provably not UTF-8 but whose encoding could not be named.
Deliberately worded as something to try rather than as a claim. Every other hint this package produces follows a byte-perfect signature and can assert what the file is; this one follows only the absence of valid UTF-8, and the two candidates it names are the common cases, not a detection.
Types ¶
type Action ¶
type Action int
Action is what the caller should do with the file.
const ( // UseAsIs means the input looks like UTF-8 or ASCII; proceed unchanged. UseAsIs Action = iota // StripBom means the input is UTF-8 with a leading BOM; skip BomLen bytes. StripBom // Warn means the input is a non-UTF-8 encoding the user should know about. Warn // WarnUnknown means the input is provably not UTF-8, but which encoding it // is could not be determined. Encoding is empty — nothing was proven about // the identity, only about what it is not. // // Naming a single-byte encoding would be a guess, and this package does not // guess. Saying the bytes are not UTF-8 is not a guess: UTF-8 is a decidable // grammar, and this decides it exactly. WarnUnknown )
type Sniff ¶
type Sniff struct {
// Action tells the caller what to do.
Action Action
// Encoding is the detected encoding (empty if UseAsIs and no BOM).
Encoding Encoding
// BomLen is the number of leading bytes to skip when Action == StripBom.
BomLen int
// Hint is a copy-pasteable iconv command. Set by SniffFile on either warning
// action; empty when sniffing bytes with no associated path.
Hint string
}
Sniff is the result of a detection pass.
func SniffBytes ¶
SniffBytes inspects the head of b and returns the detected action. The Hint field is left empty; callers with a path should use SniffFile, or compose a hint with IconvCommand.
func SniffFile ¶
SniffFile opens path, sniffs the head, and returns the result with Hint set to a copy-pasteable iconv command when Action == Warn.
func (Sniff) IsWarning ¶ added in v0.2.0
IsWarning reports whether this result is something the user should be told about.
Prefer this to comparing against Warn: every consumer in the fleet wrote `s.Action != encsniff.Warn` to mean "nothing to report", and in Go a new constant does not break that — it is silently ignored, which is worse than a compile error. A predicate absorbs the next verdict too.