Skip to content

Commit 9e89a33

Browse files
authored
Merge pull request #20 from Cosmo/development
Add method to read signed integers and fix typo in repo
2 parents 9af23cd + 3d2a2a8 commit 9e89a33

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BinaryKit helps you to break down binary data into bits and bytes, easily access
44

55
## Access Bytes
66

7-
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.
7+
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.
88

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

Sources/BinaryKit/Binary.swift

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ public struct Binary {
150150
return result
151151
}
152152

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

162-
public mutating func readBits(_ quantitiy: UInt8) throws -> Int {
163-
return try readBits(Int(quantitiy))
162+
public mutating func readBits(_ quantity: UInt8) throws -> Int {
163+
return try readBits(Int(quantity))
164164
}
165165

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

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

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

188-
/// Returns a `String` of the next n-bytes (`quantitiy`) and
188+
/// Returns a `String` of the next n-bytes (`quantity`) and
189189
/// increments the reading cursor by n-bytes.
190-
public mutating func readString(quantitiyOfBytes quantitiy: Int, encoding: String.Encoding = .ascii) throws -> String {
191-
guard let result = String(bytes: try self.readBytes(quantitiy), encoding: encoding) else {
190+
public mutating func readString(quantityOfBytes quantity: Int, encoding: String.Encoding = .ascii) throws -> String {
191+
guard let result = String(bytes: try self.readBytes(quantity), encoding: encoding) else {
192192
throw BinaryError.notString
193193
}
194194
return result
@@ -327,3 +327,12 @@ public struct Binary {
327327
bytesStore.append(contentsOf: int.bytes)
328328
}
329329
}
330+
331+
332+
extension Binary {
333+
mutating func readSignedBits(_ quantity: UInt8) throws -> Int {
334+
let multiplicationFactor = (try readBit() == 1) ? -1 : 1
335+
let value = try readBits(quantity - 1)
336+
return value * multiplicationFactor
337+
}
338+
}

Tests/BinaryKitTests/BinaryKitTests.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class BinaryKitTests: XCTestCase {
5858

5959
func testBitsRange() {
6060
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111]
61-
var bin = Binary(bytes: bytes)
61+
let bin = Binary(bytes: bytes)
6262

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

187187
// MARK: - Bool
@@ -253,7 +253,7 @@ final class BinaryKitTests: XCTestCase {
253253

254254
XCTAssertEqual(try bin.readByte(), 7)
255255
XCTAssertEqual(try bin.readByte(), 128)
256-
XCTAssertEqual(try bin.readString(quantitiyOfBytes: 12), "hello world!")
256+
XCTAssertEqual(try bin.readString(quantityOfBytes: 12), "hello world!")
257257
XCTAssertEqual(try bin.readByte(), 2)
258258

259259
XCTAssertEqual(try bin.readByte(), 255)
@@ -314,6 +314,18 @@ final class BinaryKitTests: XCTestCase {
314314
XCTAssertEqual(try bin.readBits(13), 0x11ff)
315315
}
316316

317+
func testSignedBits() {
318+
var binary = Binary(bytes: [0xFF, 0x7F, 0x00, 0xFF, 0x77, 0xFF, 0xFF])
319+
XCTAssertEqual(try binary.readSignedBits(8), -127)
320+
XCTAssertEqual(try binary.readSignedBits(8), 127)
321+
XCTAssertEqual(try binary.readSignedBits(8), 0)
322+
XCTAssertEqual(try binary.readSignedBits(4), -7)
323+
XCTAssertEqual(try binary.readSignedBits(4), -7)
324+
XCTAssertEqual(try binary.readSignedBits(4), 7)
325+
XCTAssertEqual(try binary.readSignedBits(4), 7)
326+
XCTAssertEqual(try binary.readSignedBits(16), -32767)
327+
}
328+
317329
// MARK: -
318330

319331
static var allTests = [

0 commit comments

Comments
 (0)