From 0f950d3240d334305060f4b2398fb70905e5f555 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Fri, 5 Jun 2020 14:56:53 -0300 Subject: [PATCH] Add string_swap --- src/libstring.c | 39 +++++++++++++++++++++++++++++++++++++++ src/libstring.h | 47 +++++++++++++++++++++++------------------------ test/main.c | 24 ++++++++++++++++++------ 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/libstring.c b/src/libstring.c index 0e07730..0c41a32 100644 --- a/src/libstring.c +++ b/src/libstring.c @@ -237,3 +237,42 @@ bool string_reserve(cstr_t *str, size_t capacity) str->reserved = capacity; return true; } +//! +//! \brief string_swap Swaps the content of str1 with str2. +//! \param str1 An initialized cstr_t *. +//! \param str2 An initialized cstr_t *. +//! \return A success-run boolean. +//! +bool string_swap(cstr_t * str1, cstr_t * str2) +{ + if (!str1) + { + fprintf(stderr, "In string_swap: str1 unitialized.\n"); + return false; + } + if (!str2) + { + fprintf(stderr, "In string_swap: str2 unitialized.\n"); + return false; + } + + size_t str1_val_size = str1->size; + char * str1_val_backup = malloc(str1_val_size + 1); + if(!str1_val_backup) + { + fprintf(stderr, "In string_swap: malloc failed.\n"); + } + + __strcpy(str1_val_backup, str1->value, str1_val_size); + size_t str1_res_backup = str1->reserved; + free(str1->value); + str1->value = str2->value; + str1->size = str2->size; + str1->reserved = str2->reserved; + + str2->value = str1_val_backup; + str2->size = str1_val_size; + str2->reserved = str1_res_backup; + + return true; +} diff --git a/src/libstring.h b/src/libstring.h index a71b8d0..70ef869 100644 --- a/src/libstring.h +++ b/src/libstring.h @@ -44,34 +44,33 @@ typedef struct cstr cstr_t; // Initialization and memory cstr_t * string_init(const char * origin); /* Attempts to reserve `capacity` bytes onto the string, returns true if possible. */ -bool string_reserve(cstr_t *str, size_t capacity); -bool string_resize(cstr_t *str, size_t new_size); -bool string_free(cstr_t * str); +bool string_reserve(cstr_t *str, size_t capacity); // def, tested, needs one more test case +bool string_resize(cstr_t *str, size_t new_size); // undef +bool string_free(cstr_t * str); // undef /* Frees all allocated strings */ -void string_free_all(void); // Free all alloc-ed strings +void string_free_all(void); // def, tested, unfinished // Utility functions -int string_compare(cstr_t * str1, cstr_t * str2); // Follows C++ standard -cstr_t * string_concat(cstr_t * str1, cstr_t * str2); // returns str1 + str2 -size_t string_concat_to(cstr_t * str1, cstr_t * str2); // str1 = str1 + str2 -bool string_ends_with_x(cstr_t * str, cstr_t * x); -char string_get_char_at(cstr_t * str, size_t pos); // Bound-checking -size_t string_index_of(cstr_t *str, char c); -size_t string_last_index_of(cstr_t *str, char c); -cstr_t * string_left(cstr_t * str, size_t length); // SIM TÔ COPIANDO FUNÇÃO DO QT NA CARA DURA E DAÍ -cstr_t * string_mid(cstr_t * str, size_t pos, size_t length); -size_t string_replace_char(cstr_t *str, char before, char after); -cstr_t * string_reverse(cstr_t * str); -size_t string_reserved_size(); -cstr_t string_right(cstr_t * str, size_t length); -bool string_set_char_at(cstr_t * str, size_t pos); // Bound-checking -size_t string_size(cstr_t * str); -bool string_starts_with_x(cstr_t * str, cstr_t * x); -bool string_swap(cstr_t * str1, cstr_t * str2); -int string_to_int(cstr_t * str); +int string_compare(cstr_t * str1, cstr_t * str2); // undef, Follows C++ standard +cstr_t * string_concat(cstr_t * str1, cstr_t * str2); // undef, returns str1 + str2 +size_t string_concat_to(cstr_t * str1, cstr_t * str2); // undef, str1 = str1 + str2 +bool string_ends_with_x(cstr_t * str, cstr_t * x); // undef +char string_get_char_at(cstr_t * str, size_t pos); // undef, bound-checking +size_t string_index_of(cstr_t *str, char c); // undef +size_t string_last_index_of(cstr_t *str, char c); // undef +cstr_t * string_left(cstr_t * str, size_t length); // undef +cstr_t * string_mid(cstr_t * str, size_t pos, size_t length); // undef +size_t string_replace_char(cstr_t *str, char before, char after); // undef +cstr_t * string_reverse(cstr_t * str); // undef +cstr_t string_right(cstr_t * str, size_t length); // undef +bool string_set_char_at(cstr_t * str, size_t pos); // undef +size_t string_size(cstr_t * str); // undef, TODO: unneeded? size is accessible within the string +bool string_starts_with_x(cstr_t * str, cstr_t * x); // undef +bool string_swap(cstr_t * str1, cstr_t * str2); // undef +int string_to_int(cstr_t * str); // undef -cstr_t * string_to_lower_case(cstr_t * origin); -cstr_t * string_to_upper_case(cstr_t * origin); +cstr_t * string_to_lower_case(cstr_t * origin); // def, tested +cstr_t * string_to_upper_case(cstr_t * origin); // def, tested /* Implement if possible (not a priority atm) */ //cstr_t string_tokenize(cstr_t * str, char delim); // Que inferno vai ser isso aqui. diff --git a/test/main.c b/test/main.c index 9fd4b98..12d95fa 100644 --- a/test/main.c +++ b/test/main.c @@ -5,8 +5,6 @@ int main(void) { cstr_t * str1 = string_init("THE CARMESIM PROJECT."); cstr_t * str2 = string_init("The Carmesim Project."); - printf("%s\n", str1->value); - printf("%zu %zu\n", str1->reserved, str1->size); //! Testing string_to_lower_case(cstr_t*, bool) printf("Testing string_to_lower_case(cstr_t*, bool)\n"); @@ -14,7 +12,7 @@ int main(void) printf("Before: %s\n", str1->value); cstr_t * str1low = string_to_lower_case(str1); //! str1low is a copy of str1 with all lower-case characters. printf("After: %s\n", str1low->value); - + //! //! Testing string_to_upper_case(cstr_t*) printf("Testing string_to_upper_case(cstr_t*, bool)\n"); @@ -22,13 +20,27 @@ int main(void) printf("Before: %s\n", str2->value); cstr_t * str2low = string_to_upper_case(str2); //! str1low is a copy of str1 with all lower-case characters. printf("After: %s\n", str2low->value); + //! + +// //! Testing string_resize(cstr_t*, bool); +// printf("Testing string_to_upper_case(cstr_t*, bool);\n"); +// if (string_reserve(str1, 2)) +// { +// printf("string_resized worked with cap=%zu.\n", str1->reserved); +// } +// //! + //! Testing string_swap(); + printf("Testing string_swap(cstr_t*, cstr_t*);\n"); - //! Testing string_resize(); - if (string_reserve(str1, 2)) + printf("Before string_swap: str1->val = %s, str2->val = %s.\n", str1->value, str2->value); + if(string_swap(str1, str2)) { - printf("string_resized worked with cap=%zu.\n", str1->reserved); + printf("string_swap worked.\n"); + printf("After string_swap: str1->val = %s, str2->val = %s.\n", str1->value, str2->value); } + //! + string_free_all(); } -- 2.11.4.GIT