Skip to content

Commit bebf6b2

Browse files
committed
Merge pull request swiftlang#546 from Azoy/fix-emitany
Fix crash when emitting an atom of any
1 parent be98b28 commit bebf6b2

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
@@ -1216,6 +1216,29 @@ class RegexDSLTests: XCTestCase {
12161216
}
12171217
}
12181218
}
1219+
1220+
// rdar://96280236
1221+
func testCharacterClassAnyCrash() {
1222+
let regex = Regex {
1223+
"{"
1224+
Capture {
1225+
OneOrMore {
1226+
CharacterClass.any.subtracting(.anyOf("}"))
1227+
}
1228+
}
1229+
"}"
1230+
}
1231+
1232+
func replace(_ template: String) throws -> String {
1233+
var b = template
1234+
while let result = try regex.firstMatch(in: b) {
1235+
b.replaceSubrange(result.range, with: "foo")
1236+
}
1237+
return b
1238+
}
1239+
1240+
XCTAssertEqual(try replace("{bar}"), "foo")
1241+
}
12191242
}
12201243

12211244
extension Unicode.Scalar {

0 commit comments

Comments
 (0)