2023-06-25 15:14:06 +02:00
|
|
|
#ifndef __LIBCPROJECT_STRING__
|
|
|
|
#define __LIBCPROJECT_STRING__
|
2023-01-05 19:28:05 +01:00
|
|
|
|
2023-08-07 00:11:07 +02:00
|
|
|
#include <errno.h>
|
2023-01-05 19:28:05 +01:00
|
|
|
#include <stdbool.h>
|
2023-01-07 19:38:01 +01:00
|
|
|
#include <stdio.h>
|
2023-01-05 19:28:05 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-01-07 19:38:01 +01:00
|
|
|
#include "character.h"
|
|
|
|
#include "convert.h"
|
2023-06-25 19:32:50 +02:00
|
|
|
#include "hash_map.h"
|
2023-06-25 15:07:34 +02:00
|
|
|
#include "types.h"
|
2023-06-25 15:01:25 +02:00
|
|
|
|
2023-01-05 19:28:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Return the length of a string (excluding '\0').
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return size_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
size_t string_get_length(const string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts all the alphabetic characters in a string to uppercase.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_to_uppercase(string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts all the alphabetic characters in a string to lowercase.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_to_lowercase(string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Replace all the occurrences of search value into replace value in the string.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-25 21:32:16 +02:00
|
|
|
* @param search A character search value.
|
|
|
|
* @param replace A character containing the text to replace for match.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_replace(string_t string, char search, char replace);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
2023-08-09 20:21:33 +02:00
|
|
|
/**
|
|
|
|
* @brief Removes all the occurrences of a character in a string.
|
|
|
|
*
|
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @param search A character search value.
|
|
|
|
* @since v4.1.0
|
|
|
|
*/
|
2023-08-10 00:32:49 +02:00
|
|
|
void string_remove_character(string_t string, char search);
|
2023-08-09 20:21:33 +02:00
|
|
|
|
2023-01-05 19:28:05 +01:00
|
|
|
/**
|
2023-08-05 14:19:44 +02:00
|
|
|
* @brief Removes all `character` from the start of a string.
|
2023-01-05 19:28:05 +01:00
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_trim_start(string_t string, char character);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-05 14:19:44 +02:00
|
|
|
* @brief Removes all `character` from the end of a string.
|
2023-01-05 19:28:05 +01:00
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_trim_end(string_t string, char character);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-05 14:19:44 +02:00
|
|
|
* @brief Removes all `character` from the start and end of a string.
|
2023-01-05 19:28:05 +01:00
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_trim(string_t string, char character);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the copy of a string.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
string_t string_copy(const string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Capitalizes the string.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_capitalize(string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the total number of occurrences of the given character in the string.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param character
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return size_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
size_t string_total_occurrences_of_character(string_t string, char character);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-06 23:17:07 +02:00
|
|
|
* @brief Reverse the characters in a string.
|
2023-01-05 19:28:05 +01:00
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* NOTE: Mutates the string.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
void string_reverse(const string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if two strings are equals.
|
|
|
|
*
|
|
|
|
* @param string1
|
|
|
|
* @param string2
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if the strings are equals.
|
|
|
|
* @return false if the strings are not equals.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-06-25 15:03:04 +02:00
|
|
|
bool string_equals(const string_t string1, const string_t string2);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-09 21:08:15 +02:00
|
|
|
* @brief Check if the string is an integer.
|
2023-01-05 19:28:05 +01:00
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if the string is an integer.
|
|
|
|
* @return false if the string is not an integer.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
bool string_get_is_integer(const string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Split a string into substrings using the specified separator and return them as an array and update the pointer `result_size` to the resulting size of the created array.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param separator
|
2023-06-25 21:32:16 +02:00
|
|
|
* @param result_size
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t*
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
string_t* string_split(const string_t string, char separator, size_t* result_size);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds all the elements of an array into a string, separated by the specified separator string.
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
* @param separator
|
2023-06-25 21:32:16 +02:00
|
|
|
* @param array_length
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-06-25 15:03:04 +02:00
|
|
|
string_t string_join(string_t* array, const char separator, size_t array_length);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Concatenate two strings.
|
|
|
|
*
|
2023-08-07 00:42:11 +02:00
|
|
|
* NOTE: Mutates the string `destination`.
|
|
|
|
*
|
|
|
|
* @param destination
|
|
|
|
* @param source
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-07 00:42:11 +02:00
|
|
|
void string_concatenate(string_t* destination, string_t source);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if a string contains only unique characters.
|
|
|
|
*
|
|
|
|
* @param string
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if string contains only unique characters.
|
|
|
|
* @return false if string contains duplicate characters.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-06-25 15:03:04 +02:00
|
|
|
bool string_get_has_unique_characters(const string_t string);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the part of the string between the start and end indexes (both included).
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param index_start
|
|
|
|
* @param index_end
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
string_t string_substring(const string_t string, size_t index_start, size_t index_end);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if a string contains a substring.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param substring
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if the string contains the substring.
|
|
|
|
* @return false if the string does not contain the substring.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
bool string_get_is_substring(const string_t string, const string_t substring);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Format a number to a string with specified separator.
|
|
|
|
*
|
2023-06-25 21:32:16 +02:00
|
|
|
* @param number
|
|
|
|
* @param separator
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t
|
2024-09-13 15:35:19 +02:00
|
|
|
*
|
2023-06-25 21:32:16 +02:00
|
|
|
* @code
|
|
|
|
* string_get_formatted_number(1000, " ") // "1 000"
|
2024-09-15 18:25:01 +02:00
|
|
|
*
|
2023-06-25 21:32:16 +02:00
|
|
|
* string_get_formatted_number(1000, ",") // "1,000"
|
|
|
|
* @endcode
|
2023-08-09 21:08:15 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2024-09-25 14:26:15 +02:00
|
|
|
string_t string_get_formatted_number(const int64_t number, string_t separator);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the last occurrence of character in the string.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param character
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
string_t string_get_last_occurence_of_character(const string_t string, char character);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if a string starts with a substring.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param prefix
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if the string starts with the substring.
|
|
|
|
* @return false if the string does not start with the substring.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
bool string_starts_with(const string_t string, const string_t prefix);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if a string ends with a substring.
|
|
|
|
*
|
2023-08-06 23:17:07 +02:00
|
|
|
* @param string
|
2023-01-05 19:28:05 +01:00
|
|
|
* @param prefix
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true if the string ends with the substring.
|
|
|
|
* @return false if the string does not end with the substring.
|
2023-06-24 21:06:45 +02:00
|
|
|
* @since v1.0.0
|
2023-01-05 19:28:05 +01:00
|
|
|
*/
|
2023-08-06 23:17:07 +02:00
|
|
|
bool string_ends_with(const string_t string, const string_t prefix);
|
2023-01-05 19:28:05 +01:00
|
|
|
|
2023-12-26 20:30:54 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns the position (index + 1) within the string of the first occurrence of the specified substring (0 if not found).
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @param substring
|
|
|
|
* @return size_t
|
2024-09-13 15:35:19 +02:00
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* string_position_of("hello world", 'e') // 2
|
|
|
|
* @endcode
|
2023-12-26 20:30:54 +01:00
|
|
|
* @since v4.2.0
|
|
|
|
*/
|
|
|
|
size_t string_position_of(const string_t string, const char character);
|
|
|
|
|
2023-12-26 20:40:46 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns the position (index + 1) within the string of the last occurrence of the specified substring (0 if not found).
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @param character
|
|
|
|
* @return size_t
|
2024-09-13 15:35:19 +02:00
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* string_last_position_of("hello world", 'o') // 8
|
|
|
|
* @endcode
|
2023-12-26 20:40:46 +01:00
|
|
|
* @since v4.2.0
|
|
|
|
*/
|
|
|
|
size_t string_last_position_of(const string_t string, const char character);
|
|
|
|
|
2024-09-12 12:22:53 +02:00
|
|
|
/**
|
|
|
|
* @brief Pads a `string` with another `pad_string` (multiple times, if needed) until the resulting string reaches the `target_length`. The padding is applied from the start (left) of the string.
|
|
|
|
*
|
|
|
|
* @param string The string to pad.
|
|
|
|
* @param pad_string The string to pad the current string with, to the left.
|
|
|
|
* @param target_length
|
|
|
|
* @return string_t
|
2024-09-13 15:35:19 +02:00
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* string_pad_start("hello", " ", 10) // " hello"
|
|
|
|
* @endcode
|
2024-09-15 18:43:39 +02:00
|
|
|
* @since v4.3.0
|
2024-09-12 12:22:53 +02:00
|
|
|
*/
|
|
|
|
string_t string_pad_start(const string_t string, const string_t pad_string, size_t target_length);
|
|
|
|
|
2024-09-12 12:31:58 +02:00
|
|
|
/**
|
|
|
|
* @brief Pad a number with zeros.
|
|
|
|
*
|
|
|
|
* @param number
|
|
|
|
* @param places
|
|
|
|
* @return string_t
|
2024-09-13 15:35:19 +02:00
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* string_zero_pad(1, 2) // "01"
|
|
|
|
*
|
|
|
|
* string_zero_pad(10, 2) // "10"
|
|
|
|
* @endcode
|
2024-09-15 18:43:39 +02:00
|
|
|
* @since v4.3.0
|
2024-09-12 12:31:58 +02:00
|
|
|
*/
|
|
|
|
string_t string_zero_pad(uint64_t number, size_t places);
|
|
|
|
|
2023-01-05 19:28:05 +01:00
|
|
|
#endif
|