Skip to content

Commit b1318bc

Browse files
committed
remove foundation api usage
1 parent acd43c9 commit b1318bc

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Sources/AWSLambdaEvents/Utils/DateWrappers.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
8383
// RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter
8484
// fails to parse this so we need to remove this before parsing.
8585
if let bracket = string.firstIndex(of: "(") {
86-
string = String(string[string.startIndex..<bracket].trimmingCharacters(in: .whitespaces))
86+
string = String(string[string.startIndex..<bracket]._timming(while: { $0.isWhitespace }))
8787
}
8888
do {
8989
self.wrappedValue = try Date(string, strategy: RFC5322DateStrategy())
@@ -109,6 +109,32 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
109109
}*/
110110
}
111111

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+
}
136+
}
137+
112138
struct RFC5322DateError: Error {}
113139

114140
struct RFC5322DateStrategy: ParseStrategy {

0 commit comments

Comments
 (0)