From 2419741a7cda55cfbb7ed7bf66013dec78c56798 Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Tue, 14 Jan 2020 02:00:12 +0000 Subject: [PATCH] [BigInt] Move the CustomStringConvertible conformance --- Sources/BigInt/BigInt.swift | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index 1343120..5ad5a51 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -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