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

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

Referenced by str_initcap().

1963 {
1964  char *result;
1965  char *p;
1966  int wasalnum = false;
1967 
1968  if (!buff)
1969  return NULL;
1970 
1971  result = pnstrdup(buff, nbytes);
1972 
1973  for (p = result; *p; p++)
1974  {
1975  char c;
1976 
1977  if (wasalnum)
1978  *p = c = pg_ascii_tolower((unsigned char) *p);
1979  else
1980  *p = c = pg_ascii_toupper((unsigned char) *p);
1981  /* we don't trust isalnum() here */
1982  wasalnum = ((c >= 'A' && c <= 'Z') ||
1983  (c >= 'a' && c <= 'z') ||
1984  (c >= '0' && c <= '9'));
1985  }
1986 
1987  return result;
1988 }
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:1633
char * c
#define NULL
Definition: c.h:229
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1916 of file formatting.c.

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

Referenced by asc_tolower_z(), and str_tolower().

1917 {
1918  char *result;
1919  char *p;
1920 
1921  if (!buff)
1922  return NULL;
1923 
1924  result = pnstrdup(buff, nbytes);
1925 
1926  for (p = result; *p; p++)
1927  *p = pg_ascii_tolower((unsigned char) *p);
1928 
1929  return result;
1930 }
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:1633
#define NULL
Definition: c.h:229
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1939 of file formatting.c.

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

Referenced by asc_toupper_z(), and str_toupper().

1940 {
1941  char *result;
1942  char *p;
1943 
1944  if (!buff)
1945  return NULL;
1946 
1947  result = pnstrdup(buff, nbytes);
1948 
1949  for (p = result; *p; p++)
1950  *p = pg_ascii_toupper((unsigned char) *p);
1951 
1952  return result;
1953 }
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:1633
#define NULL
Definition: c.h:229
char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

1768 {
1769  char *result;
1770  int wasalnum = false;
1771 
1772  if (!buff)
1773  return NULL;
1774 
1775  /* C/POSIX collations use this path regardless of database encoding */
1776  if (lc_ctype_is_c(collid))
1777  {
1778  result = asc_initcap(buff, nbytes);
1779  }
1780 #ifdef USE_WIDE_UPPER_LOWER
1781  else
1782  {
1783  pg_locale_t mylocale = 0;
1784 
1785  if (collid != DEFAULT_COLLATION_OID)
1786  {
1787  if (!OidIsValid(collid))
1788  {
1789  /*
1790  * This typically means that the parser could not resolve a
1791  * conflict of implicit collations, so report it that way.
1792  */
1793  ereport(ERROR,
1794  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1795  errmsg("could not determine which collation to use for initcap() function"),
1796  errhint("Use the COLLATE clause to set the collation explicitly.")));
1797  }
1798  mylocale = pg_newlocale_from_collation(collid);
1799  }
1800 
1801 #ifdef USE_ICU
1802  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1803  {
1804  int32_t len_uchar,
1805  len_conv;
1806  UChar *buff_uchar;
1807  UChar *buff_conv;
1808 
1809  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1810  len_conv = icu_convert_case(u_strToTitle_default_BI, mylocale,
1811  &buff_conv, buff_uchar, len_uchar);
1812  icu_from_uchar(&result, buff_conv, len_conv);
1813  pfree(buff_uchar);
1814  }
1815  else
1816 #endif
1817  {
1819  {
1820  wchar_t *workspace;
1821  size_t curr_char;
1822  size_t result_size;
1823 
1824  /* Overflow paranoia */
1825  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1826  ereport(ERROR,
1827  (errcode(ERRCODE_OUT_OF_MEMORY),
1828  errmsg("out of memory")));
1829 
1830  /* Output workspace cannot have more codes than input bytes */
1831  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1832 
1833  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1834 
1835  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1836  {
1837 #ifdef HAVE_LOCALE_T
1838  if (mylocale)
1839  {
1840  if (wasalnum)
1841  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1842  else
1843  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt);
1844  wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt);
1845  }
1846  else
1847 #endif
1848  {
1849  if (wasalnum)
1850  workspace[curr_char] = towlower(workspace[curr_char]);
1851  else
1852  workspace[curr_char] = towupper(workspace[curr_char]);
1853  wasalnum = iswalnum(workspace[curr_char]);
1854  }
1855  }
1856 
1857  /*
1858  * Make result large enough; case change might change number
1859  * of bytes
1860  */
1861  result_size = curr_char * pg_database_encoding_max_length() + 1;
1862  result = palloc(result_size);
1863 
1864  wchar2char(result, workspace, result_size, mylocale);
1865  pfree(workspace);
1866  }
1867 #endif /* USE_WIDE_UPPER_LOWER */
1868  else
1869  {
1870  char *p;
1871 
1872  result = pnstrdup(buff, nbytes);
1873 
1874  /*
1875  * Note: we assume that toupper_l()/tolower_l() will not be so
1876  * broken as to need guard tests. When using the default
1877  * collation, we apply the traditional Postgres behavior that
1878  * forces ASCII-style treatment of I/i, but in non-default
1879  * collations you get exactly what the collation says.
1880  */
1881  for (p = result; *p; p++)
1882  {
1883 #ifdef HAVE_LOCALE_T
1884  if (mylocale)
1885  {
1886  if (wasalnum)
1887  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1888  else
1889  *p = toupper_l((unsigned char) *p, mylocale->info.lt);
1890  wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt);
1891  }
1892  else
1893 #endif
1894  {
1895  if (wasalnum)
1896  *p = pg_tolower((unsigned char) *p);
1897  else
1898  *p = pg_toupper((unsigned char) *p);
1899  wasalnum = isalnum((unsigned char) *p);
1900  }
1901  }
1902  }
1903  }
#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:1633
#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:1962
#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  pfree(buff_uchar);
1565  }
1566  else
1567 #endif
1568  {
1570  {
1571  wchar_t *workspace;
1572  size_t curr_char;
1573  size_t result_size;
1574 
1575  /* Overflow paranoia */
1576  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1577  ereport(ERROR,
1578  (errcode(ERRCODE_OUT_OF_MEMORY),
1579  errmsg("out of memory")));
1580 
1581  /* Output workspace cannot have more codes than input bytes */
1582  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1583 
1584  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1585 
1586  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1587  {
1588 #ifdef HAVE_LOCALE_T
1589  if (mylocale)
1590  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt);
1591  else
1592 #endif
1593  workspace[curr_char] = towlower(workspace[curr_char]);
1594  }
1595 
1596  /*
1597  * Make result large enough; case change might change number
1598  * of bytes
1599  */
1600  result_size = curr_char * pg_database_encoding_max_length() + 1;
1601  result = palloc(result_size);
1602 
1603  wchar2char(result, workspace, result_size, mylocale);
1604  pfree(workspace);
1605  }
1606 #endif /* USE_WIDE_UPPER_LOWER */
1607  else
1608  {
1609  char *p;
1610 
1611  result = pnstrdup(buff, nbytes);
1612 
1613  /*
1614  * Note: we assume that tolower_l() will not be so broken as
1615  * to need an isupper_l() guard test. When using the default
1616  * collation, we apply the traditional Postgres behavior that
1617  * forces ASCII-style treatment of I/i, but in non-default
1618  * collations you get exactly what the collation says.
1619  */
1620  for (p = result; *p; p++)
1621  {
1622 #ifdef HAVE_LOCALE_T
1623  if (mylocale)
1624  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1625  else
1626 #endif
1627  *p = pg_tolower((unsigned char) *p);
1628  }
1629  }
1630  }
#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:1633
#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:1916
char* str_toupper ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

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