PostgreSQL Source Code  git master
formatting.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * str_tolower (const char *buff, size_t nbytes, Oid collid)
 
char * str_toupper (const char *buff, size_t nbytes, Oid collid)
 
char * str_initcap (const char *buff, size_t nbytes, Oid collid)
 
char * asc_tolower (const char *buff, size_t nbytes)
 
char * asc_toupper (const char *buff, size_t nbytes)
 
char * asc_initcap (const char *buff, size_t nbytes)
 
Datum parse_datetime (text *date_txt, text *fmt, bool std, Oid *typid, int32 *typmod, int *tz, bool *have_error)
 

Function Documentation

◆ asc_initcap()

char* asc_initcap ( const char *  buff,
size_t  nbytes 
)

Definition at line 2077 of file formatting.c.

References pg_ascii_tolower(), pg_ascii_toupper(), and pnstrdup().

Referenced by str_initcap().

2078 {
2079  char *result;
2080  char *p;
2081  int wasalnum = false;
2082 
2083  if (!buff)
2084  return NULL;
2085 
2086  result = pnstrdup(buff, nbytes);
2087 
2088  for (p = result; *p; p++)
2089  {
2090  char c;
2091 
2092  if (wasalnum)
2093  *p = c = pg_ascii_tolower((unsigned char) *p);
2094  else
2095  *p = c = pg_ascii_toupper((unsigned char) *p);
2096  /* we don't trust isalnum() here */
2097  wasalnum = ((c >= 'A' && c <= 'Z') ||
2098  (c >= 'a' && c <= 'z') ||
2099  (c >= '0' && c <= '9'));
2100  }
2101 
2102  return result;
2103 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
char * c

◆ asc_tolower()

char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 2031 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), get_collation_actual_version(), and str_tolower().

2032 {
2033  char *result;
2034  char *p;
2035 
2036  if (!buff)
2037  return NULL;
2038 
2039  result = pnstrdup(buff, nbytes);
2040 
2041  for (p = result; *p; p++)
2042  *p = pg_ascii_tolower((unsigned char) *p);
2043 
2044  return result;
2045 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146

◆ asc_toupper()

char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 2054 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

2055 {
2056  char *result;
2057  char *p;
2058 
2059  if (!buff)
2060  return NULL;
2061 
2062  result = pnstrdup(buff, nbytes);
2063 
2064  for (p = result; *p; p++)
2065  *p = pg_ascii_toupper((unsigned char) *p);
2066 
2067  return result;
2068 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135

◆ parse_datetime()

Datum parse_datetime ( text date_txt,
text fmt,
bool  std,
Oid typid,
int32 typmod,
int *  tz,
bool have_error 
)

Definition at line 4111 of file formatting.c.

References AdjustTimeForTypmod(), AdjustTimestampForTypmod(), Assert, CHECK_ERROR, date2j(), DateADTGetDatum, DateTimeParseError(), DCH_DATED, DCH_TIMED, DCH_ZONED, DecodeTimezone(), do_to_timestamp(), ereport, errcode(), errmsg(), ERROR, TmToChar::fsec, IS_VALID_DATE, IS_VALID_JULIAN, palloc(), POSTGRES_EPOCH_JDATE, RETURN_ERROR, text_to_cstring(), TimeTzADT::time, TimeADTGetDatum, TimestampGetDatum, TimestampTzGetDatum, TimeTzADTPGetDatum, tm2time(), tm2timestamp(), tm2timetz(), pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, pg_tm::tm_zone, and unconstify.

Referenced by executeDateTimeMethod().

4113 {
4114  struct pg_tm tm;
4115  fsec_t fsec;
4116  int fprec;
4117  uint32 flags;
4118 
4119  do_to_timestamp(date_txt, fmt, strict, &tm, &fsec, &fprec, &flags, have_error);
4120  CHECK_ERROR;
4121 
4122  *typmod = fprec ? fprec : -1; /* fractional part precision */
4123 
4124  if (flags & DCH_DATED)
4125  {
4126  if (flags & DCH_TIMED)
4127  {
4128  if (flags & DCH_ZONED)
4129  {
4130  TimestampTz result;
4131 
4132  if (tm.tm_zone)
4133  {
4134  int dterr = DecodeTimezone(unconstify(char *, tm.tm_zone), tz);
4135 
4136  if (dterr)
4137  DateTimeParseError(dterr, text_to_cstring(date_txt), "timestamptz");
4138  }
4139  else
4140  {
4141  /*
4142  * Time zone is present in format string, but not in input
4143  * string. Assuming do_to_timestamp() triggers no error
4144  * this should be possible only in non-strict case.
4145  */
4146  Assert(!strict);
4147 
4149  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4150  errmsg("missing time zone in input string for type timestamptz"))));
4151  }
4152 
4153  if (tm2timestamp(&tm, fsec, tz, &result) != 0)
4155  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4156  errmsg("timestamptz out of range"))));
4157 
4158  AdjustTimestampForTypmod(&result, *typmod);
4159 
4160  *typid = TIMESTAMPTZOID;
4161  return TimestampTzGetDatum(result);
4162  }
4163  else
4164  {
4165  Timestamp result;
4166 
4167  if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
4169  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4170  errmsg("timestamp out of range"))));
4171 
4172  AdjustTimestampForTypmod(&result, *typmod);
4173 
4174  *typid = TIMESTAMPOID;
4175  return TimestampGetDatum(result);
4176  }
4177  }
4178  else
4179  {
4180  if (flags & DCH_ZONED)
4181  {
4183  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4184  errmsg("datetime format is zoned but not timed"))));
4185  }
4186  else
4187  {
4188  DateADT result;
4189 
4190  /* Prevent overflow in Julian-day routines */
4193  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4194  errmsg("date out of range: \"%s\"",
4195  text_to_cstring(date_txt)))));
4196 
4197  result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) -
4199 
4200  /* Now check for just-out-of-range dates */
4201  if (!IS_VALID_DATE(result))
4203  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4204  errmsg("date out of range: \"%s\"",
4205  text_to_cstring(date_txt)))));
4206 
4207  *typid = DATEOID;
4208  return DateADTGetDatum(result);
4209  }
4210  }
4211  }
4212  else if (flags & DCH_TIMED)
4213  {
4214  if (flags & DCH_ZONED)
4215  {
4216  TimeTzADT *result = palloc(sizeof(TimeTzADT));
4217 
4218  if (tm.tm_zone)
4219  {
4220  int dterr = DecodeTimezone(unconstify(char *, tm.tm_zone), tz);
4221 
4222  if (dterr)
4223  RETURN_ERROR(DateTimeParseError(dterr, text_to_cstring(date_txt), "timetz"));
4224  }
4225  else
4226  {
4227  /*
4228  * Time zone is present in format string, but not in input
4229  * string. Assuming do_to_timestamp() triggers no error this
4230  * should be possible only in non-strict case.
4231  */
4232  Assert(!strict);
4233 
4235  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4236  errmsg("missing time zone in input string for type timetz"))));
4237  }
4238 
4239  if (tm2timetz(&tm, fsec, *tz, result) != 0)
4241  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4242  errmsg("timetz out of range"))));
4243 
4244  AdjustTimeForTypmod(&result->time, *typmod);
4245 
4246  *typid = TIMETZOID;
4247  return TimeTzADTPGetDatum(result);
4248  }
4249  else
4250  {
4251  TimeADT result;
4252 
4253  if (tm2time(&tm, fsec, &result) != 0)
4255  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4256  errmsg("time out of range"))));
4257 
4258  AdjustTimeForTypmod(&result, *typmod);
4259 
4260  *typid = TIMEOID;
4261  return TimeADTGetDatum(result);
4262  }
4263  }
4264  else
4265  {
4267  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4268  errmsg("datetime format is not dated and not timed"))));
4269  }
4270 
4271 on_error:
4272  return (Datum) 0;
4273 }
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3736
TimeADT time
Definition: date.h:29
int32 DateADT
Definition: date.h:23
int64 TimestampTz
Definition: timestamp.h:39
#define TimeTzADTPGetDatum(X)
Definition: date.h:59
int errcode(int sqlerrcode)
Definition: elog.c:608
#define DateADTGetDatum(X)
Definition: date.h:57
Definition: pgtime.h:25
#define CHECK_ERROR
Definition: formatting.c:131
#define DCH_TIMED
Definition: formatting.c:1023
#define DCH_DATED
Definition: formatting.c:1022
static struct pg_tm tm
Definition: localtime.c:108
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2020
#define IS_VALID_DATE(d)
Definition: timestamp.h:190
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:391
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
int DecodeTimezone(char *str, int *tzp)
Definition: datetime.c:2875
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
unsigned int uint32
Definition: c.h:367
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define DCH_ZONED
Definition: formatting.c:1024
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
const char * tm_zone
Definition: pgtime.h:37
#define ereport(elevel, rest)
Definition: elog.h:141
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define unconstify(underlying_type, expr)
Definition: c.h:1202
uintptr_t Datum
Definition: postgres.h:367
#define TimeADTGetDatum(X)
Definition: date.h:58
int date2j(int y, int m, int d)
Definition: datetime.c:269
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1887
#define Assert(condition)
Definition: c.h:738
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1266
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
static void do_to_timestamp(text *date_txt, text *fmt, bool std, struct pg_tm *tm, fsec_t *fsec, int *fprec, uint32 *flags, bool *have_error)
Definition: formatting.c:4295
Definition: date.h:27
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1442
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118

◆ str_initcap()

char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1882 of file formatting.c.

References asc_initcap(), char2wchar(), ereport, errcode(), errhint(), errmsg(), ERROR, pg_locale_struct::info, isalnum_l, iswalnum_l, lc_ctype_is_c(), OidIsValid, palloc(), pfree(), pg_database_encoding_max_length(), pg_newlocale_from_collation(), pg_tolower(), pg_toupper(), pnstrdup(), pg_locale_struct::provider, tolower_l, toupper_l, towlower_l, towupper_l, and wchar2char().

Referenced by initcap(), and str_initcap_z().

1883 {
1884  char *result;
1885  int wasalnum = false;
1886 
1887  if (!buff)
1888  return NULL;
1889 
1890  /* C/POSIX collations use this path regardless of database encoding */
1891  if (lc_ctype_is_c(collid))
1892  {
1893  result = asc_initcap(buff, nbytes);
1894  }
1895  else
1896  {
1897  pg_locale_t mylocale = 0;
1898 
1899  if (collid != DEFAULT_COLLATION_OID)
1900  {
1901  if (!OidIsValid(collid))
1902  {
1903  /*
1904  * This typically means that the parser could not resolve a
1905  * conflict of implicit collations, so report it that way.
1906  */
1907  ereport(ERROR,
1908  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1909  errmsg("could not determine which collation to use for %s function",
1910  "initcap()"),
1911  errhint("Use the COLLATE clause to set the collation explicitly.")));
1912  }
1913  mylocale = pg_newlocale_from_collation(collid);
1914  }
1915 
1916 #ifdef USE_ICU
1917  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1918  {
1919  int32_t len_uchar,
1920  len_conv;
1921  UChar *buff_uchar;
1922  UChar *buff_conv;
1923 
1924  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1925  len_conv = icu_convert_case(u_strToTitle_default_BI, mylocale,
1926  &buff_conv, buff_uchar, len_uchar);
1927  icu_from_uchar(&result, buff_conv, len_conv);
1928  pfree(buff_uchar);
1929  pfree(buff_conv);
1930  }
1931  else
1932 #endif
1933  {
1935  {
1936  wchar_t *workspace;
1937  size_t curr_char;
1938  size_t result_size;
1939 
1940  /* Overflow paranoia */
1941  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1942  ereport(ERROR,
1943  (errcode(ERRCODE_OUT_OF_MEMORY),
1944  errmsg("out of memory")));
1945 
1946  /* Output workspace cannot have more codes than input bytes */
1947  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1948 
1949  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1950 
1951  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1952  {
1953 #ifdef HAVE_LOCALE_T
1954  if (mylocale)
1955  {
1956  if (wasalnum)
1957  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1958  else
1959  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1960  wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt);
1961  }
1962  else
1963 #endif
1964  {
1965  if (wasalnum)
1966  workspace[curr_char] = towlower(workspace[curr_char]);
1967  else
1968  workspace[curr_char] = towupper(workspace[curr_char]);
1969  wasalnum = iswalnum(workspace[curr_char]);
1970  }
1971  }
1972 
1973  /*
1974  * Make result large enough; case change might change number
1975  * of bytes
1976  */
1977  result_size = curr_char * pg_database_encoding_max_length() + 1;
1978  result = palloc(result_size);
1979 
1980  wchar2char(result, workspace, result_size, mylocale);
1981  pfree(workspace);
1982  }
1983  else
1984  {
1985  char *p;
1986 
1987  result = pnstrdup(buff, nbytes);
1988 
1989  /*
1990  * Note: we assume that toupper_l()/tolower_l() will not be so
1991  * broken as to need guard tests. When using the default
1992  * collation, we apply the traditional Postgres behavior that
1993  * forces ASCII-style treatment of I/i, but in non-default
1994  * collations you get exactly what the collation says.
1995  */
1996  for (p = result; *p; p++)
1997  {
1998 #ifdef HAVE_LOCALE_T
1999  if (mylocale)
2000  {
2001  if (wasalnum)
2002  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
2003  else
2004  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
2005  wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt);
2006  }
2007  else
2008 #endif
2009  {
2010  if (wasalnum)
2011  *p = pg_tolower((unsigned char) *p);
2012  else
2013  *p = pg_toupper((unsigned char) *p);
2014  wasalnum = isalnum((unsigned char) *p);
2015  }
2016  }
2017  }
2018  }
2019  }
2020 
2021  return result;
2022 }
union pg_locale_struct::@144 info
int errhint(const char *fmt,...)
Definition: elog.c:1069
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
#define towlower_l
Definition: win32_port.h:366
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:608
#define tolower_l
Definition: win32_port.h:364
#define OidIsValid(objectId)
Definition: c.h:644
#define towupper_l
Definition: win32_port.h:367
void pfree(void *pointer)
Definition: mcxt.c:1056
#define isalnum_l
Definition: win32_port.h:372
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1841
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
#define toupper_l
Definition: win32_port.h:365
char * asc_initcap(const char *buff, size_t nbytes)
Definition: formatting.c:2077
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1770
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define iswalnum_l
Definition: win32_port.h:373

◆ str_tolower()

char* str_tolower ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1634 of file formatting.c.

References asc_tolower(), char2wchar(), ereport, errcode(), errhint(), errmsg(), ERROR, pg_locale_struct::info, lc_ctype_is_c(), OidIsValid, palloc(), pfree(), pg_database_encoding_max_length(), pg_newlocale_from_collation(), pg_tolower(), pnstrdup(), pg_locale_struct::provider, tolower_l, towlower_l, and wchar2char().

Referenced by citext_eq(), citext_hash(), citext_hash_extended(), citext_ne(), citextcmp(), internal_citext_pattern_cmp(), lower(), ltree_strncasecmp(), and str_tolower_z().

1635 {
1636  char *result;
1637 
1638  if (!buff)
1639  return NULL;
1640 
1641  /* C/POSIX collations use this path regardless of database encoding */
1642  if (lc_ctype_is_c(collid))
1643  {
1644  result = asc_tolower(buff, nbytes);
1645  }
1646  else
1647  {
1648  pg_locale_t mylocale = 0;
1649 
1650  if (collid != DEFAULT_COLLATION_OID)
1651  {
1652  if (!OidIsValid(collid))
1653  {
1654  /*
1655  * This typically means that the parser could not resolve a
1656  * conflict of implicit collations, so report it that way.
1657  */
1658  ereport(ERROR,
1659  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1660  errmsg("could not determine which collation to use for %s function",
1661  "lower()"),
1662  errhint("Use the COLLATE clause to set the collation explicitly.")));
1663  }
1664  mylocale = pg_newlocale_from_collation(collid);
1665  }
1666 
1667 #ifdef USE_ICU
1668  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1669  {
1670  int32_t len_uchar;
1671  int32_t len_conv;
1672  UChar *buff_uchar;
1673  UChar *buff_conv;
1674 
1675  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1676  len_conv = icu_convert_case(u_strToLower, mylocale,
1677  &buff_conv, buff_uchar, len_uchar);
1678  icu_from_uchar(&result, buff_conv, len_conv);
1679  pfree(buff_uchar);
1680  pfree(buff_conv);
1681  }
1682  else
1683 #endif
1684  {
1686  {
1687  wchar_t *workspace;
1688  size_t curr_char;
1689  size_t result_size;
1690 
1691  /* Overflow paranoia */
1692  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1693  ereport(ERROR,
1694  (errcode(ERRCODE_OUT_OF_MEMORY),
1695  errmsg("out of memory")));
1696 
1697  /* Output workspace cannot have more codes than input bytes */
1698  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1699 
1700  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1701 
1702  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1703  {
1704 #ifdef HAVE_LOCALE_T
1705  if (mylocale)
1706  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1707  else
1708 #endif
1709  workspace[curr_char] = towlower(workspace[curr_char]);
1710  }
1711 
1712  /*
1713  * Make result large enough; case change might change number
1714  * of bytes
1715  */
1716  result_size = curr_char * pg_database_encoding_max_length() + 1;
1717  result = palloc(result_size);
1718 
1719  wchar2char(result, workspace, result_size, mylocale);
1720  pfree(workspace);
1721  }
1722  else
1723  {
1724  char *p;
1725 
1726  result = pnstrdup(buff, nbytes);
1727 
1728  /*
1729  * Note: we assume that tolower_l() will not be so broken as
1730  * to need an isupper_l() guard test. When using the default
1731  * collation, we apply the traditional Postgres behavior that
1732  * forces ASCII-style treatment of I/i, but in non-default
1733  * collations you get exactly what the collation says.
1734  */
1735  for (p = result; *p; p++)
1736  {
1737 #ifdef HAVE_LOCALE_T
1738  if (mylocale)
1739  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1740  else
1741 #endif
1742  *p = pg_tolower((unsigned char) *p);
1743  }
1744  }
1745  }
1746  }
1747 
1748  return result;
1749 }
union pg_locale_struct::@144 info
int errhint(const char *fmt,...)
Definition: elog.c:1069
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
#define towlower_l
Definition: win32_port.h:366
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:608
#define tolower_l
Definition: win32_port.h:364
#define OidIsValid(objectId)
Definition: c.h:644
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1841
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1770
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:2031

◆ str_toupper()

char* str_toupper ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1758 of file formatting.c.

References asc_toupper(), char2wchar(), ereport, errcode(), errhint(), errmsg(), ERROR, pg_locale_struct::info, lc_ctype_is_c(), OidIsValid, palloc(), pfree(), pg_database_encoding_max_length(), pg_newlocale_from_collation(), pg_toupper(), pnstrdup(), pg_locale_struct::provider, toupper_l, towupper_l, and wchar2char().

Referenced by str_toupper_z(), and upper().

1759 {
1760  char *result;
1761 
1762  if (!buff)
1763  return NULL;
1764 
1765  /* C/POSIX collations use this path regardless of database encoding */
1766  if (lc_ctype_is_c(collid))
1767  {
1768  result = asc_toupper(buff, nbytes);
1769  }
1770  else
1771  {
1772  pg_locale_t mylocale = 0;
1773 
1774  if (collid != DEFAULT_COLLATION_OID)
1775  {
1776  if (!OidIsValid(collid))
1777  {
1778  /*
1779  * This typically means that the parser could not resolve a
1780  * conflict of implicit collations, so report it that way.
1781  */
1782  ereport(ERROR,
1783  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1784  errmsg("could not determine which collation to use for %s function",
1785  "upper()"),
1786  errhint("Use the COLLATE clause to set the collation explicitly.")));
1787  }
1788  mylocale = pg_newlocale_from_collation(collid);
1789  }
1790 
1791 #ifdef USE_ICU
1792  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1793  {
1794  int32_t len_uchar,
1795  len_conv;
1796  UChar *buff_uchar;
1797  UChar *buff_conv;
1798 
1799  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1800  len_conv = icu_convert_case(u_strToUpper, mylocale,
1801  &buff_conv, buff_uchar, len_uchar);
1802  icu_from_uchar(&result, buff_conv, len_conv);
1803  pfree(buff_uchar);
1804  pfree(buff_conv);
1805  }
1806  else
1807 #endif
1808  {
1810  {
1811  wchar_t *workspace;
1812  size_t curr_char;
1813  size_t result_size;
1814 
1815  /* Overflow paranoia */
1816  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1817  ereport(ERROR,
1818  (errcode(ERRCODE_OUT_OF_MEMORY),
1819  errmsg("out of memory")));
1820 
1821  /* Output workspace cannot have more codes than input bytes */
1822  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1823 
1824  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1825 
1826  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1827  {
1828 #ifdef HAVE_LOCALE_T
1829  if (mylocale)
1830  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1831  else
1832 #endif
1833  workspace[curr_char] = towupper(workspace[curr_char]);
1834  }
1835 
1836  /*
1837  * Make result large enough; case change might change number
1838  * of bytes
1839  */
1840  result_size = curr_char * pg_database_encoding_max_length() + 1;
1841  result = palloc(result_size);
1842 
1843  wchar2char(result, workspace, result_size, mylocale);
1844  pfree(workspace);
1845  }
1846  else
1847  {
1848  char *p;
1849 
1850  result = pnstrdup(buff, nbytes);
1851 
1852  /*
1853  * Note: we assume that toupper_l() will not be so broken as
1854  * to need an islower_l() guard test. When using the default
1855  * collation, we apply the traditional Postgres behavior that
1856  * forces ASCII-style treatment of I/i, but in non-default
1857  * collations you get exactly what the collation says.
1858  */
1859  for (p = result; *p; p++)
1860  {
1861 #ifdef HAVE_LOCALE_T
1862  if (mylocale)
1863  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
1864  else
1865 #endif
1866  *p = pg_toupper((unsigned char) *p);
1867  }
1868  }
1869  }
1870  }
1871 
1872  return result;
1873 }
union pg_locale_struct::@144 info
int errhint(const char *fmt,...)
Definition: elog.c:1069
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
int errcode(int sqlerrcode)
Definition: elog.c:608
#define OidIsValid(objectId)
Definition: c.h:644
#define towupper_l
Definition: win32_port.h:367
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:2054
#define ereport(elevel, rest)
Definition: elog.h:141
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1841
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
#define toupper_l
Definition: win32_port.h:365
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1770
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105