From 21f85e62063250f13afe4facf86532d2bff6f429 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Wed, 16 Feb 2022 12:55:19 -0800 Subject: [PATCH] Allow `.0` when `RegexMatch` has a non-tuple `Match`. This is a hacky way of allowing the match result of a non-capturing regex to have a `.0` member. --- Sources/_StringProcessing/RegexDSL/Match.swift | 8 ++++++++ Tests/RegexTests/RegexDSLTests.swift | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/Sources/_StringProcessing/RegexDSL/Match.swift b/Sources/_StringProcessing/RegexDSL/Match.swift index bf9e01f8a..4ebc99de6 100644 --- a/Sources/_StringProcessing/RegexDSL/Match.swift +++ b/Sources/_StringProcessing/RegexDSL/Match.swift @@ -17,6 +17,14 @@ public struct RegexMatch { public subscript(dynamicMember keyPath: KeyPath) -> T { match[keyPath: keyPath] } + + // Allows `.0` when `Match` is not a tuple. + @_disfavoredOverload + public subscript( + dynamicMember keyPath: KeyPath<(Match, _doNotUse: ()), Match> + ) -> Match { + match + } } extension RegexProtocol { diff --git a/Tests/RegexTests/RegexDSLTests.swift b/Tests/RegexTests/RegexDSLTests.swift index 749fd8b42..121ff2624 100644 --- a/Tests/RegexTests/RegexDSLTests.swift +++ b/Tests/RegexTests/RegexDSLTests.swift @@ -65,6 +65,11 @@ class RegexDSLTests: XCTestCase { } } + func testMatchResultDotZeroWithoutCapture() throws { + let match = try XCTUnwrap("aaa".match { oneOrMore { "a" } }) + XCTAssertEqual(match.0, "aaa") + } + func testAlternation() throws { do { let regex = choiceOf {