From 930cb3c5b8e2d55c6f54fa937d9a5ad484099c08 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 5 Jun 2022 16:03:14 -0500 Subject: [PATCH] Update: Improve the logic in the string comparison tests. Several of the F_parameter errors do not need to be returned. This logic is already handled by the called functions as not matched. Make sure the stop range is not longer than the buffer used. When the buffer used is smaller than the requested range stop, then use the buffer used rather than the stop point. --- level_1/fl_string/c/string.c | 213 +++---------------------------------------- 1 file changed, 13 insertions(+), 200 deletions(-) diff --git a/level_1/fl_string/c/string.c b/level_1/fl_string/c/string.c index aae950c..df2ed08 100644 --- a/level_1/fl_string/c/string.c +++ b/level_1/fl_string/c/string.c @@ -35,6 +35,7 @@ extern "C" { #ifndef _di_fl_string_dynamic_compare_ f_status_t fl_string_dynamic_compare(const f_string_static_t string1, const f_string_static_t string2) { + return private_fl_string_compare(string1.string, string2.string, 0, 0, string1.used, string2.used); } #endif // _di_fl_string_dynamic_compare_ @@ -90,277 +91,91 @@ extern "C" { #ifndef _di_fl_string_dynamic_partial_compare_ f_status_t fl_string_dynamic_partial_compare(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range1, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string1.used <= range1.stop) return F_status_set_error(F_parameter); - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used || range1.start > range1.stop) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare(string1.string, string2.string, range1.start, range2.start, range1.stop + 1, range2.stop + 1); + return private_fl_string_compare(string1.string, string2.string, range1.start, range2.start, string1.used <= range1.stop ? string1.used : range1.stop + 1, string2.used <= range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_ #ifndef _di_fl_string_dynamic_partial_compare_dynamic_ f_status_t fl_string_dynamic_partial_compare_dynamic(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare(string1.string, string2.string, 0, range2.start, string1.used, range2.stop + 1); + return private_fl_string_compare(string1.string, string2.string, 0, range2.start, string1.used, string2.used <= range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_dynamic_ #ifndef _di_fl_string_dynamic_partial_compare_string_ f_status_t fl_string_dynamic_partial_compare_string(const f_string_t string1, const f_string_static_t string2, const f_array_length_t length1, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!length1) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - return private_fl_string_compare(string1, string2.string, 0, range2.start, length1, range2.stop + 1); + return private_fl_string_compare(string1, string2.string, 0, range2.start, length1, string2.used < range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_string_ #ifndef _di_fl_string_dynamic_partial_compare_except_ f_status_t fl_string_dynamic_partial_compare_except(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range1, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string1.used <= range1.stop) return F_status_set_error(F_parameter); - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used || range1.start > range1.stop) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_except(string1.string, string2.string, range1.start, range2.start, range1.stop + 1, range2.stop + 1, except1, except2); + return private_fl_string_compare_except(string1.string, string2.string, range1.start, range2.start, string1.used < range1.stop ? string1.used : range1.stop + 1, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_ #ifndef _di_fl_string_dynamic_partial_compare_except_dynamic_ f_status_t fl_string_dynamic_partial_compare_except_dynamic(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_except(string1.string, string2.string, 0, range2.start, string1.used, range2.stop + 1, except1, except2); + return private_fl_string_compare_except(string1.string, string2.string, 0, range2.start, string1.used, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_dynamic_ #ifndef _di_fl_string_dynamic_partial_compare_except_string_ f_status_t fl_string_dynamic_partial_compare_except_string(const f_string_t string1, const f_string_static_t string2, const f_array_length_t length1, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!length1) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - return private_fl_string_compare_except(string1, string2.string, 0, range2.start, length1, range2.stop + 1, except1, except2); + return private_fl_string_compare_except(string1, string2.string, 0, range2.start, length1, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_string_ #ifndef _di_fl_string_dynamic_partial_compare_except_trim_ f_status_t fl_string_dynamic_partial_compare_except_trim(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range1, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string1.used <= range1.stop) return F_status_set_error(F_parameter); - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used || range1.start > range1.stop) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_except_trim(string1.string, string2.string, range1.start, range2.start, range1.stop + 1, range2.stop + 1, except1, except2); + return private_fl_string_compare_except_trim(string1.string, string2.string, range1.start, range2.start, string1.used <= range1.stop ? string1.used : range1.stop + 1, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_trim_ #ifndef _di_fl_string_dynamic_partial_compare_except_trim_dynamic_ f_status_t fl_string_dynamic_partial_compare_except_trim_dynamic(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_except_trim(string1.string, string2.string, 0, range2.start, string1.used, range2.stop + 1, except1, except2); + return private_fl_string_compare_except_trim(string1.string, string2.string, 0, range2.start, string1.used, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_trim_dynamic_ #ifndef _di_fl_string_dynamic_partial_compare_except_trim_string_ f_status_t fl_string_dynamic_partial_compare_except_trim_string(const f_string_t string1, const f_string_static_t string2, const f_array_length_t length1, const f_string_range_t range2, const f_array_lengths_t except1, const f_array_lengths_t except2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!length1) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - return private_fl_string_compare_except_trim(string1, string2.string, 0, range2.start, length1, range2.stop + 1, except1, except2); + return private_fl_string_compare_except_trim(string1, string2.string, 0, range2.start, length1, string2.used <= range2.stop ? string2.used : range2.stop + 1, except1, except2); } #endif // _di_fl_string_dynamic_partial_compare_except_trim_string_ #ifndef _di_fl_string_dynamic_partial_compare_trim_ f_status_t fl_string_dynamic_partial_compare_trim(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range1, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string1.used <= range1.stop) return F_status_set_error(F_parameter); - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used || range1.start > range1.stop) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_trim(string1.string, string2.string, range1.start, range2.start, range1.stop + 1, range2.stop + 1); + return private_fl_string_compare_trim(string1.string, string2.string, range1.start, range2.start, string1.used <= range1.stop ? string1.used : range1.stop + 1, string2.used <= range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_trim_ #ifndef _di_fl_string_dynamic_partial_compare_trim_dynamic_ f_status_t fl_string_dynamic_partial_compare_trim_dynamic(const f_string_static_t string1, const f_string_static_t string2, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!string1.used) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - - return private_fl_string_compare_trim(string1.string, string2.string, 0, range2.start, string1.used, range2.stop + 1); + return private_fl_string_compare_trim(string1.string, string2.string, 0, range2.start, string1.used, string2.used <= range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_trim_dynamic_ #ifndef _di_fl_string_dynamic_partial_compare_trim_string_ f_status_t fl_string_dynamic_partial_compare_trim_string(const f_string_t string1, const f_string_static_t string2, const f_array_length_t length1, const f_string_range_t range2) { - #ifndef _di_level_1_parameter_checking_ - if (string2.used <= range2.stop) return F_status_set_error(F_parameter); - #endif // _di_level_1_parameter_checking_ - - if (!length1) { - if (!string2.used || range2.start > range2.stop) { - return F_equal_to; - } - - return F_equal_to_not; - } - - if (!string2.used || range2.start > range2.stop) { - return F_equal_to_not; - } - return private_fl_string_compare_trim(string1, string2.string, 0, range2.start, length1, range2.stop + 1); + return private_fl_string_compare_trim(string1, string2.string, 0, range2.start, length1, string2.used <= range2.stop ? string2.used : range2.stop + 1); } #endif // _di_fl_string_dynamic_partial_compare_trim_string_ #ifndef _di_fl_string_dynamic_partial_rip_ f_status_t fl_string_dynamic_partial_rip(const f_string_static_t source, const f_string_range_t range, f_string_dynamic_t * const destination) { #ifndef _di_level_1_parameter_checking_ - if (source.used <= range.start) return F_status_set_error(F_parameter); - if (source.used <= range.stop) return F_status_set_error(F_parameter); if (!destination) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ @@ -395,8 +210,6 @@ extern "C" { #ifndef _di_fl_string_dynamic_partial_rip_nulless_ f_status_t fl_string_dynamic_partial_rip_nulless(const f_string_static_t source, const f_string_range_t range, f_string_dynamic_t * const destination) { #ifndef _di_level_1_parameter_checking_ - if (source.used <= range.start) return F_status_set_error(F_parameter); - if (source.used <= range.stop) return F_status_set_error(F_parameter); if (!destination) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ -- 1.8.3.1