Skip to content

Commit 3750b32

Browse files
committed
Merge branch 'glenna_master' into fix/fatalerror_on_jsonserialization_bad_input
2 parents d9b10d3 + 289b5a0 commit 3750b32

File tree

98 files changed

+1024
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1024
-723
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 253 additions & 245 deletions
Large diffs are not rendered by default.
File renamed without changes.

Foundation/ByteCountFormatter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ open class ByteCountFormatter : Formatter {
5050
NSUnimplemented()
5151
}
5252

53-
/* Specify the units that can be used in the output. If NSByteCountFormatterUseDefault, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
53+
/* Specify the units that can be used in the output. If ByteCountFormatter.Units.useDefault, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
5454
*/
5555
open var allowedUnits: Units = .useDefault
5656

57-
/* Specify how the count is displayed by indicating the number of bytes to be used for kilobyte. The default setting is NSByteCountFormatterFileCount, which is the system specific value for file and storage sizes.
57+
/* Specify how the count is displayed by indicating the number of bytes to be used for kilobyte. The default setting is ByteCountFormatter.CountStyle.fileCount, which is the system specific value for file and storage sizes.
5858
*/
5959
open var countStyle: CountStyle = .file
6060

61-
/* Choose whether to allow more natural display of some values, such as zero, where it may be displayed as "Zero KB," ignoring all other flags or options (with the exception of NSByteCountFormatterUseBytes, which would generate "Zero bytes"). The result is appropriate for standalone output. Default value is YES. Special handling of certain values such as zero is especially important in some languages, so it's highly recommended that this property be left in its default state.
61+
/* Choose whether to allow more natural display of some values, such as zero, where it may be displayed as "Zero KB," ignoring all other flags or options (with the exception of ByteCountFormatter.Units.useBytes, which would generate "Zero bytes"). The result is appropriate for standalone output. Default value is YES. Special handling of certain values such as zero is especially important in some languages, so it's highly recommended that this property be left in its default state.
6262
*/
6363
open var allowsNonnumericFormatting: Bool = true
6464

@@ -91,7 +91,7 @@ open class ByteCountFormatter : Formatter {
9191
*/
9292
private let numberFormatter = NumberFormatter()
9393

94-
/* Shortcut for converting a byte count into a string without creating an NSByteCountFormatter and an NSNumber. If you need to specify options other than countStyle, create an instance of NSByteCountFormatter first.
94+
/* Shortcut for converting a byte count into a string without creating an ByteCountFormatter and an NSNumber. If you need to specify options other than countStyle, create an instance of ByteCountFormatter first.
9595
*/
9696
open class func string(fromByteCount byteCount: Int64, countStyle: ByteCountFormatter.CountStyle) -> String {
9797
let formatter = ByteCountFormatter()

Foundation/Calendar.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,36 @@ extension Calendar: _ObjectTypeBridgeable {
11891189
return result!
11901190
}
11911191
}
1192+
1193+
extension Calendar : Codable {
1194+
private enum CodingKeys : Int, CodingKey {
1195+
case identifier
1196+
case locale
1197+
case timeZone
1198+
case firstWeekday
1199+
case minimumDaysInFirstWeek
1200+
}
1201+
1202+
public init(from decoder: Decoder) throws {
1203+
let container = try decoder.container(keyedBy: CodingKeys.self)
1204+
let identifierString = try container.decode(String.self, forKey: .identifier)
1205+
let identifier = Calendar._fromNSCalendarIdentifier(NSCalendar.Identifier(rawValue: identifierString))
1206+
self.init(identifier: identifier)
1207+
1208+
self.locale = try container.decodeIfPresent(Locale.self, forKey: .locale)
1209+
self.timeZone = try container.decode(TimeZone.self, forKey: .timeZone)
1210+
self.firstWeekday = try container.decode(Int.self, forKey: .firstWeekday)
1211+
self.minimumDaysInFirstWeek = try container.decode(Int.self, forKey: .minimumDaysInFirstWeek)
1212+
}
1213+
1214+
public func encode(to encoder: Encoder) throws {
1215+
var container = encoder.container(keyedBy: CodingKeys.self)
1216+
1217+
let identifier = Calendar._toNSCalendarIdentifier(self.identifier).rawValue
1218+
try container.encode(identifier, forKey: .identifier)
1219+
try container.encode(self.locale, forKey: .locale)
1220+
try container.encode(self.timeZone, forKey: .timeZone)
1221+
try container.encode(self.firstWeekday, forKey: .firstWeekday)
1222+
try container.encode(self.minimumDaysInFirstWeek, forKey: .minimumDaysInFirstWeek)
1223+
}
1224+
}

Foundation/DateComponents.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,82 @@ extension DateComponents : _ObjectTypeBridgeable {
341341
}
342342
}
343343

344+
extension DateComponents : Codable {
345+
private enum CodingKeys : Int, CodingKey {
346+
case calendar
347+
case timeZone
348+
case era
349+
case year
350+
case month
351+
case day
352+
case hour
353+
case minute
354+
case second
355+
case nanosecond
356+
case weekday
357+
case weekdayOrdinal
358+
case quarter
359+
case weekOfMonth
360+
case weekOfYear
361+
case yearForWeekOfYear
362+
}
363+
364+
public init(from decoder: Decoder) throws {
365+
let container = try decoder.container(keyedBy: CodingKeys.self)
366+
let calendar = try container.decodeIfPresent(Calendar.self, forKey: .calendar)
367+
let timeZone = try container.decodeIfPresent(TimeZone.self, forKey: .timeZone)
368+
let era = try container.decodeIfPresent(Int.self, forKey: .era)
369+
let year = try container.decodeIfPresent(Int.self, forKey: .year)
370+
let month = try container.decodeIfPresent(Int.self, forKey: .month)
371+
let day = try container.decodeIfPresent(Int.self, forKey: .day)
372+
let hour = try container.decodeIfPresent(Int.self, forKey: .hour)
373+
let minute = try container.decodeIfPresent(Int.self, forKey: .minute)
374+
let second = try container.decodeIfPresent(Int.self, forKey: .second)
375+
let nanosecond = try container.decodeIfPresent(Int.self, forKey: .nanosecond)
376+
377+
let weekday = try container.decodeIfPresent(Int.self, forKey: .weekday)
378+
let weekdayOrdinal = try container.decodeIfPresent(Int.self, forKey: .weekdayOrdinal)
379+
let quarter = try container.decodeIfPresent(Int.self, forKey: .quarter)
380+
let weekOfMonth = try container.decodeIfPresent(Int.self, forKey: .weekOfMonth)
381+
let weekOfYear = try container.decodeIfPresent(Int.self, forKey: .weekOfYear)
382+
let yearForWeekOfYear = try container.decodeIfPresent(Int.self, forKey: .yearForWeekOfYear)
383+
384+
self.init(calendar: calendar,
385+
timeZone: timeZone,
386+
era: era,
387+
year: year,
388+
month: month,
389+
day: day,
390+
hour: hour,
391+
minute: minute,
392+
second: second,
393+
nanosecond: nanosecond,
394+
weekday: weekday,
395+
weekdayOrdinal: weekdayOrdinal,
396+
quarter: quarter,
397+
weekOfMonth: weekOfMonth,
398+
weekOfYear: weekOfYear,
399+
yearForWeekOfYear: yearForWeekOfYear)
400+
}
401+
402+
public func encode(to encoder: Encoder) throws {
403+
var container = encoder.container(keyedBy: CodingKeys.self)
404+
try container.encodeIfPresent(self.calendar, forKey: .calendar)
405+
try container.encodeIfPresent(self.timeZone, forKey: .timeZone)
406+
try container.encodeIfPresent(self.era, forKey: .era)
407+
try container.encodeIfPresent(self.year, forKey: .year)
408+
try container.encodeIfPresent(self.month, forKey: .month)
409+
try container.encodeIfPresent(self.day, forKey: .day)
410+
try container.encodeIfPresent(self.hour, forKey: .hour)
411+
try container.encodeIfPresent(self.minute, forKey: .minute)
412+
try container.encodeIfPresent(self.second, forKey: .second)
413+
try container.encodeIfPresent(self.nanosecond, forKey: .nanosecond)
414+
415+
try container.encodeIfPresent(self.weekday, forKey: .weekday)
416+
try container.encodeIfPresent(self.weekdayOrdinal, forKey: .weekdayOrdinal)
417+
try container.encodeIfPresent(self.quarter, forKey: .quarter)
418+
try container.encodeIfPresent(self.weekOfMonth, forKey: .weekOfMonth)
419+
try container.encodeIfPresent(self.weekOfYear, forKey: .weekOfYear)
420+
try container.encodeIfPresent(self.yearForWeekOfYear, forKey: .yearForWeekOfYear)
421+
}
422+
}

Foundation/DateComponentsFormatter.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension DateComponentsFormatter {
3434
}
3535
}
3636

37-
/* NSDateComponentsFormatter provides locale-correct and flexible string formatting of quantities of time, such as "1 day" or "1h 10m", as specified by NSDateComponents. For formatting intervals of time (such as "2PM to 5PM"), see NSDateIntervalFormatter. NSDateComponentsFormatter is thread-safe, in that calling methods on it from multiple threads will not cause crashes or incorrect results, but it makes no attempt to prevent confusion when one thread sets something and another thread isn't expecting it to change.
37+
/* DateComponentsFormatter provides locale-correct and flexible string formatting of quantities of time, such as "1 day" or "1h 10m", as specified by NSDateComponents. For formatting intervals of time (such as "2PM to 5PM"), see DateIntervalFormatter. DateComponentsFormatter is thread-safe, in that calling methods on it from multiple threads will not cause crashes or incorrect results, but it makes no attempt to prevent confusion when one thread sets something and another thread isn't expecting it to change.
3838
*/
3939

4040
open class DateComponentsFormatter : Formatter {
@@ -53,11 +53,11 @@ open class DateComponentsFormatter : Formatter {
5353

5454
open func string(from components: DateComponents) -> String? { NSUnimplemented() }
5555

56-
/* Normally, NSDateComponentsFormatter will calculate as though counting from the current date and time (e.g. in February, 1 month formatted as a number of days will be 28). -stringFromDate:toDate: calculates from the passed-in startDate instead.
56+
/* Normally, DateComponentsFormatter will calculate as though counting from the current date and time (e.g. in February, 1 month formatted as a number of days will be 28). -stringFromDate:toDate: calculates from the passed-in startDate instead.
5757

5858
See 'allowedUnits' for how the default set of allowed units differs from -stringFromDateComponents:.
5959

60-
Note that this is still formatting the quantity of time between the dates, not the pair of dates itself. For strings like "Feb 22nd - Feb 28th", use NSDateIntervalFormatter.
60+
Note that this is still formatting the quantity of time between the dates, not the pair of dates itself. For strings like "Feb 22nd - Feb 28th", use DateIntervalFormatter.
6161
*/
6262
open func string(from startDate: Date, to endDate: Date) -> String? { NSUnimplemented() }
6363

@@ -67,7 +67,7 @@ open class DateComponentsFormatter : Formatter {
6767

6868
open class func localizedString(from components: DateComponents, unitsStyle: UnitsStyle) -> String? { NSUnimplemented() }
6969

70-
/* Choose how to indicate units. For example, 1h 10m vs 1:10. Default is NSDateComponentsFormatterUnitsStylePositional.
70+
/* Choose how to indicate units. For example, 1h 10m vs 1:10. Default is DateComponentsFormatter.UnitsStyle.positional.
7171
*/
7272
open var unitsStyle: UnitsStyle
7373

@@ -87,9 +87,9 @@ open class DateComponentsFormatter : Formatter {
8787
*/
8888
open var allowedUnits: NSCalendar.Unit
8989

90-
/* Bitmask specifying how to handle zeros in units. This includes both padding and dropping zeros so that a consistent number digits are displayed, causing updating displays to remain more stable. Default is NSDateComponentsFormatterZeroFormattingBehaviorDefault.
90+
/* Bitmask specifying how to handle zeros in units. This includes both padding and dropping zeros so that a consistent number digits are displayed, causing updating displays to remain more stable. Default is DateComponentsFormatter.ZeroFormattingBehavior.default.
9191

92-
If the combination of zero formatting behavior and style would lead to ambiguous date formats (for example, 1:10 meaning 1 hour, 10 seconds), NSDateComponentsFormatter will throw an exception.
92+
If the combination of zero formatting behavior and style would lead to ambiguous date formats (for example, 1:10 meaning 1 hour, 10 seconds), DateComponentsFormatter will throw an exception.
9393
*/
9494
open var zeroFormattingBehavior: ZeroFormattingBehavior
9595

@@ -129,7 +129,7 @@ open class DateComponentsFormatter : Formatter {
129129
*/
130130
open var formattingContext: Context
131131

132-
/* NSDateComponentsFormatter currently only implements formatting, not parsing. Until it implements parsing, this will always return NO.
132+
/* DateComponentsFormatter currently only implements formatting, not parsing. Until it implements parsing, this will always return NO.
133133
*/
134134
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
135135
/// - Note: Since this API is under consideration it may be either removed or revised in the near future

Foundation/DateIntervalFormatter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ extension DateIntervalFormatter {
1919
}
2020
}
2121

22-
// NSDateIntervalFormatter is used to format the range between two NSDates in a locale-sensitive way.
23-
// NSDateIntervalFormatter returns nil and NO for all methods in NSFormatter.
22+
// DateIntervalFormatter is used to format the range between two NSDates in a locale-sensitive way.
23+
// DateIntervalFormatter returns nil and NO for all methods in Formatter.
2424

2525
open class DateIntervalFormatter : Formatter {
2626

@@ -36,8 +36,8 @@ open class DateIntervalFormatter : Formatter {
3636
/*@NSCopying*/ open var calendar: Calendar! // default is the calendar of the locale
3737
/*@NSCopying*/ open var timeZone: TimeZone! // default is [NSTimeZone defaultTimeZone]
3838
open var dateTemplate: String! // default is an empty string
39-
open var dateStyle: Style // default is NSDateIntervalFormatterNoStyle
40-
open var timeStyle: Style // default is NSDateIntervalFormatterNoStyle
39+
open var dateStyle: Style // default is .noStyle
40+
open var timeStyle: Style // default is .noStyle
4141

4242
/*
4343
If the range smaller than the resolution specified by the dateTemplate, a single date format will be produced. If the range is larger than the format specified by the dateTemplate, a locale-specific fallback will be used to format the items missing from the pattern.

Foundation/NSDecimal.swift renamed to Foundation/Decimal.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ fileprivate let pow10 = [
19301930
/*^39 is on 9 shorts. */
19311931
]
19321932

1933-
// Copied from NSScanner.swift
1933+
// Copied from Scanner.swift
19341934
private func decimalSep(_ locale: Locale?) -> String {
19351935
if let loc = locale {
19361936
if let sep = loc._bridgeToObjectiveC().object(forKey: .decimalSeparator) as? NSString {
@@ -1942,15 +1942,15 @@ private func decimalSep(_ locale: Locale?) -> String {
19421942
}
19431943
}
19441944

1945-
// Copied from NSScanner.swift
1945+
// Copied from Scanner.swift
19461946
private func isADigit(_ ch: unichar) -> Bool {
19471947
struct Local {
19481948
static let set = CharacterSet.decimalDigits
19491949
}
19501950
return Local.set.contains(UnicodeScalar(ch)!)
19511951
}
19521952

1953-
// Copied from NSScanner.swift
1953+
// Copied from Scanner.swift
19541954
private func numericValue(_ ch: unichar) -> Int {
19551955
if (ch >= unichar(unicodeScalarLiteral: "0") && ch <= unichar(unicodeScalarLiteral: "9")) {
19561956
return Int(ch) - Int(unichar(unicodeScalarLiteral: "0"))

Foundation/EnergyFormatter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ open class EnergyFormatter: Formatter {
7878
super.init()
7979
}
8080

81-
/*@NSCopying*/ open var numberFormatter: NumberFormatter! // default is NSNumberFormatter with NSNumberFormatterDecimalStyle
81+
/*@NSCopying*/ open var numberFormatter: NumberFormatter! // default is NumberFormatter with NumberFormatter.Style.decimal
8282
open var unitStyle: UnitStyle // default is NSFormattingUnitStyleMedium
83-
open var isForFoodEnergyUse: Bool // default is NO; if it is set to YES, NSEnergyFormatterUnitKilocalorie may be “C” instead of “kcal"
83+
open var isForFoodEnergyUse: Bool // default is NO; if it is set to YES, EnergyFormatter.Unit.kilocalorie may be “C” instead of “kcal"
8484

8585
// Format a combination of a number and an unit to a localized string.
8686
open func string(fromValue value: Double, unit: Unit) -> String {

0 commit comments

Comments
 (0)