From f1a1812ff26544aa6b959aa0b12b5f66af397eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zhiyu=20Zhu/=E6=9C=B1=E6=99=BA=E8=AF=AD?= Date: Fri, 5 Apr 2019 15:01:00 -0400 Subject: [PATCH 1/4] Use synthesized implementation --- Sources/Schedule/Bag.swift | 5 ----- Sources/Schedule/Interval.swift | 8 +------- Sources/Schedule/TaskCenter.swift | 5 +---- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/Sources/Schedule/Bag.swift b/Sources/Schedule/Bag.swift index 3afac57..7305fd5 100644 --- a/Sources/Schedule/Bag.swift +++ b/Sources/Schedule/Bag.swift @@ -8,11 +8,6 @@ struct BagKey: Equatable { fileprivate init(underlying: UInt64) { self.i = underlying } - - /// Returns a Boolean value indicating whether two BagKeys are equal. - static func == (lhs: BagKey, rhs: BagKey) -> Bool { - return lhs.i == rhs.i - } } /// A generator that can generate a sequence of unique `BagKey`. diff --git a/Sources/Schedule/Interval.swift b/Sources/Schedule/Interval.swift index 417b331..d7e230b 100644 --- a/Sources/Schedule/Interval.swift +++ b/Sources/Schedule/Interval.swift @@ -12,13 +12,7 @@ public struct Interval { } } -extension Interval: Hashable { - - /// Returns a boolean value indicating whether two intervals are equal. - public static func == (lhs: Interval, rhs: Interval) -> Bool { - return lhs.nanoseconds == rhs.nanoseconds - } -} +extension Interval: Hashable { } extension Interval { diff --git a/Sources/Schedule/TaskCenter.swift b/Sources/Schedule/TaskCenter.swift index 7983684..8157d78 100644 --- a/Sources/Schedule/TaskCenter.swift +++ b/Sources/Schedule/TaskCenter.swift @@ -8,15 +8,12 @@ extension TaskCenter { weak var task: Task? - let hash: Int - init(_ task: Task) { self.task = task - self.hash = task.hashValue } func hash(into hasher: inout Hasher) { - hasher.combine(hash) + hasher.combine(task) } static func == (lhs: TaskBox, rhs: TaskBox) -> Bool { From 2ce6ec5db506f86675599532fa3dbfa1af5ee7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zhiyu=20Zhu/=E6=9C=B1=E6=99=BA=E8=AF=AD?= Date: Fri, 5 Apr 2019 15:02:15 -0400 Subject: [PATCH 2/4] Use NSRange properly --- Sources/Schedule/Period.swift | 2 +- Sources/Schedule/Time.swift | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Sources/Schedule/Period.swift b/Sources/Schedule/Period.swift index f2836e3..d1a0a98 100644 --- a/Sources/Schedule/Period.swift +++ b/Sources/Schedule/Period.swift @@ -72,7 +72,7 @@ public struct Period { let mark: Character = "秋" str = regexp.stringByReplacingMatches( in: str, - range: NSRange(location: 0, length: str.count), + range: NSRange(str.startIndex..., in: str), withTemplate: String(mark) ) diff --git a/Sources/Schedule/Time.swift b/Sources/Schedule/Time.swift index 2268d55..76d35e7 100644 --- a/Sources/Schedule/Time.swift +++ b/Sources/Schedule/Time.swift @@ -50,10 +50,11 @@ public struct Time { // swiftlint:disable force_try let regexp = try! NSRegularExpression(pattern: pattern, options: []) + let nsString = NSString(string: string) guard let matches = regexp.matches( in: string, options: [], - range: NSRange(location: 0, length: string.count)).first + range: NSRange(location: 0, length: nsString.length)).first else { return nil } @@ -65,7 +66,7 @@ public struct Time { for i in 0.. Date: Fri, 5 Apr 2019 15:06:10 -0400 Subject: [PATCH 3/4] Improve Swift collection handling --- Sources/Schedule/Plan.swift | 6 ++---- Sources/Schedule/Time.swift | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/Schedule/Plan.swift b/Sources/Schedule/Plan.swift index e01c162..75c5fc8 100644 --- a/Sources/Schedule/Plan.swift +++ b/Sources/Schedule/Plan.swift @@ -473,10 +473,8 @@ extension Plan { guard !mondays.isEmpty else { return .init(plan: .never) } var plan = every(mondays[0]).plan - if mondays.count > 1 { - for i in 1.. Date: Fri, 5 Apr 2019 17:03:55 -0400 Subject: [PATCH 4/4] Other simplifications --- Sources/Schedule/Bag.swift | 5 +---- Sources/Schedule/Period.swift | 2 +- Sources/Schedule/Plan.swift | 6 ++---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/Sources/Schedule/Bag.swift b/Sources/Schedule/Bag.swift index 7305fd5..d82a648 100644 --- a/Sources/Schedule/Bag.swift +++ b/Sources/Schedule/Bag.swift @@ -60,10 +60,7 @@ struct Bag { /// Returns the element associated with a given key. func value(for key: BagKey) -> Element? { - if let entry = entries.first(where: { $0.key == key }) { - return entry.val - } - return nil + return entries.first(where: { $0.key == key })?.val } /// Removes the given key and its associated element from this bag. diff --git a/Sources/Schedule/Period.swift b/Sources/Schedule/Period.swift index d1a0a98..12aa4ef 100644 --- a/Sources/Schedule/Period.swift +++ b/Sources/Schedule/Period.swift @@ -85,7 +85,7 @@ public struct Period { return nil } - var unit = String(pair[1]) + var unit = pair[1] if unit.last == "s" { unit.removeLast() } switch unit { case "year": period = period + number.years diff --git a/Sources/Schedule/Plan.swift b/Sources/Schedule/Plan.swift index 75c5fc8..0b62a6c 100644 --- a/Sources/Schedule/Plan.swift +++ b/Sources/Schedule/Plan.swift @@ -435,10 +435,8 @@ extension Plan { guard !weekdays.isEmpty else { return .init(plan: .never) } var plan = every(weekdays[0]).plan - if weekdays.count > 1 { - for i in 1..