From 804f32765fd909018ec22b27d5e3d7bffe802d72 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 10 Oct 2021 14:32:03 +0100 Subject: [PATCH] Make bidi type enums into list macros. This makes it easier to create the matching array of type names in bidi_gettype.c, and eliminates the need for an assertion to check the array matched the enum. And I'm about to need to add more types, so let's start by making that trivially easy. --- terminal/bidi.h | 63 ++++++++++++++++++++++------------------- terminal/bidi_gettype.c | 30 ++++---------------- 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/terminal/bidi.h b/terminal/bidi.h index eca80c21..53ffbcd3 100644 --- a/terminal/bidi.h +++ b/terminal/bidi.h @@ -25,36 +25,41 @@ shapetypes[(xh)-SHAPE_FIRST].type : SU) /*))*/ /* Function declarations used outside bidi.c */ unsigned char bidi_getType(int ch); -/* character types */ -enum { - L, - LRE, - LRO, - R, - AL, - RLE, - RLO, - PDF, - EN, - ES, - ET, - AN, - CS, - NSM, - BN, - B, - S, - WS, - ON -}; +/* Bidi character types */ +#define BIDI_CHAR_TYPE_LIST(X) \ + X(L) \ + X(LRE) \ + X(LRO) \ + X(R) \ + X(AL) \ + X(RLE) \ + X(RLO) \ + X(PDF) \ + X(EN) \ + X(ES) \ + X(ET) \ + X(AN) \ + X(CS) \ + X(NSM) \ + X(BN) \ + X(B) \ + X(S) \ + X(WS) \ + X(ON) \ + /* end of list */ /* Shaping Types */ -enum { - SL, /* Left-Joining, doesn't exist in U+0600 - U+06FF */ - SR, /* Right-Joining, ie has Isolated, Final */ - SD, /* Dual-Joining, ie has Isolated, Final, Initial, Medial */ - SU, /* Non-Joining */ - SC /* Join-Causing, like U+0640 (TATWEEL) */ -}; +#define SHAPING_CHAR_TYPE_LIST(X) \ + X(SL) /* Left-Joining, doesn't exist in U+0600 - U+06FF */ \ + X(SR) /* Right-Joining, ie has Isolated, Final */ \ + X(SD) /* Dual-Joining, ie has Isolated, Final, Initial, Medial */ \ + X(SU) /* Non-Joining */ \ + X(SC) /* Join-Causing, like U+0640 (TATWEEL) */ \ + /* end of list */ + +#define ENUM_DECL(name) name, +typedef enum { BIDI_CHAR_TYPE_LIST(ENUM_DECL) N_BIDI_TYPES } BidiType; +typedef enum { SHAPING_CHAR_TYPE_LIST(ENUM_DECL) N_SHAPING_TYPES } ShapingType; +#undef ENUM_DECL #endif /* PUTTY_BIDI_H */ diff --git a/terminal/bidi_gettype.c b/terminal/bidi_gettype.c index a3b765ae..f3f5338e 100644 --- a/terminal/bidi_gettype.c +++ b/terminal/bidi_gettype.c @@ -15,38 +15,18 @@ void out_of_memory(void) exit(2); } +#define TYPETONAME(X) #X, +static const char *const typenames[] = { BIDI_CHAR_TYPE_LIST(TYPETONAME) }; +#undef TYPETONAME + int main(int argc, char **argv) { - static const struct { int type; char *name; } typetoname[] = { -#define TYPETONAME(X) { X , #X } - TYPETONAME(L), - TYPETONAME(LRE), - TYPETONAME(LRO), - TYPETONAME(R), - TYPETONAME(AL), - TYPETONAME(RLE), - TYPETONAME(RLO), - TYPETONAME(PDF), - TYPETONAME(EN), - TYPETONAME(ES), - TYPETONAME(ET), - TYPETONAME(AN), - TYPETONAME(CS), - TYPETONAME(NSM), - TYPETONAME(BN), - TYPETONAME(B), - TYPETONAME(S), - TYPETONAME(WS), - TYPETONAME(ON), -#undef TYPETONAME - }; int i; for (i = 1; i < argc; i++) { unsigned long chr = strtoul(argv[i], NULL, 0); int type = bidi_getType(chr); - assert(typetoname[type].type == type); - printf("U+%04x: %s\n", (unsigned)chr, typetoname[type].name); + printf("U+%04x: %s\n", (unsigned)chr, typenames[type]); } return 0;