The piece presents a compact UTF-8 validator built around a finite-state automaton: process each byte from an accepting state, and the input is valid only if every transition is allowed and the final state is accepting. Its byte classes and special states handle UTF-8’s key restrictions, including continuation-byte counts and the exceptional ranges that prevent overlong encodings, surrogate code points, and values above U+10FFFF. The implementation also tracks where valid input ends and can resume at a synchronization point after an error, rather than requiring a single terminal error state.
Its main innovation is encoding transitions as a shift-DFA. Instead of looking up a transition using both the current state and byte, each byte selects a prepacked word, then a shift based on the state yields the next one. This makes transitions branchless and allows byte lookups to proceed independently, enabling parallel processing. A SAT solver packs the automaton’s 12 meaningful states into 32 possible state indices; the remaining 20 represent errors and are constrained to stay invalid. The resulting constants fit in 32-bit words despite the overlap needed to encode transitions. Parallel validation can also process overlapping chunks, verify their shared bytes, and synchronize the following chunk, while any error remains detectable by the chunk that encounters it.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.