Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
43 lines (33 sloc)
656 Bytes
This file contains 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> | |
#include <ustring.h> | |
uint32_t read_rune(FILE *); | |
int main(int argc, char **argv) { | |
uint32_t r; | |
for (;;) { | |
r = read_rune(stdin); | |
if (r == 0) { | |
break; | |
} | |
printf("U+%04x\n", r); | |
} | |
return 0; | |
} | |
uint32_t read_rune(FILE *in) { | |
uint8_t buf[4]; | |
size_t len, rlen; | |
int c; | |
c = fgetc(in); | |
if (c == EOF) { | |
return 0; | |
} | |
len = utf8_len(c); | |
if (len < 2) { | |
return (uint32_t)c; | |
} | |
buf[0] = c; | |
rlen = fread(&buf[1], 1, len, in); | |
if (rlen < len) { | |
return UNICODE_REPLACEMENT; | |
} | |
return utf8_decode(buf, NULL); | |
} |