Skip to content

Convert long runs of letters with no uppercase form in linear time [patch] - #139

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/138-linear-caseless-runs
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/138-linear-caseless-runs

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #138

What was wrong

IsWordBoundary ran at every code point, and each call ran two scans over the surrounding run of lowercase letters that have no uppercase form (ß, ĸ, fi, 𝓪, …), whether or not their result was used:

  • the forward SkipLowercaseWithNoUppercase scan, whose result only the acronym-tail check reads
  • the backward EndsWordThatIsNotUppercase scan, which ran before the cheap currentIsUpper test

Every converter was therefore O(n²) on such a run. 16,000 ß took about 17 s, and pasted "fancy font" text made this a cheap denial of service.

Change

Both are the guards the issue suggested, and neither changes which boundaries are found:

  1. Compute casedNextStart only when previousIsUpper && currentIsUpper, which is the only case where it is read.
  2. Test currentIsUpper before EndsWordThatIsNotUppercase. && is commutative here because both operands are side-effect free.

Tests

LongRunOfLowercaseWithNoUppercaseConvertsInLinearTime has three rows: ß, ĸ and 𝓪 ×16,000. Each row runs all six converters on the run by itself and again after an all-caps run ("A"×16000 + run + "a"), which keeps the acronym path exercised. Each conversion must finish within 2 s.

  • With the fix reverted, all three rows fail. The first conversion alone took 6–7 s.
  • With the fix, all three rows pass in milliseconds.
  • Full suite on net10.0: 213/213 pass, including "STRAßE" and "XMLÉtude".

Making IsLowercaseWithNoUppercase allocation-free is left as the follow-up the triage suggested.


Generated by Claude Code

IsWordBoundary ran two scans over the surrounding run of lowercase letters
with no uppercase form (ß, ĸ, fi, 𝓪) at every code point, whether or not
their result was used. On a run of n such letters every converter did
O(n²) allocating work: 16,000 "ß" took ~17 s.

The forward scan now runs only when an acronym tail is possible, and the
backward scan only after the cheap currentIsUpper check.

Fixes #138

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2PeSdxz6omjxYwwT4
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 58f679d into main Nov 4, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/138-linear-caseless-runs branch November 4, 2026 00:42
matt-edmondson pushed a commit that referenced this pull request Nov 4, 2026
Resolves the conflict in IsWordBoundary with #139 and #143. The
inherited uppercase of a letter with no uppercase form is now looked up
only before a capital, the same way #139 made the cased-next scan lazy.
Looked up at every code point, it walked back over a long run of such
letters each time and failed LongRunOfLowercaseWithNoUppercaseConvertsInLinearTime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5wGJLhri3KXBeZJwH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants