Skip to content

Commit 6a62ebd

Browse files
authored
Merge pull request swiftlang#550 from Azoy/fix-emitany-57
[5.7] Fix crash when emitting an atom of any
2 parents e2de2f8 + bebf6b2 commit 6a62ebd

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
@@ -1237,6 +1237,29 @@ class RegexDSLTests: XCTestCase {
12371237
}
12381238
}
12391239
}
1240+
1241+
// rdar://96280236
1242+
func testCharacterClassAnyCrash() {
1243+
let regex = Regex {
1244+
"{"
1245+
Capture {
1246+
OneOrMore {
1247+
CharacterClass.any.subtracting(.anyOf("}"))
1248+
}
1249+
}
1250+
"}"
1251+
}
1252+
1253+
func replace(_ template: String) throws -> String {
1254+
var b = template
1255+
while let result = try regex.firstMatch(in: b) {
1256+
b.replaceSubrange(result.range, with: "foo")
1257+
}
1258+
return b
1259+
}
1260+
1261+
XCTAssertEqual(try replace("{bar}"), "foo")
1262+
}
12401263
}
12411264

12421265
extension Unicode.Scalar {

0 commit comments

Comments
 (0)