@@ -661,17 +661,19 @@ extension Decimal {
661
661
public mutating func formTruncatingRemainder( dividingBy other: Decimal ) { fatalError ( " Decimal does not yet fully adopt FloatingPoint " ) }
662
662
}
663
663
664
- extension Decimal : CustomStringConvertible {
664
+ extension Decimal : CustomStringConvertible {
665
665
public init ? ( string: String , locale: Locale ? = nil ) {
666
666
let scan = Scanner ( string: string)
667
- scan. locale = locale
668
667
var theDecimal = Decimal ( )
668
+ scan. locale = locale
669
669
if !scan. scanDecimal ( & theDecimal) {
670
670
return nil
671
671
}
672
672
self = theDecimal
673
673
}
674
674
675
+ // Note: In the Darwin overlay, `description` is implemented in terms of
676
+ // `NSDecimalString(_:_:)`; here, it's the other way around.
675
677
public var description : String {
676
678
if self . isNaN {
677
679
return " NaN "
@@ -680,11 +682,11 @@ extension Decimal: CustomStringConvertible {
680
682
return " 0 "
681
683
}
682
684
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
686
688
687
- let bufferSize = 200 // max value : 39+ 128+ sign+decimalpoint
689
+ let bufferSize = 200 // Max value: 39 + 128 + sign + decimal point
688
690
var buffer = Array < CChar > ( repeating: 0 , count: bufferSize)
689
691
690
692
var i = bufferSize - 1
@@ -702,10 +704,10 @@ extension Decimal: CustomStringConvertible {
702
704
var remainder : UInt16 = 0
703
705
if copy. _exponent == 0 {
704
706
i -= 1
705
- buffer [ i] = decimalChar
707
+ buffer [ i] = DECIMALPOINT
706
708
}
707
709
copy. _exponent += 1
708
- ( remainder, _) = divideByShort ( & copy, 10 )
710
+ ( remainder, _) = divideByShort ( & copy, 10 )
709
711
i -= 1
710
712
buffer [ i] = Int8 ( remainder) + ZERO
711
713
}
@@ -716,15 +718,15 @@ extension Decimal: CustomStringConvertible {
716
718
copy. _exponent += 1
717
719
}
718
720
i -= 1
719
- buffer [ i] = decimalChar
721
+ buffer [ i] = DECIMALPOINT
720
722
i -= 1
721
723
buffer [ i] = ZERO
722
724
}
723
725
if copy. _isNegative != 0 {
724
726
i -= 1
725
727
buffer [ i] = MINUS
726
728
}
727
- return String ( cString: Array ( buffer. suffix ( from: i) ) )
729
+ return String ( cString: Array ( buffer. suffix ( from: i) ) )
728
730
}
729
731
}
730
732
0 commit comments