PostgreSQL Source Code  git master
pg_locale.h
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------
2  *
3  * PostgreSQL locale utilities
4  *
5  * src/include/utils/pg_locale.h
6  *
7  * Copyright (c) 2002-2024, PostgreSQL Global Development Group
8  *
9  *-----------------------------------------------------------------------
10  */
11 
12 #ifndef _PG_LOCALE_
13 #define _PG_LOCALE_
14 
15 #if defined(LOCALE_T_IN_XLOCALE) || defined(WCSTOMBS_L_IN_XLOCALE)
16 #include <xlocale.h>
17 #endif
18 #ifdef USE_ICU
19 #include <unicode/ucol.h>
20 #endif
21 
22 #ifdef USE_ICU
23 /*
24  * ucol_strcollUTF8() was introduced in ICU 50, but it is buggy before ICU 53.
25  * (see
26  * <https://www.postgresql.org/message-id/flat/f1438ec6-22aa-4029-9a3b-26f79d330e72%40manitou-mail.org>)
27  */
28 #if U_ICU_VERSION_MAJOR_NUM >= 53
29 #define HAVE_UCOL_STRCOLLUTF8 1
30 #else
31 #undef HAVE_UCOL_STRCOLLUTF8
32 #endif
33 #endif
34 
35 /* use for libc locale names */
36 #define LOCALE_NAME_BUFLEN 128
37 
38 /* GUC settings */
39 extern PGDLLIMPORT char *locale_messages;
40 extern PGDLLIMPORT char *locale_monetary;
41 extern PGDLLIMPORT char *locale_numeric;
42 extern PGDLLIMPORT char *locale_time;
44 
45 /* lc_time localization cache */
46 extern PGDLLIMPORT char *localized_abbrev_days[];
47 extern PGDLLIMPORT char *localized_full_days[];
49 extern PGDLLIMPORT char *localized_full_months[];
50 
51 /* is the databases's LC_CTYPE the C locale? */
53 
54 extern bool check_locale(int category, const char *locale, char **canonname);
55 extern char *pg_perm_setlocale(int category, const char *locale);
56 
57 /*
58  * Return the POSIX lconv struct (contains number/money formatting
59  * information) with locale information for all categories.
60  */
61 extern struct lconv *PGLC_localeconv(void);
62 
63 extern void cache_locale_time(void);
64 
65 
66 /*
67  * We use a discriminated union to hold either a locale_t or an ICU collator.
68  * pg_locale_t is occasionally checked for truth, so make it a pointer.
69  *
70  * Also, hold two flags: whether the collation's LC_COLLATE or LC_CTYPE is C
71  * (or POSIX), so we can optimize a few code paths in various places. For the
72  * built-in C and POSIX collations, we can know that without even doing a
73  * cache lookup, but we want to support aliases for C/POSIX too. For the
74  * "default" collation, there are separate static cache variables, since
75  * consulting the pg_collation catalog doesn't tell us what we need.
76  *
77  * Note that some code relies on the flags not reporting false negatives
78  * (that is, saying it's not C when it is). For example, char2wchar()
79  * could fail if the locale is C, so str_tolower() shouldn't call it
80  * in that case.
81  */
83 {
84  char provider;
87  bool ctype_is_c;
88  union
89  {
90  struct
91  {
92  const char *locale;
95 #ifdef USE_ICU
96  struct
97  {
98  const char *locale;
99  UCollator *ucol;
100  } icu;
101 #endif
102  } info;
103 };
104 
106 
107 extern void make_icu_collator(const char *iculocstr,
108  const char *icurules,
109  struct pg_locale_struct *resultp);
110 
111 extern void init_database_collation(void);
113 
114 extern char *get_collation_actual_version(char collprovider, const char *collcollate);
115 extern int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale);
116 extern int pg_strncoll(const char *arg1, size_t len1,
117  const char *arg2, size_t len2, pg_locale_t locale);
119 extern size_t pg_strxfrm(char *dest, const char *src, size_t destsize,
121 extern size_t pg_strnxfrm(char *dest, size_t destsize, const char *src,
122  size_t srclen, pg_locale_t locale);
124 extern size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize,
126 extern size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
127  size_t srclen, pg_locale_t locale);
128 
129 extern int builtin_locale_encoding(const char *locale);
130 extern const char *builtin_validate_locale(int encoding, const char *locale);
131 extern void icu_validate_locale(const char *loc_str);
132 extern char *icu_language_tag(const char *loc_str, int elevel);
133 
134 #ifdef USE_ICU
135 extern int32_t icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes);
136 extern int32_t icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar);
137 #endif
138 
139 /* These functions convert from/to libc's wchar_t, *not* pg_wchar_t */
140 extern size_t wchar2char(char *to, const wchar_t *from, size_t tolen,
142 extern size_t char2wchar(wchar_t *to, size_t tolen,
143  const char *from, size_t fromlen, pg_locale_t locale);
144 
145 #endif /* _PG_LOCALE_ */
#define PGDLLIMPORT
Definition: c.h:1316
Oid collid
static char * locale
Definition: initdb.c:140
int32 encoding
Definition: pg_database.h:41
struct pg_locale_struct * pg_locale_t
Definition: pg_locale.h:105
size_t pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, pg_locale_t locale)
Definition: pg_locale.c:2339
PGDLLIMPORT int icu_validation_level
Definition: pg_locale.c:103
void cache_locale_time(void)
Definition: pg_locale.c:830
bool pg_strxfrm_enabled(pg_locale_t locale)
Definition: pg_locale.c:2263
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:2992
struct lconv * PGLC_localeconv(void)
Definition: pg_locale.c:548
int pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, pg_locale_t locale)
Definition: pg_locale.c:2057
void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp)
Definition: pg_locale.c:1367
void icu_validate_locale(const char *loc_str)
Definition: pg_locale.c:2909
PGDLLIMPORT char * locale_time
Definition: pg_locale.c:101
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1510
PGDLLIMPORT char * localized_abbrev_days[]
Definition: pg_locale.c:112
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:2450
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1659
PGDLLIMPORT char * locale_numeric
Definition: pg_locale.c:100
PGDLLIMPORT char * localized_full_days[]
Definition: pg_locale.c:113
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:236
char * icu_language_tag(const char *loc_str, int elevel)
Definition: pg_locale.c:2851
int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
Definition: pg_locale.c:2022
PGDLLIMPORT char * localized_full_months[]
Definition: pg_locale.c:115
PGDLLIMPORT char * localized_abbrev_months[]
Definition: pg_locale.c:114
PGDLLIMPORT char * locale_monetary
Definition: pg_locale.c:99
bool pg_strxfrm_prefix_enabled(pg_locale_t locale)
Definition: pg_locale.c:2362
PGDLLIMPORT char * locale_messages
Definition: pg_locale.c:98
size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, size_t srclen, pg_locale_t locale)
Definition: pg_locale.c:2430
void init_database_collation(void)
Definition: pg_locale.c:1422
PGDLLIMPORT bool database_ctype_is_c
Definition: pg_locale.c:118
size_t pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:2299
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:2471
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:339
size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:2393
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:3048
unsigned int Oid
Definition: postgres_ext.h:31
union pg_locale_struct::@153 info
struct pg_locale_struct::@153::@154 builtin
locale_t lt
Definition: pg_locale.h:94
const char * locale
Definition: pg_locale.h:92
bool deterministic
Definition: pg_locale.h:85
#define locale_t
Definition: win32_port.h:442