Skip to content

Add Substring algorithms tests #289

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

Merged
merged 1 commit into from
Apr 18, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -61,6 +61,6 @@ extension BidirectionalCollection where SubSequence == Substring {
of r: R
) -> Regex<R.RegexOutput>.Match? {
let slice = self[...]
return try? r.regex.firstMatch(in: slice.base)
return try? r.regex.firstMatch(in: slice)
}
}
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ extension Regex {
/// Find the first match in a substring
///
/// Returns `nil` if no match is found and throws on abort
public func firstMatch(_ s: Substring) throws -> Regex<Output>.Match? {
public func firstMatch(in s: Substring) throws -> Regex<Output>.Match? {
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought Output was renamed to RegexOutput?

Copy link
Contributor

Choose a reason for hiding this comment

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

Regex's generic parameter is still named Output.

try _firstMatch(s.base, in: s.startIndex..<s.endIndex)
}

47 changes: 37 additions & 10 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
@@ -34,19 +34,11 @@ class RegexConsumerTests: XCTestCase {
) {
let regex = try! Regex(compiling: regex)

let actualSeq: [Range<Int>] = string[...].ranges(of: regex).map {
let start = string.offset(ofIndex: $0.lowerBound)
let end = string.offset(ofIndex: $0.upperBound)
return start..<end
}
let actualSeq: [Range<Int>] = string[...].ranges(of: regex).map(string.offsets(of:))
XCTAssertEqual(actualSeq, expected, file: file, line: line)

// `IndexingIterator` tests the collection conformance
let actualCol: [Range<Int>] = string[...].ranges(of: regex)[...].map {
let start = string.offset(ofIndex: $0.lowerBound)
let end = string.offset(ofIndex: $0.upperBound)
return start..<end
}
let actualCol: [Range<Int>] = string[...].ranges(of: regex)[...].map(string.offsets(of:))
XCTAssertEqual(actualCol, expected, file: file, line: line)
}

@@ -145,4 +137,39 @@ class RegexConsumerTests: XCTestCase {
XCTAssertEqual("x", "axb".trimming(r))
XCTAssertEqual("x", "axbb".trimming(r))
}

func testSubstring() throws {
let s = "aaa | aaaaaa | aaaaaaaaaa"
let s1 = s.dropFirst(6) // "aaaaaa | aaaaaaaaaa"
let s2 = s1.dropLast(17) // "aa"
let regex = try! Regex(compiling: "a+")

XCTAssertEqual(s.firstMatch(of: regex)?.0, "aaa")
XCTAssertEqual(s1.firstMatch(of: regex)?.0, "aaaaaa")
XCTAssertEqual(s2.firstMatch(of: regex)?.0, "aa")

XCTAssertEqual(
s.ranges(of: regex).map(s.offsets(of:)),
[0..<3, 6..<12, 15..<25])
XCTAssertEqual(
s1.ranges(of: regex).map(s.offsets(of:)),
[6..<12, 15..<25])
XCTAssertEqual(
s2.ranges(of: regex).map(s.offsets(of:)),
[6..<8])

XCTAssertEqual(s.replacing(regex, with: ""), " | | ")
XCTAssertEqual(s1.replacing(regex, with: ""), " | ")
XCTAssertEqual(s2.replacing(regex, with: ""), "")

XCTAssertEqual(
s._matches(of: regex).map(\.0),
["aaa", "aaaaaa", "aaaaaaaaaa"])
XCTAssertEqual(
s1._matches(of: regex).map(\.0),
["aaaaaa", "aaaaaaaaaa"])
XCTAssertEqual(
s2._matches(of: regex).map(\.0),
["aa"])
}
}