Repository navigation
feat(encoding): Parsec could only parse text that was already decoded - #79
Merged
Merged
Conversation
Pages arrive as bytes, and Parsec took only &str, so every caller had to guess the encoding first. crates/parsec-encoding (package parsec-html-encoding, crate parsec_encoding) adds parse_bytes, which follows the HTML Standard's encoding sniffing: byte order mark, then the transport-layer charset if given, then the prescan of the first 1,024 bytes, then a fallback (windows-1252, or the spec's suggested default for a locale via locale_default). When the parser meets a <meta> that names another encoding than a tentative guess, the page is decoded and parsed once more in that encoding, as a browser restarts; the time budget covers both parses. The decoding is encoding_rs, re-exported, so every WHATWG encoding decodes and encodes by label, and streams through its Decoder. Tests: the html5lib-tests encoding/ files (82 pass, 1 scripted case skipped) and the web-platform-tests encoding/ data pinned in tests/wpt: labels, single-byte tables, the ISO-2022-JP, GB18030 and GBK decoder cases and the legacy multi-byte character pages, all passing, with counts written for CI's conformance summary.
`parsec-html-bench encoding` times bytes to UTF-8 for UTF-8, Shift_JIS, windows-1251 and GBK, on two real pages transcoded with encoding_rs and a generated page of dense text, against Lexbor's encoding module (to code points, and on to UTF-8 with its encoder). It also runs Lexbor's decoders over the WPT character pages, which shows its Shift_JIS decoder refusing lead bytes 0xE1 to 0xFB.
This was referenced Nov 19, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Parsec took only
&str. You had to know a page's encoding before you could parse it, and most pages on the web don't tell you in the place you'd look first. Lexbor sniffs it (lxb_html_encoding_determine) and has its own decoders.Change
A new crate,
crates/parsec-encoding(packageparsec-html-encoding, crateparsec_encoding), beside the synced core, which it doesn't touch.parse_bytes(&[u8], Options)follows the HTML Standard's encoding sniffing: a byte order mark, then the transport-layer charset if you pass one, then the prescan of the first 1,024 bytes, then a fallback. It returns the document, the encoding and the spec's confidence.<meta>naming another encoding than a tentative guess (one past the prescan's 1,024 bytes, say), the spec has the browser start again. Here that's one more decode and parse of the same bytes, at most once; the time budget covers both.sniffandprescanon their own. The prescan follows "prescan a byte stream", "get an attribute", "get an XML encoding" and the meta-content extraction step by step.locale_defaultgives the spec's suggested default for a language. That's the table already in the standard; TLD and text guessing aren't, so they're left out.Decoder. Feeding the parser chunk by chunk waits for chunked input (Chunked input that builds exactly the same tree #6).bench/:parsec-html-bench encodingtimes bytes to UTF-8 against Lexbor's encoding module. It also runs Lexbor's decoders over the WPT character pages. Lexbor 3.0.x gets 5,240 of 7,393 Shift_JIS characters right because it refuses lead bytes 0xE1 to 0xFB. That's fixed on Lexbor's master in Encoding: accept the full Shift_JIS upper lead byte range lexbor/lexbor#424, not yet released.New dependency:
encoding_rs0.8 ("(Apache-2.0 OR MIT) AND BSD-3-Clause", MSRV 1.88), plussimdutf8,scopeguard,cfg-ifandmultiversion_no_opunder it, all MIT or Apache-2.0.cargo deny checkpasses.Tests
encoding/(tests1.dat,tests2.dat,test-yahoo-jp.dat): 82 passed, 0 failed, 1 skipped (scripted/tests1.datwrites its<meta>withdocument.write). The prescan alone decides 75; the other 7 put the<meta>past byte 1,024 and need the reparse.encoding/data, pinned incrates/parsec-encoding/tests/wptat0c423abwith its licence: 228 labels (with case and whitespace variants), 168 single-byte label tables, 38 ISO-2022-JP and 357 GB18030/GBK decoder cases, and 62,352 characters across the Shift_JIS, EUC-JP, ISO-2022-JP, EUC-KR and Big5 pages, each page throughparse_bytes. All pass. An encoder round trip over the same pages passes 53,813 of 53,813. That one is not a WPT count.<meta>, the late-<meta>reparse and the locale table. Doc examples run too.$CONFORMANCE_DIRlike the other suites, so they show in the run summary.Closes #5
Refs #25: streaming the decoder into the parser waits for chunked input (#6).