Скачиваний:
2
Добавлен:
15.08.2023
Размер:
2.9 Кб
Скачать
#include "tString.h"

#include <iostream>
#include <string>

void tString::tstrcpy( 
	_In_ char* buffer, 
	_In_z_ const char* string)
{
	for (unsigned int i{}; i < this->capacity; i++){
		this->str_buffer[i] = string[i];
	}
}

tString::tString()
{
	this->length = 0;
	this->capacity = length + 20;

	this->str_buffer = new char [capacity];
}

tString::tString(_In_z_ const char* str)
{	
	this->length = strlen(str);
	this->capacity = length + 20;

	this->str_buffer = new char[capacity];
	tstrcpy(this->str_buffer, str);
}

tString::tString(_In_ tString& tstr)
{
	const char* temp = tstr.str_buffer;

	this->length = tstr.length;
	this->capacity = tstr.capacity;

	::free(this->str_buffer);
	this->str_buffer = new char[capacity];
	tstrcpy(this->str_buffer, temp);
}

tString::tString(_In_z_ std::ifstream& file)
{
	//Get file length
	file.seekg(0, file.end);
	this->length = file.tellg();
	this->capacity = length + 20;
	file.seekg(0, file.beg);

	this->str_buffer = new char[capacity];

	file.read(str_buffer, length);
	file.close();
}

const char* tString::cstr()
{
	return (const char*)str_buffer;
}

unsigned int tString::DirectSearch(
	_In_z_ const char* substr,
	_In_ unsigned int pose)
{
	const unsigned int n = this->length - pose;
	const unsigned int nSubLength = strlen(substr);

	assert(nSubLength > 0);
	for (unsigned int i, j = 0,
		end = n - nSubLength;
		j <= end; ++j)
	{
		for (i = 0; i < nSubLength && this->str_buffer[i + j + pose] == substr[i]; ++i);
		if (i < nSubLength) continue;
		return j + pose;
	}
	return -1;
}

unsigned int tString::BrootForce(
	_In_z_ const char* substr,
	_In_ unsigned int pose
)
{
	unsigned int nSubLength = strlen(substr);
	unsigned int point;

	for (unsigned int i{}; i < length; i++)
	{
		if (str_buffer[i] == substr[0])
		{
			point = i;
			for (unsigned int j{}; j < nSubLength; j++)
			{
				if (str_buffer[i] == substr[j]);
				else break;
				if (j == nSubLength-1) {
					return point;
				}
				i++;
			}
		}
	}

	return -1;
}

unsigned int tString::BMSearch(
	_In_z_ const char* substr,
	_In_ unsigned int pose
)
{
	unsigned int nSubLength = strlen(substr);

	unsigned int* d = new unsigned int[256];
	for (unsigned int i{}; i < 256; i++) 
	{
		d[i] = nSubLength;
	}
	for (unsigned int i{}; i < nSubLength - 1; i++)
	{
		d[(unsigned char)substr[i]] = nSubLength - i - 1;
	}
	int result = -1;
	for (unsigned int i = nSubLength; i <= length; i += d[(unsigned char)str_buffer[i - 1]])
	{
		unsigned int j, k;

		for (j = nSubLength - 1, k = i - 1;
			j >= 0 && str_buffer[k] == substr[j]; k--, j--);
		if (j < 0)
		{
			result = i - nSubLength;
			break;
		}
	}

	delete[]d;
	if (result > 200001)
	{
		return -1;
	}
	return result;
}

bool tString::find(_In_ const char* str)
{
	return true;
}
Соседние файлы в папке 2