2021-11-10 20:09:01 +01:00
|
|
|
#include "string.h"
|
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
size_t string_get_length(const char* string) {
|
|
|
|
size_t length = 0;
|
|
|
|
while (string[length] != '\0') {
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
2021-11-10 20:09:01 +01:00
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
void string_to_lowercase(char* string) {
|
|
|
|
size_t string_length = string_get_length(string);
|
|
|
|
for (size_t index = 0; index < string_length; index++) {
|
|
|
|
string[index] = character_to_lower(string[index]);
|
|
|
|
}
|
|
|
|
string[string_length] = '\0';
|
|
|
|
}
|
2021-11-10 20:09:01 +01:00
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
void string_trim_start(char* string, char character) {
|
|
|
|
size_t string_length = string_get_length(string);
|
2021-11-10 20:09:01 +01:00
|
|
|
size_t index_space = 0;
|
|
|
|
while (string[index_space] == character) {
|
|
|
|
index_space++;
|
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
for (size_t index = 0; index < string_length - index_space; index++) {
|
|
|
|
string[index] = string[index + index_space];
|
2021-11-10 20:09:01 +01:00
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
string[string_length - index_space] = '\0';
|
2021-11-10 20:09:01 +01:00
|
|
|
}
|
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
void string_trim_end(char* string, char character) {
|
|
|
|
size_t string_length = string_get_length(string);
|
2021-11-10 20:09:01 +01:00
|
|
|
size_t index_space = string_length - 1;
|
|
|
|
while (string[index_space] == character) {
|
|
|
|
index_space--;
|
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
string[index_space + 1] = '\0';
|
2021-11-10 20:09:01 +01:00
|
|
|
}
|
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
void string_trim(char* string, char character) {
|
|
|
|
string_trim_start(string, character);
|
|
|
|
string_trim_end(string, character);
|
2021-11-10 20:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char* string_slugify(char* string) {
|
|
|
|
size_t string_length = strlen(string);
|
|
|
|
char* result = malloc(sizeof(char) * (string_length + 1));
|
2023-08-10 11:13:06 +02:00
|
|
|
result[0] = '\0';
|
|
|
|
size_t words = 0;
|
2021-11-10 20:09:01 +01:00
|
|
|
char* current = malloc(sizeof(char) * (string_length + 1));
|
2023-08-10 11:13:06 +02:00
|
|
|
current[0] = '\0';
|
|
|
|
size_t current_index = 0;
|
2021-11-10 20:09:01 +01:00
|
|
|
for (size_t index = 0; index < string_length; index++) {
|
|
|
|
if (string[index] == ' ' || (string[index] == '-' && strlen(current) > 0)) {
|
|
|
|
strcat(result, current);
|
|
|
|
character_append(result, '-');
|
|
|
|
memset(current, 0, sizeof(char) * (string_length + 1));
|
2023-08-10 11:13:06 +02:00
|
|
|
current_index = 0;
|
|
|
|
words += 1;
|
2021-11-10 20:09:01 +01:00
|
|
|
} else {
|
|
|
|
if (character_is_alphanumeric(string[index])) {
|
2023-08-10 11:13:06 +02:00
|
|
|
current[current_index] = string[index];
|
|
|
|
current_index += 1;
|
|
|
|
current[current_index] = '\0';
|
2021-11-10 20:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcat(result, current);
|
|
|
|
free(current);
|
|
|
|
return result;
|
|
|
|
}
|