/*
 * Ibis - IRCv3 Library
 * Copyright (C) 2022-2024 Ibis Developers
 *
 * Ibis is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "ibisnormalize.h"
#include "ibisstring.h"

/******************************************************************************
 * Helpers
 *****************************************************************************/
static gboolean
ibis_normalize_unichar_is_upper_letter(gunichar uchar) {
	return uchar >= 'A' && uchar <= 'Z';
}

static gboolean
ibis_normalize_unichar_is_rfc1459_strict(gunichar uchar) {
	return ibis_normalize_unichar_is_upper_letter(uchar) ||
		uchar == '[' ||
		uchar == ']' ||
		uchar == '\\';
}

static gboolean
ibis_normalize_unichar_is_rfc1459(gunichar uchar) {
	return ibis_normalize_unichar_is_rfc1459_strict(uchar) ||
		uchar == '~';
}

static gunichar
ibis_normalize_rfc1459_strict_tolower(gunichar uchar) {
	switch(uchar) {
	case '[':
		return '{';
	case ']':
		return '}';
	case '\\':
		return '|';
	}

	return g_unichar_tolower(uchar);
}

static gunichar
ibis_normalize_rfc1459_tolower(gunichar uchar) {
	if(uchar == '~') {
		return '^';
	}

	return ibis_normalize_rfc1459_strict_tolower(uchar);
}

static char *
ibis_normalize_string(const char *input,
                      gboolean (*needs_normalization)(gunichar),
                      gunichar (*normalize)(gunichar))
{
	GString *normalized = NULL;
	const char *input_ptr = NULL;

	normalized = g_string_new("");
	if(ibis_str_is_empty(input)) {
		return g_string_free_and_steal(normalized);
	}

	input_ptr = input;
	while(*input_ptr) {
		gunichar uchar = g_utf8_get_char(input_ptr);

		if(needs_normalization(uchar)) {
			g_string_append_unichar(normalized, normalize(uchar));
		} else {
			g_string_append_unichar(normalized, uchar);
		}

		input_ptr = g_utf8_next_char(input_ptr);
	}

	return g_string_free_and_steal(normalized);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
char *
ibis_normalize_ascii(const char *input) {
	return ibis_normalize_string(input, ibis_normalize_unichar_is_upper_letter,
	                             g_unichar_tolower);
}

char *
ibis_normalize_rfc1459(const char *input) {
	return ibis_normalize_string(input, ibis_normalize_unichar_is_rfc1459,
	                             ibis_normalize_rfc1459_tolower);
}

char *
ibis_normalize_rfc1459_strict(const char *input) {
	return ibis_normalize_string(input, ibis_normalize_unichar_is_rfc1459_strict,
	                             ibis_normalize_rfc1459_strict_tolower);
}
