Skip to content

Commit f75fc49

Browse files
committed
[NFC] Note divergence in Decimal.description from Darwin overlay, synchronize CustomStringConvertible initializer implementation with overlay
1 parent 09a995a commit f75fc49

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Foundation/Decimal.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -661,17 +661,19 @@ extension Decimal {
661661
public mutating func formTruncatingRemainder(dividingBy other: Decimal) { fatalError("Decimal does not yet fully adopt FloatingPoint") }
662662
}
663663

664-
extension Decimal: CustomStringConvertible {
664+
extension Decimal : CustomStringConvertible {
665665
public init?(string: String, locale: Locale? = nil) {
666666
let scan = Scanner(string: string)
667-
scan.locale = locale
668667
var theDecimal = Decimal()
668+
scan.locale = locale
669669
if !scan.scanDecimal(&theDecimal) {
670670
return nil
671671
}
672672
self = theDecimal
673673
}
674674

675+
// Note: In the Darwin overlay, `description` is implemented in terms of
676+
// `NSDecimalString(_:_:)`; here, it's the other way around.
675677
public var description: String {
676678
if self.isNaN {
677679
return "NaN"
@@ -680,11 +682,11 @@ extension Decimal: CustomStringConvertible {
680682
return "0"
681683
}
682684
var copy = self
683-
let ZERO : CChar = 0x30 // ASCII '0' == 0x30
684-
let decimalChar : CChar = 0x2e // ASCII '.' == 0x2e
685-
let MINUS : CChar = 0x2d // ASCII '-' == 0x2d
685+
let ZERO: CChar = 0x30 // ASCII '0' == 0x30
686+
let DECIMALPOINT: CChar = 0x2e // ASCII '.' == 0x2e
687+
let MINUS: CChar = 0x2d // ASCII '-' == 0x2d
686688

687-
let bufferSize = 200 // max value : 39+128+sign+decimalpoint
689+
let bufferSize = 200 // Max value: 39 + 128 + sign + decimal point
688690
var buffer = Array<CChar>(repeating: 0, count: bufferSize)
689691

690692
var i = bufferSize - 1
@@ -702,10 +704,10 @@ extension Decimal: CustomStringConvertible {
702704
var remainder: UInt16 = 0
703705
if copy._exponent == 0 {
704706
i -= 1
705-
buffer[i] = decimalChar
707+
buffer[i] = DECIMALPOINT
706708
}
707709
copy._exponent += 1
708-
(remainder,_) = divideByShort(&copy, 10)
710+
(remainder, _) = divideByShort(&copy, 10)
709711
i -= 1
710712
buffer[i] = Int8(remainder) + ZERO
711713
}
@@ -716,15 +718,15 @@ extension Decimal: CustomStringConvertible {
716718
copy._exponent += 1
717719
}
718720
i -= 1
719-
buffer[i] = decimalChar
721+
buffer[i] = DECIMALPOINT
720722
i -= 1
721723
buffer[i] = ZERO
722724
}
723725
if copy._isNegative != 0 {
724726
i -= 1
725727
buffer[i] = MINUS
726728
}
727-
return String(cString: Array(buffer.suffix(from:i)))
729+
return String(cString: Array(buffer.suffix(from: i)))
728730
}
729731
}
730732

0 commit comments

Comments
 (0)