@@ -196,6 +196,37 @@ extension Decimal {
196
196
}
197
197
198
198
extension Decimal : Hashable , Comparable {
199
+ // (Used by VariableLengthNumber and doubleValue.)
200
+ fileprivate subscript( index: UInt32 ) -> UInt16 {
201
+ get {
202
+ switch index {
203
+ case 0 : return _mantissa. 0
204
+ case 1 : return _mantissa. 1
205
+ case 2 : return _mantissa. 2
206
+ case 3 : return _mantissa. 3
207
+ case 4 : return _mantissa. 4
208
+ case 5 : return _mantissa. 5
209
+ case 6 : return _mantissa. 6
210
+ case 7 : return _mantissa. 7
211
+ default : fatalError ( " Invalid index \( index) for _mantissa " )
212
+ }
213
+ }
214
+ set {
215
+ switch index {
216
+ case 0 : _mantissa. 0 = newValue
217
+ case 1 : _mantissa. 1 = newValue
218
+ case 2 : _mantissa. 2 = newValue
219
+ case 3 : _mantissa. 3 = newValue
220
+ case 4 : _mantissa. 4 = newValue
221
+ case 5 : _mantissa. 5 = newValue
222
+ case 6 : _mantissa. 6 = newValue
223
+ case 7 : _mantissa. 7 = newValue
224
+ default : fatalError ( " Invalid index \( index) for _mantissa " )
225
+ }
226
+ }
227
+ }
228
+
229
+ // (Used by NSDecimalNumber and hash(into:).)
199
230
internal var doubleValue : Double {
200
231
if _length == 0 {
201
232
return _isNegative == 1 ? Double . nan : 0
@@ -218,13 +249,12 @@ extension Decimal : Hashable, Comparable {
218
249
return _isNegative != 0 ? - d : d
219
250
}
220
251
221
- // Return the low 64bits of the integer part
252
+ // The low 64 bits of the integer part. (Used by uint64Value and int64Value.)
222
253
private var _unsignedInt64Value : UInt64 {
223
254
if _exponent < - 20 || _exponent > 20 {
224
255
return 0
225
256
}
226
-
227
- if _length == 0 || isZero || magnitude < Decimal ( 0 ) {
257
+ if _length == 0 || isZero || magnitude < ( 0 as Decimal ) {
228
258
return 0
229
259
}
230
260
@@ -243,33 +273,33 @@ extension Decimal : Hashable, Comparable {
243
273
return uint64
244
274
}
245
275
246
- // Perform a best effort conversion of the integer value, trying to match Darwin for
247
- // values outside of UInt64.min .. UInt64.max. Used by NSDecimalNumber.
276
+ // A best- effort conversion of the integer value, trying to match Darwin for
277
+ // values outside of UInt64.min... UInt64.max. ( Used by NSDecimalNumber.)
248
278
internal var uint64Value : UInt64 {
249
279
let value = _unsignedInt64Value
250
280
if !self . isNegative {
251
281
return value
252
282
}
253
-
254
283
if value == Int64 . max. magnitude + 1 {
255
284
return UInt64 ( bitPattern: Int64 . min)
256
- } else if value <= Int64 . max. magnitude {
285
+ }
286
+ if value <= Int64 . max. magnitude {
257
287
var value = Int64 ( value)
258
288
value. negate ( )
259
289
return UInt64 ( bitPattern: value)
260
- } else {
261
- return value
262
290
}
291
+ return value
263
292
}
264
293
265
- // Perform a best effort conversion of the integer value, trying to match Darwin for
266
- // values outside of Int64.min .. Int64.max. Used by NSDecimalNumber.
294
+ // A best- effort conversion of the integer value, trying to match Darwin for
295
+ // values outside of Int64.min... Int64.max. ( Used by NSDecimalNumber.)
267
296
internal var int64Value : Int64 {
268
297
let uint64Value = _unsignedInt64Value
269
298
if self . isNegative {
270
299
if uint64Value == Int64 . max. magnitude + 1 {
271
300
return Int64 . min
272
- } else if uint64Value <= Int64 . max. magnitude {
301
+ }
302
+ if uint64Value <= Int64 . max. magnitude {
273
303
var value = Int64 ( uint64Value)
274
304
value. negate ( )
275
305
return value
@@ -1900,34 +1930,7 @@ extension Decimal {
1900
1930
}
1901
1931
return comparison
1902
1932
}
1903
- fileprivate subscript( index: UInt32 ) -> UInt16 {
1904
- get {
1905
- switch index {
1906
- case 0 : return _mantissa. 0
1907
- case 1 : return _mantissa. 1
1908
- case 2 : return _mantissa. 2
1909
- case 3 : return _mantissa. 3
1910
- case 4 : return _mantissa. 4
1911
- case 5 : return _mantissa. 5
1912
- case 6 : return _mantissa. 6
1913
- case 7 : return _mantissa. 7
1914
- default : fatalError ( " Invalid index \( index) for _mantissa " )
1915
- }
1916
- }
1917
- set {
1918
- switch index {
1919
- case 0 : _mantissa. 0 = newValue
1920
- case 1 : _mantissa. 1 = newValue
1921
- case 2 : _mantissa. 2 = newValue
1922
- case 3 : _mantissa. 3 = newValue
1923
- case 4 : _mantissa. 4 = newValue
1924
- case 5 : _mantissa. 5 = newValue
1925
- case 6 : _mantissa. 6 = newValue
1926
- case 7 : _mantissa. 7 = newValue
1927
- default : fatalError ( " Invalid index \( index) for _mantissa " )
1928
- }
1929
- }
1930
- }
1933
+
1931
1934
fileprivate mutating func setNaN( ) {
1932
1935
_length = 0
1933
1936
_isNegative = 1
0 commit comments