PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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

Definition at line 1959 of file formatting.c.

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

Referenced by str_initcap().

1960 {
1961  char *result;
1962  char *p;
1963  int wasalnum = false;
1964 
1965  if (!buff)
1966  return NULL;
1967 
1968  result = pnstrdup(buff, nbytes);
1969 
1970  for (p = result; *p; p++)
1971  {
1972  char c;
1973 
1974  if (wasalnum)
1975  *p = c = pg_ascii_tolower((unsigned char) *p);
1976  else
1977  *p = c = pg_ascii_toupper((unsigned char) *p);
1978  /* we don't trust isalnum() here */
1979  wasalnum = ((c >= 'A' && c <= 'Z') ||
1980  (c >= 'a' && c <= 'z') ||
1981  (c >= '0' && c <= '9'));
1982  }
1983 
1984  return result;
1985 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
return result
Definition: formatting.c:1632
char * c
#define NULL
Definition: c.h:229
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1913 of file formatting.c.

References NULL, pg_ascii_tolower(), pnstrdup(), and result.

Referenced by asc_tolower_z(), and str_tolower().

1914 {
1915  char *result;
1916  char *p;
1917 
1918  if (!buff)
1919  return NULL;
1920 
1921  result = pnstrdup(buff, nbytes);
1922 
1923  for (p = result; *p; p++)
1924  *p = pg_ascii_tolower((unsigned char) *p);
1925 
1926  return result;
1927 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
return result
Definition: formatting.c:1632
#define NULL
Definition: c.h:229
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1936 of file formatting.c.

References NULL, pg_ascii_toupper(), pnstrdup(), and result.

Referenced by asc_toupper_z(), and str_toupper().

1937 {
1938  char *result;
1939  char *p;
1940 
1941  if (!buff)
1942  return NULL;
1943 
1944  result = pnstrdup(buff, nbytes);
1945 
1946  for (p = result; *p; p++)
1947  *p = pg_ascii_toupper((unsigned char) *p);
1948 
1949  return result;
1950 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
return result
Definition: formatting.c:1632
#define NULL
Definition: c.h:229
char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1765 of file formatting.c.

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

Referenced by initcap(), and str_initcap_z().

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

Definition at line 1519 of file formatting.c.

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

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

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

Definition at line 1642 of file formatting.c.

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

Referenced by str_toupper_z(), and upper().

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