Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Strip unicode (why was this tricky in php?)
#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;
}
#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