WIP Unicode

This commit is contained in:
Nishi 2026-06-19 04:55:57 +09:00
commit f0700d97a6
6 changed files with 30126 additions and 7 deletions

View file

@ -55,8 +55,40 @@ error(char* s) {
exit(1);
}
static const char utf8_bytes[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static int
readchar() {
#ifdef UTF8
int c[4];
int i = 0, j;
int n = 0;
do {
if((c[i++] = getch()) == EOF) return EOF;
} while(i < utf8_bytes[c[0]]);
if(i == 1) {
n = c[0];
} else {
n |= c[0] & (0xff >> (8 - i));
for(j = 1; j < i; j++) {
n = n << 6;
n |= c[j] & 63;
}
}
return n;
#else
int c1;
int c2;
@ -82,6 +114,7 @@ readchar() {
c1 = ((c1 & 0xffff) << 8) + (c2 & 0xff);
}
return c1;
#endif
}
static int
@ -114,10 +147,36 @@ retry:
}
for(i = 0; !Isblank(c);) {
int j = 0, k;
if(c == '\n' || c == ':')
break;
if(Isillegal(c))
error(ILLEGALFORMAT);
#ifdef UTF8
printf("%x\n", c);
if(c >= 0x10000) {
hinsi[i++] = ((c >> (6 * 3)) & 7) | 0xf0;
j = 3;
} else if(c >= 0x800) {
hinsi[i++] = ((c >> (6 * 2)) & 15) | 0xe0;
j = 2;
} else if(c >= 0x80) {
hinsi[i++] = ((c >> (6 * 1)) & 31) | 0xc0;
j = 1;
} else {
hinsi[i++] = c;
}
for(k = 0; k < j; k++) {
if(i >= 126) error(TOOLONGHINSI);
hinsi[i++] = ((c >> (6 * (j - k - 1))) & 63) | 0x80;
}
#else
if(c > 0xffffff) {
if(i >= 126)
error(TOOLONGHINSI);
@ -140,10 +199,13 @@ retry:
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 8) & 0xff);
}
#endif
if(i >= 127)
error(TOOLONGHINSI);
#ifndef UTF8
hinsi[i++] = (c & 0xff);
c = readchar();
#endif
c = readchar();
}
if(i == 0)
@ -153,18 +215,39 @@ retry:
hinsi[i - 1] = 0;
i = cnvhinsi(hinsi + 1);
if(i > 0) {
fprintf(stderr, "\311\312\273\354 \"%s\" \244\313\263\347\270\314\244\254\244\304\244\244\244\306\244\244\244\353\n",
hinsi + 1);
fprintf(stderr,
#ifdef UTF8
"品詞 \"%s\" に括弧がついている\n"
#else
"\311\312\273\354 \"%s\" \244\313\263\347\270\314\244\254\244\304\244\244\244\306\244\244\244\353\n"
#endif
,
hinsi +
1);
} else if(!i) {
fprintf(stderr, "\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n",
hinsi + 1);
fprintf(stderr,
#ifdef UTF8
"\"%s\" がコード化できません\n"
#else
"\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n"
#endif
,
hinsi +
1);
goto retry;
}
} else {
hinsi[i] = 0;
i = cnvhinsi(hinsi);
if(!i) {
fprintf(stderr, "\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n", hinsi);
fprintf(stderr,
#ifdef UTF8
"\"%s\" がコード化できません\n"
#else
"\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n"
#endif
,
hinsi);
goto retry;
}
}