From 70ee2ee9115515efeb9880b9ea1679dd6ee91f25 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 24 Aug 2020 21:44:13 -0500 Subject: [PATCH] Feature: provide common string related memory increment functions. There are a lot of string functions. In this case, it seems worth more to use functions instead of relying on macros, given how common this is. This does not update most of the existing code that may want to use this. Future consideration will likely be made in regards to the array of array string types. There is no intention to expand this process to other types. --- level_1/fl_string/c/string.c | 141 ++++++++++++++++++++++++++++++++++ level_1/fl_string/c/string.h | 138 +++++++++++++++++++++++++++++++++ level_2/fll_fss/c/fss_basic.c | 2 +- level_2/fll_fss/c/fss_basic.h | 1 + level_2/fll_fss/c/fss_basic_list.c | 2 +- level_2/fll_fss/c/fss_basic_list.h | 1 + level_2/fll_fss/c/fss_extended_list.c | 2 +- 7 files changed, 284 insertions(+), 3 deletions(-) diff --git a/level_1/fl_string/c/string.c b/level_1/fl_string/c/string.c index afa4063..be23725 100644 --- a/level_1/fl_string/c/string.c +++ b/level_1/fl_string/c/string.c @@ -764,6 +764,100 @@ extern "C" { } #endif // _di_fl_string_dynamic_prepend_nulless_ +#ifndef _di_fl_string_dynamic_size_decrease_ + f_return_status fl_string_dynamic_size_decrease(const f_string_length length, f_string_dynamic *string) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (string == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (string->used - length > 0) { + f_macro_string_dynamic_resize(status, (*string), string->size - length); + } + else if (string->used - length <= 0) { + f_macro_string_dynamic_delete(status, (*string)); + } + + return status; + } +#endif // _di_fl_string_dynamic_size_decrease_ + +#ifndef _di_fl_string_dynamic_size_increase_ + f_return_status fl_string_dynamic_size_increase(const f_string_length length, f_string_dynamic *string) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (string == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (string->used + length > string->size) { + if (string->used + length > f_string_length_size) { + if (string->used == f_string_length_size) { + status = F_status_set_error(F_string_too_large); + } + else { + f_macro_string_dynamic_resize(status, (*string), f_string_length_size); + } + } + else { + f_macro_string_dynamic_resize(status, (*string), string->size + length); + } + } + + return status; + } +#endif // _di_fl_string_dynamic_size_increase_ + +#ifndef _di_fl_string_dynamics_size_decrease_ + f_return_status fl_string_dynamics_size_decrease(const f_array_length length, f_string_dynamics *strings) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (strings == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (strings->used - length > 0) { + f_macro_string_dynamics_resize(status, (*strings), strings->size - length); + } + else if (strings->used - length <= 0) { + f_macro_string_dynamics_delete(status, (*strings)); + } + + return status; + } +#endif // _di_fl_string_dynamics_size_decrease_ + +#ifndef _di_fl_string_dynamics_size_increase_ + f_return_status fl_string_dynamics_size_increase(const f_array_length length, f_string_dynamics *strings) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (strings == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (strings->used + length > strings->size) { + if (strings->used + length > f_array_length_size) { + if (strings->used == f_array_length_size) { + status = F_status_set_error(F_string_too_large); + } + else { + f_macro_string_dynamics_resize(status, (*strings), f_array_length_size); + } + } + else { + f_macro_string_dynamics_resize(status, (*strings), strings->size + length); + } + } + + return status; + } +#endif // _di_fl_string_dynamics_size_increase_ + #ifndef _di_fl_string_dynamic_rip_ f_return_status fl_string_dynamic_rip(const f_string_static source, const f_string_range range, f_string_dynamic *destination) { #ifndef _di_level_1_parameter_checking_ @@ -1135,6 +1229,53 @@ extern "C" { } #endif // _di_fl_string_dynamic_terminate_after_ +#ifndef _di_fl_string_lengths_size_decrease_ + f_return_status fl_string_lengths_size_decrease(const f_array_length length, f_string_lengths *lengths) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (lengths == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (lengths->used - length > 0) { + f_macro_string_lengths_resize(status, (*lengths), lengths->size - length); + } + else if (lengths->used - length <= 0) { + f_macro_string_lengths_delete(status, (*lengths)); + } + + return status; + } +#endif // _di_fl_string_lengths_size_decrease_ + +#ifndef _di_fl_string_lengths_size_increase_ + f_return_status fl_string_lengths_size_increase(const f_array_length length, f_string_lengths *lengths) { + #ifndef _di_level_1_parameter_checking_ + if (length == 0) return F_status_set_error(F_parameter); + if (lengths == 0) return F_status_set_error(F_parameter); + #endif // _di_level_1_parameter_checking_ + + f_status status = F_none; + + if (lengths->used + length > lengths->size) { + if (lengths->used + length > f_array_length_size) { + if (lengths->used == f_array_length_size) { + status = F_status_set_error(F_string_too_large); + } + else { + f_macro_string_lengths_resize(status, (*lengths), f_array_length_size); + } + } + else { + f_macro_string_lengths_resize(status, (*lengths), lengths->size + length); + } + } + + return status; + } +#endif // _di_fl_string_lengths_size_increase_ + #ifndef _di_fl_string_mash_ f_return_status fl_string_mash(const f_string glue, const f_string_length glue_length, const f_string source, const f_string_length length, f_string_dynamic *destination) { #ifndef _di_level_1_parameter_checking_ diff --git a/level_1/fl_string/c/string.h b/level_1/fl_string/c/string.h index 904d973..8d108d5 100644 --- a/level_1/fl_string/c/string.h +++ b/level_1/fl_string/c/string.h @@ -1057,6 +1057,98 @@ extern "C" { #endif // _di_fl_string_dynamic_prepend_nulless_ /** + * Resize the dynamic string to a smaller size. + * + * This will resize making the string smaller based on the given length. + * If the given length is too small, then the resize will fail. + * This will not shrink the size to less than 0. + * + * @param length + * A positive number greater than 0 representing how much to decrease the size by. + * @param string + * The string to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_dynamic_size_decrease_ + extern f_return_status fl_string_dynamic_size_decrease(const f_string_length length, f_string_dynamic *string); +#endif // _di_fl_string_dynamic_size_decrease_ + +/** + * Resize the dynamic string to a larger size. + * + * This will resize making the string larger based on the given length. + * If the given length is too large for the buffer, then attempt to set max buffer size (f_string_length_size). + * If already set to the maximum buffer size, then the resize will fail. + * + * @param length + * A positive number greater than 0 representing how much to increase the size by. + * @param string + * The string to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_dynamic_size_increase_ + extern f_return_status fl_string_dynamic_size_increase(const f_string_length length, f_string_dynamic *string); +#endif // _di_fl_string_dynamic_size_increase_ + +/** + * Resize the array of dynamic strings to a smaller size. + * + * This will resize making the string smaller based on the given length. + * If the given length is too small, then the resize will fail. + * This will not shrink the size to less than 0. + * + * @param length + * A positive number greater than 0 representing how much to decrease the size by. + * @param strings + * The string array to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_dynamics_size_decrease_ + extern f_return_status fl_string_dynamics_size_decrease(const f_array_length length, f_string_dynamics *strings); +#endif // _di_fl_string_dynamics_size_decrease_ + +/** + * Resize the array of dynamic strings to a larger size. + * + * This will resize making the string larger based on the given length. + * If the given length is too large for the buffer, then attempt to set max buffer size (f_array_length_size). + * If already set to the maximum buffer size, then the resize will fail. + * + * @param length + * A positive number greater than 0 representing how much to increase the size by. + * @param strings + * The string array to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_dynamics_size_increase_ + extern f_return_status fl_string_dynamics_size_increase(const f_array_length length, f_string_dynamics *strings); +#endif // _di_fl_string_dynamics_size_increase_ + +/** * Allocate a new string from the provided range in the buffer. * * Ignores leading and trailing whitespace. @@ -1329,6 +1421,52 @@ extern "C" { #endif // _di_fl_string_dynamic_terminate_after_ /** + * Resize the array of string lengths to a smaller size. + * + * This will resize making the string smaller based on the given length. + * If the given length is too small, then the resize will fail. + * This will not shrink the size to less than 0. + * + * @param length + * A positive number greater than 0 representing how much to decrease the size by. + * @param strings + * The string array to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_lengths_size_decrease_ + extern f_return_status fl_string_length_size_decrease(const f_array_length length, f_string_lengths *lengths); +#endif // _di_fl_string_lengths_size_decrease_ + +/** + * Resize the array of string lengths to a larger size. + * + * This will resize making the string larger based on the given length. + * If the given length is too large for the buffer, then attempt to set max buffer size (f_array_length_size). + * If already set to the maximum buffer size, then the resize will fail. + * + * @param length + * A positive number greater than 0 representing how much to increase the size by. + * @param strings + * The string array to resize. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on memory allocation error. + * F_memory_reallocation (with error bit) on memory reallocation error. + * F_parameter (with error bit) if a parameter is invalid. + * F_string_too_large (with error bit) if the combined string is too large. + */ +#ifndef _di_fl_string_lengths_size_increase_ + extern f_return_status fl_string_lengths_size_increase(const f_array_length length, f_string_lengths *lengths); +#endif // _di_fl_string_lengths_size_increase_ + +/** * Append the source string onto the destination with the glue in between. * * If the destination string is empty, then no glue is appended. diff --git a/level_2/fll_fss/c/fss_basic.c b/level_2/fll_fss/c/fss_basic.c index 2b3ed90..96fa82f 100644 --- a/level_2/fll_fss/c/fss_basic.c +++ b/level_2/fll_fss/c/fss_basic.c @@ -183,7 +183,7 @@ extern "C" { } else { if (destination->used == destination->size) { - f_macro_string_dynamic_resize(status, (*destination), destination->size + f_fss_default_allocation_step_string); + status = fl_string_dynamic_size_increase(f_fss_default_allocation_step_string, destination); if (F_status_is_error(status)) return status; } diff --git a/level_2/fll_fss/c/fss_basic.h b/level_2/fll_fss/c/fss_basic.h index d3656e8..0a2e33d 100644 --- a/level_2/fll_fss/c/fss_basic.h +++ b/level_2/fll_fss/c/fss_basic.h @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { diff --git a/level_2/fll_fss/c/fss_basic_list.c b/level_2/fll_fss/c/fss_basic_list.c index d2767da..9835ba9 100644 --- a/level_2/fll_fss/c/fss_basic_list.c +++ b/level_2/fll_fss/c/fss_basic_list.c @@ -165,7 +165,7 @@ extern "C" { } else { if (buffer->used == buffer->size) { - f_macro_string_dynamic_resize(status, (*buffer), buffer->size + f_fss_default_allocation_step_string); + status = fl_string_dynamic_size_increase(f_fss_default_allocation_step_string, buffer); if (F_status_is_error(status)) return status; } diff --git a/level_2/fll_fss/c/fss_basic_list.h b/level_2/fll_fss/c/fss_basic_list.h index 6b603d5..5db758c 100644 --- a/level_2/fll_fss/c/fss_basic_list.h +++ b/level_2/fll_fss/c/fss_basic_list.h @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { diff --git a/level_2/fll_fss/c/fss_extended_list.c b/level_2/fll_fss/c/fss_extended_list.c index 05b0cd5..e5cf709 100644 --- a/level_2/fll_fss/c/fss_extended_list.c +++ b/level_2/fll_fss/c/fss_extended_list.c @@ -140,7 +140,7 @@ extern "C" { } else { if (buffer->used == buffer->size) { - f_macro_string_dynamic_resize(status, (*buffer), buffer->size + f_fss_default_allocation_step_string); + status = fl_string_dynamic_size_increase(f_fss_default_allocation_step_string, buffer); if (F_status_is_error(status)) return status; } -- 1.8.3.1