From 290691564efedbd899d89eaf3c685dc667cc96fe Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 1 May 2020 23:51:35 -0500 Subject: [PATCH] Update: Fix string length calculation and report when no changes for fll_program functions Both fl_string_rip and fl_string_rip_trim use include ranges and not lengths. Record the start length to identify if there were changes and return f_no_data when there are not changes. --- level_2/fll_program/c/program.c | 102 +++++++++++++++++++++++++++------------- level_2/fll_program/c/program.h | 2 + 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index 3fb05b0..654502d 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -161,6 +161,7 @@ extern "C" { f_status status = f_none; f_string_length length = 0; + f_string_length start = destination->used; for (f_string_length i = 0; i < additional.used; i++) { length = strnlen(argv[additional.array[i]], f_console_max_size); @@ -172,6 +173,10 @@ extern "C" { } } // for + if (status == f_none && start == destination->used) { + return f_no_data; + } + return status; } #endif // _di_fll_program_parameter_additional_mash_ @@ -186,22 +191,28 @@ extern "C" { f_status status = f_none; + f_string_length length = 0; + f_string_length start = destination->used; f_string_dynamic ripped = f_string_dynamic_initialize; for (f_string_length i = 0; i < additional.used; i++) { - status = fl_string_rip_trim(argv[additional.array[i]], 0, strnlen(argv[additional.array[i]], f_console_max_size), &ripped); - - if (f_status_is_error(status)) { - f_macro_string_dynamic_delete_simple(ripped); - return status; - } + length = strnlen(argv[additional.array[i]], f_console_max_size); - if (ripped.used > 0) { - status = fl_string_dynamic_mash(glue, glue_length, ripped, destination); + if (length > 0) { + status = fl_string_rip_trim(argv[additional.array[i]], 0, length - 1, &ripped); if (f_status_is_error(status)) { f_macro_string_dynamic_delete_simple(ripped); - return f_status_set_error(f_string_too_large); + return status; + } + + if (ripped.used > 0) { + status = fl_string_dynamic_mash(glue, glue_length, ripped, destination); + + if (f_status_is_error(status)) { + f_macro_string_dynamic_delete_simple(ripped); + return f_status_set_error(f_string_too_large); + } } } } // for @@ -210,6 +221,10 @@ extern "C" { f_macro_string_dynamic_delete(status, ripped); } + if (status == f_none && start == destination->used) { + return f_no_data; + } + return status; } #endif // _di_fll_program_parameter_additional_mash_trim_ @@ -223,28 +238,39 @@ extern "C" { f_status status = f_none; + f_string_length length = 0; + f_string_length start = result->used; + for (f_string_length i = 0; i < additional.used; i++) { - f_string_dynamic ripped = f_string_dynamic_initialize; + length = strnlen(argv[additional.array[i]], f_console_max_size); - status = fl_string_rip(argv[additional.array[i]], 0, strnlen(argv[additional.array[i]], f_console_max_size), &ripped); + if (length > 0) { + f_string_dynamic ripped = f_string_dynamic_initialize; - if (f_status_is_error(status)) return status; + status = fl_string_rip(argv[additional.array[i]], 0, length - 1, &ripped); - if (status == f_no_data) { - status = f_none; - } - else { - if (result->used >= result->size) { - f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); + if (f_status_is_error(status)) return status; - if (f_status_is_error(status)) return status; + if (status == f_no_data) { + status = f_none; } + else { + if (result->used >= result->size) { + f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); - result->array[result->used] = ripped; - result->used++; + if (f_status_is_error(status)) return status; + } + + result->array[result->used] = ripped; + result->used++; + } } } // for + if (status == f_none && start == result->used) { + return f_no_data; + } + return status; } #endif // _di_fll_program_parameter_additional_rip_ @@ -257,29 +283,39 @@ extern "C" { #endif // _di_level_2_parameter_checking_ f_status status = f_none; + f_string_length length = 0; + f_string_length start = result->used; for (f_string_length i = 0; i < additional.used; i++) { - f_string_dynamic ripped = f_string_dynamic_initialize; + length = strnlen(argv[additional.array[i]], f_console_max_size); - status = fl_string_rip_trim(argv[additional.array[i]], 0, strnlen(argv[additional.array[i]], f_console_max_size), &ripped); + if (length > 0) { + f_string_dynamic ripped = f_string_dynamic_initialize; - if (f_status_is_error(status)) return status; + status = fl_string_rip_trim(argv[additional.array[i]], 0, length - 1, &ripped); - if (status == f_no_data) { - status = f_none; - } - else { - if (result->used >= result->size) { - f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); + if (f_status_is_error(status)) return status; - if (f_status_is_error(status)) return status; + if (status == f_no_data) { + status = f_none; } + else { + if (result->used >= result->size) { + f_macro_string_dynamics_resize(status, (*result), result->size + f_console_default_allocation_step); - result->array[result->used] = ripped; - result->used++; + if (f_status_is_error(status)) return status; + } + + result->array[result->used] = ripped; + result->used++; + } } } // for + if (status == f_none && start == result->used) { + return f_no_data; + } + return status; } #endif // _di_fll_program_parameter_additional_rip_trim_ diff --git a/level_2/fll_program/c/program.h b/level_2/fll_program/c/program.h index 68c55b7..aaa637b 100644 --- a/level_2/fll_program/c/program.h +++ b/level_2/fll_program/c/program.h @@ -175,6 +175,7 @@ extern "C" { * * @return * f_none on success. + * f_no_data if nothing to rip, no allocations or reallocations are performed. * f_string_max_size (with error bit) if the combined string is too large. * f_invalid_parameter (with error bit) if a parameter is invalid. * f_error_allocation (with error bit) on memory allocation error. @@ -201,6 +202,7 @@ extern "C" { * * @return * f_none on success. + * f_no_data if nothing to rip, no allocations or reallocations are performed. * f_string_max_size (with error bit) if the combined string is too large. * f_invalid_parameter (with error bit) if a parameter is invalid. * f_error_allocation (with error bit) on memory allocation error. -- 1.8.3.1