forked from nsoperations/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemeMatcher.swift
45 lines (36 loc) · 1.11 KB
/
SchemeMatcher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// SchemeMatcher.swift
// CarthageKit
//
// Created by Werner Altewischer on 26/04/2019.
//
import Foundation
import XCDBLD
public protocol SchemeMatcher {
func matches(scheme: Scheme) -> Bool
}
public class RegexSchemeMatcher: SchemeMatcher {
let include: Bool
let regex: NSRegularExpression
public init?(pattern: String, include: Bool) {
guard let regex = try? NSRegularExpression(pattern: pattern) else {
return nil
}
self.regex = regex
self.include = include
}
public func matches(scheme: Scheme) -> Bool {
let nsRange = NSRange(scheme.name.startIndex..<scheme.name.endIndex, in: scheme.name)
let regexMatch = regex.firstMatch(in: scheme.name, options: [], range: nsRange) != nil
return self.include ? regexMatch : !regexMatch
}
}
public class LitteralSchemeMatcher: SchemeMatcher {
let schemeNames: Set<String>
public init(schemeNames: Set<String>) {
self.schemeNames = schemeNames
}
public func matches(scheme: Scheme) -> Bool {
return self.schemeNames.contains(scheme.name)
}
}