CJK codecs: less magic macros, require explicit semicolon

This commit is contained in:
Victor Stinner 2013-10-29 00:19:27 +01:00
parent 146a2ed0f2
commit 14c9fea60a
10 changed files with 233 additions and 190 deletions

View File

@ -24,22 +24,31 @@
*/ */
#define GBK_DECODE(dc1, dc2, writer) \ #define GBK_DECODE(dc1, dc2, writer) \
if ((dc1) == 0xa1 && (dc2) == 0xaa) OUTCHAR(0x2014); \ if ((dc1) == 0xa1 && (dc2) == 0xaa) { \
else if ((dc1) == 0xa8 && (dc2) == 0x44) OUTCHAR(0x2015); \ OUTCHAR(0x2014); \
else if ((dc1) == 0xa1 && (dc2) == 0xa4) OUTCHAR(0x00b7); \ } \
else if (TRYMAP_DEC(gb2312, decoded, dc1 ^ 0x80, dc2 ^ 0x80)) \ else if ((dc1) == 0xa8 && (dc2) == 0x44) { \
OUTCHAR(0x2015); \
} \
else if ((dc1) == 0xa1 && (dc2) == 0xa4) { \
OUTCHAR(0x00b7); \
} \
else if (TRYMAP_DEC(gb2312, decoded, dc1 ^ 0x80, dc2 ^ 0x80)) { \
OUTCHAR(decoded); \ OUTCHAR(decoded); \
else if (TRYMAP_DEC(gbkext, decoded, dc1, dc2)) \ } \
OUTCHAR(decoded); else if (TRYMAP_DEC(gbkext, decoded, dc1, dc2)) { \
OUTCHAR(decoded); \
}
#define GBK_ENCODE(code, assi) \ #define GBK_ENCODE(code, assi) \
if ((code) == 0x2014) \ if ((code) == 0x2014) { \
(assi) = 0xa1aa; \ (assi) = 0xa1aa; \
else if ((code) == 0x2015) \ } else if ((code) == 0x2015) { \
(assi) = 0xa844; \ (assi) = 0xa844; \
else if ((code) == 0x00b7) \ } else if ((code) == 0x00b7) { \
(assi) = 0xa1a4; \ (assi) = 0xa1a4; \
else if ((code) != 0x30fb && TRYMAP_ENC(gbcommon, assi, code)) } else if ((code) != 0x30fb && TRYMAP_ENC(gbcommon, assi, code)) { \
}
/* /*
* GB2312 codec * GB2312 codec
@ -52,7 +61,7 @@ ENCODER(gb2312)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -60,7 +69,7 @@ ENCODER(gb2312)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(gbcommon, code, c)) if (TRYMAP_ENC(gbcommon, code, c))
; ;
else else
@ -69,8 +78,8 @@ ENCODER(gb2312)
if (code & 0x8000) /* MSB set: GBK */ if (code & 0x8000) /* MSB set: GBK */
return 1; return 1;
OUTBYTE1((code >> 8) | 0x80) OUTBYTE1((code >> 8) | 0x80);
OUTBYTE2((code & 0xFF) | 0x80) OUTBYTE2((code & 0xFF) | 0x80);
NEXT(1, 2); NEXT(1, 2);
} }
@ -113,7 +122,7 @@ ENCODER(gbk)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -121,18 +130,17 @@ ENCODER(gbk)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
GBK_ENCODE(c, code) GBK_ENCODE(c, code)
;
else else
return 1; return 1;
OUTBYTE1((code >> 8) | 0x80) OUTBYTE1((code >> 8) | 0x80);
if (code & 0x8000) if (code & 0x8000)
OUTBYTE2((code & 0xFF)) /* MSB set: GBK */ OUTBYTE2((code & 0xFF)); /* MSB set: GBK */
else else
OUTBYTE2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */ OUTBYTE2((code & 0xFF) | 0x80); /* MSB unset: GB2312 */
NEXT(1, 2); NEXT(1, 2);
} }
@ -175,7 +183,7 @@ ENCODER(gb18030)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1(c) WRITEBYTE1(c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -184,30 +192,29 @@ ENCODER(gb18030)
Py_UCS4 tc = c - 0x10000; Py_UCS4 tc = c - 0x10000;
assert (c <= 0x10FFFF); assert (c <= 0x10FFFF);
REQUIRE_OUTBUF(4) REQUIRE_OUTBUF(4);
OUTBYTE4((unsigned char)(tc % 10) + 0x30) OUTBYTE4((unsigned char)(tc % 10) + 0x30);
tc /= 10; tc /= 10;
OUTBYTE3((unsigned char)(tc % 126) + 0x81) OUTBYTE3((unsigned char)(tc % 126) + 0x81);
tc /= 126; tc /= 126;
OUTBYTE2((unsigned char)(tc % 10) + 0x30) OUTBYTE2((unsigned char)(tc % 10) + 0x30);
tc /= 10; tc /= 10;
OUTBYTE1((unsigned char)(tc + 0x90)) OUTBYTE1((unsigned char)(tc + 0x90));
NEXT(1, 4); NEXT(1, 4);
continue; continue;
} }
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
GBK_ENCODE(c, code) GBK_ENCODE(c, code)
;
else if (TRYMAP_ENC(gb18030ext, code, c)) else if (TRYMAP_ENC(gb18030ext, code, c))
; ;
else { else {
const struct _gb18030_to_unibmp_ranges *utrrange; const struct _gb18030_to_unibmp_ranges *utrrange;
REQUIRE_OUTBUF(4) REQUIRE_OUTBUF(4);
for (utrrange = gb18030_to_unibmp_ranges; for (utrrange = gb18030_to_unibmp_ranges;
utrrange->first != 0; utrrange->first != 0;
@ -219,13 +226,13 @@ ENCODER(gb18030)
tc = c - utrrange->first + tc = c - utrrange->first +
utrrange->base; utrrange->base;
OUTBYTE4((unsigned char)(tc % 10) + 0x30) OUTBYTE4((unsigned char)(tc % 10) + 0x30);
tc /= 10; tc /= 10;
OUTBYTE3((unsigned char)(tc % 126) + 0x81) OUTBYTE3((unsigned char)(tc % 126) + 0x81);
tc /= 126; tc /= 126;
OUTBYTE2((unsigned char)(tc % 10) + 0x30) OUTBYTE2((unsigned char)(tc % 10) + 0x30);
tc /= 10; tc /= 10;
OUTBYTE1((unsigned char)tc + 0x81) OUTBYTE1((unsigned char)tc + 0x81);
NEXT(1, 4); NEXT(1, 4);
break; break;
@ -236,11 +243,11 @@ ENCODER(gb18030)
continue; continue;
} }
OUTBYTE1((code >> 8) | 0x80) OUTBYTE1((code >> 8) | 0x80);
if (code & 0x8000) if (code & 0x8000)
OUTBYTE2((code & 0xFF)) /* MSB set: GBK or GB18030ext */ OUTBYTE2((code & 0xFF)); /* MSB set: GBK or GB18030ext */
else else
OUTBYTE2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */ OUTBYTE2((code & 0xFF) | 0x80); /* MSB unset: GB2312 */
NEXT(1, 2); NEXT(1, 2);
} }
@ -326,7 +333,7 @@ ENCODER_INIT(hz)
ENCODER_RESET(hz) ENCODER_RESET(hz)
{ {
if (state->i != 0) { if (state->i != 0) {
WRITEBYTE2('~', '}') WRITEBYTE2('~', '}');
state->i = 0; state->i = 0;
NEXT_OUT(2); NEXT_OUT(2);
} }
@ -341,11 +348,11 @@ ENCODER(hz)
if (c < 0x80) { if (c < 0x80) {
if (state->i == 0) { if (state->i == 0) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
} }
else { else {
WRITEBYTE3('~', '}', (unsigned char)c) WRITEBYTE3('~', '}', (unsigned char)c);
NEXT(1, 3); NEXT(1, 3);
state->i = 0; state->i = 0;
} }
@ -364,12 +371,12 @@ ENCODER(hz)
return 1; return 1;
if (state->i == 0) { if (state->i == 0) {
WRITEBYTE4('~', '{', code >> 8, code & 0xff) WRITEBYTE4('~', '{', code >> 8, code & 0xff);
NEXT(1, 4); NEXT(1, 4);
state->i = 1; state->i = 1;
} }
else { else {
WRITEBYTE2(code >> 8, code & 0xff) WRITEBYTE2(code >> 8, code & 0xff);
NEXT(1, 2); NEXT(1, 2);
} }
} }

View File

@ -44,14 +44,14 @@ ENCODER(big5hkscs)
Py_ssize_t insize; Py_ssize_t insize;
if (c < 0x80) { if (c < 0x80) {
REQUIRE_OUTBUF(1) REQUIRE_OUTBUF(1);
**outbuf = (unsigned char)c; **outbuf = (unsigned char)c;
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
insize = 1; insize = 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (c < 0x10000) { if (c < 0x10000) {
if (TRYMAP_ENC(big5hkscs_bmp, code, c)) { if (TRYMAP_ENC(big5hkscs_bmp, code, c)) {
@ -97,8 +97,8 @@ ENCODER(big5hkscs)
else else
return insize; return insize;
OUTBYTE1(code >> 8) OUTBYTE1(code >> 8);
OUTBYTE2(code & 0xFF) OUTBYTE2(code & 0xFF);
NEXT(insize, 2); NEXT(insize, 2);
} }

View File

@ -141,12 +141,12 @@ ENCODER_INIT(iso2022)
ENCODER_RESET(iso2022) ENCODER_RESET(iso2022)
{ {
if (STATE_GETFLAG(F_SHIFTED)) { if (STATE_GETFLAG(F_SHIFTED)) {
WRITEBYTE1(SI) WRITEBYTE1(SI);
NEXT_OUT(1); NEXT_OUT(1);
STATE_CLEARFLAG(F_SHIFTED) STATE_CLEARFLAG(F_SHIFTED)
} }
if (STATE_G0 != CHARSET_ASCII) { if (STATE_G0 != CHARSET_ASCII) {
WRITEBYTE3(ESC, '(', 'B') WRITEBYTE3(ESC, '(', 'B');
NEXT_OUT(3); NEXT_OUT(3);
STATE_SETG0(CHARSET_ASCII) STATE_SETG0(CHARSET_ASCII)
} }
@ -163,16 +163,16 @@ ENCODER(iso2022)
if (c < 0x80) { if (c < 0x80) {
if (STATE_G0 != CHARSET_ASCII) { if (STATE_G0 != CHARSET_ASCII) {
WRITEBYTE3(ESC, '(', 'B') WRITEBYTE3(ESC, '(', 'B');
STATE_SETG0(CHARSET_ASCII) STATE_SETG0(CHARSET_ASCII)
NEXT_OUT(3); NEXT_OUT(3);
} }
if (STATE_GETFLAG(F_SHIFTED)) { if (STATE_GETFLAG(F_SHIFTED)) {
WRITEBYTE1(SI) WRITEBYTE1(SI);
STATE_CLEARFLAG(F_SHIFTED) STATE_CLEARFLAG(F_SHIFTED)
NEXT_OUT(1); NEXT_OUT(1);
} }
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -210,24 +210,24 @@ ENCODER(iso2022)
switch (dsg->plane) { switch (dsg->plane) {
case 0: /* G0 */ case 0: /* G0 */
if (STATE_GETFLAG(F_SHIFTED)) { if (STATE_GETFLAG(F_SHIFTED)) {
WRITEBYTE1(SI) WRITEBYTE1(SI);
STATE_CLEARFLAG(F_SHIFTED) STATE_CLEARFLAG(F_SHIFTED)
NEXT_OUT(1); NEXT_OUT(1);
} }
if (STATE_G0 != dsg->mark) { if (STATE_G0 != dsg->mark) {
if (dsg->width == 1) { if (dsg->width == 1) {
WRITEBYTE3(ESC, '(', ESCMARK(dsg->mark)) WRITEBYTE3(ESC, '(', ESCMARK(dsg->mark));
STATE_SETG0(dsg->mark) STATE_SETG0(dsg->mark)
NEXT_OUT(3); NEXT_OUT(3);
} }
else if (dsg->mark == CHARSET_JISX0208) { else if (dsg->mark == CHARSET_JISX0208) {
WRITEBYTE3(ESC, '$', ESCMARK(dsg->mark)) WRITEBYTE3(ESC, '$', ESCMARK(dsg->mark));
STATE_SETG0(dsg->mark) STATE_SETG0(dsg->mark)
NEXT_OUT(3); NEXT_OUT(3);
} }
else { else {
WRITEBYTE4(ESC, '$', '(', WRITEBYTE4(ESC, '$', '(',
ESCMARK(dsg->mark)) ESCMARK(dsg->mark));
STATE_SETG0(dsg->mark) STATE_SETG0(dsg->mark)
NEXT_OUT(4); NEXT_OUT(4);
} }
@ -236,19 +236,19 @@ ENCODER(iso2022)
case 1: /* G1 */ case 1: /* G1 */
if (STATE_G1 != dsg->mark) { if (STATE_G1 != dsg->mark) {
if (dsg->width == 1) { if (dsg->width == 1) {
WRITEBYTE3(ESC, ')', ESCMARK(dsg->mark)) WRITEBYTE3(ESC, ')', ESCMARK(dsg->mark));
STATE_SETG1(dsg->mark) STATE_SETG1(dsg->mark)
NEXT_OUT(3); NEXT_OUT(3);
} }
else { else {
WRITEBYTE4(ESC, '$', ')', WRITEBYTE4(ESC, '$', ')',
ESCMARK(dsg->mark)) ESCMARK(dsg->mark));
STATE_SETG1(dsg->mark) STATE_SETG1(dsg->mark)
NEXT_OUT(4); NEXT_OUT(4);
} }
} }
if (!STATE_GETFLAG(F_SHIFTED)) { if (!STATE_GETFLAG(F_SHIFTED)) {
WRITEBYTE1(SO) WRITEBYTE1(SO);
STATE_SETFLAG(F_SHIFTED) STATE_SETFLAG(F_SHIFTED)
NEXT_OUT(1); NEXT_OUT(1);
} }
@ -259,11 +259,11 @@ ENCODER(iso2022)
} }
if (dsg->width == 1) { if (dsg->width == 1) {
WRITEBYTE1((unsigned char)encoded) WRITEBYTE1((unsigned char)encoded);
NEXT_OUT(1); NEXT_OUT(1);
} }
else { else {
WRITEBYTE2(encoded >> 8, encoded & 0xff) WRITEBYTE2(encoded >> 8, encoded & 0xff);
NEXT_OUT(2); NEXT_OUT(2);
} }
NEXT_INCHAR(insize); NEXT_INCHAR(insize);
@ -372,19 +372,20 @@ iso2022processesc(const void *config, MultibyteCodec_State *state,
} }
#define ISO8859_7_DECODE(c, writer) \ #define ISO8859_7_DECODE(c, writer) \
if ((c) < 0xa0) \ if ((c) < 0xa0) { \
OUTCHAR(c); \ OUTCHAR(c); \
else if ((c) < 0xc0 && (0x288f3bc9L & (1L << ((c)-0xa0)))) \ } else if ((c) < 0xc0 && (0x288f3bc9L & (1L << ((c)-0xa0)))) { \
OUTCHAR(c); \ OUTCHAR(c); \
else if ((c) >= 0xb4 && (c) <= 0xfe && ((c) >= 0xd4 || \ } else if ((c) >= 0xb4 && (c) <= 0xfe && ((c) >= 0xd4 || \
(0xbffffd77L & (1L << ((c)-0xb4))))) \ (0xbffffd77L & (1L << ((c)-0xb4))))) { \
OUTCHAR(0x02d0 + (c)); \ OUTCHAR(0x02d0 + (c)); \
else if ((c) == 0xa1) \ } else if ((c) == 0xa1) { \
OUTCHAR(0x2018); \ OUTCHAR(0x2018); \
else if ((c) == 0xa2) \ } else if ((c) == 0xa2) { \
OUTCHAR(0x2019); \ OUTCHAR(0x2019); \
else if ((c) == 0xaf) \ } else if ((c) == 0xaf) { \
OUTCHAR(0x2015); OUTCHAR(0x2015); \
}
static Py_ssize_t static Py_ssize_t
iso2022processg2(const void *config, MultibyteCodec_State *state, iso2022processg2(const void *config, MultibyteCodec_State *state,

View File

@ -25,33 +25,33 @@ ENCODER(cp932)
unsigned char c1, c2; unsigned char c1, c2;
if (c <= 0x80) { if (c <= 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
else if (c >= 0xff61 && c <= 0xff9f) { else if (c >= 0xff61 && c <= 0xff9f) {
WRITEBYTE1(c - 0xfec0) WRITEBYTE1(c - 0xfec0);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
else if (c >= 0xf8f0 && c <= 0xf8f3) { else if (c >= 0xf8f0 && c <= 0xf8f3) {
/* Windows compatibility */ /* Windows compatibility */
REQUIRE_OUTBUF(1) REQUIRE_OUTBUF(1);
if (c == 0xf8f0) if (c == 0xf8f0)
OUTBYTE1(0xa0) OUTBYTE1(0xa0);
else else
OUTBYTE1(c - 0xfef1 + 0xfd) OUTBYTE1(c - 0xfef1 + 0xfd);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(cp932ext, code, c)) { if (TRYMAP_ENC(cp932ext, code, c)) {
OUTBYTE1(code >> 8) OUTBYTE1(code >> 8);
OUTBYTE2(code & 0xff) OUTBYTE2(code & 0xff);
} }
else if (TRYMAP_ENC(jisxcommon, code, c)) { else if (TRYMAP_ENC(jisxcommon, code, c)) {
if (code & 0x8000) /* MSB set: JIS X 0212 */ if (code & 0x8000) /* MSB set: JIS X 0212 */
@ -62,15 +62,15 @@ ENCODER(cp932)
c2 = code & 0xff; c2 = code & 0xff;
c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21); c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
c1 = (c1 - 0x21) >> 1; c1 = (c1 - 0x21) >> 1;
OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1) OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1);
OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41) OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41);
} }
else if (c >= 0xe000 && c < 0xe758) { else if (c >= 0xe000 && c < 0xe758) {
/* User-defined area */ /* User-defined area */
c1 = (Py_UCS4)(c - 0xe000) / 188; c1 = (Py_UCS4)(c - 0xe000) / 188;
c2 = (Py_UCS4)(c - 0xe000) % 188; c2 = (Py_UCS4)(c - 0xe000) % 188;
OUTBYTE1(c1 + 0xf0) OUTBYTE1(c1 + 0xf0);
OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41) OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41);
} }
else else
return 1; return 1;
@ -156,7 +156,7 @@ ENCODER(euc_jis_2004)
Py_ssize_t insize; Py_ssize_t insize;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1(c) WRITEBYTE1(c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -201,7 +201,7 @@ ENCODER(euc_jis_2004)
; ;
else if (c >= 0xff61 && c <= 0xff9f) { else if (c >= 0xff61 && c <= 0xff9f) {
/* JIS X 0201 half-width katakana */ /* JIS X 0201 half-width katakana */
WRITEBYTE2(0x8e, c - 0xfec0) WRITEBYTE2(0x8e, c - 0xfec0);
NEXT(1, 2); NEXT(1, 2);
continue; continue;
} }
@ -226,11 +226,11 @@ ENCODER(euc_jis_2004)
if (code & 0x8000) { if (code & 0x8000) {
/* Codeset 2 */ /* Codeset 2 */
WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80) WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80);
NEXT(insize, 3); NEXT(insize, 3);
} else { } else {
/* Codeset 1 */ /* Codeset 1 */
WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80) WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80);
NEXT(insize, 2); NEXT(insize, 2);
} }
} }
@ -333,7 +333,7 @@ ENCODER(euc_jp)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -345,7 +345,7 @@ ENCODER(euc_jp)
; ;
else if (c >= 0xff61 && c <= 0xff9f) { else if (c >= 0xff61 && c <= 0xff9f) {
/* JIS X 0201 half-width katakana */ /* JIS X 0201 half-width katakana */
WRITEBYTE2(0x8e, c - 0xfec0) WRITEBYTE2(0x8e, c - 0xfec0);
NEXT(1, 2); NEXT(1, 2);
continue; continue;
} }
@ -367,11 +367,11 @@ ENCODER(euc_jp)
if (code & 0x8000) { if (code & 0x8000) {
/* JIS X 0212 */ /* JIS X 0212 */
WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80) WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80);
NEXT(1, 3); NEXT(1, 3);
} else { } else {
/* JIS X 0208 */ /* JIS X 0208 */
WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80) WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80);
NEXT(1, 2); NEXT(1, 2);
} }
} }
@ -470,14 +470,14 @@ ENCODER(shift_jis)
code = NOCHAR; code = NOCHAR;
if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) { if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
REQUIRE_OUTBUF(1) REQUIRE_OUTBUF(1);
OUTBYTE1((unsigned char)code) OUTBYTE1((unsigned char)code);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (code == NOCHAR) { if (code == NOCHAR) {
if (TRYMAP_ENC(jisxcommon, code, c)) if (TRYMAP_ENC(jisxcommon, code, c))
@ -497,8 +497,8 @@ ENCODER(shift_jis)
c2 = code & 0xff; c2 = code & 0xff;
c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21); c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
c1 = (c1 - 0x21) >> 1; c1 = (c1 - 0x21) >> 1;
OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1) OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1);
OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41) OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41);
NEXT(1, 2); NEXT(1, 2);
} }
@ -572,12 +572,12 @@ ENCODER(shift_jis_2004)
JISX0201_ENCODE(c, code) JISX0201_ENCODE(c, code)
if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) { if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
WRITEBYTE1((unsigned char)code) WRITEBYTE1((unsigned char)code);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
insize = 1; insize = 1;
if (code == NOCHAR) { if (code == NOCHAR) {
@ -654,8 +654,8 @@ ENCODER(shift_jis_2004)
if (c1 & 1) if (c1 & 1)
c2 += 0x5e; c2 += 0x5e;
c1 >>= 1; c1 >>= 1;
OUTBYTE1(c1 + (c1 < 0x1f ? 0x81 : 0xc1)) OUTBYTE1(c1 + (c1 < 0x1f ? 0x81 : 0xc1));
OUTBYTE2(c2 + (c2 < 0x3f ? 0x40 : 0x41)) OUTBYTE2(c2 + (c2 < 0x3f ? 0x40 : 0x41));
NEXT(insize, 2); NEXT(insize, 2);
} }

View File

@ -38,7 +38,7 @@ ENCODER(euc_kr)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -46,7 +46,7 @@ ENCODER(euc_kr)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(cp949, code, c)) if (TRYMAP_ENC(cp949, code, c))
; ;
else else
@ -54,33 +54,33 @@ ENCODER(euc_kr)
if ((code & 0x8000) == 0) { if ((code & 0x8000) == 0) {
/* KS X 1001 coded character */ /* KS X 1001 coded character */
OUTBYTE1((code >> 8) | 0x80) OUTBYTE1((code >> 8) | 0x80);
OUTBYTE2((code & 0xFF) | 0x80) OUTBYTE2((code & 0xFF) | 0x80);
NEXT(1, 2); NEXT(1, 2);
} }
else { /* Mapping is found in CP949 extension, else { /* Mapping is found in CP949 extension,
* but we encode it in KS X 1001:1998 Annex 3, * but we encode it in KS X 1001:1998 Annex 3,
* make-up sequence for EUC-KR. */ * make-up sequence for EUC-KR. */
REQUIRE_OUTBUF(8) REQUIRE_OUTBUF(8);
/* syllable composition precedence */ /* syllable composition precedence */
OUTBYTE1(EUCKR_JAMO_FIRSTBYTE) OUTBYTE1(EUCKR_JAMO_FIRSTBYTE);
OUTBYTE2(EUCKR_JAMO_FILLER) OUTBYTE2(EUCKR_JAMO_FILLER);
/* All codepoints in CP949 extension are in unicode /* All codepoints in CP949 extension are in unicode
* Hangul Syllable area. */ * Hangul Syllable area. */
assert(0xac00 <= c && c <= 0xd7a3); assert(0xac00 <= c && c <= 0xd7a3);
c -= 0xac00; c -= 0xac00;
OUTBYTE3(EUCKR_JAMO_FIRSTBYTE) OUTBYTE3(EUCKR_JAMO_FIRSTBYTE);
OUTBYTE4(u2cgk_choseong[c / 588]) OUTBYTE4(u2cgk_choseong[c / 588]);
NEXT_OUT(4); NEXT_OUT(4);
OUTBYTE1(EUCKR_JAMO_FIRSTBYTE) OUTBYTE1(EUCKR_JAMO_FIRSTBYTE);
OUTBYTE2(u2cgk_jungseong[(c / 28) % 21]) OUTBYTE2(u2cgk_jungseong[(c / 28) % 21]);
OUTBYTE3(EUCKR_JAMO_FIRSTBYTE) OUTBYTE3(EUCKR_JAMO_FIRSTBYTE);
OUTBYTE4(u2cgk_jongseong[c % 28]) OUTBYTE4(u2cgk_jongseong[c % 28]);
NEXT(1, 4); NEXT(1, 4);
} }
} }
@ -175,7 +175,7 @@ ENCODER(cp949)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -183,17 +183,17 @@ ENCODER(cp949)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(cp949, code, c)) if (TRYMAP_ENC(cp949, code, c))
; ;
else else
return 1; return 1;
OUTBYTE1((code >> 8) | 0x80) OUTBYTE1((code >> 8) | 0x80);
if (code & 0x8000) if (code & 0x8000)
OUTBYTE2(code & 0xFF) /* MSB set: CP949 */ OUTBYTE2(code & 0xFF); /* MSB set: CP949 */
else else
OUTBYTE2((code & 0xFF) | 0x80) /* MSB unset: ks x 1001 */ OUTBYTE2((code & 0xFF) | 0x80); /* MSB unset: ks x 1001 */
NEXT(1, 2); NEXT(1, 2);
} }
@ -265,7 +265,7 @@ ENCODER(johab)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -273,7 +273,7 @@ ENCODER(johab)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (c >= 0xac00 && c <= 0xd7a3) { if (c >= 0xac00 && c <= 0xd7a3) {
c -= 0xac00; c -= 0xac00;
@ -297,8 +297,8 @@ ENCODER(johab)
t1 = (c1 < 0x4a ? (c1 - 0x21 + 0x1b2) : t1 = (c1 < 0x4a ? (c1 - 0x21 + 0x1b2) :
(c1 - 0x21 + 0x197)); (c1 - 0x21 + 0x197));
t2 = ((t1 & 1) ? 0x5e : 0) + (c2 - 0x21); t2 = ((t1 & 1) ? 0x5e : 0) + (c2 - 0x21);
OUTBYTE1(t1 >> 1) OUTBYTE1(t1 >> 1);
OUTBYTE2(t2 < 0x4e ? t2 + 0x31 : t2 + 0x43) OUTBYTE2(t2 < 0x4e ? t2 + 0x31 : t2 + 0x43);
NEXT(1, 2); NEXT(1, 2);
continue; continue;
} }
@ -308,8 +308,8 @@ ENCODER(johab)
else else
return 1; return 1;
OUTBYTE1(code >> 8) OUTBYTE1(code >> 8);
OUTBYTE2(code & 0xff) OUTBYTE2(code & 0xff);
NEXT(1, 2); NEXT(1, 2);
} }

View File

@ -18,7 +18,7 @@ ENCODER(big5)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
REQUIRE_OUTBUF(1) REQUIRE_OUTBUF(1);
**outbuf = (unsigned char)c; **outbuf = (unsigned char)c;
NEXT(1, 1); NEXT(1, 1);
continue; continue;
@ -27,15 +27,15 @@ ENCODER(big5)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(big5, code, c)) if (TRYMAP_ENC(big5, code, c))
; ;
else else
return 1; return 1;
OUTBYTE1(code >> 8) OUTBYTE1(code >> 8);
OUTBYTE2(code & 0xFF) OUTBYTE2(code & 0xFF);
NEXT(1, 2); NEXT(1, 2);
} }
@ -77,7 +77,7 @@ ENCODER(cp950)
DBCHAR code; DBCHAR code;
if (c < 0x80) { if (c < 0x80) {
WRITEBYTE1((unsigned char)c) WRITEBYTE1((unsigned char)c);
NEXT(1, 1); NEXT(1, 1);
continue; continue;
} }
@ -85,7 +85,7 @@ ENCODER(cp950)
if (c > 0xFFFF) if (c > 0xFFFF)
return 1; return 1;
REQUIRE_OUTBUF(2) REQUIRE_OUTBUF(2);
if (TRYMAP_ENC(cp950ext, code, c)) if (TRYMAP_ENC(cp950ext, code, c))
; ;
else if (TRYMAP_ENC(big5, code, c)) else if (TRYMAP_ENC(big5, code, c))
@ -93,8 +93,8 @@ ENCODER(cp950)
else else
return 1; return 1;
OUTBYTE1(code >> 8) OUTBYTE1(code >> 8);
OUTBYTE2(code & 0xFF) OUTBYTE2(code & 0xFF);
NEXT(1, 2); NEXT(1, 2);
} }

View File

@ -10,8 +10,9 @@
} }
#define JISX0201_K_ENCODE(c, assi) \ #define JISX0201_K_ENCODE(c, assi) \
if ((c) >= 0xff61 && (c) <= 0xff9f) \ if ((c) >= 0xff61 && (c) <= 0xff9f) { \
{ (assi) = (c) - 0xfec0; } (assi) = (c) - 0xfec0; \
}
#define JISX0201_ENCODE(c, assi) \ #define JISX0201_ENCODE(c, assi) \
JISX0201_R_ENCODE(c, assi) \ JISX0201_R_ENCODE(c, assi) \
@ -35,18 +36,30 @@
} }
#define JISX0201_R_DECODE(c, writer) \ #define JISX0201_R_DECODE(c, writer) \
if ((c) < 0x5c) OUTCHAR(c); \ if ((c) < 0x5c) { \
else if ((c) == 0x5c) OUTCHAR(0x00a5); \ OUTCHAR(c); \
else if ((c) < 0x7e) OUTCHAR(c); \ } \
else if ((c) == 0x7e) OUTCHAR(0x203e); \ else if ((c) == 0x5c) { \
else if ((c) == 0x7f) OUTCHAR(0x7f); OUTCHAR(0x00a5); \
} \
else if ((c) < 0x7e) { \
OUTCHAR(c); \
} \
else if ((c) == 0x7e) { \
OUTCHAR(0x203e); \
} \
else if ((c) == 0x7f) { \
OUTCHAR(0x7f); \
}
#define JISX0201_K_DECODE(c, writer) \ #define JISX0201_K_DECODE(c, writer) \
if ((c) >= 0xa1 && (c) <= 0xdf) \ if ((c) >= 0xa1 && (c) <= 0xdf) { \
OUTCHAR(0xfec0 + (c)); OUTCHAR(0xfec0 + (c)); \
}
#define JISX0201_K_DECODE_CHAR(c, assi) \ #define JISX0201_K_DECODE_CHAR(c, assi) \
if ((c) >= 0xa1 && (c) <= 0xdf) \ if ((c) >= 0xa1 && (c) <= 0xdf) { \
(assi) = 0xfec0 + (c); (assi) = 0xfec0 + (c); \
}
#define JISX0201_DECODE(c, writer) \ #define JISX0201_DECODE(c, writer) \
JISX0201_R_DECODE(c, writer) \ JISX0201_R_DECODE(c, writer) \
else JISX0201_K_DECODE(c, writer) else JISX0201_K_DECODE(c, writer)

View File

@ -116,16 +116,18 @@ static const struct dbcs_map *mapping_list;
if (inleft < (n)) \ if (inleft < (n)) \
return MBERR_TOOFEW; return MBERR_TOOFEW;
#define REQUIRE_OUTBUF(n) \ #define REQUIRE_OUTBUF(n) \
do { \
if (outleft < (n)) \ if (outleft < (n)) \
return MBERR_TOOSMALL; return MBERR_TOOSMALL; \
} while (0)
#define INBYTE1 ((*inbuf)[0]) #define INBYTE1 ((*inbuf)[0])
#define INBYTE2 ((*inbuf)[1]) #define INBYTE2 ((*inbuf)[1])
#define INBYTE3 ((*inbuf)[2]) #define INBYTE3 ((*inbuf)[2])
#define INBYTE4 ((*inbuf)[3]) #define INBYTE4 ((*inbuf)[3])
#define INCHAR1 PyUnicode_READ(kind, data, *inpos) #define INCHAR1 (PyUnicode_READ(kind, data, *inpos))
#define INCHAR2 PyUnicode_READ(kind, data, *inpos + 1) #define INCHAR2 (PyUnicode_READ(kind, data, *inpos + 1))
#define OUTCHAR(c) \ #define OUTCHAR(c) \
do { \ do { \
@ -144,29 +146,41 @@ static const struct dbcs_map *mapping_list;
writer->pos += 2; \ writer->pos += 2; \
} while (0) } while (0)
#define OUTBYTE1(c) ((*outbuf)[0]) = (c); #define OUTBYTE1(c) \
#define OUTBYTE2(c) ((*outbuf)[1]) = (c); do { ((*outbuf)[0]) = (c); } while (0)
#define OUTBYTE3(c) ((*outbuf)[2]) = (c); #define OUTBYTE2(c) \
#define OUTBYTE4(c) ((*outbuf)[3]) = (c); do { ((*outbuf)[1]) = (c); } while (0)
#define OUTBYTE3(c) \
do { ((*outbuf)[2]) = (c); } while (0)
#define OUTBYTE4(c) \
do { ((*outbuf)[3]) = (c); } while (0)
#define WRITEBYTE1(c1) \ #define WRITEBYTE1(c1) \
REQUIRE_OUTBUF(1) \ do { \
(*outbuf)[0] = (c1); REQUIRE_OUTBUF(1); \
#define WRITEBYTE2(c1, c2) \
REQUIRE_OUTBUF(2) \
(*outbuf)[0] = (c1); \ (*outbuf)[0] = (c1); \
(*outbuf)[1] = (c2); } while (0)
#define WRITEBYTE3(c1, c2, c3) \ #define WRITEBYTE2(c1, c2) \
REQUIRE_OUTBUF(3) \ do { \
REQUIRE_OUTBUF(2); \
(*outbuf)[0] = (c1); \ (*outbuf)[0] = (c1); \
(*outbuf)[1] = (c2); \ (*outbuf)[1] = (c2); \
(*outbuf)[2] = (c3); } while (0)
#define WRITEBYTE4(c1, c2, c3, c4) \ #define WRITEBYTE3(c1, c2, c3) \
REQUIRE_OUTBUF(4) \ do { \
REQUIRE_OUTBUF(3); \
(*outbuf)[0] = (c1); \ (*outbuf)[0] = (c1); \
(*outbuf)[1] = (c2); \ (*outbuf)[1] = (c2); \
(*outbuf)[2] = (c3); \ (*outbuf)[2] = (c3); \
(*outbuf)[3] = (c4); } while (0)
#define WRITEBYTE4(c1, c2, c3, c4) \
do { \
REQUIRE_OUTBUF(4); \
(*outbuf)[0] = (c1); \
(*outbuf)[1] = (c2); \
(*outbuf)[2] = (c3); \
(*outbuf)[3] = (c4); \
} while (0)
#define _TRYMAP_ENC(m, assi, val) \ #define _TRYMAP_ENC(m, assi, val) \
((m)->map != NULL && (val) >= (m)->bottom && \ ((m)->map != NULL && (val) >= (m)->bottom && \

View File

@ -11,14 +11,17 @@
(c) == 0x525D || (c) == 0x541E || \ (c) == 0x525D || (c) == 0x541E || \
(c) == 0x5653 || (c) == 0x59F8 || \ (c) == 0x5653 || (c) == 0x59F8 || \
(c) == 0x5C5B || (c) == 0x5E77 || \ (c) == 0x5C5B || (c) == 0x5E77 || \
(c) == 0x7626 || (c) == 0x7E6B)) \ (c) == 0x7626 || (c) == 0x7E6B)) { \
return EMULATE_JISX0213_2000_ENCODE_INVALID; \ return EMULATE_JISX0213_2000_ENCODE_INVALID; \
else if (config == (void *)2000 && (c) == 0x9B1D) \ } \
else if (config == (void *)2000 && (c) == 0x9B1D) { \
(assi) = 0x8000 | 0x7d3b; \ (assi) = 0x8000 | 0x7d3b; \
}
#define EMULATE_JISX0213_2000_ENCODE_EMP(assi, c) \ #define EMULATE_JISX0213_2000_ENCODE_EMP(assi, c) \
if (config == (void *)2000 && (c) == 0x20B9F) \ if (config == (void *)2000 && (c) == 0x20B9F) { \
return EMULATE_JISX0213_2000_ENCODE_INVALID; return EMULATE_JISX0213_2000_ENCODE_INVALID; \
}
#ifndef EMULATE_JISX0213_2000_DECODE_INVALID #ifndef EMULATE_JISX0213_2000_DECODE_INVALID
# define EMULATE_JISX0213_2000_DECODE_INVALID 2 # define EMULATE_JISX0213_2000_DECODE_INVALID 2
@ -35,12 +38,17 @@
((c1) == 0x7E && (c2) == 0x7B) || \ ((c1) == 0x7E && (c2) == 0x7B) || \
((c1) == 0x7E && (c2) == 0x7C) || \ ((c1) == 0x7E && (c2) == 0x7C) || \
((c1) == 0x7E && (c2) == 0x7D) || \ ((c1) == 0x7E && (c2) == 0x7D) || \
((c1) == 0x7E && (c2) == 0x7E))) \ ((c1) == 0x7E && (c2) == 0x7E))) { \
return EMULATE_JISX0213_2000_DECODE_INVALID; return EMULATE_JISX0213_2000_DECODE_INVALID; \
}
#define EMULATE_JISX0213_2000_DECODE_PLANE2(writer, c1, c2) \ #define EMULATE_JISX0213_2000_DECODE_PLANE2(writer, c1, c2) \
if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) \ if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
OUTCHAR(0x9B1D); OUTCHAR(0x9B1D); \
}
#define EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(assi, c1, c2) \ #define EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(assi, c1, c2) \
if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) \ if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
(assi) = 0x9B1D; (assi) = 0x9B1D; \
}

View File

@ -118,10 +118,10 @@ typedef struct {
#define ERROR_IGNORE (PyObject *)(2) #define ERROR_IGNORE (PyObject *)(2)
#define ERROR_REPLACE (PyObject *)(3) #define ERROR_REPLACE (PyObject *)(3)
#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p)) #define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
#define ERROR_DECREF(p) do { \ #define ERROR_DECREF(p) \
if (p != NULL && ERROR_ISCUSTOM(p)) { \ do { \
if (p != NULL && ERROR_ISCUSTOM(p)) \
Py_DECREF(p); \ Py_DECREF(p); \
} \
} while (0); } while (0);
#define MBENC_FLUSH 0x0001 /* encode all characters encodable */ #define MBENC_FLUSH 0x0001 /* encode all characters encodable */