diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index 3821ad5..9eff36d 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -29,9 +29,9 @@ public struct BigInt: SignedInteger { words[words.endIndex - 1] > Int.max } - private static let _digits = { - (0 ... 10).map { BigInt(words: [UInt(bitPattern: $0)]) } - }() + private static let _digits: [BigInt] = (0 ... 36).map { + BigInt(words: [UInt(bitPattern: $0)]) + } } // MARK: - Basic Behaviors @@ -109,26 +109,40 @@ extension BigInt: CustomStringConvertible { extension BigInt: LosslessStringConvertible { public init?(_ description: String) { + self.init(description, radix: 10) + } + + public init?(_ description: T, radix: Int = 10) where T: StringProtocol { + precondition(2 ... 36 ~= radix, "Radix not in range 2 ... 36") + + self = 0 + let isNegative = description.hasPrefix("-") - let hasPrefixOperator = isNegative || description.hasPrefix("+") + let hasPrefix = isNegative || description.hasPrefix("+") + let utf8 = description.utf8.dropFirst(hasPrefix ? 1 : 0) + guard !utf8.isEmpty else { return nil } - let unprefixedDescription = hasPrefixOperator ? description.dropFirst() : description[...] - guard !unprefixedDescription.isEmpty else { return nil } - - let digits = Set("0123456789") - guard unprefixedDescription.allSatisfy({ digits.contains($0) }) else { return nil } - - var result: BigInt = 0 - for (i, char) in unprefixedDescription.drop(while: { $0 == "0" }).reversed().enumerated() { - // We're ok to force unwrap here because of the guard above. - result += BigInt(char.wholeNumberValue!) * pow(10, BigInt(i)) + for var byte in utf8 { + switch byte { + case UInt8(ascii: "0") ... UInt8(ascii: "9"): + byte -= UInt8(ascii: "0") + case UInt8(ascii: "A") ... UInt8(ascii: "Z"): + byte -= UInt8(ascii: "A") + byte += 10 + case UInt8(ascii: "a") ... UInt8(ascii: "z"): + byte -= UInt8(ascii: "a") + byte += 10 + default: + return nil + } + guard byte < radix else { return nil } + self *= BigInt._digits[radix] + self += BigInt._digits[Int(byte)] } if isNegative { - result = -result + self.negate() } - - words = result.words } } diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index fd7619d..e37be27 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -164,4 +164,27 @@ final class BigIntTests: XCTestCase { XCTAssertEqual(actualStrings, expectedStrings, "Strings: actual, expected") } } + + func testRandomDescriptions() { + for _ in 0 ..< 100 { + let expectedNumber = BigInt(Int.random(in: .min ... .max)) + for radix in 2 ... 36 { + for uppercase in [false, true] { + let expectedString = String(expectedNumber, + radix: radix, + uppercase: uppercase) + let actualNumber = BigInt(expectedString, radix: radix) + XCTAssertNotNil(actualNumber) + if let actualNumber = actualNumber { + XCTAssertEqual(actualNumber, expectedNumber, + "Numbers: actual, expected") + if radix == 10 { + XCTAssertEqual(actualNumber.description, expectedString, + "Strings: actual, expected") + } + } + } + } + } + } }