From 812c394771f0be9ecbcfe1296e2a7853f54abc60 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Tue, 17 May 2022 12:31:01 -0500 Subject: [PATCH] Keep substring bounds when searching in Regex.wholeMatch --- Sources/_StringProcessing/Regex/Match.swift | 2 +- Tests/RegexBuilderTests/AlgorithmsTests.swift | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/_StringProcessing/Regex/Match.swift b/Sources/_StringProcessing/Regex/Match.swift index e2c6e1a87..3a9f37b60 100644 --- a/Sources/_StringProcessing/Regex/Match.swift +++ b/Sources/_StringProcessing/Regex/Match.swift @@ -177,7 +177,7 @@ extension BidirectionalCollection where SubSequence == Substring { public func wholeMatch( of r: R ) -> Regex.Match? { - try? r.regex.wholeMatch(in: self[...].base) + try? r.regex.wholeMatch(in: self[...]) } /// Checks for a match against the string, starting at its beginning. diff --git a/Tests/RegexBuilderTests/AlgorithmsTests.swift b/Tests/RegexBuilderTests/AlgorithmsTests.swift index 4545e8b1f..def55bc17 100644 --- a/Tests/RegexBuilderTests/AlgorithmsTests.swift +++ b/Tests/RegexBuilderTests/AlgorithmsTests.swift @@ -229,6 +229,16 @@ class AlgorithmsResultBuilderTests: XCTestCase { } func testMatches() throws { + do { + let regex = Regex { OneOrMore(.any) } + XCTAssertEqual("abc".wholeMatch(of: regex)!.0, "abc") + XCTAssertEqual("abc".prefixMatch(of: regex)!.0, "abc") + XCTAssertEqual("abc".firstMatch(of: regex)!.0, "abc") + XCTAssertEqual("abc".suffix(1).wholeMatch(of: regex)!.0, "c") + XCTAssertEqual("abc".suffix(1).prefixMatch(of: regex)!.0, "c") + XCTAssertEqual("abc".suffix(1).firstMatch(of: regex)!.0, "c") + } + let int = Capture(OneOrMore(.digit)) { Int($0)! } // Test syntax