1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

feat: add string type

This commit is contained in:
Théo LUDWIG 2023-06-25 15:01:25 +02:00
parent ce088f8ff5
commit 8f3ee199e5
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
13 changed files with 172 additions and 194 deletions

View File

@ -97,9 +97,9 @@ touch main.c
#include "libcproject/libcproject.h"
int main() {
char* string = "Hello, world!";
printf("%s\n", string);
printf("string_length = %ld\n", string_get_length(string)); // `string_get_length` is a function from `libcproject`
string string_value = "Hello, world!"; // `string` is a typedef from `libcproject`
printf("%s\n", string_value);
printf("string_length = %ld\n", string_get_length(string_value)); // `string_get_length` is a function from `libcproject`
return EXIT_SUCCESS;
}
```

View File

@ -1,17 +1,17 @@
#include "character.h"
void character_append(char* string, const char character) {
size_t length = string_get_length(string);
character_append_at(string, character, length);
void character_append(string string_value, const char character) {
size_t length = string_get_length(string_value);
character_append_at(string_value, character, length);
}
void character_append_at(char* string, const char character, const size_t index) {
size_t length = string_get_length(string);
void character_append_at(string string_value, const char character, const size_t index) {
size_t length = string_get_length(string_value);
for (size_t index_string = length; index_string > index; index_string--) {
string[index_string] = string[index_string - 1];
string_value[index_string] = string_value[index_string - 1];
}
string[index] = character;
string[length + 1] = '\0';
string_value[index] = character;
string_value[length + 1] = '\0';
}
char character_to_upper(const char character) {

View File

@ -14,7 +14,7 @@
* @param character
* @since v1.0.0
*/
void character_append(char* string, char character);
void character_append(string string_value, char character);
/**
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
@ -24,7 +24,7 @@ void character_append(char* string, char character);
* @param index
* @since v1.0.0
*/
void character_append_at(char* string, const char character, const size_t index);
void character_append_at(string string_value, const char character, const size_t index);
/**
* @brief Converts the character to uppercase.

View File

@ -1,7 +1,7 @@
#include "convert.h"
char* convert_character_to_string(const char character) {
char* string = malloc(sizeof(char*) * 2);
string convert_character_to_string(const char character) {
string string = malloc(sizeof(string) * 2);
if (string == NULL) {
exit(EXIT_FAILURE);
}
@ -17,42 +17,42 @@ char convert_digit_to_character(const char digit) {
return digit + '0';
}
long long convert_string_to_number(const char* string) {
bool is_negative = string[0] == '-';
long long convert_string_to_number(const string string_value) {
bool is_negative = string_value[0] == '-';
long long integer = 0;
size_t length = string_get_length(string);
size_t length = string_get_length(string_value);
for (size_t index = is_negative ? 1 : 0; index < length; index++) {
integer = integer * 10 + convert_character_to_digit(string[index]);
integer = integer * 10 + convert_character_to_digit(string_value[index]);
}
return is_negative ? integer * -1 : integer;
}
char* convert_number_to_string(const long long integer) {
string convert_number_to_string(const long long integer) {
if (integer == 0) {
return convert_character_to_string('0');
}
bool is_negative = integer < 0;
size_t length = 1;
char* string = malloc(sizeof(char*) * length);
if (string == NULL) {
string string_value = malloc(sizeof(string) * length);
if (string_value == NULL) {
exit(EXIT_FAILURE);
}
long long current = mathematics_absolute_value(integer);
while (current != 0) {
character_append(string, convert_digit_to_character(current % 10));
character_append(string_value, convert_digit_to_character(current % 10));
current = current / 10;
length++;
string = realloc(string, sizeof(char*) * length);
string_value = realloc(string_value, sizeof(string) * length);
}
if (is_negative) {
character_append(string, '-');
character_append(string_value, '-');
length++;
string = realloc(string, sizeof(char*) * length);
string_value = realloc(string_value, sizeof(string) * length);
}
return string_reverse(string);
return string_reverse(string_value);
}
char* convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {
string convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {
if (number == 0) {
return "0";
}
@ -63,7 +63,7 @@ char* convert_number_from_base_10_to_base(unsigned long long number, unsigned in
number = number / base;
index++;
}
char* result = malloc(sizeof(char) * (index + 1));
string result = malloc(sizeof(string) * (index + 1));
int index_result = 0;
for (int iteration = index - 1; iteration >= 0; iteration--) {
int remainder = remainders[iteration];
@ -77,7 +77,7 @@ char* convert_number_from_base_10_to_base(unsigned long long number, unsigned in
return result;
}
int convert_number_from_base_to_base_10(char* number, unsigned int base) {
int convert_number_from_base_to_base_10(string number, unsigned int base) {
int length = string_get_length(number);
int exponent = length - 1;
int result = 0;
@ -96,6 +96,6 @@ int convert_number_from_base_to_base_10(char* number, unsigned int base) {
return result;
}
char* convert_number_from_base_to_another(char* number, int base_from, int base_target) {
string convert_number_from_base_to_another(string number, int base_from, int base_target) {
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
}

View File

@ -15,13 +15,12 @@
* @param character
* @since v1.0.0
*/
char* convert_character_to_string(const char character);
string convert_character_to_string(const char character);
/**
* @brief Convert a character to a digit.
*
* @param character
* @return char
* @since v1.0.0
*/
char convert_character_to_digit(const char character);
@ -30,7 +29,6 @@ char convert_character_to_digit(const char character);
* @brief Convert a digit to a character.
*
* @param digit
* @return char
* @since v1.0.0
*/
char convert_digit_to_character(const char digit);
@ -39,39 +37,35 @@ char convert_digit_to_character(const char digit);
* @brief Convert a string to a number.
*
* @param string
* @return long long
* @since v1.0.0
*/
long long convert_string_to_number(const char* string);
long long convert_string_to_number(const string string_value);
/**
* @brief Convert a number to a string.
*
* @param integer
* @return char*
* @since v1.0.0
*/
char* convert_number_to_string(const long long integer);
string convert_number_to_string(const long long integer);
/**
* @brief Convert a number (base 10) to a string with a specific base.
*
* @param number
* @param base
* @return char*
* @since v1.0.0
*/
char* convert_number_from_base_10_to_base(unsigned long long number, unsigned int base);
string convert_number_from_base_10_to_base(unsigned long long number, unsigned int base);
/**
* @brief Convert a number with a specific base to a number base 10.
*
* @param number
* @param base
* @return int
* @since v1.0.0
*/
int convert_number_from_base_to_base_10(char* number, unsigned int base);
int convert_number_from_base_to_base_10(string number, unsigned int base);
/**
* @brief Convert a number with a specific base to a number of specific base.
@ -79,9 +73,8 @@ int convert_number_from_base_to_base_10(char* number, unsigned int base);
* @param number
* @param base_from
* @param base_target
* @return char*
* @since v1.0.0
*/
char* convert_number_from_base_to_another(char* number, int base_from, int base_target);
string convert_number_from_base_to_another(string number, int base_from, int base_target);
#endif

View File

@ -1,6 +1,6 @@
#include "filesystem.h"
int filesystem_read(char *path, byte **file_content, off_t *file_size) {
int filesystem_read(string path, byte **file_content, off_t *file_size) {
int file_descriptor = open(path, O_RDONLY);
if (file_descriptor == -1) {
return -1;
@ -15,7 +15,7 @@ int filesystem_read(char *path, byte **file_content, off_t *file_size) {
return 0;
}
int filesystem_write(char *path, byte *file_content, off_t file_size) {
int filesystem_write(string path, byte *file_content, off_t file_size) {
int file_descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file_descriptor == -1) {
return -1;
@ -27,7 +27,7 @@ int filesystem_write(char *path, byte *file_content, off_t file_size) {
return 0;
}
char *filesystem_get_mimetype(char *path) {
char *filesystem_get_mimetype(string path) {
if (string_ends_with(path, ".html")) {
return "text/html";
}

View File

@ -26,7 +26,7 @@ typedef uint8_t byte;
* @retval 0 for success.
* @since v1.0.0
*/
int filesystem_read(char *path, byte **file_content, off_t *file_size);
int filesystem_read(string path, byte **file_content, off_t *file_size);
/**
* @brief Write the content to a file.
@ -39,15 +39,14 @@ int filesystem_read(char *path, byte **file_content, off_t *file_size);
* @retval 0 for success.
* @since v1.0.0
*/
int filesystem_write(char *path, byte *file_content, off_t file_size);
int filesystem_write(string path, byte *file_content, off_t file_size);
/**
* @brief Get the mimetype of a file.
*
* @param path
* @return char*
* @since v1.0.0
*/
char *filesystem_get_mimetype(char *path);
string filesystem_get_mimetype(string path);
#endif

View File

@ -1,118 +1,118 @@
#include "string.h"
size_t string_get_length(const char* string) {
size_t string_get_length(const string string_value) {
size_t length = 0;
while (string[length] != '\0') {
while (string_value[length] != '\0') {
length++;
}
return length;
}
char* string_to_uppercase(char* string) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_to_uppercase(string string_value) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
character_append(result, character_to_upper(string[index]));
character_append(result, character_to_upper(string_value[index]));
}
return result;
}
char* string_to_lowercase(char* string) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_to_lowercase(string string_value) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
character_append(result, character_to_lower(string[index]));
character_append(result, character_to_lower(string_value[index]));
}
return result;
}
char* string_replace(char* string, char search, char replace) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_replace(string string_value, char search, char replace) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index = 0; index < string_length; index++) {
bool is_search_value = search == string[index];
bool is_search_value = search == string_value[index];
if (is_search_value) {
character_append(result, replace);
} else {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
}
return result;
}
char* string_trim_start(char* string) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_trim_start(string string_value) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = 0;
while (string[index_space] == ' ') {
while (string_value[index_space] == ' ') {
index_space++;
}
for (size_t index = index_space; index < string_length; index++) {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
return result;
}
char* string_trim_end(char* string) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_trim_end(string string_value) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_space = string_length - 1;
while (string[index_space] == ' ') {
while (string_value[index_space] == ' ') {
index_space--;
}
for (size_t index = 0; index < index_space + 1; index++) {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
return result;
}
char* string_trim(char* string) {
char* result = string_trim_start(string);
string string_trim(string string_value) {
string result = string_trim_start(string_value);
result = string_trim_end(result);
return result;
}
char* string_copy(const char* string) {
return string_substring(string, 0, string_get_length(string));
string string_copy(const string string_value) {
return string_substring(string_value, 0, string_get_length(string_value));
}
char* string_capitalize(char* string) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_capitalize(string string_value) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
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) {
character_append(result, character_to_upper(string[index]));
character_append(result, character_to_upper(string_value[index]));
} else {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
}
return result;
}
size_t string_total_occurrences_of_character(char* string, char character) {
size_t string_total_occurrences_of_character(string string_value, char character) {
size_t result = 0;
size_t string_length = string_get_length(string);
size_t string_length = string_get_length(string_value);
for (size_t index = 0; index < string_length; index++) {
char current_character = string[index];
char current_character = string_value[index];
if (current_character == character) {
result += 1;
}
@ -120,21 +120,21 @@ size_t string_total_occurrences_of_character(char* string, char character) {
return result;
}
char* string_reverse(const char* string) {
size_t string_length = string_get_length(string);
string string_reverse(const string string_value) {
size_t string_length = string_get_length(string_value);
size_t index = 0;
char* result = malloc(sizeof(char*) * string_length);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
for (index = string_length - 1; index > 0; index--) {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
character_append(result, string[index]);
character_append(result, string_value[index]);
return result;
}
bool string_equals(const char* string1, const char* string2) {
bool string_equals(const string string1, const string string2) {
size_t string1_length = string_get_length(string1);
size_t string2_length = string_get_length(string2);
bool is_equal = string1_length == string2_length;
@ -146,11 +146,11 @@ bool string_equals(const char* string1, const char* string2) {
return is_equal;
}
bool string_get_is_integer(const char* string) {
bool string_get_is_integer(const string string_value) {
size_t index = 0;
size_t string_length = string_get_length(string);
size_t string_length = string_get_length(string_value);
bool is_integer = string_length >= 1;
if (is_integer && string[0] == '-') {
if (is_integer && string_value[0] == '-') {
if (string_length == 1) {
is_integer = false;
} else {
@ -158,7 +158,7 @@ bool string_get_is_integer(const char* string) {
}
}
while (index < string_length && is_integer) {
if (!character_get_is_digit(string[index])) {
if (!character_get_is_digit(string_value[index])) {
is_integer = false;
}
index++;
@ -166,28 +166,28 @@ bool string_get_is_integer(const char* string) {
return is_integer;
}
char** string_split(const char* string, char separator, size_t* result_size) {
size_t string_length = string_get_length(string);
string* string_split(const string string_value, char separator, size_t* result_size) {
size_t string_length = string_get_length(string_value);
size_t index_string = 0;
size_t index_current = 0;
size_t index_result = 0;
char* current = malloc(sizeof(char*) * string_length);
char** result = malloc(sizeof(char**) * index_result);
string current = malloc(sizeof(string) * string_length);
string* result = malloc(sizeof(string*) * index_result);
if (result == NULL || current == NULL) {
exit(EXIT_FAILURE);
}
while (index_string < string_length) {
if (string[index_string] == separator) {
if (string_value[index_string] == separator) {
result[index_result] = string_copy(current);
index_result++;
result = realloc(result, sizeof(char*) * index_result);
result = realloc(result, sizeof(string_value) * index_result);
if (result == NULL) {
exit(EXIT_FAILURE);
}
index_current = 0;
current = string_copy("");
} else {
character_append(current, string[index_string]);
character_append(current, string_value[index_string]);
index_current++;
}
index_string++;
@ -198,36 +198,36 @@ char** string_split(const char* string, char separator, size_t* result_size) {
return result;
}
char* string_join(char** array, const char separator, size_t array_length) {
string string_join(string* array, const char separator, size_t array_length) {
size_t string_length = array_length;
char* string = malloc(sizeof(char*) * string_length);
if (string == NULL) {
string string_value = malloc(sizeof(string_value) * string_length);
if (string_value == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index_array = 0; index_array < array_length; index_array++) {
char* substring = array[index_array];
string substring = array[index_array];
size_t substring_length = string_get_length(substring);
string_length += substring_length;
string = realloc(string, sizeof(char*) * string_length);
if (string == NULL) {
string_value = realloc(string_value, sizeof(string_value) * string_length);
if (string_value == NULL) {
exit(EXIT_FAILURE);
}
for (size_t index_substring = 0; index_substring < substring_length; index_substring++) {
character_append(string, substring[index_substring]);
character_append(string_value, substring[index_substring]);
}
bool is_last_character = index_array == (array_length - 1);
if (!is_last_character) {
character_append(string, separator);
character_append(string_value, separator);
}
}
return string;
return string_value;
}
char* string_concatenate(char* string1, char* string2) {
string string_concatenate(string string1, string string2) {
size_t string1_length = string_get_length(string1);
size_t string2_length = string_get_length(string2);
size_t result_length = string1_length + string2_length;
char* result = malloc(sizeof(char*) * result_length);
string result = malloc(sizeof(string) * result_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
@ -240,13 +240,13 @@ char* string_concatenate(char* string1, char* string2) {
return result;
}
bool string_get_has_unique_characters(const char* string) {
bool string_get_has_unique_characters(const string string_value) {
bool has_unique = true;
size_t string_length = string_get_length(string);
size_t string_length = string_get_length(string_value);
struct dictionary* characters_already_seen = dictionary_initialization();
for (size_t index = 0; index < string_length && has_unique; index++) {
char character = string[index];
char* key = convert_character_to_string(character);
char character = string_value[index];
string key = convert_character_to_string(character);
if (dictionary_contains_key(characters_already_seen, key)) {
has_unique = false;
} else {
@ -256,23 +256,23 @@ bool string_get_has_unique_characters(const char* string) {
return has_unique;
}
char* string_substring(const char* string, size_t index_start, size_t index_end) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_substring(const string string_value, size_t index_start, size_t index_end) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
for (size_t index = index_start; index <= index_end; index++) {
character_append(result, string[index]);
character_append(result, string_value[index]);
}
return result;
}
bool string_get_is_substring(const char* string, const char* substring) {
bool string_get_is_substring(const string string_value, const string substring) {
bool is_substring = false;
size_t string_length = string_get_length(string);
size_t string_length = string_get_length(string_value);
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;
while (index_substring < substring_length && string[index_considered] == substring[index_substring]) {
while (index_substring < substring_length && string_value[index_considered] == substring[index_substring]) {
index_substring++;
index_considered++;
}
@ -281,14 +281,14 @@ bool string_get_is_substring(const char* string, const char* substring) {
return is_substring;
}
char* string_get_formatted_number(const long long number, char* separator) {
char* number_string = convert_number_to_string(number);
string string_get_formatted_number(const long long number, string separator) {
string number_string = convert_number_to_string(number);
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);
char* result = malloc(sizeof(char*) * number_string_length);
string result = malloc(sizeof(string) * number_string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
@ -313,41 +313,41 @@ char* string_get_formatted_number(const long long number, char* separator) {
return result;
}
char* string_get_last_occurence_of_character(const char* string, char character) {
size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char*) * string_length);
string string_get_last_occurence_of_character(const string string_value, char character) {
size_t string_length = string_get_length(string_value);
string result = malloc(sizeof(string) * string_length);
if (result == NULL) {
exit(EXIT_FAILURE);
}
size_t index_result = 0;
for (size_t index = 0; index < string_length; index++) {
if (string[index] == character) {
if (string_value[index] == character) {
index_result = 0;
result = string_copy("");
}
character_append(result, string[index]);
character_append(result, string_value[index]);
index_result++;
}
return result;
}
bool string_starts_with(const char* string, const char* prefix) {
bool string_starts_with(const string string_value, const string prefix) {
bool starts_with = true;
size_t prefix_length = string_get_length(prefix);
for (size_t index = 0; index < prefix_length && starts_with; index++) {
starts_with = string[index] == prefix[index];
starts_with = string_value[index] == prefix[index];
}
return starts_with;
}
bool string_ends_with(const char* string, const char* prefix) {
bool string_ends_with(const string string_value, const string prefix) {
bool ends_with = true;
size_t string_length = string_get_length(string);
size_t string_length = string_get_length(string_value);
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) {
ends_with = string[index_string] == prefix[index_prefix];
ends_with = string_value[index_string] == prefix[index_prefix];
index_string--;
index_prefix--;
}

View File

@ -9,6 +9,8 @@
#include "convert.h"
#include "dictionary.h"
typedef char* string;
/**
* @brief Return the length of a string (excluding '\0').
*
@ -16,25 +18,23 @@
* @return size_t
* @since v1.0.0
*/
size_t string_get_length(const char* string);
size_t string_get_length(const string string_value);
/**
* @brief Converts all the alphabetic characters in a string to uppercase.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_to_uppercase(char* string);
string string_to_uppercase(string string_value);
/**
* @brief Converts all the alphabetic characters in a string to lowercase.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_to_lowercase(char* string);
string string_to_lowercase(string string_value);
/**
* @brief Replace all the occurrences of search value into replace value in the string.
@ -42,55 +42,49 @@ char* string_to_lowercase(char* string);
* @param string
* @param search_value A character search value.
* @param replace_value A character containing the text to replace for match.
* @return char*
* @since v1.0.0
*/
char* string_replace(char* string, char search, char replace);
string string_replace(string string_value, char search, char replace);
/**
* @brief Removes all whitespace from the start of a string.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_trim_start(char* string);
string string_trim_start(string string_value);
/**
* @brief Removes all whitespace from the end of a string.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_trim_end(char* string);
string string_trim_end(string string_value);
/**
* @brief Removes all whitespace from the start and end of a string.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_trim(char* string);
string string_trim(string string_value);
/**
* @brief Return the copy of a string.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_copy(const char* string);
string string_copy(const string string_value);
/**
* @brief Capitalizes the string.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_capitalize(char* string);
string string_capitalize(string string_value);
/**
* @brief Returns the total number of occurrences of the given character in the string.
@ -100,16 +94,15 @@ char* string_capitalize(char* string);
* @return size_t
* @since v1.0.0
*/
size_t string_total_occurrences_of_character(char* string, char character);
size_t string_total_occurrences_of_character(string string_value, char character);
/**
* @brief Reverse the characters in an array.
*
* @param string
* @return char*
* @since v1.0.0
*/
char* string_reverse(const char* string);
string string_reverse(const string string_value);
/**
* @brief Check if two strings are equals.
@ -119,7 +112,7 @@ char* string_reverse(const char* string);
* @return true if the strings are equals, false otherwise.
* @since v1.0.0
*/
bool string_equals(const char* string1, const char* string2);
bool string_equals(const string string1, const string string2);
/**
* @brief Check if the string is a integer.
@ -128,37 +121,34 @@ bool string_equals(const char* string1, const char* string2);
* @return true if the string is a integer, false otherwise.
* @since v1.0.0
*/
bool string_get_is_integer(const char* string);
bool string_get_is_integer(const string string_value);
/**
* @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.
*
* @param string
* @param separator
* @return char**
* @since v1.0.0
*/
char** string_split(const char* string, char separator, size_t* result_size);
string* string_split(const string string_value, char separator, size_t* result_size);
/**
* @brief Adds all the elements of an array into a string, separated by the specified separator string.
*
* @param array
* @param separator
* @return char*
* @since v1.0.0
*/
char* string_join(char** array, const char separator, size_t array_length);
string string_join(string* array, const char separator, size_t array_length);
/**
* @brief Concatenate two strings.
*
* @param string1
* @param string2
* @return char*
* @since v1.0.0
*/
char* string_concatenate(char* string1, char* string2);
string string_concatenate(string string1, string string2);
/**
* @brief Check if a string contains only unique characters.
@ -167,7 +157,7 @@ char* string_concatenate(char* string1, char* string2);
* @return true if string contains only unique characters, false otherwise.
* @since v1.0.0
*/
bool string_get_has_unique_characters(const char* string);
bool string_get_has_unique_characters(const string string);
/**
* @brief Returns the part of the string between the start and end indexes (both included).
@ -175,10 +165,9 @@ bool string_get_has_unique_characters(const char* string);
* @param string
* @param index_start
* @param index_end
* @return char*
* @since v1.0.0
*/
char* string_substring(const char* string, size_t index_start, size_t index_end);
string string_substring(const string string_value, size_t index_start, size_t index_end);
/**
* @brief Check if a string contains a substring.
@ -188,26 +177,25 @@ char* string_substring(const char* string, size_t index_start, size_t index_end)
* @return true if the string contains the substring, false otherwise.
* @since v1.0.0
*/
bool string_get_is_substring(const char* string, const char* substring);
bool string_get_is_substring(const string string_value, const string substring);
/**
* @brief Format a number to a string with specified separator.
*
* @param integer
* @return char* example: string_get_formatted_number(1000, " ") => "1 000"
* @example string_get_formatted_number(1000, " ") // "1 000"
* @since v1.0.0
*/
char* string_get_formatted_number(const long long number, char* separator);
string string_get_formatted_number(const long long number, string separator);
/**
* @brief Returns a pointer to the last occurrence of character in the string.
*
* @param string
* @param character
* @return char*
* @since v1.0.0
*/
char* string_get_last_occurence_of_character(const char* string, char character);
string string_get_last_occurence_of_character(const string string_value, char character);
/**
* @brief Check if a string starts with a substring.
@ -217,7 +205,7 @@ char* string_get_last_occurence_of_character(const char* string, char character)
* @return true if the string starts with the substring, false otherwise.
* @since v1.0.0
*/
bool string_starts_with(const char* string, const char* prefix);
bool string_starts_with(const string string_value, const string prefix);
/**
* @brief Check if a string ends with a substring.
@ -227,6 +215,6 @@ bool string_starts_with(const char* string, const char* prefix);
* @return true if the string ends with the substring, false otherwise.
* @since v1.0.0
*/
bool string_ends_with(const char* string, const char* prefix);
bool string_ends_with(const string string_value, const string prefix);
#endif

View File

@ -1,22 +1,22 @@
#include "terminal.h"
char* terminal_input() {
string terminal_input() {
char character;
size_t length = 1;
char* string = malloc(length * sizeof(char));
if (string == NULL) {
string string_value = malloc(length * sizeof(char));
if (string_value == NULL) {
exit(EXIT_FAILURE);
}
*string = '\0';
*string_value = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
if (string == NULL) {
string_value = realloc(string_value, length * sizeof(char));
if (string_value == NULL) {
exit(EXIT_FAILURE);
}
character_append(string, character);
character_append(string_value, character);
}
return string;
return string_value;
}
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*)) {

View File

@ -14,11 +14,9 @@
/**
* @brief Read a line from stdin.
*
* @return char*
* @since v1.0.0
*/
char* terminal_input();
string terminal_input();
/**
* @brief Print an array.

6
main.c
View File

@ -4,8 +4,8 @@
#include "libcproject.h"
int main() {
char* string = "Hello, world!";
printf("%s\n", string);
printf("string_length = %ld\n", string_get_length(string));
string string_value = "Hello, world!";
printf("%s\n", string_value);
printf("string_length = %ld\n", string_get_length(string_value));
return EXIT_SUCCESS;
}

View File

@ -3,12 +3,12 @@
#include "libcproject.h"
int main(int argc, char* argv[]) {
int main(int argc, string* argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <version>\n", argv[0]);
return EXIT_FAILURE;
}
char* content = "#ifndef __LIBCPROJECT_VERSION__\n";
string content = "#ifndef __LIBCPROJECT_VERSION__\n";
content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \"");
content = string_concatenate(content, argv[1]);
content = string_concatenate(content, "\"\n\n");