Skip to content

Commit

Permalink
Improve readability of binary integer description methods (#131).
Browse files Browse the repository at this point in the history
Using coder/format reads better than as coder/format.
  • Loading branch information
oscbyspro committed Nov 15, 2024
1 parent cd3662d commit b2144f0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Sources/CoreKit/BinaryInteger+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
}

@inlinable public init(_ description: some StringProtocol, as format: TextInt) throws {
@inlinable public init(_ description: some StringProtocol, using format: TextInt) throws {
self = try format.decode(description)
}
Expand All @@ -37,10 +37,10 @@
//=------------------------------------------------------------------------=
@inlinable public var description: String {
self.description(as: .decimal)
self.description(using: TextInt.decimal)
}

@inlinable public func description(as format: TextInt) -> String {
@inlinable public func description(using format: TextInt) -> String {
format.encode(self)
}
}
6 changes: 3 additions & 3 deletions Sources/StdlibIntKit/StdlibInt+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension StdlibInt {
/// - Note: This method uses `TextInt`.
///
@inlinable public init(_ description: some StringProtocol, as format: TextInt) throws {
try self.init(Base(description, as: format))
try self.init(Base(description, using: format))
}

//=------------------------------------------------------------------------=
Expand All @@ -58,7 +58,7 @@ extension StdlibInt {
///
/// - Note: `String.init(_:radix:)` does not use `TextInt`.
///
@inlinable public func description(as format: TextInt) -> String {
self.base.description(as: format)
@inlinable public func description(using format: TextInt) -> String {
self.base.description(using: format)
}
}
12 changes: 6 additions & 6 deletions Tests/Benchmarks/Fibonacci.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final class FibonacciBenchmarks: XCTestCase {
//=------------------------------------------------------------------------=

static let fib1e6 = IXL.fibonacci(1_000_000)
static let fib1e6r10 = fib1e6.description(as: .decimal)
static let fib1e6r16 = fib1e6.description(as: .hexadecimal)
static let fib1e6r10 = fib1e6.description(using: .decimal)
static let fib1e6r16 = fib1e6.description(using: .hexadecimal)

//=------------------------------------------------------------------------=
// MARK: Initialization
Expand Down Expand Up @@ -78,28 +78,28 @@ final class FibonacciBenchmarks: XCTestCase {
func testFibonacciIXL1e6ToTextAsDecimal() throws {
let data = blackHoleIdentity(Self.fib1e6)
let format = blackHoleIdentity(TextInt.decimal)
let text = data.description(as: format)
let text = data.description(using: format)
XCTAssertEqual(text.utf8.count, 208988)
}

func testFibonacciIXL1e6ToTextAsHexadecimal() throws {
let data = blackHoleIdentity(Self.fib1e6)
let format = blackHoleIdentity(TextInt.hexadecimal)
let text = data.description(as: format)
let text = data.description(using: format)
XCTAssertEqual(text.utf8.count, 173561)
}

func testFibonacciIXL1e6FromTextAsDecimal() throws {
let text = blackHoleIdentity(Self.fib1e6r10)
let format = blackHoleIdentity(TextInt.decimal)
let data = try IXL.init(text, as: format)
let data = try IXL.init(text, using: format)
XCTAssertEqual(data, Self.fib1e6)
}

func testFibonacciIXL1e6FromTextAsHexadecimal() throws {
let text = blackHoleIdentity(Self.fib1e6r16)
let format = blackHoleIdentity(TextInt.hexadecimal)
let data = try IXL.init(text, as: format)
let data = try IXL.init(text, using: format)
XCTAssertEqual(data, Self.fib1e6)
}
}
4 changes: 2 additions & 2 deletions Tests/StdlibIntKitTests/StdlibInt+Integers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ import TestKit
}

description: do {
let radix10 = destination.description(as: .decimal)
let radix16 = destination.description(as: .hexadecimal)
let radix10 = destination.description(using: .decimal)
let radix16 = destination.description(using: .hexadecimal)

try #require(radix10 == source.description, sourceLocation: location)
try #require(radix10 == destination.description, sourceLocation: location)
Expand Down
8 changes: 4 additions & 4 deletions Tests/StdlibIntKitTests/StdlibInt+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ import TestKit
let value = IXL.entropic(size: 256, using: &randomness)
let radix = IX .random(in: 02...36, using: &randomness)
let coder = try TextInt(radix: radix)
let lowercase = value.description(as: coder.lowercased())
let uppercase = value.description(as: coder.uppercased())
let lowercase = value.description(using: coder.lowercased())
let uppercase = value.description(using: coder.uppercased())

try #require(try StdlibInt(lowercase, as: coder) == StdlibInt(value))
try #require(try StdlibInt(uppercase, as: coder) == StdlibInt(value))

try #require(StdlibInt(value).description(as: coder.lowercased()) == lowercase)
try #require(StdlibInt(value).description(as: coder.uppercased()) == uppercase)
try #require(StdlibInt(value).description(using: coder.lowercased()) == lowercase)
try #require(StdlibInt(value).description(using: coder.uppercased()) == uppercase)

try #require(String(StdlibInt(value), radix: Swift.Int(radix), uppercase: false) == lowercase)
try #require(String(StdlibInt(value), radix: Swift.Int(radix), uppercase: true ) == uppercase)
Expand Down
66 changes: 33 additions & 33 deletions Tests/UltimathnumTests/BinaryInteger+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import TestKit
}

func whereIs(_ value: T, using coder: TextInt, at location: SourceLocation = #_sourceLocation) throws {
let encoded = value.description(as: coder)
let decoded = try T(((encoded)),as: coder)
let encoded = value.description(using: coder)
let decoded = try T(((encoded)),using: coder)
try #require(decoded == value, sourceLocation: location)
}
}
Expand All @@ -85,8 +85,8 @@ import TestKit
let coder = TextInt.random(using: &randomness)
let value = T.entropic(size: size, using: &randomness)

let lowercased: String = value.description(as: coder.lowercased())
let uppercased: String = value.description(as: coder.uppercased())
let lowercased: String = value.description(using: coder.lowercased())
let uppercased: String = value.description(using: coder.uppercased())

if coder.radix <= 10 {
try #require(lowercased == uppercased)
Expand All @@ -112,7 +112,7 @@ import TestKit
let regex = BinaryIntegerTestsOnText.regex
let coder = TextInt.random(using: &randomness)
let value = T.entropic(through: Shift.max(or: 255), using: &randomness)
let description = value.description(as: coder)
let description = value.description(using: coder)
let match = try #require(try regex.wholeMatch(in: description))
try #require(match.output.0 == description)
try #require(match.output.sign == (value.isNegative ? "-" : nil))
Expand All @@ -135,7 +135,7 @@ import TestKit
let coder = TextInt.random(using: &randomness)
let value = T.entropic(through: Shift.max(or: 255), using: &randomness)
let magnitude: T.Magnitude = value.magnitude()
let description: String = value.description(as: coder)
let description: String = value.description(using: coder)

always: do {
let result = coder.encode(value)
Expand Down Expand Up @@ -175,7 +175,7 @@ import TestKit
let uppercase = Bool.random(using: &randomness.stdlib)
let letters = TextInt.Letters(uppercase: uppercase)
let coder = try TextInt(radix: radix,letters: letters)
try #require(negative.description(as: coder) == "-\(positive.description(as: coder))")
try #require(negative.description(using: coder) == "-\(positive.description(using: coder))")
}
}
}
Expand All @@ -201,7 +201,7 @@ import TestKit
let uppercase = Bool.random(using: &randomness.stdlib)
let letters = TextInt.Letters(uppercase: uppercase)
let coder = try TextInt(radix: radix,letters: letters)
try #require(infinite.description(as: coder) == "&\(finite.description(as: coder))")
try #require(infinite.description(using: coder) == "&\(finite.description(using: coder))")
}
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ import TestKit
for sign in signs {
for mask in masks {
try #require(throws: TextInt.Error.invalid) {
try T(sign + mask, as: coder)
try T(sign + mask, using: coder)
}
}
}
Expand Down Expand Up @@ -283,19 +283,19 @@ import TestKit

always: do {
try #require(throws: TextInt.Error.invalid) {
try T("0" + scalar, as: coder)
try T("0" + scalar, using: coder)
}
}

if !prefix.contains(element) {
try #require(throws: TextInt.Error.invalid) {
try T(scalar + "0", as: coder)
try T(scalar + "0", using: coder)
}
}

always: do {
try #require(throws: TextInt.Error.invalid) {
try T("0" + scalar + "0", as: coder)
try T("0" + scalar + "0", using: coder)
}
}
}
Expand All @@ -319,7 +319,7 @@ import TestKit
for _ in 0 ..< conditional(debug: 8, release: 32) {
let coder = coders.randomElement(using: &randomness.stdlib)!
let value = T.entropic(through: Shift.max(or: 255), using: &randomness)
let description = value.description(as: coder)
let description = value.description(using: coder)
let match = try #require(try regex.firstMatch(in: description)).output

for _ in 0 ..< 4 {
Expand All @@ -340,7 +340,7 @@ import TestKit
let zeros = IX.random(in: 0...12, using: &randomness)
modified.append(contentsOf: repeatElement("0", count: Swift.Int(zeros)))
modified.append(contentsOf: try #require(match.body))
try #require(try T(modified, as: coder) == value)
try #require(try T(modified, using: coder) == value)
}
}
}
Expand Down Expand Up @@ -369,8 +369,8 @@ import TestKit
func whereIs<T>(_ type: T.Type) throws where T: EdgyInteger {
for coder in BinaryIntegerTestsOnText.coders {
for value in [T.min, T.max] {
let description: String = value.description(as: coder)
try #require(try T(description, as: coder) == value)
let description: String = value.description(using: coder)
try #require(try T(description, using: coder) == value)
}
}
}
Expand All @@ -389,9 +389,9 @@ import TestKit
let value = IXL(T.min).decremented()

for coder in BinaryIntegerTestsOnText.coders {
let description = value.description(as: coder)
let description = value.description(using: coder)
try #require(throws: TextInt.Error.lossy) {
try T(description, as: coder)
try T(description, using: coder)
}
}
}
Expand All @@ -410,9 +410,9 @@ import TestKit
let value = IXL(T.max).incremented()

for coder in BinaryIntegerTestsOnText.coders {
let description = value.description(as: coder)
let description = value.description(using: coder)
try #require(throws: TextInt.Error.lossy) {
try T(description, as: coder)
try T(description, using: coder)
}
}
}
Expand All @@ -434,9 +434,9 @@ import TestKit
for _ in 0 ..< 64 {
let coder = coders.randomElement(using: &randomness.stdlib)!
let natural = IXL.entropic(size: 256, as: Domain.natural, using: &randomness)
let description: String = base.minus(natural).description(as: coder)
let description: String = base.minus(natural).description(using: coder)
try #require(throws: TextInt.Error.lossy) {
try T(description, as: coder)
try T(description, using: coder)
}
}
}
Expand All @@ -458,9 +458,9 @@ import TestKit
for _ in 0 ..< 64 {
let coder = coders.randomElement(using: &randomness.stdlib)!
let natural = IXL.entropic(size: 256, as: Domain.natural, using: &randomness)
let description: String = base.plus(natural).description(as: coder)
let description: String = base.plus(natural).description(using: coder)
try #require(throws: TextInt.Error.lossy) {
try T(description, as: coder)
try T(description, using: coder)
}
}
}
Expand All @@ -481,10 +481,10 @@ import TestKit
for _ in 0 ..< 64 {
let coder = coders.randomElement(using: &randomness.stdlib)!
let value = UXL.entropic(size: 256, as: Domain.natural, using: &randomness).toggled()
let description: String = value.description(as: coder)
let description: String = value.description(using: coder)
try #require(value.isInfinite)
try #require(throws: TextInt.Error.lossy) {
try T(description, as: coder)
try T(description, using: coder)
}
}
}
Expand Down Expand Up @@ -534,8 +534,8 @@ import TestKit

for _ in 0 ..< 64 {
if decoded.error { break }
try #require(encoded == decoded.value.description(as: coder))
try #require(try decoded.value == T.init(encoded, as: coder))
try #require(encoded == decoded.value.description(using: coder))
try #require(try decoded.value == T.init(encoded, using: coder))

encoded.append("0")
decoded = decoded.value.times(radix)
Expand Down Expand Up @@ -574,8 +574,8 @@ import TestKit
encoded.append(String(UnicodeScalar(UInt8(numeral))))

if decoded.error { break }
try #require(encoded == decoded.value.description(as: coder))
try #require(try decoded.value == T.init(encoded, as: coder))
try #require(encoded == decoded.value.description(using: coder))
try #require(try decoded.value == T.init(encoded, using: coder))
}
}
}
Expand Down Expand Up @@ -611,8 +611,8 @@ import TestKit
encoded.append(String(UnicodeScalar(UInt8(numeral))))

if decoded.error { break }
try #require(encoded == decoded.value.description(as: coder))
try #require(try decoded.value == T.init(encoded, as: coder))
try #require(encoded == decoded.value.description(using: coder))
try #require(try decoded.value == T.init(encoded, using: coder))
}
}
}
Expand Down Expand Up @@ -649,7 +649,7 @@ import TestKit

for _ in 0 ..< 32 {
let decoded = T.entropic(through: Shift.max(or: 255), using: &randomness)
let encoded = decoded.description(as: TextInt.decimal)
let encoded = decoded.description(using: TextInt.decimal)

try #require(decoded == T(encoded))
try #require(encoded == decoded.description)
Expand Down

0 comments on commit b2144f0

Please sign in to comment.