EncodingRs. Detector
(encoding_rs v0.5.0)
Copy Markdown
Stateful streaming character encoding detector powered by Mozilla's
chardetng crate.
Use this module to guess the encoding of large files or network streams
incrementally without loading the entire input into memory. For complete
binaries already in memory, use EncodingRs.detect/2.
Usage
Detecting from a stream
{:ok, encoding} =
File.stream!("unknown.txt", [], 64 * 1024)
|> Stream.take(4)
|> EncodingRs.Detector.detect_stream(tld: "jp")Manual chunk feeding
detector = EncodingRs.Detector.new()
{:ok, _non_ascii?} = EncodingRs.Detector.feed(detector, chunk1, false)
{:ok, _non_ascii?} = EncodingRs.Detector.feed(detector, chunk2, true)
{:ok, encoding} = EncodingRs.Detector.guess(detector, tld: "jp")Important notes
- Leading UTF-8 and UTF-16 BOMs take precedence over heuristics unless
bom: falseis passed tonew/1ordetect_stream/2. - Pass
is_last: trueonly when the full stream ends. If you feed only a prefix of a larger file, passis_last: falseand callguess/2directly. - Calling
feed/3afteris_last: truereturns{:error, :detector_finished}. - Each detector holds mutable state. Use one detector per stream.
Summary
Functions
Feeds an enumerable of binary chunks into a detector and returns the guessed encoding.
Feeds an enumerable of binary chunks into a detector and returns the guessed encoding directly, raising on error.
Feeds a chunk of bytes into the detector.
Feeds a chunk of bytes into the detector, returning non_ascii_seen? or
raising on error.
Returns the current guessed WHATWG encoding name for all bytes fed so far.
Returns the guessed encoding name directly, raising on error.
Creates a new streaming encoding detector.
Types
@type feed_error() :: :input_too_large | :detector_finished | :lock_poisoned
Error reasons returned while feeding chunks.
@type new_options() :: [ dirty_threshold: non_neg_integer(), max_input_size: non_neg_integer() | :infinity, allow_iso_2022_jp: boolean(), bom: boolean() ]
Options accepted by new/1.
@opaque t()
Functions
@spec detect_stream(Enumerable.t(), EncodingRs.detect_options()) :: {:ok, EncodingRs.encoding()} | {:error, feed_error()}
Feeds an enumerable of binary chunks into a detector and returns the guessed encoding.
Accepts all options supported by EncodingRs.detect/2.
Examples
iex> ["こん", "にちは"] |> EncodingRs.Detector.detect_stream()
{:ok, "UTF-8"}
@spec detect_stream!(Enumerable.t(), EncodingRs.detect_options()) :: EncodingRs.encoding()
Feeds an enumerable of binary chunks into a detector and returns the guessed encoding directly, raising on error.
Examples
iex> ["こん", "にちは"] |> EncodingRs.Detector.detect_stream!()
"UTF-8"
@spec feed(t(), binary(), boolean()) :: {:ok, boolean()} | {:error, feed_error()}
Feeds a chunk of bytes into the detector.
Returns {:ok, non_ascii_seen?} on success, where non_ascii_seen? is
true once any non-ASCII byte (or BOM) has been observed.
Set is_last to true only on the final chunk of a complete stream. Calling
feed/3 again after is_last: true returns {:error, :detector_finished}.
Examples
iex> detector = EncodingRs.Detector.new()
iex> EncodingRs.Detector.feed(detector, "ascii only", false)
{:ok, false}
@spec feed(t(), binary(), boolean(), EncodingRs.options()) :: {:ok, boolean()} | {:error, feed_error()}
Feeds a chunk of bytes into the detector, returning non_ascii_seen? or
raising on error.
Examples
iex> detector = EncodingRs.Detector.new()
iex> EncodingRs.Detector.feed!(detector, "hello", true)
false
@spec feed!(t(), binary(), boolean(), EncodingRs.options()) :: boolean()
@spec guess(t(), guess_options()) :: {:ok, EncodingRs.encoding()} | {:error, :lock_poisoned}
Returns the current guessed WHATWG encoding name for all bytes fed so far.
You can call guess/2 multiple times with different :tld or :allow_utf8
options without re-feeding the input.
Options
:tld(defaultnil): Lowercase ASCII top-level domain label without dots (such as"jp","cn", or"ru"), or punycode for IDN TLDs.:allow_utf8(defaulttrue): Set tofalseto exclude"UTF-8"from heuristic guesses.
Examples
iex> detector = EncodingRs.Detector.new()
iex> {:ok, false} = EncodingRs.Detector.feed(detector, "hello", true)
iex> EncodingRs.Detector.guess(detector)
{:ok, "UTF-8"}
iex> EncodingRs.Detector.guess(detector, allow_utf8: false)
{:ok, "windows-1252"}
@spec guess!(t(), guess_options()) :: EncodingRs.encoding()
Returns the guessed encoding name directly, raising on error.
Examples
iex> detector = EncodingRs.Detector.new()
iex> EncodingRs.Detector.feed!(detector, "hello", true)
false
iex> EncodingRs.Detector.guess!(detector)
"UTF-8"
@spec new(new_options()) :: t()
Creates a new streaming encoding detector.
Options
:allow_iso_2022_jp(defaultfalse): Set totruewhen detecting email or inputs that may useISO-2022-JP.:bom(defaulttrue): Set tofalseto ignore leading UTF-8 and UTF-16 BOMs.:dirty_threshold(default65_536): Chunk byte threshold for dirty CPU schedulers.:max_input_size(default104_857_600): Maximum chunk size in bytes, or:infinity.
Examples
iex> detector = EncodingRs.Detector.new()
iex> {:ok, true} = EncodingRs.Detector.feed(detector, "こんにちは", true)
iex> EncodingRs.Detector.guess(detector)
{:ok, "UTF-8"}