-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify coding if it's using standard values
If the region's values are equivalent to the standard values, then we only really to encode the identifiers and not the full object
- Loading branch information
1 parent
e3fe2d9
commit 2e959fe
Showing
5 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Dave DeLong on 2/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Calendar { | ||
|
||
func isEquivalent(to other: Calendar) -> Bool { | ||
guard identifier == other.identifier else { return false } | ||
guard timeZone.isEquivalent(to: other.timeZone) else { return false } | ||
guard firstWeekday == other.firstWeekday else { return false } | ||
guard minimumDaysInFirstWeek == other.minimumDaysInFirstWeek else { return false } | ||
|
||
return true | ||
} | ||
|
||
} | ||
|
||
extension TimeZone { | ||
|
||
func isEquivalent(to other: TimeZone) -> Bool { | ||
guard identifier == other.identifier else { return false } | ||
|
||
return true | ||
} | ||
|
||
} | ||
|
||
extension Locale { | ||
|
||
func isEquivalent(to other: Locale) -> Bool { | ||
guard calendar.identifier == other.calendar.identifier else { return false } | ||
guard collation == other.collation else { return false } | ||
guard currency == other.currency else { return false } | ||
guard firstDayOfWeek == other.firstDayOfWeek else { return false } | ||
guard hourCycle == other.hourCycle else { return false } | ||
|
||
guard measurementSystem == other.measurementSystem else { return false } | ||
guard numberingSystem == other.numberingSystem else { return false } | ||
guard region == other.region else { return false } | ||
guard subdivision == other.subdivision else { return false } | ||
guard variant == other.variant else { return false } | ||
|
||
guard language == other.language else { return false } | ||
|
||
return true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Dave DeLong on 2/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Locale { | ||
private static let cache = SimpleCache<String, Locale>() | ||
|
||
static func standard(_ id: String) -> Locale { | ||
return cache.get(id, create: { Locale(identifier: id) }) | ||
} | ||
} | ||
|
||
extension Calendar { | ||
private static let cache = SimpleCache<Calendar.Identifier, Calendar>() | ||
|
||
static func standard(_ id: Calendar.Identifier) -> Calendar { | ||
return cache.get(id, create: { Calendar(identifier: id) }) | ||
} | ||
} | ||
|
||
extension TimeZone { | ||
private static let cache = SimpleCache<String, TimeZone>() | ||
|
||
static func standard(_ id: String) -> TimeZone { | ||
return cache.get(id, create: { TimeZone(identifier: id)! }) | ||
} | ||
} | ||
|
||
private class SimpleCache<Key: Hashable, T> { | ||
|
||
private var storage = Dictionary<Key, T>() | ||
|
||
#warning("TODO: better synchronization primitive?") | ||
private let lock = NSLock() | ||
|
||
init() { } | ||
|
||
func get(_ id: Key, create: () -> T) -> T { | ||
lock.lock() | ||
|
||
let returnValue: T | ||
if let existing = storage[id] { | ||
returnValue = existing | ||
} else { | ||
returnValue = create() | ||
storage[id] = returnValue | ||
} | ||
|
||
lock.unlock() | ||
return returnValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters