Skip to content

Commit ac1df32

Browse files
authored
Merge pull request #11 from Dashlane/optimisation
Parse optimisation
2 parents f5b1b12 + d13e21a commit ac1df32

File tree

6 files changed

+22
-24
lines changed

6 files changed

+22
-24
lines changed

DomainParser/DomainParser/BasicRulesParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct BasicRulesParser {
1616
}
1717
public func parse(host: String) -> ParsedHost? {
1818
let lowercasedHost = host.lowercased()
19-
let hostComponents = lowercasedHost.components(separatedBy: ".")
19+
let hostComponents = lowercasedHost.split(separator: ".")
2020
var hostSlices = ArraySlice(hostComponents)
2121

2222
/// A host must have at least two parts else it's a TLD

DomainParser/DomainParser/DomainParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct DomainParser {
4141
}
4242

4343
func parseExceptionsAndWildCardRules(host: String) -> ParsedHost? {
44-
let hostComponents = host.components(separatedBy: ".")
44+
let hostComponents = host.split(separator: ".")
4545
let isMatching: (Rule) -> Bool = { $0.isMatching(hostLabels: hostComponents) }
4646
let rule = parsedRules.exceptions.first(where: isMatching) ?? parsedRules.wildcardRules.first(where: isMatching)
4747
return rule?.parse(hostLabels: hostComponents)

DomainParser/DomainParser/Model/Rule.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ struct Rule {
2323
/// Score used to sort the rules. If a URL match multiple rules, the one with the highest Score is prevailing
2424
let rankingScore: Int
2525

26-
init(raw: String) {
26+
init(raw: Substring) {
2727

2828
/// If the line starts with "!" it's an exceptional Rule
2929
exception = raw.starts(with: C.exceptionMarker)
30-
source = exception ? String(raw.dropFirst()) : raw
31-
parts = source.components(separatedBy: ".").map(RuleLabel.init)
30+
source = exception ? String(raw.dropFirst()) : String(raw)
31+
parts = source.split(separator: ".").map(RuleLabel.init)
3232

3333
/// Exceptions should have a higher Rank than regular rules
3434
rankingScore = (exception ? 1000 : 0) + parts.count
@@ -44,7 +44,7 @@ extension Rule {
4444
/// - Beginning with the right-most labels of both the domain and the rule,
4545
/// and continuing for all labels in the rule, one finds that for every pair,
4646
/// either they are identical, or that the label from the rule is "*".
47-
func isMatching(hostLabels: [String]) -> Bool {
47+
func isMatching(hostLabels: [Substring]) -> Bool {
4848
let delta = hostLabels.count - self.parts.count
4949

5050
/// The url should have at least the same number of labels than the url
@@ -55,7 +55,7 @@ extension Rule {
5555

5656
let zipped = zip(self.parts, trimmedHostLabels)
5757
/// Closure that check if a RuleLabel match a given string
58-
let matchingClosure:(RuleLabel, String) -> Bool = {ruleComponent, hostComponent in
58+
let matchingClosure:(RuleLabel, Substring) -> Bool = {ruleComponent, hostComponent in
5959
return ruleComponent.isMatching(label: hostComponent)
6060
}
6161

@@ -73,7 +73,7 @@ extension Rule {
7373

7474

7575
/// ⚠️ Should be called only for host matching the rule
76-
func parse(hostLabels: [String]) -> ParsedHost {
76+
func parse(hostLabels: [Substring]) -> ParsedHost {
7777
let partsCount = parts.count - (self.exception ? 1 : 0)
7878
let delta = hostLabels.count - partsCount
7979

DomainParser/DomainParser/Model/RuleLabel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ enum RuleLabel {
1414
/// Wildcards are not restricted to appear only in the leftmost position, but they must wildcard an entire label. (I.e. *.*.foo is a valid rule: *bar.foo is not.)
1515
case wildcard
1616

17-
init(fromComponent component: String) {
18-
self = component == Constant.wildcardComponent ? .wildcard : .text(component)
17+
init(fromComponent component: Substring) {
18+
self = component == Constant.wildcardComponent ? .wildcard : .text(String(component))
1919
}
2020

2121
/// Return true if self matches the given label
22-
func isMatching(label: String) -> Bool {
22+
func isMatching(label: Substring) -> Bool {
2323
switch self {
2424
case let .text(text):
2525
return text == label

DomainParser/DomainParser/RulesParser.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,20 @@ class RulesParser {
2222
throw DomainParserError.parsingError(details: nil)
2323
}
2424
rulesText
25-
.components(separatedBy: .newlines)
25+
.split(separator: "\n")
2626
.forEach(parseRule)
2727
return ParsedRules.init(exceptions: exceptions,
2828
wildcardRules: wildcardRules,
2929
basicRules: basicRules)
3030
}
31-
32-
private func parseRule(line: String) {
33-
guard let trimmedLine = line.components(separatedBy: .whitespaces).first,
34-
!trimmedLine.isComment && !trimmedLine.isEmpty else { return }
35-
36-
/// From `publicsuffix.org/list/` Each line is only read up to the first whitespace; entire lines can also be commented using //.
37-
if trimmedLine.contains("*") {
38-
wildcardRules.append(Rule(raw: trimmedLine))
39-
} else if trimmedLine.starts(with: "!") {
40-
exceptions.append(Rule(raw: trimmedLine))
31+
32+
private func parseRule(line: Substring) {
33+
if line.contains("*") {
34+
wildcardRules.append(Rule(raw: line))
35+
} else if line.starts(with: "!") {
36+
exceptions.append(Rule(raw: line))
4137
} else {
42-
basicRules.insert(trimmedLine)
38+
basicRules.insert(String(line))
4339
}
4440
}
4541
}

script/UpdatePSL.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ struct PublicSuffixListMinimifier {
4444
init(data: Data) {
4545
self.data = data
4646
}
47-
// A valid line is a non-empty, non-comment line
47+
48+
/// A valid line is a non-empty, non-comment line
4849
func isLineValid(line: String) -> Bool {
4950
return !line.isEmpty && !line.starts(with: "//")
5051
}
5152

5253
func minimify() throws -> Data {
5354
guard let stringifiedData = String.init(data: data, encoding: .utf8) else { throw ErrorType.notUTF8Convertible(data: data) }
5455

56+
// From `publicsuffix.org/list/` Each line is only read up to the first whitespace; entire lines can also be commented using //.
5557
let validLinesArray = stringifiedData.components(separatedBy: .newlines)
5658
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
5759
.compactMap { $0.components(separatedBy: CharacterSet.whitespaces).first }

0 commit comments

Comments
 (0)