Skip to content

Treat digits as word characters for simple \b with unicodeScalar semantics - #904

Open
hippi345 wants to merge 1 commit into
swiftlang:mainfrom
hippi345:fix/884-simple-boundary-digits
Open

hippi345 wants to merge 1 commit into
swiftlang:mainfrom
hippi345:fix/884-simple-boundary-digits

Conversation

@hippi345

Copy link
Copy Markdown

Summary

With .wordBoundaryKind(.simple) and .matchingSemantics(.unicodeScalar) together, \b treated digits as non-word characters even though \w still matches them. Either option alone already classified digits as word characters.

atSimpleBoundary in unicode-scalar mode used isAlphabetic || "_" and omitted digits. It now uses the same word-character predicate as \w / Character.isWordCharacter (letter, digit/number, or underscore).

Fixes #884

#901 reports the same bug.

Changes

Testing

On unmodified main (test only, no source fix), swift test --filter testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics failed:

Test Case 'RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics' started at 2026-10-17 02:42:54.739
/workspace/Tests/RegexTests/MatchTests.swift:2551: error: RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics : XCTAssertTrue failed -
/workspace/Tests/RegexTests/MatchTests.swift:2552: error: RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics : XCTAssertTrue failed -
/workspace/Tests/RegexTests/MatchTests.swift:2553: error: RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics : XCTAssertFalse failed -
/workspace/Tests/RegexTests/MatchTests.swift:2554: error: RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics : XCTAssertFalse failed -
/workspace/Tests/RegexTests/MatchTests.swift:2555: error: RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics : XCTAssertFalse failed -

Those five failures are the #884 cases: "9" vs 9\b, "e9" vs (r|e)9\b, "x5" vs x\b, "5x" vs \bx, and "1pw2" vs \bpwd?\b.

After the fix, the same filter passed:

Test Case 'RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics' started at 2026-10-17 02:43:15.081
Test Case 'RegexTests.testSimpleWordBoundaryDigitsWithUnicodeScalarSemantics' passed (0.004 seconds)
Test Suite 'RegexTests' passed at 2026-10-17 02:43:15.085
	 Executed 1 test, with 0 failures (0 unexpected) in 0.004 (0.004) seconds

swift test --filter RegexTests also passed: 131 XCTest cases in the RegexTests target (including MatchTests and nearby suites), 9 DocumentationTests RegexTests cases, and 6 Swift Testing cases in OptimizationTests/DSLListTests, 0 failures.

This change was prepared with AI assistance and reviewed by the author.

`atSimpleBoundary` under `.unicodeScalar` semantics only treated
alphabetic scalars and underscore as word characters, so `\b` with
`.wordBoundaryKind(.simple)` disagreed with `\w` on digits.

Use `Character.isWordCharacter` (letter, digit/number, or underscore)
for the scalar path as well.

Fixes swiftlang#884
@hippi345
hippi345 marked this pull request as ready for review October 17, 2026 03:03
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.

\b treats digits as non-word characters with .wordBoundaryKind(.simple) and .matchingSemantics(.unicodeScalar)

1 participant