perf: Replace ISO date parser's split/collect with fixed-position structural checks#6
perf: Replace ISO date parser's split/collect with fixed-position structural checks#6Copilot wants to merge 3 commits intots/address-quality-auditfrom
Conversation
…ocations Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
tcrypt25519
left a comment
There was a problem hiding this comment.
@copilot There's no reason to bridge like 20 times for this. You can get it down to 2 if you do this with separate fns for 8 and 10.
Structural ISO 8601 date validation algorithm
Goal: validate only the character structure of a string as either:
YYYY-MM (length 8)
YYYY-MM-DD (length 10)
No semantic checks. Only shape.
Step-by-step algorithm
- Read input
Let s be the character array.
Let len be its length.
- Length guard
if len != 8 AND len != 10:
return false
- Initialize counters
digitSum = 0
hyphenSum = 0
- Validate required digit positions
Always check:
indices: 0,1,2,3,5,6
For each index i above:
if s[i] is digit:
digitSum += 1
If len == 10, also check:
indices: 8,9
- Validate hyphen positions
Always check:
if s[4] == '-':
hyphenSum += 1
If len == 10:
if s[7] == '-':
hyphenSum += 1
- Final structural verdict
if len == 8:
return digitSum == 6 AND hyphenSum == 1
if len == 10:
return digitSum == 8 AND hyphenSum == 2
- End
Result is true only if the string matches the required structural layout exactly.
…parse_iso_7 + parse_iso_10) Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Done in e2d4624. The byte-walker is replaced by two fixed-position functions: |
The ISO format parser (
YYYY-MM,YYYY-MM-DD) was usingsplit().collect::<Vec<_>>(), allocating aVecon every successful parse — inconsistent with the already-optimized slash-format parser.Changes
parse_iso_datedispatcher: selects between two dedicated functions based on string length (7 or 10); emits a"Too many - separators"error for lengths with excess hyphensparse_iso_7: handlesYYYY-MM(exactly 7 bytes) — validates digits at fixed positions 0–3 and 5–6, and a-at position 4, using a single compound boolean; no loop, no allocationparse_iso_10: handlesYYYY-MM-DD(exactly 10 bytes) — validates digits at fixed positions 0–3, 5–6, and 8–9, and-separators at positions 4 and 7, using a single compound boolean; no loop, no allocationparse_iso_month_yearandparse_iso_full_date: consolidated into the two new functions🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.