From 34ed3b740987d3c17cde5a1eb3ff6f626f029a1d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 21 Apr 2020 22:50:15 -0500 Subject: [PATCH] Bugfix: correctly calculate end-- with UTF-8 and use correct function name When walking back from the end of the string and handling possible UTF-8 characters, the byte width needs to be properly handled. The functions in these projects are fl_ and not f_. Includes minor code cleanup. --- level_1/fl_string/c/private-string.c | 17 +++++++++-------- level_1/fl_string/c/string.c | 12 ++++++------ level_1/fl_string/c/string.h | 12 ++++++------ level_1/fl_utf/c/private-utf.c | 8 ++------ 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/level_1/fl_string/c/private-string.c b/level_1/fl_string/c/private-string.c index c151feb..f46b5b7 100644 --- a/level_1/fl_string/c/private-string.c +++ b/level_1/fl_string/c/private-string.c @@ -186,9 +186,7 @@ extern "C" { f_macro_string_dynamic_resize(status, (*result), size); } - if (f_status_is_error(status)) { - return status; - } + if (f_status_is_error(status)) return status; memcpy(result->string, string + start, size); result->used = size; @@ -213,9 +211,7 @@ extern "C" { f_macro_string_dynamic_resize(status, (*result), size); } - if (f_status_is_error(status)) { - return status; - } + if (f_status_is_error(status)) return status; f_string_length begin = start; f_string_length end = stop; @@ -225,7 +221,7 @@ extern "C" { // skip past leading whitespace. for (; begin <= end; begin += width) { // skip past NULL. - while (begin < size && string[begin] == f_string_eos) begin++; + while (begin < end && string[begin] == f_string_eos) begin++; if (begin > end) break; status = f_utf_is_whitespace(string + begin, (end - begin) + 1); @@ -239,14 +235,17 @@ extern "C" { if (status == f_false) break; - width = f_macro_utf_byte_width_is(string[begin]); + width = f_macro_utf_byte_width(string[begin]); } // for for (; end > begin; end--) { // skip past NULL. + while (end > begin && string[end] == f_string_eos) end--; if (string[end] == f_string_eos) continue; if (end == begin) break; + // each UTF-8 character of width 8 is an incomplete part. + // go left until either width is 0 (ascii, or > 1) to determine the character. for (;;) { width = f_macro_utf_byte_width_is(string[end]); if (width == 1) { @@ -269,6 +268,8 @@ extern "C" { } if (status == f_false) break; + + width = f_macro_utf_byte_width(string[end]); } // for memcpy(result->string, string + begin, (end - begin) + 1); diff --git a/level_1/fl_string/c/string.c b/level_1/fl_string/c/string.c index 514c95c..4979046 100644 --- a/level_1/fl_string/c/string.c +++ b/level_1/fl_string/c/string.c @@ -27,8 +27,8 @@ extern "C" { } #endif // _di_fl_string_compare_trim_ -#ifndef _di_f_string_dynamic_compare_ - f_return_status f_string_dynamic_compare(const f_string_dynamic string1, const f_string_dynamic string2) { +#ifndef _di_fl_string_dynamic_compare_ + f_return_status fl_string_dynamic_compare(const f_string_dynamic string1, const f_string_dynamic string2) { #ifndef _di_level_1_parameter_checking_ if (string1.used <= 0) return f_status_set_error(f_invalid_parameter); if (string2.used <= 0) return f_status_set_error(f_invalid_parameter); @@ -36,10 +36,10 @@ extern "C" { return private_fl_string_compare(string1.string, string2.string, 0, 0, string1.used, string2.used); } -#endif // _di_f_string_dynamic_compare_ +#endif // _di_fl_string_dynamic_compare_ -#ifndef _di_f_string_dynamic_compare_trim_ - f_return_status f_string_dynamic_compare_trim(const f_string_dynamic string1, const f_string_dynamic string2) { +#ifndef _di_fl_string_dynamic_compare_trim_ + f_return_status fl_string_dynamic_compare_trim(const f_string_dynamic string1, const f_string_dynamic string2) { #ifndef _di_level_1_parameter_checking_ if (string1.used <= 0) return f_status_set_error(f_invalid_parameter); if (string2.used <= 0) return f_status_set_error(f_invalid_parameter); @@ -47,7 +47,7 @@ extern "C" { return private_fl_string_compare_trim(string1.string, string2.string, 0, 0, string1.used, string2.used); } -#endif // _di_f_string_dynamic_compare_trim_ +#endif // _di_fl_string_dynamic_compare_trim_ #ifndef _di_fl_string_dynamic_partial_compare_ f_return_status fl_string_dynamic_partial_compare(const f_string_dynamic string1, const f_string_dynamic string2, const f_string_location offset1, const f_string_location offset2) { diff --git a/level_1/fl_string/c/string.h b/level_1/fl_string/c/string.h index c44da94..81434fe 100644 --- a/level_1/fl_string/c/string.h +++ b/level_1/fl_string/c/string.h @@ -114,9 +114,9 @@ extern "C" { * @see fl_string_dynamic_partial_compare() * @see fl_string_dynamic_partial_compare_trim() */ -#ifndef _di_f_string_dynamic_compare_ - extern f_return_status f_string_dynamic_compare(const f_string_dynamic string1, const f_string_dynamic string2); -#endif // _di_f_string_dynamic_compare_ +#ifndef _di_fl_string_dynamic_compare_ + extern f_return_status fl_string_dynamic_compare(const f_string_dynamic string1, const f_string_dynamic string2); +#endif // _di_fl_string_dynamic_compare_ /** * Compare two strings, similar to strncmp(). @@ -140,9 +140,9 @@ extern "C" { * @see fl_string_dynamic_partial_compare() * @see fl_string_dynamic_partial_compare_trim() */ -#ifndef _di_f_string_dynamic_compare_trim_ - extern f_return_status f_string_dynamic_compare_trim(const f_string_dynamic string1, const f_string_dynamic string2); -#endif // _di_f_string_dynamic_compare_trim_ +#ifndef _di_fl_string_dynamic_compare_trim_ + extern f_return_status fl_string_dynamic_compare_trim(const f_string_dynamic string1, const f_string_dynamic string2); +#endif // _di_fl_string_dynamic_compare_trim_ /** * Compare two strings, similar to strncmp(), but restricted to the given ranges. diff --git a/level_1/fl_utf/c/private-utf.c b/level_1/fl_utf/c/private-utf.c index 51779ea..68d79ca 100644 --- a/level_1/fl_utf/c/private-utf.c +++ b/level_1/fl_utf/c/private-utf.c @@ -166,9 +166,7 @@ extern "C" { f_macro_string_dynamic_resize(status, (*result), size); } - if (f_status_is_error(status)) { - return status; - } + if (f_status_is_error(status)) return status; memcpy(result->string, string + start, size); result->used = size; @@ -193,9 +191,7 @@ extern "C" { f_macro_string_dynamic_resize(status, (*result), size); } - if (f_status_is_error(status)) { - return status; - } + if (f_status_is_error(status)) return status; f_utf_string_length begin = start; f_utf_string_length end = stop; -- 1.8.3.1