/*
 * 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 Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef IBIS_STRING_H
#define IBIS_STRING_H

#include <glib.h>

#include "ibisversion.h"

G_BEGIN_DECLS

/**
 * ibis_str_is_empty:
 * @str: (nullable): the string to check
 *
 * Determines if @str is empty or not.
 *
 * Returns: true if @str is empty; false otherwise.
 *
 * Since: 0.1
 */
IBIS_AVAILABLE_STATIC_INLINE_IN_0_1
static inline gboolean
ibis_str_is_empty(const char *str) {
	return (str == NULL || str[0] == '\0');
}

/**
 * ibis_str_equal:
 * @str1: (nullable): the first string
 * @str2: (nullable): the second string
 *
 * Checks if @str1 is equal to @str2.
 *
 * Returns: true if the strings are equal; false otherwise.
 *
 * Since: 0.1
 */
IBIS_AVAILABLE_STATIC_INLINE_IN_0_1
static inline gboolean
ibis_str_equal(const char *str1, const char *str2) {
	return (g_strcmp0(str1, str2) == 0);
}

/**
 * ibis_str_to_uint:
 * @str: (nullable): a string to convert to an unsigned integer
 *
 * Converts @str to an unsigned integer.
 *
 * Returns: The converted value or 0 on error.
 *
 * Since: 0.3
 */
IBIS_AVAILABLE_STATIC_INLINE_IN_0_3
static inline guint
ibis_str_to_uint(const char *str) {
	int ivalue;

	if(ibis_str_is_empty(str)) {
		return 0;
	}

	ivalue = atoi(str);

	return (guint)ivalue;
}

G_END_DECLS

#endif /* IBIS_STRING_H */
