Skip to content

Commit 598b2f8

Browse files
committed
[NFC] Reorder Decimal conformances to match documentation and overlay
1 parent f75fc49 commit 598b2f8

File tree

1 file changed

+139
-141
lines changed

1 file changed

+139
-141
lines changed

Foundation/Decimal.swift

Lines changed: 139 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,130 @@ extension Decimal : Hashable, Comparable {
344344
}
345345
}
346346

347+
extension Decimal : CustomStringConvertible {
348+
public init?(string: String, locale: Locale? = nil) {
349+
let scan = Scanner(string: string)
350+
var theDecimal = Decimal()
351+
scan.locale = locale
352+
if !scan.scanDecimal(&theDecimal) {
353+
return nil
354+
}
355+
self = theDecimal
356+
}
357+
358+
// Note: In the Darwin overlay, `description` is implemented in terms of
359+
// `NSDecimalString(_:_:)`; here, it's the other way around.
360+
public var description: String {
361+
if self.isNaN {
362+
return "NaN"
363+
}
364+
if _length == 0 {
365+
return "0"
366+
}
367+
var copy = self
368+
let ZERO: CChar = 0x30 // ASCII '0' == 0x30
369+
let DECIMALPOINT: CChar = 0x2e // ASCII '.' == 0x2e
370+
let MINUS: CChar = 0x2d // ASCII '-' == 0x2d
371+
372+
let bufferSize = 200 // Max value: 39 + 128 + sign + decimal point
373+
var buffer = Array<CChar>(repeating: 0, count: bufferSize)
374+
375+
var i = bufferSize - 1
376+
while copy._exponent > 0 {
377+
i -= 1
378+
buffer[i] = ZERO
379+
copy._exponent -= 1
380+
}
381+
382+
if copy._exponent == 0 {
383+
copy._exponent = 1
384+
}
385+
386+
while copy._length != 0 {
387+
var remainder: UInt16 = 0
388+
if copy._exponent == 0 {
389+
i -= 1
390+
buffer[i] = DECIMALPOINT
391+
}
392+
copy._exponent += 1
393+
(remainder, _) = divideByShort(&copy, 10)
394+
i -= 1
395+
buffer[i] = Int8(remainder) + ZERO
396+
}
397+
if copy._exponent <= 0 {
398+
while copy._exponent != 0 {
399+
i -= 1
400+
buffer[i] = ZERO
401+
copy._exponent += 1
402+
}
403+
i -= 1
404+
buffer[i] = DECIMALPOINT
405+
i -= 1
406+
buffer[i] = ZERO
407+
}
408+
if copy._isNegative != 0 {
409+
i -= 1
410+
buffer[i] = MINUS
411+
}
412+
return String(cString: Array(buffer.suffix(from: i)))
413+
}
414+
}
415+
416+
extension Decimal : Codable {
417+
private enum CodingKeys : Int, CodingKey {
418+
case exponent
419+
case length
420+
case isNegative
421+
case isCompact
422+
case mantissa
423+
}
424+
425+
public init(from decoder: Decoder) throws {
426+
let container = try decoder.container(keyedBy: CodingKeys.self)
427+
let exponent = try container.decode(CInt.self, forKey: .exponent)
428+
let length = try container.decode(CUnsignedInt.self, forKey: .length)
429+
let isNegative = try container.decode(Bool.self, forKey: .isNegative)
430+
let isCompact = try container.decode(Bool.self, forKey: .isCompact)
431+
432+
var mantissaContainer = try container.nestedUnkeyedContainer(forKey: .mantissa)
433+
var mantissa: (CUnsignedShort, CUnsignedShort, CUnsignedShort, CUnsignedShort,
434+
CUnsignedShort, CUnsignedShort, CUnsignedShort, CUnsignedShort) = (0,0,0,0,0,0,0,0)
435+
mantissa.0 = try mantissaContainer.decode(CUnsignedShort.self)
436+
mantissa.1 = try mantissaContainer.decode(CUnsignedShort.self)
437+
mantissa.2 = try mantissaContainer.decode(CUnsignedShort.self)
438+
mantissa.3 = try mantissaContainer.decode(CUnsignedShort.self)
439+
mantissa.4 = try mantissaContainer.decode(CUnsignedShort.self)
440+
mantissa.5 = try mantissaContainer.decode(CUnsignedShort.self)
441+
mantissa.6 = try mantissaContainer.decode(CUnsignedShort.self)
442+
mantissa.7 = try mantissaContainer.decode(CUnsignedShort.self)
443+
444+
self.init(_exponent: exponent,
445+
_length: length,
446+
_isNegative: CUnsignedInt(isNegative ? 1 : 0),
447+
_isCompact: CUnsignedInt(isCompact ? 1 : 0),
448+
_reserved: 0,
449+
_mantissa: mantissa)
450+
}
451+
452+
public func encode(to encoder: Encoder) throws {
453+
var container = encoder.container(keyedBy: CodingKeys.self)
454+
try container.encode(_exponent, forKey: .exponent)
455+
try container.encode(_length, forKey: .length)
456+
try container.encode(_isNegative == 0 ? false : true, forKey: .isNegative)
457+
try container.encode(_isCompact == 0 ? false : true, forKey: .isCompact)
458+
459+
var mantissaContainer = container.nestedUnkeyedContainer(forKey: .mantissa)
460+
try mantissaContainer.encode(_mantissa.0)
461+
try mantissaContainer.encode(_mantissa.1)
462+
try mantissaContainer.encode(_mantissa.2)
463+
try mantissaContainer.encode(_mantissa.3)
464+
try mantissaContainer.encode(_mantissa.4)
465+
try mantissaContainer.encode(_mantissa.5)
466+
try mantissaContainer.encode(_mantissa.6)
467+
try mantissaContainer.encode(_mantissa.7)
468+
}
469+
}
470+
347471
extension Decimal : ExpressibleByFloatLiteral {
348472
public init(floatLiteral value: Double) {
349473
self.init(value)
@@ -428,30 +552,6 @@ extension Decimal : SignedNumeric {
428552
}
429553
}
430554

431-
432-
extension Decimal: _ObjectiveCBridgeable {
433-
public func _bridgeToObjectiveC() -> NSDecimalNumber {
434-
return NSDecimalNumber(decimal: self)
435-
}
436-
437-
public static func _forceBridgeFromObjectiveC(_ x: NSDecimalNumber, result: inout Decimal?) {
438-
result = _unconditionallyBridgeFromObjectiveC(x)
439-
}
440-
441-
public static func _conditionallyBridgeFromObjectiveC(_ x: NSDecimalNumber, result: inout Decimal?) -> Bool {
442-
result = x.decimalValue
443-
return true
444-
}
445-
446-
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDecimalNumber?) -> Decimal {
447-
var result: Decimal?
448-
guard let src = source else { return Decimal(0) }
449-
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Decimal(0) }
450-
return result!
451-
}
452-
}
453-
454-
455555
extension Decimal {
456556
@available(swift, obsoleted: 4, message: "Please use arithmetic operators instead")
457557
@_transparent
@@ -661,72 +761,25 @@ extension Decimal {
661761
public mutating func formTruncatingRemainder(dividingBy other: Decimal) { fatalError("Decimal does not yet fully adopt FloatingPoint") }
662762
}
663763

664-
extension Decimal : CustomStringConvertible {
665-
public init?(string: String, locale: Locale? = nil) {
666-
let scan = Scanner(string: string)
667-
var theDecimal = Decimal()
668-
scan.locale = locale
669-
if !scan.scanDecimal(&theDecimal) {
670-
return nil
671-
}
672-
self = theDecimal
764+
extension Decimal: _ObjectiveCBridgeable {
765+
public func _bridgeToObjectiveC() -> NSDecimalNumber {
766+
return NSDecimalNumber(decimal: self)
673767
}
674768

675-
// Note: In the Darwin overlay, `description` is implemented in terms of
676-
// `NSDecimalString(_:_:)`; here, it's the other way around.
677-
public var description: String {
678-
if self.isNaN {
679-
return "NaN"
680-
}
681-
if _length == 0 {
682-
return "0"
683-
}
684-
var copy = self
685-
let ZERO: CChar = 0x30 // ASCII '0' == 0x30
686-
let DECIMALPOINT: CChar = 0x2e // ASCII '.' == 0x2e
687-
let MINUS: CChar = 0x2d // ASCII '-' == 0x2d
688-
689-
let bufferSize = 200 // Max value: 39 + 128 + sign + decimal point
690-
var buffer = Array<CChar>(repeating: 0, count: bufferSize)
691-
692-
var i = bufferSize - 1
693-
while copy._exponent > 0 {
694-
i -= 1
695-
buffer[i] = ZERO
696-
copy._exponent -= 1
697-
}
769+
public static func _forceBridgeFromObjectiveC(_ x: NSDecimalNumber, result: inout Decimal?) {
770+
result = _unconditionallyBridgeFromObjectiveC(x)
771+
}
698772

699-
if copy._exponent == 0 {
700-
copy._exponent = 1
701-
}
773+
public static func _conditionallyBridgeFromObjectiveC(_ x: NSDecimalNumber, result: inout Decimal?) -> Bool {
774+
result = x.decimalValue
775+
return true
776+
}
702777

703-
while copy._length != 0 {
704-
var remainder: UInt16 = 0
705-
if copy._exponent == 0 {
706-
i -= 1
707-
buffer[i] = DECIMALPOINT
708-
}
709-
copy._exponent += 1
710-
(remainder, _) = divideByShort(&copy, 10)
711-
i -= 1
712-
buffer[i] = Int8(remainder) + ZERO
713-
}
714-
if copy._exponent <= 0 {
715-
while copy._exponent != 0 {
716-
i -= 1
717-
buffer[i] = ZERO
718-
copy._exponent += 1
719-
}
720-
i -= 1
721-
buffer[i] = DECIMALPOINT
722-
i -= 1
723-
buffer[i] = ZERO
724-
}
725-
if copy._isNegative != 0 {
726-
i -= 1
727-
buffer[i] = MINUS
728-
}
729-
return String(cString: Array(buffer.suffix(from: i)))
778+
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDecimalNumber?) -> Decimal {
779+
var result: Decimal?
780+
guard let src = source else { return Decimal(0) }
781+
guard _conditionallyBridgeFromObjectiveC(src, result: &result) else { return Decimal(0) }
782+
return result!
730783
}
731784
}
732785

@@ -2195,58 +2248,3 @@ extension Scanner {
21952248
return Character(s).wholeNumberValue
21962249
}
21972250
}
2198-
2199-
extension Decimal : Codable {
2200-
private enum CodingKeys : Int, CodingKey {
2201-
case exponent
2202-
case length
2203-
case isNegative
2204-
case isCompact
2205-
case mantissa
2206-
}
2207-
2208-
public init(from decoder: Decoder) throws {
2209-
let container = try decoder.container(keyedBy: CodingKeys.self)
2210-
let exponent = try container.decode(CInt.self, forKey: .exponent)
2211-
let length = try container.decode(CUnsignedInt.self, forKey: .length)
2212-
let isNegative = try container.decode(Bool.self, forKey: .isNegative)
2213-
let isCompact = try container.decode(Bool.self, forKey: .isCompact)
2214-
2215-
var mantissaContainer = try container.nestedUnkeyedContainer(forKey: .mantissa)
2216-
var mantissa: (CUnsignedShort, CUnsignedShort, CUnsignedShort, CUnsignedShort,
2217-
CUnsignedShort, CUnsignedShort, CUnsignedShort, CUnsignedShort) = (0,0,0,0,0,0,0,0)
2218-
mantissa.0 = try mantissaContainer.decode(CUnsignedShort.self)
2219-
mantissa.1 = try mantissaContainer.decode(CUnsignedShort.self)
2220-
mantissa.2 = try mantissaContainer.decode(CUnsignedShort.self)
2221-
mantissa.3 = try mantissaContainer.decode(CUnsignedShort.self)
2222-
mantissa.4 = try mantissaContainer.decode(CUnsignedShort.self)
2223-
mantissa.5 = try mantissaContainer.decode(CUnsignedShort.self)
2224-
mantissa.6 = try mantissaContainer.decode(CUnsignedShort.self)
2225-
mantissa.7 = try mantissaContainer.decode(CUnsignedShort.self)
2226-
2227-
self.init(_exponent: exponent,
2228-
_length: length,
2229-
_isNegative: CUnsignedInt(isNegative ? 1 : 0),
2230-
_isCompact: CUnsignedInt(isCompact ? 1 : 0),
2231-
_reserved: 0,
2232-
_mantissa: mantissa)
2233-
}
2234-
2235-
public func encode(to encoder: Encoder) throws {
2236-
var container = encoder.container(keyedBy: CodingKeys.self)
2237-
try container.encode(_exponent, forKey: .exponent)
2238-
try container.encode(_length, forKey: .length)
2239-
try container.encode(_isNegative == 0 ? false : true, forKey: .isNegative)
2240-
try container.encode(_isCompact == 0 ? false : true, forKey: .isCompact)
2241-
2242-
var mantissaContainer = container.nestedUnkeyedContainer(forKey: .mantissa)
2243-
try mantissaContainer.encode(_mantissa.0)
2244-
try mantissaContainer.encode(_mantissa.1)
2245-
try mantissaContainer.encode(_mantissa.2)
2246-
try mantissaContainer.encode(_mantissa.3)
2247-
try mantissaContainer.encode(_mantissa.4)
2248-
try mantissaContainer.encode(_mantissa.5)
2249-
try mantissaContainer.encode(_mantissa.6)
2250-
try mantissaContainer.encode(_mantissa.7)
2251-
}
2252-
}

0 commit comments

Comments
 (0)