Skip to content

Commit 7cf89cf

Browse files
committed
Remove unneeded code
1 parent b1318bc commit 7cf89cf

File tree

1 file changed

+41
-67
lines changed

1 file changed

+41
-67
lines changed

Sources/AWSLambdaEvents/Utils/DateWrappers.swift

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
7979

8080
public init(from decoder: Decoder) throws {
8181
let container = try decoder.singleValueContainer()
82-
var string = try container.decode(String.self)
83-
// RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter
84-
// fails to parse this so we need to remove this before parsing.
85-
if let bracket = string.firstIndex(of: "(") {
86-
string = String(string[string.startIndex..<bracket]._timming(while: { $0.isWhitespace }))
87-
}
82+
let string = try container.decode(String.self)
83+
8884
do {
8985
self.wrappedValue = try Date(string, strategy: RFC5322DateStrategy())
9086
} catch {
@@ -95,44 +91,6 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
9591
)
9692
}
9793
}
98-
99-
/*private static var dateFormatters: [DateFormatter] {
100-
// rfc5322 dates received in SES mails sometimes do not include the day, so need two dateformatters
101-
// one with a day and one without
102-
let formatterWithDay = DateFormatter()
103-
formatterWithDay.dateFormat = "EEE, d MMM yyy HH:mm:ss z"
104-
formatterWithDay.locale = Locale(identifier: "en_US_POSIX")
105-
let formatterWithoutDay = DateFormatter()
106-
formatterWithoutDay.dateFormat = "d MMM yyy HH:mm:ss z"
107-
formatterWithoutDay.locale = Locale(identifier: "en_US_POSIX")
108-
return [formatterWithDay, formatterWithoutDay]
109-
}*/
110-
}
111-
112-
extension RangeReplaceableCollection {
113-
func _timming(while predicate: (Element) -> Bool) -> SubSequence {
114-
var idx = startIndex
115-
while idx < endIndex && predicate(self[idx]) {
116-
formIndex(after: &idx)
117-
}
118-
119-
let startOfNonTrimmedRange = idx // Points at the first char not in the set
120-
guard startOfNonTrimmedRange != endIndex else {
121-
return self[endIndex...]
122-
}
123-
124-
let beforeEnd = index(endIndex, offsetBy: -1)
125-
guard startOfNonTrimmedRange < beforeEnd else {
126-
return self[startOfNonTrimmedRange ..< endIndex]
127-
}
128-
129-
var backIdx = beforeEnd
130-
// No need to bound-check because we've already trimmed from the beginning, so we'd definitely break off of this loop before `backIdx` rewinds before `startIndex`
131-
while predicate(self[backIdx]) {
132-
formIndex(&backIdx, offsetBy: -1)
133-
}
134-
return self[startOfNonTrimmedRange ... backIdx]
135-
}
13694
}
13795

13896
struct RFC5322DateError: Error {}
@@ -149,23 +107,24 @@ struct RFC5322DateStrategy: ParseStrategy {
149107
}
150108

151109
func components(from input: String) -> DateComponents? {
152-
var s = input[...]
153-
return s.withUTF8 { buffer -> DateComponents? in
154-
// if the 4th character is a comma, then we have a day of the week
155-
guard buffer.count > 5 else { return nil }
156-
var offset = 0
157-
if buffer[3] == UInt8(ascii: ",") {
158-
offset = 5
159-
}
110+
var endIndex = input.endIndex
111+
// If the date string has a timezone in brackets, we need to remove it before parsing.
112+
if let bracket = input.firstIndex(of: "(") {
113+
endIndex = bracket
114+
}
115+
var s = input[input.startIndex..<endIndex]
116+
117+
let asciiNumbers = UInt8(ascii: "0") ... UInt8(ascii: "9")
160118

119+
return s.withUTF8 { buffer -> DateComponents? in
161120
func parseDay(_ it: inout UnsafeBufferPointer<UInt8>.Iterator) -> Int? {
162121
let first = it.next()
163122
let second = it.next()
164123
guard let first = first, let second = second else { return nil }
165124

166-
guard first >= UInt8(ascii: "0") && first <= UInt8(ascii: "9") else { return nil }
125+
guard asciiNumbers.contains(first) else { return nil }
167126

168-
if second >= UInt8(ascii: "0") && second <= UInt8(ascii: "9") {
127+
if asciiNumbers.contains(second) {
169128
return Int(first - UInt8(ascii: "0")) * 10 + Int(second - UInt8(ascii: "0"))
170129
} else {
171130
return Int(first - UInt8(ascii: "0"))
@@ -241,10 +200,15 @@ struct RFC5322DateStrategy: ParseStrategy {
241200

242201
var it = buffer.makeIterator()
243202

244-
for _ in 0..<offset {
245-
_ = it.next()
246-
}
203+
// if the 4th character is a comma, then we have a day of the week
204+
guard buffer.count > 5 else { return nil }
247205

206+
if buffer[3] == UInt8(ascii: ",") {
207+
for _ in 0..<5 {
208+
_ = it.next()
209+
}
210+
}
211+
248212
guard let day = parseDay(&it) else { return nil }
249213
guard let month = parseMonth(&it) else { return nil }
250214
guard let year = parseYear(&it) else { return nil }
@@ -293,10 +257,9 @@ extension IteratorProtocol where Self.Element == UInt8 {
293257
continue
294258
}
295259
}
296-
if c >= UInt8(ascii: "0") && c <= UInt8(ascii: "9") {
297-
return c
298-
} else {
299-
return nil
260+
switch c {
261+
case UInt8(ascii: "0") ... UInt8(ascii: "9"): return c
262+
default: return nil
300263
}
301264
}
302265
return nil
@@ -309,10 +272,11 @@ extension IteratorProtocol where Self.Element == UInt8 {
309272
continue
310273
}
311274
}
312-
if c >= UInt8(ascii: "A") && c <= UInt8(ascii: "Z") || c >= UInt8(ascii: "a") && c <= UInt8(ascii: "z") {
313-
return c
314-
} else {
315-
return nil
275+
276+
switch c {
277+
case UInt8(ascii: "A") ... UInt8(ascii: "Z"),
278+
UInt8(ascii: "a") ... UInt8(ascii: "z"): return c
279+
default: return nil
316280
}
317281
}
318282
return nil
@@ -321,7 +285,11 @@ extension IteratorProtocol where Self.Element == UInt8 {
321285

322286
extension UInt8 {
323287
var isAsciiLetter: Bool {
324-
return self >= UInt8(ascii: "A") && self <= UInt8(ascii: "Z") || self >= UInt8(ascii: "a") && self <= UInt8(ascii: "z")
288+
switch self {
289+
case UInt8(ascii: "A") ... UInt8(ascii: "Z"),
290+
UInt8(ascii: "a") ... UInt8(ascii: "z"): return true
291+
default: return false
292+
}
325293
}
326294
}
327295

@@ -331,5 +299,11 @@ let monthMap : [Array<UInt8> : Int ] = [
331299
]
332300

333301
let timezoneOffsetMap: [Array<UInt8> : Int] = [
334-
Array("UTC".utf8): 0, Array("GMT".utf8): 0, Array("EDT".utf8): -4 * 60, Array("CDT".utf8): -5 * 60, Array("MDT".utf8): -6 * 60, Array("PDT".utf8): -7 * 60
302+
Array("UTC".utf8): 0,
303+
Array("GMT".utf8): 0,
304+
Array("EDT".utf8): -4 * 60,
305+
Array("CDT".utf8): -5 * 60,
306+
Array("MDT".utf8): -6 * 60,
307+
Array("PDT".utf8): -7 * 60,
308+
335309
]

0 commit comments

Comments
 (0)