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)
 

Function Documentation

◆ asc_initcap()

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

Definition at line 2013 of file formatting.c.

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

Referenced by str_initcap().

2014 {
2015  char *result;
2016  char *p;
2017  int wasalnum = false;
2018 
2019  if (!buff)
2020  return NULL;
2021 
2022  result = pnstrdup(buff, nbytes);
2023 
2024  for (p = result; *p; p++)
2025  {
2026  char c;
2027 
2028  if (wasalnum)
2029  *p = c = pg_ascii_tolower((unsigned char) *p);
2030  else
2031  *p = c = pg_ascii_toupper((unsigned char) *p);
2032  /* we don't trust isalnum() here */
2033  wasalnum = ((c >= 'A' && c <= 'Z') ||
2034  (c >= 'a' && c <= 'z') ||
2035  (c >= '0' && c <= '9'));
2036  }
2037 
2038  return result;
2039 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
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 1967 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

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

1968 {
1969  char *result;
1970  char *p;
1971 
1972  if (!buff)
1973  return NULL;
1974 
1975  result = pnstrdup(buff, nbytes);
1976 
1977  for (p = result; *p; p++)
1978  *p = pg_ascii_tolower((unsigned char) *p);
1979 
1980  return result;
1981 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
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 1990 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1991 {
1992  char *result;
1993  char *p;
1994 
1995  if (!buff)
1996  return NULL;
1997 
1998  result = pnstrdup(buff, nbytes);
1999 
2000  for (p = result; *p; p++)
2001  *p = pg_ascii_toupper((unsigned char) *p);
2002 
2003  return result;
2004 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135

◆ str_initcap()

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

Definition at line 1818 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().

1819 {
1820  char *result;
1821  int wasalnum = false;
1822 
1823  if (!buff)
1824  return NULL;
1825 
1826  /* C/POSIX collations use this path regardless of database encoding */
1827  if (lc_ctype_is_c(collid))
1828  {
1829  result = asc_initcap(buff, nbytes);
1830  }
1831  else
1832  {
1833  pg_locale_t mylocale = 0;
1834 
1835  if (collid != DEFAULT_COLLATION_OID)
1836  {
1837  if (!OidIsValid(collid))
1838  {
1839  /*
1840  * This typically means that the parser could not resolve a
1841  * conflict of implicit collations, so report it that way.
1842  */
1843  ereport(ERROR,
1844  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1845  errmsg("could not determine which collation to use for %s function",
1846  "initcap()"),
1847  errhint("Use the COLLATE clause to set the collation explicitly.")));
1848  }
1849  mylocale = pg_newlocale_from_collation(collid);
1850  }
1851 
1852 #ifdef USE_ICU
1853  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1854  {
1855  int32_t len_uchar,
1856  len_conv;
1857  UChar *buff_uchar;
1858  UChar *buff_conv;
1859 
1860  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1861  len_conv = icu_convert_case(u_strToTitle_default_BI, mylocale,
1862  &buff_conv, buff_uchar, len_uchar);
1863  icu_from_uchar(&result, buff_conv, len_conv);
1864  pfree(buff_uchar);
1865  pfree(buff_conv);
1866  }
1867  else
1868 #endif
1869  {
1871  {
1872  wchar_t *workspace;
1873  size_t curr_char;
1874  size_t result_size;
1875 
1876  /* Overflow paranoia */
1877  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1878  ereport(ERROR,
1879  (errcode(ERRCODE_OUT_OF_MEMORY),
1880  errmsg("out of memory")));
1881 
1882  /* Output workspace cannot have more codes than input bytes */
1883  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1884 
1885  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1886 
1887  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1888  {
1889 #ifdef HAVE_LOCALE_T
1890  if (mylocale)
1891  {
1892  if (wasalnum)
1893  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1894  else
1895  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1896  wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt);
1897  }
1898  else
1899 #endif
1900  {
1901  if (wasalnum)
1902  workspace[curr_char] = towlower(workspace[curr_char]);
1903  else
1904  workspace[curr_char] = towupper(workspace[curr_char]);
1905  wasalnum = iswalnum(workspace[curr_char]);
1906  }
1907  }
1908 
1909  /*
1910  * Make result large enough; case change might change number
1911  * of bytes
1912  */
1913  result_size = curr_char * pg_database_encoding_max_length() + 1;
1914  result = palloc(result_size);
1915 
1916  wchar2char(result, workspace, result_size, mylocale);
1917  pfree(workspace);
1918  }
1919  else
1920  {
1921  char *p;
1922 
1923  result = pnstrdup(buff, nbytes);
1924 
1925  /*
1926  * Note: we assume that toupper_l()/tolower_l() will not be so
1927  * broken as to need guard tests. When using the default
1928  * collation, we apply the traditional Postgres behavior that
1929  * forces ASCII-style treatment of I/i, but in non-default
1930  * collations you get exactly what the collation says.
1931  */
1932  for (p = result; *p; p++)
1933  {
1934 #ifdef HAVE_LOCALE_T
1935  if (mylocale)
1936  {
1937  if (wasalnum)
1938  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1939  else
1940  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
1941  wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt);
1942  }
1943  else
1944 #endif
1945  {
1946  if (wasalnum)
1947  *p = pg_tolower((unsigned char) *p);
1948  else
1949  *p = pg_toupper((unsigned char) *p);
1950  wasalnum = isalnum((unsigned char) *p);
1951  }
1952  }
1953  }
1954  }
1955  }
1956 
1957  return result;
1958 }
int errhint(const char *fmt,...)
Definition: elog.c:974
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
#define towlower_l
Definition: win32_port.h:375
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:570
#define tolower_l
Definition: win32_port.h:373
#define OidIsValid(objectId)
Definition: c.h:638
#define towupper_l
Definition: win32_port.h:376
void pfree(void *pointer)
Definition: mcxt.c:1031
#define isalnum_l
Definition: win32_port.h:381
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
#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:1845
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1338
#define toupper_l
Definition: win32_port.h:374
char * asc_initcap(const char *buff, size_t nbytes)
Definition: formatting.c:2013
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1774
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1241
union pg_locale_struct::@143 info
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define iswalnum_l
Definition: win32_port.h:382

◆ str_tolower()

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

Definition at line 1570 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().

1571 {
1572  char *result;
1573 
1574  if (!buff)
1575  return NULL;
1576 
1577  /* C/POSIX collations use this path regardless of database encoding */
1578  if (lc_ctype_is_c(collid))
1579  {
1580  result = asc_tolower(buff, nbytes);
1581  }
1582  else
1583  {
1584  pg_locale_t mylocale = 0;
1585 
1586  if (collid != DEFAULT_COLLATION_OID)
1587  {
1588  if (!OidIsValid(collid))
1589  {
1590  /*
1591  * This typically means that the parser could not resolve a
1592  * conflict of implicit collations, so report it that way.
1593  */
1594  ereport(ERROR,
1595  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1596  errmsg("could not determine which collation to use for %s function",
1597  "lower()"),
1598  errhint("Use the COLLATE clause to set the collation explicitly.")));
1599  }
1600  mylocale = pg_newlocale_from_collation(collid);
1601  }
1602 
1603 #ifdef USE_ICU
1604  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1605  {
1606  int32_t len_uchar;
1607  int32_t len_conv;
1608  UChar *buff_uchar;
1609  UChar *buff_conv;
1610 
1611  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1612  len_conv = icu_convert_case(u_strToLower, mylocale,
1613  &buff_conv, buff_uchar, len_uchar);
1614  icu_from_uchar(&result, buff_conv, len_conv);
1615  pfree(buff_uchar);
1616  pfree(buff_conv);
1617  }
1618  else
1619 #endif
1620  {
1622  {
1623  wchar_t *workspace;
1624  size_t curr_char;
1625  size_t result_size;
1626 
1627  /* Overflow paranoia */
1628  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1629  ereport(ERROR,
1630  (errcode(ERRCODE_OUT_OF_MEMORY),
1631  errmsg("out of memory")));
1632 
1633  /* Output workspace cannot have more codes than input bytes */
1634  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1635 
1636  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1637 
1638  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1639  {
1640 #ifdef HAVE_LOCALE_T
1641  if (mylocale)
1642  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1643  else
1644 #endif
1645  workspace[curr_char] = towlower(workspace[curr_char]);
1646  }
1647 
1648  /*
1649  * Make result large enough; case change might change number
1650  * of bytes
1651  */
1652  result_size = curr_char * pg_database_encoding_max_length() + 1;
1653  result = palloc(result_size);
1654 
1655  wchar2char(result, workspace, result_size, mylocale);
1656  pfree(workspace);
1657  }
1658  else
1659  {
1660  char *p;
1661 
1662  result = pnstrdup(buff, nbytes);
1663 
1664  /*
1665  * Note: we assume that tolower_l() will not be so broken as
1666  * to need an isupper_l() guard test. When using the default
1667  * collation, we apply the traditional Postgres behavior that
1668  * forces ASCII-style treatment of I/i, but in non-default
1669  * collations you get exactly what the collation says.
1670  */
1671  for (p = result; *p; p++)
1672  {
1673 #ifdef HAVE_LOCALE_T
1674  if (mylocale)
1675  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1676  else
1677 #endif
1678  *p = pg_tolower((unsigned char) *p);
1679  }
1680  }
1681  }
1682  }
1683 
1684  return result;
1685 }
int errhint(const char *fmt,...)
Definition: elog.c:974
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
#define towlower_l
Definition: win32_port.h:375
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:570
#define tolower_l
Definition: win32_port.h:373
#define OidIsValid(objectId)
Definition: c.h:638
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
#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:1845
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1338
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1774
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1241
union pg_locale_struct::@143 info
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1967

◆ str_toupper()

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

Definition at line 1694 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().

1695 {
1696  char *result;
1697 
1698  if (!buff)
1699  return NULL;
1700 
1701  /* C/POSIX collations use this path regardless of database encoding */
1702  if (lc_ctype_is_c(collid))
1703  {
1704  result = asc_toupper(buff, nbytes);
1705  }
1706  else
1707  {
1708  pg_locale_t mylocale = 0;
1709 
1710  if (collid != DEFAULT_COLLATION_OID)
1711  {
1712  if (!OidIsValid(collid))
1713  {
1714  /*
1715  * This typically means that the parser could not resolve a
1716  * conflict of implicit collations, so report it that way.
1717  */
1718  ereport(ERROR,
1719  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1720  errmsg("could not determine which collation to use for %s function",
1721  "upper()"),
1722  errhint("Use the COLLATE clause to set the collation explicitly.")));
1723  }
1724  mylocale = pg_newlocale_from_collation(collid);
1725  }
1726 
1727 #ifdef USE_ICU
1728  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1729  {
1730  int32_t len_uchar,
1731  len_conv;
1732  UChar *buff_uchar;
1733  UChar *buff_conv;
1734 
1735  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1736  len_conv = icu_convert_case(u_strToUpper, mylocale,
1737  &buff_conv, buff_uchar, len_uchar);
1738  icu_from_uchar(&result, buff_conv, len_conv);
1739  pfree(buff_uchar);
1740  pfree(buff_conv);
1741  }
1742  else
1743 #endif
1744  {
1746  {
1747  wchar_t *workspace;
1748  size_t curr_char;
1749  size_t result_size;
1750 
1751  /* Overflow paranoia */
1752  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1753  ereport(ERROR,
1754  (errcode(ERRCODE_OUT_OF_MEMORY),
1755  errmsg("out of memory")));
1756 
1757  /* Output workspace cannot have more codes than input bytes */
1758  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1759 
1760  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1761 
1762  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1763  {
1764 #ifdef HAVE_LOCALE_T
1765  if (mylocale)
1766  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1767  else
1768 #endif
1769  workspace[curr_char] = towupper(workspace[curr_char]);
1770  }
1771 
1772  /*
1773  * Make result large enough; case change might change number
1774  * of bytes
1775  */
1776  result_size = curr_char * pg_database_encoding_max_length() + 1;
1777  result = palloc(result_size);
1778 
1779  wchar2char(result, workspace, result_size, mylocale);
1780  pfree(workspace);
1781  }
1782  else
1783  {
1784  char *p;
1785 
1786  result = pnstrdup(buff, nbytes);
1787 
1788  /*
1789  * Note: we assume that toupper_l() will not be so broken as
1790  * to need an islower_l() guard test. When using the default
1791  * collation, we apply the traditional Postgres behavior that
1792  * forces ASCII-style treatment of I/i, but in non-default
1793  * collations you get exactly what the collation says.
1794  */
1795  for (p = result; *p; p++)
1796  {
1797 #ifdef HAVE_LOCALE_T
1798  if (mylocale)
1799  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
1800  else
1801 #endif
1802  *p = pg_toupper((unsigned char) *p);
1803  }
1804  }
1805  }
1806  }
1807 
1808  return result;
1809 }
int errhint(const char *fmt,...)
Definition: elog.c:974
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
int errcode(int sqlerrcode)
Definition: elog.c:570
#define OidIsValid(objectId)
Definition: c.h:638
#define towupper_l
Definition: win32_port.h:376
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1990
#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:1845
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1338
#define toupper_l
Definition: win32_port.h:374
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1774
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1241
union pg_locale_struct::@143 info
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105