Skip to content

Commit 0a81bf3

Browse files
Azoyhamishknight
authored andcommitted
Merge pull request swiftlang#546 from Azoy/fix-emitany
Fix crash when emitting an atom of any
1 parent 0ca8d67 commit 0a81bf3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,15 @@ extension DSLTree.Atom {
117117

118118
case .any:
119119
// FIXME: Should this be a total ordering?
120-
fatalError(
121-
"unreachable: emitAny() should be called isntead")
120+
if opts.semanticLevel == .graphemeCluster {
121+
return { input, bounds in
122+
input.index(after: bounds.lowerBound)
123+
}
124+
} else {
125+
return consumeScalar { _ in
126+
true
127+
}
128+
}
122129

123130
case .assertion:
124131
// TODO: We could handle, should this be total?

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,29 @@ class RegexDSLTests: XCTestCase {
12311231
}
12321232
}
12331233
}
1234+
1235+
// rdar://96280236
1236+
func testCharacterClassAnyCrash() {
1237+
let regex = Regex {
1238+
"{"
1239+
Capture {
1240+
OneOrMore {
1241+
CharacterClass.any.subtracting(.anyOf("}"))
1242+
}
1243+
}
1244+
"}"
1245+
}
1246+
1247+
func replace(_ template: String) throws -> String {
1248+
var b = template
1249+
while let result = try regex.firstMatch(in: b) {
1250+
b.replaceSubrange(result.range, with: "foo")
1251+
}
1252+
return b
1253+
}
1254+
1255+
XCTAssertEqual(try replace("{bar}"), "foo")
1256+
}
12341257
}
12351258

12361259
extension Unicode.Scalar {

0 commit comments

Comments
 (0)