Skip to content

Remove firstMatch and wholeMatch from String #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private func graphemeBreakPropertyData<RP: RegexComponent>(
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(
Expand All @@ -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 {
".."
Expand All @@ -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)
}
Expand Down
45 changes: 0 additions & 45 deletions Sources/RegexBuilder/Match.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,3 @@ extension Regex {
return nil
}
}

@available(SwiftStdlib 5.7, *)
extension String {
public func wholeMatch<R: RegexComponent>(
of r: R
) -> Regex<R.RegexOutput>.Match? {
try? r.regex.wholeMatch(in: self)
}

public func prefixMatch<R: RegexComponent>(
of r: R
) -> Regex<R.RegexOutput>.Match? {
try? r.regex.prefixMatch(in: self)
}
}

@available(SwiftStdlib 5.7, *)
extension Substring {
public func wholeMatch<R: RegexComponent>(
of r: R
) -> Regex<R.RegexOutput>.Match? {
try? r.regex.wholeMatch(in: self)
}

public func prefixMatch<R: RegexComponent>(
of r: R
) -> Regex<R.RegexOutput>.Match? {
try? r.regex.prefixMatch(in: self)
}
}
2 changes: 1 addition & 1 deletion Tests/RegexBuilderTests/CustomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func customTest<Match: Equatable>(
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
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import XCTest
import _StringProcessing
@testable import RegexBuilder

fileprivate extension StringProtocol where SubSequence == Substring {
func wholeMatch<R: RegexComponent>(of rc: R) -> Regex<R.RegexOutput>.Match? {
try? rc.regex.wholeMatch(in: self[...])
}

func wholeMatch<R: RegexComponent>(@RegexComponentBuilder _ content: () -> R) -> Regex<R.RegexOutput>.Match? {
wholeMatch(of: content())
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be API? @itingliu


class RegexDSLTests: XCTestCase {
func _testDSLCaptures<Content: RegexComponent, MatchType>(
_ tests: (input: String, expectedCaptures: MatchType?)...,
Expand Down