diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-10-28 22:54:13 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-10-28 22:54:13 (GMT) |
commit | bd97ac35f7cab6229243e2c93b6fa6eaf7f0cc19 (patch) | |
tree | 65a872a6465d226a833445ce4a5f46ea6d363949 | |
parent | 0a6e2c59d362659001adfb232537f9af5fdb256c (diff) | |
download | cpython-bd97ac35f7cab6229243e2c93b6fa6eaf7f0cc19.zip cpython-bd97ac35f7cab6229243e2c93b6fa6eaf7f0cc19.tar.gz cpython-bd97ac35f7cab6229243e2c93b6fa6eaf7f0cc19.tar.bz2 |
CJK codecs: use less magic and more readable macros, write explicit if
-rw-r--r-- | Modules/cjkcodecs/_codecs_cn.c | 8 | ||||
-rw-r--r-- | Modules/cjkcodecs/_codecs_hk.c | 6 | ||||
-rw-r--r-- | Modules/cjkcodecs/_codecs_iso2022.c | 16 | ||||
-rw-r--r-- | Modules/cjkcodecs/_codecs_jp.c | 20 | ||||
-rw-r--r-- | Modules/cjkcodecs/_codecs_kr.c | 6 | ||||
-rw-r--r-- | Modules/cjkcodecs/_codecs_tw.c | 6 | ||||
-rw-r--r-- | Modules/cjkcodecs/cjkcodecs.h | 4 |
7 files changed, 32 insertions, 34 deletions
diff --git a/Modules/cjkcodecs/_codecs_cn.c b/Modules/cjkcodecs/_codecs_cn.c index 430ca5c..3bd3f23 100644 --- a/Modules/cjkcodecs/_codecs_cn.c +++ b/Modules/cjkcodecs/_codecs_cn.c @@ -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 */ diff --git a/Modules/cjkcodecs/_codecs_hk.c b/Modules/cjkcodecs/_codecs_hk.c index cc7627a..825772e 100644 --- a/Modules/cjkcodecs/_codecs_hk.c +++ b/Modules/cjkcodecs/_codecs_hk.c @@ -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 diff --git a/Modules/cjkcodecs/_codecs_iso2022.c b/Modules/cjkcodecs/_codecs_iso2022.c index 62a3f07..44234ca 100644 --- a/Modules/cjkcodecs/_codecs_iso2022.c +++ b/Modules/cjkcodecs/_codecs_iso2022.c @@ -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; } diff --git a/Modules/cjkcodecs/_codecs_jp.c b/Modules/cjkcodecs/_codecs_jp.c index a08aa15..993096e 100644 --- a/Modules/cjkcodecs/_codecs_jp.c +++ b/Modules/cjkcodecs/_codecs_jp.c @@ -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 diff --git a/Modules/cjkcodecs/_codecs_kr.c b/Modules/cjkcodecs/_codecs_kr.c index 8291e61..71d9099 100644 --- a/Modules/cjkcodecs/_codecs_kr.c +++ b/Modules/cjkcodecs/_codecs_kr.c @@ -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; diff --git a/Modules/cjkcodecs/_codecs_tw.c b/Modules/cjkcodecs/_codecs_tw.c index fb3ed00..9f32bf7 100644 --- a/Modules/cjkcodecs/_codecs_tw.c +++ b/Modules/cjkcodecs/_codecs_tw.c @@ -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) diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h index ea234db..4a5f0c1 100644 --- a/Modules/cjkcodecs/cjkcodecs.h +++ b/Modules/cjkcodecs/cjkcodecs.h @@ -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 && \ |