From 3498af9da4186ffa9b3e8ce649b38bb60944fcd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Thu, 27 Apr 2023 16:52:15 +0200 Subject: [PATCH] cleanup. req. integer text de/encoding (#97) --- .../ANKBigEndianTextCodable.swift | 2 +- Sources/ANKFoundation/ANKBinaryInteger.swift | 6 ++- .../ANKBitPatternConvertible.swift | 16 +++++++ .../ANKFoundation/ANKFixedWidthInteger.swift | 42 +++++++------------ .../ANKFullWidthKit/Private/Arithmetic.swift | 6 +-- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/Sources/ANKFoundation/ANKBigEndianTextCodable.swift b/Sources/ANKFoundation/ANKBigEndianTextCodable.swift index f8598582..d561bd92 100644 --- a/Sources/ANKFoundation/ANKBigEndianTextCodable.swift +++ b/Sources/ANKFoundation/ANKBigEndianTextCodable.swift @@ -52,7 +52,7 @@ extension ANKBigEndianTextCodable { /// If the given radix is `nil`, it is either decoded from the string or assigned the value `10`. /// @_transparent public init?(decoding source: some StringProtocol, radix: Int? = nil) { - if let value = try? Self.decodeBigEndianText(source, radix: radix) { self = value } else { return nil } + do { self = try Self.decodeBigEndianText(source, radix: radix) } catch { return nil } } } diff --git a/Sources/ANKFoundation/ANKBinaryInteger.swift b/Sources/ANKFoundation/ANKBinaryInteger.swift index 167bcd08..d59e3eaa 100644 --- a/Sources/ANKFoundation/ANKBinaryInteger.swift +++ b/Sources/ANKFoundation/ANKBinaryInteger.swift @@ -22,10 +22,12 @@ /// /// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// -public protocol ANKBinaryInteger: ANKBitPatternConvertible, BinaryInteger, Sendable where Magnitude: ANKUnsignedInteger, Words: Sendable { +public protocol ANKBinaryInteger: ANKBigEndianTextCodable, ANKBitPatternConvertible, +BinaryInteger, Sendable where Magnitude: ANKUnsignedInteger, Words: Sendable { /// A machine word of some kind, or this type. - associatedtype Digit: ANKBinaryInteger = Self where Digit.Digit == Digit, Digit.Magnitude == Magnitude.Digit + associatedtype Digit: ANKBinaryInteger = Self where + Digit.Digit == Digit, Digit.Magnitude == Magnitude.Digit //=------------------------------------------------------------------------= // MARK: Initializers diff --git a/Sources/ANKFoundation/ANKBitPatternConvertible.swift b/Sources/ANKFoundation/ANKBitPatternConvertible.swift index 89367c5b..82394e1b 100644 --- a/Sources/ANKFoundation/ANKBitPatternConvertible.swift +++ b/Sources/ANKFoundation/ANKBitPatternConvertible.swift @@ -58,10 +58,26 @@ extension ANKBitPatternConvertible where BitPattern == Self { // MARK: Initializers //=------------------------------------------------------------------------= + /// Creates a new instance from the given bit pattern. + /// + /// ```swift + /// Int8(bitPattern: UInt8(255)) // Int8(-1) + /// Int8(bitPattern: UInt8(254)) // Int8(-2) + /// Int8(bitPattern: UInt8(253)) // Int8(-3) + /// ``` + /// @_transparent public init(bitPattern source: some ANKBitPatternConvertible) { self = source.bitPattern } + /// The bit pattern of this value. + /// + /// ```swift + /// Int8(-1).bitPattern // UInt8(255) + /// Int8(-2).bitPattern // UInt8(254) + /// Int8(-3).bitPattern // UInt8(253) + /// ``` + /// @_transparent public var bitPattern: BitPattern { self } diff --git a/Sources/ANKFoundation/ANKFixedWidthInteger.swift b/Sources/ANKFoundation/ANKFixedWidthInteger.swift index 95e08d38..1f052fb9 100644 --- a/Sources/ANKFoundation/ANKFixedWidthInteger.swift +++ b/Sources/ANKFoundation/ANKFixedWidthInteger.swift @@ -42,8 +42,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { // MARK: Details x Addition //=------------------------------------------------------------------------= - /// Forms the sum of adding the given value to this value, - /// and returns a value indicating whether overflow occurred. + /// Forms the sum of adding the given value to this value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -53,8 +52,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable mutating func addReportingOverflow(_ amount: Self) -> Bool - /// Forms the sum of adding the given value to this value, - /// and returns a value indicating whether overflow occurred. + /// Forms the sum of adding the given value to this value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -64,8 +62,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @_disfavoredOverload @inlinable mutating func addReportingOverflow(_ amount: Digit) -> Bool - /// Returns the sum of adding the given value to this value, - /// along with a value indicating whether overflow occurred. + /// Returns the sum of adding the given value to this value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -75,8 +72,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable func addingReportingOverflow(_ amount: Self) -> PVO - /// Returns the sum of adding the given value to this value, - /// along with a value indicating whether overflow occurred. + /// Returns the sum of adding the given value to this value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -90,8 +86,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { // MARK: Details x Subtraction //=------------------------------------------------------------------------= - /// Forms the difference of subtracting the given value from this value, - /// and returns a value indicating whether overflow occurred. + /// Forms the difference of subtracting the given value from this value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -101,8 +96,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable mutating func subtractReportingOverflow(_ amount: Self) -> Bool - /// Forms the difference of subtracting the given value from this value, - /// and returns a value indicating whether overflow occurred. + /// Forms the difference of subtracting the given value from this value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -112,8 +106,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @_disfavoredOverload @inlinable mutating func subtractReportingOverflow(_ amount: Digit) -> Bool - /// Returns the difference of subtracting the given value from this value, - /// along with a value indicating whether overflow occurred. + /// Returns the difference of subtracting the given value from this value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -123,8 +116,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable func subtractingReportingOverflow(_ amount: Self) -> PVO - /// Returns the difference of subtracting the given value from this value, - /// along with a value indicating whether overflow occurred. + /// Returns the difference of subtracting the given value from this value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -138,8 +130,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { // MARK: Details x Multiplication //=------------------------------------------------------------------------= - /// Forms the product of multiplying this value by the given value, - /// and returns a value indicating whether overflow occurred. + /// Forms the product of multiplying this value by the given value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -149,8 +140,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable mutating func multiplyReportingOverflow(by amount: Self) -> Bool - /// Forms the product of multiplying this value by the given value, - /// and returns a value indicating whether overflow occurred. + /// Forms the product of multiplying this value by the given value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -160,8 +150,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @_disfavoredOverload @inlinable mutating func multiplyReportingOverflow(by amount: Digit) -> Bool - /// Returns the product of multiplying this value by the given value, - /// along with a value indicating whether overflow occurred. + /// Returns the product of multiplying this value by the given value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -171,8 +160,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable func multipliedReportingOverflow(by amount: Self) -> PVO - /// Returns the product of multiplying this value by the given value, - /// along with a value indicating whether overflow occurred. + /// Returns the product of multiplying this value by the given value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -182,8 +170,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @_disfavoredOverload @inlinable func multipliedReportingOverflow(by amount: Digit) -> PVO - /// Forms the low part of multiplying this value by the given value, - /// and returns the high. + /// Forms the low part of multiplying this value by the given value, and returns the high. /// /// ```swift /// var a = Int8(11); a.multiplyFullWidth(by: Int8(4)) // a = Int8(44); -> Int8(0) @@ -192,8 +179,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern { /// @inlinable mutating func multiplyFullWidth(by amount: Self) -> Self - /// Forms the low part of multiplying this value by the given value, - /// and returns the high. + /// Forms the low part of multiplying this value by the given value, and returns the high. /// /// ```swift /// var a = Int256(11); a.multiplyFullWidth(by: Int(4)) // a = Int256(44); -> Int(0) diff --git a/Sources/ANKFullWidthKit/Private/Arithmetic.swift b/Sources/ANKFullWidthKit/Private/Arithmetic.swift index 33cb935a..3ed06f92 100644 --- a/Sources/ANKFullWidthKit/Private/Arithmetic.swift +++ b/Sources/ANKFullWidthKit/Private/Arithmetic.swift @@ -19,8 +19,7 @@ extension UInt { // MARK: Transformations //=------------------------------------------------------------------------= - /// Forms the sum of adding both values to this value, - /// and returns a value indicating whether overflow occured. + /// Forms the sum of adding both values to this value, and returns an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift @@ -38,8 +37,7 @@ extension UInt { return a || b } - /// Returns the sum of adding both values to this value, - /// along with a value indicating whether overflow occured. + /// Returns the sum of adding both values to this value, along with an overflow indicator. /// In the case of overflow, the result is truncated. /// /// ```swift