Reorganize code and remove hack for empty block outputs

This commit is contained in:
Utkarsh Verma
2023-03-25 21:38:05 +05:30
parent f6a5fa6480
commit 8dd9bc6a7d
17 changed files with 531 additions and 362 deletions

39
src/util.c Normal file
View File

@@ -0,0 +1,39 @@
#include "util.h"
int gcd(int a, int b) {
int temp;
while (b > 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
void closePipe(int pipe[2]) {
close(pipe[0]);
close(pipe[1]);
}
void trimUTF8(char* buffer, unsigned int size) {
int length = (size - 1) / 4;
int count = 0, j = 0;
char ch = buffer[j];
while (ch != '\0' && ch != '\n' && count < length) {
// Skip continuation bytes, if any
int skip = 1;
while ((ch & 0xc0) > 0x80) {
ch <<= 1;
skip++;
}
j += skip;
ch = buffer[j];
count++;
}
// Trim trailing newline and spaces
buffer[j] = ' ';
while (j >= 0 && buffer[j] == ' ') j--;
buffer[j + 1] = '\0';
}