Last active
May 5, 2021 22:25
-
-
Save louisswarren/657e1f1b224ca0767c063c973e7a5172 to your computer and use it in GitHub Desktop.
Strip unicode (why was this tricky in php?)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUF_SZ (1 << 16) | |
size_t | |
find_unicode(char *p, size_t n) | |
{ | |
size_t i; | |
for (i = 0; i < n; ++i) { | |
if (p[i] & 128) | |
return i; | |
} | |
return i; | |
} | |
int | |
main(void) | |
{ | |
char buf[BUF_SZ]; | |
size_t len_in, len_out; | |
do { | |
len_in = fread(buf, 1, BUF_SZ, stdin); | |
len_out = find_unicode(buf, len_in); | |
fwrite(buf, 1, len_out, stdout); | |
} while (len_out == BUF_SZ); | |
return len_in != len_out; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUF_SZ (1 << 16) | |
size_t | |
strip_unicode(char *p, size_t n) | |
{ | |
size_t i, j; | |
for (i = 0, j = 0; j < n; ++j) { | |
p[i] = p[j]; | |
if (!(p[i] & 128)) | |
++i; | |
} | |
return i; | |
} | |
int | |
main(void) | |
{ | |
char buf[BUF_SZ]; | |
size_t len_in, len_out; | |
do { | |
len_in = fread(buf, 1, BUF_SZ, stdin); | |
len_out = strip_unicode(buf, len_in); | |
fwrite(buf, 1, len_out, stdout); | |
} while (len_in == BUF_SZ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment