Repository navigation
♻️ refactor(c): shared ascii.h and reconcile whitespace/case drift - #512
Merged
Merged
Conversation
Route the hand-rolled ASCII classification scattered across the C core through one Python-free header, core/ascii.h, next to core/vec.h. It holds lower_ascii, is_ascii_alpha, is_ascii_digit, is_ascii_hexdigit, and is_space as static inline helpers on a uint32_t, so each site inlines the same comparison it wrote by hand and the css/js engines compile it in their JM_STANDALONE builds. The width-tagged buffer helpers (ucs_width, buf_read, buf_write) move to tokenizer/statemachine.h, next to th_buf, so ascii.h keeps no CPython dependency. Reconciling the drift the audit flagged, kept explicit rather than accidental: - Whitespace, CR omission (BUG). Four scanners in links.c, sanitize.c, serialize/internal.h, and readability.c dropped U+000D, reasoning that input preprocessing folds every CR to LF. A &tox-dev#13; character reference decodes to a real CR past that step, and CR is HTML ASCII whitespace, so links() kept a stray CR on a trimmed URL and the text/markdown/main_text exporters left one uncollapsed. All four now share is_space. Regression tests cover each. - dates.c is_space includes vertical tab (INTENTIONAL). It matches the Perl \s the ported _dates.py regex used, not HTML whitespace. Renamed is_perl_space with a why-comment; kept local; a VT-separated date test locks it. - dates.c lower_cp folds Latin-1 (INTENTIONAL). The month vocabulary needs accented letters (AOUT, MARZ). Renamed lower_month_cp, noting it is wider than ASCII lower_ascii on purpose. - char vs Py_UCS4 case fold (RECONCILED). The encoding sniffer's byte folds now call (char)lower_ascii(ch), so classification is shared while char storage stays. Parser and serializer output is unchanged except the CR fix. Verified 100% line and branch on both toolchains: macOS llvm-cov and gcc-13 in ubuntu:24.04. Part of tox-dev#478.
gaborbernat
force-pushed
the
refactor/core-ascii-dedup
branch
from
July 5, 2026 10:27
8718940 to
66d1d1b
Compare
gaborbernat
enabled auto-merge (squash)
July 5, 2026 10:27
Merging this PR will not alter performance
Comparing Footnotes
|
The 3.10 whole-tree C branch gate slipped to 96.9% (floor 97%) after the ascii.h refactor folded ~50 covered inline ASCII checks into shared inline functions, shrinking the branch denominator so 3.10's dead PyUnicode_READ ternaries crossed the 3% budget. dates.c sat at 55% branch: every CP() read is PyUnicode_READ, whose 2- and 4-byte ternary sides never run when the whole date-test corpus feeds 1-byte ASCII strings (branchless on 3.11+, hence green there). Re-run the scan/scan_all/url cases with a BMP star and astral emoji appended so the argument is 2- and 4-byte stored; the same dates must fall out. This lifts dates.c to 99.5% and the tree to 99.0% branch on gcc-13/3.10, keeps macOS llvm-cov at 100% line+branch.
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.
Part of #478, the
ascii.hremainder of the C-consolidation work. The core had the same handful of ASCII questions written by hand in about forty-five places across the tokenizer, tree builder, selector and XPath parsers, encoding sniffer, and the link, date, sanitize, markdown, and readability scanners: whether a code point is an ASCII letter, a digit, a hex digit, or HTML whitespace, and what its ASCII-lowercase form is. 🧹This routes them through one Python-free header,
core/ascii.h, styled aftercore/vec.hfrom #511. It holdslower_ascii,is_ascii_alpha,is_ascii_digit,is_ascii_hexdigit, andis_spaceasstatic inlinehelpers over auint32_t, so every call site inlines the comparison it used to spell out and the css/js engines compile the header in theirJM_STANDALONEbuilds. The width-tagged buffer helpers (ucs_width,buf_read,buf_write) move next toth_bufintokenizer/statemachine.h, which keepsascii.hclear of any CPython dependency.A byte-identical merge would have been wrong, because several sites had real semantic drift. #511 flagged this, so each one is decided here rather than folded away silently.
links.c,sanitize.c,serialize/internal.h,readability.cwhitespace, CR omittedis_space, which includes CRdates.cis_spaceincludes vertical tabis_perl_space, why-comment, kept localdates.clower_cpfolds Latin-1lower_month_cp, noted as wider than ASCIIcharvsPy_UCS4case fold in the encoding sniffer(char)lower_ascii(ch)Four whitespace scanners dropped U+000D, each commented that the WHATWG input preprocessor folds every carriage return to a line feed, so a parsed value never carries one. A
character reference decodes to a real carriage return after preprocessing, which the header's ownis_spacecomment already noted, and carriage return is HTML ASCII whitespace. The contexts here run the "strip leading and trailing ASCII whitespace" and "split on ASCII whitespace" steps, so dropping it was a bug:links()reported\rhttp://example.com/\rforhref=" http://example.com/ "instead of the trimmed URL, andto_text(),to_markdown(), andmain_text()left a run uncollapsed. All four now shareis_space, and the fix is exercised for the trim, both collapses, and the readability normalize path. 🐛The intentional keeps stay distinct on purpose.
dates.cparses arbitrary extracted text, not a tokenizer stream, and its whitespace is the Perl\sthe ported_dates.pyregex matched, which keeps the vertical tab that HTML whitespace omits, sois_perl_spacenames that. Its month vocabulary folds accented Latin-1 letters (AOÛT, MÄRZ), solower_month_cpstays wider than the shared ASCII fold. The XPath lexer'sxp_is_spaceis a third set again (space, tab, CR, LF, no form feed, the XMLSproduction), already distinctly named and left local.Parsing and serialization produce the same bytes as before apart from the carriage-return fix. Coverage holds at 100% line and branch on both toolchains: macOS llvm-cov over the whole tree, and gcc-13 in ubuntu:24.04 over every changed file, the layout-dependent-branch check that llvm alone can miss. The
warnings(-Werrorplus clang-tidy),type,docs,prek, and standaloneasan-jsgates pass, with the JS engine compilingcore/ascii.hunder-Werrorand running sanitizer-clean; 3.10 through 3.14 are green, andstatic inlinekeeps codegen identical so CodSpeed stays neutral.