|
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 { |
@@ -2717,4 +2717,40 @@ extension RegexTests { |
2717 | 2717 | XCTAssertNotNil(str.wholeMatch(of: possessiveRegex)) |
2718 | 2718 | } |
2719 | 2719 | } |
| 2720 | + |
| 2721 | + func testNSRECompatibility() throws { |
| 2722 | + // NSRE-compatibility includes scalar matching, so `[\r\n]` should match |
| 2723 | + // either `\r` or `\n`. |
| 2724 | + let text = #""" |
| 2725 | + y=sin(x)+sin(2x)+sin(3x);\#rText "This is a function of x.";\r |
| 2726 | + """# |
| 2727 | + let lineTerminationRegex = try Regex(#";[\r\n]"#) |
| 2728 | + ._nsreCompatibility |
| 2729 | + |
| 2730 | + let afterLine = try XCTUnwrap(text.firstRange(of: "Text")) |
| 2731 | + let match = try lineTerminationRegex.firstMatch(in: text) |
| 2732 | + XCTAssert(match?.range.upperBound == afterLine.lowerBound) |
| 2733 | + |
| 2734 | + // NSRE-compatibility treats "dot" as special, in that it can match a |
| 2735 | + // newline sequence as well as a single Unicode scalar. |
| 2736 | + let aDotBRegex = try Regex(#"a.b"#) |
| 2737 | + ._nsreCompatibility |
| 2738 | + .dotMatchesNewlines() |
| 2739 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2740 | + XCTAssertNotNil(try aDotBRegex.wholeMatch(in: input)) |
| 2741 | + } |
| 2742 | + |
| 2743 | + // NSRE-compatibility doesn't give special treatment to newline sequences |
| 2744 | + // when matching other "match everything" regex patterns, like `[[^z]z]`, |
| 2745 | + // so this pattern doesn't match "a\r\nb". |
| 2746 | + let aCCBRegex = try Regex(#"a[[^z]z]b"#) |
| 2747 | + ._nsreCompatibility |
| 2748 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2749 | + if input.unicodeScalars.count == 3 { |
| 2750 | + XCTAssertNotNil(try aCCBRegex.wholeMatch(in: input)) |
| 2751 | + } else { |
| 2752 | + XCTAssertNil(try aCCBRegex.wholeMatch(in: input)) |
| 2753 | + } |
| 2754 | + } |
| 2755 | + } |
2720 | 2756 | } |
0 commit comments