Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active May 5, 2021 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisswarren/657e1f1b224ca0767c063c973e7a5172 to your computer and use it in GitHub Desktop.
Save louisswarren/657e1f1b224ca0767c063c973e7a5172 to your computer and use it in GitHub Desktop.
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