Improve UTF8 trimming algorithm

This commit is contained in:
Utkarsh Verma 2022-05-01 15:27:30 +05:30
parent 6cdd6f7ff2
commit 65bfd0eef5
No known key found for this signature in database
GPG Key ID: 817656CF818EFCCC
1 changed files with 11 additions and 5 deletions

14
main.c
View File

@ -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)
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