CJK codecs: use less magic and more readable macros, write explicit if

This commit is contained in:
Victor Stinner 2013-10-28 23:54:13 +01:00
parent 0a6e2c59d3
commit bd97ac35f7
7 changed files with 32 additions and 34 deletions

View File

@ -36,7 +36,7 @@
if ((code) == 0x2014) (assi) = 0xa1aa; \
else if ((code) == 0x2015) (assi) = 0xa844; \
else if ((code) == 0x00b7) (assi) = 0xa1a4; \
else if ((code) != 0x30fb && TRYMAP_ENC_COND(gbcommon, assi, code));
else if ((code) != 0x30fb && TRYMAP_ENC(gbcommon, assi, code));
/*
* GB2312 codec
@ -58,7 +58,7 @@ ENCODER(gb2312)
return 1;
REQUIRE_OUTBUF(2)
TRYMAP_ENC(gbcommon, code, c);
if (TRYMAP_ENC(gbcommon, code, c));
else return 1;
if (code & 0x8000) /* MSB set: GBK */
@ -192,7 +192,7 @@ ENCODER(gb18030)
REQUIRE_OUTBUF(2)
GBK_ENCODE(c, code)
else TRYMAP_ENC(gb18030ext, code, c);
else if (TRYMAP_ENC(gb18030ext, code, c));
else {
const struct _gb18030_to_unibmp_ranges *utrrange;
@ -343,7 +343,7 @@ ENCODER(hz)
if (c > 0xFFFF)
return 1;
TRYMAP_ENC(gbcommon, code, c);
if (TRYMAP_ENC(gbcommon, code, c));
else return 1;
if (code & 0x8000) /* MSB set: GBK */

View File

@ -54,7 +54,7 @@ ENCODER(big5hkscs)
REQUIRE_OUTBUF(2)
if (c < 0x10000) {
TRYMAP_ENC(big5hkscs_bmp, code, c) {
if (TRYMAP_ENC(big5hkscs_bmp, code, c)) {
if (code == MULTIC) {
Py_UCS4 c2;
if (inlen - *inpos >= 2)
@ -81,13 +81,13 @@ ENCODER(big5hkscs)
}
}
}
else TRYMAP_ENC(big5, code, c);
else if (TRYMAP_ENC(big5, code, c));
else return 1;
}
else if (c < 0x20000)
return insize;
else if (c < 0x30000) {
TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff);
if (TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff));
else return insize;
}
else

View File

@ -578,9 +578,10 @@ ksx1001_encoder(const Py_UCS4 *data, Py_ssize_t *length)
DBCHAR coded;
assert(*length == 1);
if (*data < 0x10000) {
TRYMAP_ENC(cp949, coded, *data)
if (TRYMAP_ENC(cp949, coded, *data)) {
if (!(coded & 0x8000))
return coded;
}
}
return MAP_UNMAPPABLE;
}
@ -618,7 +619,7 @@ jisx0208_encoder(const Py_UCS4 *data, Py_ssize_t *length)
if (*data < 0x10000) {
if (*data == 0xff3c) /* F/W REVERSE SOLIDUS */
return 0x2140;
else TRYMAP_ENC(jisxcommon, coded, *data) {
else if (TRYMAP_ENC(jisxcommon, coded, *data)) {
if (!(coded & 0x8000))
return coded;
}
@ -655,7 +656,7 @@ jisx0212_encoder(const Py_UCS4 *data, Py_ssize_t *length)
DBCHAR coded;
assert(*length == 1);
if (*data < 0x10000) {
TRYMAP_ENC(jisxcommon, coded, *data) {
if (TRYMAP_ENC(jisxcommon, coded, *data)) {
if (coded & 0x8000)
return coded & 0x7fff;
}
@ -759,19 +760,18 @@ jisx0213_encoder(const Py_UCS4 *data, Py_ssize_t *length, void *config)
if (*data >= 0x10000) {
if ((*data) >> 16 == 0x20000 >> 16) {
EMULATE_JISX0213_2000_ENCODE_EMP(coded, *data)
else TRYMAP_ENC(jisx0213_emp, coded,
(*data) & 0xffff)
else if (TRYMAP_ENC(jisx0213_emp, coded, (*data) & 0xffff))
return coded;
}
return MAP_UNMAPPABLE;
}
EMULATE_JISX0213_2000_ENCODE_BMP(coded, *data)
else TRYMAP_ENC(jisx0213_bmp, coded, *data) {
else if (TRYMAP_ENC(jisx0213_bmp, coded, *data)) {
if (coded == MULTIC)
return MAP_MULTIPLE_AVAIL;
}
else TRYMAP_ENC(jisxcommon, coded, *data) {
else if (TRYMAP_ENC(jisxcommon, coded, *data)) {
if (coded & 0x8000)
return MAP_UNMAPPABLE;
}
@ -962,7 +962,7 @@ gb2312_encoder(const Py_UCS4 *data, Py_ssize_t *length)
DBCHAR coded;
assert(*length == 1);
if (*data < 0x10000) {
TRYMAP_ENC(gbcommon, coded, *data) {
if (TRYMAP_ENC(gbcommon, coded, *data)) {
if (!(coded & 0x8000))
return coded;
}

View File

@ -49,11 +49,11 @@ ENCODER(cp932)
return 1;
REQUIRE_OUTBUF(2)
TRYMAP_ENC(cp932ext, code, c) {
if (TRYMAP_ENC(cp932ext, code, c)) {
OUTBYTE1(code >> 8)
OUTBYTE2(code & 0xff)
}
else TRYMAP_ENC(jisxcommon, code, c) {
else if (TRYMAP_ENC(jisxcommon, code, c)) {
if (code & 0x8000) /* MSB set: JIS X 0212 */
return 1;
@ -165,7 +165,7 @@ ENCODER(euc_jis_2004)
if (c <= 0xFFFF) {
EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
else TRYMAP_ENC(jisx0213_bmp, code, c) {
else if (TRYMAP_ENC(jisx0213_bmp, code, c)) {
if (code == MULTIC) {
if (inlen - *inpos < 2) {
if (flags & MBENC_FLUSH) {
@ -197,7 +197,7 @@ ENCODER(euc_jis_2004)
}
}
}
else TRYMAP_ENC(jisxcommon, code, c);
else if (TRYMAP_ENC(jisxcommon, code, c));
else if (c >= 0xff61 && c <= 0xff9f) {
/* JIS X 0201 half-width katakana */
WRITEBYTE2(0x8e, c - 0xfec0)
@ -215,7 +215,7 @@ ENCODER(euc_jis_2004)
}
else if (c >> 16 == EMPBASE >> 16) {
EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
else TRYMAP_ENC(jisx0213_emp, code, c & 0xffff);
else if (TRYMAP_ENC(jisx0213_emp, code, c & 0xffff));
else return insize;
}
else
@ -334,7 +334,7 @@ ENCODER(euc_jp)
if (c > 0xFFFF)
return 1;
TRYMAP_ENC(jisxcommon, code, c);
if (TRYMAP_ENC(jisxcommon, code, c));
else if (c >= 0xff61 && c <= 0xff9f) {
/* JIS X 0201 half-width katakana */
WRITEBYTE2(0x8e, c - 0xfec0)
@ -469,7 +469,7 @@ ENCODER(shift_jis)
REQUIRE_OUTBUF(2)
if (code == NOCHAR) {
TRYMAP_ENC(jisxcommon, code, c);
if (TRYMAP_ENC(jisxcommon, code, c));
#ifndef STRICT_BUILD
else if (c == 0xff3c)
code = 0x2140; /* FULL-WIDTH REVERSE SOLIDUS */
@ -570,7 +570,7 @@ ENCODER(shift_jis_2004)
if (code == NOCHAR) {
if (c <= 0xffff) {
EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
else TRYMAP_ENC(jisx0213_bmp, code, c) {
else if (TRYMAP_ENC(jisx0213_bmp, code, c)) {
if (code == MULTIC) {
if (inlen - *inpos < 2) {
if (flags & MBENC_FLUSH) {
@ -603,7 +603,7 @@ ENCODER(shift_jis_2004)
}
}
}
else TRYMAP_ENC(jisxcommon, code, c) {
else if (TRYMAP_ENC(jisxcommon, code, c)) {
/* abandon JIS X 0212 codes */
if (code & 0x8000)
return 1;
@ -612,7 +612,7 @@ ENCODER(shift_jis_2004)
}
else if (c >> 16 == EMPBASE >> 16) {
EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
else TRYMAP_ENC(jisx0213_emp, code, c&0xffff);
else if (TRYMAP_ENC(jisx0213_emp, code, c&0xffff));
else return insize;
}
else

View File

@ -47,7 +47,7 @@ ENCODER(euc_kr)
return 1;
REQUIRE_OUTBUF(2)
TRYMAP_ENC(cp949, code, c);
if (TRYMAP_ENC(cp949, code, c));
else return 1;
if ((code & 0x8000) == 0) {
@ -182,7 +182,7 @@ ENCODER(cp949)
return 1;
REQUIRE_OUTBUF(2)
TRYMAP_ENC(cp949, code, c);
if (TRYMAP_ENC(cp949, code, c));
else return 1;
OUTBYTE1((code >> 8) | 0x80)
@ -279,7 +279,7 @@ ENCODER(johab)
}
else if (c >= 0x3131 && c <= 0x3163)
code = u2johabjamo[c - 0x3131];
else TRYMAP_ENC(cp949, code, c) {
else if (TRYMAP_ENC(cp949, code, c)) {
unsigned char c1, c2, t2;
unsigned short t1;

View File

@ -29,7 +29,7 @@ ENCODER(big5)
REQUIRE_OUTBUF(2)
TRYMAP_ENC(big5, code, c);
if (TRYMAP_ENC(big5, code, c));
else return 1;
OUTBYTE1(code >> 8)
@ -84,8 +84,8 @@ ENCODER(cp950)
return 1;
REQUIRE_OUTBUF(2)
TRYMAP_ENC(cp950ext, code, c);
else TRYMAP_ENC(big5, code, c);
if (TRYMAP_ENC(cp950ext, code, c));
else if (TRYMAP_ENC(big5, code, c));
else return 1;
OUTBYTE1(code >> 8)

View File

@ -172,10 +172,8 @@ static const struct dbcs_map *mapping_list;
((m)->map != NULL && (val) >= (m)->bottom && \
(val)<= (m)->top && ((assi) = (m)->map[(val) - \
(m)->bottom]) != NOCHAR)
#define TRYMAP_ENC_COND(charset, assi, uni) \
#define TRYMAP_ENC(charset, assi, uni) \
_TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
#define TRYMAP_ENC(charset, assi, uni) \
if TRYMAP_ENC_COND(charset, assi, uni)
#define _TRYMAP_DEC(m, assi, val) \
((m)->map != NULL && \