Skip to content

Commit 655dc31

Browse files
committed
Simplify pg_enc2gettext_tbl[] with C99-designated initializer syntax
This commit switches pg_enc2gettext_tbl[] in encnames.c to use a C99-designated initializer syntax. pg_bind_textdomain_codeset() is simplified so as it is possible to do a direct lookup at the gettext() array with a value of the enum pg_enc rather than doing a loop through all its elements, as long as the encoding value provided by GetDatabaseEncoding() is in the correct range of supported encoding values. Note that PG_MULE_INTERNAL gains a value in the array, pointing to NULL. Author: Jelte Fennema-Nio Discussion: https://postgr.es/m/CAGECzQT3caUbcCcszNewCCmMbCuyP7XNAm60J3ybd6PN5kH2Dw@mail.gmail.com
1 parent def0ce3 commit 655dc31

File tree

4 files changed

+55
-67
lines changed

4 files changed

+55
-67
lines changed

src/backend/utils/mb/mbutils.c

+9-15
Original file line numberDiff line numberDiff line change
@@ -1188,24 +1188,18 @@ static bool
11881188
raw_pg_bind_textdomain_codeset(const char *domainname, int encoding)
11891189
{
11901190
bool elog_ok = (CurrentMemoryContext != NULL);
1191-
int i;
11921191

1193-
for (i = 0; pg_enc2gettext_tbl[i].name != NULL; i++)
1194-
{
1195-
if (pg_enc2gettext_tbl[i].encoding == encoding)
1196-
{
1197-
if (bind_textdomain_codeset(domainname,
1198-
pg_enc2gettext_tbl[i].name) != NULL)
1199-
return true;
1192+
if (!PG_VALID_ENCODING(encoding) || pg_enc2gettext_tbl[encoding] == NULL)
1193+
return false;
12001194

1201-
if (elog_ok)
1202-
elog(LOG, "bind_textdomain_codeset failed");
1203-
else
1204-
write_stderr("bind_textdomain_codeset failed");
1195+
if (bind_textdomain_codeset(domainname,
1196+
pg_enc2gettext_tbl[encoding]) != NULL)
1197+
return true;
12051198

1206-
break;
1207-
}
1208-
}
1199+
if (elog_ok)
1200+
elog(LOG, "bind_textdomain_codeset failed");
1201+
else
1202+
write_stderr("bind_textdomain_codeset failed");
12091203

12101204
return false;
12111205
}

src/common/encnames.c

+43-43
Original file line numberDiff line numberDiff line change
@@ -357,50 +357,50 @@ const pg_enc2name pg_enc2name_tbl[] =
357357
* This covers all encodings except MULE_INTERNAL, which is alien to gettext.
358358
* ----------
359359
*/
360-
const pg_enc2gettext pg_enc2gettext_tbl[] =
360+
const char *pg_enc2gettext_tbl[] =
361361
{
362-
{PG_SQL_ASCII, "US-ASCII"},
363-
{PG_UTF8, "UTF-8"},
364-
{PG_LATIN1, "LATIN1"},
365-
{PG_LATIN2, "LATIN2"},
366-
{PG_LATIN3, "LATIN3"},
367-
{PG_LATIN4, "LATIN4"},
368-
{PG_ISO_8859_5, "ISO-8859-5"},
369-
{PG_ISO_8859_6, "ISO_8859-6"},
370-
{PG_ISO_8859_7, "ISO-8859-7"},
371-
{PG_ISO_8859_8, "ISO-8859-8"},
372-
{PG_LATIN5, "LATIN5"},
373-
{PG_LATIN6, "LATIN6"},
374-
{PG_LATIN7, "LATIN7"},
375-
{PG_LATIN8, "LATIN8"},
376-
{PG_LATIN9, "LATIN-9"},
377-
{PG_LATIN10, "LATIN10"},
378-
{PG_KOI8R, "KOI8-R"},
379-
{PG_KOI8U, "KOI8-U"},
380-
{PG_WIN1250, "CP1250"},
381-
{PG_WIN1251, "CP1251"},
382-
{PG_WIN1252, "CP1252"},
383-
{PG_WIN1253, "CP1253"},
384-
{PG_WIN1254, "CP1254"},
385-
{PG_WIN1255, "CP1255"},
386-
{PG_WIN1256, "CP1256"},
387-
{PG_WIN1257, "CP1257"},
388-
{PG_WIN1258, "CP1258"},
389-
{PG_WIN866, "CP866"},
390-
{PG_WIN874, "CP874"},
391-
{PG_EUC_CN, "EUC-CN"},
392-
{PG_EUC_JP, "EUC-JP"},
393-
{PG_EUC_KR, "EUC-KR"},
394-
{PG_EUC_TW, "EUC-TW"},
395-
{PG_EUC_JIS_2004, "EUC-JP"},
396-
{PG_SJIS, "SHIFT-JIS"},
397-
{PG_BIG5, "BIG5"},
398-
{PG_GBK, "GBK"},
399-
{PG_UHC, "UHC"},
400-
{PG_GB18030, "GB18030"},
401-
{PG_JOHAB, "JOHAB"},
402-
{PG_SHIFT_JIS_2004, "SHIFT_JISX0213"},
403-
{0, NULL}
362+
[PG_SQL_ASCII] = "US-ASCII",
363+
[PG_UTF8] = "UTF-8",
364+
[PG_MULE_INTERNAL] = NULL,
365+
[PG_LATIN1] = "LATIN1",
366+
[PG_LATIN2] = "LATIN2",
367+
[PG_LATIN3] = "LATIN3",
368+
[PG_LATIN4] = "LATIN4",
369+
[PG_ISO_8859_5] = "ISO-8859-5",
370+
[PG_ISO_8859_6] = "ISO_8859-6",
371+
[PG_ISO_8859_7] = "ISO-8859-7",
372+
[PG_ISO_8859_8] = "ISO-8859-8",
373+
[PG_LATIN5] = "LATIN5",
374+
[PG_LATIN6] = "LATIN6",
375+
[PG_LATIN7] = "LATIN7",
376+
[PG_LATIN8] = "LATIN8",
377+
[PG_LATIN9] = "LATIN-9",
378+
[PG_LATIN10] = "LATIN10",
379+
[PG_KOI8R] = "KOI8-R",
380+
[PG_KOI8U] = "KOI8-U",
381+
[PG_WIN1250] = "CP1250",
382+
[PG_WIN1251] = "CP1251",
383+
[PG_WIN1252] = "CP1252",
384+
[PG_WIN1253] = "CP1253",
385+
[PG_WIN1254] = "CP1254",
386+
[PG_WIN1255] = "CP1255",
387+
[PG_WIN1256] = "CP1256",
388+
[PG_WIN1257] = "CP1257",
389+
[PG_WIN1258] = "CP1258",
390+
[PG_WIN866] = "CP866",
391+
[PG_WIN874] = "CP874",
392+
[PG_EUC_CN] = "EUC-CN",
393+
[PG_EUC_JP] = "EUC-JP",
394+
[PG_EUC_KR] = "EUC-KR",
395+
[PG_EUC_TW] = "EUC-TW",
396+
[PG_EUC_JIS_2004] = "EUC-JP",
397+
[PG_SJIS] = "SHIFT-JIS",
398+
[PG_BIG5] = "BIG5",
399+
[PG_GBK] = "GBK",
400+
[PG_UHC] = "UHC",
401+
[PG_GB18030] = "GB18030",
402+
[PG_JOHAB] = "JOHAB",
403+
[PG_SHIFT_JIS_2004] = "SHIFT_JISX0213",
404404
};
405405

406406

src/include/mb/pg_wchar.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ typedef unsigned int pg_wchar;
225225
* PostgreSQL encoding identifiers
226226
*
227227
* WARNING: If you add some encoding don't forget to update
228-
* the pg_enc2name_tbl[] array (in src/common/encnames.c) and
228+
* the pg_enc2name_tbl[] array (in src/common/encnames.c),
229+
* the pg_enc2gettext_tbl[] array (in src/common/encnames.c) and
229230
* the pg_wchar_table[] array (in src/common/wchar.c) and to check
230231
* PG_ENCODING_BE_LAST macro.
231232
*
@@ -365,13 +366,7 @@ extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[];
365366
/*
366367
* Encoding names for gettext
367368
*/
368-
typedef struct pg_enc2gettext
369-
{
370-
pg_enc encoding;
371-
const char *name;
372-
} pg_enc2gettext;
373-
374-
extern PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[];
369+
extern PGDLLIMPORT const char *pg_enc2gettext_tbl[];
375370

376371
/*
377372
* pg_wchar stuff

src/tools/pgindent/typedefs.list

-1
Original file line numberDiff line numberDiff line change
@@ -3574,7 +3574,6 @@ pg_cryptohash_errno
35743574
pg_cryptohash_type
35753575
pg_ctype_cache
35763576
pg_enc
3577-
pg_enc2gettext
35783577
pg_enc2name
35793578
pg_encname
35803579
pg_fe_sasl_mech

0 commit comments

Comments
 (0)