]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Simplify pg_enc2gettext_tbl[] with C99-designated initializer syntax
authorMichael Paquier <michael@paquier.xyz>
Fri, 1 Mar 2024 09:03:48 +0000 (18:03 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 1 Mar 2024 09:03:48 +0000 (18:03 +0900)
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

src/backend/utils/mb/mbutils.c
src/common/encnames.c
src/include/mb/pg_wchar.h
src/tools/pgindent/typedefs.list

index c13f947a8278df95c83d16eac3dffc262b7bad2d..7108ab89836afb8c2ec5ba6905679b41710598e6 100644 (file)
@@ -1188,24 +1188,18 @@ static bool
 raw_pg_bind_textdomain_codeset(const char *domainname, int encoding)
 {
        bool            elog_ok = (CurrentMemoryContext != NULL);
-       int                     i;
 
-       for (i = 0; pg_enc2gettext_tbl[i].name != NULL; i++)
-       {
-               if (pg_enc2gettext_tbl[i].encoding == encoding)
-               {
-                       if (bind_textdomain_codeset(domainname,
-                                                                               pg_enc2gettext_tbl[i].name) != NULL)
-                               return true;
+       if (!PG_VALID_ENCODING(encoding) || pg_enc2gettext_tbl[encoding] == NULL)
+               return false;
 
-                       if (elog_ok)
-                               elog(LOG, "bind_textdomain_codeset failed");
-                       else
-                               write_stderr("bind_textdomain_codeset failed");
+       if (bind_textdomain_codeset(domainname,
+                                                               pg_enc2gettext_tbl[encoding]) != NULL)
+               return true;
 
-                       break;
-               }
-       }
+       if (elog_ok)
+               elog(LOG, "bind_textdomain_codeset failed");
+       else
+               write_stderr("bind_textdomain_codeset failed");
 
        return false;
 }
index dba6bd2c9eef4e1a53db1f90ea6dbcc3c1b071ef..910cc2c7e5cf0fc3062b1766465de18e744ba9ce 100644 (file)
@@ -357,50 +357,50 @@ const pg_enc2name pg_enc2name_tbl[] =
  * This covers all encodings except MULE_INTERNAL, which is alien to gettext.
  * ----------
  */
-const pg_enc2gettext pg_enc2gettext_tbl[] =
+const char *pg_enc2gettext_tbl[] =
 {
-       {PG_SQL_ASCII, "US-ASCII"},
-       {PG_UTF8, "UTF-8"},
-       {PG_LATIN1, "LATIN1"},
-       {PG_LATIN2, "LATIN2"},
-       {PG_LATIN3, "LATIN3"},
-       {PG_LATIN4, "LATIN4"},
-       {PG_ISO_8859_5, "ISO-8859-5"},
-       {PG_ISO_8859_6, "ISO_8859-6"},
-       {PG_ISO_8859_7, "ISO-8859-7"},
-       {PG_ISO_8859_8, "ISO-8859-8"},
-       {PG_LATIN5, "LATIN5"},
-       {PG_LATIN6, "LATIN6"},
-       {PG_LATIN7, "LATIN7"},
-       {PG_LATIN8, "LATIN8"},
-       {PG_LATIN9, "LATIN-9"},
-       {PG_LATIN10, "LATIN10"},
-       {PG_KOI8R, "KOI8-R"},
-       {PG_KOI8U, "KOI8-U"},
-       {PG_WIN1250, "CP1250"},
-       {PG_WIN1251, "CP1251"},
-       {PG_WIN1252, "CP1252"},
-       {PG_WIN1253, "CP1253"},
-       {PG_WIN1254, "CP1254"},
-       {PG_WIN1255, "CP1255"},
-       {PG_WIN1256, "CP1256"},
-       {PG_WIN1257, "CP1257"},
-       {PG_WIN1258, "CP1258"},
-       {PG_WIN866, "CP866"},
-       {PG_WIN874, "CP874"},
-       {PG_EUC_CN, "EUC-CN"},
-       {PG_EUC_JP, "EUC-JP"},
-       {PG_EUC_KR, "EUC-KR"},
-       {PG_EUC_TW, "EUC-TW"},
-       {PG_EUC_JIS_2004, "EUC-JP"},
-       {PG_SJIS, "SHIFT-JIS"},
-       {PG_BIG5, "BIG5"},
-       {PG_GBK, "GBK"},
-       {PG_UHC, "UHC"},
-       {PG_GB18030, "GB18030"},
-       {PG_JOHAB, "JOHAB"},
-       {PG_SHIFT_JIS_2004, "SHIFT_JISX0213"},
-       {0, NULL}
+       [PG_SQL_ASCII] = "US-ASCII",
+       [PG_UTF8] = "UTF-8",
+       [PG_MULE_INTERNAL] = NULL,
+       [PG_LATIN1] = "LATIN1",
+       [PG_LATIN2] = "LATIN2",
+       [PG_LATIN3] = "LATIN3",
+       [PG_LATIN4] = "LATIN4",
+       [PG_ISO_8859_5] = "ISO-8859-5",
+       [PG_ISO_8859_6] = "ISO_8859-6",
+       [PG_ISO_8859_7] = "ISO-8859-7",
+       [PG_ISO_8859_8] = "ISO-8859-8",
+       [PG_LATIN5] = "LATIN5",
+       [PG_LATIN6] = "LATIN6",
+       [PG_LATIN7] = "LATIN7",
+       [PG_LATIN8] = "LATIN8",
+       [PG_LATIN9] = "LATIN-9",
+       [PG_LATIN10] = "LATIN10",
+       [PG_KOI8R] = "KOI8-R",
+       [PG_KOI8U] = "KOI8-U",
+       [PG_WIN1250] = "CP1250",
+       [PG_WIN1251] = "CP1251",
+       [PG_WIN1252] = "CP1252",
+       [PG_WIN1253] = "CP1253",
+       [PG_WIN1254] = "CP1254",
+       [PG_WIN1255] = "CP1255",
+       [PG_WIN1256] = "CP1256",
+       [PG_WIN1257] = "CP1257",
+       [PG_WIN1258] = "CP1258",
+       [PG_WIN866] = "CP866",
+       [PG_WIN874] = "CP874",
+       [PG_EUC_CN] = "EUC-CN",
+       [PG_EUC_JP] = "EUC-JP",
+       [PG_EUC_KR] = "EUC-KR",
+       [PG_EUC_TW] = "EUC-TW",
+       [PG_EUC_JIS_2004] = "EUC-JP",
+       [PG_SJIS] = "SHIFT-JIS",
+       [PG_BIG5] = "BIG5",
+       [PG_GBK] = "GBK",
+       [PG_UHC] = "UHC",
+       [PG_GB18030] = "GB18030",
+       [PG_JOHAB] = "JOHAB",
+       [PG_SHIFT_JIS_2004] = "SHIFT_JISX0213",
 };
 
 
index fd91aefbcb70846e1f5ce63f7d7ccea0820b733d..32e25a1a6eae98154ae1de9aec4502156ad10f25 100644 (file)
@@ -225,7 +225,8 @@ typedef unsigned int pg_wchar;
  * PostgreSQL encoding identifiers
  *
  * WARNING: If you add some encoding don't forget to update
- *                     the pg_enc2name_tbl[] array (in src/common/encnames.c) and
+ *                     the pg_enc2name_tbl[] array (in src/common/encnames.c),
+ *                     the pg_enc2gettext_tbl[] array (in src/common/encnames.c) and
  *                     the pg_wchar_table[] array (in src/common/wchar.c) and to check
  *                     PG_ENCODING_BE_LAST macro.
  *
@@ -365,13 +366,7 @@ extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[];
 /*
  * Encoding names for gettext
  */
-typedef struct pg_enc2gettext
-{
-       pg_enc          encoding;
-       const char *name;
-} pg_enc2gettext;
-
-extern PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[];
+extern PGDLLIMPORT const char *pg_enc2gettext_tbl[];
 
 /*
  * pg_wchar stuff
index fc8b15d0cf2bfb89b3622d28831856a170d5bf7b..ee40a341d3f92cb92f8f126265e82dd6ac16ba7b 100644 (file)
@@ -3574,7 +3574,6 @@ pg_cryptohash_errno
 pg_cryptohash_type
 pg_ctype_cache
 pg_enc
-pg_enc2gettext
 pg_enc2name
 pg_encname
 pg_fe_sasl_mech