Skip to content

Commit

Permalink
DataReader and PassiveDataReader: Added single byte accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Aug 18, 2022
1 parent 98b680f commit 40aeaa0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Sources/OTCore/Abstractions/DataReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,26 @@ public struct DataReader {
}

/// Manually advance by _n_ number of bytes from current read offset.
/// Note that this method is unchecked which may result in an offset beyond the end of the data stream.
public mutating func advanceBy(_ count: Int) {
readOffset += count
}

/// Return the next byte and increment the read offset.
///
/// If no bytes remain, `nil` will be returned.
public mutating func readByte() -> UInt8? {
guard let d = dataByte() else { return nil }
defer { readOffset += 1 }
return d
}

/// Read the next byte without advancing the read offset.
/// If no bytes remain, `nil` will be returned.
public func nonAdvancingReadByte() -> UInt8? {
dataByte()
}

/// Return the next _n_ number of bytes and increment the read offset.
///
/// If `bytes` parameter is nil, the remainder of the data will be returned.
Expand All @@ -59,6 +75,11 @@ public struct DataReader {

// MARK: - Internal

func dataByte() -> UInt8? {
guard remainingByteCount > 0 else { return nil }
return base[readOffset]
}

func data(bytes count: Int? = nil) -> (data: Data, advanceCount: Int)? {
if count == 0 {
return (data: Data(), advanceCount: 0)
Expand Down
22 changes: 22 additions & 0 deletions Sources/OTCore/Abstractions/PassiveDataReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,26 @@ public struct PassiveDataReader<D: DataProtocol> {
// MARK: - Methods

/// Manually advance by _n_ number of bytes from current read offset.
/// Note that this method is unchecked which may result in an offset beyond the end of the data stream.
public mutating func advanceBy(_ count: Int) {
readOffset += count
}

/// Return the next byte and increment the read offset.
///
/// If no bytes remain, `nil` will be returned.
public mutating func readByte() -> D.Element? {
guard let d = dataByte() else { return nil }
defer { readOffset += 1 }
return d
}

/// Read the next byte without advancing the read offset.
/// If no bytes remain, `nil` will be returned.
public func nonAdvancingReadByte() -> D.Element? {
dataByte()
}

/// Return the next _n_ number of bytes and increment the read offset.
///
/// If `bytes` parameter is nil, the remainder of the data will be returned.
Expand All @@ -75,6 +91,12 @@ public struct PassiveDataReader<D: DataProtocol> {
return out
}

func dataByte() -> D.Element? {
guard remainingByteCount > 0 else { return nil }
let readPosIndex = withData { $0.index($0.startIndex, offsetBy: readOffset) }
return withData { $0[readPosIndex] }
}

func data(bytes count: Int? = nil) -> (data: D.SubSequence, advanceCount: Int)? {
if count == 0 {
return (data: withData { $0[$0.startIndex ..< $0.startIndex] }, advanceCount: 0)
Expand Down
30 changes: 29 additions & 1 deletion Tests/OTCoreTests/Abstractions/DataReader Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Abstractions_DataReader_Tests: XCTestCase {
func testRead() {
let data = Data([0x01, 0x02, 0x03, 0x04])

// .read - byte by byte
// .read(bytes:)
do {
var dr = DataReader(data)

Expand All @@ -33,6 +33,24 @@ class Abstractions_DataReader_Tests: XCTestCase {
XCTAssertEqual(dr.remainingByteCount, 0)
}

// .readByte()
do {
var dr = DataReader(data)

XCTAssertEqual(dr.readOffset, 0)
XCTAssertEqual(dr.remainingByteCount, 4)
XCTAssertEqual(dr.readByte(), 0x01)
XCTAssertEqual(dr.remainingByteCount, 3)
XCTAssertEqual(dr.readByte(), 0x02)
XCTAssertEqual(dr.remainingByteCount, 2)
XCTAssertEqual(dr.readByte(), 0x03)
XCTAssertEqual(dr.remainingByteCount, 1)
XCTAssertEqual(dr.readByte(), 0x04)
XCTAssertEqual(dr.remainingByteCount, 0)
XCTAssertEqual(dr.readByte(), nil)
XCTAssertEqual(dr.remainingByteCount, 0)
}

// .read - nil read - return all remaining bytes
do {
var dr = DataReader(data)
Expand Down Expand Up @@ -67,6 +85,16 @@ class Abstractions_DataReader_Tests: XCTestCase {
XCTAssertEqual(dr.read(bytes: 1), Data([0x01]))
}

// single bytes
do {
var dr = DataReader(data)

XCTAssertEqual(dr.nonAdvancingReadByte(), data[0])
XCTAssertEqual(dr.nonAdvancingReadByte(), data[0])
XCTAssertEqual(dr.readByte(), data[0])
XCTAssertEqual(dr.readByte(), data[1])
}

// .nonAdvancingRead - read byte counts
do {
let dr = DataReader(data)
Expand Down
30 changes: 29 additions & 1 deletion Tests/OTCoreTests/Abstractions/PassiveDataReader Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Abstractions_PassiveDataReader_Tests: XCTestCase {
func testRead() {
var data = Data([0x01, 0x02, 0x03, 0x04])

// .read - byte by byte
// .read(bytes:)
do {
var dr = PassiveDataReader { $0(&data) }

Expand All @@ -33,6 +33,24 @@ class Abstractions_PassiveDataReader_Tests: XCTestCase {
XCTAssertEqual(dr.remainingByteCount, 0)
}

// .readByte()
do {
var dr = PassiveDataReader { $0(&data) }

XCTAssertEqual(dr.readOffset, 0)
XCTAssertEqual(dr.remainingByteCount, 4)
XCTAssertEqual(dr.readByte(), 0x01)
XCTAssertEqual(dr.remainingByteCount, 3)
XCTAssertEqual(dr.readByte(), 0x02)
XCTAssertEqual(dr.remainingByteCount, 2)
XCTAssertEqual(dr.readByte(), 0x03)
XCTAssertEqual(dr.remainingByteCount, 1)
XCTAssertEqual(dr.readByte(), 0x04)
XCTAssertEqual(dr.remainingByteCount, 0)
XCTAssertEqual(dr.readByte(), nil)
XCTAssertEqual(dr.remainingByteCount, 0)
}

// .read - nil read - return all remaining bytes
do {
var dr = PassiveDataReader { $0(&data) }
Expand Down Expand Up @@ -67,6 +85,16 @@ class Abstractions_PassiveDataReader_Tests: XCTestCase {
XCTAssertEqual(dr.read(bytes: 1), Data([0x01]))
}

// single bytes
do {
var dr = PassiveDataReader { $0(&data) }

XCTAssertEqual(dr.nonAdvancingReadByte(), data[0])
XCTAssertEqual(dr.nonAdvancingReadByte(), data[0])
XCTAssertEqual(dr.readByte(), data[0])
XCTAssertEqual(dr.readByte(), data[1])
}

// .nonAdvancingRead - read byte counts
do {
let dr = PassiveDataReader { $0(&data) }
Expand Down

0 comments on commit 40aeaa0

Please sign in to comment.