Skip to content

Commit 871e9c7

Browse files
committed
more efficient zero out assurance after buffer creation
1 parent db6ef73 commit 871e9c7

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

Sources/Data Conversion.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ extension BigUInt {
5353
let byteCount = (self.bitWidth + 7) / 8
5454

5555
let buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: byteCount)
56-
buffer.initialize(repeating: 0)
5756

5857
guard byteCount > 0 else { return UnsafeRawBufferPointer(start: buffer.baseAddress, count: 0) }
5958

@@ -69,6 +68,8 @@ extension BigUInt {
6968
i -= 1
7069
}
7170
}
71+
let zeroOut = UnsafeMutableBufferPointer<UInt8>(start: buffer.baseAddress, count: i)
72+
zeroOut.initialize(repeating: 0)
7273
return UnsafeRawBufferPointer(start: buffer.baseAddress, count: byteCount)
7374
}
7475

Tests/BigIntTests/BigUIntTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ class BigUIntTests: XCTestCase {
688688
test(BigUInt(), [])
689689
test(BigUInt(1), [0x01])
690690
test(BigUInt(2), [0x02])
691+
test(BigUInt(0x010203), [0x1, 0x2, 0x3])
691692
test(BigUInt(0x0102030405060708), [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])
692693
test(BigUInt(0x01) << 64 + BigUInt(0x0203040506070809), [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09])
693694
}

0 commit comments

Comments
 (0)