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

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

Referenced by str_initcap().

1936 {
1937  char *result;
1938  char *p;
1939  int wasalnum = false;
1940 
1941  if (!buff)
1942  return NULL;
1943 
1944  result = pnstrdup(buff, nbytes);
1945 
1946  for (p = result; *p; p++)
1947  {
1948  char c;
1949 
1950  if (wasalnum)
1951  *p = c = pg_ascii_tolower((unsigned char) *p);
1952  else
1953  *p = c = pg_ascii_toupper((unsigned char) *p);
1954  /* we don't trust isalnum() here */
1955  wasalnum = ((c >= 'A' && c <= 'Z') ||
1956  (c >= 'a' && c <= 'z') ||
1957  (c >= '0' && c <= '9'));
1958  }
1959 
1960  return result;
1961 }
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:1618
char * c
#define NULL
Definition: c.h:229
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1889 of file formatting.c.

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

Referenced by asc_tolower_z(), and str_tolower().

1890 {
1891  char *result;
1892  char *p;
1893 
1894  if (!buff)
1895  return NULL;
1896 
1897  result = pnstrdup(buff, nbytes);
1898 
1899  for (p = result; *p; p++)
1900  *p = pg_ascii_tolower((unsigned char) *p);
1901 
1902  return result;
1903 }
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:1618
#define NULL
Definition: c.h:229
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1912 of file formatting.c.

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

Referenced by asc_toupper_z(), and str_toupper().

1913 {
1914  char *result;
1915  char *p;
1916 
1917  if (!buff)
1918  return NULL;
1919 
1920  result = pnstrdup(buff, nbytes);
1921 
1922  for (p = result; *p; p++)
1923  *p = pg_ascii_toupper((unsigned char) *p);
1924 
1925  return result;
1926 }
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:1618
#define NULL
Definition: c.h:229
char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

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

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

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 #ifdef USE_WIDE_UPPER_LOWER
1522  else
1523  {
1524  pg_locale_t mylocale = 0;
1525 
1526  if (collid != DEFAULT_COLLATION_OID)
1527  {
1528  if (!OidIsValid(collid))
1529  {
1530  /*
1531  * This typically means that the parser could not resolve a
1532  * conflict of implicit collations, so report it that way.
1533  */
1534  ereport(ERROR,
1535  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1536  errmsg("could not determine which collation to use for lower() function"),
1537  errhint("Use the COLLATE clause to set the collation explicitly.")));
1538  }
1539  mylocale = pg_newlocale_from_collation(collid);
1540  }
1541 
1542 #ifdef USE_ICU
1543  if (mylocale && mylocale->provider == COLLPROVIDER_ICU)
1544  {
1545  int32_t len_uchar;
1546  int32_t len_conv;
1547  UChar *buff_uchar;
1548  UChar *buff_conv;
1549 
1550  len_uchar = icu_to_uchar(&buff_uchar, buff, nbytes);
1551  len_conv = icu_convert_case(u_strToLower, mylocale, &buff_conv, buff_uchar, len_uchar);
1552  icu_from_uchar(&result, buff_conv, len_conv);
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  /* Make result large enough; case change might change number of bytes */
1585  result_size = curr_char * pg_database_encoding_max_length() + 1;
1586  result = palloc(result_size);
1587 
1588  wchar2char(result, workspace, result_size, mylocale);
1589  pfree(workspace);
1590  }
1591 #endif /* USE_WIDE_UPPER_LOWER */
1592  else
1593  {
1594  char *p;
1595 
1596  result = pnstrdup(buff, nbytes);
1597 
1598  /*
1599  * Note: we assume that tolower_l() will not be so broken as to need
1600  * an isupper_l() guard test. When using the default collation, we
1601  * apply the traditional Postgres behavior that forces ASCII-style
1602  * treatment of I/i, but in non-default collations you get exactly
1603  * what the collation says.
1604  */
1605  for (p = result; *p; p++)
1606  {
1607 #ifdef HAVE_LOCALE_T
1608  if (mylocale)
1609  *p = tolower_l((unsigned char) *p, mylocale->info.lt);
1610  else
1611 #endif
1612  *p = pg_tolower((unsigned char) *p);
1613  }
1614  }
1615  }
#define COLLPROVIDER_ICU
Definition: pg_collation.h:84
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
union pg_locale_struct::@113 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:1618
#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:74
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:334
#define towlower_l
Definition: win32.h:336
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:1889
char* str_toupper ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

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

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