PostgreSQL Source Code git master
pg_locale_builtin.c File Reference
#include "postgres.h"
#include "catalog/pg_database.h"
#include "catalog/pg_collation.h"
#include "common/unicode_case.h"
#include "common/unicode_category.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "utils/syscache.h"
Include dependency graph for pg_locale_builtin.c:

Go to the source code of this file.

Data Structures

struct  WordBoundaryState
 

Functions

pg_locale_t create_pg_locale_builtin (Oid collid, MemoryContext context)
 
char * get_collation_actual_version_builtin (const char *collcollate)
 
static char32_t to_char32 (pg_wchar wc)
 
static pg_wchar to_pg_wchar (char32_t c32)
 
static size_t initcap_wbnext (void *state)
 
static size_t strlower_builtin (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
static size_t strtitle_builtin (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
static size_t strupper_builtin (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
static size_t strfold_builtin (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
static bool wc_isdigit_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isalpha_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isalnum_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isupper_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_islower_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isgraph_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isprint_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_ispunct_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isspace_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool wc_isxdigit_builtin (pg_wchar wc, pg_locale_t locale)
 
static bool char_is_cased_builtin (char ch, pg_locale_t locale)
 
static pg_wchar wc_toupper_builtin (pg_wchar wc, pg_locale_t locale)
 
static pg_wchar wc_tolower_builtin (pg_wchar wc, pg_locale_t locale)
 

Variables

static const struct ctype_methods ctype_methods_builtin
 

Function Documentation

◆ char_is_cased_builtin()

static bool char_is_cased_builtin ( char  ch,
pg_locale_t  locale 
)
static

Definition at line 189 of file pg_locale_builtin.c.

190{
191 return IS_HIGHBIT_SET(ch) ||
192 (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
193}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1143

References IS_HIGHBIT_SET.

◆ create_pg_locale_builtin()

pg_locale_t create_pg_locale_builtin ( Oid  collid,
MemoryContext  context 
)

Definition at line 228 of file pg_locale_builtin.c.

229{
230 const char *locstr;
231 pg_locale_t result;
232
233 if (collid == DEFAULT_COLLATION_OID)
234 {
235 HeapTuple tp;
236 Datum datum;
237
239 if (!HeapTupleIsValid(tp))
240 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
241 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
242 Anum_pg_database_datlocale);
243 locstr = TextDatumGetCString(datum);
244 ReleaseSysCache(tp);
245 }
246 else
247 {
248 HeapTuple tp;
249 Datum datum;
250
252 if (!HeapTupleIsValid(tp))
253 elog(ERROR, "cache lookup failed for collation %u", collid);
254 datum = SysCacheGetAttrNotNull(COLLOID, tp,
255 Anum_pg_collation_colllocale);
256 locstr = TextDatumGetCString(datum);
257 ReleaseSysCache(tp);
258 }
259
261
262 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
263
264 result->builtin.locale = MemoryContextStrdup(context, locstr);
265 result->builtin.casemap_full = (strcmp(locstr, "PG_UNICODE_FAST") == 0);
266 result->deterministic = true;
267 result->collate_is_c = true;
268 result->ctype_is_c = (strcmp(locstr, "C") == 0);
269 if (!result->ctype_is_c)
270 result->ctype = &ctype_methods_builtin;
271
272 return result;
273}
#define TextDatumGetCString(d)
Definition: builtins.h:98
Oid collid
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
Oid MyDatabaseId
Definition: globals.c:94
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int GetDatabaseEncoding(void)
Definition: mbutils.c:1262
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1746
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:1687
static const struct ctype_methods ctype_methods_builtin
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
const struct ctype_methods * ctype
Definition: pg_locale.h:161
struct pg_locale_struct::@166::@168 builtin
const char * locale
Definition: pg_locale.h:167
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:625

References pg_locale_struct::builtin, builtin_validate_locale(), pg_locale_struct::casemap_full, pg_locale_struct::collate_is_c, collid, pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, ctype_methods_builtin, pg_locale_struct::deterministic, elog, ERROR, GetDatabaseEncoding(), HeapTupleIsValid, pg_locale_struct::locale, MemoryContextAllocZero(), MemoryContextStrdup(), MyDatabaseId, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

Referenced by create_pg_locale(), and init_database_collation().

◆ get_collation_actual_version_builtin()

char * get_collation_actual_version_builtin ( const char *  collcollate)

Definition at line 276 of file pg_locale_builtin.c.

277{
278 /*
279 * The only two supported locales (C and C.UTF-8) are both based on memcmp
280 * and are not expected to change, but track the version anyway.
281 *
282 * Note that the character semantics may change for some locales, but the
283 * collation version only tracks changes to sort order.
284 */
285 if (strcmp(collcollate, "C") == 0)
286 return "1";
287 else if (strcmp(collcollate, "C.UTF-8") == 0)
288 return "1";
289 else if (strcmp(collcollate, "PG_UNICODE_FAST") == 0)
290 return "1";
291 else
293 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
294 errmsg("invalid locale name \"%s\" for builtin provider",
295 collcollate)));
296
297 return NULL; /* keep compiler quiet */
298}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150

References ereport, errcode(), errmsg(), and ERROR.

Referenced by get_collation_actual_version().

◆ initcap_wbnext()

static size_t initcap_wbnext ( void *  state)
static

Definition at line 59 of file pg_locale_builtin.c.

60{
61 struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
62
63 while (wbstate->offset < wbstate->len &&
64 wbstate->str[wbstate->offset] != '\0')
65 {
66 char32_t u = utf8_to_unicode((unsigned char *) wbstate->str +
67 wbstate->offset);
68 bool curr_alnum = pg_u_isalnum(u, wbstate->posix);
69
70 if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
71 {
72 size_t prev_offset = wbstate->offset;
73
74 wbstate->init = true;
75 wbstate->offset += unicode_utf8len(u);
76 wbstate->prev_alnum = curr_alnum;
77 return prev_offset;
78 }
79
80 wbstate->offset += unicode_utf8len(u);
81 }
82
83 return wbstate->len;
84}
static char32_t utf8_to_unicode(const unsigned char *c)
Definition: mbprint.c:53
static int unicode_utf8len(char32_t c)
Definition: pg_wchar.h:607
Definition: regguts.h:323
bool pg_u_isalnum(char32_t code, bool posix)

References WordBoundaryState::init, WordBoundaryState::len, WordBoundaryState::offset, pg_u_isalnum(), WordBoundaryState::posix, WordBoundaryState::prev_alnum, WordBoundaryState::str, unicode_utf8len(), and utf8_to_unicode().

Referenced by strtitle_builtin().

◆ strfold_builtin()

static size_t strfold_builtin ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)
static

Definition at line 121 of file pg_locale_builtin.c.

123{
124 return unicode_strfold(dest, destsize, src, srclen,
125 locale->builtin.casemap_full);
126}
static char * locale
Definition: initdb.c:140
size_t unicode_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:189

References generate_unaccent_rules::dest, locale, and unicode_strfold().

◆ strlower_builtin()

static size_t strlower_builtin ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)
static

Definition at line 87 of file pg_locale_builtin.c.

89{
90 return unicode_strlower(dest, destsize, src, srclen,
91 locale->builtin.casemap_full);
92}
size_t unicode_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:101

References generate_unaccent_rules::dest, locale, and unicode_strlower().

◆ strtitle_builtin()

static size_t strtitle_builtin ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)
static

Definition at line 95 of file pg_locale_builtin.c.

97{
98 struct WordBoundaryState wbstate = {
99 .str = src,
100 .len = srclen,
101 .offset = 0,
102 .posix = !locale->builtin.casemap_full,
103 .init = false,
104 .prev_alnum = false,
105 };
106
107 return unicode_strtitle(dest, destsize, src, srclen,
108 locale->builtin.casemap_full,
109 initcap_wbnext, &wbstate);
110}
static size_t initcap_wbnext(void *state)
size_t unicode_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)
Definition: unicode_case.c:138

References generate_unaccent_rules::dest, initcap_wbnext(), locale, WordBoundaryState::str, and unicode_strtitle().

◆ strupper_builtin()

static size_t strupper_builtin ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)
static

Definition at line 113 of file pg_locale_builtin.c.

115{
116 return unicode_strupper(dest, destsize, src, srclen,
117 locale->builtin.casemap_full);
118}
size_t unicode_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:165

References generate_unaccent_rules::dest, locale, and unicode_strupper().

◆ to_char32()

◆ to_pg_wchar()

static pg_wchar to_pg_wchar ( char32_t  c32)
inlinestatic

Definition at line 48 of file pg_locale_builtin.c.

49{
51 return (pg_wchar) c32;
52}
unsigned int pg_wchar
Definition: mbprint.c:31

References Assert(), GetDatabaseEncoding(), and PG_UTF8.

Referenced by wc_tolower_builtin(), and wc_toupper_builtin().

◆ wc_isalnum_builtin()

static bool wc_isalnum_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 141 of file pg_locale_builtin.c.

142{
143 return pg_u_isalnum(to_char32(wc), !locale->builtin.casemap_full);
144}
static char32_t to_char32(pg_wchar wc)

References locale, pg_u_isalnum(), and to_char32().

◆ wc_isalpha_builtin()

static bool wc_isalpha_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 135 of file pg_locale_builtin.c.

136{
137 return pg_u_isalpha(to_char32(wc));
138}
bool pg_u_isalpha(char32_t code)

References pg_u_isalpha(), and to_char32().

◆ wc_isdigit_builtin()

static bool wc_isdigit_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 129 of file pg_locale_builtin.c.

130{
131 return pg_u_isdigit(to_char32(wc), !locale->builtin.casemap_full);
132}
bool pg_u_isdigit(char32_t code, bool posix)

References locale, pg_u_isdigit(), and to_char32().

◆ wc_isgraph_builtin()

static bool wc_isgraph_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 159 of file pg_locale_builtin.c.

160{
161 return pg_u_isgraph(to_char32(wc));
162}
bool pg_u_isgraph(char32_t code)

References pg_u_isgraph(), and to_char32().

◆ wc_islower_builtin()

static bool wc_islower_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 153 of file pg_locale_builtin.c.

154{
155 return pg_u_islower(to_char32(wc));
156}
bool pg_u_islower(char32_t code)

References pg_u_islower(), and to_char32().

◆ wc_isprint_builtin()

static bool wc_isprint_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 165 of file pg_locale_builtin.c.

166{
167 return pg_u_isprint(to_char32(wc));
168}
bool pg_u_isprint(char32_t code)

References pg_u_isprint(), and to_char32().

◆ wc_ispunct_builtin()

static bool wc_ispunct_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 171 of file pg_locale_builtin.c.

172{
173 return pg_u_ispunct(to_char32(wc), !locale->builtin.casemap_full);
174}
bool pg_u_ispunct(char32_t code, bool posix)

References locale, pg_u_ispunct(), and to_char32().

◆ wc_isspace_builtin()

static bool wc_isspace_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 177 of file pg_locale_builtin.c.

178{
179 return pg_u_isspace(to_char32(wc));
180}
bool pg_u_isspace(char32_t code)

References pg_u_isspace(), and to_char32().

◆ wc_isupper_builtin()

static bool wc_isupper_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 147 of file pg_locale_builtin.c.

148{
149 return pg_u_isupper(to_char32(wc));
150}
bool pg_u_isupper(char32_t code)

References pg_u_isupper(), and to_char32().

◆ wc_isxdigit_builtin()

static bool wc_isxdigit_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 183 of file pg_locale_builtin.c.

184{
185 return pg_u_isxdigit(to_char32(wc), !locale->builtin.casemap_full);
186}
bool pg_u_isxdigit(char32_t code, bool posix)

References locale, pg_u_isxdigit(), and to_char32().

◆ wc_tolower_builtin()

static pg_wchar wc_tolower_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 202 of file pg_locale_builtin.c.

203{
205}
static pg_wchar to_pg_wchar(char32_t c32)
char32_t unicode_lowercase_simple(char32_t code)
Definition: unicode_case.c:50

References to_char32(), to_pg_wchar(), and unicode_lowercase_simple().

◆ wc_toupper_builtin()

static pg_wchar wc_toupper_builtin ( pg_wchar  wc,
pg_locale_t  locale 
)
static

Definition at line 196 of file pg_locale_builtin.c.

197{
199}
char32_t unicode_uppercase_simple(char32_t code)
Definition: unicode_case.c:66

References to_char32(), to_pg_wchar(), and unicode_uppercase_simple().

Variable Documentation

◆ ctype_methods_builtin

const struct ctype_methods ctype_methods_builtin
static
Initial value:
= {
.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,
.wc_isxdigit = wc_isxdigit_builtin,
.char_is_cased = char_is_cased_builtin,
.wc_tolower = wc_tolower_builtin,
.wc_toupper = wc_toupper_builtin,
}
static pg_wchar wc_toupper_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isgraph_builtin(pg_wchar wc, pg_locale_t locale)
static pg_wchar wc_tolower_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_islower_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isprint_builtin(pg_wchar wc, pg_locale_t locale)
static size_t strupper_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
static size_t strlower_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
static bool wc_ispunct_builtin(pg_wchar wc, pg_locale_t locale)
static bool char_is_cased_builtin(char ch, pg_locale_t locale)
static bool wc_isdigit_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isupper_builtin(pg_wchar wc, pg_locale_t locale)
static size_t strfold_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
static bool wc_isspace_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isalpha_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isxdigit_builtin(pg_wchar wc, pg_locale_t locale)
static bool wc_isalnum_builtin(pg_wchar wc, pg_locale_t locale)
static size_t strtitle_builtin(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)

Definition at line 207 of file pg_locale_builtin.c.

Referenced by create_pg_locale_builtin().