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: false is passed to new/1 or detect_stream/2.
  • Pass is_last: true only when the full stream ends. If you feed only a prefix of a larger file, pass is_last: false and call guess/2 directly.
  • Calling feed/3 after is_last: true returns {:error, :detector_finished}.
  • Each detector holds mutable state. Use one detector per stream.

Summary

Types

Error reasons returned while feeding chunks.

Options accepted by guess/2 and guess!/2.

Options accepted by new/1.

t()

An opaque detector created with new/0 or new/1.

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

feed_error()

@type feed_error() :: :input_too_large | :detector_finished | :lock_poisoned

Error reasons returned while feeding chunks.

guess_options()

@type guess_options() :: [tld: String.t() | nil, allow_utf8: boolean()]

Options accepted by guess/2 and guess!/2.

new_options()

@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.

t()

@opaque t()

An opaque detector created with new/0 or new/1.

Functions

detect_stream(chunks, opts \\ [])

@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"}

detect_stream!(chunks, opts \\ [])

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"

feed(detector, chunk, is_last \\ false)

@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}

feed(detector, chunk, is_last, opts)

@spec feed(t(), binary(), boolean(), EncodingRs.options()) ::
  {:ok, boolean()} | {:error, feed_error()}

feed!(detector, chunk, is_last \\ false)

@spec feed!(t(), binary(), boolean()) :: boolean()

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

feed!(detector, chunk, is_last, opts)

@spec feed!(t(), binary(), boolean(), EncodingRs.options()) :: boolean()

guess(detector, opts \\ [])

@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 (default nil): Lowercase ASCII top-level domain label without dots (such as "jp", "cn", or "ru"), or punycode for IDN TLDs.
  • :allow_utf8 (default true): Set to false to 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"}

guess!(detector, opts \\ [])

@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"

new(opts \\ [])

@spec new(new_options()) :: t()

Creates a new streaming encoding detector.

Options

  • :allow_iso_2022_jp (default false): Set to true when detecting email or inputs that may use ISO-2022-JP.
  • :bom (default true): Set to false to ignore leading UTF-8 and UTF-16 BOMs.
  • :dirty_threshold (default 65_536): Chunk byte threshold for dirty CPU schedulers.
  • :max_input_size (default 104_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"}