From a9cb86810290cb588a7993042672034be2eee4da Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Fri, 15 Apr 2022 08:08:09 -0500 Subject: [PATCH] Remove firstMatch and wholeMatch from String These methods are available directly on Regex with the caller/callee swapped, having them on String confuses the intended user-facing Algorithms APIs. --- .../Participants/RegexParticipant.swift | 7 +-- Sources/RegexBuilder/Match.swift | 45 ------------------- Sources/_StringProcessing/Regex/Match.swift | 30 ------------- Tests/RegexBuilderTests/CustomTests.swift | 2 +- Tests/RegexBuilderTests/RegexDSLTests.swift | 10 +++++ 5 files changed, 15 insertions(+), 79 deletions(-) delete mode 100644 Sources/RegexBuilder/Match.swift diff --git a/Sources/Exercises/Participants/RegexParticipant.swift b/Sources/Exercises/Participants/RegexParticipant.swift index 6c53b3adf..aabde2e25 100644 --- a/Sources/Exercises/Participants/RegexParticipant.swift +++ b/Sources/Exercises/Participants/RegexParticipant.swift @@ -63,7 +63,7 @@ private func graphemeBreakPropertyData( forLine line: String, using regex: RP ) -> GraphemeBreakEntry? where RP.RegexOutput == (Substring, Substring, Substring?, Substring) { - line.wholeMatch(of: regex).map(\.output).flatMap(extractFromCaptures) + try? regex.regex.wholeMatch(in: line).map(\.output).flatMap(extractFromCaptures) } private func graphemeBreakPropertyDataLiteral( @@ -80,7 +80,7 @@ private func graphemeBreakPropertyDataLiteral( private func graphemeBreakPropertyData( forLine line: String ) -> GraphemeBreakEntry? { - line.wholeMatch { + let regex = Regex { TryCapture(OneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) } Optionally { ".." @@ -91,7 +91,8 @@ private func graphemeBreakPropertyData( OneOrMore(.whitespace) TryCapture(OneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) } ZeroOrMore(.any) - }.map { + } + return try? regex.wholeMatch(in: line).map { let (_, lower, upper, property) = $0.output return GraphemeBreakEntry(lower...(upper ?? lower), property) } diff --git a/Sources/RegexBuilder/Match.swift b/Sources/RegexBuilder/Match.swift deleted file mode 100644 index 78a466a18..000000000 --- a/Sources/RegexBuilder/Match.swift +++ /dev/null @@ -1,45 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// -//===----------------------------------------------------------------------===// - -import _StringProcessing - -@available(SwiftStdlib 5.7, *) -extension String { - @available(SwiftStdlib 5.7, *) - public func wholeMatch( - @RegexComponentBuilder of content: () -> R - ) -> Regex.Match? { - wholeMatch(of: content()) - } - - @available(SwiftStdlib 5.7, *) - public func prefixMatch( - @RegexComponentBuilder of content: () -> R - ) -> Regex.Match? { - prefixMatch(of: content()) - } -} - -extension Substring { - @available(SwiftStdlib 5.7, *) - public func wholeMatch( - @RegexComponentBuilder of content: () -> R - ) -> Regex.Match? { - wholeMatch(of: content()) - } - - @available(SwiftStdlib 5.7, *) - public func prefixMatch( - @RegexComponentBuilder of content: () -> R - ) -> Regex.Match? { - prefixMatch(of: content()) - } -} diff --git a/Sources/_StringProcessing/Regex/Match.swift b/Sources/_StringProcessing/Regex/Match.swift index 251febc87..93d78a409 100644 --- a/Sources/_StringProcessing/Regex/Match.swift +++ b/Sources/_StringProcessing/Regex/Match.swift @@ -154,33 +154,3 @@ extension Regex { return nil } } - -@available(SwiftStdlib 5.7, *) -extension String { - public func wholeMatch( - of r: R - ) -> Regex.Match? { - try? r.regex.wholeMatch(in: self) - } - - public func prefixMatch( - of r: R - ) -> Regex.Match? { - try? r.regex.prefixMatch(in: self) - } -} - -@available(SwiftStdlib 5.7, *) -extension Substring { - public func wholeMatch( - of r: R - ) -> Regex.Match? { - try? r.regex.wholeMatch(in: self) - } - - public func prefixMatch( - of r: R - ) -> Regex.Match? { - try? r.regex.prefixMatch(in: self) - } -} diff --git a/Tests/RegexBuilderTests/CustomTests.swift b/Tests/RegexBuilderTests/CustomTests.swift index 0ac6b46c5..c90ff4d2e 100644 --- a/Tests/RegexBuilderTests/CustomTests.swift +++ b/Tests/RegexBuilderTests/CustomTests.swift @@ -62,7 +62,7 @@ func customTest( let result: Match? switch call { case .match: - result = input.wholeMatch(of: regex)?.output + result = try? regex.wholeMatch(in: input)?.output case .firstMatch: result = input.firstMatch(of: regex)?.output } diff --git a/Tests/RegexBuilderTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift index b38b82a33..8480a3a0c 100644 --- a/Tests/RegexBuilderTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -13,6 +13,16 @@ import XCTest import _StringProcessing @testable import RegexBuilder +fileprivate extension StringProtocol where SubSequence == Substring { + func wholeMatch(of rc: R) -> Regex.Match? { + try? rc.regex.wholeMatch(in: self[...]) + } + + func wholeMatch(@RegexComponentBuilder _ content: () -> R) -> Regex.Match? { + wholeMatch(of: content()) + } +} + class RegexDSLTests: XCTestCase { func _testDSLCaptures( _ tests: (input: String, expectedCaptures: MatchType?)...,