Skip to content

Commit

Permalink
unsafe api cleanup #51
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Feb 18, 2023
1 parent 6ba28ca commit 07240b1
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Addition+Digit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension ANKFullWidth {
let lhsWasLessThanZero: Bool = self.isLessThanZero
let rhsWasLessThanZero: Bool = amount.isLessThanZero
//=--------------------------------------=
let carry: Bool = self.withUnsafeMutableWordsPointer { LHS in
let carry: Bool = self.withUnsafeMutableWords { LHS in
var index = LHS.startIndex
var carry: Bool = LHS[index].addReportingOverflow(UInt(bitPattern: amount))
LHS.formIndex(after: &index)
Expand Down
6 changes: 3 additions & 3 deletions Sources/ANKFullWidthKit/ANKFullWidth+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ extension ANKFullWidth {
//=------------------------------------------------------------------------=

@inlinable public var byteSwapped: Self {
Self.fromUnsafeMutableWordsAllocation { NEXT in
self.withUnsafeWordsPointer { SELF in
Self.fromUnsafeMutableWords { RESULT in
self.withUnsafeWords { SELF in
for index in SELF.indices {
let word: UInt = SELF[index].byteSwapped
NEXT[SELF.lastIndex - index] = word
RESULT[RESULT.lastIndex &- index] = word
}
}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ extension ANKFullWidth {
/// unchecked subscript access and wrapping index arithmetic. So, don't
/// do stupid stuff. Understood? Cool. Let's go!
///
@_transparent public func withUnsafeWordsPointer<T>(
@_transparent public func withUnsafeWords<T>(
_ body: (UnsafeWordsPointer<BitPattern>) throws -> T) rethrows -> T {
try Swift.withUnsafePointer(to: self) { SELF in
try Swift.withUnsafePointer(to: self) { SELF in
try SELF.withMemoryRebound(to: UInt.self, capacity: Self.count) { WORDS in
try body(UnsafeWordsPointer(WORDS))
}
Expand All @@ -239,7 +239,7 @@ extension ANKFullWidth {
/// unchecked subscript access and wrapping index arithmetic. So, don't
/// do stupid stuff. Understood? Cool. Let's go!
///
@_transparent public mutating func withUnsafeMutableWordsPointer<T>(
@_transparent public mutating func withUnsafeMutableWords<T>(
_ body: (UnsafeMutableWordsPointer<BitPattern>) throws -> T) rethrows -> T {
try Swift.withUnsafeMutablePointer(to: &self) { SELF in
try SELF.withMemoryRebound(to: UInt.self, capacity: Self.count) { WORDS in
Expand All @@ -254,7 +254,7 @@ extension ANKFullWidth {
/// unchecked subscript access and wrapping index arithmetic. So, don't
/// do stupid stuff. Understood? Cool. Let's go!
///
@_transparent public static func fromUnsafeMutableWordsAllocation(
@_transparent public static func fromUnsafeMutableWords(
_ body: (UnsafeMutableWordsPointer<BitPattern>) throws -> Void) rethrows -> Self {
try Swift.withUnsafeTemporaryAllocation(of: Self.self, capacity: 1) { BUFFER in
let SELF = BUFFER.baseAddress.unsafelyUnwrapped
Expand Down
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/ANKFullWidth+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ extension ANKFullWidth {
//=------------------------------------------------------------------------=

@usableFromInline subscript(unchecked index: Int) -> UInt {
@_transparent _read { yield self.withUnsafeWordsPointer({ $0[index] /*------*/ }) }
@_transparent set { self.withUnsafeMutableWordsPointer({ $0[index] = newValue }) }
@_transparent _read { yield self.withUnsafeWords({ $0[index] /*------*/ }) }
@_transparent set { self.withUnsafeMutableWords({ $0[index] = newValue }) }
}

public subscript(index: Int) -> UInt {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Complements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension ANKFullWidth {
//=------------------------------------------------------------------------=

@inlinable public mutating func formTwosComplement() {
self.withUnsafeMutableWordsPointer { SELF in
self.withUnsafeMutableWords { SELF in
var carry: Bool = true
for index: Int in SELF.indices {
var word: UInt = ~SELF[index]
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Division+Digit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension ANKFullWidth where High == High.Magnitude {
var quotient = self
var remainder = UInt()
//=--------------------------------------=
quotient.withUnsafeMutableWordsPointer { QUOTIENT in
quotient.withUnsafeMutableWords { QUOTIENT in
//=----------------------------------=
var quotientIndex: Int = QUOTIENT.endIndex
//=----------------------------------=
Expand Down
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/ANKFullWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ extension ANKFullWidth where High == High.Magnitude {
//=--------------------------------------=
// Division
//=--------------------------------------=
var quotient = Self(); quotient.withUnsafeMutableWordsPointer { QUOTIENT in
var quotient = Self(); quotient.withUnsafeMutableWords { QUOTIENT in
var remainderIndex = remainder_.minLastIndex &+ 1
var quotientIndex = remainderIndex &- divisor_.minLastIndex
let divisorLast0 = divisor[unchecked: divisor_.minLastIndex]
Expand All @@ -188,7 +188,7 @@ extension ANKFullWidth where High == High.Magnitude {
//=------------------------------=
// Approximate Quotient Digit
//=------------------------------=
var digit: UInt = remainder.withUnsafeWordsPointer { REMAINDER in
var digit: UInt = remainder.withUnsafeWords { REMAINDER in
let remainderLast0 = REMAINDER[remainderIndex]
REMAINDER.formIndex(before:/*-*/&remainderIndex)
if remainderLast0 >= divisorLast0 { return UInt.max }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ extension ANKFullWidth {
let rhsIsLessThanZero: Bool = amount.isLessThanZero
//=--------------------------------------=
var high = UInt()
let low: Magnitude = self.withUnsafeWordsPointer { LHS in
Magnitude.fromUnsafeMutableWordsAllocation { LOW in
let low: Magnitude = self.withUnsafeWords { LHS in
Magnitude.fromUnsafeMutableWords { LOW in
//=----------------------------------=
let rhsWord = UInt(bitPattern: amount)
var rhsIsLessThanZeroCarry = rhsIsLessThanZero
Expand Down
6 changes: 3 additions & 3 deletions Sources/ANKFullWidthKit/ANKFullWidth+Multiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ extension ANKFullWidth {
//=--------------------------------------=
var product = DoubleWidth()
//=--------------------------------------=
self .withUnsafeWordsPointer { LHS in
amount .withUnsafeWordsPointer { RHS in
product.withUnsafeMutableWordsPointer { PRODUCT in
self .withUnsafeWords { LHS in
amount .withUnsafeWords { RHS in
product.withUnsafeMutableWords { PRODUCT in
//=----------------------------------=
for lhsIndex: Int in LHS.indices {
var carry: UInt = UInt()
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ extension ANKFullWidth {
assert(sign == UInt.min || sign == UInt.max)
//=--------------------------------------=
var index = words.startIndex
let value = Self.fromUnsafeMutableWordsAllocation { VALUE in
let value = Self.fromUnsafeMutableWords { VALUE in
//=----------------------------------=
var valueIndex = VALUE.startIndex
var wordsIndex = words.startIndex
Expand Down
8 changes: 4 additions & 4 deletions Sources/ANKFullWidthKit/ANKFullWidth+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ extension ANKFullWidth {
//=--------------------------------------=
var next = Self()
//=--------------------------------------=
next.withUnsafeMutableWordsPointer { NEXT in
self.withUnsafeWordsPointer { SELF in
next.withUnsafeMutableWords { NEXT in
self.withUnsafeWords { SELF in
let a: Int = bits
let b: Int = UInt.bitWidth &- bits

Expand Down Expand Up @@ -152,8 +152,8 @@ extension ANKFullWidth {
let isLessThanZero = self.isLessThanZero as Bool
var next: Self = Self(repeating: isLessThanZero)
//=--------------------------------------=
next.withUnsafeMutableWordsPointer { NEXT in
self.withUnsafeWordsPointer { SELF in
next.withUnsafeMutableWords { NEXT in
self.withUnsafeWords { SELF in
let a: Int = bits
let b: Int = UInt.bitWidth &- bits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension ANKFullWidth {
let lhsWasLessThanZero: Bool = self.isLessThanZero
let rhsWasLessThanZero: Bool = amount.isLessThanZero
//=--------------------------------------=
let borrow: Bool = self.withUnsafeMutableWordsPointer { LHS in
let borrow: Bool = self.withUnsafeMutableWords { LHS in
var index = LHS.startIndex
var borrow: Bool = LHS[index].subtractReportingOverflow(UInt(bitPattern: amount))
LHS.formIndex(after: &index)
Expand Down
6 changes: 3 additions & 3 deletions Sources/ANKFullWidthKit/ANKFullWidth+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension ANKFullWidth {
}

@inlinable public var debugDescription: String {
self.withUnsafeWordsPointer { SELF in
self.withUnsafeWords { SELF in
let signedness = !Self.isSigned ? "U" : ""
let size = String(Self.bitWidth)
let body = SELF.lazy.map(String.init).joined(separator: ", ")
Expand Down Expand Up @@ -96,7 +96,7 @@ extension ANKFullWidth where High == High.Magnitude {
let utf8 = source.utf8
var magnitude: Magnitude = Self()
//=--------------------------------------=
let success = magnitude.withUnsafeMutableWordsPointer { MAGNITUDE in
let success = magnitude.withUnsafeMutableWords { MAGNITUDE in
//=----------------------------------=
var chunkEndIndex = utf8.endIndex
var magnitudeIndex = MAGNITUDE.startIndex
Expand Down Expand Up @@ -181,7 +181,7 @@ extension ANKFullWidth where High == High.Magnitude {
let magnitude_ = value.magnitude.minLastIndexReportingIsZeroOrMinusOne()
if magnitude_.isZeroOrMinusOne { return "0" }
//=--------------------------------------=
return value.magnitude.withUnsafeWordsPointer { MAGNITUDE in
return value.magnitude.withUnsafeWords { MAGNITUDE in
var index = magnitude_.minLastIndex
//=----------------------------------=
var text = value.sign != .plus ? "-" : ""
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Words.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extension ANKFullWidth {
///
@inlinable public func minLastIndexReportingIsZeroOrMinusOne() -> (minLastIndex: Int, isZeroOrMinusOne: Bool) {
let sign: UInt = UInt(repeating: self.isLessThanZero)
let index: Int? = self.withUnsafeWordsPointer({ SELF in SELF.lastIndex(where:{ word in word != sign }) })
let index: Int? = self.withUnsafeWords({ SELF in SELF.lastIndex(where:{ word in word != sign }) })
return index.map({( $0, false )}) ?? (0, true)
}

Expand Down
32 changes: 16 additions & 16 deletions Tests/ANKFullWidthKitTests/192+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,44 @@ final class Int192TestsOnCollection: XCTestCase {
// MARK: Tests x Words
//=------------------------------------------------------------------------=

func testWithUnsafeWordsPointer() {
func testWithUnsafeWords() {
let x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

x0.withUnsafeWordsPointer { WORDS in
x0.withUnsafeWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
}

x1.withUnsafeWordsPointer { WORDS in
x1.withUnsafeWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}
}

func testWithUnsafeMutableWordsPointer() {
func testWithUnsafeMutableWords() {
var x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

x0.withUnsafeMutableWordsPointer { WORDS in
x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

func testFromUnsafeMutableWordsAllocation() {
func testFromUnsafeMutableWords() {
let x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

let y0 = T.fromUnsafeMutableWordsAllocation { WORDS in
let y0 = T.fromUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
WORDS.indices.forEach({ WORDS[$0] = UInt.min })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableWordsAllocation { WORDS in
let y1 = T.fromUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
Expand Down Expand Up @@ -180,44 +180,44 @@ final class UInt192TestsOnCollection: XCTestCase {
// MARK: Tests x Words
//=------------------------------------------------------------------------=

func testWithUnsafeWordsPointer() {
func testWithUnsafeWords() {
let x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

x0.withUnsafeWordsPointer { WORDS in
x0.withUnsafeWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
}

x1.withUnsafeWordsPointer { WORDS in
x1.withUnsafeWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}
}

func testWithUnsafeMutableWordsPointer() {
func testWithUnsafeMutableWords() {
var x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

x0.withUnsafeMutableWordsPointer { WORDS in
x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

func testFromUnsafeMutableWordsAllocation() {
func testFromUnsafeMutableWords() {
let x0 = T(truncatingIfNeeded: 0)
let x1 = T(truncatingIfNeeded: -1)

let y0 = T.fromUnsafeMutableWordsAllocation { WORDS in
let y0 = T.fromUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
WORDS.indices.forEach({ WORDS[$0] = UInt.min })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableWordsAllocation { WORDS in
let y1 = T.fromUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
Expand Down
Loading

0 comments on commit 07240b1

Please sign in to comment.