Skip to content

Update contains and split with different parameter names #411

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
4 changes: 2 additions & 2 deletions Sources/RegexBuilder/Algorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension BidirectionalCollection where SubSequence == Substring {
public func contains<R: RegexComponent>(
@RegexComponentBuilder _ content: () -> R
) -> Bool {
contains(content())
contains(pattern: content())
}

/// Returns the range of the first match for the regex within this collection,
Expand Down Expand Up @@ -104,7 +104,7 @@ extension BidirectionalCollection where SubSequence == Substring {
omittingEmptySubsequences: Bool = true,
@RegexComponentBuilder separator: () -> R
) -> [SubSequence] {
split(separator: separator(), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
split(pattern: separator(), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
}

/// Returns a Boolean value indicating whether the initial elements of this
Expand Down
18 changes: 2 additions & 16 deletions Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Collection where Element: Equatable {
/// - Returns: `true` if the collection contains the specified sequence,
/// otherwise `false`.
@available(SwiftStdlib 5.7, *)
public func contains<C: Collection>(_ other: C) -> Bool
public func contains<C: Collection>(pattern other: C) -> Bool
where C.Element == Element
{
firstRange(of: other) != nil
Expand All @@ -46,20 +46,6 @@ extension BidirectionalCollection where Element: Comparable {
}
}

// Overload breakers

extension StringProtocol {
@available(SwiftStdlib 5.7, *)
public func contains(_ other: String) -> Bool {
firstRange(of: other) != nil
}

@available(SwiftStdlib 5.7, *)
public func contains(_ other: Substring) -> Bool {
firstRange(of: other) != nil
}
}

// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
Expand All @@ -69,7 +55,7 @@ extension BidirectionalCollection where SubSequence == Substring {
/// - Returns: `true` if the regex was found in the collection, otherwise
/// `false`.
@available(SwiftStdlib 5.7, *)
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
public func contains<R: RegexComponent>(pattern regex: R) -> Bool {
contains(RegexConsumer(regex))
}
}
8 changes: 4 additions & 4 deletions Sources/_StringProcessing/Algorithms/Algorithms/Split.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ extension Collection where Element: Equatable {
/// elements.
@available(SwiftStdlib 5.7, *)
public func split<C: Collection>(
separator: C,
pattern: C,
Copy link
Contributor

Choose a reason for hiding this comment

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

For split, I think calling this parameter pattern would make its role unclear. Have you considered separatorPattern?

maxSplits: Int = .max,
omittingEmptySubsequences: Bool = true
) -> [SubSequence] where C.Element == Element {
Array(split(by: ZSearcher(pattern: Array(separator), by: ==), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences))
Array(split(by: ZSearcher(pattern: Array(pattern), by: ==), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences))
}
}

Expand Down Expand Up @@ -384,10 +384,10 @@ extension BidirectionalCollection where SubSequence == Substring {
/// elements.
@_disfavoredOverload
public func split<R: RegexComponent>(
separator: R,
pattern: R,
maxSplits: Int = .max,
omittingEmptySubsequences: Bool = true
) -> [SubSequence] {
Array(split(by: RegexConsumer(separator), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences))
Array(split(by: RegexConsumer(pattern), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences))
}
}
8 changes: 4 additions & 4 deletions Tests/RegexTests/AlgorithmsInternalsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ extension AlgorithmTests {
func testAdHoc() {
let r = try! Regex("a|b+")

XCTAssert("palindrome".contains(r))
XCTAssert("botany".contains(r))
XCTAssert("antiquing".contains(r))
XCTAssertFalse("cdef".contains(r))
XCTAssert("palindrome".contains(pattern: r))
XCTAssert("botany".contains(pattern: r))
XCTAssert("antiquing".contains(pattern: r))
XCTAssertFalse("cdef".contains(pattern: r))

let str = "a string with the letter b in it"
let first = str.firstRange(of: r)
Expand Down
34 changes: 17 additions & 17 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class AlgorithmTests: XCTestCase {

for start in 0..<9 {
for end in start..<9 {
XCTAssertTrue((0..<10).contains(start...end))
XCTAssertFalse((0..<10).contains(start...10))
XCTAssertTrue((0..<10).contains(pattern: start...end))
XCTAssertFalse((0..<10).contains(pattern: start...10))
}
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ class AlgorithmTests: XCTestCase {
file: StaticString = #file, line: UInt = #line
) {
let regex = try! Regex(regex)
let actual = Array(string.split(separator: regex, omittingEmptySubsequences: false))
let actual = Array(string.split(pattern: regex, omittingEmptySubsequences: false))
XCTAssertEqual(actual, expected, file: file, line: line)
}

Expand All @@ -136,8 +136,8 @@ class AlgorithmTests: XCTestCase {
expectSplit("a____a____a", "_+", ["a", "a", "a"])
expectSplit("____a____a____a____", "_+", ["", "a", "a", "a", ""])

XCTAssertEqual("".split(separator: ""), [])
XCTAssertEqual("".split(separator: "", omittingEmptySubsequences: false), [""])
XCTAssertEqual("".split(pattern: ""), [])
XCTAssertEqual("".split(pattern: "", omittingEmptySubsequences: false), [""])

// Test that original `split` functions are still accessible
let splitRef = "abcd".split
Expand All @@ -149,36 +149,36 @@ class AlgorithmTests: XCTestCase {
func testSplitPermutations() throws {
let splitRegex = try Regex(#"\|"#)
XCTAssertEqual(
"a|a|||a|a".split(separator: splitRegex),
"a|a|||a|a".split(pattern: splitRegex),
["a", "a", "a", "a"])
XCTAssertEqual(
"a|a|||a|a".split(separator: splitRegex, omittingEmptySubsequences: false),
"a|a|||a|a".split(pattern: splitRegex, omittingEmptySubsequences: false),
["a", "a", "", "", "a", "a"])
XCTAssertEqual(
"a|a|||a|a".split(separator: splitRegex, maxSplits: 2),
"a|a|||a|a".split(pattern: splitRegex, maxSplits: 2),
["a", "a", "||a|a"])

XCTAssertEqual(
"a|a|||a|a|||a|a|||".split(separator: "|||"),
"a|a|||a|a|||a|a|||".split(pattern: "|||"),
["a|a", "a|a", "a|a"])
XCTAssertEqual(
"a|a|||a|a|||a|a|||".split(separator: "|||", omittingEmptySubsequences: false),
"a|a|||a|a|||a|a|||".split(pattern: "|||", omittingEmptySubsequences: false),
["a|a", "a|a", "a|a", ""])
XCTAssertEqual(
"a|a|||a|a|||a|a|||".split(separator: "|||", maxSplits: 2),
"a|a|||a|a|||a|a|||".split(pattern: "|||", maxSplits: 2),
["a|a", "a|a", "a|a|||"])

XCTAssertEqual(
"aaaa".split(separator: ""),
"aaaa".split(pattern: ""),
["a", "a", "a", "a"])
XCTAssertEqual(
"aaaa".split(separator: "", omittingEmptySubsequences: false),
"aaaa".split(pattern: "", omittingEmptySubsequences: false),
["", "a", "a", "a", "a", ""])
XCTAssertEqual(
"aaaa".split(separator: "", maxSplits: 2),
"aaaa".split(pattern: "", maxSplits: 2),
["a", "a", "aa"])
XCTAssertEqual(
"aaaa".split(separator: "", maxSplits: 2, omittingEmptySubsequences: false),
"aaaa".split(pattern: "", maxSplits: 2, omittingEmptySubsequences: false),
["", "a", "aaa"])

// Fuzzing the input and parameters
Expand All @@ -205,11 +205,11 @@ class AlgorithmTests: XCTestCase {
maxSplits: maxSplits,
omittingEmptySubsequences: omitEmpty)
let regexActual = str.split(
separator: splitRegex,
pattern: splitRegex,
maxSplits: maxSplits,
omittingEmptySubsequences: omitEmpty)
let stringActual = str.split(
separator: "|" as String,
pattern: "|",
maxSplits: maxSplits,
omittingEmptySubsequences: omitEmpty)
XCTAssertEqual(regexActual, expected, """
Expand Down
12 changes: 6 additions & 6 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1352,12 +1352,12 @@ extension RegexTests {
04: Arkansas
05: California
"""
XCTAssertTrue(string.contains(try Regex(#"^\d+"#)))
XCTAssertTrue(string.contains(pattern: try Regex(#"^\d+"#)))
XCTAssertEqual(string.ranges(of: try Regex(#"^\d+"#)).count, 1)
XCTAssertEqual(string.ranges(of: try Regex(#"(?m)^\d+"#)).count, 5)

let regex = try Regex(#"^\d+: [\w ]+$"#)
XCTAssertFalse(string.contains(regex))
XCTAssertFalse(string.contains(pattern: regex))
let allRanges = string.ranges(of: regex.anchorsMatchLineEndings())
XCTAssertEqual(allRanges.count, 5)
}
Expand Down Expand Up @@ -1396,12 +1396,12 @@ extension RegexTests {

func testOptionMethods() throws {
let regex = try Regex("c.f.")
XCTAssertTrue ("cafe".contains(regex))
XCTAssertFalse("CaFe".contains(regex))
XCTAssertTrue ("cafe".contains(pattern: regex))
XCTAssertFalse("CaFe".contains(pattern: regex))

let caseInsensitiveRegex = regex.ignoresCase()
XCTAssertTrue("cafe".contains(caseInsensitiveRegex))
XCTAssertTrue("CaFe".contains(caseInsensitiveRegex))
XCTAssertTrue("cafe".contains(pattern: caseInsensitiveRegex))
XCTAssertTrue("CaFe".contains(pattern: caseInsensitiveRegex))
}

// MARK: Character Semantics
Expand Down
Loading