|
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 {
|
@@ -2749,13 +2749,41 @@ extension RegexTests {
|
2749 | 2749 | XCTAssertNotNil(str.wholeMatch(of: possessiveRegex))
|
2750 | 2750 | }
|
2751 | 2751 | }
|
2752 |
| - |
2753 |
| - func testFoo() throws { |
2754 |
| - let re = try Regex(#"^[\u{0000}-\u{024F}]+$"#) |
2755 |
| - |
2756 |
| - XCTAssertNotNil("aaa".wholeMatch(of: re)) |
2757 |
| - XCTAssertNotNil("aa\u{301}a".wholeMatch(of: re)) |
2758 |
| - XCTAssertNil("aa\u{301}\u{302}a".wholeMatch(of: re)) |
| 2752 | + |
| 2753 | + func testNSRECompatibility() throws { |
| 2754 | + // NSRE-compatibility includes scalar matching, so `[\r\n]` should match |
| 2755 | + // either `\r` or `\n`. |
| 2756 | + let text = #""" |
| 2757 | + y=sin(x)+sin(2x)+sin(3x);\#rText "This is a function of x.";\r |
| 2758 | + """# |
| 2759 | + let lineTerminationRegex = try Regex(#";[\r\n]"#) |
| 2760 | + ._nsreCompatibility |
| 2761 | + |
| 2762 | + let afterLine = try XCTUnwrap(text.firstRange(of: "Text")) |
| 2763 | + let match = try lineTerminationRegex.firstMatch(in: text) |
| 2764 | + XCTAssert(match?.range.upperBound == afterLine.lowerBound) |
| 2765 | + |
| 2766 | + // NSRE-compatibility treats "dot" as special, in that it can match a |
| 2767 | + // newline sequence as well as a single Unicode scalar. |
| 2768 | + let aDotBRegex = try Regex(#"a.b"#) |
| 2769 | + ._nsreCompatibility |
| 2770 | + .dotMatchesNewlines() |
| 2771 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2772 | + XCTAssertNotNil(try aDotBRegex.wholeMatch(in: input)) |
| 2773 | + } |
| 2774 | + |
| 2775 | + // NSRE-compatibility doesn't give special treatment to newline sequences |
| 2776 | + // when matching other "match everything" regex patterns, like `[[^z]z]`, |
| 2777 | + // so this pattern doesn't match "a\r\nb". |
| 2778 | + let aCCBRegex = try Regex(#"a[[^z]z]b"#) |
| 2779 | + ._nsreCompatibility |
| 2780 | + for input in ["a\rb", "a\nb", "a\r\nb"] { |
| 2781 | + if input.unicodeScalars.count == 3 { |
| 2782 | + XCTAssertNotNil(try aCCBRegex.wholeMatch(in: input)) |
| 2783 | + } else { |
| 2784 | + XCTAssertNil(try aCCBRegex.wholeMatch(in: input)) |
| 2785 | + } |
| 2786 | + } |
2759 | 2787 | }
|
2760 | 2788 | }
|
2761 | 2789 |
|
|
0 commit comments