diff --git a/Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift b/Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift index f1861fcf2..853c73271 100644 --- a/Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift +++ b/Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift @@ -175,12 +175,24 @@ extension BidirectionalCollection { // MARK: Fixed pattern algorithms extension Collection where Element: Equatable { - // FIXME: Replace `RangesCollection` when SE-0346 is enabled func ranges( of other: S ) -> RangesCollection> where S.Element == Element { ranges(of: ZSearcher(pattern: Array(other), by: ==)) } + + // FIXME: Return `some Collection>` for SE-0346 + /// Finds and returns the ranges of the all occurrences of a given sequence + /// within the collection. + /// - Parameter other: The sequence to search for. + /// - Returns: A collection of ranges of all occurrences of `other`. Returns + /// an empty collection if `other` is not found. + @available(SwiftStdlib 5.7, *) + public func ranges( + of other: S + ) -> [Range] where S.Element == Element { + ranges(of: ZSearcher(pattern: Array(other), by: ==)).map { $0 } + } } extension BidirectionalCollection where Element: Equatable { @@ -217,8 +229,8 @@ extension BidirectionalCollection where Element: Comparable { // MARK: Regex algorithms extension BidirectionalCollection where SubSequence == Substring { - // FIXME: Replace `RangesCollection` when SE-0346 is enabled @available(SwiftStdlib 5.7, *) + @_disfavoredOverload func ranges( of regex: R ) -> RangesCollection> { @@ -231,4 +243,17 @@ extension BidirectionalCollection where SubSequence == Substring { ) -> ReversedRangesCollection> { rangesFromBack(of: RegexConsumer(regex)) } + + // FIXME: Return `some Collection>` for SE-0346 + /// Finds and returns the ranges of the all occurrences of a given sequence + /// within the collection. + /// - Parameter regex: The regex to search for. + /// - Returns: A collection or ranges in the receiver of all occurrences of + /// `regex`. Returns an empty collection if `regex` is not found. + @available(SwiftStdlib 5.7, *) + public func ranges( + of regex: R + ) -> [Range] { + Array(ranges(of: RegexConsumer(regex))) + } } diff --git a/Sources/_StringProcessing/Algorithms/Algorithms/Split.swift b/Sources/_StringProcessing/Algorithms/Algorithms/Split.swift index 485bc3b7f..8c7a9832d 100644 --- a/Sources/_StringProcessing/Algorithms/Algorithms/Split.swift +++ b/Sources/_StringProcessing/Algorithms/Algorithms/Split.swift @@ -233,16 +233,24 @@ extension BidirectionalCollection where Element: Equatable { // MARK: Fixed pattern algorithms extension Collection where Element: Equatable { - // FIXME: Replace `SplitCollection` when SE-0346 is enabled + @_disfavoredOverload + func split( + by separator: S + ) -> SplitCollection> where S.Element == Element { + split(by: ZSearcher(pattern: Array(separator), by: ==)) + } + + // FIXME: Return `some Collection` for SE-0346 /// Returns the longest possible subsequences of the collection, in order, /// around elements equal to the given separator. /// - Parameter separator: The element to be split upon. /// - Returns: A collection of subsequences, split from this collection's /// elements. - func split( + @available(SwiftStdlib 5.7, *) + public func split( by separator: S - ) -> SplitCollection> where S.Element == Element { - split(by: ZSearcher(pattern: Array(separator), by: ==)) + ) -> [SubSequence] where S.Element == Element { + Array(split(by: ZSearcher(pattern: Array(separator), by: ==))) } } @@ -282,12 +290,7 @@ extension BidirectionalCollection where Element: Comparable { @available(SwiftStdlib 5.7, *) extension BidirectionalCollection where SubSequence == Substring { - // FIXME: Replace `SplitCollection` when SE-0346 is enabled - /// Returns the longest possible subsequences of the collection, in order, - /// around elements equal to the given separator. - /// - Parameter separator: A regex describing elements to be split upon. - /// - Returns: A collection of substrings, split from this collection's - /// elements. + @_disfavoredOverload func split( by separator: R ) -> SplitCollection> { @@ -299,4 +302,16 @@ extension BidirectionalCollection where SubSequence == Substring { ) -> ReversedSplitCollection> { splitFromBack(by: RegexConsumer(separator)) } + + // FIXME: Return `some Collection` for SE-0346 + /// Returns the longest possible subsequences of the collection, in order, + /// around elements equal to the given separator. + /// - Parameter separator: A regex describing elements to be split upon. + /// - Returns: A collection of substrings, split from this collection's + /// elements. + public func split( + by separator: R + ) -> [SubSequence] { + Array(split(by: RegexConsumer(separator))) + } } diff --git a/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift b/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift index 8485182de..09e021a29 100644 --- a/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift +++ b/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift @@ -139,7 +139,7 @@ extension RangeReplaceableCollection where SubSequence == Substring { var result = Self() result.append(contentsOf: self[..( of regex: R ) -> MatchesCollection> { @@ -202,10 +199,14 @@ extension BidirectionalCollection where SubSequence == Substring { matchesFromBack(of: RegexConsumer(regex)) } - // FIXME: Replace the returned value as `some Collection.Match> - // when SE-0346 is enabled + // FIXME: Return `some Collection.Match> for SE-0346 + /// Returns a collection containing all matches of the specified regex. + /// - Parameter regex: The regex to search for. + /// - Returns: A collection of matches of `regex`. @available(SwiftStdlib 5.7, *) - func _matches(of r: R) -> [Regex.Match] { + public func matches( + of r: R + ) -> [Regex.Match] { let slice = self[...] var start = self.startIndex let end = self.endIndex diff --git a/Tests/RegexBuilderTests/AlgorithmsTests.swift b/Tests/RegexBuilderTests/AlgorithmsTests.swift index cf117690a..793054cd1 100644 --- a/Tests/RegexBuilderTests/AlgorithmsTests.swift +++ b/Tests/RegexBuilderTests/AlgorithmsTests.swift @@ -15,13 +15,11 @@ import _StringProcessing @available(SwiftStdlib 5.7, *) class RegexConsumerTests: XCTestCase { - // FIXME: enable this test when we update the return type of `matches(of:)` - // when SE-0346 is available - // func testMatches() { - // let regex = Capture(OneOrMore(.digit)) { 2 * Int($0)! } - // let str = "foo 160 bar 99 baz" - // XCTAssertEqual(str.matches(of: regex).map(\.result.1), [320, 198]) - // } + func testMatches() { + let regex = Capture(OneOrMore(.digit)) { 2 * Int($0)! } + let str = "foo 160 bar 99 baz" + XCTAssertEqual(str.matches(of: regex).map(\.output.1), [320, 198]) + } func testMatchReplace() { func replaceTest( diff --git a/Tests/RegexTests/AlgorithmsTests.swift b/Tests/RegexTests/AlgorithmsTests.swift index 5c3c34f89..a788ad13c 100644 --- a/Tests/RegexTests/AlgorithmsTests.swift +++ b/Tests/RegexTests/AlgorithmsTests.swift @@ -163,13 +163,13 @@ class RegexConsumerTests: XCTestCase { XCTAssertEqual(s2.replacing(regex, with: ""), "") XCTAssertEqual( - s._matches(of: regex).map(\.0), + s.matches(of: regex).map(\.0), ["aaa", "aaaaaa", "aaaaaaaaaa"]) XCTAssertEqual( - s1._matches(of: regex).map(\.0), + s1.matches(of: regex).map(\.0), ["aaaaaa", "aaaaaaaaaa"]) XCTAssertEqual( - s2._matches(of: regex).map(\.0), + s2.matches(of: regex).map(\.0), ["aa"]) } }