Skip to content

Commit

Permalink
PoC: put big5 codec in cjk module state
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Apr 26, 2023
1 parent 03e16a8 commit 664f961
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Modules/cjkcodecs/_codecs_hk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

#define USING_IMPORTED_MAPS

#define CJK_MOD_SPECIFIC_STATE \
const encode_map *big5_encmap; \
const decode_map *big5_decmap;

#include "cjkcodecs.h"
#include "mappings_hk.h"

/*
* BIG5HKSCS codec
*/

static const encode_map *big5_encmap = NULL;
static const decode_map *big5_decmap = NULL;

CODEC_INIT(big5hkscs)
{
static int initialized = 0;

if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
cjkcodecs_module_state *st = codec->modstate;
if (IMPORT_MAP(tw, big5, &st->big5_encmap, &st->big5_decmap)) {
return -1;
initialized = 1;
}
return 0;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ ENCODER(big5hkscs)
}
}
}
else if (TRYMAP_ENC(big5, code, c))
else if (TRYMAP_ENC_ST(big5, code, c))
;
else
return 1;
Expand Down Expand Up @@ -122,7 +122,7 @@ DECODER(big5hkscs)
REQUIRE_INBUF(2);

if (0xc6 > c || c > 0xc8 || (c < 0xc7 && INBYTE2 < 0xa1)) {
if (TRYMAP_DEC(big5, decoded, c, INBYTE2)) {
if (TRYMAP_DEC_ST(big5, decoded, c, INBYTE2)) {
OUTCHAR(decoded);
NEXT_IN(2);
continue;
Expand Down
5 changes: 5 additions & 0 deletions Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ get_module_state(PyObject *mod)
(m)->bottom]) != NOCHAR)
#define TRYMAP_ENC(charset, assi, uni) \
_TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
#define TRYMAP_ENC_ST(charset, assi, uni) \
_TRYMAP_ENC(&(codec->modstate->charset##_encmap)[(uni) >> 8], \
assi, (uni) & 0xff)

#define _TRYMAP_DEC(m, assi, val) \
((m)->map != NULL && \
Expand All @@ -219,6 +222,8 @@ get_module_state(PyObject *mod)
((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV)
#define TRYMAP_DEC(charset, assi, c1, c2) \
_TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
#define TRYMAP_DEC_ST(charset, assi, c1, c2) \
_TRYMAP_DEC(&(codec->modstate->charset##_decmap)[c1], assi, c2)

#define BEGIN_MAPPINGS_LIST(NUM) \
static int \
Expand Down

0 comments on commit 664f961

Please sign in to comment.