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 #ifdef USE_ICU
16 #include <unicode/ucol.h>
17 #endif
18 
19 #ifdef USE_ICU
20 /*
21  * ucol_strcollUTF8() was introduced in ICU 50, but it is buggy before ICU 53.
22  * (see
23  * <https://www.postgresql.org/message-id/flat/f1438ec6-22aa-4029-9a3b-26f79d330e72%40manitou-mail.org>)
24  */
25 #if U_ICU_VERSION_MAJOR_NUM >= 53
26 #define HAVE_UCOL_STRCOLLUTF8 1
27 #else
28 #undef HAVE_UCOL_STRCOLLUTF8
29 #endif
30 #endif
31 
32 /* use for libc locale names */
33 #define LOCALE_NAME_BUFLEN 128
34 
35 /* GUC settings */
36 extern PGDLLIMPORT char *locale_messages;
37 extern PGDLLIMPORT char *locale_monetary;
38 extern PGDLLIMPORT char *locale_numeric;
39 extern PGDLLIMPORT char *locale_time;
41 
42 /* lc_time localization cache */
43 extern PGDLLIMPORT char *localized_abbrev_days[];
44 extern PGDLLIMPORT char *localized_full_days[];
46 extern PGDLLIMPORT char *localized_full_months[];
47 
48 /* is the databases's LC_CTYPE the C locale? */
50 
51 extern bool check_locale(int category, const char *locale, char **canonname);
52 extern char *pg_perm_setlocale(int category, const char *locale);
53 
54 /*
55  * Return the POSIX lconv struct (contains number/money formatting
56  * information) with locale information for all categories.
57  */
58 extern struct lconv *PGLC_localeconv(void);
59 
60 extern void cache_locale_time(void);
61 
62 
63 /*
64  * We use a discriminated union to hold either a locale_t or an ICU collator.
65  * pg_locale_t is occasionally checked for truth, so make it a pointer.
66  *
67  * Also, hold two flags: whether the collation's LC_COLLATE or LC_CTYPE is C
68  * (or POSIX), so we can optimize a few code paths in various places. For the
69  * built-in C and POSIX collations, we can know that without even doing a
70  * cache lookup, but we want to support aliases for C/POSIX too. For the
71  * "default" collation, there are separate static cache variables, since
72  * consulting the pg_collation catalog doesn't tell us what we need.
73  *
74  * Note that some code relies on the flags not reporting false negatives
75  * (that is, saying it's not C when it is). For example, char2wchar()
76  * could fail if the locale is C, so str_tolower() shouldn't call it
77  * in that case.
78  */
80 {
81  char provider;
84  bool ctype_is_c;
85  bool is_default;
86  union
87  {
88  struct
89  {
90  const char *locale;
93 #ifdef USE_ICU
94  struct
95  {
96  const char *locale;
97  UCollator *ucol;
98  } icu;
99 #endif
100  } info;
101 };
102 
104 
105 extern void init_database_collation(void);
107 
108 extern char *get_collation_actual_version(char collprovider, const char *collcollate);
109 extern int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale);
110 extern int pg_strncoll(const char *arg1, ssize_t len1,
111  const char *arg2, ssize_t len2, pg_locale_t locale);
113 extern size_t pg_strxfrm(char *dest, const char *src, size_t destsize,
115 extern size_t pg_strnxfrm(char *dest, size_t destsize, const char *src,
116  ssize_t srclen, pg_locale_t locale);
118 extern size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize,
120 extern size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
121  ssize_t srclen, pg_locale_t locale);
122 
123 extern int builtin_locale_encoding(const char *locale);
124 extern const char *builtin_validate_locale(int encoding, const char *locale);
125 extern void icu_validate_locale(const char *loc_str);
126 extern char *icu_language_tag(const char *loc_str, int elevel);
127 
128 #ifdef USE_ICU
129 extern int32_t icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes);
130 extern int32_t icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar);
131 #endif
132 
133 /* These functions convert from/to libc's wchar_t, *not* pg_wchar_t */
134 extern size_t wchar2char(char *to, const wchar_t *from, size_t tolen,
136 extern size_t char2wchar(wchar_t *to, size_t tolen,
137  const char *from, size_t fromlen, pg_locale_t locale);
138 
139 #endif /* _PG_LOCALE_ */
#define PGDLLIMPORT
Definition: c.h:1295
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:103
PGDLLIMPORT int icu_validation_level
Definition: pg_locale.c:124
void cache_locale_time(void)
Definition: pg_locale.c:817
size_t pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1716
bool pg_strxfrm_enabled(pg_locale_t locale)
Definition: pg_locale.c:1656
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
struct lconv * PGLC_localeconv(void)
Definition: pg_locale.c:535
void icu_validate_locale(const char *loc_str)
Definition: pg_locale.c:1920
PGDLLIMPORT char * locale_time
Definition: pg_locale.c:122
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1434
PGDLLIMPORT char * localized_abbrev_days[]
Definition: pg_locale.c:133
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1803
size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1783
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1483
PGDLLIMPORT char * locale_numeric
Definition: pg_locale.c:121
PGDLLIMPORT char * localized_full_days[]
Definition: pg_locale.c:134
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:201
char * icu_language_tag(const char *loc_str, int elevel)
Definition: pg_locale.c:1862
int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
Definition: pg_locale.c:1591
PGDLLIMPORT char * localized_full_months[]
Definition: pg_locale.c:136
PGDLLIMPORT char * localized_abbrev_months[]
Definition: pg_locale.c:135
PGDLLIMPORT char * locale_monetary
Definition: pg_locale.c:120
bool pg_strxfrm_prefix_enabled(pg_locale_t locale)
Definition: pg_locale.c:1739
PGDLLIMPORT char * locale_messages
Definition: pg_locale.c:119
int pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
Definition: pg_locale.c:1623
void init_database_collation(void)
Definition: pg_locale.c:1335
PGDLLIMPORT bool database_ctype_is_c
Definition: pg_locale.c:139
size_t pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:1679
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:1824
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:304
size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:1758
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
unsigned int Oid
Definition: postgres_ext.h:31
locale_t lt
Definition: pg_locale.h:92
union pg_locale_struct::@156 info
const char * locale
Definition: pg_locale.h:90
struct pg_locale_struct::@156::@157 builtin
bool deterministic
Definition: pg_locale.h:82
#define locale_t
Definition: win32_port.h:442