Skip to content

Commit 464ddd5

Browse files
authored
Merge pull request #801 from swiftlang/swift61-repeated-case-insensitivity
[6.1] Fix repeated case-insensitive ASCII match
2 parents 03db4e2 + 57b97e7 commit 464ddd5

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,19 @@ fileprivate extension Compiler.ByteCodeGen {
773773
case .atom(let atom):
774774
switch atom {
775775
case .char(let c):
776-
// Single scalar ascii value character
777-
guard let val = c._singleScalarAsciiValue else {
778-
return false
776+
if options.isCaseInsensitive && c.isCased {
777+
// Cased character with case-insensitive matching; match only as an ASCII bitset
778+
guard let bitset = DSLTree.CustomCharacterClass(members: [.atom(atom)]).asAsciiBitset(options) else {
779+
return false
780+
}
781+
builder.buildQuantify(bitset: bitset, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
782+
} else {
783+
// Uncased character OR case-sensitive matching; match as a single scalar ascii value character
784+
guard let val = c._singleScalarAsciiValue else {
785+
return false
786+
}
787+
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
779788
}
780-
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
781789

782790
case .any:
783791
builder.buildQuantifyAny(

Tests/RegexTests/MatchTests.swift

+35
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,41 @@ extension RegexTests {
21932193
("cafe", true),
21942194
("CaFe", true),
21952195
("EfAc", true))
2196+
2197+
matchTest(
2198+
#"(?i)a+b"#,
2199+
("ab", true),
2200+
("Ab", true),
2201+
("aB", true),
2202+
("AB", true),
2203+
("AaAab", true),
2204+
("aaaAB", true))
2205+
matchTest(
2206+
#"^(?i)a?b$"#,
2207+
("ab", true),
2208+
("Ab", true),
2209+
("aB", true),
2210+
("AB", true),
2211+
("aaB", false),
2212+
("b", true),
2213+
("B", true))
2214+
matchTest(
2215+
#"^(?i)[a]?b$"#,
2216+
("ab", true),
2217+
("Ab", true),
2218+
("aB", true),
2219+
("AB", true),
2220+
("b", true),
2221+
("B", true))
2222+
matchTest(
2223+
#"^(?i)a{2,4}b$"#,
2224+
("ab", false),
2225+
("Ab", false),
2226+
("AaB", true),
2227+
("aAB", true),
2228+
("aAaB", true),
2229+
("aAaAB", true),
2230+
("AaAaAB", false))
21962231
}
21972232

21982233
func testNonSemanticWhitespace() {

0 commit comments

Comments
 (0)