<commit_before>#include "StdAfx.h"
#include "Util/EncodingTable.h"
#include "Util/Util.h"
#include "Core/Common.h"
#include "Core/MathParser.h"

#define MAXHEXLENGTH 32

EncodingTable::EncodingTable()
{

}

EncodingTable::~EncodingTable()
{

}

void EncodingTable::clear()
{
	hexData.clear();
	valueData.clear();
	entries.clear();
}

int parseHexString(std::wstring& hex, unsigned char* dest)
{
	for (size_t i = 0; i < hex.size(); i++)
	{
		wchar_t source = towlower(hex[i]);
		int value;

		if (source >= 'a' && source <= 'f')
		{
			value = source-'a'+10;
		} else if (source >= '0' && source <= '9')
		{
			value = source-'0';
		} else {
			return -1;
		}

		int index = i/2;
		if (i % 2)
			dest[index] = (dest[index] << 4) | value;
		else
			dest[index] = value;
	}

	return hex.size()/2;
}

bool EncodingTable::load(const std::wstring& fileName, TextFile::Encoding encoding)
{
	unsigned char hexBuffer[MAXHEXLENGTH];

	TextFile input;
	if (input.open(fileName,TextFile::Read,encoding) == false)
		return false;

	hexData.clear();
	valueData.clear();
	setTerminationEntry((unsigned char*)"\0",1);

	while (!input.atEnd())
	{
		std::wstring line = input.readLine();
		if (line.empty() || line[0] == '*') continue;
		
		if (line[0] == '/')
		{
			std::wstring hex = line.substr(1);
			if (hex.empty() || hex.length() > 2*MAXHEXLENGTH)
			{
				// error<commit_msg>Clear table entries on loading table, fixes #12<commit_after>#include "StdAfx.h"
#include "Util/EncodingTable.h"
#include "Util/Util.h"
#include "Core/Common.h"
#include "Core/MathParser.h"

#define MAXHEXLENGTH 32

EncodingTable::EncodingTable()
{

}

EncodingTable::~EncodingTable()
{

}

void EncodingTable::clear()
{
	hexData.clear();
	valueData.clear();
	entries.clear();
}

int parseHexString(std::wstring& hex, unsigned char* dest)
{
	for (size_t i = 0; i < hex.size(); i++)
	{
		wchar_t source = towlower(hex[i]);
		int value;

		if (source >= 'a' && source <= 'f')
		{
			value = source-'a'+10;
		} else if (source >= '0' && source <= '9')
		{
			value = source-'0';
		} else {
			return -1;
		}

		int index = i/2;
		if (i % 2)
			dest[index] = (dest[index] << 4) | value;
		else
			dest[index] = value;
	}

	return hex.size()/2;
}

bool EncodingTable::load(const std::wstring& fileName, TextFile::Encoding encoding)
{
	unsigned char hexBuffer[MAXHEXLENGTH];

	TextFile input;
	if (input.open(fileName,TextFile::Read,encoding) == false)
		return false;

	hexData.clear();
	valueData.clear();
	entries.clear();
	setTerminationEntry((unsigned char*)"\0",1);

	while (!input.atEnd())
	{
		std::wstring line = input.readLine();
		if (line.empty() || line[0] == '*') continue;
		
		if (line[0] == '/')
		{
			std::wstring hex = line.substr(1);
			if (hex.empty() || hex.length() > 2*MAXHEXLENGTH)
			{
				// error
{'fraction_non_alphanumeric': 0.19095477386934673, 'fraction_numerical': 0.014357501794687724, 'mean_word_length': 3.863874345549738, 'pattern_counts': {'":': 0, '<': 13, '<?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': '2093236', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1057, 'n_words': 227}