Skip to content

Commit 0b8e95f

Browse files
committed
Stop passing original string for error reporting
1 parent 8180dd1 commit 0b8e95f

File tree

3 files changed

+77
-69
lines changed

3 files changed

+77
-69
lines changed

Sources/FoundationEssentials/Formatting/Date+HTTPFormatStyle.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extension Date {
6868
let result = v.withUTF8 { buffer -> (Int, Date)? in
6969
let view = BufferView(unsafeBufferPointer: buffer)!
7070

71-
guard let comps = try? componentsStyle.components(from: value, in: view) else {
71+
guard let comps = try? componentsStyle.components(in: view) else {
7272
return nil
7373
}
7474

@@ -324,7 +324,7 @@ extension DateComponents {
324324
let result = v.withUTF8 { buffer -> (Int, DateComponents)? in
325325
let view = BufferView(unsafeBufferPointer: buffer)!
326326

327-
guard let comps = try? components(from: value, in: view) else {
327+
guard let comps = try? components(in: view) else {
328328
return nil
329329
}
330330

@@ -338,8 +338,8 @@ extension DateComponents {
338338
let endIndex = value.utf8.index(v.startIndex, offsetBy: result.0)
339339
return (endIndex, result.1)
340340
}
341-
342-
fileprivate func components(from inputString: String, in view: borrowing BufferView<UInt8>) throws -> ComponentsParseResult {
341+
342+
fileprivate func components(in view: borrowing BufferView<UInt8>) throws -> ComponentsParseResult {
343343
// https://www.rfc-editor.org/rfc/rfc9110.html#http.date
344344
// <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
345345

@@ -348,17 +348,17 @@ extension DateComponents {
348348

349349
// Despite the spec, we allow the weekday name to be optional.
350350
guard let maybeWeekday1 = it.peek() else {
351-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now))
351+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now))
352352
}
353353

354354
if isASCIIDigit(maybeWeekday1) {
355355
// This is the first digit of the day. Weekday is not present.
356356
} else {
357357
// Anything else must be a day-name (Mon, Tue, ... Sun)
358358
guard let weekday1 = it.next(), let weekday2 = it.next(), let weekday3 = it.next() else {
359-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now))
359+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now))
360360
}
361-
361+
362362
dc.weekday = switch (weekday1, weekday2, weekday3) {
363363
case (UInt8(ascii: "S"), UInt8(ascii: "u"), UInt8(ascii: "n")):
364364
1
@@ -375,20 +375,20 @@ extension DateComponents {
375375
case (UInt8(ascii: "S"), UInt8(ascii: "a"), UInt8(ascii: "t")):
376376
7
377377
default:
378-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Malformed weekday name")
378+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Malformed weekday name")
379379
}
380380

381381
// Move past , and space to weekday
382-
try it.expectCharacter(UInt8(ascii: ","), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing , after weekday")
383-
try it.expectCharacter(UInt8(ascii: " "), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing space after weekday")
382+
try it.expectCharacter(UInt8(ascii: ","), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing , after weekday")
383+
try it.expectCharacter(UInt8(ascii: " "), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing space after weekday")
384384
}
385385

386-
dc.day = try it.digits(minDigits: 2, maxDigits: 2, input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing or malformed day")
387-
try it.expectCharacter(UInt8(ascii: " "), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
386+
dc.day = try it.digits(minDigits: 2, maxDigits: 2, input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing or malformed day")
387+
try it.expectCharacter(UInt8(ascii: " "), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
388388

389389
// month-name (Jan, Feb, ... Dec)
390390
guard let month1 = it.next(), let month2 = it.next(), let month3 = it.next() else {
391-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing month")
391+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing month")
392392
}
393393

394394
dc.month = switch (month1, month2, month3) {
@@ -417,45 +417,45 @@ extension DateComponents {
417417
case (UInt8(ascii: "D"), UInt8(ascii: "e"), UInt8(ascii: "c")):
418418
12
419419
default:
420-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Month \(String(describing: dc.month)) is out of bounds")
420+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Month \(String(describing: dc.month)) is out of bounds")
421421
}
422422

423-
try it.expectCharacter(UInt8(ascii: " "), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
423+
try it.expectCharacter(UInt8(ascii: " "), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
424424

425-
dc.year = try it.digits(minDigits: 4, maxDigits: 4, input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
426-
try it.expectCharacter(UInt8(ascii: " "), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
425+
dc.year = try it.digits(minDigits: 4, maxDigits: 4, input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
426+
try it.expectCharacter(UInt8(ascii: " "), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
427427

428-
let hour = try it.digits(minDigits: 2, maxDigits: 2, input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
428+
let hour = try it.digits(minDigits: 2, maxDigits: 2, input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
429429
if hour < 0 || hour > 23 {
430-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Hour \(hour) is out of bounds")
430+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Hour \(hour) is out of bounds")
431431
}
432432
dc.hour = hour
433433

434-
try it.expectCharacter(UInt8(ascii: ":"), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
435-
let minute = try it.digits(minDigits: 2, maxDigits: 2, input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
434+
try it.expectCharacter(UInt8(ascii: ":"), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
435+
let minute = try it.digits(minDigits: 2, maxDigits: 2, input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
436436
if minute < 0 || minute > 59 {
437-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Minute \(minute) is out of bounds")
437+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Minute \(minute) is out of bounds")
438438
}
439439
dc.minute = minute
440440

441-
try it.expectCharacter(UInt8(ascii: ":"), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
442-
let second = try it.digits(minDigits: 2, maxDigits: 2, input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
441+
try it.expectCharacter(UInt8(ascii: ":"), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
442+
let second = try it.digits(minDigits: 2, maxDigits: 2, input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
443443
// second '60' is supported in the spec for leap seconds, but Foundation does not support leap seconds. 60 is adjusted to 59.
444444
if second < 0 || second > 60 {
445-
throw parseError(inputString, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Second \(second) is out of bounds")
445+
throw parseError(view, exampleFormattedString: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Second \(second) is out of bounds")
446446
}
447447
// Foundation does not support leap seconds. We convert 60 seconds into 59 seconds.
448448
if second == 60 {
449449
dc.second = 59
450450
} else {
451451
dc.second = second
452452
}
453-
try it.expectCharacter(UInt8(ascii: " "), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now))
453+
try it.expectCharacter(UInt8(ascii: " "), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now))
454454

455455
// "GMT"
456-
try it.expectCharacter(UInt8(ascii: "G"), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
457-
try it.expectCharacter(UInt8(ascii: "M"), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
458-
try it.expectCharacter(UInt8(ascii: "T"), input: inputString, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
456+
try it.expectCharacter(UInt8(ascii: "G"), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
457+
try it.expectCharacter(UInt8(ascii: "M"), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
458+
try it.expectCharacter(UInt8(ascii: "T"), input: view, onFailure: Date.HTTPFormatStyle().format(Date.now), extendedDescription: "Missing GMT time zone")
459459

460460
// Time zone is always GMT, calendar is always Gregorian
461461
dc.timeZone = .gmt

0 commit comments

Comments
 (0)