[BigInt] Move the CustomStringConvertible conformance

This commit is contained in:
Ben Rimmington committed 2020-01-14 02:00:12 +00:00
1 parent 368caf5b3f
commit 2419741a7c
1 file changed
+20 -17
+20 -17
View File
@@ -79,6 +79,26 @@ extension BigInt: Comparable {
}
}
extension BigInt: CustomStringConvertible {
public var description: String {
var result = ""
if words.count == 1 {
return Int64(bitPattern: UInt64(words[0])).description
} else {
var next = abs(self)
while next != 0 {
let digit: BigInt
(next, digit) = BigInt._div(lhs: next, rhs: 10)
result += "\(digit.words[0])"
}
}
return (self < 0 ? "-" : "") + String(result.reversed())
}
}
extension BigInt {
public init?(_ description: String) {
@@ -722,23 +742,6 @@ extension BigInt {
}
}
}
public var description: String {
var result = ""
if words.count == 1 {
return Int64(bitPattern: UInt64(words[0])).description
} else {
var next = abs(self)
while next != 0 {
let digit: BigInt
(next, digit) = BigInt._div(lhs: next, rhs: 10)
result += "\(digit.words[0])"
}
}
return (self < 0 ? "-" : "") + String(result.reversed())
}
}
// inspired by https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms