From: Kevin Day Date: Tue, 15 Sep 2020 01:05:03 +0000 (-0500) Subject: Update: use *_size_increase() functions, improve logic, and return F_string_too_large. X-Git-Tag: 0.5.0^0 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=20581a1bbb0da9d40b885e7e8925178e32dcc79c;p=fll Update: use *_size_increase() functions, improve logic, and return F_string_too_large. Use the *_size_increase() functions more. Anything declared inside of a loop is destroyed at the end of the loop and created at the beginning. This is an easily avoidable performance concern. When the max size is reached but allocation is still possible (but at a smaller than requested length) return F_string_too_large without the error bit set. This is useful for when only 1 unit is needed but the resize is say 4. In the cases where the resize must exactly match the given length, then treat F_string_too_large without the error bit as if it does have an error bit. --- diff --git a/level_1/fl_string/c/private-string.c b/level_1/fl_string/c/private-string.c index 9779f3a..27a4b9a 100644 --- a/level_1/fl_string/c/private-string.c +++ b/level_1/fl_string/c/private-string.c @@ -8,19 +8,15 @@ extern "C" { #if !defined(_di_fl_string_append_) || !defined(_di_fl_string_dynamic_append_) || !defined(_di_fl_string_append_mash_) || !defined(_di_fl_string_dynamic_mash_) f_return_status private_fl_string_append(const f_string_t source, const f_string_length_t length, f_string_dynamic_t *destination) { - if (destination->used + length > f_string_length_t_size) return F_status_set_error(F_string_too_large); - f_status_t status = F_none; - const f_string_length_t total = destination->used + length; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + if (destination->used + length > destination->size) { + status = private_fl_string_dynamic_size_increase(length, destination); if (F_status_is_error(status)) return status; } memcpy(destination->string + destination->used, source, length); - destination->used = total; + destination->used = destination->used + length; return F_none; } @@ -34,23 +30,21 @@ extern "C" { f_status_t status = F_none; f_string_length_t first = 0; + f_string_length_t size = 0; for (f_string_length_t i = 0; i <= length; i++) { + if (i == length) { if (i > first) { - f_string_length_t size = i - first; + size = i - first; - if (destination->used + size > f_string_length_t_size) return F_status_set_error(F_string_too_large); - - f_string_length_t total = destination->used + size; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_size_increase(size, destination); if (F_status_is_error(status)) return status; } memcpy(destination->string + destination->used, source + first, size); - destination->used = total; + destination->used = destination->used + size; } break; @@ -59,19 +53,15 @@ extern "C" { if (source[i] == 0) { if (i > 0) { if (i > first) { - f_string_length_t size = i - first; - - if (destination->used + size > f_string_length_t_size) return F_status_set_error(F_string_too_large); + size = i - first; - f_string_length_t total = destination->used + size; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_size_increase(size, destination); if (F_status_is_error(status)) return status; } memcpy(destination->string + destination->used, source + first, size); - destination->used = total; + destination->used = destination->used + size; } } @@ -135,6 +125,7 @@ extern "C" { width_max = (stop1 - i1) + 1; status = f_utf_is_whitespace(string1 + i1, width_max); + if (F_status_is_error(status)) { if (F_status_set_fine(status) == F_maybe) return F_status_set_error(F_utf); @@ -154,6 +145,7 @@ extern "C" { width_max = (stop2 - i2) + 1; status = f_utf_is_whitespace(string2 + i2, width_max); + if (F_status_is_error(status)) { if (F_status_set_fine(status) == F_maybe) return F_status_set_error(F_utf); @@ -203,6 +195,7 @@ extern "C" { width_max = (stop2 - j) + 1; status = f_utf_is_whitespace(string2 + j, width_max); + if (F_status_is_error(status)) { if (F_status_set_fine(status) == F_maybe) return F_status_set_error(F_utf); @@ -247,6 +240,24 @@ extern "C" { } #endif // !defined(_di_fl_string_compare_trim_) || !defined(_di_fl_string_dynamic_compare_trim_) || !defined(_di_fl_string_dynamic_partial_compare_trim_) +#if !defined(_di_fl_string_dynamic_size_increase_) || !defined(_di_fl_string_append_) || !defined(_di_fl_string_dynamic_append_) || !defined(_di_fl_string_append_mash_) || !defined(_di_fl_string_dynamic_mash_) || !defined(_di_fl_string_append_nulless_) || !defined(_di_fl_string_dynamic_append_nulless_) || !defined(_di_fl_string_mash_nulless_) || !defined(_di_fl_string_dynamic_mash_nulless_) || !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) || !defined(_di_fl_string_prepend_nulless_) || !defined(_di_fl_string_dynamic_prepend_nulless_) + f_return_status private_fl_string_dynamic_size_increase(const f_string_length_t length, f_string_dynamic_t *string) { + f_status_t status = F_none; + + if (string->size + length > f_string_length_t_size) { + if (string->size == f_string_length_t_size) { + return F_status_set_error(F_string_too_large); + } + + f_macro_string_dynamic_t_resize(status, (*string), f_string_length_t_size); + return F_string_too_large; + } + + f_macro_string_dynamic_t_resize(status, (*string), string->size + length); + return status; + } +#endif // !defined(_di_fl_string_dynamic_size_increase_) || !defined(_di_fl_string_append_) || !defined(_di_fl_string_dynamic_append_) || !defined(_di_fl_string_append_mash_) || !defined(_di_fl_string_dynamic_mash_) || !defined(_di_fl_string_append_nulless_) || !defined(_di_fl_string_dynamic_append_nulless_) || !defined(_di_fl_string_mash_nulless_) || !defined(_di_fl_string_dynamic_mash_nulless_) || !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) || !defined(_di_fl_string_prepend_nulless_) || !defined(_di_fl_string_dynamic_prepend_nulless_) + #if !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) || !defined(_di_fl_string_append_mish_) || !defined(_di_fl_string_dynamic_mish_) f_return_status private_fl_string_prepend(const f_string_t source, const f_string_length_t length, f_string_dynamic_t *destination) { @@ -256,14 +267,12 @@ extern "C" { f_status_t status = F_none; - const f_string_length_t total = destination->used + length; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + if (destination->used + length > destination->size) { + status = private_fl_string_dynamic_size_increase(length, destination); if (F_status_is_error(status)) return status; } - if (destination->used > 0) { + if (destination->used) { memmove(destination->string + length, destination->string, destination->used); memcpy(destination->string, source, length); } @@ -271,7 +280,7 @@ extern "C" { memcpy(destination->string, source, length); } - destination->used = total; + destination->used = destination->used + length; return F_none; } #endif // !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) @@ -288,26 +297,22 @@ extern "C" { f_string_length_t first = 0; f_string_length_t offset = 0; + f_string_length_t size = 0; + for (f_string_length_t i = 0; i <= length; i++) { if (i == length) { if (i > first) { - const f_string_length_t size = i - first; + size = i - first; - if (destination->used + size > f_string_length_t_size) { - return F_status_set_error(F_string_too_large); - } - - const f_string_length_t total = destination->used + size; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_size_increase(size, destination); if (F_status_is_error(status)) return status; } memmove(destination->string + offset + size, destination->string + offset, destination->used - offset); memcpy(destination->string + offset, source + first, size); - destination->used = total; + destination->used = destination->used + size; offset += size; } @@ -317,24 +322,17 @@ extern "C" { if (source[i] == 0) { if (i > 0) { if (i > first) { - const f_string_length_t size = i - first; - - if (destination->used + size > f_string_length_t_size) { - return F_status_set_error(F_string_too_large); - } - - const f_string_length_t total = destination->used + size; - - if (total > destination->size) { - f_macro_string_dynamic_t_resize(status, (*destination), total); + size = i - first; + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_size_increase(size, destination); if (F_status_is_error(status)) return status; } memmove(destination->string + offset + size, destination->string + offset, destination->used - offset); memcpy(destination->string + offset, source + first, size); - destination->used = total; + destination->used = destination->used + size; offset += size; } } diff --git a/level_1/fl_string/c/private-string.h b/level_1/fl_string/c/private-string.h index 0739ac3..d7a90d1 100644 --- a/level_1/fl_string/c/private-string.h +++ b/level_1/fl_string/c/private-string.h @@ -138,6 +138,28 @@ extern "C" { #endif // !defined(_di_fl_string_compare_trim_) || !defined(_di_fl_string_dynamic_compare_trim_) || !defined(_di_fl_string_dynamic_partial_compare_trim_) /** + * Private implementation of fl_string_dynamic_size_increase(). + * + * Intended to be shared to each of the different implementation variations. + * + * @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_string_too_large on success, but requested size is too small (resize is smaller than requested length). + * 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. + */ +#if !defined(_di_fl_string_dynamic_size_increase_) || !defined(_di_fl_string_append_) || !defined(_di_fl_string_dynamic_append_) || !defined(_di_fl_string_append_mash_) || !defined(_di_fl_string_dynamic_mash_) || !defined(_di_fl_string_append_nulless_) || !defined(_di_fl_string_dynamic_append_nulless_) || !defined(_di_fl_string_mash_nulless_) || !defined(_di_fl_string_dynamic_mash_nulless_) || !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) || !defined(_di_fl_string_prepend_nulless_) || !defined(_di_fl_string_dynamic_prepend_nulless_) + extern f_return_status private_fl_string_dynamic_size_increase(const f_string_length_t length, f_string_dynamic_t *string) f_gcc_attribute_visibility_internal; +#endif // !defined(_di_fl_string_dynamic_size_increase_) || !defined(_di_fl_string_append_) || !defined(_di_fl_string_dynamic_append_) || !defined(_di_fl_string_append_mash_) || !defined(_di_fl_string_dynamic_mash_) || !defined(_di_fl_string_append_nulless_) || !defined(_di_fl_string_dynamic_append_nulless_) || !defined(_di_fl_string_mash_nulless_) || !defined(_di_fl_string_dynamic_mash_nulless_) || !defined(_di_fl_string_prepend_) || !defined(_di_fl_string_dynamic_prepend_) || !defined(_di_fl_string_prepend_nulless_) || !defined(_di_fl_string_dynamic_prepend_nulless_) + +/** * Private implementation of fl_string_prepend(). * * Intended to be shared to each of the different implementation variations. diff --git a/level_1/fl_string/c/string.c b/level_1/fl_string/c/string.c index db52543..8ebb363 100644 --- a/level_1/fl_string/c/string.c +++ b/level_1/fl_string/c/string.c @@ -773,10 +773,10 @@ extern "C" { f_status_t status = F_none; - if (string->used - length > 0) { + if (string->size - length > 0) { f_macro_string_dynamic_t_resize(status, (*string), string->size - length); } - else if (string->used - length <= 0) { + else if (string->size - length <= 0) { f_macro_string_dynamic_t_delete(status, (*string)); } @@ -791,23 +791,7 @@ extern "C" { if (string == 0) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - f_status_t status = F_none; - - if (string->used + length > string->size) { - if (string->used + length > f_string_length_t_size) { - if (string->used == f_string_length_t_size) { - status = F_status_set_error(F_string_too_large); - } - else { - f_macro_string_dynamic_t_resize(status, (*string), f_string_length_t_size); - } - } - else { - f_macro_string_dynamic_t_resize(status, (*string), string->size + length); - } - } - - return status; + return private_fl_string_dynamic_size_increase(length, string); } #endif // _di_fl_string_dynamic_size_increase_ @@ -820,10 +804,10 @@ extern "C" { f_status_t status = F_none; - if (strings->used - length > 0) { + if (strings->size - length > 0) { f_macro_string_dynamics_resize(status, (*strings), strings->size - length); } - else if (strings->used - length <= 0) { + else if (strings->size - length <= 0) { f_macro_string_dynamics_t_delete(status, (*strings)); } @@ -840,20 +824,16 @@ extern "C" { f_status_t status = F_none; - if (strings->used + length > strings->size) { - if (strings->used + length > f_array_length_t_size) { - if (strings->used == f_array_length_t_size) { - status = F_status_set_error(F_string_too_large); - } - else { - f_macro_string_dynamics_resize(status, (*strings), f_array_length_t_size); - } - } - else { - f_macro_string_dynamics_resize(status, (*strings), strings->size + length); + if (strings->size + length > f_array_length_t_size) { + if (strings->size == f_array_length_t_size) { + return F_status_set_error(F_string_too_large); } + + f_macro_string_dynamics_resize(status, (*strings), f_array_length_t_size); + return F_string_too_large; } + f_macro_string_dynamics_resize(status, (*strings), strings->size + length); return status; } #endif // _di_fl_string_dynamics_size_increase_ diff --git a/level_1/fl_string/c/string.h b/level_1/fl_string/c/string.h index f6becec..066574a 100644 --- a/level_1/fl_string/c/string.h +++ b/level_1/fl_string/c/string.h @@ -1093,6 +1093,7 @@ extern "C" { * * @return * F_none on success. + * F_string_too_large on success, but the requested length is too large for the buffer. * 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. @@ -1139,6 +1140,7 @@ extern "C" { * * @return * F_none on success. + * F_string_too_large on success, but the requested length is too large for the buffer. * 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. diff --git a/level_3/fake/c/private-make.c b/level_3/fake/c/private-make.c index 3e6a9cd..f72eac6 100644 --- a/level_3/fake/c/private-make.c +++ b/level_3/fake/c/private-make.c @@ -1131,15 +1131,10 @@ extern "C" { // pre-allocate the known arguments size. if (arguments->used + content.used > arguments->size) { - if (arguments->used + content.used > F_buffer_too_large) { - *status = F_status_set_error(F_buffer_too_large); - } - else { - f_macro_string_dynamics_resize((*status), (*arguments), arguments->used + content.used); - } + *status = fl_string_dynamics_size_increase(content.used, arguments); - if (F_status_is_error(*status)) { - fake_print_message(data, F_status_set_fine(*status), "f_macro_string_dynamics_resize", F_true, data_make->print); + if (F_status_is_error(*status) || *status == F_string_too_large) { + fake_print_message(data, F_status_set_fine(*status), "fl_string_dynamics_size_increase", F_true, data_make->print); return; } }