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 1956 of file formatting.c.

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

Referenced by str_initcap().

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

Definition at line 1910 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), and str_tolower().

1911 {
1912  char *result;
1913  char *p;
1914 
1915  if (!buff)
1916  return NULL;
1917 
1918  result = pnstrdup(buff, nbytes);
1919 
1920  for (p = result; *p; p++)
1921  *p = pg_ascii_tolower((unsigned char) *p);
1922 
1923  return result;
1924 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1087
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1933 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1934 {
1935  char *result;
1936  char *p;
1937 
1938  if (!buff)
1939  return NULL;
1940 
1941  result = pnstrdup(buff, nbytes);
1942 
1943  for (p = result; *p; p++)
1944  *p = pg_ascii_toupper((unsigned char) *p);
1945 
1946  return result;
1947 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1087
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

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

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  else
1532  {
1533  pg_locale_t mylocale = 0;
1534 
1535  if (collid != DEFAULT_COLLATION_OID)
1536  {
1537  if (!OidIsValid(collid))
1538  {
1539  /*
1540  * This typically means that the parser could not resolve a
1541  * conflict of implicit collations, so report it that way.
1542  */
1543  ereport(ERROR,
1544  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1545  errmsg("could not determine which collation to use for lower() function"),
1546  errhint("Use the COLLATE clause to set the collation explicitly.")));
1547  }
1548  mylocale = pg_newlocale_from_collation(collid);
1549  }
1550 
1551 #ifdef USE_ICU
1552  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1553  {
1554  int32_t len_uchar;
1555  int32_t len_conv;
1556  UChar *buff_uchar;
1557  UChar *buff_conv;
1558 
1559  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1560  len_conv = icu_convert_case(u_strToLower, mylocale,
1561  &buff_conv, buff_uchar, len_uchar);
1562  icu_from_uchar(&result, buff_conv, len_conv);
1563  pfree(buff_uchar);
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  else
1606  {
1607  char *p;
1608 
1609  result = pnstrdup(buff, nbytes);
1610 
1611  /*
1612  * Note: we assume that tolower_l() will not be so broken as
1613  * to need an isupper_l() guard test. When using the default
1614  * collation, we apply the traditional Postgres behavior that
1615  * forces ASCII-style treatment of I/i, but in non-default
1616  * collations you get exactly what the collation says.
1617  */
1618  for (p = result; *p; p++)
1619  {
1620 #ifdef HAVE_LOCALE_T
1621  if (mylocale)
1622  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1623  else
1624 #endif
1625  *p = pg_tolower((unsigned char) *p);
1626  }
1627  }
1628  }
1629  }
1630 
1631  return result;
1632 }
#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:1087
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
#define OidIsValid(objectId)
Definition: c.h:532
void pfree(void *pointer)
Definition: mcxt.c:949
#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 tolower_l
Definition: win32.h:324
#define towlower_l
Definition: win32.h:326
void * palloc(Size size)
Definition: mcxt.c:848
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:1910
char* str_toupper ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

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