PostgreSQL Source Code git master
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
 
#define UNICODE_CASEMAP_LEN   3
 
#define UNICODE_CASEMAP_BUFSZ   (UNICODE_CASEMAP_LEN * MAX_MULTIBYTE_CHAR_LEN)
 

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_database_locale (void)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
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)
 
size_t pg_downcase_ident (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
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)
 
bool pg_iswdigit (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswalpha (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswalnum (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswupper (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswlower (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswgraph (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswprint (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswpunct (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswspace (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswxdigit (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswcased (pg_wchar wc, pg_locale_t locale)
 
pg_wchar pg_towupper (pg_wchar wc, pg_locale_t locale)
 
pg_wchar pg_towlower (pg_wchar wc, pg_locale_t locale)
 
const char * pg_icu_unicode_version (void)
 
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)
 

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 []
 

Macro Definition Documentation

◆ LOCALE_NAME_BUFLEN

#define LOCALE_NAME_BUFLEN   128

Definition at line 18 of file pg_locale.h.

◆ UNICODE_CASEMAP_BUFSZ

#define UNICODE_CASEMAP_BUFSZ   (UNICODE_CASEMAP_LEN * MAX_MULTIBYTE_CHAR_LEN)

Definition at line 32 of file pg_locale.h.

◆ UNICODE_CASEMAP_LEN

#define UNICODE_CASEMAP_LEN   3

Definition at line 31 of file pg_locale.h.

Typedef Documentation

◆ pg_locale_t

typedef struct pg_locale_struct* pg_locale_t

Definition at line 60 of file pg_locale.h.

Function Documentation

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1667 of file pg_locale.c.

1668{
1669 if (strcmp(locale, "C") == 0)
1670 return -1;
1671 else if (strcmp(locale, "C.UTF-8") == 0)
1672 return PG_UTF8;
1673 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1674 return PG_UTF8;
1675
1676
1677 ereport(ERROR,
1678 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1679 errmsg("invalid locale name \"%s\" for builtin provider",
1680 locale)));
1681
1682 return 0; /* keep compiler quiet */
1683}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#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 1691 of file pg_locale.c.

1692{
1693 const char *canonical_name = NULL;
1694 int required_encoding;
1695
1696 if (strcmp(locale, "C") == 0)
1697 canonical_name = "C";
1698 else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1699 canonical_name = "C.UTF-8";
1700 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1701 canonical_name = "PG_UNICODE_FAST";
1702
1703 if (!canonical_name)
1704 ereport(ERROR,
1705 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1706 errmsg("invalid locale name \"%s\" for builtin provider",
1707 locale)));
1708
1709 required_encoding = builtin_locale_encoding(canonical_name);
1710 if (required_encoding >= 0 && encoding != required_encoding)
1711 ereport(ERROR,
1712 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1713 errmsg("encoding \"%s\" does not match locale \"%s\"",
1715
1716 return canonical_name;
1717}
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1667
#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 702 of file pg_locale.c.

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

◆ check_locale()

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

Definition at line 275 of file pg_locale.c.

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

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 1247 of file pg_locale.c.

1248{
1249 char *collversion = NULL;
1250
1251 if (collprovider == COLLPROVIDER_BUILTIN)
1252 collversion = get_collation_actual_version_builtin(collcollate);
1253#ifdef USE_ICU
1254 else if (collprovider == COLLPROVIDER_ICU)
1255 collversion = get_collation_actual_version_icu(collcollate);
1256#endif
1257 else if (collprovider == COLLPROVIDER_LIBC)
1258 collversion = get_collation_actual_version_libc(collcollate);
1259
1260 return collversion;
1261}
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 1731 of file pg_locale.c.

1732{
1733#ifdef USE_ICU
1734 UErrorCode status;
1735 char *langtag;
1736 size_t buflen = 32; /* arbitrary starting buffer size */
1737 const bool strict = true;
1738
1739 /*
1740 * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1741 * RFC5646 section 4.4). Additionally, in older ICU versions,
1742 * uloc_toLanguageTag() doesn't always return the ultimate length on the
1743 * first call, necessitating a loop.
1744 */
1745 langtag = palloc(buflen);
1746 while (true)
1747 {
1748 status = U_ZERO_ERROR;
1749 uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1750
1751 /* try again if the buffer is not large enough */
1752 if ((status == U_BUFFER_OVERFLOW_ERROR ||
1753 status == U_STRING_NOT_TERMINATED_WARNING) &&
1754 buflen < MaxAllocSize)
1755 {
1756 buflen = Min(buflen * 2, MaxAllocSize);
1757 langtag = repalloc(langtag, buflen);
1758 continue;
1759 }
1760
1761 break;
1762 }
1763
1764 if (U_FAILURE(status))
1765 {
1766 pfree(langtag);
1767
1768 if (elevel > 0)
1769 ereport(elevel,
1770 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1771 errmsg("could not convert locale name \"%s\" to language tag: %s",
1772 loc_str, u_errorName(status))));
1773 return NULL;
1774 }
1775
1776 return langtag;
1777#else /* not USE_ICU */
1778 ereport(ERROR,
1779 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1780 errmsg("ICU is not supported in this build")));
1781 return NULL; /* keep compiler quiet */
1782#endif /* not USE_ICU */
1783}
#define Min(x, y)
Definition: c.h:1003
#define MaxAllocSize
Definition: fe_memutils.h:22
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1632
void * palloc(Size size)
Definition: mcxt.c:1387

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 1789 of file pg_locale.c.

1790{
1791#ifdef USE_ICU
1792 UCollator *collator;
1793 UErrorCode status;
1794 char lang[ULOC_LANG_CAPACITY];
1795 bool found = false;
1796 int elevel = icu_validation_level;
1797
1798 /* no validation */
1799 if (elevel < 0)
1800 return;
1801
1802 /* downgrade to WARNING during pg_upgrade */
1803 if (IsBinaryUpgrade && elevel > WARNING)
1804 elevel = WARNING;
1805
1806 /* validate that we can extract the language */
1807 status = U_ZERO_ERROR;
1808 uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1809 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1810 {
1811 ereport(elevel,
1812 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1813 errmsg("could not get language from ICU locale \"%s\": %s",
1814 loc_str, u_errorName(status)),
1815 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1816 "icu_validation_level", "disabled")));
1817 return;
1818 }
1819
1820 /* check for special language name */
1821 if (strcmp(lang, "") == 0 ||
1822 strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1823 found = true;
1824
1825 /* search for matching language within ICU */
1826 for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1827 {
1828 const char *otherloc = uloc_getAvailable(i);
1829 char otherlang[ULOC_LANG_CAPACITY];
1830
1831 status = U_ZERO_ERROR;
1832 uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1833 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1834 continue;
1835
1836 if (strcmp(lang, otherlang) == 0)
1837 found = true;
1838 }
1839
1840 if (!found)
1841 ereport(elevel,
1842 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1843 errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1844 loc_str, lang),
1845 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1846 "icu_validation_level", "disabled")));
1847
1848 /* check that it can be opened */
1849 collator = pg_ucol_open(loc_str);
1850 ucol_close(collator);
1851#else /* not USE_ICU */
1852 /* could get here if a collation was created by a build with ICU */
1853 ereport(ERROR,
1854 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1855 errmsg("ICU is not supported in this build")));
1856#endif /* not USE_ICU */
1857}
int errhint(const char *fmt,...)
Definition: elog.c:1330
bool IsBinaryUpgrade
Definition: globals.c:121
int icu_validation_level
Definition: pg_locale.c:92

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 1131 of file pg_locale.c.

1132{
1133 HeapTuple tup;
1134 Form_pg_database dbform;
1135 pg_locale_t result;
1136
1137 Assert(default_locale == NULL);
1138
1139 /* Fetch our pg_database row normally, via syscache */
1140 tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1141 if (!HeapTupleIsValid(tup))
1142 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1143 dbform = (Form_pg_database) GETSTRUCT(tup);
1144
1145 if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1146 result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
1148 else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1149 result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
1151 else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1152 result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
1154 else
1155 /* shouldn't happen */
1156 PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1157
1158 result->is_default = true;
1159
1160 Assert((result->collate_is_c && result->collate == NULL) ||
1161 (!result->collate_is_c && result->collate != NULL));
1162
1163 Assert((result->ctype_is_c && result->ctype == NULL) ||
1164 (!result->ctype_is_c && result->ctype != NULL));
1165
1166 ReleaseSysCache(tup);
1167
1168 default_locale = result;
1169}
Oid MyDatabaseId
Definition: globals.c:94
Assert(PointerIsAligned(start, uint64))
#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:60
pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context)
static pg_locale_t default_locale
Definition: pg_locale.c:106
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
const struct ctype_methods * ctype
Definition: pg_locale.h:146
const struct collate_methods * collate
Definition: pg_locale.h:145
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

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

Referenced by CheckMyDatabase().

◆ pg_database_locale()

pg_locale_t pg_database_locale ( void  )

Definition at line 1175 of file pg_locale.c.

1176{
1177 return pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
1178}
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1189

References pg_newlocale_from_collation().

Referenced by ltree_crc32_sz(), ltree_prefix_eq_ci(), t_isalnum(), and t_isalpha().

◆ pg_downcase_ident()

size_t pg_downcase_ident ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)

Definition at line 1366 of file pg_locale.c.

1367{
1369
1370 if (locale == NULL || locale->ctype == NULL ||
1371 locale->ctype->downcase_ident == NULL)
1372 return strlower_c(dst, dstsize, src, srclen);
1373 else
1374 return locale->ctype->downcase_ident(dst, dstsize, src, srclen,
1375 locale);
1376}
static size_t strlower_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1265

References default_locale, locale, and strlower_c().

Referenced by downcase_identifier().

◆ pg_icu_unicode_version()

const char * pg_icu_unicode_version ( void  )

Definition at line 1653 of file pg_locale.c.

1654{
1655#ifdef USE_ICU
1656 return U_UNICODE_VERSION;
1657#else
1658 return NULL;
1659#endif
1660}

Referenced by icu_unicode_version().

◆ pg_iswalnum()

bool pg_iswalnum ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1533 of file pg_locale.c.

1534{
1535 if (locale->ctype == NULL)
1536 return (wc <= (pg_wchar) 127 &&
1538 else
1539 return locale->ctype->wc_isalnum(wc, locale);
1540}
unsigned int pg_wchar
Definition: mbprint.c:31
#define PG_ISALNUM
Definition: pg_locale_c.h:21
static const unsigned char pg_char_properties[128]
Definition: pg_locale_c.h:29

References locale, pg_char_properties, and PG_ISALNUM.

Referenced by t_isalnum().

◆ pg_iswalpha()

bool pg_iswalpha ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1523 of file pg_locale.c.

1524{
1525 if (locale->ctype == NULL)
1526 return (wc <= (pg_wchar) 127 &&
1528 else
1529 return locale->ctype->wc_isalpha(wc, locale);
1530}
#define PG_ISALPHA
Definition: pg_locale_c.h:20

References locale, pg_char_properties, and PG_ISALPHA.

Referenced by t_isalpha().

◆ pg_iswcased()

bool pg_iswcased ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1615 of file pg_locale.c.

1616{
1617 /* for the C locale, Cased and Alpha are equivalent */
1618 if (locale->ctype == NULL)
1619 return (wc <= (pg_wchar) 127 &&
1621 else
1622 return locale->ctype->wc_iscased(wc, locale);
1623}

References locale, pg_char_properties, and PG_ISALPHA.

Referenced by like_fixed_prefix_ci().

◆ pg_iswdigit()

bool pg_iswdigit ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1513 of file pg_locale.c.

1514{
1515 if (locale->ctype == NULL)
1516 return (wc <= (pg_wchar) 127 &&
1518 else
1519 return locale->ctype->wc_isdigit(wc, locale);
1520}
#define PG_ISDIGIT
Definition: pg_locale_c.h:19

References locale, pg_char_properties, and PG_ISDIGIT.

◆ pg_iswgraph()

bool pg_iswgraph ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1563 of file pg_locale.c.

1564{
1565 if (locale->ctype == NULL)
1566 return (wc <= (pg_wchar) 127 &&
1568 else
1569 return locale->ctype->wc_isgraph(wc, locale);
1570}
#define PG_ISGRAPH
Definition: pg_locale_c.h:24

References locale, pg_char_properties, and PG_ISGRAPH.

◆ pg_iswlower()

bool pg_iswlower ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1553 of file pg_locale.c.

1554{
1555 if (locale->ctype == NULL)
1556 return (wc <= (pg_wchar) 127 &&
1558 else
1559 return locale->ctype->wc_islower(wc, locale);
1560}
#define PG_ISLOWER
Definition: pg_locale_c.h:23

References locale, pg_char_properties, and PG_ISLOWER.

◆ pg_iswprint()

bool pg_iswprint ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1573 of file pg_locale.c.

1574{
1575 if (locale->ctype == NULL)
1576 return (wc <= (pg_wchar) 127 &&
1578 else
1579 return locale->ctype->wc_isprint(wc, locale);
1580}
#define PG_ISPRINT
Definition: pg_locale_c.h:25

References locale, pg_char_properties, and PG_ISPRINT.

◆ pg_iswpunct()

bool pg_iswpunct ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1583 of file pg_locale.c.

1584{
1585 if (locale->ctype == NULL)
1586 return (wc <= (pg_wchar) 127 &&
1588 else
1589 return locale->ctype->wc_ispunct(wc, locale);
1590}
#define PG_ISPUNCT
Definition: pg_locale_c.h:26

References locale, pg_char_properties, and PG_ISPUNCT.

◆ pg_iswspace()

bool pg_iswspace ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1593 of file pg_locale.c.

1594{
1595 if (locale->ctype == NULL)
1596 return (wc <= (pg_wchar) 127 &&
1598 else
1599 return locale->ctype->wc_isspace(wc, locale);
1600}
#define PG_ISSPACE
Definition: pg_locale_c.h:27

References locale, pg_char_properties, and PG_ISSPACE.

◆ pg_iswupper()

bool pg_iswupper ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1543 of file pg_locale.c.

1544{
1545 if (locale->ctype == NULL)
1546 return (wc <= (pg_wchar) 127 &&
1548 else
1549 return locale->ctype->wc_isupper(wc, locale);
1550}
#define PG_ISUPPER
Definition: pg_locale_c.h:22

References locale, pg_char_properties, and PG_ISUPPER.

◆ pg_iswxdigit()

bool pg_iswxdigit ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1603 of file pg_locale.c.

1604{
1605 if (locale->ctype == NULL)
1606 return (wc <= (pg_wchar) 127 &&
1607 ((pg_char_properties[wc] & PG_ISDIGIT) ||
1608 ((wc >= 'A' && wc <= 'F') ||
1609 (wc >= 'a' && wc <= 'f'))));
1610 else
1611 return locale->ctype->wc_isxdigit(wc, locale);
1612}

References locale, pg_char_properties, and PG_ISDIGIT.

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1189 of file pg_locale.c.

1190{
1191 collation_cache_entry *cache_entry;
1192 bool found;
1193
1194 if (collid == DEFAULT_COLLATION_OID)
1195 return default_locale;
1196
1197 /*
1198 * Some callers expect C_COLLATION_OID to succeed even without catalog
1199 * access.
1200 */
1201 if (collid == C_COLLATION_OID)
1202 return &c_locale;
1203
1204 if (!OidIsValid(collid))
1205 elog(ERROR, "cache lookup failed for collation %u", collid);
1206
1208
1211
1212 if (CollationCache == NULL)
1213 {
1215 "collation cache",
1217 CollationCache = collation_cache_create(CollationCacheContext,
1218 16, NULL);
1219 }
1220
1221 cache_entry = collation_cache_insert(CollationCache, collid, &found);
1222 if (!found)
1223 {
1224 /*
1225 * Make sure cache entry is marked invalid, in case we fail before
1226 * setting things.
1227 */
1228 cache_entry->locale = NULL;
1229 }
1230
1231 if (cache_entry->locale == NULL)
1232 {
1234 }
1235
1237 last_collation_cache_locale = cache_entry->locale;
1238
1239 return cache_entry->locale;
1240}
#define OidIsValid(objectId)
Definition: c.h:794
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:151
static struct pg_locale_struct c_locale
Definition: pg_locale.c:112
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1049
static MemoryContext CollationCacheContext
Definition: pg_locale.c:143
static collation_cache_hash * CollationCache
Definition: pg_locale.c:144
static Oid last_collation_cache_oid
Definition: pg_locale.c:150
static void AssertCouldGetRelation(void)
Definition: relcache.h:44
Definition: pg_locale.c:121
pg_locale_t locale
Definition: pg_locale.c:123

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AssertCouldGetRelation(), c_locale, 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_ci(), make_greater_string(), match_pattern_prefix(), pg_database_locale(), 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 172 of file pg_locale.c.

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

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 1384 of file pg_locale.c.

1385{
1386 return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
1387}

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 1348 of file pg_locale.c.

1350{
1351 /* in the C locale, casefolding is the same as lowercasing */
1352 if (locale->ctype == NULL)
1353 return strlower_c(dst, dstsize, src, srclen);
1354 else
1355 return locale->ctype->strfold(dst, dstsize, src, srclen, locale);
1356}

References locale, and strlower_c().

Referenced by ltree_crc32_sz(), ltree_prefix_eq_ci(), and 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 1318 of file pg_locale.c.

1320{
1321 if (locale->ctype == NULL)
1322 return strlower_c(dst, dstsize, src, srclen);
1323 else
1324 return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1325}

References locale, and strlower_c().

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 1404 of file pg_locale.c.

1406{
1407 return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
1408}

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 1459 of file pg_locale.c.

1461{
1462 return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
1463}

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 1506 of file pg_locale.c.

1508{
1509 return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
1510}

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 1328 of file pg_locale.c.

1330{
1331 if (locale->ctype == NULL)
1332 return strtitle_c(dst, dstsize, src, srclen);
1333 else
1334 return locale->ctype->strtitle(dst, dstsize, src, srclen, locale);
1335}
static size_t strtitle_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1279

References locale, and strtitle_c().

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 1338 of file pg_locale.c.

1340{
1341 if (locale->ctype == NULL)
1342 return strupper_c(dst, dstsize, src, srclen);
1343 else
1344 return locale->ctype->strupper(dst, dstsize, src, srclen, locale);
1345}
static size_t strupper_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1305

References locale, and strupper_c().

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 1434 of file pg_locale.c.

1435{
1436 return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
1437}

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 1418 of file pg_locale.c.

1419{
1420 /*
1421 * locale->collate->strnxfrm is still a required method, even if it may
1422 * have the wrong behavior, because the planner uses it for estimates in
1423 * some cases.
1424 */
1425 return locale->collate->strxfrm_is_safe;
1426}

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 1481 of file pg_locale.c.

1483{
1484 return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
1485}

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 1470 of file pg_locale.c.

1471{
1472 return (locale->collate->strnxfrm_prefix != NULL);
1473}

References locale.

Referenced by varstr_abbrev_convert().

◆ pg_towlower()

pg_wchar pg_towlower ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1639 of file pg_locale.c.

1640{
1641 if (locale->ctype == NULL)
1642 {
1643 if (wc <= (pg_wchar) 127)
1644 return pg_ascii_tolower((unsigned char) wc);
1645 return wc;
1646 }
1647 else
1648 return locale->ctype->wc_tolower(wc, locale);
1649}
static unsigned char pg_ascii_tolower(unsigned char ch)
Definition: port.h:188

References locale, and pg_ascii_tolower().

◆ pg_towupper()

pg_wchar pg_towupper ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1626 of file pg_locale.c.

1627{
1628 if (locale->ctype == NULL)
1629 {
1630 if (wc <= (pg_wchar) 127)
1631 return pg_ascii_toupper((unsigned char) wc);
1632 return wc;
1633 }
1634 else
1635 return locale->ctype->wc_toupper(wc, locale);
1636}
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition: port.h:177

References locale, and pg_ascii_toupper().

◆ PGLC_localeconv()

struct lconv * PGLC_localeconv ( void  )

Definition at line 506 of file pg_locale.c.

507{
508 static struct lconv CurrentLocaleConv;
509 static bool CurrentLocaleConvAllocated = false;
510 struct lconv *extlconv;
511 struct lconv tmp;
512 struct lconv worklconv = {0};
513
514 /* Did we do it already? */
516 return &CurrentLocaleConv;
517
518 /* Free any already-allocated storage */
519 if (CurrentLocaleConvAllocated)
520 {
521 free_struct_lconv(&CurrentLocaleConv);
522 CurrentLocaleConvAllocated = false;
523 }
524
525 /*
526 * Use thread-safe method of obtaining a copy of lconv from the operating
527 * system.
528 */
531 &tmp) != 0)
532 elog(ERROR,
533 "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
535
536 /* Must copy data now so we can re-encode it. */
537 extlconv = &tmp;
538 worklconv.decimal_point = strdup(extlconv->decimal_point);
539 worklconv.thousands_sep = strdup(extlconv->thousands_sep);
540 worklconv.grouping = strdup(extlconv->grouping);
541 worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
542 worklconv.currency_symbol = strdup(extlconv->currency_symbol);
543 worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
544 worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
545 worklconv.mon_grouping = strdup(extlconv->mon_grouping);
546 worklconv.positive_sign = strdup(extlconv->positive_sign);
547 worklconv.negative_sign = strdup(extlconv->negative_sign);
548 /* Copy scalar fields as well */
549 worklconv.int_frac_digits = extlconv->int_frac_digits;
550 worklconv.frac_digits = extlconv->frac_digits;
551 worklconv.p_cs_precedes = extlconv->p_cs_precedes;
552 worklconv.p_sep_by_space = extlconv->p_sep_by_space;
553 worklconv.n_cs_precedes = extlconv->n_cs_precedes;
554 worklconv.n_sep_by_space = extlconv->n_sep_by_space;
555 worklconv.p_sign_posn = extlconv->p_sign_posn;
556 worklconv.n_sign_posn = extlconv->n_sign_posn;
557
558 /* Free the contents of the object populated by pg_localeconv_r(). */
559 pg_localeconv_free(&tmp);
560
561 /* If any of the preceding strdup calls failed, complain now. */
562 if (!struct_lconv_is_valid(&worklconv))
564 (errcode(ERRCODE_OUT_OF_MEMORY),
565 errmsg("out of memory")));
566
567 PG_TRY();
568 {
569 int encoding;
570
571 /*
572 * Now we must perform encoding conversion from whatever's associated
573 * with the locales into the database encoding. If we can't identify
574 * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
575 * use PG_SQL_ASCII, which will result in just validating that the
576 * strings are OK in the database encoding.
577 */
579 if (encoding < 0)
581
582 db_encoding_convert(encoding, &worklconv.decimal_point);
583 db_encoding_convert(encoding, &worklconv.thousands_sep);
584 /* grouping is not text and does not require conversion */
585
587 if (encoding < 0)
589
590 db_encoding_convert(encoding, &worklconv.int_curr_symbol);
591 db_encoding_convert(encoding, &worklconv.currency_symbol);
592 db_encoding_convert(encoding, &worklconv.mon_decimal_point);
593 db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
594 /* mon_grouping is not text and does not require conversion */
595 db_encoding_convert(encoding, &worklconv.positive_sign);
596 db_encoding_convert(encoding, &worklconv.negative_sign);
597 }
598 PG_CATCH();
599 {
600 free_struct_lconv(&worklconv);
601 PG_RE_THROW();
602 }
603 PG_END_TRY();
604
605 /*
606 * Everything is good, so save the results.
607 */
608 CurrentLocaleConv = worklconv;
609 CurrentLocaleConvAllocated = true;
611 return &CurrentLocaleConv;
612}
#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:89
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:476
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:426
static bool CurrentLocaleConvValid
Definition: pg_locale.c:109
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:445
char * locale_monetary
Definition: pg_locale.c:88
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 1127 of file pg_locale_libc.c.

1128{
1129 int save_errno;
1130
1131 /*
1132 * Windows doesn't provide any useful error indication from
1133 * _create_locale(), and BSD-derived platforms don't seem to feel they
1134 * need to set errno either (even though POSIX is pretty clear that
1135 * newlocale should do so). So, if errno hasn't been set, assume ENOENT
1136 * is what to report.
1137 */
1138 if (errno == 0)
1139 errno = ENOENT;
1140
1141 /*
1142 * ENOENT means "no such locale", not "no such file", so clarify that
1143 * errno with an errdetail message.
1144 */
1145 save_errno = errno; /* auxiliary funcs might change errno */
1146 ereport(ERROR,
1147 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1148 errmsg("could not create locale \"%s\": %m",
1149 localename),
1150 (save_errno == ENOENT ?
1151 errdetail("The operating system could not find any locale data for the locale name \"%s\".",
1152 localename) : 0)));
1153}
int errdetail(const char *fmt,...)
Definition: elog.c:1216

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 1205 of file pg_locale_libc.c.

1206{
1207 size_t result;
1208
1209 if (tolen == 0)
1210 return 0;
1211
1212#ifdef WIN32
1213
1214 /*
1215 * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
1216 * for some reason mbstowcs and wcstombs won't do this for us, so we use
1217 * MultiByteToWideChar().
1218 */
1220 {
1221 result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
1222 NULL, NULL);
1223 /* A zero return is failure */
1224 if (result <= 0)
1225 result = -1;
1226 else
1227 {
1228 Assert(result <= tolen);
1229 /* Microsoft counts the zero terminator in the result */
1230 result--;
1231 }
1232 }
1233 else
1234#endif /* WIN32 */
1235 if (loc == (locale_t) 0)
1236 {
1237 /* Use wcstombs directly for the default locale */
1238 result = wcstombs(to, from, tolen);
1239 }
1240 else
1241 {
1242 /* Use wcstombs_l for nondefault locales */
1243 result = wcstombs_l(to, from, tolen, loc);
1244 }
1245
1246 return result;
1247}
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

◆ icu_validation_level

PGDLLIMPORT int icu_validation_level
extern

Definition at line 92 of file pg_locale.c.

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

◆ locale_messages

PGDLLIMPORT char* locale_messages
extern

Definition at line 87 of file pg_locale.c.

◆ locale_monetary

PGDLLIMPORT char* locale_monetary
extern

Definition at line 88 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

PGDLLIMPORT char* locale_numeric
extern

Definition at line 89 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

PGDLLIMPORT char* locale_time
extern

Definition at line 90 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

PGDLLIMPORT char* localized_abbrev_days[]
extern

Definition at line 101 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 103 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 102 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 104 of file pg_locale.c.

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