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

16
main.c
View File

@ -115,10 +115,15 @@ void updateBlock(int i) {
// Trim UTF-8 string to desired length // Trim UTF-8 string to desired length
int count = 0, j = 0; int count = 0, j = 0;
while (buffer[j] != '\n' && count <= CMDLENGTH) { while (buffer[j] != '\n' && count < CMDLENGTH) {
// Increment count for non-continuation bytes count++;
if ((buffer[j++] & 0xc0) != 0x80)
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 // Cache last character and replace it with a trailing space
@ -126,7 +131,8 @@ void updateBlock(int i) {
buffer[j] = ' '; buffer[j] = ' ';
// Trim trailing spaces // Trim trailing spaces
while (j >= 0 && buffer[j] == ' ') j--; while (j >= 0 && buffer[j] == ' ')
j--;
buffer[j + 1] = 0; buffer[j + 1] = 0;
// Clear the pipe // Clear the pipe