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

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

Referenced by str_initcap().

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

◆ asc_tolower()

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

Definition at line 1899 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), and str_tolower().

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

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1923 {
1924  char *result;
1925  char *p;
1926 
1927  if (!buff)
1928  return NULL;
1929 
1930  result = pnstrdup(buff, nbytes);
1931 
1932  for (p = result; *p; p++)
1933  *p = pg_ascii_toupper((unsigned char) *p);
1934 
1935  return result;
1936 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1087
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 1752 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().

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

◆ str_tolower()

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

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

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

◆ str_toupper()

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

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

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