<commit_before>//
//  DataExtension.swift
//  SwiftyUtils iOS
//
//  Created by <NAME> on 2/13/19.
//  Copyright © 2019 <NAME>. All rights reserved.
//

import Foundation

extension Data {

    // MARK: Initializers
    /**
     Initializes a new data object from a hex string

     - Parameter hexString: String in hexadecimal format. Can start with "0x" or not and may have spaces. Any illegal (non hex) characters or an odd number of legal characters will cause it to fail.
     */
    public init?(hexString: String) {
        let noSpaces = hexString.replacingOccurrences(of: "\\s", with: "", options: .regularExpression, range: nil)
        guard noSpaces.count % 2 == 0 else { print("invalid hex string: uneven character count"); return nil }
        let hexArray = noSpaces.split(intoChunksOf: 2)

        var byteArray = [UInt8]()
        byteArray.reserveCapacity(hexString.count)
        for substringByte in hexArray {
            guard substringByte != "0x" else { continue }
            guard let byte = UInt8(substringByte, radix: 16) else { print("could not convert substring '\(substringByte)' to byte"); return nil }
            byteArray.append(byte)
        }
        self = Data(bytes: byteArray)
    }

    // MARK: managable data
    /**
    Provides the data as a hex string with formatting options.

    - Parameter hexLeader: Boolean determining if the string should start with "0x"
    - Parameter spaces: Boolean determining if there should be spaces inserted every 8 characters

    - Returns: String with hex encoded data
     */
    public func toHexString(hexLeader: Bool = false, spaces: Bool = false) -> String {
        let count = self.count / MemoryLayout<UInt8>.stride

        var hexString = ""
        if hexLeader {
            hexString = "0x"
        }
        self.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in
            for index in 0..<count {
                if spaces && (index % 4) == 0 && index != 0 && index != (count - 1) {
                    hexString += " "
                }
                hexString += String(format: "%02X", bytes[index])
            }
        }
        return hexString
    }

    /**
    Provides the data as an array of UInt8 bytes for easy manipulation.
     */
    public var bytesArray: [UInt8] {
        return toBytesArray()
    }

    private func toBytesArray() -> [UInt8] {
        let count = self.count / MemoryLayout<UInt8>.stride
        let array = self.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> [UInt8] in
            var byteArray = [UInt8]()
            byteArray.reserveCapacity(count)
            for index in 0..<count {
                byteArray.append(bytes[index])
            }
            return byteArray
        }
        return array
    }

}
<commit_msg>made requested changes to the data extensions<commit_after>//
//  DataExtension.swift
//  SwiftyUtils iOS
//
//  Created by <NAME> on 2/13/19.
//  Copyright © 2019 <NAME>. All rights reserved.
//

import Foundation

extension Data {

    // MARK: Initializers
    /**
     Initializes a new data object from a hex string

     - Parameter hexString: String in hexadecimal format. Can start with "0x" or not and may have spaces. Any illegal (non hex) characters or an odd number of legal characters will cause it to fail.
     */
    public init?(hexString: String) {
        let noSpaces = hexString.replacingOccurrences(of: "\\s", with: "", options: .regularExpression, range: nil)
        guard noSpaces.count % 2 == 0 else {
            return nil
        }
        let hexArray = noSpaces.split(intoChunksOf: 2)

        var byteArray = [UInt8]()
        byteArray.reserveCapacity(hexString.count)
        for substringByte in hexArray {
            guard substringByte != "0x" else {
                continue
            }
            guard let byte = UInt8(substringByte, radix: 16) else {
                return nil
            }
            byteArray.append(byte)
        }
        self = Data(bytes: byteArray)
    }

    // MARK: managable data
    /**
    Provides the data as a hex string with formatting options.

    - Parameter hexLeader: Boolean determining if the string should start with "0x"
    - Parameter spaces: Boolean determining if there should be spaces inserted every 8 characters

    - Returns: String with hex encoded data
     */
    public func toHexString(hexLeader: Bool = false, spaces: Bool = false) -> String {
        let count = self.count / MemoryLayout<UInt8>.stride

        var hexString = ""
        if hexLeader {
            hexString = "0x"
        }
        self.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in
            for index in 0..<count {
                if spaces && (index % 4) == 0 && index != 0 && index != (count - 1) {
                    hexString += " "
                }
                hexString += String(format: "%02X", bytes[index])
            }
        }
        return hexString
    }

    /**
    Provides the data as an array of UInt8 bytes for easy manipulation.
     */
    public var bytesArray: [UInt8] {
        let count = self.count / MemoryLayout<UInt8>.stride
        let array = self.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> [UInt8] in
            var byteArray = [UInt8]()
            byteArray.reserveCapacity(count)
            for index in 0..<count {
                byteArray.append(bytes[index])
            }
            return byteArray
        }
        return array
    }
}

{'fraction_non_alphanumeric': 0.09668558872001465, 'fraction_numerical': 0.013733748397729353, 'mean_word_length': 1.9733260751224824, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, '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': '2962413', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1472, 'n_words': 505}