<commit_before>/**
  * Touhou Community Reliant Automatic Patcher
  * Tasogare Frontier support plugin
  *
  * ----
  *
  * On-the-fly th105 pl patcher
  */

#include <thcrap.h>
#include "thcrap_tasofro.h"
#include "cv0.h"

TasofroCv0::LineType TasofroCv0::guessLineType(const char* file, size_t size)
{
	for (size_t i = 0; i < size && file[i] != '\n'; i++) {
		if (file[i] == '#') {
			return EMPTY;
		}
		else if (file[i] == ':') {
			return COMMAND;
		}
	}
	for (size_t i = 0; i < size && file[i] != '\n'; i++) {
		if (file[i] != '\r' && file[i] != '\t' && file[i] != ' ') {
			return TEXT;
		}
	}
	return EMPTY;
}

TasofroCv0::ALine* TasofroCv0::readLine(const char*& file, size_t& size)
{
	switch (guessLineType(file, size)) {
	case EMPTY:
		return Empty::read(file, size);
	case COMMAND:
		return Command::read(file, size);
	case TEXT:
		return Text::read(file, size);
	default:
		return nullptr; // Can't happen
	}
}



std::string TasofroCv0::ALine::readLine(const char*& file, size_t& size)
{
	std::string out;
	size_t i = 0;
	while (i < size && file[i] != '\n') {
		i++;
	}
	if (i > 0 && file[i - 1] == '\r') {
		out.assign(file, i - 1);
	}
	else {
		out.assign(file, i);
	}
	if (i < size) {
		i++;
	}

	file += i;
	size -= i;
	return out;
}



TasofroCv0::LineType TasofroCv0::Empty::getType() const
{
	return EMPTY;
}
TasofroCv0::LineType TasofroCv0::Command::getType() const
{
	return COMMAND;
}
TasofroCv0::LineType TasofroCv0::Text::getType() const
{
	return TEXT;
}


TasofroCv0::ALine::ALine()
{}

std::string TasofroCv0::ALine::unescape(const std::string& in) const
{
	bool is_in_quote = false;
	std::string out;

	for (size_t pos = 0; pos < in.size(); pos++) {
		if (in[pos] == '\\' && in[pos + 1] == ',') {
			pos++;
		}

		out += in[pos];
	}
	return out;
}

std::string TasofroCv0::ALine::escape(const std::string& in) const
{
	std::string out;

	for (size_t i = 0; i < in.size(); i++) {
		if (in[i] == ',') {
			out += "\\,";
		}
		/*
		else if (in.compare(i, 7, "{{ruby|") == 0) {
			i += 7;
			out += "\\R[";
			while (i < in.size() && in.compare(i, 2, "}}") != 0) {
				out += in[i];
				i++;
			}
			out += ']';
			i++; // Actually i += 2, because it will be incremented once again in the for loop.
		}
		*/
		else {
			out += in[i];
		}
	}
<commit_msg>thcrap_tasofro: cv0: add ruby support. [V]<commit_after>/**
  * Touhou Community Reliant Automatic Patcher
  * Tasogare Frontier support plugin
  *
  * ----
  *
  * On-the-fly th105 pl patcher
  */

#include <thcrap.h>
#include "thcrap_tasofro.h"
#include "cv0.h"

TasofroCv0::LineType TasofroCv0::guessLineType(const char* file, size_t size)
{
	for (size_t i = 0; i < size && file[i] != '\n'; i++) {
		if (file[i] == '#') {
			return EMPTY;
		}
		else if (file[i] == ':') {
			return COMMAND;
		}
	}
	for (size_t i = 0; i < size && file[i] != '\n'; i++) {
		if (file[i] != '\r' && file[i] != '\t' && file[i] != ' ') {
			return TEXT;
		}
	}
	return EMPTY;
}

TasofroCv0::ALine* TasofroCv0::readLine(const char*& file, size_t& size)
{
	switch (guessLineType(file, size)) {
	case EMPTY:
		return Empty::read(file, size);
	case COMMAND:
		return Command::read(file, size);
	case TEXT:
		return Text::read(file, size);
	default:
		return nullptr; // Can't happen
	}
}



std::string TasofroCv0::ALine::readLine(const char*& file, size_t& size)
{
	std::string out;
	size_t i = 0;
	while (i < size && file[i] != '\n') {
		i++;
	}
	if (i > 0 && file[i - 1] == '\r') {
		out.assign(file, i - 1);
	}
	else {
		out.assign(file, i);
	}
	if (i < size) {
		i++;
	}

	file += i;
	size -= i;
	return out;
}



TasofroCv0::LineType TasofroCv0::Empty::getType() const
{
	return EMPTY;
}
TasofroCv0::LineType TasofroCv0::Command::getType() const
{
	return COMMAND;
}
TasofroCv0::LineType TasofroCv0::Text::getType() const
{
	return TEXT;
}


TasofroCv0::ALine::ALine()
{}

std::string TasofroCv0::ALine::unescape(const std::string& in) const
{
	bool is_in_quote = false;
	std::string out;

	for (size_t pos = 0; pos < in.size(); pos++) {
		if (in[pos] == '\\' && in[pos + 1] == ',') {
			pos++;
		}

		out += in[pos];
	}
	return out;
}

std::string TasofroCv0::ALine::escape(const std::string& in) const
{
	std::string out;

	for (size_t i = 0; i < in.size(); i++) {
		if (in[i] == ',') {
			out += "\\,";
		}
		else if (in.compare(i, 7, "{{ruby|") == 0) {
			i += 7;
			std::string top;
			std::string bot;
			while (i < in.size() && in[i] != '|') {
				bot += in[i];
				i++;
			}
			i++;
			while (i < in.size() && in.compare(i, 2, "}}") != 0) {
				top += in[i];
				i++;
			}
			out += std::string("<ruby ") + top + ">" + bot + "</ruby>";
			i++; // Actually i += 2, because it will be incremented once again in the for loop.
		}
		else {
			out += in[i];
		}
	}

{'fraction_non_alphanumeric': 0.2167019027484144, 'fraction_numerical': 0.014164904862579281, 'mean_word_length': 3.1427320490367774, 'pattern_counts': {'":': 0, '<': 22, '<?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': '7119865', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2064, 'n_words': 475}