more work
This commit is contained in:
parent
0697a13fcd
commit
039a17f454
@ -4,6 +4,8 @@ utf8_t utf8(utf32_t c){
|
|||||||
unsigned char n;
|
unsigned char n;
|
||||||
utf8_t r;
|
utf8_t r;
|
||||||
|
|
||||||
|
if(c > UTF8_MAX)
|
||||||
|
c = 0;
|
||||||
r = 0;
|
r = 0;
|
||||||
switch(n = (c < 0x10000) + (c < 0x0800) + (c < 0x0080)){
|
switch(n = (c < 0x10000) + (c < 0x0800) + (c < 0x0080)){
|
||||||
case 0: r = 0xF0 + ((c >> 18) & 0x07); /* 11110xxx */
|
case 0: r = 0xF0 + ((c >> 18) & 0x07); /* 11110xxx */
|
||||||
|
@ -10,5 +10,7 @@
|
|||||||
* strings; >=32b value */
|
* strings; >=32b value */
|
||||||
#define utf8_t unicode_codepoint_t
|
#define utf8_t unicode_codepoint_t
|
||||||
|
|
||||||
|
#define UTF8_MAX 0x10FFFF
|
||||||
|
|
||||||
/* encode UTF-32 value into UTF-8 */
|
/* encode UTF-32 value into UTF-8 */
|
||||||
utf8_t utf8(utf32_t c);
|
utf8_t utf8(utf32_t c);
|
||||||
|
@ -16,44 +16,39 @@ int main(int argc, char *argv[]){
|
|||||||
utf32_t codepoint;
|
utf32_t codepoint;
|
||||||
utf8_t encoded;
|
utf8_t encoded;
|
||||||
|
|
||||||
c = '\0';
|
l = 0;
|
||||||
|
|
||||||
|
init: codepoint = 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
l = 1;
|
++l;
|
||||||
while(c != EOF){
|
n = NULL;
|
||||||
for(
|
while((c = getc(stdin)) != EOF){
|
||||||
i = 0, n = NULL, codepoint = 0;
|
if(c == '\n'){
|
||||||
(c = getc(stdin)) != '\n'
|
if(i < 2 && i > 0) /* empty lines are fine */
|
||||||
&& c != EOF
|
|
||||||
&& i < 10;
|
|
||||||
++i
|
|
||||||
){
|
|
||||||
if(
|
|
||||||
(i == 0 && c != 'U')
|
|
||||||
|| (i == 1 && c != '+')
|
|
||||||
|| i > 10
|
|
||||||
|| (i > 1 && (n = strchr(hex, c))
|
|
||||||
== NULL)
|
|
||||||
){
|
|
||||||
fprintf(stderr, "%s: %s: Syntax error.\n",
|
fprintf(stderr, "%s: %s: Syntax error.\n",
|
||||||
argv[0], l);
|
argv[0], l);
|
||||||
SKIPLINE;
|
else if(i >= 2){
|
||||||
i = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(n != NULL)
|
|
||||||
codepoint = (codepoint << 4) + (n - hex) % 16;
|
|
||||||
}
|
|
||||||
if(i < 3){
|
|
||||||
if(c != '\n' && c != EOF)
|
|
||||||
SKIPLINE;
|
|
||||||
if(c == EOF)
|
|
||||||
return 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
encoded = utf8(codepoint);
|
encoded = utf8(codepoint);
|
||||||
for(i = 3; i >= 0; --i)
|
for(i = 3; i >= 0; --i)
|
||||||
if((encoded >> 8 * i) > 0 || i == 0)
|
if((encoded >> 8 * i) > 0 || i == 0)
|
||||||
putc(encoded >> 8 * i, stdout);
|
putc(encoded >> 8 * i, stdout);
|
||||||
|
}
|
||||||
|
goto init;
|
||||||
|
}
|
||||||
|
if(
|
||||||
|
(i == 0 && c != 'U')
|
||||||
|
|| (i == 1 && c != '+')
|
||||||
|
|| i > 8 /* strlen("U+10FFFF") */
|
||||||
|
|| (i > 1 && ((n = strchr(hex, c)) == NULL))
|
||||||
|
){
|
||||||
|
fprintf(stderr, "%s: %s: Syntax error.\n",
|
||||||
|
argv[0], l);
|
||||||
|
while((c = getc(stdin)) != '\n' && c != EOF);
|
||||||
++l;
|
++l;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(n != NULL)
|
||||||
|
codepoint = (codepoint << 4) + (n - hex) % 16;
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user