PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_locale.h File Reference
#include "mb/pg_wchar.h"
Include dependency graph for pg_locale.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  collate_methods
 
struct  ctype_methods
 
struct  pg_locale_struct
 

Macros

#define LOCALE_NAME_BUFLEN   128
 

Typedefs

typedef struct pg_locale_structpg_locale_t
 

Functions

bool check_locale (int category, const char *locale, char **canonname)
 
char * pg_perm_setlocale (int category, const char *locale)
 
struct lconv * PGLC_localeconv (void)
 
void cache_locale_time (void)
 
void init_database_collation (void)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
bool char_is_cased (char ch, pg_locale_t locale)
 
bool char_tolower_enabled (pg_locale_t locale)
 
char char_tolower (unsigned char ch, pg_locale_t locale)
 
size_t pg_strlower (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strtitle (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strupper (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strfold (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
int pg_strcoll (const char *arg1, const char *arg2, pg_locale_t locale)
 
int pg_strncoll (const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
 
bool pg_strxfrm_enabled (pg_locale_t locale)
 
size_t pg_strxfrm (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
bool pg_strxfrm_prefix_enabled (pg_locale_t locale)
 
size_t pg_strxfrm_prefix (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm_prefix (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
int builtin_locale_encoding (const char *locale)
 
const char * builtin_validate_locale (int encoding, const char *locale)
 
void icu_validate_locale (const char *loc_str)
 
char * icu_language_tag (const char *loc_str, int elevel)
 
void report_newlocale_failure (const char *localename)
 
size_t wchar2char (char *to, const wchar_t *from, size_t tolen, locale_t loc)
 
size_t char2wchar (wchar_t *to, size_t tolen, const char *from, size_t fromlen, locale_t loc)
 

Variables

PGDLLIMPORT char * locale_messages
 
PGDLLIMPORT char * locale_monetary
 
PGDLLIMPORT char * locale_numeric
 
PGDLLIMPORT char * locale_time
 
PGDLLIMPORT int icu_validation_level
 
PGDLLIMPORT char * localized_abbrev_days []
 
PGDLLIMPORT char * localized_full_days []
 
PGDLLIMPORT char * localized_abbrev_months []
 
PGDLLIMPORT char * localized_full_months []
 
PGDLLIMPORT bool database_ctype_is_c
 

Macro Definition Documentation

◆ LOCALE_NAME_BUFLEN

#define LOCALE_NAME_BUFLEN   128

Definition at line 27 of file pg_locale.h.

Typedef Documentation

◆ pg_locale_t

typedef struct pg_locale_struct* pg_locale_t

Definition at line 58 of file pg_locale.h.

Function Documentation

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1438 of file pg_locale.c.

1439{
1440 if (strcmp(locale, "C") == 0)
1441 return -1;
1442 else if (strcmp(locale, "C.UTF-8") == 0)
1443 return PG_UTF8;
1444 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1445 return PG_UTF8;
1446
1447
1448 ereport(ERROR,
1449 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1450 errmsg("invalid locale name \"%s\" for builtin provider",
1451 locale)));
1452
1453 return 0; /* keep compiler quiet */
1454}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static char * locale
Definition: initdb.c:140
@ PG_UTF8
Definition: pg_wchar.h:232

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

Referenced by builtin_validate_locale(), and DefineCollation().

◆ builtin_validate_locale()

const char * builtin_validate_locale ( int  encoding,
const char *  locale 
)

Definition at line 1462 of file pg_locale.c.

1463{
1464 const char *canonical_name = NULL;
1465 int required_encoding;
1466
1467 if (strcmp(locale, "C") == 0)
1468 canonical_name = "C";
1469 else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1470 canonical_name = "C.UTF-8";
1471 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1472 canonical_name = "PG_UNICODE_FAST";
1473
1474 if (!canonical_name)
1475 ereport(ERROR,
1476 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1477 errmsg("invalid locale name \"%s\" for builtin provider",
1478 locale)));
1479
1480 required_encoding = builtin_locale_encoding(canonical_name);
1481 if (required_encoding >= 0 && encoding != required_encoding)
1482 ereport(ERROR,
1483 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1484 errmsg("encoding \"%s\" does not match locale \"%s\"",
1486
1487 return canonical_name;
1488}
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1438
#define pg_encoding_to_char
Definition: pg_wchar.h:630

References builtin_locale_encoding(), encoding, ereport, errcode(), errmsg(), ERROR, locale, and pg_encoding_to_char.

Referenced by create_pg_locale_builtin(), createdb(), and DefineCollation().

◆ cache_locale_time()

void cache_locale_time ( void  )

Definition at line 695 of file pg_locale.c.

696{
697 char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
698 char *bufptr;
699 time_t timenow;
700 struct tm *timeinfo;
701 struct tm timeinfobuf;
702 bool strftimefail = false;
703 int encoding;
704 int i;
706
707 /* did we do this already? */
709 return;
710
711 elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
712
713 errno = ENOENT;
714#ifdef WIN32
715 locale = _create_locale(LC_ALL, locale_time);
716 if (locale == (locale_t) 0)
717 _dosmaperr(GetLastError());
718#else
719 locale = newlocale(LC_ALL_MASK, locale_time, (locale_t) 0);
720#endif
721 if (!locale)
723
724 /* We use times close to current time as data for strftime(). */
725 timenow = time(NULL);
726 timeinfo = gmtime_r(&timenow, &timeinfobuf);
727
728 /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
729 bufptr = buf;
730
731 /*
732 * MAX_L10N_DATA is sufficient buffer space for every known locale, and
733 * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
734 * error.) An implementation might report errors (e.g. ENOMEM) by
735 * returning 0 (or, less plausibly, a negative value) and setting errno.
736 * Report errno just in case the implementation did that, but clear it in
737 * advance of the calls so we don't emit a stale, unrelated errno.
738 */
739 errno = 0;
740
741 /* localized days */
742 for (i = 0; i < 7; i++)
743 {
744 timeinfo->tm_wday = i;
745 if (strftime_l(bufptr, MAX_L10N_DATA, "%a", timeinfo, locale) <= 0)
746 strftimefail = true;
747 bufptr += MAX_L10N_DATA;
748 if (strftime_l(bufptr, MAX_L10N_DATA, "%A", timeinfo, locale) <= 0)
749 strftimefail = true;
750 bufptr += MAX_L10N_DATA;
751 }
752
753 /* localized months */
754 for (i = 0; i < 12; i++)
755 {
756 timeinfo->tm_mon = i;
757 timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
758 if (strftime_l(bufptr, MAX_L10N_DATA, "%b", timeinfo, locale) <= 0)
759 strftimefail = true;
760 bufptr += MAX_L10N_DATA;
761 if (strftime_l(bufptr, MAX_L10N_DATA, "%B", timeinfo, locale) <= 0)
762 strftimefail = true;
763 bufptr += MAX_L10N_DATA;
764 }
765
766#ifdef WIN32
767 _free_locale(locale);
768#else
769 freelocale(locale);
770#endif
771
772 /*
773 * At this point we've done our best to clean up, and can throw errors, or
774 * call functions that might throw errors, with a clean conscience.
775 */
776 if (strftimefail)
777 elog(ERROR, "strftime_l() failed");
778
779#ifndef WIN32
780
781 /*
782 * As in PGLC_localeconv(), we must convert strftime()'s output from the
783 * encoding implied by LC_TIME to the database encoding. If we can't
784 * identify the LC_TIME encoding, just perform encoding validation.
785 */
787 if (encoding < 0)
789
790#else
791
792 /*
793 * On Windows, strftime_win32() always returns UTF8 data, so convert from
794 * that if necessary.
795 */
797
798#endif /* WIN32 */
799
800 bufptr = buf;
801
802 /* localized days */
803 for (i = 0; i < 7; i++)
804 {
806 bufptr += MAX_L10N_DATA;
808 bufptr += MAX_L10N_DATA;
809 }
810 localized_abbrev_days[7] = NULL;
811 localized_full_days[7] = NULL;
812
813 /* localized months */
814 for (i = 0; i < 12; i++)
815 {
817 bufptr += MAX_L10N_DATA;
819 bufptr += MAX_L10N_DATA;
820 }
821 localized_abbrev_months[12] = NULL;
822 localized_full_months[12] = NULL;
823
824 CurrentLCTimeValid = true;
825}
#define DEBUG3
Definition: elog.h:28
#define elog(elevel,...)
Definition: elog.h:226
int i
Definition: isn.c:77
static struct pg_tm tm
Definition: localtime.c:104
char * localized_full_months[12+1]
Definition: pg_locale.c:100
static bool CurrentLCTimeValid
Definition: pg_locale.c:109
char * locale_time
Definition: pg_locale.c:86
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:672
#define MAX_L10N_DATA
Definition: pg_locale.c:65
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:99
char * localized_full_days[7+1]
Definition: pg_locale.c:98
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:97
void report_newlocale_failure(const char *localename)
static char * buf
Definition: pg_test_fsync.c:72
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301
#define locale_t
Definition: win32_port.h:432
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), buf, cache_single_string(), CurrentLCTimeValid, DEBUG3, elog, encoding, ERROR, i, locale, locale_t, locale_time, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, MAX_L10N_DATA, pg_get_encoding_from_locale(), PG_SQL_ASCII, PG_UTF8, report_newlocale_failure(), and tm.

Referenced by DCH_from_char(), and DCH_to_char().

◆ char2wchar()

size_t char2wchar ( wchar_t *  to,
size_t  tolen,
const char *  from,
size_t  fromlen,
locale_t  loc 
)

Definition at line 1187 of file pg_locale_libc.c.

1189{
1190 size_t result;
1191
1192 if (tolen == 0)
1193 return 0;
1194
1195#ifdef WIN32
1196 /* See WIN32 "Unicode" comment above */
1198 {
1199 /* Win32 API does not work for zero-length input */
1200 if (fromlen == 0)
1201 result = 0;
1202 else
1203 {
1204 result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
1205 /* A zero return is failure */
1206 if (result == 0)
1207 result = -1;
1208 }
1209
1210 if (result != -1)
1211 {
1212 Assert(result < tolen);
1213 /* Append trailing null wchar (MultiByteToWideChar() does not) */
1214 to[result] = 0;
1215 }
1216 }
1217 else
1218#endif /* WIN32 */
1219 {
1220 /* mbstowcs requires ending '\0' */
1221 char *str = pnstrdup(from, fromlen);
1222
1223 if (loc == (locale_t) 0)
1224 {
1225 /* Use mbstowcs directly for the default locale */
1226 result = mbstowcs(to, str, tolen);
1227 }
1228 else
1229 {
1230 /* Use mbstowcs_l for nondefault locales */
1231 result = mbstowcs_l(to, str, tolen, loc);
1232 }
1233
1234 pfree(str);
1235 }
1236
1237 if (result == -1)
1238 {
1239 /*
1240 * Invalid multibyte character encountered. We try to give a useful
1241 * error message by letting pg_verifymbstr check the string. But it's
1242 * possible that the string is OK to us, and not OK to mbstowcs ---
1243 * this suggests that the LC_CTYPE locale is different from the
1244 * database encoding. Give a generic error message if pg_verifymbstr
1245 * can't find anything wrong.
1246 */
1247 pg_verifymbstr(from, fromlen, false); /* might not return */
1248 /* but if it does ... */
1249 ereport(ERROR,
1250 (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1251 errmsg("invalid multibyte character for locale"),
1252 errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
1253 }
1254
1255 return result;
1256}
int errhint(const char *fmt,...)
Definition: elog.c:1321
Assert(PointerIsAligned(start, uint64))
const char * str
int GetDatabaseEncoding(void)
Definition: mbutils.c:1262
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1557
void pfree(void *pointer)
Definition: mcxt.c:1594
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1770
static size_t mbstowcs_l(wchar_t *dest, const char *src, size_t n, locale_t loc)

References Assert(), ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), locale_t, mbstowcs_l(), pfree(), PG_UTF8, pg_verifymbstr(), pnstrdup(), and str.

Referenced by strlower_libc_mb(), strtitle_libc_mb(), strupper_libc_mb(), t_isalnum(), t_isalpha(), and TParserInit().

◆ char_is_cased()

bool char_is_cased ( char  ch,
pg_locale_t  locale 
)

Definition at line 1406 of file pg_locale.c.

1407{
1408 return locale->ctype->char_is_cased(ch, locale);
1409}

References locale.

Referenced by pattern_char_isalpha().

◆ char_tolower()

char char_tolower ( unsigned char  ch,
pg_locale_t  locale 
)

Definition at line 1428 of file pg_locale.c.

1429{
1430 return locale->ctype->char_tolower(ch, locale);
1431}

References locale.

Referenced by SB_lower_char().

◆ char_tolower_enabled()

bool char_tolower_enabled ( pg_locale_t  locale)

Definition at line 1417 of file pg_locale.c.

1418{
1419 return (locale->ctype->char_tolower != NULL);
1420}

References locale.

Referenced by Generic_Text_IC_like().

◆ check_locale()

bool check_locale ( int  category,
const char *  locale,
char **  canonname 
)

Definition at line 268 of file pg_locale.c.

269{
270 char *save;
271 char *res;
272
273 /* Don't let Windows' non-ASCII locale names in. */
274 if (!pg_is_ascii(locale))
275 {
277 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
278 errmsg("locale name \"%s\" contains non-ASCII characters",
279 locale)));
280 return false;
281 }
282
283 if (canonname)
284 *canonname = NULL; /* in case of failure */
285
286 save = setlocale(category, NULL);
287 if (!save)
288 return false; /* won't happen, we hope */
289
290 /* save may be pointing at a modifiable scratch variable, see above. */
291 save = pstrdup(save);
292
293 /* set the locale with setlocale, to see if it accepts it. */
294 res = setlocale(category, locale);
295
296 /* save canonical name if requested. */
297 if (res && canonname)
298 *canonname = pstrdup(res);
299
300 /* restore old value. */
301 if (!setlocale(category, save))
302 elog(WARNING, "failed to restore old locale \"%s\"", save);
303 pfree(save);
304
305 /* Don't let Windows' non-ASCII locale names out. */
306 if (canonname && *canonname && !pg_is_ascii(*canonname))
307 {
309 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
310 errmsg("locale name \"%s\" contains non-ASCII characters",
311 *canonname)));
312 pfree(*canonname);
313 *canonname = NULL;
314 return false;
315 }
316
317 return (res != NULL);
318}
#define WARNING
Definition: elog.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:1759
bool pg_is_ascii(const char *str)
Definition: string.c:132
#define setlocale(a, b)
Definition: win32_port.h:475

References elog, ereport, errcode(), errmsg(), locale, pfree(), pg_is_ascii(), pstrdup(), setlocale, and WARNING.

Referenced by check_locale_messages(), check_locale_monetary(), check_locale_numeric(), check_locale_time(), and createdb().

◆ get_collation_actual_version()

char * get_collation_actual_version ( char  collprovider,
const char *  collcollate 
)

Definition at line 1217 of file pg_locale.c.

1218{
1219 char *collversion = NULL;
1220
1221 if (collprovider == COLLPROVIDER_BUILTIN)
1222 collversion = get_collation_actual_version_builtin(collcollate);
1223#ifdef USE_ICU
1224 else if (collprovider == COLLPROVIDER_ICU)
1225 collversion = get_collation_actual_version_icu(collcollate);
1226#endif
1227 else if (collprovider == COLLPROVIDER_LIBC)
1228 collversion = get_collation_actual_version_libc(collcollate);
1229
1230 return collversion;
1231}
char * get_collation_actual_version_libc(const char *collcollate)
char * get_collation_actual_version_builtin(const char *collcollate)

References get_collation_actual_version_builtin(), and get_collation_actual_version_libc().

Referenced by AlterCollation(), AlterDatabaseRefreshColl(), CheckMyDatabase(), create_pg_locale(), createdb(), DefineCollation(), pg_collation_actual_version(), pg_database_collation_actual_version(), and pg_import_system_collations().

◆ icu_language_tag()

char * icu_language_tag ( const char *  loc_str,
int  elevel 
)

Definition at line 1502 of file pg_locale.c.

1503{
1504#ifdef USE_ICU
1505 UErrorCode status;
1506 char *langtag;
1507 size_t buflen = 32; /* arbitrary starting buffer size */
1508 const bool strict = true;
1509
1510 /*
1511 * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1512 * RFC5646 section 4.4). Additionally, in older ICU versions,
1513 * uloc_toLanguageTag() doesn't always return the ultimate length on the
1514 * first call, necessitating a loop.
1515 */
1516 langtag = palloc(buflen);
1517 while (true)
1518 {
1519 status = U_ZERO_ERROR;
1520 uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1521
1522 /* try again if the buffer is not large enough */
1523 if ((status == U_BUFFER_OVERFLOW_ERROR ||
1524 status == U_STRING_NOT_TERMINATED_WARNING) &&
1525 buflen < MaxAllocSize)
1526 {
1527 buflen = Min(buflen * 2, MaxAllocSize);
1528 langtag = repalloc(langtag, buflen);
1529 continue;
1530 }
1531
1532 break;
1533 }
1534
1535 if (U_FAILURE(status))
1536 {
1537 pfree(langtag);
1538
1539 if (elevel > 0)
1540 ereport(elevel,
1541 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1542 errmsg("could not convert locale name \"%s\" to language tag: %s",
1543 loc_str, u_errorName(status))));
1544 return NULL;
1545 }
1546
1547 return langtag;
1548#else /* not USE_ICU */
1549 ereport(ERROR,
1550 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1551 errmsg("ICU is not supported in this build")));
1552 return NULL; /* keep compiler quiet */
1553#endif /* not USE_ICU */
1554}
#define Min(x, y)
Definition: c.h:1003
#define MaxAllocSize
Definition: fe_memutils.h:22
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1610
void * palloc(Size size)
Definition: mcxt.c:1365

References ereport, errcode(), errmsg(), ERROR, MaxAllocSize, Min, palloc(), pfree(), and repalloc().

Referenced by createdb(), DefineCollation(), and pg_import_system_collations().

◆ icu_validate_locale()

void icu_validate_locale ( const char *  loc_str)

Definition at line 1560 of file pg_locale.c.

1561{
1562#ifdef USE_ICU
1563 UCollator *collator;
1564 UErrorCode status;
1565 char lang[ULOC_LANG_CAPACITY];
1566 bool found = false;
1567 int elevel = icu_validation_level;
1568
1569 /* no validation */
1570 if (elevel < 0)
1571 return;
1572
1573 /* downgrade to WARNING during pg_upgrade */
1574 if (IsBinaryUpgrade && elevel > WARNING)
1575 elevel = WARNING;
1576
1577 /* validate that we can extract the language */
1578 status = U_ZERO_ERROR;
1579 uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1580 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1581 {
1582 ereport(elevel,
1583 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1584 errmsg("could not get language from ICU locale \"%s\": %s",
1585 loc_str, u_errorName(status)),
1586 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1587 "icu_validation_level", "disabled")));
1588 return;
1589 }
1590
1591 /* check for special language name */
1592 if (strcmp(lang, "") == 0 ||
1593 strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1594 found = true;
1595
1596 /* search for matching language within ICU */
1597 for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1598 {
1599 const char *otherloc = uloc_getAvailable(i);
1600 char otherlang[ULOC_LANG_CAPACITY];
1601
1602 status = U_ZERO_ERROR;
1603 uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1604 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1605 continue;
1606
1607 if (strcmp(lang, otherlang) == 0)
1608 found = true;
1609 }
1610
1611 if (!found)
1612 ereport(elevel,
1613 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1614 errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1615 loc_str, lang),
1616 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1617 "icu_validation_level", "disabled")));
1618
1619 /* check that it can be opened */
1620 collator = pg_ucol_open(loc_str);
1621 ucol_close(collator);
1622#else /* not USE_ICU */
1623 /* could get here if a collation was created by a build with ICU */
1624 ereport(ERROR,
1625 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1626 errmsg("ICU is not supported in this build")));
1627#endif /* not USE_ICU */
1628}
bool IsBinaryUpgrade
Definition: globals.c:121
int icu_validation_level
Definition: pg_locale.c:88

References ereport, errcode(), errhint(), errmsg(), ERROR, i, icu_validation_level, IsBinaryUpgrade, and WARNING.

Referenced by createdb(), and DefineCollation().

◆ init_database_collation()

void init_database_collation ( void  )

Definition at line 1124 of file pg_locale.c.

1125{
1126 HeapTuple tup;
1127 Form_pg_database dbform;
1128 pg_locale_t result;
1129
1130 Assert(default_locale == NULL);
1131
1132 /* Fetch our pg_database row normally, via syscache */
1133 tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1134 if (!HeapTupleIsValid(tup))
1135 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1136 dbform = (Form_pg_database) GETSTRUCT(tup);
1137
1138 if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1139 result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
1141 else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1142 result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
1144 else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1145 result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
1147 else
1148 /* shouldn't happen */
1149 PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1150
1151 result->is_default = true;
1152 ReleaseSysCache(tup);
1153
1154 default_locale = result;
1155}
Oid MyDatabaseId
Definition: globals.c:94
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
MemoryContext TopMemoryContext
Definition: mcxt.c:166
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
pg_locale_t create_pg_locale_libc(Oid collid, MemoryContext context)
pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context)
#define PGLOCALE_SUPPORT_ERROR(provider)
Definition: pg_locale.c:56
pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context)
static pg_locale_t default_locale
Definition: pg_locale.c:105
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References Assert(), create_pg_locale_builtin(), create_pg_locale_icu(), create_pg_locale_libc(), default_locale, elog, ERROR, GETSTRUCT(), HeapTupleIsValid, pg_locale_struct::is_default, MyDatabaseId, ObjectIdGetDatum(), PGLOCALE_SUPPORT_ERROR, ReleaseSysCache(), SearchSysCache1(), and TopMemoryContext.

Referenced by CheckMyDatabase().

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1166 of file pg_locale.c.

1167{
1168 collation_cache_entry *cache_entry;
1169 bool found;
1170
1171 if (collid == DEFAULT_COLLATION_OID)
1172 return default_locale;
1173
1174 if (!OidIsValid(collid))
1175 elog(ERROR, "cache lookup failed for collation %u", collid);
1176
1178
1181
1182 if (CollationCache == NULL)
1183 {
1185 "collation cache",
1187 CollationCache = collation_cache_create(CollationCacheContext,
1188 16, NULL);
1189 }
1190
1191 cache_entry = collation_cache_insert(CollationCache, collid, &found);
1192 if (!found)
1193 {
1194 /*
1195 * Make sure cache entry is marked invalid, in case we fail before
1196 * setting things.
1197 */
1198 cache_entry->locale = 0;
1199 }
1200
1201 if (cache_entry->locale == 0)
1202 {
1204 }
1205
1207 last_collation_cache_locale = cache_entry->locale;
1208
1209 return cache_entry->locale;
1210}
#define OidIsValid(objectId)
Definition: c.h:774
Oid collid
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static pg_locale_t last_collation_cache_locale
Definition: pg_locale.c:144
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1042
static MemoryContext CollationCacheContext
Definition: pg_locale.c:136
static collation_cache_hash * CollationCache
Definition: pg_locale.c:137
static Oid last_collation_cache_oid
Definition: pg_locale.c:143
static void AssertCouldGetRelation(void)
Definition: relcache.h:44
Definition: pg_locale.c:114
pg_locale_t locale
Definition: pg_locale.c:116

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AssertCouldGetRelation(), CollationCache, CollationCacheContext, collid, create_pg_locale(), default_locale, elog, ERROR, last_collation_cache_locale, last_collation_cache_oid, collation_cache_entry::locale, OidIsValid, and TopMemoryContext.

Referenced by bpchareq(), bpcharne(), btvarstrequalimage(), convert_string_datum(), DefineCollation(), Generic_Text_IC_like(), GenericMatchText(), hashbpchar(), hashbpcharextended(), hashtext(), hashtextextended(), like_fixed_prefix(), make_greater_string(), match_pattern_prefix(), pg_set_regex_collation(), spg_text_inner_consistent(), str_casefold(), str_initcap(), str_tolower(), str_toupper(), text_position(), text_position_setup(), text_starts_with(), texteq(), textne(), varstr_cmp(), and varstr_sortsupport().

◆ pg_perm_setlocale()

char * pg_perm_setlocale ( int  category,
const char *  locale 
)

Definition at line 165 of file pg_locale.c.

166{
167 char *result;
168 const char *envvar;
169
170#ifndef WIN32
171 result = setlocale(category, locale);
172#else
173
174 /*
175 * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
176 * the given value is good and set it in the environment variables. We
177 * must ignore attempts to set to "", which means "keep using the old
178 * environment value".
179 */
180#ifdef LC_MESSAGES
181 if (category == LC_MESSAGES)
182 {
183 result = (char *) locale;
184 if (locale == NULL || locale[0] == '\0')
185 return result;
186 }
187 else
188#endif
189 result = setlocale(category, locale);
190#endif /* WIN32 */
191
192 if (result == NULL)
193 return result; /* fall out immediately on failure */
194
195 /*
196 * Use the right encoding in translated messages. Under ENABLE_NLS, let
197 * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
198 * format strings are ASCII, but database-encoding strings may enter the
199 * message via %s. This makes the overall message encoding equal to the
200 * database encoding.
201 */
202 if (category == LC_CTYPE)
203 {
204 static char save_lc_ctype[LOCALE_NAME_BUFLEN];
205
206 /* copy setlocale() return value before callee invokes it again */
207 strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
208 result = save_lc_ctype;
209
210#ifdef ENABLE_NLS
211 SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
212#else
214#endif
215 }
216
217 switch (category)
218 {
219 case LC_COLLATE:
220 envvar = "LC_COLLATE";
221 break;
222 case LC_CTYPE:
223 envvar = "LC_CTYPE";
224 break;
225#ifdef LC_MESSAGES
226 case LC_MESSAGES:
227 envvar = "LC_MESSAGES";
228#ifdef WIN32
229 result = IsoLocaleName(locale);
230 if (result == NULL)
231 result = (char *) locale;
232 elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
233#endif /* WIN32 */
234 break;
235#endif /* LC_MESSAGES */
236 case LC_MONETARY:
237 envvar = "LC_MONETARY";
238 break;
239 case LC_NUMERIC:
240 envvar = "LC_NUMERIC";
241 break;
242 case LC_TIME:
243 envvar = "LC_TIME";
244 break;
245 default:
246 elog(FATAL, "unrecognized LC category: %d", category);
247 return NULL; /* keep compiler quiet */
248 }
249
250 if (setenv(envvar, result, 1) != 0)
251 return NULL;
252
253 return result;
254}
#define FATAL
Definition: elog.h:41
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1172
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:27
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define setenv(x, y, z)
Definition: win32_port.h:545

References DEBUG3, elog, FATAL, GetDatabaseEncoding(), locale, LOCALE_NAME_BUFLEN, setenv, setlocale, SetMessageEncoding(), and strlcpy().

Referenced by assign_locale_messages(), CheckMyDatabase(), and init_locale().

◆ pg_strcoll()

int pg_strcoll ( const char *  arg1,
const char *  arg2,
pg_locale_t  locale 
)

Definition at line 1270 of file pg_locale.c.

1271{
1272 return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
1273}

References locale.

Referenced by varstrfastcmp_locale().

◆ pg_strfold()

size_t pg_strfold ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1255 of file pg_locale.c.

1257{
1258 if (locale->ctype->strfold)
1259 return locale->ctype->strfold(dst, dstsize, src, srclen, locale);
1260 else
1261 return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1262}

References locale.

Referenced by str_casefold().

◆ pg_strlower()

size_t pg_strlower ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1234 of file pg_locale.c.

1236{
1237 return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1238}

References locale.

Referenced by str_tolower().

◆ pg_strncoll()

int pg_strncoll ( const char *  arg1,
ssize_t  len1,
const char *  arg2,
ssize_t  len2,
pg_locale_t  locale 
)

Definition at line 1290 of file pg_locale.c.

1292{
1293 return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
1294}

References locale.

Referenced by MatchText(), text_position_next_internal(), and varstr_cmp().

◆ pg_strnxfrm()

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

Definition at line 1345 of file pg_locale.c.

1347{
1348 return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
1349}

References generate_unaccent_rules::dest, and locale.

Referenced by hashbpchar(), hashbpcharextended(), hashtext(), and hashtextextended().

◆ pg_strnxfrm_prefix()

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

Definition at line 1392 of file pg_locale.c.

1394{
1395 return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
1396}

References generate_unaccent_rules::dest, and locale.

◆ pg_strtitle()

size_t pg_strtitle ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1241 of file pg_locale.c.

1243{
1244 return locale->ctype->strtitle(dst, dstsize, src, srclen, locale);
1245}

References locale.

Referenced by str_initcap().

◆ pg_strupper()

size_t pg_strupper ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1248 of file pg_locale.c.

1250{
1251 return locale->ctype->strupper(dst, dstsize, src, srclen, locale);
1252}

References locale.

Referenced by str_toupper().

◆ pg_strxfrm()

size_t pg_strxfrm ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1320 of file pg_locale.c.

1321{
1322 return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
1323}

References generate_unaccent_rules::dest, and locale.

Referenced by convert_string_datum(), and varstr_abbrev_convert().

◆ pg_strxfrm_enabled()

bool pg_strxfrm_enabled ( pg_locale_t  locale)

Definition at line 1304 of file pg_locale.c.

1305{
1306 /*
1307 * locale->collate->strnxfrm is still a required method, even if it may
1308 * have the wrong behavior, because the planner uses it for estimates in
1309 * some cases.
1310 */
1311 return locale->collate->strxfrm_is_safe;
1312}

References locale.

Referenced by varstr_sortsupport().

◆ pg_strxfrm_prefix()

size_t pg_strxfrm_prefix ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1367 of file pg_locale.c.

1369{
1370 return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
1371}

References generate_unaccent_rules::dest, and locale.

Referenced by varstr_abbrev_convert().

◆ pg_strxfrm_prefix_enabled()

bool pg_strxfrm_prefix_enabled ( pg_locale_t  locale)

Definition at line 1356 of file pg_locale.c.

1357{
1358 return (locale->collate->strnxfrm_prefix != NULL);
1359}

References locale.

Referenced by varstr_abbrev_convert().

◆ PGLC_localeconv()

struct lconv * PGLC_localeconv ( void  )

Definition at line 499 of file pg_locale.c.

500{
501 static struct lconv CurrentLocaleConv;
502 static bool CurrentLocaleConvAllocated = false;
503 struct lconv *extlconv;
504 struct lconv tmp;
505 struct lconv worklconv = {0};
506
507 /* Did we do it already? */
509 return &CurrentLocaleConv;
510
511 /* Free any already-allocated storage */
512 if (CurrentLocaleConvAllocated)
513 {
514 free_struct_lconv(&CurrentLocaleConv);
515 CurrentLocaleConvAllocated = false;
516 }
517
518 /*
519 * Use thread-safe method of obtaining a copy of lconv from the operating
520 * system.
521 */
524 &tmp) != 0)
525 elog(ERROR,
526 "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
528
529 /* Must copy data now so we can re-encode it. */
530 extlconv = &tmp;
531 worklconv.decimal_point = strdup(extlconv->decimal_point);
532 worklconv.thousands_sep = strdup(extlconv->thousands_sep);
533 worklconv.grouping = strdup(extlconv->grouping);
534 worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
535 worklconv.currency_symbol = strdup(extlconv->currency_symbol);
536 worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
537 worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
538 worklconv.mon_grouping = strdup(extlconv->mon_grouping);
539 worklconv.positive_sign = strdup(extlconv->positive_sign);
540 worklconv.negative_sign = strdup(extlconv->negative_sign);
541 /* Copy scalar fields as well */
542 worklconv.int_frac_digits = extlconv->int_frac_digits;
543 worklconv.frac_digits = extlconv->frac_digits;
544 worklconv.p_cs_precedes = extlconv->p_cs_precedes;
545 worklconv.p_sep_by_space = extlconv->p_sep_by_space;
546 worklconv.n_cs_precedes = extlconv->n_cs_precedes;
547 worklconv.n_sep_by_space = extlconv->n_sep_by_space;
548 worklconv.p_sign_posn = extlconv->p_sign_posn;
549 worklconv.n_sign_posn = extlconv->n_sign_posn;
550
551 /* Free the contents of the object populated by pg_localeconv_r(). */
552 pg_localeconv_free(&tmp);
553
554 /* If any of the preceding strdup calls failed, complain now. */
555 if (!struct_lconv_is_valid(&worklconv))
557 (errcode(ERRCODE_OUT_OF_MEMORY),
558 errmsg("out of memory")));
559
560 PG_TRY();
561 {
562 int encoding;
563
564 /*
565 * Now we must perform encoding conversion from whatever's associated
566 * with the locales into the database encoding. If we can't identify
567 * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
568 * use PG_SQL_ASCII, which will result in just validating that the
569 * strings are OK in the database encoding.
570 */
572 if (encoding < 0)
574
575 db_encoding_convert(encoding, &worklconv.decimal_point);
576 db_encoding_convert(encoding, &worklconv.thousands_sep);
577 /* grouping is not text and does not require conversion */
578
580 if (encoding < 0)
582
583 db_encoding_convert(encoding, &worklconv.int_curr_symbol);
584 db_encoding_convert(encoding, &worklconv.currency_symbol);
585 db_encoding_convert(encoding, &worklconv.mon_decimal_point);
586 db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
587 /* mon_grouping is not text and does not require conversion */
588 db_encoding_convert(encoding, &worklconv.positive_sign);
589 db_encoding_convert(encoding, &worklconv.negative_sign);
590 }
591 PG_CATCH();
592 {
593 free_struct_lconv(&worklconv);
594 PG_RE_THROW();
595 }
596 PG_END_TRY();
597
598 /*
599 * Everything is good, so save the results.
600 */
601 CurrentLocaleConv = worklconv;
602 CurrentLocaleConvAllocated = true;
604 return &CurrentLocaleConv;
605}
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
char * locale_numeric
Definition: pg_locale.c:85
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:469
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:419
static bool CurrentLocaleConvValid
Definition: pg_locale.c:108
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:438
char * locale_monetary
Definition: pg_locale.c:84
int pg_localeconv_r(const char *lc_monetary, const char *lc_numeric, struct lconv *output)
void pg_localeconv_free(struct lconv *lconv)

References CurrentLocaleConvValid, db_encoding_convert(), elog, encoding, ereport, errcode(), errmsg(), ERROR, free_struct_lconv(), locale_monetary, locale_numeric, PG_CATCH, PG_END_TRY, pg_get_encoding_from_locale(), pg_localeconv_free(), pg_localeconv_r(), PG_RE_THROW, PG_SQL_ASCII, PG_TRY, and struct_lconv_is_valid().

Referenced by cash_in(), cash_numeric(), cash_out(), int4_cash(), int8_cash(), NUM_prepare_locale(), and numeric_cash().

◆ report_newlocale_failure()

void report_newlocale_failure ( const char *  localename)

Definition at line 1055 of file pg_locale_libc.c.

1056{
1057 int save_errno;
1058
1059 /*
1060 * Windows doesn't provide any useful error indication from
1061 * _create_locale(), and BSD-derived platforms don't seem to feel they
1062 * need to set errno either (even though POSIX is pretty clear that
1063 * newlocale should do so). So, if errno hasn't been set, assume ENOENT
1064 * is what to report.
1065 */
1066 if (errno == 0)
1067 errno = ENOENT;
1068
1069 /*
1070 * ENOENT means "no such locale", not "no such file", so clarify that
1071 * errno with an errdetail message.
1072 */
1073 save_errno = errno; /* auxiliary funcs might change errno */
1074 ereport(ERROR,
1075 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1076 errmsg("could not create locale \"%s\": %m",
1077 localename),
1078 (save_errno == ENOENT ?
1079 errdetail("The operating system could not find any locale data for the locale name \"%s\".",
1080 localename) : 0)));
1081}
int errdetail(const char *fmt,...)
Definition: elog.c:1207

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

Referenced by cache_locale_time(), and make_libc_collator().

◆ wchar2char()

size_t wchar2char ( char *  to,
const wchar_t *  from,
size_t  tolen,
locale_t  loc 
)

Definition at line 1133 of file pg_locale_libc.c.

1134{
1135 size_t result;
1136
1137 if (tolen == 0)
1138 return 0;
1139
1140#ifdef WIN32
1141
1142 /*
1143 * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
1144 * for some reason mbstowcs and wcstombs won't do this for us, so we use
1145 * MultiByteToWideChar().
1146 */
1148 {
1149 result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
1150 NULL, NULL);
1151 /* A zero return is failure */
1152 if (result <= 0)
1153 result = -1;
1154 else
1155 {
1156 Assert(result <= tolen);
1157 /* Microsoft counts the zero terminator in the result */
1158 result--;
1159 }
1160 }
1161 else
1162#endif /* WIN32 */
1163 if (loc == (locale_t) 0)
1164 {
1165 /* Use wcstombs directly for the default locale */
1166 result = wcstombs(to, from, tolen);
1167 }
1168 else
1169 {
1170 /* Use wcstombs_l for nondefault locales */
1171 result = wcstombs_l(to, from, tolen, loc);
1172 }
1173
1174 return result;
1175}
static size_t wcstombs_l(char *dest, const wchar_t *src, size_t n, locale_t loc)

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

Referenced by strlower_libc_mb(), strtitle_libc_mb(), and strupper_libc_mb().

Variable Documentation

◆ database_ctype_is_c

PGDLLIMPORT bool database_ctype_is_c
extern

Definition at line 103 of file pg_locale.c.

Referenced by CheckMyDatabase(), t_isalnum(), t_isalpha(), and TParserInit().

◆ icu_validation_level

PGDLLIMPORT int icu_validation_level
extern

Definition at line 88 of file pg_locale.c.

Referenced by createdb(), DefineCollation(), and icu_validate_locale().

◆ locale_messages

PGDLLIMPORT char* locale_messages
extern

Definition at line 83 of file pg_locale.c.

◆ locale_monetary

PGDLLIMPORT char* locale_monetary
extern

Definition at line 84 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

PGDLLIMPORT char* locale_numeric
extern

Definition at line 85 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

PGDLLIMPORT char* locale_time
extern

Definition at line 86 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

PGDLLIMPORT char* localized_abbrev_days[]
extern

Definition at line 97 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_abbrev_months

PGDLLIMPORT char* localized_abbrev_months[]
extern

Definition at line 99 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_days

PGDLLIMPORT char* localized_full_days[]
extern

Definition at line 98 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_months

PGDLLIMPORT char* localized_full_months[]
extern

Definition at line 100 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().