---
title: "A Better UTF-8 Automaton"
authors:
  - "Orson R. L. Peters"
year: 2026
venue: "orlp.net blog, 29 Nov 2026"
arxiv: null
doi: null
source: "https://orlp.net/blog/utf8-dfa/"
version_reviewed: "web 2026-11-29"
topics:
  - "utf-8"
  - "unicode"
  - "shift-dfa"
  - "finite-automata"
  - "instruction-level-parallelism"
  - "sat-solving"
  - "text-processing"
seed_rank: 2772
seed_batch: "craft-2026-12-01"
reviewed: "2026-12-01"
pool: "systems"
relevance_score: 8
lineage: unicode-text-shaping
cites:
  - title: "A Better UTF-8 Automaton"
    url: "https://orlp.net/blog/utf8-dfa/"
    year: 2026
    arxiv: null
    doi: null
  - title: "Shift-based DFAs (Per Vognsen, gist)"
    url: "https://gist.github.com/pervognsen/218ea17743e1442e59bb60d29b1aa725"
    year: null
    arxiv: null
    doi: null
  - title: "Flexible and Economical UTF-8 Decoder (Björn Höhrmann)"
    url: "https://bjoern.hoehrmann.de/utf-8/decoder/dfa/"
    year: null
    arxiv: null
    doi: null
  - title: "simdutf"
    url: "https://github.com/simdutf/simdutf"
    year: null
    arxiv: null
    doi: null
see: []
---

# A Better UTF-8 Automaton

## One-sentence takeaway

A UTF-8 validating DFA with thirteen states (twelve valid plus error), packed by a Z3 SAT search into 32-bit "shift-DFA" constants (`state = DFA[byte] >> (state & 31)`, a 1 KiB table where earlier versions needed 2 KiB). It adds a SYNC state that absorbs continuation bytes, so two DFAs can run interleaved over overlapping chunks for instruction-level parallelism. It also never merges partial-codepoint states, so error recovery reads off how many bytes to back up. About 30 lines of scalar C validate Chinese text 9× faster than Höhrmann's classic DFA and only about 2.5× slower than simdutf on an Apple M5.

## Why it matters here

Anoptic's scanline text and ano's Japanese surface both start at "is this byte stream valid UTF-8, and where does it break?" This gives a tiny, branch-free, dependency-free C answer with exact error positions, which suits a C23 engine that may not want simdutf's size. The deeper takeaway is the technique. Storing *symbol → (state → state)* in one register takes the table lookup off the critical path, and a SAT solver can pack overlapping transition functions into magic constants. Any small automaton in an engine fits that pattern: a tokenizer, an input-combo recognizer, an ano lexer state machine. The SYNC-and-overlap trick shows how to parallelize a serial DFA when the language has bounded resynchronization. The ano and engine applications are my reading.

## Key ideas

- **Shift-DFA.** For ≤32 states, each byte class maps to a uint32 whose 5-bit fields, read at the current state's offset, give the next state. One load plus one shift per byte, and loads don't depend on the previous state.
- **SAT packing.** Thirteen states don't fit naively (13 × 4 = 52 bits). So the 12 valid states sit at overlapping 5-bit offsets, and the other 20 of the 32 offsets are all "invalid" states that must stay invalid under every transition. Z3 finds offsets and words in about 15 minutes. Validity is one shift into a bitmask (`0x4459a285`).
- **Parallel validation.** Process 32-byte chunks with two DFAs, one from the carried state over bytes 0–17 and one from SYNC over bytes 14–31. The 4-byte overlap (the maximum UTF-8 length) lets the second synchronize, and the first catches any error in the overlap. That is roughly 2× over prior shift-DFA validators.
- **Instant recovery.** Because states encode how many bytes of a partial codepoint were consumed, `partial(s)` tells how far to back up to insert U+FFFD.
- **Not a complete validator.** The author notes a real routine needs an ASCII fast path. This is the core automaton.

## Caveats

This is a blog post with one benchmark (Chinese text, Apple M5). There are no x86 numbers, and the ASCII-heavy case isn't measured. simdutf is still faster, and the author credits Dougall Johnson and Thom Chiovoloni with earlier 32-bit SAT packings, so the novelty is the SYNC state and recovery-friendly state set rather than the packing itself. The code relies on GNU range designators (`[0x00 ... 0x7f]`), which are not ISO C23. I read the full post. I did not run the code or re-run the Z3 search.

## Links

- Post (with C and Z3 code): https://orlp.net/blog/utf8-dfa/
- Per Vognsen's shift-DFA gist: https://gist.github.com/pervognsen/218ea17743e1442e59bb60d29b1aa725
- Höhrmann's UTF-8 DFA decoder: https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
- simdutf: https://github.com/simdutf/simdutf
