Skip to content

Commit 7cd3569

Browse files
authored
Merge pull request #189 from sebj/feeddateformatter-optimization
Small optimisations to `FeedDateFormatter`
2 parents f8ee173 + 62ec8f9 commit 7cd3569

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

Sources/FeedKit/FeedDateFormatter.swift

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ final class RFC3339DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
141141

142142
/// Formatter for RFC822 date specification with backup formats.
143143
final class RFC822DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
144+
// MARK: Internal
145+
144146
/// List of date formats supported for RFC822.
145147
override var dateFormats: [String] {
146148
[
@@ -180,8 +182,7 @@ final class RFC822DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
180182
// handle these in full compliance with Unicode tr35-31. For example,
181183
// "Tues, 6 November 2007 12:00:00 GMT" is rejected because of the "Tues",
182184
// even though "Tues" is used as an example for EEE in tr35-31.
183-
let trimRegEx = try! NSRegularExpression(pattern: "^[a-zA-Z]+, ([\\w :+-]+)$")
184-
let trimmed = trimRegEx.stringByReplacingMatches(
185+
let trimmed = Self.trimRegEx.stringByReplacingMatches(
185186
in: string,
186187
options: [],
187188
range: NSMakeRange(0, string.count),
@@ -196,6 +197,10 @@ final class RFC822DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
196197
}
197198
return nil
198199
}
200+
201+
// MARK: Private
202+
203+
private static let trimRegEx = try! NSRegularExpression(pattern: "^[a-zA-Z]+, ([\\w :+-]+)$")
199204
}
200205

201206
// MARK: - RFC1123 formatter
@@ -255,21 +260,6 @@ final class FeedDateFormatter: DateFormatter, @unchecked Sendable {
255260

256261
// MARK: Internal
257262

258-
/// The date specification to use for formatting dates.
259-
let spec: DateSpec
260-
261-
/// ISO8601 date formatter.
262-
lazy var iso8601Formatter: ISO8601DateFormatter = .init()
263-
264-
/// RFC3339 date formatter.
265-
lazy var rfc3339Formatter: RFC3339DateFormatter = .init()
266-
267-
/// RFC822 date formatter.
268-
lazy var rfc822Formatter: RFC822DateFormatter = .init()
269-
270-
/// RFC1123 date formatter.
271-
lazy var rfc1123Formatter: RFC1123DateFormatter = .init()
272-
273263
/// Converts a string to a Date based on the given date specification.
274264
///
275265
/// - Parameters:
@@ -316,4 +306,21 @@ final class FeedDateFormatter: DateFormatter, @unchecked Sendable {
316306
fatalError()
317307
}
318308
}
309+
310+
// MARK: Private
311+
312+
/// The date specification to use for formatting dates.
313+
private let spec: DateSpec
314+
315+
/// ISO8601 date formatter.
316+
private lazy var iso8601Formatter: ISO8601DateFormatter = .init()
317+
318+
/// RFC3339 date formatter.
319+
private lazy var rfc3339Formatter: RFC3339DateFormatter = .init()
320+
321+
/// RFC822 date formatter.
322+
private lazy var rfc822Formatter: RFC822DateFormatter = .init()
323+
324+
/// RFC1123 date formatter.
325+
private lazy var rfc1123Formatter: RFC1123DateFormatter = .init()
319326
}

0 commit comments

Comments
 (0)