Skip to content

♻️ refactor(c): shared ascii.h and reconcile whitespace/case drift - #512

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:refactor/core-ascii-dedup
Jul 5, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:refactor/core-ascii-dedup

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Part of #478, the ascii.h remainder 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 after core/vec.h from #511. It holds lower_ascii, is_ascii_alpha, is_ascii_digit, is_ascii_hexdigit, and is_space as static inline helpers over a uint32_t, so every call site inlines the comparison it used to spell out and the css/js engines compile the header in their JM_STANDALONE builds. The width-tagged buffer helpers (ucs_width, buf_read, buf_write) move next to th_buf in tokenizer/statemachine.h, which keeps ascii.h clear 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.

Site Verdict Resolution
links.c, sanitize.c, serialize/internal.h, readability.c whitespace, CR omitted Bug Route to the shared is_space, which includes CR
dates.c is_space includes vertical tab Intentional Renamed is_perl_space, why-comment, kept local
dates.c lower_cp folds Latin-1 Intentional Renamed lower_month_cp, noted as wider than ASCII
char vs Py_UCS4 case fold in the encoding sniffer Reconciled The byte folds call (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 own is_space comment 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/\r for href="
http://example.com/
" instead of the trimmed URL, and to_text(), to_markdown(), and main_text() left a 
 run uncollapsed. All four now share is_space, and the fix is exercised for the trim, both collapses, and the readability normalize path. 🐛

The intentional keeps stay distinct on purpose. dates.c parses arbitrary extracted text, not a tokenizer stream, and its whitespace is the Perl \s the ported _dates.py regex matched, which keeps the vertical tab that HTML whitespace omits, so is_perl_space names that. Its month vocabulary folds accented Latin-1 letters (AOÛT, MÄRZ), so lower_month_cp stays wider than the shared ASCII fold. The XPath lexer's xp_is_space is a third set again (space, tab, CR, LF, no form feed, the XML S production), 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 (-Werror plus clang-tidy), type, docs, prek, and standalone asan-js gates pass, with the JS engine compiling core/ascii.h under -Werror and running sanitizer-clean; 3.10 through 3.14 are green, and static inline keeps codegen identical so CodSpeed stays neutral.

@gaborbernat gaborbernat added bug Something isn't working enhancement New feature or request labels Jul 5, 2026
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
gaborbernat force-pushed the refactor/core-ascii-dedup branch from 8718940 to 66d1d1b Compare July 5, 2026 10:27
@gaborbernat
gaborbernat enabled auto-merge (squash) July 5, 2026 10:27
@codspeed

codspeed Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 18 skipped benchmarks1


Comparing gaborbernat:refactor/core-ascii-dedup (6689058) with main (75cadc6)

Open in CodSpeed

Footnotes

  1. 18 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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.
@gaborbernat
gaborbernat merged commit e1c098b into tox-dev:main Jul 5, 2026
48 checks passed
@gaborbernat
gaborbernat deleted the refactor/core-ascii-dedup branch July 10, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant