ByteBuffer.set with Collection of UInt8

This commit is contained in:
Johannes Weiss committed 2017-09-29 15:41:32 +01:00
1 parent b735ddf306
commit de2cb8a038
4 files changed
+43 -8

No files matched your search

+2 -3
View File
@@ -68,8 +68,7 @@ extension ByteBuffer {
@discardableResult
public mutating func set(string: String, at index: Int, encoding: String.Encoding = .utf8) -> Int? {
if encoding == .utf8 {
let bytes = Array(string.utf8)
return self.set(bytes: UnsafeRawBufferPointer(start: bytes, count: bytes.count), at: index)
return self.set(bytes: string.utf8, at: index)
} else {
if let data = string.data(using: encoding) {
return self.set(data: data, at: index)
@@ -112,7 +111,7 @@ extension ByteBuffer {
}
@discardableResult
public mutating func write(bytes: UnsafeRawBufferPointer) -> Int {
public mutating func write<S: Collection>(bytes: S) -> Int where S.Element == UInt8 {
let written = set(bytes: bytes, at: self.writerIndex)
self.moveWriterIndex(forwardBy: written)
return written
+10 -5
View File
@@ -171,16 +171,21 @@ public struct ByteBuffer {
deallocator: .custom { _, _ in storageRef.release() })
}
private mutating func set(bytes: UnsafeRawBufferPointer, at index: Index) -> Capacity {
let newEndIndex: Index = index + toIndex(bytes.count)
private mutating func set<S: Collection>(bytes: S, at index: Index) -> Capacity where S.Element == UInt8 {
let newEndIndex: Index = index + toIndex(Int(bytes.count))
if !isKnownUniquelyReferenced(&self._storage) {
let extraCapacity = newEndIndex > self._slice.upperBound ? newEndIndex - self._slice.upperBound : 0
self.copyStorageAndRebase(extraCapacity: extraCapacity)
}
self.ensureAvailableCapacity(Capacity(bytes.count), at: index)
self._storage.bytes.advanced(by: Int(self._slice.lowerBound + index)).copyBytes(from: bytes.baseAddress!, count: bytes.count)
return toCapacity(bytes.count)
let base = self._storage.bytes.advanced(by: Int(self._slice.lowerBound + index)).assumingMemoryBound(to: UInt8.self)
var idx = 0
for b in bytes {
base[idx] = b
idx += 1
}
return toCapacity(Int(bytes.count))
}
// MARK: Public Core API
@@ -295,7 +300,7 @@ extension ByteBuffer: CustomStringConvertible {
/* change types to the user visible `Int` */
extension ByteBuffer {
public mutating func set(bytes: UnsafeRawBufferPointer, at index: Int) -> Int {
public mutating func set<S: Collection>(bytes: S, at index: Int) -> Int where S.Element == UInt8 {
return Int(self.set(bytes: bytes, at: toIndex(index)))
}
@@ -85,6 +85,8 @@ extension ByteBufferTest {
("testWriteBuffer", testWriteBuffer),
("testMisalignedIntegerRead", testMisalignedIntegerRead),
("testSetAndWriteBytes", testSetAndWriteBytes),
("testWriteABunchOfCollections", testWriteABunchOfCollections),
("testSetABunchOfCollections", testSetABunchOfCollections),
]
}
}
+29
View File
@@ -736,4 +736,33 @@ class ByteBufferTest: XCTestCase {
let actualString = String(bytes: actualData, encoding: .utf8)
XCTAssertEqual(Array(repeating: str, count: 3).joined(), actualString)
}
func testWriteABunchOfCollections() throws {
let overallData = "0123456789abcdef".data(using: .utf8)!
buf.write(bytes: "0123".utf8)
"4567".withCString { ptr in
ptr.withMemoryRebound(to: UInt8.self, capacity: 4) { ptr in
_ = buf.write(bytes: UnsafeBufferPointer<UInt8>(start: ptr, count: 4))
}
}
buf.write(bytes: Array("89ab".utf8))
buf.write(bytes: "cdef".data(using: .utf8)!)
let actual = buf.data(at: 0, length: buf.readableBytes)
XCTAssertEqual(overallData, actual)
}
func testSetABunchOfCollections() throws {
let overallData = "0123456789abcdef".data(using: .utf8)!
_ = buf.set(bytes: "0123".utf8, at: 0)
"4567".withCString { ptr in
ptr.withMemoryRebound(to: UInt8.self, capacity: 4) { ptr in
_ = buf.set(bytes: UnsafeBufferPointer<UInt8>(start: ptr, count: 4), at: 4)
}
}
_ = buf.set(bytes: Array("89ab".utf8), at: 8)
_ = buf.set(bytes: "cdef".data(using: .utf8)!, at: 12)
let actual = buf.data(at: 0, length: 16)
XCTAssertEqual(overallData, actual)
}
}