<commit_before>//
//  Bitstream.swift
//  MIT License
//
//  Created by <NAME> on 2017-06-19.
//  Copyright © 2017 <NAME>.
//

import Foundation
import Bitter

public class Bitstream : NSObject {

    private var data : Data

    private var bitsLeftInReadBuffer : Int = 0
    private var readByteIndex : Int = 0
    private var readBuffer : UInt8 = 0

    private var writeBuffer : UInt8 = 0
    private var bitsLeftInWriteBuffer : Int = 8

    public init(data: Data ) {
        self.data = data
    }

    public func getNextBit() -> UInt8! {
        if bitsLeftInReadBuffer == 0 && readByteIndex < data.count {
            readBuffer = data[readByteIndex]
            bitsLeftInReadBuffer = 8
            readByteIndex += 1
        }
        if bitsLeftInReadBuffer > 0 {
            let bit = readBuffer.b7
            readBuffer = readBuffer << 1
            bitsLeftInReadBuffer -= 1
            return bit
        }
        return nil
    }

    public func getNextBits(_ bitsToRead : UInt8 ) -> UInt64! {
        if bitsToRead > 64 {
            return nil
        }

        var bitsLeft = bitsToRead
        var combinedValue : UInt64 = 0
        while bitsLeft > 0 {
            let bit = getNextBit()
            if bit == nil {
                return nil
            }
            combinedValue = combinedValue << 1
            combinedValue = combinedValue | bit!.toU64
            bitsLeft -= 1
        }
        return combinedValue
    }

    public func setNextBit(_ bit : UInt8 ) {
        if( bitsLeftInWriteBuffer == 0 ) {
            data.append(writeBuffer)
            writeBuffer = 0
            bitsLeftInWriteBuffer = 8
        }
        writeBuffer = (writeBuffer << 1) | (bit & 0x01)
        bitsLeftInWriteBuffer -= 1
    }

    public func setNextBits(_ bitsToWrite : UInt8, from bits: UInt64) {
        if( bitsToWrite > 64 ) {
            return
        }

        var shiftedBits = bits << (64-bitsToWrite.toU64)
        var bitsLeftToWrite = bitsToWrite
        while bitsLeftToWrite > 0 {
            let bit = shiftedBits >> 63
            setNextBit(bit.toU8)
            shiftedBits = shiftedBits << 1
            bitsLeftToWrite -= 1
        }
    }

    public func finalizedData() -> Data {
        setNextBits(UInt8(bitsLeftInWriteBuffer), from: 0)
        if( bitsLeftInWriteBuffer != 8 ) {
            data.append(writeBuffer)
        }
        let finalizedData = data<commit_msg>swift style cleanup<commit_after>//
//  Bitstream.swift
//  MIT License
//
//  Created by <NAME> on 2017-06-19.
//  Copyright © 2017 <NAME>.
//

import Foundation
import Bitter

public class Bitstream: NSObject {

    private var data: Data

    private var bitsLeftInReadBuffer: Int = 0
    private var readByteIndex: Int = 0
    private var readBuffer: UInt8 = 0

    private var writeBuffer: UInt8 = 0
    private var bitsLeftInWriteBuffer: Int = 8

    public init(data: Data ) {
        self.data = data
    }

    public func getNextBit() -> UInt8! {
        if bitsLeftInReadBuffer == 0 && readByteIndex < data.count {
            readBuffer = data[readByteIndex]
            bitsLeftInReadBuffer = 8
            readByteIndex += 1
        }
        if bitsLeftInReadBuffer > 0 {
            let bit = readBuffer.b7
            readBuffer = readBuffer << 1
            bitsLeftInReadBuffer -= 1
            return bit
        }
        return nil
    }

    public func getNextBits(_ bitsToRead: UInt8 ) -> UInt64! {
        if bitsToRead > 64 {
            return nil
        }

        var bitsLeft = bitsToRead
        var combinedValue: UInt64 = 0
        while bitsLeft > 0 {
            let bit = getNextBit()
            if bit == nil {
                return nil
            }
            combinedValue = combinedValue << 1
            combinedValue = combinedValue | bit!.toU64
            bitsLeft -= 1
        }
        return combinedValue
    }

    public func setNextBit(_ bit: UInt8 ) {
        if( bitsLeftInWriteBuffer == 0 ) {
            data.append(writeBuffer)
            writeBuffer = 0
            bitsLeftInWriteBuffer = 8
        }
        writeBuffer = (writeBuffer << 1) | (bit & 0x01)
        bitsLeftInWriteBuffer -= 1
    }

    public func setNextBits(_ bitsToWrite: UInt8, from bits: UInt64) {
        if( bitsToWrite > 64 ) {
            return
        }

        var shiftedBits = bits << (64 - bitsToWrite.toU64)
        var bitsLeftToWrite = bitsToWrite
        while bitsLeftToWrite > 0 {
            let bit = shiftedBits >> 63
            setNextBit(bit.toU8)
            shiftedBits = shiftedBits << 1
            bitsLeftToWrite -= 1
        }
    }

    public func finalizedData() -> Data {
        setNextBits(UInt8(bitsLeftInWriteBuffer), from: 0)
        if( bitsLeftInWriteBuffer != 8 ) {
            data.append(writeBuffer)
        }
        let finalizedData = data
{'fraction_non_alphanumeric': 0.07957122242836528, 'fraction_numerical': 0.027623170480313337, 'mean_word_length': 1.6427015250544663, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4296785', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1354, 'n_words': 382}