Refactor hankaku.c

This commit is contained in:
kichikuou
2020-10-11 15:07:43 +09:00
parent ebe3762809
commit 500fffaee2
2 changed files with 43 additions and 30 deletions
+22 -30
View File
@@ -107,37 +107,29 @@ static const BYTE hankakutable[3][192] = {{
0, 0, 0, 0, 0, 0, 0, 0,
}};
static const BYTE kanatbl[][2] = {
0x81, 0x40, 0x81, 0x42, 0x81, 0x75, 0x81, 0x76,
0x81, 0x41, 0x81, 0x45, 0x82, 0xf0, 0x82, 0x9f,
0x82, 0xa1, 0x82, 0xa3, 0x82, 0xa5, 0x82, 0xa7,
0x82, 0xe1, 0x82, 0xe3, 0x82, 0xe5, 0x82, 0xc1,
0x81, 0x5b, 0x82, 0xa0, 0x82, 0xa2, 0x82, 0xa4,
0x82, 0xa6, 0x82, 0xa8, 0x82, 0xa9, 0x82, 0xab,
0x82, 0xad, 0x82, 0xaf, 0x82, 0xb1, 0x82, 0xb3,
0x82, 0xb5, 0x82, 0xb7, 0x82, 0xb9, 0x82, 0xbb,
0x82, 0xbd, 0x82, 0xbf, 0x82, 0xc2, 0x82, 0xc4,
0x82, 0xc6, 0x82, 0xc8, 0x82, 0xc9, 0x82, 0xca,
0x82, 0xcb, 0x82, 0xcc, 0x82, 0xcd, 0x82, 0xd0,
0x82, 0xd3, 0x82, 0xd6, 0x82, 0xd9, 0x82, 0xdc,
0x82, 0xdd, 0x82, 0xde, 0x82, 0xdf, 0x82, 0xe0,
0x82, 0xe2, 0x82, 0xe4, 0x82, 0xe6, 0x82, 0xe7,
0x82, 0xe8, 0x82, 0xe9, 0x82, 0xea, 0x82, 0xeb,
0x82, 0xed, 0x82, 0xf1, 0x81, 0x4a, 0x81, 0x4b
static const WORD kanatbl[] = {
0x8140, 0x8142, 0x8175, 0x8176, 0x8141, 0x8145, 0x82f0, 0x829f,
0x82a1, 0x82a3, 0x82a5, 0x82a7, 0x82e1, 0x82e3, 0x82e5, 0x82c1,
0x815b, 0x82a0, 0x82a2, 0x82a4, 0x82a6, 0x82a8, 0x82a9, 0x82ab,
0x82ad, 0x82af, 0x82b1, 0x82b3, 0x82b5, 0x82b7, 0x82b9, 0x82bb,
0x82bd, 0x82bf, 0x82c2, 0x82c4, 0x82c6, 0x82c8, 0x82c9, 0x82ca,
0x82cb, 0x82cc, 0x82cd, 0x82d0, 0x82d3, 0x82d6, 0x82d9, 0x82dc,
0x82dd, 0x82de, 0x82df, 0x82e0, 0x82e2, 0x82e4, 0x82e6, 0x82e7,
0x82e8, 0x82e9, 0x82ea, 0x82eb, 0x82ed, 0x82f1, 0x814a, 0x814b
};
static const char zenkaku_digits[][3] = {
0x82,0x4f,0x00, /* 0 */
0x82,0x50,0x00, /* 1 */
0x82,0x51,0x00, /* 2 */
0x82,0x52,0x00, /* 3 */
0x82,0x53,0x00, /* 4 */
0x82,0x54,0x00, /* 5 */
0x82,0x55,0x00, /* 6 */
0x82,0x56,0x00, /* 7 */
0x82,0x57,0x00, /* 8 */
0x82,0x58,0x00, /* 9 */
0x81,0x40,0x00, /* space */
{0x82, 0x4f, 0x00}, /* 0 */
{0x82, 0x50, 0x00}, /* 1 */
{0x82, 0x51, 0x00}, /* 2 */
{0x82, 0x52, 0x00}, /* 3 */
{0x82, 0x53, 0x00}, /* 4 */
{0x82, 0x54, 0x00}, /* 5 */
{0x82, 0x55, 0x00}, /* 6 */
{0x82, 0x56, 0x00}, /* 7 */
{0x82, 0x57, 0x00}, /* 8 */
{0x82, 0x58, 0x00}, /* 9 */
{0x81, 0x40, 0x00}, /* space */
};
static BYTE *zen2han_sjis(const BYTE *src) {
@@ -385,8 +377,8 @@ BYTE *han2zen(const BYTE *src, CharacterEncoding enc) {
} else if (c0 >= 0xe0) {
*dst++ = (char)c0; *dst++ = *src++;
} else if (c0 >= 0xa0) {
const BYTE *kindex = kanatbl[c0 - 0xa0];
*dst++ = *kindex; *dst++ = *(kindex+1);
const WORD kana = kanatbl[c0 - 0xa0];
*dst++ = kana >> 8; *dst++ = kana & 0xff;
} else {
*dst++ = (char)c0; *dst++ = *src++;
}
+21
View File
@@ -25,6 +25,11 @@ typedef struct {
const char *han;
} Zen2hanTestCase;
typedef struct {
const char *han;
const char *zen;
} Han2ZenTestCase;
static void zen2han_test(void) {
const Zen2hanTestCase testcases[] = {
{"", ""},
@@ -59,6 +64,21 @@ static void zen2han_test(void) {
ASSERT_STRCMP(zen2han(unconvertable, UTF8), unconvertable);
}
static void han2zen_test(void) {
const Han2ZenTestCase testcases[] = {
{"", ""},
{" ", " "},
{"ァアィイゥウェエォオカキクケコサシスセソタチッツテトナニヌネノハヒフヘホマミムメモャヤュユョヨラリルレロワヲン", "ぁあぃいぅうぇえぉおかきくけこさしすせそたちっつてとなにぬねのはひふへほまみむめもゃやゅゆょよらりるれろわをん"},
{"、。「」・゙゚", "、。「」・゛゜"},
{"0123456789", "0123456789"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{"abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
{NULL, NULL},
};
for (const Han2ZenTestCase *tc = testcases; tc->han; tc++)
ASSERT_STRCMP(sjis2utf(han2zen(utf2sjis(tc->han), SHIFT_JIS)), tc->zen);
}
static void format_number_test(void) {
char buf[256];
ASSERT_STRCMP(format_number(0, 0, buf), "0");
@@ -79,6 +99,7 @@ static void format_number_zenkaku_test(void) {
void hankaku_test(void) {
zen2han_test();
han2zen_test();
format_number_test();
format_number_zenkaku_test();
}