Skip to content

Commit

Permalink
Merge pull request #6 from JohnSundell/single-use-matchers
Browse files Browse the repository at this point in the history
Enable matchers to be single-use only
  • Loading branch information
JohnSundell committed May 11, 2019
2 parents 77282be + 11149b0 commit d357d12
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Sources/Sweep/Sweep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public struct Matcher {
/// identifier that started the session to be passed to the
/// matcher's handler.
public var terminators: [Terminator]
/// Whether this matcher should be allowed to handle multiple
/// matches, or if it's single-use only. Default value: true.
public var allowMultipleMatches: Bool
/// The handler to be called when a match was found. A match
/// is considered found when a substring appears between any
/// of the matcher's identifiers and its terminators.
Expand All @@ -103,9 +106,11 @@ public struct Matcher {
/// the documentation for each property for more information.
public init(identifiers: [Identifier],
terminators: [Terminator],
allowMultipleMatches: Bool = true,
handler: @escaping Handler) {
self.identifiers = identifiers
self.terminators = terminators
self.allowMultipleMatches = allowMultipleMatches
self.handler = handler
}
}
Expand All @@ -115,9 +120,11 @@ public extension Matcher {
/// identifier and terminator, rather than arrays of them.
init(identifier: Identifier,
terminator: Terminator,
allowMultipleMatches: Bool = true,
handler: @escaping Handler) {
self.init(identifiers: [identifier],
terminators: [terminator],
allowMultipleMatches: allowMultipleMatches,
handler: handler)
}
}
Expand Down Expand Up @@ -156,6 +163,16 @@ public extension StringProtocol where SubSequence == Substring {
var idleMatchers = matchers

for index in indices {
let noSessionsRemain = (
activeSessions.isEmpty &&
partialSessions.isEmpty &&
idleMatchers.isEmpty
)

guard !noSessionsRemain else {
return
}

activeSessions = activeSessions.compactMap { matcher, identifier, range in
let range = range.lowerBound...index
let match = self[range]
Expand All @@ -179,6 +196,10 @@ public extension StringProtocol where SubSequence == Substring {
let identifierLength = identifier.string.count
let lowerBound = self.index(range.lowerBound, offsetBy: -identifierLength)
matcher.handler(match, lowerBound...range.upperBound)

guard matcher.allowMultipleMatches else {
return nil
}
}

idleMatchers.append(matcher)
Expand Down
21 changes: 20 additions & 1 deletion Tests/SweepTests/SweepTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ final class SweepTests: XCTestCase {
XCTAssertEqual(ranges.b.map { string[$0] }, ["[[Second]]"])
}

func testDisallowingMultipleMatches() {
let string = "Some text <First> some other text <Second>, <Third>."
var matches = [Substring]()

string.scan(using: [
Matcher(
identifier: "<",
terminator: ">",
allowMultipleMatches: false,
handler: { match, _ in
matches.append(match)
}
)
])

XCTAssertEqual(matches, ["First"])
}

func testAllTestsRunOnLinux() {
verifyAllTestsRunOnLinux()
}
Expand All @@ -154,7 +172,8 @@ extension SweepTests: LinuxTestable {
("testIgnoringEmptyMatch", testIgnoringEmptyMatch),
("testHTMLScanning", testHTMLScanning),
("testMarkdownScanning", testMarkdownScanning),
("testMultipleMatchers", testMultipleMatchers)
("testMultipleMatchers", testMultipleMatchers),
("testDisallowingMultipleMatches", testDisallowingMultipleMatches)
]
}
}

0 comments on commit d357d12

Please sign in to comment.