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

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

Referenced by str_initcap().

1947 {
1948  char *result;
1949  char *p;
1950  int wasalnum = false;
1951 
1952  if (!buff)
1953  return NULL;
1954 
1955  result = pnstrdup(buff, nbytes);
1956 
1957  for (p = result; *p; p++)
1958  {
1959  char c;
1960 
1961  if (wasalnum)
1962  *p = c = pg_ascii_tolower((unsigned char) *p);
1963  else
1964  *p = c = pg_ascii_toupper((unsigned char) *p);
1965  /* we don't trust isalnum() here */
1966  wasalnum = ((c >= 'A' && c <= 'Z') ||
1967  (c >= 'a' && c <= 'z') ||
1968  (c >= '0' && c <= '9'));
1969  }
1970 
1971  return result;
1972 }
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 1900 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), and str_tolower().

1901 {
1902  char *result;
1903  char *p;
1904 
1905  if (!buff)
1906  return NULL;
1907 
1908  result = pnstrdup(buff, nbytes);
1909 
1910  for (p = result; *p; p++)
1911  *p = pg_ascii_tolower((unsigned char) *p);
1912 
1913  return result;
1914 }
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 1923 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1924 {
1925  char *result;
1926  char *p;
1927 
1928  if (!buff)
1929  return NULL;
1930 
1931  result = pnstrdup(buff, nbytes);
1932 
1933  for (p = result; *p; p++)
1934  *p = pg_ascii_toupper((unsigned char) *p);
1935 
1936  return result;
1937 }
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 1753 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().

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

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

◆ str_toupper()

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

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

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