Skip to content

Commit aa2662d

Browse files
committed
Simplify offset calculations of tbl0208
1 parent 75a8c93 commit aa2662d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

enc/trans/iso2022.trans

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -436,15 +436,24 @@ rb_cp50221_encoder = {
436436

437437
/* JIS0201 to JIS0208 conversion table */
438438
enum {tbl0208_num = 0xDF - 0xA1 + 1};
439-
static const char tbl0208[2 * tbl0208_num] =
440-
"\x21\x23\x21\x56\x21\x57\x21\x22\x21\x26\x25\x72\x25\x21\x25\x23" \
441-
"\x25\x25\x25\x27\x25\x29\x25\x63\x25\x65\x25\x67\x25\x43\x21\x3C" \
442-
"\x25\x22\x25\x24\x25\x26\x25\x28\x25\x2A\x25\x2B\x25\x2D\x25\x2F" \
443-
"\x25\x31\x25\x33\x25\x35\x25\x37\x25\x39\x25\x3B\x25\x3D\x25\x3F" \
444-
"\x25\x41\x25\x44\x25\x46\x25\x48\x25\x4A\x25\x4B\x25\x4C\x25\x4D" \
445-
"\x25\x4E\x25\x4F\x25\x52\x25\x55\x25\x58\x25\x5B\x25\x5E\x25\x5F" \
446-
"\x25\x60\x25\x61\x25\x62\x25\x64\x25\x66\x25\x68\x25\x69\x25\x6A" \
447-
"\x25\x6B\x25\x6C\x25\x6D\x25\x6F\x25\x73\x21\x2B\x21\x2C";
439+
static const char tbl0208[tbl0208_num][2] = {
440+
"\x21\x23", "\x21\x56", "\x21\x57", "\x21\x22",
441+
"\x21\x26", "\x25\x72", "\x25\x21", "\x25\x23",
442+
"\x25\x25", "\x25\x27", "\x25\x29", "\x25\x63",
443+
"\x25\x65", "\x25\x67", "\x25\x43", "\x21\x3C",
444+
"\x25\x22", "\x25\x24", "\x25\x26", "\x25\x28",
445+
"\x25\x2A", "\x25\x2B", "\x25\x2D", "\x25\x2F",
446+
"\x25\x31", "\x25\x33", "\x25\x35", "\x25\x37",
447+
"\x25\x39", "\x25\x3B", "\x25\x3D", "\x25\x3F",
448+
"\x25\x41", "\x25\x44", "\x25\x46", "\x25\x48",
449+
"\x25\x4A", "\x25\x4B", "\x25\x4C", "\x25\x4D",
450+
"\x25\x4E", "\x25\x4F", "\x25\x52", "\x25\x55",
451+
"\x25\x58", "\x25\x5B", "\x25\x5E", "\x25\x5F",
452+
"\x25\x60", "\x25\x61", "\x25\x62", "\x25\x64",
453+
"\x25\x66", "\x25\x68", "\x25\x69", "\x25\x6A",
454+
"\x25\x6B", "\x25\x6C", "\x25\x6D", "\x25\x6F",
455+
"\x25\x73", "\x21\x2B", "\x21\x2C"
456+
};
448457

449458
static ssize_t
450459
fun_so_cp50220_encoder(void *statep, const unsigned char *s, size_t l,
@@ -455,17 +464,19 @@ fun_so_cp50220_encoder(void *statep, const unsigned char *s, size_t l,
455464

456465
if (sp[0] == G0_JISX0201_KATAKANA && sp[2]) {
457466
int c = sp[2] & 0x7F;
458-
const char *p = tbl0208 + (c - 0x21) * 2;
467+
const char *p = tbl0208[c - 0x21];
459468
sp[2] = 0;
460469
o = iso2022jp_put_state(sp, o, sp[1], G0_JISX0208_1983);
461470
sp[0] = G0_JISX0208_1983;
462471
*o++ = *p++;
463472
if (l == 2 && s[0] == 0x8E) {
464473
if (s[1] == 0xDE) {
474+
/* VOICED SOUND MARK */
465475
*o++ = *p + 1;
466476
return o - output0;
467477
}
468478
else if (s[1] == 0xDF && (0x4A <= c && c <= 0x4E)) {
479+
/* SEMI-VOICED SOUND MARK */
469480
*o++ = *p + 2;
470481
return o - output0;
471482
}
@@ -477,7 +488,8 @@ fun_so_cp50220_encoder(void *statep, const unsigned char *s, size_t l,
477488
if ((0xA1 <= s[1] && s[1] <= 0xB5) ||
478489
(0xC5 <= s[1] && s[1] <= 0xC9) ||
479490
(0xCF <= s[1] && s[1] <= 0xDF)) {
480-
const char *p = tbl0208 + (s[1] - 0xA1) * 2;
491+
/* May not be followed by a sound mark */
492+
const char *p = tbl0208[s[1] - 0xA1];
481493
o = iso2022jp_put_state(sp, o, *sp, G0_JISX0208_1983);
482494
*o++ = *p++;
483495
*o++ = *p;
@@ -491,6 +503,7 @@ fun_so_cp50220_encoder(void *statep, const unsigned char *s, size_t l,
491503
return o - output0;
492504
}
493505

506+
/* Katakana that may be followed by a sound mark */
494507
sp[2] = s[1];
495508
sp[1] = sp[0];
496509
sp[0] = G0_JISX0201_KATAKANA;
@@ -512,7 +525,7 @@ finish_cp50220_encoder(void *statep, unsigned char *o, size_t osize)
512525

513526
if (sp[0] == G0_JISX0201_KATAKANA && sp[2]) {
514527
int c = sp[2] & 0x7F;
515-
const char *p = tbl0208 + (c - 0x21) * 2;
528+
const char *p = tbl0208[c - 0x21];
516529
o = iso2022jp_put_state(sp, o, sp[1], G0_JISX0208_1983);
517530
sp[0] = G0_JISX0208_1983;
518531
*o++ = *p++;
@@ -549,4 +562,3 @@ TRANS_INIT(iso2022)
549562
rb_register_transcoder(&rb_cp50220_encoder);
550563
rb_register_transcoder(&rb_cp50221_encoder);
551564
}
552-

0 commit comments

Comments
 (0)