<commit_before>//
//  SVDFileModel.swift
//  Jupiter80Librarian
//
//  Created by <NAME> on 07/12/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Cocoa

enum SVDFileFormat {
	case Unknown
	case Jupiter80
	case Jupiter50
}

struct SVDBytes {
	let location: Int
	let bytes: [UInt8]
}

private let kBytesSVD = SVDBytes(location: 0x2, bytes: [0x53, 0x56, 0x44])
private let kBytesREG = SVDBytes(location: 0xA, bytes: [0x52, 0x45, 0x47])
private let kBytesJPTR = SVDBytes(location: 0xE, bytes: [0x4A, 0x50, 0x54, 0x52])
private let kBytesJP50 = SVDBytes(location: 0xE, bytes: [0x4A, 0x50, 0x35, 0x30])
private let kBytesVCL = SVDBytes(location: 0x40, bytes: [0x56, 0x43, 0x4C])
private let kBytesSYS = SVDBytes(location: 0x50, bytes: [0x53, 0x59, 0x53])
private let kBytesRBN = SVDBytes(location: 0x60, bytes: [0x52, 0x42, 0x4E])

private let kLocationNrOfRegs = 0x40

class SVDFile: NSObject {
	private var fileData: NSData
	private var fileFormat: SVDFileFormat
	private var isFileValid: Bool
	private var headerOffset: Int

	init(fileData: NSData) {
		self.fileData = fileData
		self.fileFormat = .Unknown
		self.isFileValid = false
		self.headerOffset = 0x0

		super.init()

		self.isFileValid = self.checkValidityOfData(self.fileData)

		if !self.isFileValid {
			return;
		}

		self.findHeaderOffset()
	}

	private func compareData(byteStruct: SVDBytes) -> Bool {
		let byteRange = NSRange(location: byteStruct.location, length: byteStruct.bytes.count)
		let byteData = self.fileData.subdataWithRange(byteRange)
		let byteCheck = NSData(bytes: byteStruct.bytes, length: byteStruct.bytes.count)

		return byteData.isEqualToData(byteCheck)
	}

	private func checkValidityOfData(fileData: NSData) -> Bool {
		// Check for string SVD
		let isSVDFile = self.compareData(kBytesSVD)

		if !isSVDFile {
			return false
		}

		// Check for string REG
		let isRegFile = self.compareData(kBytesREG)

		if isRegFile {
			// Check for string JPTR
			let isJupiter80File = self.compareData(kBytesJPTR)

			if isJupiter80File {
				self.fileFormat = .Jupiter80
			} else {
				// Check for string JP50
				let isJupiter50File = self.compareData(kBytesJP50)

				if isJupiter50File {
					self.fileFormat = .Jupiter50
				}
			}
		} else {
			return false
		}

		return true;
	}

	private func findHeaderOffset() {
		let hasVCLPart = self.compareData(kBytesVCL)

		if hasVCLPart {
			self.headerOffset += 0x10
		}

		let hasSYSPart = self.compareData(kBytesSYS)

		if hasSYSPart {
			self.headerOffset += 0x10
		}

		let hasRBNPart = self.compareData(kBytesRBN)

		if hasRBNPart {
			self.headerOffset += 0x10
		}
	}
}<commit_msg>Expanded SVD byte structure to allow specifying length if not specifying byte array (where length is then implicit).<commit_after>//
//  SVDFileModel.swift
//  Jupiter80Librarian
//
//  Created by <NAME> on 07/12/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Cocoa

enum SVDFileFormat {
	case Unknown
	case Jupiter80
	case Jupiter50
}

struct SVDBytes {
	let location: Int
	let bytes: [UInt8]
	let length: Int

	init(location: Int, bytes: [UInt8]) {
		self.location = location
		self.bytes = bytes
		self.length = self.bytes.count
	}

	init(location: Int, length: Int) {
		self.location = location
		self.length = length
		self.bytes = []
	}
}

private let kBytesSVD = SVDBytes(location: 0x2, bytes: [0x53, 0x56, 0x44])
private let kBytesREG = SVDBytes(location: 0xA, bytes: [0x52, 0x45, 0x47])
private let kBytesJPTR = SVDBytes(location: 0xE, bytes: [0x4A, 0x50, 0x54, 0x52])
private let kBytesJP50 = SVDBytes(location: 0xE, bytes: [0x4A, 0x50, 0x35, 0x30])
private let kBytesVCL = SVDBytes(location: 0x40, bytes: [0x56, 0x43, 0x4C])
private let kBytesSYS = SVDBytes(location: 0x50, bytes: [0x53, 0x59, 0x53])
private let kBytesRBN = SVDBytes(location: 0x60, bytes: [0x52, 0x42, 0x4E])

private let kBytesRegLength = SVDBytes(location: 0x40, length: 4)

class SVDFile: NSObject {
	private var fileData: NSData
	private var fileFormat: SVDFileFormat
	private var isFileValid: Bool
	private var headerOffset: Int

	init(fileData: NSData) {
		self.fileData = fileData
		self.fileFormat = .Unknown
		self.isFileValid = false
		self.headerOffset = 0x0

		super.init()

		self.isFileValid = self.checkValidityOfData(self.fileData)

		if !self.isFileValid {
			return;
		}

		self.findHeaderOffset()
		self.findPartLengths()
	}

	func dataFromByteStruct(byteStruct: SVDBytes) -> NSData {
		let byteRange = NSRange(location: byteStruct.location, length: byteStruct.length)
		let byteData = self.fileData.subdataWithRange(byteRange)

		return byteData
	}

	func compareData(byteStruct: SVDBytes) -> Bool {
		let byteData = self.dataFromByteStruct(byteStruct)
		let byteCheck = NSData(bytes: byteStruct.bytes, length: byteStruct.length)

		return byteData.isEqualToData(byteCheck)
	}

	private func checkValidityOfData(fileData: NSData) -> Bool {
		// Check for string SVD
		let isSVDFile = self.compareData(kBytesSVD)

		if !isSVDFile {
			return false
		}

		// Check for string REG
		let isRegFile = self.compareData(kBytesREG)

		if isRegFile {
			// Check for string JPTR
			let isJupiter80File = self.compareData(kBytesJPTR)

			if isJupiter80File {
				self.fileFormat = .Jupiter80
			} else {
				// Check for string JP50
				let isJupiter50File = self.compareData(kBytesJP50)

				if isJupiter50File {
					self.fileFormat = .Jupiter50
				}
			}
		} else {
			return false
		}

		return true;
	}

	private func findHeaderOffset() {
		let hasVCLPart = self.compareData(kBytesVCL)

		if hasVCLPart {
			self.headerOffset += 0x10
		}

		let hasSYSPart = self.compareData(kBytesSYS)

		if hasSYSPart {
			self.headerOffset += 0x10
		}

		let hasRBNPart = self.compareData(kBytesRBN)

		if hasRBNPart {
			self.headerOffset += 0x10
		}
	}

	private func findPartLengths() {
		let regLengthData = self.dataFromByteStruct(kBytesRegLength)

	}
}
{'fraction_non_alphanumeric': 0.09960970643135925, 'fraction_numerical': 0.04378075683013745, 'mean_word_length': 3.879139072847682, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '6799863', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2127, 'n_words': 531}