ToonEx.Decode.Codegen (toon_ex v1.1.0)

Copy Markdown View Source

Compile-time code generation utilities for the TOON decoder.

Inspired by Jason.Codegen, this module provides macros for generating efficient byte-level dispatch tables at compile time. This replaces runtime regex checks and String.contains? calls with O(1) binary pattern matching.

How it works

The bytecase/2 macro generates a case expression on a binary where each clause matches a specific leading byte. This is more efficient than calling String.starts_with?/2 or regex matching because:

  1. The BEAM compiler optimizes binary pattern matching into a jump table
  2. No intermediate string allocations
  3. The dispatch is O(1) regardless of the number of clauses

Example

bytecase data do
  _ in ~c'0123456789', rest ->
    parse_number(rest)
  _ in ~c'"', rest ->
    parse_string(rest)
  _, rest ->
    error(rest)
end

Generates:

case data do
  <<48, rest::bits>> -> parse_number(rest)
  <<49, rest::bits>> -> parse_number(rest)
  ...
  <<34, rest::bits>> -> parse_string(rest)
  <<byte, rest::bits>> -> error(rest)
end

Summary

Functions

Generates a case expression that dispatches on the first byte of a binary.

Like bytecase/2 but with an explicit max byte value for the jump table. Useful when you want to cover all bytes up to a certain value.

Builds a compile-time jump table from byte ranges.

Functions

bytecase(var, list)

(macro)

Generates a case expression that dispatches on the first byte of a binary.

Clause syntax

bytecase var do
  _ in ~c'abc', rest ->
    # matches bytes 97, 98, 99
    handle_abc(rest)

  _ in 0..31, rest ->
    # matches bytes 0-31
    handle_control(rest)

  _, rest ->
    # default clause
    handle_other(rest)
end

The _ in patterns is the byte variable (unused but required for syntax). The second element rest captures the remaining binary.

bytecase(var, max, list)

(macro)

Like bytecase/2 but with an explicit max byte value for the jump table. Useful when you want to cover all bytes up to a certain value.

jump_table(ranges, default)

Builds a compile-time jump table from byte ranges.

Returns a list of {byte_value, action} tuples suitable for generating case clauses.

jump_table(ranges, default, max)