PostgreSQL Source Code  git master
formatting.h File Reference
#include "fmgr.h"
Include dependency graph for formatting.h:
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 1953 of file formatting.c.

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

Referenced by str_initcap().

1954 {
1955  char *result;
1956  char *p;
1957  int wasalnum = false;
1958 
1959  if (!buff)
1960  return NULL;
1961 
1962  result = pnstrdup(buff, nbytes);
1963 
1964  for (p = result; *p; p++)
1965  {
1966  char c;
1967 
1968  if (wasalnum)
1969  *p = c = pg_ascii_tolower((unsigned char) *p);
1970  else
1971  *p = c = pg_ascii_toupper((unsigned char) *p);
1972  /* we don't trust isalnum() here */
1973  wasalnum = ((c >= 'A' && c <= 'Z') ||
1974  (c >= 'a' && c <= 'z') ||
1975  (c >= '0' && c <= '9'));
1976  }
1977 
1978  return result;
1979 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
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 1907 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), and str_tolower().

1908 {
1909  char *result;
1910  char *p;
1911 
1912  if (!buff)
1913  return NULL;
1914 
1915  result = pnstrdup(buff, nbytes);
1916 
1917  for (p = result; *p; p++)
1918  *p = pg_ascii_tolower((unsigned char) *p);
1919 
1920  return result;
1921 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
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 1930 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1931 {
1932  char *result;
1933  char *p;
1934 
1935  if (!buff)
1936  return NULL;
1937 
1938  result = pnstrdup(buff, nbytes);
1939 
1940  for (p = result; *p; p++)
1941  *p = pg_ascii_toupper((unsigned char) *p);
1942 
1943  return result;
1944 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
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 1760 of file formatting.c.

References asc_initcap(), char2wchar(), COLLPROVIDER_ICU, DEFAULT_COLLATION_OID, 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().

1761 {
1762  char *result;
1763  int wasalnum = false;
1764 
1765  if (!buff)
1766  return NULL;
1767 
1768  /* C/POSIX collations use this path regardless of database encoding */
1769  if (lc_ctype_is_c(collid))
1770  {
1771  result = asc_initcap(buff, nbytes);
1772  }
1773  else
1774  {
1775  pg_locale_t mylocale = 0;
1776 
1777  if (collid != DEFAULT_COLLATION_OID)
1778  {
1779  if (!OidIsValid(collid))
1780  {
1781  /*
1782  * This typically means that the parser could not resolve a
1783  * conflict of implicit collations, so report it that way.
1784  */
1785  ereport(ERROR,
1786  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1787  errmsg("could not determine which collation to use for initcap() function"),
1788  errhint("Use the COLLATE clause to set the collation explicitly.")));
1789  }
1790  mylocale = pg_newlocale_from_collation(collid);
1791  }
1792 
1793 #ifdef USE_ICU
1794  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1795  {
1796  int32_t len_uchar,
1797  len_conv;
1798  UChar *buff_uchar;
1799  UChar *buff_conv;
1800 
1801  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1802  len_conv = icu_convert_case(u_strToTitle_default_BI, mylocale,
1803  &buff_conv, buff_uchar, len_uchar);
1804  icu_from_uchar(&result, buff_conv, len_conv);
1805  pfree(buff_uchar);
1806  }
1807  else
1808 #endif
1809  {
1811  {
1812  wchar_t *workspace;
1813  size_t curr_char;
1814  size_t result_size;
1815 
1816  /* Overflow paranoia */
1817  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1818  ereport(ERROR,
1819  (errcode(ERRCODE_OUT_OF_MEMORY),
1820  errmsg("out of memory")));
1821 
1822  /* Output workspace cannot have more codes than input bytes */
1823  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1824 
1825  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1826 
1827  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1828  {
1829 #ifdef HAVE_LOCALE_T
1830  if (mylocale)
1831  {
1832  if (wasalnum)
1833  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1834  else
1835  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1836  wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt);
1837  }
1838  else
1839 #endif
1840  {
1841  if (wasalnum)
1842  workspace[curr_char] = towlower(workspace[curr_char]);
1843  else
1844  workspace[curr_char] = towupper(workspace[curr_char]);
1845  wasalnum = iswalnum(workspace[curr_char]);
1846  }
1847  }
1848 
1849  /*
1850  * Make result large enough; case change might change number
1851  * of bytes
1852  */
1853  result_size = curr_char * pg_database_encoding_max_length() + 1;
1854  result = palloc(result_size);
1855 
1856  wchar2char(result, workspace, result_size, mylocale);
1857  pfree(workspace);
1858  }
1859  else
1860  {
1861  char *p;
1862 
1863  result = pnstrdup(buff, nbytes);
1864 
1865  /*
1866  * Note: we assume that toupper_l()/tolower_l() will not be so
1867  * broken as to need guard tests. When using the default
1868  * collation, we apply the traditional Postgres behavior that
1869  * forces ASCII-style treatment of I/i, but in non-default
1870  * collations you get exactly what the collation says.
1871  */
1872  for (p = result; *p; p++)
1873  {
1874 #ifdef HAVE_LOCALE_T
1875  if (mylocale)
1876  {
1877  if (wasalnum)
1878  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1879  else
1880  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
1881  wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt);
1882  }
1883  else
1884 #endif
1885  {
1886  if (wasalnum)
1887  *p = pg_tolower((unsigned char) *p);
1888  else
1889  *p = pg_toupper((unsigned char) *p);
1890  wasalnum = isalnum((unsigned char) *p);
1891  }
1892  }
1893  }
1894  }
1895  }
1896 
1897  return result;
1898 }
#define COLLPROVIDER_ICU
Definition: pg_collation.h:85
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
#define towlower_l
Definition: win32_port.h:377
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:575
union pg_locale_struct::@129 info
#define tolower_l
Definition: win32_port.h:375
#define OidIsValid(objectId)
Definition: c.h:594
#define towupper_l
Definition: win32_port.h:378
void pfree(void *pointer)
Definition: mcxt.c:936
#define isalnum_l
Definition: win32_port.h:383
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define ereport(elevel, rest)
Definition: elog.h:122
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1677
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1275
#define toupper_l
Definition: win32_port.h:376
char * asc_initcap(const char *buff, size_t nbytes)
Definition: formatting.c:1953
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1606
int int32_t
Definition: crypt.c:108
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define iswalnum_l
Definition: win32_port.h:384

◆ str_tolower()

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

Definition at line 1516 of file formatting.c.

References asc_tolower(), char2wchar(), COLLPROVIDER_ICU, DEFAULT_COLLATION_OID, 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_ne(), citextcmp(), internal_citext_pattern_cmp(), lower(), ltree_strncasecmp(), and str_tolower_z().

1517 {
1518  char *result;
1519 
1520  if (!buff)
1521  return NULL;
1522 
1523  /* C/POSIX collations use this path regardless of database encoding */
1524  if (lc_ctype_is_c(collid))
1525  {
1526  result = asc_tolower(buff, nbytes);
1527  }
1528  else
1529  {
1530  pg_locale_t mylocale = 0;
1531 
1532  if (collid != DEFAULT_COLLATION_OID)
1533  {
1534  if (!OidIsValid(collid))
1535  {
1536  /*
1537  * This typically means that the parser could not resolve a
1538  * conflict of implicit collations, so report it that way.
1539  */
1540  ereport(ERROR,
1541  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1542  errmsg("could not determine which collation to use for lower() function"),
1543  errhint("Use the COLLATE clause to set the collation explicitly.")));
1544  }
1545  mylocale = pg_newlocale_from_collation(collid);
1546  }
1547 
1548 #ifdef USE_ICU
1549  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1550  {
1551  int32_t len_uchar;
1552  int32_t len_conv;
1553  UChar *buff_uchar;
1554  UChar *buff_conv;
1555 
1556  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1557  len_conv = icu_convert_case(u_strToLower, mylocale,
1558  &buff_conv, buff_uchar, len_uchar);
1559  icu_from_uchar(&result, buff_conv, len_conv);
1560  pfree(buff_uchar);
1561  }
1562  else
1563 #endif
1564  {
1566  {
1567  wchar_t *workspace;
1568  size_t curr_char;
1569  size_t result_size;
1570 
1571  /* Overflow paranoia */
1572  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1573  ereport(ERROR,
1574  (errcode(ERRCODE_OUT_OF_MEMORY),
1575  errmsg("out of memory")));
1576 
1577  /* Output workspace cannot have more codes than input bytes */
1578  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1579 
1580  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1581 
1582  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1583  {
1584 #ifdef HAVE_LOCALE_T
1585  if (mylocale)
1586  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1587  else
1588 #endif
1589  workspace[curr_char] = towlower(workspace[curr_char]);
1590  }
1591 
1592  /*
1593  * Make result large enough; case change might change number
1594  * of bytes
1595  */
1596  result_size = curr_char * pg_database_encoding_max_length() + 1;
1597  result = palloc(result_size);
1598 
1599  wchar2char(result, workspace, result_size, mylocale);
1600  pfree(workspace);
1601  }
1602  else
1603  {
1604  char *p;
1605 
1606  result = pnstrdup(buff, nbytes);
1607 
1608  /*
1609  * Note: we assume that tolower_l() will not be so broken as
1610  * to need an isupper_l() guard test. When using the default
1611  * collation, we apply the traditional Postgres behavior that
1612  * forces ASCII-style treatment of I/i, but in non-default
1613  * collations you get exactly what the collation says.
1614  */
1615  for (p = result; *p; p++)
1616  {
1617 #ifdef HAVE_LOCALE_T
1618  if (mylocale)
1619  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1620  else
1621 #endif
1622  *p = pg_tolower((unsigned char) *p);
1623  }
1624  }
1625  }
1626  }
1627 
1628  return result;
1629 }
#define COLLPROVIDER_ICU
Definition: pg_collation.h:85
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1074
#define towlower_l
Definition: win32_port.h:377
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int errcode(int sqlerrcode)
Definition: elog.c:575
union pg_locale_struct::@129 info
#define tolower_l
Definition: win32_port.h:375
#define OidIsValid(objectId)
Definition: c.h:594
void pfree(void *pointer)
Definition: mcxt.c:936
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define ereport(elevel, rest)
Definition: elog.h:122
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1677
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1275
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1606
int int32_t
Definition: crypt.c:108
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1907

◆ str_toupper()

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

Definition at line 1638 of file formatting.c.

References asc_toupper(), char2wchar(), COLLPROVIDER_ICU, DEFAULT_COLLATION_OID, 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().

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