mirror of
https://github.com/mintycube/dwmblocks.git
synced 2024-10-22 14:05:47 +02:00
Improve UTF8 trimming algorithm
This commit is contained in:
parent
6cdd6f7ff2
commit
65bfd0eef5
16
main.c
16
main.c
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user