fix(table-cells): parse currency input in linear time
Merged
WA
11d ago+46+46+01234567890123456789-14-14-01234567890123456789
Summary
parseCurrencyInputbacktracked quadratically on crafted cells (CodeQLjs/polynomial-redos); table imports and coercion run it per cell on the server, so one long cell could stall the event loop for secondsCURRENCY_MARKER_PREFIX: whitespace runs on both sides of an optional sign split ambiguously → the sign and its trailing whitespace are one optional group (^\s*(?:([+-])\s*)?…), callback returnssign ?? ''CURRENCY_MARKER_SUFFIX: unanchored with a leading whitespace run, soreplacerescanned the run from every start → drop the whitespace runs and match againsttrimEnd(); the whitespace left before the marker is stripped with every other space right afterAMOUNT_SHAPE: overlapping[\d.,]*\d[\d.,]*→[.,]*\d[\d.,]*(same language, disjoint runs)[\s ]→\s(\sandtrimEndalready cover U+00A0/U+202F); no length cap, no behaviour change- Other regexes in
packages/table-cells/srcchecked and measured: all linear (ttl.ts's/0+$/runs on at most 6 chars)
Type of Change
- Bug fix
Testing
- Equivalence (one-off script, old vs new): 0 differences over 550k inputs (test corpus,
Intloutput across 40 locales × 21 currencies × 4 displays × 2 signs, random and structured combos of digits,.,'’, whitespace incl. U+00A0/U+202F, signs incl. U+2212, parens, symbols, ISO codes,kr/zł/R$, letters,e/E) × 8 currency codes on both Bun and Node, plus every string up to length 6 over a 14-char alphabet × 3 codes (24.3M comparisons) - Linearity: new
expectLinearGrowthcases incurrency.test.tsfail on the old code (growth ratios 55–91 vs the 20 limit) and pass on the new; reverting each regex alone turns its own case red. At 40k chars the old code took 1–7s per cell, the new under 1ms table-cells,workspace-codekinds, andapps/simtable import/coercion/column-type tests; lint, type-check,check:audits,check:test-types, block-registry check, rootbun run test(one unrelatedworkflow-codescaling flake at 20.5, green on rerun)
Checklist
- Code follows project style guidelines
- Self-reviewed my changes
- Tests added/updated and passing (new tests pass the
test-auditauthoring gate) - No new warnings introduced
- I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)