Skip to content

[swift/main] Fix repeated case-insensitive ASCII match #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,19 @@ fileprivate extension Compiler.ByteCodeGen {
case .atom(let atom):
switch atom {
case .char(let c):
// Single scalar ascii value character
guard let val = c._singleScalarAsciiValue else {
return false
if options.isCaseInsensitive && c.isCased {
// Cased character with case-insensitive matching; match only as an ASCII bitset
guard let bitset = DSLTree.CustomCharacterClass(members: [.atom(atom)]).asAsciiBitset(options) else {
return false
}
builder.buildQuantify(bitset: bitset, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
} else {
// Uncased character OR case-sensitive matching; match as a single scalar ascii value character
guard let val = c._singleScalarAsciiValue else {
return false
}
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
}
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)

case .any:
builder.buildQuantifyAny(
Expand Down
35 changes: 35 additions & 0 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,41 @@ extension RegexTests {
("cafe", true),
("CaFe", true),
("EfAc", true))

matchTest(
#"(?i)a+b"#,
("ab", true),
("Ab", true),
("aB", true),
("AB", true),
("AaAab", true),
("aaaAB", true))
matchTest(
#"^(?i)a?b$"#,
("ab", true),
("Ab", true),
("aB", true),
("AB", true),
("aaB", false),
("b", true),
("B", true))
matchTest(
#"^(?i)[a]?b$"#,
("ab", true),
("Ab", true),
("aB", true),
("AB", true),
("b", true),
("B", true))
matchTest(
#"^(?i)a{2,4}b$"#,
("ab", false),
("Ab", false),
("AaB", true),
("aAB", true),
("aAaB", true),
("aAaAB", true),
("AaAaAB", false))
}

func testNonSemanticWhitespace() {
Expand Down