Skip to content

Commit

Permalink
cleanup. req. integer text de/encoding (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Apr 27, 2023
1 parent 94892f1 commit 3498af9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Sources/ANKFoundation/ANKBigEndianTextCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/ANKFoundation/ANKBinaryInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions Sources/ANKFoundation/ANKBitPatternConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitPattern>) {
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
}
Expand Down
42 changes: 14 additions & 28 deletions Sources/ANKFoundation/ANKFixedWidthInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -75,8 +72,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern {
///
@inlinable func addingReportingOverflow(_ amount: Self) -> PVO<Self>

/// 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -123,8 +116,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern {
///
@inlinable func subtractingReportingOverflow(_ amount: Self) -> PVO<Self>

/// 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -171,8 +160,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern {
///
@inlinable func multipliedReportingOverflow(by amount: Self) -> PVO<Self>

/// 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
Expand All @@ -182,8 +170,7 @@ Magnitude: ANKFixedWidthInteger, Magnitude.BitPattern == BitPattern {
///
@_disfavoredOverload @inlinable func multipliedReportingOverflow(by amount: Digit) -> PVO<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 = Int8(11); a.multiplyFullWidth(by: Int8(4)) // a = Int8(44); -> Int8(0)
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions Sources/ANKFullWidthKit/Private/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3498af9

Please sign in to comment.