Skip to content

Commit

Permalink
Add Swift 2 Compatibility, add support for bits and bytes reading wit…
Browse files Browse the repository at this point in the history
…h cursor
  • Loading branch information
Cosmo committed Aug 15, 2016
1 parent 298d359 commit df08e72
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions Sources/BinaryKit.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Foundation

public struct Binary {
private let bytes: [UInt8]
public let bytes: [UInt8]
var readingOffset: Int = 0

public init(bytes: [UInt8]) {
self.bytes = bytes
Expand All @@ -22,10 +23,6 @@ public struct Binary {
return (byte >> bitPosition) & 0x01
}

public func bit(position: Int) -> Bit {
return self.bit(position) == 1 ? Bit.One : Bit.Zero
}

public func bits(range: Range<Int>) -> Int {
return range.reverse().enumerate().reduce(0) {
$0 + (bit($1.element) << $1.index)
Expand All @@ -48,4 +45,34 @@ public struct Binary {
return bits(start*8, length*8)
}

}
public func bitsWithInternalOffsetAvailable(length: Int) -> Bool {
return (self.bytes.count * 8) >= (self.readingOffset + length)
}

public mutating func next(bits length: Int) -> Int {
if self.bitsWithInternalOffsetAvailable(length) {
let returnValue = self.bits(self.readingOffset, length)
self.readingOffset = self.readingOffset + length
return returnValue
} else {
fatalError("Couldn't extract Bits.")
}
}

public func bytesWithInternalOffsetAvailable(length: Int) -> Bool {
let availableBits = self.bytes.count * 8
let requestedBits = readingOffset + (length * 8)
let possible = availableBits >= requestedBits
return possible
}

public mutating func next(bytes length: Int) -> [UInt8] {
if bytesWithInternalOffsetAvailable(length) {
let returnValue = self.bytes[(self.readingOffset / 8)..<((self.readingOffset / 8) + length)]
self.readingOffset = self.readingOffset + (length * 8)
return Array(returnValue)
} else {
fatalError("Couldn't extract Bytes.")
}
}
}

0 comments on commit df08e72

Please sign in to comment.