forked from apple/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSNumber.swift
686 lines (582 loc) · 23.4 KB
/
NSNumber.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_exported import Foundation // Clang module
import CoreGraphics
extension Int8 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.int8Value
}
public init(truncating number: NSNumber) {
self = number.int8Value
}
public init?(exactly number: NSNumber) {
let value = number.int8Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int8?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int8?) -> Bool {
guard let value = Int8(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Int8 {
var result: Int8?
guard let src = source else { return Int8(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Int8(0) }
return result!
}
}
extension UInt8 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.uint8Value
}
public init(truncating number: NSNumber) {
self = number.uint8Value
}
public init?(exactly number: NSNumber) {
let value = number.uint8Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt8?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt8?) -> Bool {
guard let value = UInt8(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> UInt8 {
var result: UInt8?
guard let src = source else { return UInt8(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return UInt8(0) }
return result!
}
}
extension Int16 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.int16Value
}
public init(truncating number: NSNumber) {
self = number.int16Value
}
public init?(exactly number: NSNumber) {
let value = number.int16Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int16?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int16?) -> Bool {
guard let value = Int16(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Int16 {
var result: Int16?
guard let src = source else { return Int16(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Int16(0) }
return result!
}
}
extension UInt16 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.uint16Value
}
public init(truncating number: NSNumber) {
self = number.uint16Value
}
public init?(exactly number: NSNumber) {
let value = number.uint16Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt16?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt16?) -> Bool {
guard let value = UInt16(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> UInt16 {
var result: UInt16?
guard let src = source else { return UInt16(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return UInt16(0) }
return result!
}
}
extension Int32 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.int32Value
}
public init(truncating number: NSNumber) {
self = number.int32Value
}
public init?(exactly number: NSNumber) {
let value = number.int32Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int32?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int32?) -> Bool {
guard let value = Int32(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Int32 {
var result: Int32?
guard let src = source else { return Int32(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Int32(0) }
return result!
}
}
extension UInt32 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.uint32Value
}
public init(truncating number: NSNumber) {
self = number.uint32Value
}
public init?(exactly number: NSNumber) {
let value = number.uint32Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt32?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt32?) -> Bool {
guard let value = UInt32(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> UInt32 {
var result: UInt32?
guard let src = source else { return UInt32(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return UInt32(0) }
return result!
}
}
extension Int64 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.int64Value
}
public init(truncating number: NSNumber) {
self = number.int64Value
}
public init?(exactly number: NSNumber) {
let value = number.int64Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int64?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int64?) -> Bool {
guard let value = Int64(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Int64 {
var result: Int64?
guard let src = source else { return Int64(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Int64(0) }
return result!
}
}
extension UInt64 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.uint64Value
}
public init(truncating number: NSNumber) {
self = number.uint64Value
}
public init?(exactly number: NSNumber) {
let value = number.uint64Value
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt64?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt64?) -> Bool {
guard let value = UInt64(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> UInt64 {
var result: UInt64?
guard let src = source else { return UInt64(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return UInt64(0) }
return result!
}
}
extension Int : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.intValue
}
public init(truncating number: NSNumber) {
self = number.intValue
}
public init?(exactly number: NSNumber) {
let value = number.intValue
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) -> Bool {
guard let value = Int(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Int {
var result: Int?
guard let src = source else { return Int(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Int(0) }
return result!
}
}
extension UInt : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.uintValue
}
public init(truncating number: NSNumber) {
self = number.uintValue
}
public init?(exactly number: NSNumber) {
let value = number.uintValue
guard NSNumber(value: value) == number else { return nil }
self = value
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout UInt?) -> Bool {
guard let value = UInt(exactly: x) else { return false }
result = value
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> UInt {
var result: UInt?
guard let src = source else { return UInt(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return UInt(0) }
return result!
}
}
extension Float : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.floatValue
}
public init(truncating number: NSNumber) {
self = number.floatValue
}
public init?(exactly number: NSNumber) {
let type = number.objCType.pointee
if type == 0x49 || type == 0x4c || type == 0x51 {
guard let result = Float(exactly: number.uint64Value) else { return nil }
self = result
} else if type == 0x69 || type == 0x6c || type == 0x71 {
guard let result = Float(exactly: number.int64Value) else { return nil }
self = result
} else {
guard let result = Float(exactly: number.doubleValue) else { return nil }
self = result
}
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Float?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Float?) -> Bool {
if x.floatValue.isNaN {
result = x.floatValue
return true
}
result = Float(exactly: x)
return result != nil
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Float {
var result: Float?
guard let src = source else { return Float(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Float(0) }
return result!
}
}
extension Double : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.doubleValue
}
public init(truncating number: NSNumber) {
self = number.doubleValue
}
public init?(exactly number: NSNumber) {
let type = number.objCType.pointee
if type == 0x51 {
guard let result = Double(exactly: number.uint64Value) else { return nil }
self = result
} else if type == 0x71 {
guard let result = Double(exactly: number.int64Value) else { return nil }
self = result
} else {
// All other integer types and single-precision floating points will
// fit in a `Double` without truncation.
guard let result = Double(exactly: number.doubleValue) else { return nil }
self = result
}
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Double?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Double?) -> Bool {
if x.doubleValue.isNaN {
result = x.doubleValue
return true
}
result = Double(exactly: x)
return result != nil
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Double {
var result: Double?
guard let src = source else { return Double(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Double(0) }
return result!
}
}
extension Bool : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self = number.boolValue
}
public init(truncating number: NSNumber) {
self = number.boolValue
}
public init?(exactly number: NSNumber) {
if number === kCFBooleanTrue as NSNumber || NSNumber(value: 1) == number {
self = true
} else if number === kCFBooleanFalse as NSNumber || NSNumber(value: 0) == number {
self = false
} else {
return nil
}
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) -> Bool {
if x === kCFBooleanTrue as NSNumber || NSNumber(value: 1) == x {
result = true
return true
} else if x === kCFBooleanFalse as NSNumber || NSNumber(value: 0) == x {
result = false
return true
}
return false
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Bool {
var result: Bool?
guard let src = source else { return false }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return false }
return result!
}
}
extension CGFloat : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
public init(_ number: NSNumber) {
self.init(CGFloat.NativeType(truncating: number))
}
public init(truncating number: NSNumber) {
self.init(CGFloat.NativeType(truncating: number))
}
public init?(exactly number: NSNumber) {
var nativeValue: CGFloat.NativeType? = 0
guard CGFloat.NativeType._conditionallyBridgeFromObjectiveC(number, result: &nativeValue) else { return nil }
self.init(nativeValue!)
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSNumber {
return NSNumber(value: self.native)
}
public static func _forceBridgeFromObjectiveC(_ x: NSNumber, result: inout CGFloat?) {
if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout CGFloat?) -> Bool {
var nativeValue: CGFloat.NativeType? = 0
guard CGFloat.NativeType._conditionallyBridgeFromObjectiveC(x, result: &nativeValue) else { return false }
result = CGFloat(nativeValue!)
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> CGFloat {
var result: CGFloat?
guard let src = source else { return CGFloat(0) }
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return CGFloat(0) }
return result!
}
}
// Literal support for NSNumber
extension NSNumber : ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, ExpressibleByBooleanLiteral {
/// Create an instance initialized to `value`.
@nonobjc
public required convenience init(integerLiteral value: Int) {
self.init(value: value)
}
/// Create an instance initialized to `value`.
@nonobjc
public required convenience init(floatLiteral value: Double) {
self.init(value: value)
}
/// Create an instance initialized to `value`.
@nonobjc
public required convenience init(booleanLiteral value: Bool) {
self.init(value: value)
}
}
extension NSNumber : _HasCustomAnyHashableRepresentation {
// Must be @nonobjc to prevent infinite recursion trying to bridge
// AnyHashable to NSObject.
@nonobjc
public func _toCustomAnyHashable() -> AnyHashable? {
// The custom AnyHashable representation here is used when checking for
// equality during bridging NSDictionary to Dictionary (or looking up
// values in a Dictionary bridged to NSDictionary).
//
// When we've got NSNumber values as keys that we want to compare
// through an AnyHashable box, we want to compare values through the
// largest box size we've got available to us (i.e. upcast numbers).
// This happens to resemble the representation that NSNumber uses
// internally: (long long | unsigned long long | double).
//
// This allows us to compare things like
//
// ([Int : Any] as [AnyHashable : Any]) vs. [NSNumber : Any]
//
// because Int can be upcast to Int64 and compared with the number's
// Int64 value.
//
// If NSNumber adds 128-bit representations, this will need to be
// updated to use those.
if let nsDecimalNumber: NSDecimalNumber = self as? NSDecimalNumber {
return AnyHashable(nsDecimalNumber.decimalValue)
} else if self === kCFBooleanTrue as NSNumber {
return AnyHashable(true)
} else if self === kCFBooleanFalse as NSNumber {
return AnyHashable(false)
} else if NSNumber(value: int64Value) == self {
return AnyHashable(int64Value)
} else if NSNumber(value: uint64Value) == self {
return AnyHashable(uint64Value)
} else if NSNumber(value: doubleValue) == self {
return AnyHashable(doubleValue)
} else {
return nil
}
}
}