<commit_before>//
//  GAPManufacturerSpecificData.swift
//  Bluetooth
//
//  Created by <NAME> on 6/14/18.
//  Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation

/**
 The Manufacturer Specific data type is used for manufacturer specific data.
 The first two data octets shall contain a company identifier code from the Assigned Numbers - Company Identifiers document.
 The interpretation of any other octets within the data shall be defined by the manufacturer specified by the company identifier.
 
 Size: 2 or more octets
 The first 2 octets contain the Company Identifier Code followed by additional manufacturer specific data
 */
public struct GAPManufacturerSpecificData: GAPData {
    
    internal static let minimumLength = MemoryLayout<UInt16>.size
    
    public static let dataType: GAPDataType = .manufacturerSpecificData
    
    public var companyIdentifier: CompanyIdentifier
    
    public var additionalData: Data
    
    public init(companyIdentifier: CompanyIdentifier,
                additionalData: Data = Data()) {
        
        self.companyIdentifier = companyIdentifier
        self.additionalData = additionalData
    }
    
    public init?(data: Data) {
        
        guard data.count >= type(of: self).minimumLength
            else { return nil }
        
        let companyIdentifier = CompanyIdentifier(rawValue: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
        
        var additionalData = Data()
        
        if data.count > type(of: self).minimumLength {
            
            additionalData = Data(data.suffix(from: 2))
            
        } else {
            
            additionalData = Data()
        }
        
        self.init(companyIdentifier: companyIdentifier, additionalData: additionalData)
    }
    
    public var data: Data {
        
        let identifierBytes = companyIdentifier.rawValue.littleEndian.bytes
        
        return Data([identifierBytes.0, identifierBytes.1]) + additionalData
    }
}<commit_msg>#117 Refactored `GAPManufacturerSpecificData` data encoding<commit_after>//
//  GAPManufacturerSpecificData.swift
//  Bluetooth
//
//  Created by <NAME> on 6/14/18.
//  Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation

/**
 The Manufacturer Specific data type is used for manufacturer specific data.
 The first two data octets shall contain a company identifier code from the Assigned Numbers - Company Identifiers document.
 The interpretation of any other octets within the data shall be defined by the manufacturer specified by the company identifier.
 
 Size: 2 or more octets
 The first 2 octets contain the Company Identifier Code followed by additional manufacturer specific data
 */
public struct GAPManufacturerSpecificData: GAPData, Equatable {
    
    public static var dataType: GAPDataType { return .manufacturerSpecificData }
    
    public var companyIdentifier: CompanyIdentifier
    
    public var additionalData: Data
    
    public init(companyIdentifier: CompanyIdentifier,
                additionalData: Data = Data()) {
        
        self.companyIdentifier = companyIdentifier
        self.additionalData = additionalData
    }
}

public extension GAPManufacturerSpecificData {
    
    public init?(data: Data) {
        
        guard data.count >= 2
            else { return nil }
        
        self.companyIdentifier = CompanyIdentifier(rawValue: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
        self.additionalData = data.suffixCheckingBounds(from: 2)
    }
    
    public var data: Data {
        
       return Data(self)
    }
}

// MARK: - DataConvertible

extension GAPManufacturerSpecificData: DataConvertible {
    
    var dataLength: Int {
        
        return 2 + additionalData.count
    }
    
    static func += (data: inout Data, value: GAPManufacturerSpecificData) {
        
        data += value.companyIdentifier.rawValue.littleEndian
        data += value.additionalData
    }
}
{'fraction_non_alphanumeric': 0.06793206793206794, 'fraction_numerical': 0.011238761238761238, 'mean_word_length': 2.6642268984446478, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, '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': '2353748', 'n_tokens_mistral': 1061, 'n_tokens_neox': 995, 'n_words': 333}