From 65bfd0eef541502e01e6b9501d841777ac2f6d19 Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Sun, 1 May 2022 15:27:30 +0530 Subject: [PATCH] Improve UTF8 trimming algorithm --- main.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index f70522b..f101146 100644 --- a/main.c +++ b/main.c @@ -115,10 +115,15 @@ void updateBlock(int i) { // Trim UTF-8 string to desired length int count = 0, j = 0; - while (buffer[j] != '\n' && count <= CMDLENGTH) { - // Increment count for non-continuation bytes - if ((buffer[j++] & 0xc0) != 0x80) - count++; + while (buffer[j] != '\n' && count < CMDLENGTH) { + count++; + + // Skip continuation bytes, if any. + char ch = buffer[j]; + int skip = 1; + while ((ch & 0xc0) > 0x80) + ch <<= 1, skip++; + j += skip; } // Cache last character and replace it with a trailing space @@ -126,7 +131,8 @@ void updateBlock(int i) { buffer[j] = ' '; // Trim trailing spaces - while (j >= 0 && buffer[j] == ' ') j--; + while (j >= 0 && buffer[j] == ' ') + j--; buffer[j + 1] = 0; // Clear the pipe