1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*-----------------------------------------------------------------------
*
* PostgreSQL locale utilities for builtin provider
*
* Portions Copyright (c) 2002-2025, PostgreSQL Global Development Group
*
* src/backend/utils/adt/pg_locale_builtin.c
*
*-----------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_database.h"
#include "catalog/pg_collation.h"
#include "common/unicode_case.h"
#include "common/unicode_category.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "utils/syscache.h"
extern pg_locale_t create_pg_locale_builtin(Oid collid,
MemoryContext context);
extern char *get_collation_actual_version_builtin(const char *collcollate);
struct WordBoundaryState
{
const char *str;
size_t len;
size_t offset;
bool posix;
bool init;
bool prev_alnum;
};
/*
* Simple word boundary iterator that draws boundaries each time the result of
* pg_u_isalnum() changes.
*/
static size_t
initcap_wbnext(void *state)
{
struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
while (wbstate->offset < wbstate->len &&
wbstate->str[wbstate->offset] != '\0')
{
pg_wchar u = utf8_to_unicode((unsigned char *) wbstate->str +
wbstate->offset);
bool curr_alnum = pg_u_isalnum(u, wbstate->posix);
if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
{
size_t prev_offset = wbstate->offset;
wbstate->init = true;
wbstate->offset += unicode_utf8len(u);
wbstate->prev_alnum = curr_alnum;
return prev_offset;
}
wbstate->offset += unicode_utf8len(u);
}
return wbstate->len;
}
static size_t
strlower_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen,
pg_locale_t locale)
{
return unicode_strlower(dest, destsize, src, srclen,
locale->info.builtin.casemap_full);
}
static size_t
strtitle_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen,
pg_locale_t locale)
{
struct WordBoundaryState wbstate = {
.str = src,
.len = srclen,
.offset = 0,
.posix = !locale->info.builtin.casemap_full,
.init = false,
.prev_alnum = false,
};
return unicode_strtitle(dest, destsize, src, srclen,
locale->info.builtin.casemap_full,
initcap_wbnext, &wbstate);
}
static size_t
strupper_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen,
pg_locale_t locale)
{
return unicode_strupper(dest, destsize, src, srclen,
locale->info.builtin.casemap_full);
}
static size_t
strfold_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen,
pg_locale_t locale)
{
return unicode_strfold(dest, destsize, src, srclen,
locale->info.builtin.casemap_full);
}
static bool
wc_isdigit_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isdigit(wc, !locale->info.builtin.casemap_full);
}
static bool
wc_isalpha_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isalpha(wc);
}
static bool
wc_isalnum_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isalnum(wc, !locale->info.builtin.casemap_full);
}
static bool
wc_isupper_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isupper(wc);
}
static bool
wc_islower_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_islower(wc);
}
static bool
wc_isgraph_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isgraph(wc);
}
static bool
wc_isprint_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isprint(wc);
}
static bool
wc_ispunct_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_ispunct(wc, !locale->info.builtin.casemap_full);
}
static bool
wc_isspace_builtin(pg_wchar wc, pg_locale_t locale)
{
return pg_u_isspace(wc);
}
static bool
char_is_cased_builtin(char ch, pg_locale_t locale)
{
return IS_HIGHBIT_SET(ch) ||
(ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}
static pg_wchar
wc_toupper_builtin(pg_wchar wc, pg_locale_t locale)
{
return unicode_uppercase_simple(wc);
}
static pg_wchar
wc_tolower_builtin(pg_wchar wc, pg_locale_t locale)
{
return unicode_lowercase_simple(wc);
}
static const struct ctype_methods ctype_methods_builtin = {
.strlower = strlower_builtin,
.strtitle = strtitle_builtin,
.strupper = strupper_builtin,
.strfold = strfold_builtin,
.wc_isdigit = wc_isdigit_builtin,
.wc_isalpha = wc_isalpha_builtin,
.wc_isalnum = wc_isalnum_builtin,
.wc_isupper = wc_isupper_builtin,
.wc_islower = wc_islower_builtin,
.wc_isgraph = wc_isgraph_builtin,
.wc_isprint = wc_isprint_builtin,
.wc_ispunct = wc_ispunct_builtin,
.wc_isspace = wc_isspace_builtin,
.char_is_cased = char_is_cased_builtin,
.wc_tolower = wc_tolower_builtin,
.wc_toupper = wc_toupper_builtin,
};
pg_locale_t
create_pg_locale_builtin(Oid collid, MemoryContext context)
{
const char *locstr;
pg_locale_t result;
if (collid == DEFAULT_COLLATION_OID)
{
HeapTuple tp;
Datum datum;
tp = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
Anum_pg_database_datlocale);
locstr = TextDatumGetCString(datum);
ReleaseSysCache(tp);
}
else
{
HeapTuple tp;
Datum datum;
tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for collation %u", collid);
datum = SysCacheGetAttrNotNull(COLLOID, tp,
Anum_pg_collation_colllocale);
locstr = TextDatumGetCString(datum);
ReleaseSysCache(tp);
}
builtin_validate_locale(GetDatabaseEncoding(), locstr);
result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
result->info.builtin.locale = MemoryContextStrdup(context, locstr);
result->info.builtin.casemap_full = (strcmp(locstr, "PG_UNICODE_FAST") == 0);
result->deterministic = true;
result->collate_is_c = true;
result->ctype_is_c = (strcmp(locstr, "C") == 0);
if (!result->ctype_is_c)
result->ctype = &ctype_methods_builtin;
return result;
}
char *
get_collation_actual_version_builtin(const char *collcollate)
{
/*
* The only two supported locales (C and C.UTF-8) are both based on memcmp
* and are not expected to change, but track the version anyway.
*
* Note that the character semantics may change for some locales, but the
* collation version only tracks changes to sort order.
*/
if (strcmp(collcollate, "C") == 0)
return "1";
else if (strcmp(collcollate, "C.UTF-8") == 0)
return "1";
else if (strcmp(collcollate, "PG_UNICODE_FAST") == 0)
return "1";
else
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("invalid locale name \"%s\" for builtin provider",
collcollate)));
return NULL; /* keep compiler quiet */
}
|