1 /* Dispatching based on the current locale's character encoding.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2018. */
22 #include "lc-charset-dispatch.h"
24 #if GNULIB_defined_mbstate_t
26 # include "localcharset.h"
29 # if GNULIB_WCHAR_SINGLE_LOCALE
30 /* When we know that the locale does not change, provide a speedup by
31 caching the value of locale_encoding_classification. */
32 # define locale_encoding_classification_cached locale_encoding_classification
34 /* By default, don't make assumptions, hence no caching. */
35 # define locale_encoding_classification_uncached locale_encoding_classification
38 # if GNULIB_WCHAR_SINGLE_LOCALE
42 locale_encoding_classification_uncached (void)
44 const char *encoding
= locale_charset ();
45 if (STREQ_OPT (encoding
, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
47 if (STREQ_OPT (encoding
, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
49 if (STREQ_OPT (encoding
, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
50 || STREQ_OPT (encoding
, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
51 || STREQ_OPT (encoding
, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
53 if (STREQ_OPT (encoding
, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
55 if (STREQ_OPT (encoding
, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
57 if (STREQ_OPT (encoding
, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
62 # if GNULIB_WCHAR_SINGLE_LOCALE
64 static int cached_locale_enc
= -1;
67 locale_encoding_classification_cached (void)
69 if (cached_locale_enc
< 0)
70 cached_locale_enc
= locale_encoding_classification_uncached ();
71 return cached_locale_enc
;
78 /* This declaration is solely to ensure that after preprocessing
79 this file is never empty. */