Skip to content

Count letters directly in IsAlphaNumber instead of matching a regex - #4075

Open
rootkiller6788 wants to merge 3 commits into
google:masterfrom
rootkiller6788:fix-cpp-isalphanumber-linear-scan
Open

rootkiller6788 wants to merge 3 commits into
google:masterfrom
rootkiller6788:fix-cpp-isalphanumber-linear-scan

Conversation

@rootkiller6788

Copy link
Copy Markdown

Java's isAlphaNumber() was switched to a linear letter-count + a 250-char length bound a while back (PR #4017) because the regex could blow up on long input. The C++ port was left on the old anchored regex "(?i)(?:.*?[a-z]){3}", and that regex is also stricter than the Java/JS one: it requires the number to end with a letter, so "800 FLOWER7" was rejected in C++ but accepted in Java and JavaScript. Since IsAlphaNumber is a public API that should behave the same across ports, I ported the Java approach: count at least three ASCII letters and bail out for anything over 250 chars before running the viable-number regex.

Verified with a small std::regex harness: the old fullmatch is equivalent to (>=3 letters && ends with a letter), the new scan matches the Java/JS semantics, and the existing IsAlphaNumber tests keep passing.

IsAlphaNumber() was checking the whole number against the anchored regex
"(?i)(?:.*?[a-z]){3}", which requires the number to end in a letter. That
means vanity numbers ending in a digit (e.g. "800 FLOWER7") were rejected
here while the Java and JavaScript ports, whose pattern has a trailing
".*", accept them. A plain scan for three ASCII letters matches the other
ports and no longer depends on how the regexp engine anchors the match.

Also reject input longer than 250 characters up front, mirroring the
MAX_INPUT_STRING_LENGTH bound that was recently added to the Java port.
Covers numbers like "800 FLOWER7" that have three or more letters but end
in a digit, plus over-long input that should be rejected up front. The
old code returned false for these even though Java and JavaScript accept
them.
@rootkiller6788
rootkiller6788 requested a review from a team as a code owner September 8, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants