From: Kevin Day Date: Sat, 25 Apr 2020 17:23:20 +0000 (-0500) Subject: Bugfix: --last does not count correctly when 2-byte or more characters exist X-Git-Tag: 0.5.0~334 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=c9a69946e9d3425c98d73eee275ca98dd2809e9e;p=fll Bugfix: --last does not count correctly when 2-byte or more characters exist The last parameter does not take into consideration UTF-8 widths. Redesign counting to accommodate the 2-byte, 3-byte, and 4-byte character widths. --- diff --git a/level_3/byte_dump/c/byte_dump.c b/level_3/byte_dump/c/byte_dump.c index ac446a1..65a723e 100644 --- a/level_3/byte_dump/c/byte_dump.c +++ b/level_3/byte_dump/c/byte_dump.c @@ -58,6 +58,12 @@ extern "C" { printf("%c%c", f_string_eol, f_string_eol); + printf(" When "); + fl_color_print(f_standard_output, data.context.notable, data.context.reset, "--%s", byte_dump_long_last); + printf(" is used, any UTF-8 sequences will still be printed in full should any part is found within the requested range."); + + printf("%c%c", f_string_eol, f_string_eol); + return f_none; } #endif // _di_byte_dump_print_help_ diff --git a/level_3/byte_dump/c/private-byte_dump.c b/level_3/byte_dump/c/private-byte_dump.c index 3f30db1..3b5eb04 100644 --- a/level_3/byte_dump/c/private-byte_dump.c +++ b/level_3/byte_dump/c/private-byte_dump.c @@ -77,10 +77,6 @@ extern "C" { } // Process the UTF-8 character. else if (width_utf > 1) { - position++; - - if (data.last > 0 && position > data.last) break; - continue; } } @@ -103,13 +99,7 @@ extern "C" { // UTF-8 character fragments must have a width of 1 (and ASCII characters can only be the first character in a sequence). if (width_current == 1) { // Grab the next UTF-8 character fragment if the entire sequence is not collected yet. - if (width_count < width_utf) { - position++; - - if (data.last > 0 && position > data.last) break; - - continue; - } + if (width_count < width_utf) continue; } else { found_invalid_utf = f_true; @@ -145,12 +135,20 @@ extern "C" { } } } + + if (data.last) { + position += width_utf; + + if (position >= data.last) break; + } } + else if (data.last) { + position++; - width_utf = -1; - position++; + if (position >= data.last) break; + } - if (data.last > 0 && position > data.last) break; + width_utf = -1; } // while // Print placeholders to fill out the remaining line and then optionally print the text block.