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:
- The BEAM compiler optimizes binary pattern matching into a jump table
- No intermediate string allocations
- 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)
endGenerates:
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
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)
endThe _ in patterns is the byte variable (unused but required for syntax).
The second element rest captures the remaining 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.
Returns a list of {byte_value, action} tuples suitable for generating
case clauses.