hn.today

A Better UTF-8 Automaton

orlp.net55 points0 comments
Screenshot of A Better UTF-8 Automaton

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.

Read on orlp.net0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Laravel Go

Laravel Go

Laravel for Go is a full-stack web framework with routing, validation, database tools, background jobs, workflows, and real-time events. It also supports Inertia for frontend integration and is open source under the MIT license. (github.com)

Laravel Go

Laravel Go

Laravel Go is a batteries-included Go framework that combines routing, data access, authentication, queues, workflows, and other features in one package. It uses standard Go patterns and Inertia to serve React, Vue, or Svelte pages without separate API boilerplate. (laravel.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.