From 0fd42f80ef1a84cc6a67453187fb8867fe1a5e3c Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 19 Apr 2020 23:53:54 -0500 Subject: [PATCH] Update: the print functions should increment by utf-8 char width and not 1 The UTF-8 character may be of a width greater than 1. The loop must increment by this width and not the literal 1. --- level_1/fl_print/c/print.c | 57 +++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/level_1/fl_print/c/print.c b/level_1/fl_print/c/print.c index b25777f..3c26540 100644 --- a/level_1/fl_print/c/print.c +++ b/level_1/fl_print/c/print.c @@ -15,7 +15,7 @@ extern "C" { f_status status = f_none; uint8_t width_max = 0; - for (; i < length; i++) { + for (; i < length; i += f_macro_utf_byte_width(string[i])) { width_max = (length - i) + 1; status = f_utf_is_whitespace(string + i, width_max); @@ -30,7 +30,7 @@ extern "C" { if (status == f_false) break; } // for - for (; i < length; i++) { + for (; i < length; i += f_macro_utf_byte_width(string[i])) { if (string[i] == f_string_eos) continue; width_max = (length - i) + 1; @@ -45,13 +45,13 @@ extern "C" { } if (status == f_true) { - f_string_length j = i + 1; + f_string_length j = i + f_macro_utf_byte_width(string[i]); if (j == length) { return f_none; } - for (; j < length; j++) { + for (; j < length; j += f_macro_utf_byte_width(string[j])) { width_max = (length - j) + 1; status = f_utf_is_whitespace(string + j, width_max); @@ -101,7 +101,7 @@ extern "C" { f_status status = f_none; uint8_t width_max = 0; - for (; i < buffer.used; i++) { + for (; i < buffer.used; i += f_macro_utf_byte_width(buffer.string[i])) { width_max = (buffer.used - i) + 1; status = f_utf_is_whitespace(buffer.string + i, width_max); @@ -116,7 +116,7 @@ extern "C" { if (status == f_false) break; } // for - for (; i < buffer.used; i++) { + for (; i < buffer.used; i += f_macro_utf_byte_width(buffer.string[i])) { if (buffer.string[i] == f_string_eos) continue; width_max = (buffer.used - i) + 1; @@ -131,13 +131,13 @@ extern "C" { } if (status == f_true) { - f_string_length j = i + 1; + f_string_length j = i + f_macro_utf_byte_width(buffer.string[i]); if (j == buffer.used) { return f_none; } - for (; j < buffer.used; j++) { + for (; j < buffer.used; j += f_macro_utf_byte_width(buffer.string[j])) { width_max = (buffer.used - j) + 1; status = f_utf_is_whitespace(buffer.string + j, width_max); @@ -189,9 +189,10 @@ extern "C" { register f_string_length i = location.start; f_status status = f_none; + uint8_t width_max = 0; - for (; i <= location.stop; i++) { + for (; i <= location.stop; i += f_macro_utf_byte_width(buffer.string[i])) { width_max = (location.stop - i) + 1; status = f_utf_is_whitespace(buffer.string + i, width_max); @@ -206,9 +207,13 @@ extern "C" { if (status == f_false) break; } // for - for (; i <= location.stop; i++) { - if (buffer.string[i] == f_string_eos) continue; + for (uint8_t width_i = f_macro_utf_byte_width(buffer.string[i]); i <= location.stop; i += width_i) { + if (buffer.string[i] == f_string_eos) { + width_i = 1; + continue; + } + width_i = f_macro_utf_byte_width(buffer.string[i]); width_max = (location.stop - i) + 1; status = f_utf_is_whitespace(buffer.string + i, width_max); @@ -221,13 +226,14 @@ extern "C" { } if (status == f_true) { - f_string_length j = i + 1; + f_string_length j = i + width_i; if (j == location.stop) { return f_none; } - for (; j <= location.stop; j++) { + for (uint8_t width_j = f_macro_utf_byte_width(buffer.string[j]); j <= location.stop; j += width_j) { + width_j = f_macro_utf_byte_width(buffer.string[j]); width_max = (location.stop - j) + 1; status = f_utf_is_whitespace(buffer.string + j, width_max); @@ -241,12 +247,19 @@ extern "C" { // all whitespaces found so far must be printed when a non-whitespace is found. if (status == f_false) { - for (; i <= j; i++) { - if (buffer.string[i] == f_string_eos) continue; - - if (fputc(buffer.string[i], output) == 0) { - return f_status_set_error(f_error_output); + for (; i <= j; i += width_i) { + if (buffer.string[i] == f_string_eos) { + width_i = 1; + continue; } + + width_i = f_macro_utf_byte_width(buffer.string[i]); + + for (uint8_t k = 0; k < width_i; k++) { + if (fputc(buffer.string[i + k], output) == 0) { + return f_status_set_error(f_error_output); + } + } // for } // for break; @@ -258,9 +271,11 @@ extern "C" { } } - if (fputc(buffer.string[i], output) == 0) { - return f_status_set_error(f_error_output); - } + for (uint8_t k = 0; k < width_i; k++) { + if (fputc(buffer.string[i + k], output) == 0) { + return f_status_set_error(f_error_output); + } + } // for } // for return f_none; -- 1.8.3.1