|
11 | 11 |
|
12 | 12 | import XCTest
|
13 | 13 | @testable import _RegexParser
|
14 |
| -@testable @_spi(RegexBenchmark) import _StringProcessing |
| 14 | +@testable @_spi(RegexBenchmark) @_spi(Foundation) import _StringProcessing |
15 | 15 | import TestSupport
|
16 | 16 |
|
17 | 17 | struct MatchError: Error {
|
@@ -2726,4 +2726,40 @@ extension RegexTests {
|
2726 | 2726 | XCTAssertNotNil(str.wholeMatch(of: possessiveRegex))
|
2727 | 2727 | }
|
2728 | 2728 | }
|
| 2729 | + |
| 2730 | + func testNSRECompatibility() throws { |
| 2731 | + // NSRE-compatibility includes scalar matching, so `[\r\n]` should match |
| 2732 | + // either `\r` or `\n`. |
| 2733 | + let text = #""" |
| 2734 | + y=sin(x)+sin(2x)+sin(3x);\#rText "This is a function of x.";\r |
| 2735 | + """# |
| 2736 | + let lineTerminationRegex = try Regex(#";[\r\n]"#) |
| 2737 | + ._nsreCompatibility |
| 2738 | + |
| 2739 | + let afterLine = try XCTUnwrap(text.firstRange(of: "Text")) |
| 2740 | + let match = try lineTerminationRegex.firstMatch(in: text) |
| 2741 | + XCTAssert(match?.range.upperBound == afterLine.lowerBound) |
| 2742 | + |
| 2743 | + // NSRE-compatibility treats "dot" as special, in that it can match a |
| 2744 | + // newline sequence as well as a single Unicode scalar. |
| 2745 | + let aDotBRegex = try Regex(#"a.b"#) |
| 2746 | + ._nsreCompatibility |
| 2747 | + .dotMatchesNewlines() |
| 2748 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2749 | + XCTAssertNotNil(try aDotBRegex.wholeMatch(in: input)) |
| 2750 | + } |
| 2751 | + |
| 2752 | + // NSRE-compatibility doesn't give special treatment to newline sequences |
| 2753 | + // when matching other "match everything" regex patterns, like `[[^z]z]`, |
| 2754 | + // so this pattern doesn't match "a\r\nb". |
| 2755 | + let aCCBRegex = try Regex(#"a[[^z]z]b"#) |
| 2756 | + ._nsreCompatibility |
| 2757 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2758 | + if input.unicodeScalars.count == 3 { |
| 2759 | + XCTAssertNotNil(try aCCBRegex.wholeMatch(in: input)) |
| 2760 | + } else { |
| 2761 | + XCTAssertNil(try aCCBRegex.wholeMatch(in: input)) |
| 2762 | + } |
| 2763 | + } |
| 2764 | + } |
2729 | 2765 | }
|
0 commit comments