mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-12-11 21:13:00 +01:00
feat!: rename types string
to string_t
and byte
to byte_t
This commit is contained in:
parent
8f3ee199e5
commit
1ded37b106
@ -97,7 +97,7 @@ touch main.c
|
|||||||
#include "libcproject/libcproject.h"
|
#include "libcproject/libcproject.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
string string_value = "Hello, world!"; // `string` is a typedef from `libcproject`
|
string_t string_value = "Hello, world!"; // `string_t` is a typedef from `libcproject`
|
||||||
printf("%s\n", string_value);
|
printf("%s\n", string_value);
|
||||||
printf("string_length = %ld\n", string_get_length(string_value)); // `string_get_length` is a function from `libcproject`
|
printf("string_length = %ld\n", string_get_length(string_value)); // `string_get_length` is a function from `libcproject`
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#include "character.h"
|
#include "character.h"
|
||||||
|
|
||||||
void character_append(string string_value, const char character) {
|
void character_append(string_t string_value, const char character) {
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string_value);
|
||||||
character_append_at(string_value, character, length);
|
character_append_at(string_value, character, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void character_append_at(string string_value, const char character, const size_t index) {
|
void character_append_at(string_t string_value, const char character, const size_t index) {
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string_value);
|
||||||
for (size_t index_string = length; index_string > index; index_string--) {
|
for (size_t index_string = length; index_string > index; index_string--) {
|
||||||
string_value[index_string] = string_value[index_string - 1];
|
string_value[index_string] = string_value[index_string - 1];
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* @param character
|
* @param character
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void character_append(string string_value, char character);
|
void character_append(string_t string_value, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
|
* @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(string string_value, char character);
|
|||||||
* @param index
|
* @param index
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void character_append_at(string string_value, const char character, const size_t index);
|
void character_append_at(string_t string_value, const char character, const size_t index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts the character to uppercase.
|
* @brief Converts the character to uppercase.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
|
|
||||||
string convert_character_to_string(const char character) {
|
string_t convert_character_to_string(const char character) {
|
||||||
string string = malloc(sizeof(string) * 2);
|
string_t string = malloc(sizeof(string) * 2);
|
||||||
if (string == NULL) {
|
if (string == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -17,7 +17,7 @@ char convert_digit_to_character(const char digit) {
|
|||||||
return digit + '0';
|
return digit + '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
long long convert_string_to_number(const string string_value) {
|
long long convert_string_to_number(const string_t string_value) {
|
||||||
bool is_negative = string_value[0] == '-';
|
bool is_negative = string_value[0] == '-';
|
||||||
long long integer = 0;
|
long long integer = 0;
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string_value);
|
||||||
@ -27,13 +27,13 @@ long long convert_string_to_number(const string string_value) {
|
|||||||
return is_negative ? integer * -1 : integer;
|
return is_negative ? integer * -1 : integer;
|
||||||
}
|
}
|
||||||
|
|
||||||
string convert_number_to_string(const long long integer) {
|
string_t convert_number_to_string(const long long integer) {
|
||||||
if (integer == 0) {
|
if (integer == 0) {
|
||||||
return convert_character_to_string('0');
|
return convert_character_to_string('0');
|
||||||
}
|
}
|
||||||
bool is_negative = integer < 0;
|
bool is_negative = integer < 0;
|
||||||
size_t length = 1;
|
size_t length = 1;
|
||||||
string string_value = malloc(sizeof(string) * length);
|
string_t string_value = malloc(sizeof(string_t) * length);
|
||||||
if (string_value == NULL) {
|
if (string_value == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -42,17 +42,17 @@ string convert_number_to_string(const long long integer) {
|
|||||||
character_append(string_value, convert_digit_to_character(current % 10));
|
character_append(string_value, convert_digit_to_character(current % 10));
|
||||||
current = current / 10;
|
current = current / 10;
|
||||||
length++;
|
length++;
|
||||||
string_value = realloc(string_value, sizeof(string) * length);
|
string_value = realloc(string_value, sizeof(string_t) * length);
|
||||||
}
|
}
|
||||||
if (is_negative) {
|
if (is_negative) {
|
||||||
character_append(string_value, '-');
|
character_append(string_value, '-');
|
||||||
length++;
|
length++;
|
||||||
string_value = realloc(string_value, sizeof(string) * length);
|
string_value = realloc(string_value, sizeof(string_t) * length);
|
||||||
}
|
}
|
||||||
return string_reverse(string_value);
|
return string_reverse(string_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
string convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {
|
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {
|
||||||
if (number == 0) {
|
if (number == 0) {
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ string convert_number_from_base_10_to_base(unsigned long long number, unsigned i
|
|||||||
number = number / base;
|
number = number / base;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
string result = malloc(sizeof(string) * (index + 1));
|
string_t result = malloc(sizeof(string_t) * (index + 1));
|
||||||
int index_result = 0;
|
int index_result = 0;
|
||||||
for (int iteration = index - 1; iteration >= 0; iteration--) {
|
for (int iteration = index - 1; iteration >= 0; iteration--) {
|
||||||
int remainder = remainders[iteration];
|
int remainder = remainders[iteration];
|
||||||
@ -77,7 +77,7 @@ string convert_number_from_base_10_to_base(unsigned long long number, unsigned i
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int convert_number_from_base_to_base_10(string number, unsigned int base) {
|
int convert_number_from_base_to_base_10(string_t number, unsigned int base) {
|
||||||
int length = string_get_length(number);
|
int length = string_get_length(number);
|
||||||
int exponent = length - 1;
|
int exponent = length - 1;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
@ -96,6 +96,6 @@ int convert_number_from_base_to_base_10(string number, unsigned int base) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string convert_number_from_base_to_another(string number, int base_from, int base_target) {
|
string_t convert_number_from_base_to_another(string_t 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);
|
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* @param character
|
* @param character
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string convert_character_to_string(const char character);
|
string_t convert_character_to_string(const char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a character to a digit.
|
* @brief Convert a character to a digit.
|
||||||
@ -39,7 +39,7 @@ char convert_digit_to_character(const char digit);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
long long convert_string_to_number(const string string_value);
|
long long convert_string_to_number(const string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number to a string.
|
* @brief Convert a number to a string.
|
||||||
@ -47,7 +47,7 @@ long long convert_string_to_number(const string string_value);
|
|||||||
* @param integer
|
* @param integer
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string convert_number_to_string(const long long integer);
|
string_t convert_number_to_string(const long long integer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number (base 10) to a string with a specific base.
|
* @brief Convert a number (base 10) to a string with a specific base.
|
||||||
@ -56,7 +56,7 @@ string convert_number_to_string(const long long integer);
|
|||||||
* @param base
|
* @param base
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string convert_number_from_base_10_to_base(unsigned long long number, unsigned int base);
|
string_t 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.
|
* @brief Convert a number with a specific base to a number base 10.
|
||||||
@ -65,7 +65,7 @@ string convert_number_from_base_10_to_base(unsigned long long number, unsigned i
|
|||||||
* @param base
|
* @param base
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
int convert_number_from_base_to_base_10(string number, unsigned int base);
|
int convert_number_from_base_to_base_10(string_t number, unsigned int base);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number with a specific base to a number of specific base.
|
* @brief Convert a number with a specific base to a number of specific base.
|
||||||
@ -75,6 +75,6 @@ int convert_number_from_base_to_base_10(string number, unsigned int base);
|
|||||||
* @param base_target
|
* @param base_target
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string convert_number_from_base_to_another(string number, int base_from, int base_target);
|
string_t convert_number_from_base_to_another(string_t number, int base_from, int base_target);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
|
|
||||||
int filesystem_read(string path, byte **file_content, off_t *file_size) {
|
int filesystem_read(string_t path, byte_t **file_content, off_t *file_size) {
|
||||||
int file_descriptor = open(path, O_RDONLY);
|
int file_descriptor = open(path, O_RDONLY);
|
||||||
if (file_descriptor == -1) {
|
if (file_descriptor == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -15,7 +15,7 @@ int filesystem_read(string path, byte **file_content, off_t *file_size) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int filesystem_write(string path, byte *file_content, off_t file_size) {
|
int filesystem_write(string_t path, byte_t *file_content, off_t file_size) {
|
||||||
int file_descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
int file_descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
if (file_descriptor == -1) {
|
if (file_descriptor == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -27,7 +27,7 @@ int filesystem_write(string path, byte *file_content, off_t file_size) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *filesystem_get_mimetype(string path) {
|
char *filesystem_get_mimetype(string_t path) {
|
||||||
if (string_ends_with(path, ".html")) {
|
if (string_ends_with(path, ".html")) {
|
||||||
return "text/html";
|
return "text/html";
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
typedef uint8_t byte;
|
typedef uint8_t byte_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read the content of a file.
|
* @brief Read the content of a file.
|
||||||
@ -26,7 +26,7 @@ typedef uint8_t byte;
|
|||||||
* @retval 0 for success.
|
* @retval 0 for success.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
int filesystem_read(string path, byte **file_content, off_t *file_size);
|
int filesystem_read(string_t path, byte_t **file_content, off_t *file_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Write the content to a file.
|
* @brief Write the content to a file.
|
||||||
@ -39,7 +39,7 @@ int filesystem_read(string path, byte **file_content, off_t *file_size);
|
|||||||
* @retval 0 for success.
|
* @retval 0 for success.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
int filesystem_write(string path, byte *file_content, off_t file_size);
|
int filesystem_write(string_t path, byte_t *file_content, off_t file_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the mimetype of a file.
|
* @brief Get the mimetype of a file.
|
||||||
@ -47,6 +47,6 @@ int filesystem_write(string path, byte *file_content, off_t file_size);
|
|||||||
* @param path
|
* @param path
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string filesystem_get_mimetype(string path);
|
string_t filesystem_get_mimetype(string_t path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
82
lib/string.c
82
lib/string.c
@ -1,6 +1,6 @@
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
size_t string_get_length(const string string_value) {
|
size_t string_get_length(const string_t string_value) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
while (string_value[length] != '\0') {
|
while (string_value[length] != '\0') {
|
||||||
length++;
|
length++;
|
||||||
@ -8,9 +8,9 @@ size_t string_get_length(const string string_value) {
|
|||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_to_uppercase(string string_value) {
|
string_t string_to_uppercase(string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -20,9 +20,9 @@ string string_to_uppercase(string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_to_lowercase(string string_value) {
|
string_t string_to_lowercase(string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -32,9 +32,9 @@ string string_to_lowercase(string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_replace(string string_value, char search, char replace) {
|
string_t string_replace(string_t string_value, char search, char replace) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -49,9 +49,9 @@ string string_replace(string string_value, char search, char replace) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_trim_start(string string_value) {
|
string_t string_trim_start(string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -65,9 +65,9 @@ string string_trim_start(string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_trim_end(string string_value) {
|
string_t string_trim_end(string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -81,19 +81,19 @@ string string_trim_end(string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_trim(string string_value) {
|
string_t string_trim(string_t string_value) {
|
||||||
string result = string_trim_start(string_value);
|
string_t result = string_trim_start(string_value);
|
||||||
result = string_trim_end(result);
|
result = string_trim_end(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_copy(const string string_value) {
|
string_t string_copy(const string_t string_value) {
|
||||||
return string_substring(string_value, 0, string_get_length(string_value));
|
return string_substring(string_value, 0, string_get_length(string_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_capitalize(string string_value) {
|
string_t string_capitalize(string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ string string_capitalize(string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t string_total_occurrences_of_character(string string_value, char character) {
|
size_t string_total_occurrences_of_character(string_t string_value, char character) {
|
||||||
size_t result = 0;
|
size_t result = 0;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
@ -120,10 +120,10 @@ size_t string_total_occurrences_of_character(string string_value, char character
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_reverse(const string string_value) {
|
string_t string_reverse(const string_t string_value) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ string string_reverse(const string string_value) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_equals(const string string1, const string string2) {
|
bool string_equals(const string_t string1, const string_t string2) {
|
||||||
size_t string1_length = string_get_length(string1);
|
size_t string1_length = string_get_length(string1);
|
||||||
size_t string2_length = string_get_length(string2);
|
size_t string2_length = string_get_length(string2);
|
||||||
bool is_equal = string1_length == string2_length;
|
bool is_equal = string1_length == string2_length;
|
||||||
@ -146,7 +146,7 @@ bool string_equals(const string string1, const string string2) {
|
|||||||
return is_equal;
|
return is_equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_is_integer(const string string_value) {
|
bool string_get_is_integer(const string_t string_value) {
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
bool is_integer = string_length >= 1;
|
bool is_integer = string_length >= 1;
|
||||||
@ -166,13 +166,13 @@ bool string_get_is_integer(const string string_value) {
|
|||||||
return is_integer;
|
return is_integer;
|
||||||
}
|
}
|
||||||
|
|
||||||
string* string_split(const string string_value, char separator, size_t* result_size) {
|
string_t* string_split(const string_t string_value, char separator, size_t* result_size) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
size_t index_string = 0;
|
size_t index_string = 0;
|
||||||
size_t index_current = 0;
|
size_t index_current = 0;
|
||||||
size_t index_result = 0;
|
size_t index_result = 0;
|
||||||
string current = malloc(sizeof(string) * string_length);
|
string_t current = malloc(sizeof(string_t) * string_length);
|
||||||
string* result = malloc(sizeof(string*) * index_result);
|
string_t* result = malloc(sizeof(string_t*) * index_result);
|
||||||
if (result == NULL || current == NULL) {
|
if (result == NULL || current == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -198,14 +198,14 @@ string* string_split(const string string_value, char separator, size_t* result_s
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_join(string* array, const char separator, size_t array_length) {
|
string_t string_join(string_t* array, const char separator, size_t array_length) {
|
||||||
size_t string_length = array_length;
|
size_t string_length = array_length;
|
||||||
string string_value = malloc(sizeof(string_value) * string_length);
|
string_t string_value = malloc(sizeof(string_value) * string_length);
|
||||||
if (string_value == NULL) {
|
if (string_value == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
for (size_t index_array = 0; index_array < array_length; index_array++) {
|
for (size_t index_array = 0; index_array < array_length; index_array++) {
|
||||||
string substring = array[index_array];
|
string_t substring = array[index_array];
|
||||||
size_t substring_length = string_get_length(substring);
|
size_t substring_length = string_get_length(substring);
|
||||||
string_length += substring_length;
|
string_length += substring_length;
|
||||||
string_value = realloc(string_value, sizeof(string_value) * string_length);
|
string_value = realloc(string_value, sizeof(string_value) * string_length);
|
||||||
@ -223,11 +223,11 @@ string string_join(string* array, const char separator, size_t array_length) {
|
|||||||
return string_value;
|
return string_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_concatenate(string string1, string string2) {
|
string_t string_concatenate(string_t string1, string_t string2) {
|
||||||
size_t string1_length = string_get_length(string1);
|
size_t string1_length = string_get_length(string1);
|
||||||
size_t string2_length = string_get_length(string2);
|
size_t string2_length = string_get_length(string2);
|
||||||
size_t result_length = string1_length + string2_length;
|
size_t result_length = string1_length + string2_length;
|
||||||
string result = malloc(sizeof(string) * result_length);
|
string_t result = malloc(sizeof(string_t) * result_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -240,13 +240,13 @@ string string_concatenate(string string1, string string2) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_has_unique_characters(const string string_value) {
|
bool string_get_has_unique_characters(const string_t string_value) {
|
||||||
bool has_unique = true;
|
bool has_unique = true;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
struct dictionary* characters_already_seen = dictionary_initialization();
|
struct dictionary* characters_already_seen = dictionary_initialization();
|
||||||
for (size_t index = 0; index < string_length && has_unique; index++) {
|
for (size_t index = 0; index < string_length && has_unique; index++) {
|
||||||
char character = string_value[index];
|
char character = string_value[index];
|
||||||
string key = convert_character_to_string(character);
|
string_t key = convert_character_to_string(character);
|
||||||
if (dictionary_contains_key(characters_already_seen, key)) {
|
if (dictionary_contains_key(characters_already_seen, key)) {
|
||||||
has_unique = false;
|
has_unique = false;
|
||||||
} else {
|
} else {
|
||||||
@ -256,16 +256,16 @@ bool string_get_has_unique_characters(const string string_value) {
|
|||||||
return has_unique;
|
return has_unique;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_substring(const string string_value, size_t index_start, size_t index_end) {
|
string_t string_substring(const string_t string_value, size_t index_start, size_t index_end) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
for (size_t index = index_start; index <= index_end; index++) {
|
for (size_t index = index_start; index <= index_end; index++) {
|
||||||
character_append(result, string_value[index]);
|
character_append(result, string_value[index]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_is_substring(const string string_value, const string substring) {
|
bool string_get_is_substring(const string_t string_value, const string_t substring) {
|
||||||
bool is_substring = false;
|
bool is_substring = false;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
size_t substring_length = string_get_length(substring);
|
size_t substring_length = string_get_length(substring);
|
||||||
@ -281,14 +281,14 @@ bool string_get_is_substring(const string string_value, const string substring)
|
|||||||
return is_substring;
|
return is_substring;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_get_formatted_number(const long long number, string separator) {
|
string_t string_get_formatted_number(const long long number, string_t separator) {
|
||||||
string number_string = convert_number_to_string(number);
|
string_t number_string = convert_number_to_string(number);
|
||||||
bool is_negative = number_string[0] == '-';
|
bool is_negative = number_string[0] == '-';
|
||||||
if (is_negative) {
|
if (is_negative) {
|
||||||
number_string = string_substring(number_string, 1, string_get_length(number_string));
|
number_string = string_substring(number_string, 1, string_get_length(number_string));
|
||||||
}
|
}
|
||||||
size_t number_string_length = string_get_length(number_string);
|
size_t number_string_length = string_get_length(number_string);
|
||||||
string result = malloc(sizeof(string) * number_string_length);
|
string_t result = malloc(sizeof(string_t) * number_string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -313,9 +313,9 @@ string string_get_formatted_number(const long long number, string separator) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string string_get_last_occurence_of_character(const string string_value, char character) {
|
string_t string_get_last_occurence_of_character(const string_t string_value, char character) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
string result = malloc(sizeof(string) * string_length);
|
string_t result = malloc(sizeof(string_t) * string_length);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -331,7 +331,7 @@ string string_get_last_occurence_of_character(const string string_value, char ch
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_starts_with(const string string_value, const string prefix) {
|
bool string_starts_with(const string_t string_value, const string_t prefix) {
|
||||||
bool starts_with = true;
|
bool starts_with = true;
|
||||||
size_t prefix_length = string_get_length(prefix);
|
size_t prefix_length = string_get_length(prefix);
|
||||||
for (size_t index = 0; index < prefix_length && starts_with; index++) {
|
for (size_t index = 0; index < prefix_length && starts_with; index++) {
|
||||||
@ -340,7 +340,7 @@ bool string_starts_with(const string string_value, const string prefix) {
|
|||||||
return starts_with;
|
return starts_with;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_ends_with(const string string_value, const string prefix) {
|
bool string_ends_with(const string_t string_value, const string_t prefix) {
|
||||||
bool ends_with = true;
|
bool ends_with = true;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string_value);
|
||||||
size_t prefix_length = string_get_length(prefix);
|
size_t prefix_length = string_get_length(prefix);
|
||||||
|
48
lib/string.h
48
lib/string.h
@ -9,7 +9,7 @@
|
|||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
#include "dictionary.h"
|
#include "dictionary.h"
|
||||||
|
|
||||||
typedef char* string;
|
typedef char* string_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the length of a string (excluding '\0').
|
* @brief Return the length of a string (excluding '\0').
|
||||||
@ -18,7 +18,7 @@ typedef char* string;
|
|||||||
* @return size_t
|
* @return size_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
size_t string_get_length(const string string_value);
|
size_t string_get_length(const string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts all the alphabetic characters in a string to uppercase.
|
* @brief Converts all the alphabetic characters in a string to uppercase.
|
||||||
@ -26,7 +26,7 @@ size_t string_get_length(const string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_to_uppercase(string string_value);
|
string_t string_to_uppercase(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts all the alphabetic characters in a string to lowercase.
|
* @brief Converts all the alphabetic characters in a string to lowercase.
|
||||||
@ -34,7 +34,7 @@ string string_to_uppercase(string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_to_lowercase(string string_value);
|
string_t string_to_lowercase(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Replace all the occurrences of search value into replace value in the string.
|
* @brief Replace all the occurrences of search value into replace value in the string.
|
||||||
@ -44,7 +44,7 @@ string string_to_lowercase(string string_value);
|
|||||||
* @param replace_value A character containing the text to replace for match.
|
* @param replace_value A character containing the text to replace for match.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_replace(string string_value, char search, char replace);
|
string_t string_replace(string_t string_value, char search, char replace);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all whitespace from the start of a string.
|
* @brief Removes all whitespace from the start of a string.
|
||||||
@ -52,7 +52,7 @@ string string_replace(string string_value, char search, char replace);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_trim_start(string string_value);
|
string_t string_trim_start(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all whitespace from the end of a string.
|
* @brief Removes all whitespace from the end of a string.
|
||||||
@ -60,7 +60,7 @@ string string_trim_start(string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_trim_end(string string_value);
|
string_t string_trim_end(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all whitespace from the start and end of a string.
|
* @brief Removes all whitespace from the start and end of a string.
|
||||||
@ -68,7 +68,7 @@ string string_trim_end(string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_trim(string string_value);
|
string_t string_trim(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the copy of a string.
|
* @brief Return the copy of a string.
|
||||||
@ -76,7 +76,7 @@ string string_trim(string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_copy(const string string_value);
|
string_t string_copy(const string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Capitalizes the string.
|
* @brief Capitalizes the string.
|
||||||
@ -84,7 +84,7 @@ string string_copy(const string string_value);
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_capitalize(string string_value);
|
string_t string_capitalize(string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the total number of occurrences of the given character in the string.
|
* @brief Returns the total number of occurrences of the given character in the string.
|
||||||
@ -94,7 +94,7 @@ string string_capitalize(string string_value);
|
|||||||
* @return size_t
|
* @return size_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
size_t string_total_occurrences_of_character(string string_value, char character);
|
size_t string_total_occurrences_of_character(string_t string_value, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reverse the characters in an array.
|
* @brief Reverse the characters in an array.
|
||||||
@ -102,7 +102,7 @@ size_t string_total_occurrences_of_character(string string_value, char character
|
|||||||
* @param string
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_reverse(const string string_value);
|
string_t string_reverse(const string_t string_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if two strings are equals.
|
* @brief Check if two strings are equals.
|
||||||
@ -112,7 +112,7 @@ string string_reverse(const string string_value);
|
|||||||
* @return true if the strings are equals, false otherwise.
|
* @return true if the strings are equals, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_equals(const string string1, const string string2);
|
bool string_equals(const string_t string1, const string_t string2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the string is a integer.
|
* @brief Check if the string is a integer.
|
||||||
@ -121,7 +121,7 @@ bool string_equals(const string string1, const string string2);
|
|||||||
* @return true if the string is a integer, false otherwise.
|
* @return true if the string is a integer, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_is_integer(const string string_value);
|
bool string_get_is_integer(const string_t 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.
|
* @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.
|
||||||
@ -130,7 +130,7 @@ bool string_get_is_integer(const string string_value);
|
|||||||
* @param separator
|
* @param separator
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string* string_split(const string string_value, char separator, size_t* result_size);
|
string_t* string_split(const string_t 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.
|
* @brief Adds all the elements of an array into a string, separated by the specified separator string.
|
||||||
@ -139,7 +139,7 @@ string* string_split(const string string_value, char separator, size_t* result_s
|
|||||||
* @param separator
|
* @param separator
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_join(string* array, const char separator, size_t array_length);
|
string_t string_join(string_t* array, const char separator, size_t array_length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Concatenate two strings.
|
* @brief Concatenate two strings.
|
||||||
@ -148,7 +148,7 @@ string string_join(string* array, const char separator, size_t array_length);
|
|||||||
* @param string2
|
* @param string2
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_concatenate(string string1, string string2);
|
string_t string_concatenate(string_t string1, string_t string2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string contains only unique characters.
|
* @brief Check if a string contains only unique characters.
|
||||||
@ -157,7 +157,7 @@ string string_concatenate(string string1, string string2);
|
|||||||
* @return true if string contains only unique characters, false otherwise.
|
* @return true if string contains only unique characters, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_has_unique_characters(const string string);
|
bool string_get_has_unique_characters(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the part of the string between the start and end indexes (both included).
|
* @brief Returns the part of the string between the start and end indexes (both included).
|
||||||
@ -167,7 +167,7 @@ bool string_get_has_unique_characters(const string string);
|
|||||||
* @param index_end
|
* @param index_end
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_substring(const string string_value, size_t index_start, size_t index_end);
|
string_t string_substring(const string_t string_value, size_t index_start, size_t index_end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string contains a substring.
|
* @brief Check if a string contains a substring.
|
||||||
@ -177,7 +177,7 @@ string string_substring(const string string_value, size_t index_start, size_t in
|
|||||||
* @return true if the string contains the substring, false otherwise.
|
* @return true if the string contains the substring, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_is_substring(const string string_value, const string substring);
|
bool string_get_is_substring(const string_t string_value, const string_t substring);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Format a number to a string with specified separator.
|
* @brief Format a number to a string with specified separator.
|
||||||
@ -186,7 +186,7 @@ bool string_get_is_substring(const string string_value, const string substring);
|
|||||||
* @example string_get_formatted_number(1000, " ") // "1 000"
|
* @example string_get_formatted_number(1000, " ") // "1 000"
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_get_formatted_number(const long long number, string separator);
|
string_t string_get_formatted_number(const long long number, string_t separator);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns a pointer to the last occurrence of character in the string.
|
* @brief Returns a pointer to the last occurrence of character in the string.
|
||||||
@ -195,7 +195,7 @@ string string_get_formatted_number(const long long number, string separator);
|
|||||||
* @param character
|
* @param character
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string string_get_last_occurence_of_character(const string string_value, char character);
|
string_t string_get_last_occurence_of_character(const string_t string_value, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string starts with a substring.
|
* @brief Check if a string starts with a substring.
|
||||||
@ -205,7 +205,7 @@ string string_get_last_occurence_of_character(const string string_value, char ch
|
|||||||
* @return true if the string starts with the substring, false otherwise.
|
* @return true if the string starts with the substring, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_starts_with(const string string_value, const string prefix);
|
bool string_starts_with(const string_t string_value, const string_t prefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string ends with a substring.
|
* @brief Check if a string ends with a substring.
|
||||||
@ -215,6 +215,6 @@ bool string_starts_with(const string string_value, const string prefix);
|
|||||||
* @return true if the string ends with the substring, false otherwise.
|
* @return true if the string ends with the substring, false otherwise.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_ends_with(const string string_value, const string prefix);
|
bool string_ends_with(const string_t string_value, const string_t prefix);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
|
||||||
string terminal_input() {
|
string_t terminal_input() {
|
||||||
char character;
|
char character;
|
||||||
size_t length = 1;
|
size_t length = 1;
|
||||||
string string_value = malloc(length * sizeof(char));
|
string_t string_value = malloc(length * sizeof(char));
|
||||||
if (string_value == NULL) {
|
if (string_value == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* @brief Read a line from stdin.
|
* @brief Read a line from stdin.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string terminal_input();
|
string_t terminal_input();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print an array.
|
* @brief Print an array.
|
||||||
|
2
main.c
2
main.c
@ -4,7 +4,7 @@
|
|||||||
#include "libcproject.h"
|
#include "libcproject.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
string string_value = "Hello, world!";
|
string_t string_value = "Hello, world!";
|
||||||
printf("%s\n", string_value);
|
printf("%s\n", string_value);
|
||||||
printf("string_length = %ld\n", string_get_length(string_value));
|
printf("string_length = %ld\n", string_get_length(string_value));
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
#include "libcproject.h"
|
#include "libcproject.h"
|
||||||
|
|
||||||
int main(int argc, string* argv) {
|
int main(int argc, string_t* argv) {
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "Usage: %s <version>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <version>\n", argv[0]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
string content = "#ifndef __LIBCPROJECT_VERSION__\n";
|
string_t content = "#ifndef __LIBCPROJECT_VERSION__\n";
|
||||||
content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \"");
|
content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \"");
|
||||||
content = string_concatenate(content, argv[1]);
|
content = string_concatenate(content, argv[1]);
|
||||||
content = string_concatenate(content, "\"\n\n");
|
content = string_concatenate(content, "\"\n\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user