Skip to content

Commit

Permalink
Merge pull request #20 from Cosmo/development
Browse files Browse the repository at this point in the history
Add method to read signed integers and fix typo in repo
  • Loading branch information
Cosmo committed Oct 18, 2020
2 parents 9af23cd + 3d2a2a8 commit 9e89a33
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BinaryKit helps you to break down binary data into bits and bytes, easily access

## Access Bytes

By using any `read*` method (`readByte()`, `readBytes(quantitiy:)`, `readBit()`, …), BinaryKit will increment an internal cursor (or reading offset) to the end of the requested bit or byte, so the next `read*` method can continue from there.
By using any `read*` method (`readByte()`, `readBytes(quantity:)`, `readBit()`, …), BinaryKit will increment an internal cursor (or reading offset) to the end of the requested bit or byte, so the next `read*` method can continue from there.

Any `get*` method (`getByte(index:)`, `getBytes(range:)`, `getBit(index:)`, …) will give access to binary data at any given location — without incrementing the internal cursor.

Expand Down
39 changes: 24 additions & 15 deletions Sources/BinaryKit/Binary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ public struct Binary {
return result
}

/// Returns the `Int`-value of the next n-bits (`quantitiy`)
/// Returns the `Int`-value of the next n-bits (`quantity`)
/// and increments the reading cursor by n-bits.
public mutating func readBits(_ quantitiy: Int) throws -> Int {
let range = (readBitCursor..<(readBitCursor + quantitiy))
public mutating func readBits(_ quantity: Int) throws -> Int {
let range = (readBitCursor..<(readBitCursor + quantity))
let result = try getBits(range: range)
incrementReadCursorBy(bits: quantitiy)
incrementReadCursorBy(bits: quantity)
return result
}

public mutating func readBits(_ quantitiy: UInt8) throws -> Int {
return try readBits(Int(quantitiy))
public mutating func readBits(_ quantity: UInt8) throws -> Int {
return try readBits(Int(quantity))
}

/// Returns the `UInt8`-value of the next byte and
Expand All @@ -169,26 +169,26 @@ public struct Binary {
return UInt8(try readBits(byteSize))
}

/// Returns a `[UInt8]` of the next n-bytes (`quantitiy`) and
/// Returns a `[UInt8]` of the next n-bytes (`quantity`) and
/// increments the reading cursor by n-bytes.
public mutating func readBytes(_ quantitiy: Int) throws -> [UInt8] {
public mutating func readBytes(_ quantity: Int) throws -> [UInt8] {
// Check if the request is within bounds
let range = (readBitCursor..<(readBitCursor + quantitiy * byteSize))
let range = (readBitCursor..<(readBitCursor + quantity * byteSize))
let storeRange = 0...(bytesStore.count * byteSize)
guard storeRange.contains(range.endIndex) else {
throw BinaryError.outOfBounds
}
return try (0..<quantitiy).map{ _ in try readByte() }
return try (0..<quantity).map{ _ in try readByte() }
}

public mutating func readBytes(_ quantitiy: UInt8) throws -> [UInt8] {
return try readBytes(Int(quantitiy))
public mutating func readBytes(_ quantity: UInt8) throws -> [UInt8] {
return try readBytes(Int(quantity))
}

/// Returns a `String` of the next n-bytes (`quantitiy`) and
/// Returns a `String` of the next n-bytes (`quantity`) and
/// increments the reading cursor by n-bytes.
public mutating func readString(quantitiyOfBytes quantitiy: Int, encoding: String.Encoding = .ascii) throws -> String {
guard let result = String(bytes: try self.readBytes(quantitiy), encoding: encoding) else {
public mutating func readString(quantityOfBytes quantity: Int, encoding: String.Encoding = .ascii) throws -> String {
guard let result = String(bytes: try self.readBytes(quantity), encoding: encoding) else {
throw BinaryError.notString
}
return result
Expand Down Expand Up @@ -327,3 +327,12 @@ public struct Binary {
bytesStore.append(contentsOf: int.bytes)
}
}


extension Binary {
mutating func readSignedBits(_ quantity: UInt8) throws -> Int {
let multiplicationFactor = (try readBit() == 1) ? -1 : 1
let value = try readBits(quantity - 1)
return value * multiplicationFactor
}
}
20 changes: 16 additions & 4 deletions Tests/BinaryKitTests/BinaryKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class BinaryKitTests: XCTestCase {

func testBitsRange() {
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111]
var bin = Binary(bytes: bytes)
let bin = Binary(bytes: bytes)

XCTAssertEqual(try bin.getBits(range: 0..<4), 10)
XCTAssertEqual(try bin.getBits(range: 4..<8), 13)
Expand Down Expand Up @@ -179,9 +179,9 @@ final class BinaryKitTests: XCTestCase {
func testStringAndCharacter() {
let bytes: [UInt8] = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0, 255, 0, 100, 0, 9]
var bin = Binary(bytes: bytes)
XCTAssertEqual(try bin.readString(quantitiyOfBytes: 12), "hello, world")
XCTAssertEqual(try bin.readString(quantityOfBytes: 12), "hello, world")
XCTAssertEqual(try bin.readCharacter(), "!")
XCTAssertThrowsError(try bin.readString(quantitiyOfBytes: 6, encoding: .nonLossyASCII))
XCTAssertThrowsError(try bin.readString(quantityOfBytes: 6, encoding: .nonLossyASCII))
}

// MARK: - Bool
Expand Down Expand Up @@ -253,7 +253,7 @@ final class BinaryKitTests: XCTestCase {

XCTAssertEqual(try bin.readByte(), 7)
XCTAssertEqual(try bin.readByte(), 128)
XCTAssertEqual(try bin.readString(quantitiyOfBytes: 12), "hello world!")
XCTAssertEqual(try bin.readString(quantityOfBytes: 12), "hello world!")
XCTAssertEqual(try bin.readByte(), 2)

XCTAssertEqual(try bin.readByte(), 255)
Expand Down Expand Up @@ -314,6 +314,18 @@ final class BinaryKitTests: XCTestCase {
XCTAssertEqual(try bin.readBits(13), 0x11ff)
}

func testSignedBits() {
var binary = Binary(bytes: [0xFF, 0x7F, 0x00, 0xFF, 0x77, 0xFF, 0xFF])
XCTAssertEqual(try binary.readSignedBits(8), -127)
XCTAssertEqual(try binary.readSignedBits(8), 127)
XCTAssertEqual(try binary.readSignedBits(8), 0)
XCTAssertEqual(try binary.readSignedBits(4), -7)
XCTAssertEqual(try binary.readSignedBits(4), -7)
XCTAssertEqual(try binary.readSignedBits(4), 7)
XCTAssertEqual(try binary.readSignedBits(4), 7)
XCTAssertEqual(try binary.readSignedBits(16), -32767)
}

// MARK: -

static var allTests = [
Expand Down

0 comments on commit 9e89a33

Please sign in to comment.