1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-14 16:03:13 +01:00
libcproject/lib/string.c

356 lines
11 KiB
C
Raw Normal View History

2023-01-05 19:28:05 +01:00
#include "string.h"
size_t string_get_length(const string_t string_value) {
2023-01-05 19:28:05 +01:00
size_t length = 0;
2023-06-25 15:01:25 +02:00
while (string_value[length] != '\0') {
2023-01-05 19:28:05 +01:00
length++;
}
return length;
}
string_t string_to_uppercase(string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
character_append(result, character_to_upper(string_value[index]));
2023-01-05 19:28:05 +01:00
}
return result;
}
string_t string_to_lowercase(string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
character_append(result, character_to_lower(string_value[index]));
2023-01-05 19:28:05 +01:00
}
return result;
}
string_t string_replace(string_t string_value, char search, char replace) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
bool is_search_value = search == string_value[index];
2023-01-05 19:28:05 +01:00
if (is_search_value) {
character_append(result, replace);
} else {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
}
return result;
}
string_t string_trim_start(string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = 0;
2023-06-25 15:01:25 +02:00
while (string_value[index_space] == ' ') {
2023-01-05 19:28:05 +01:00
index_space++;
}
for (size_t index = index_space; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
return result;
}
string_t string_trim_end(string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = string_length - 1;
2023-06-25 15:01:25 +02:00
while (string_value[index_space] == ' ') {
2023-01-05 19:28:05 +01:00
index_space--;
}
for (size_t index = 0; index < index_space + 1; index++) {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
return result;
}
string_t string_trim(string_t string_value) {
string_t result = string_trim_start(string_value);
2023-01-05 19:28:05 +01:00
result = string_trim_end(result);
return result;
}
string_t string_copy(const string_t string_value) {
2023-06-25 15:01:25 +02:00
return string_substring(string_value, 0, string_get_length(string_value));
2023-01-05 19:28:05 +01:00
}
string_t string_capitalize(string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
bool is_first_character = index == 0;
if (is_first_character) {
2023-06-25 15:01:25 +02:00
character_append(result, character_to_upper(string_value[index]));
2023-01-05 19:28:05 +01:00
} else {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
}
return result;
}
size_t string_total_occurrences_of_character(string_t string_value, char character) {
2023-01-05 19:28:05 +01:00
size_t result = 0;
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
for (size_t index = 0; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
char current_character = string_value[index];
2023-01-05 19:28:05 +01:00
if (current_character == character) {
result += 1;
}
}
return result;
}
string_t string_reverse(const string_t string_value) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
size_t index = 0;
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (index = string_length - 1; index > 0; index--) {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
return result;
}
bool string_equals(const string_t string1, const string_t string2) {
2023-01-05 19:28:05 +01:00
size_t string1_length = string_get_length(string1);
size_t string2_length = string_get_length(string2);
bool is_equal = string1_length == string2_length;
for (size_t index = 0; index < string1_length && is_equal; index++) {
if (string1[index] != string2[index]) {
is_equal = false;
}
}
return is_equal;
}
bool string_get_is_integer(const string_t string_value) {
2023-01-05 19:28:05 +01:00
size_t index = 0;
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
bool is_integer = string_length >= 1;
2023-06-25 15:01:25 +02:00
if (is_integer && string_value[0] == '-') {
2023-01-05 19:28:05 +01:00
if (string_length == 1) {
is_integer = false;
} else {
index++;
}
}
while (index < string_length && is_integer) {
2023-06-25 15:01:25 +02:00
if (!character_get_is_digit(string_value[index])) {
2023-01-05 19:28:05 +01:00
is_integer = false;
}
index++;
}
return is_integer;
}
string_t* string_split(const string_t string_value, char separator, size_t* result_size) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
size_t index_string = 0;
size_t index_current = 0;
size_t index_result = 0;
string_t current = malloc(sizeof(string_t) * string_length);
string_t* result = malloc(sizeof(string_t*) * index_result);
2023-01-05 19:28:05 +01:00
if (result == NULL || current == NULL) {
exit(EXIT_FAILURE);
}
while (index_string < string_length) {
2023-06-25 15:01:25 +02:00
if (string_value[index_string] == separator) {
2023-01-05 19:28:05 +01:00
result[index_result] = string_copy(current);
index_result++;
2023-06-25 15:01:25 +02:00
result = realloc(result, sizeof(string_value) * index_result);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
index_current = 0;
current = string_copy("");
} else {
2023-06-25 15:01:25 +02:00
character_append(current, string_value[index_string]);
2023-01-05 19:28:05 +01:00
index_current++;
}
index_string++;
}
result[index_result] = string_copy(current);
free(current);
*result_size = index_result + 1;
return result;
}
string_t string_join(string_t* array, const char separator, size_t array_length) {
2023-01-05 19:28:05 +01:00
size_t string_length = array_length;
string_t string_value = malloc(sizeof(string_value) * string_length);
2023-06-25 15:01:25 +02:00
if (string_value == NULL) {
2023-01-05 19:28:05 +01:00
exit(EXIT_FAILURE);
}
for (size_t index_array = 0; index_array < array_length; index_array++) {
string_t substring = array[index_array];
2023-01-05 19:28:05 +01:00
size_t substring_length = string_get_length(substring);
string_length += substring_length;
2023-06-25 15:01:25 +02:00
string_value = realloc(string_value, sizeof(string_value) * string_length);
if (string_value == NULL) {
2023-01-05 19:28:05 +01:00
exit(EXIT_FAILURE);
}
for (size_t index_substring = 0; index_substring < substring_length; index_substring++) {
2023-06-25 15:01:25 +02:00
character_append(string_value, substring[index_substring]);
2023-01-05 19:28:05 +01:00
}
bool is_last_character = index_array == (array_length - 1);
if (!is_last_character) {
2023-06-25 15:01:25 +02:00
character_append(string_value, separator);
2023-01-05 19:28:05 +01:00
}
}
2023-06-25 15:01:25 +02:00
return string_value;
2023-01-05 19:28:05 +01:00
}
string_t string_concatenate(string_t string1, string_t string2) {
2023-01-05 19:28:05 +01:00
size_t string1_length = string_get_length(string1);
size_t string2_length = string_get_length(string2);
size_t result_length = string1_length + string2_length;
string_t result = malloc(sizeof(string_t) * result_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index_string1 = 0; index_string1 < string1_length; index_string1++) {
character_append(result, string1[index_string1]);
}
for (size_t index_string2 = 0; index_string2 < string2_length; index_string2++) {
character_append(result, string2[index_string2]);
}
return result;
}
bool string_get_has_unique_characters(const string_t string_value) {
2023-01-05 19:28:05 +01:00
bool has_unique = true;
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
struct dictionary* characters_already_seen = dictionary_initialization();
for (size_t index = 0; index < string_length && has_unique; index++) {
2023-06-25 15:01:25 +02:00
char character = string_value[index];
string_t key = convert_character_to_string(character);
2023-01-05 19:28:05 +01:00
if (dictionary_contains_key(characters_already_seen, key)) {
has_unique = false;
} else {
dictionary_add(characters_already_seen, key, (void*)true);
}
}
return has_unique;
}
string_t string_substring(const string_t string_value, size_t index_start, size_t index_end) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
for (size_t index = index_start; index <= index_end; index++) {
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
}
return result;
}
bool string_get_is_substring(const string_t string_value, const string_t substring) {
2023-01-05 19:28:05 +01:00
bool is_substring = false;
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
size_t substring_length = string_get_length(substring);
for (size_t index_string = 0; index_string < string_length && !is_substring; index_string++) {
size_t index_substring = 0;
size_t index_considered = index_string;
2023-06-25 15:01:25 +02:00
while (index_substring < substring_length && string_value[index_considered] == substring[index_substring]) {
2023-01-05 19:28:05 +01:00
index_substring++;
index_considered++;
}
is_substring = index_substring == substring_length;
}
return is_substring;
}
string_t string_get_formatted_number(const long long number, string_t separator) {
string_t number_string = convert_number_to_string(number);
2023-01-05 19:28:05 +01:00
bool is_negative = number_string[0] == '-';
if (is_negative) {
number_string = string_substring(number_string, 1, string_get_length(number_string));
}
size_t number_string_length = string_get_length(number_string);
string_t result = malloc(sizeof(string_t) * number_string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t count = 0;
for (size_t index = 0; index < number_string_length; index++) {
size_t index_reversed = number_string_length - index - 1;
count++;
result = string_concatenate(result, convert_character_to_string(number_string[index_reversed]));
if (count == 3) {
result = string_concatenate(result, separator);
count = 0;
}
}
result = string_reverse(result);
size_t result_length = string_get_length(result);
if (result_length % 4 == 0) {
result = string_substring(result, 1, result_length);
}
if (is_negative) {
result = string_concatenate(convert_character_to_string('-'), result);
}
return result;
}
string_t string_get_last_occurence_of_character(const string_t string_value, char character) {
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
string_t result = malloc(sizeof(string_t) * string_length);
2023-01-05 19:28:05 +01:00
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_result = 0;
for (size_t index = 0; index < string_length; index++) {
2023-06-25 15:01:25 +02:00
if (string_value[index] == character) {
2023-01-05 19:28:05 +01:00
index_result = 0;
result = string_copy("");
}
2023-06-25 15:01:25 +02:00
character_append(result, string_value[index]);
2023-01-05 19:28:05 +01:00
index_result++;
}
return result;
}
bool string_starts_with(const string_t string_value, const string_t prefix) {
2023-01-05 19:28:05 +01:00
bool starts_with = true;
size_t prefix_length = string_get_length(prefix);
for (size_t index = 0; index < prefix_length && starts_with; index++) {
2023-06-25 15:01:25 +02:00
starts_with = string_value[index] == prefix[index];
2023-01-05 19:28:05 +01:00
}
return starts_with;
}
bool string_ends_with(const string_t string_value, const string_t prefix) {
2023-01-05 19:28:05 +01:00
bool ends_with = true;
2023-06-25 15:01:25 +02:00
size_t string_length = string_get_length(string_value);
2023-01-05 19:28:05 +01:00
size_t prefix_length = string_get_length(prefix);
size_t index_string = string_length - 1;
size_t index_prefix = prefix_length - 1;
while (index_prefix > 0 && ends_with) {
2023-06-25 15:01:25 +02:00
ends_with = string_value[index_string] == prefix[index_prefix];
2023-01-05 19:28:05 +01:00
index_string--;
index_prefix--;
}
return ends_with;
}