@@ -18,8 +18,6 @@ import FoundationEssentials
18
18
import Foundation
19
19
#endif
20
20
21
-
22
-
23
21
@propertyWrapper
24
22
public struct ISO8601Coding : Decodable , Sendable {
25
23
public let wrappedValue : Date
@@ -31,7 +29,7 @@ public struct ISO8601Coding: Decodable, Sendable {
31
29
public init ( from decoder: Decoder ) throws {
32
30
let container = try decoder. singleValueContainer ( )
33
31
let dateString = try container. decode ( String . self)
34
-
32
+
35
33
struct InvalidDateError : Error { }
36
34
37
35
do {
@@ -125,9 +123,9 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
125
123
}
126
124
} catch {
127
125
throw DecodingError . dataCorruptedError (
128
- in: container,
129
- debugDescription:
130
- " Expected date to be in RFC5322 date-time format, but ` \( string) ` is not in the correct format "
126
+ in: container,
127
+ debugDescription:
128
+ " Expected date to be in RFC5322 date-time format, but ` \( string) ` is not in the correct format "
131
129
)
132
130
}
133
131
}
@@ -151,17 +149,17 @@ struct RFC5322DateStrategy {
151
149
// If the date string has a timezone in brackets, we need to remove it before parsing.
152
150
if let bracket = input. firstIndex ( of: " ( " ) {
153
151
endIndex = bracket
154
- }
152
+ }
155
153
var s = input [ input. startIndex..< endIndex]
156
154
157
- let asciiNumbers = UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " )
155
+ let asciiNumbers = UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " )
158
156
159
157
return s. withUTF8 { buffer -> DateComponents ? in
160
158
func parseDay( _ it: inout UnsafeBufferPointer < UInt8 > . Iterator ) -> Int ? {
161
159
let first = it. next ( )
162
160
let second = it. next ( )
163
161
guard let first = first, let second = second else { return nil }
164
-
162
+
165
163
guard asciiNumbers. contains ( first) else { return nil }
166
164
167
165
if asciiNumbers. contains ( second) {
@@ -185,7 +183,7 @@ struct RFC5322DateStrategy {
185
183
let second = it. nextAsciiLetter ( )
186
184
let third = it. nextAsciiLetter ( )
187
185
guard let first = first, let second = second, let third = third else { return nil }
188
- guard first. isAsciiLetter else { return nil }
186
+ guard first. isAsciiLetter else { return nil }
189
187
return monthMap [ [ first, second, third] ]
190
188
}
191
189
@@ -194,8 +192,14 @@ struct RFC5322DateStrategy {
194
192
let second = it. nextAsciiNumber ( )
195
193
let third = it. nextAsciiNumber ( )
196
194
let fourth = it. nextAsciiNumber ( )
197
- guard let first = first, let second = second, let third = third, let fourth = fourth else { return nil }
198
- return Int ( first - UInt8( ascii: " 0 " ) ) * 1000 + Int( second - UInt8( ascii: " 0 " ) ) * 100 + Int( third - UInt8( ascii: " 0 " ) ) * 10 + Int( fourth - UInt8( ascii: " 0 " ) )
195
+ guard let first = first,
196
+ let second = second,
197
+ let third = third,
198
+ let fourth = fourth else { return nil }
199
+ return Int ( first - UInt8( ascii: " 0 " ) ) * 1000
200
+ + Int( second - UInt8( ascii: " 0 " ) ) * 100
201
+ + Int( third - UInt8( ascii: " 0 " ) ) * 10
202
+ + Int( fourth - UInt8( ascii: " 0 " ) )
199
203
}
200
204
201
205
func parseHour( _ it: inout UnsafeBufferPointer < UInt8 > . Iterator ) -> Int ? {
@@ -242,7 +246,7 @@ struct RFC5322DateStrategy {
242
246
243
247
// if the 4th character is a comma, then we have a day of the week
244
248
guard buffer. count > 5 else { return nil }
245
-
249
+
246
250
if buffer [ 3 ] == UInt8 ( ascii: " , " ) {
247
251
for _ in 0 ..< 5 {
248
252
_ = it. next ( )
@@ -276,7 +280,7 @@ struct RFC5322DateStrategy {
276
280
}
277
281
278
282
@available ( macOS 12 . 0 , * )
279
- extension RFC5322DateStrategy : ParseStrategy { }
283
+ extension RFC5322DateStrategy : ParseStrategy { }
280
284
281
285
extension IteratorProtocol where Self. Element == UInt8 {
282
286
mutating func expect( _ expected: UInt8 ) -> Bool {
@@ -301,8 +305,8 @@ extension IteratorProtocol where Self.Element == UInt8 {
301
305
}
302
306
}
303
307
switch c {
304
- case UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " ) : return c
305
- default : return nil
308
+ case UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " ) : return c
309
+ default : return nil
306
310
}
307
311
}
308
312
return nil
@@ -317,9 +321,10 @@ extension IteratorProtocol where Self.Element == UInt8 {
317
321
}
318
322
319
323
switch c {
320
- case UInt8 ( ascii: " A " ) ... UInt8 ( ascii: " Z " ) ,
321
- UInt8 ( ascii: " a " ) ... UInt8 ( ascii: " z " ) : return c
322
- default : return nil
324
+ case UInt8 ( ascii: " A " ) ... UInt8 ( ascii: " Z " ) ,
325
+ UInt8 ( ascii: " a " ) ... UInt8 ( ascii: " z " ) :
326
+ return c
327
+ default : return nil
323
328
}
324
329
}
325
330
return nil
@@ -329,19 +334,30 @@ extension IteratorProtocol where Self.Element == UInt8 {
329
334
extension UInt8 {
330
335
var isAsciiLetter : Bool {
331
336
switch self {
332
- case UInt8 ( ascii: " A " ) ... UInt8 ( ascii: " Z " ) ,
333
- UInt8 ( ascii: " a " ) ... UInt8 ( ascii: " z " ) : return true
334
- default : return false
337
+ case UInt8 ( ascii: " A " ) ... UInt8 ( ascii: " Z " ) ,
338
+ UInt8 ( ascii: " a " ) ... UInt8 ( ascii: " z " ) :
339
+ return true
340
+ default : return false
335
341
}
336
342
}
337
343
}
338
344
339
- let monthMap : [ Array < UInt8 > : Int ] = [
340
- Array ( " Jan " . utf8) : 1 , Array ( " Feb " . utf8) : 2 , Array ( " Mar " . utf8) : 3 , Array ( " Apr " . utf8) : 4 , Array ( " May " . utf8) : 5 , Array ( " Jun " . utf8) : 6 ,
341
- Array ( " Jul " . utf8) : 7 , Array ( " Aug " . utf8) : 8 , Array ( " Sep " . utf8) : 9 , Array ( " Oct " . utf8) : 10 , Array ( " Nov " . utf8) : 11 , Array ( " Dec " . utf8) : 12
345
+ let monthMap : [ [ UInt8 ] : Int ] = [
346
+ Array ( " Jan " . utf8) : 1 ,
347
+ Array ( " Feb " . utf8) : 2 ,
348
+ Array ( " Mar " . utf8) : 3 ,
349
+ Array ( " Apr " . utf8) : 4 ,
350
+ Array ( " May " . utf8) : 5 ,
351
+ Array ( " Jun " . utf8) : 6 ,
352
+ Array ( " Jul " . utf8) : 7 ,
353
+ Array ( " Aug " . utf8) : 8 ,
354
+ Array ( " Sep " . utf8) : 9 ,
355
+ Array ( " Oct " . utf8) : 10 ,
356
+ Array ( " Nov " . utf8) : 11 ,
357
+ Array ( " Dec " . utf8) : 12 ,
342
358
]
343
359
344
- let timezoneOffsetMap : [ Array < UInt8 > : Int ] = [
360
+ let timezoneOffsetMap : [ [ UInt8 ] : Int ] = [
345
361
Array ( " UTC " . utf8) : 0 ,
346
362
Array ( " GMT " . utf8) : 0 ,
347
363
Array ( " EDT " . utf8) : - 4 * 60 ,
0 commit comments