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

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

Referenced by str_initcap().

1901 {
1902  char *result;
1903  char *p;
1904  int wasalnum = false;
1905 
1906  if (!buff)
1907  return NULL;
1908 
1909  result = pnstrdup(buff, nbytes);
1910 
1911  for (p = result; *p; p++)
1912  {
1913  char c;
1914 
1915  if (wasalnum)
1916  *p = c = pg_ascii_tolower((unsigned char) *p);
1917  else
1918  *p = c = pg_ascii_toupper((unsigned char) *p);
1919  /* we don't trust isalnum() here */
1920  wasalnum = ((c >= 'A' && c <= 'Z') ||
1921  (c >= 'a' && c <= 'z') ||
1922  (c >= '0' && c <= '9'));
1923  }
1924 
1925  return result;
1926 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
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
#define NULL
Definition: c.h:226
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1854 of file formatting.c.

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

Referenced by asc_tolower_z(), and str_tolower().

1855 {
1856  char *result;
1857  char *p;
1858 
1859  if (!buff)
1860  return NULL;
1861 
1862  result = pnstrdup(buff, nbytes);
1863 
1864  for (p = result; *p; p++)
1865  *p = pg_ascii_tolower((unsigned char) *p);
1866 
1867  return result;
1868 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
#define NULL
Definition: c.h:226
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1877 of file formatting.c.

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

Referenced by asc_toupper_z(), and str_toupper().

1878 {
1879  char *result;
1880  char *p;
1881 
1882  if (!buff)
1883  return NULL;
1884 
1885  result = pnstrdup(buff, nbytes);
1886 
1887  for (p = result; *p; p++)
1888  *p = pg_ascii_toupper((unsigned char) *p);
1889 
1890  return result;
1891 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
#define NULL
Definition: c.h:226
char* str_initcap ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1709 of file formatting.c.

References asc_initcap(), DEFAULT_COLLATION_OID, ereport, errcode(), errhint(), errmsg(), ERROR, 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(), tolower_l, toupper_l, towlower_l, and towupper_l.

Referenced by initcap(), and str_initcap_z().

1710 {
1711  char *result;
1712  int wasalnum = false;
1713 
1714  if (!buff)
1715  return NULL;
1716 
1717  /* C/POSIX collations use this path regardless of database encoding */
1718  if (lc_ctype_is_c(collid))
1719  {
1720  result = asc_initcap(buff, nbytes);
1721  }
1722 #ifdef USE_WIDE_UPPER_LOWER
1723  else if (pg_database_encoding_max_length() > 1)
1724  {
1725  pg_locale_t mylocale = 0;
1726  wchar_t *workspace;
1727  size_t curr_char;
1728  size_t result_size;
1729 
1730  if (collid != DEFAULT_COLLATION_OID)
1731  {
1732  if (!OidIsValid(collid))
1733  {
1734  /*
1735  * This typically means that the parser could not resolve a
1736  * conflict of implicit collations, so report it that way.
1737  */
1738  ereport(ERROR,
1739  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1740  errmsg("could not determine which collation to use for initcap() function"),
1741  errhint("Use the COLLATE clause to set the collation explicitly.")));
1742  }
1743  mylocale = pg_newlocale_from_collation(collid);
1744  }
1745 
1746  /* Overflow paranoia */
1747  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1748  ereport(ERROR,
1749  (errcode(ERRCODE_OUT_OF_MEMORY),
1750  errmsg("out of memory")));
1751 
1752  /* Output workspace cannot have more codes than input bytes */
1753  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1754 
1755  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1756 
1757  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1758  {
1759 #ifdef HAVE_LOCALE_T
1760  if (mylocale)
1761  {
1762  if (wasalnum)
1763  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale);
1764  else
1765  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale);
1766  wasalnum = iswalnum_l(workspace[curr_char], mylocale);
1767  }
1768  else
1769 #endif
1770  {
1771  if (wasalnum)
1772  workspace[curr_char] = towlower(workspace[curr_char]);
1773  else
1774  workspace[curr_char] = towupper(workspace[curr_char]);
1775  wasalnum = iswalnum(workspace[curr_char]);
1776  }
1777  }
1778 
1779  /* Make result large enough; case change might change number of bytes */
1780  result_size = curr_char * pg_database_encoding_max_length() + 1;
1781  result = palloc(result_size);
1782 
1783  wchar2char(result, workspace, result_size, mylocale);
1784  pfree(workspace);
1785  }
1786 #endif /* USE_WIDE_UPPER_LOWER */
1787  else
1788  {
1789 #ifdef HAVE_LOCALE_T
1790  pg_locale_t mylocale = 0;
1791 #endif
1792  char *p;
1793 
1794  if (collid != DEFAULT_COLLATION_OID)
1795  {
1796  if (!OidIsValid(collid))
1797  {
1798  /*
1799  * This typically means that the parser could not resolve a
1800  * conflict of implicit collations, so report it that way.
1801  */
1802  ereport(ERROR,
1803  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1804  errmsg("could not determine which collation to use for initcap() function"),
1805  errhint("Use the COLLATE clause to set the collation explicitly.")));
1806  }
1807 #ifdef HAVE_LOCALE_T
1808  mylocale = pg_newlocale_from_collation(collid);
1809 #endif
1810  }
1811 
1812  result = pnstrdup(buff, nbytes);
1813 
1814  /*
1815  * Note: we assume that toupper_l()/tolower_l() will not be so broken
1816  * as to need guard tests. When using the default collation, we apply
1817  * the traditional Postgres behavior that forces ASCII-style treatment
1818  * of I/i, but in non-default collations you get exactly what the
1819  * collation says.
1820  */
1821  for (p = result; *p; p++)
1822  {
1823 #ifdef HAVE_LOCALE_T
1824  if (mylocale)
1825  {
1826  if (wasalnum)
1827  *p = tolower_l((unsigned char) *p, mylocale);
1828  else
1829  *p = toupper_l((unsigned char) *p, mylocale);
1830  wasalnum = isalnum_l((unsigned char) *p, mylocale);
1831  }
1832  else
1833 #endif
1834  {
1835  if (wasalnum)
1836  *p = pg_tolower((unsigned char) *p);
1837  else
1838  *p = pg_toupper((unsigned char) *p);
1839  wasalnum = isalnum((unsigned char) *p);
1840  }
1841  }
1842  }
1843 
1844  return result;
1845 }
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
#define toupper_l
Definition: win32.h:335
#define iswalnum_l
Definition: win32.h:343
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:533
int pg_locale_t
Definition: pg_locale.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:68
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:1260
char * asc_initcap(const char *buff, size_t nbytes)
Definition: formatting.c:1900
#define NULL
Definition: c.h:226
#define tolower_l
Definition: win32.h:334
#define towlower_l
Definition: win32.h:336
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1173
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 1469 of file formatting.c.

References asc_tolower(), DEFAULT_COLLATION_OID, ereport, errcode(), errhint(), errmsg(), ERROR, lc_ctype_is_c(), NULL, OidIsValid, palloc(), pfree(), pg_database_encoding_max_length(), pg_newlocale_from_collation(), pg_tolower(), pnstrdup(), tolower_l, and towlower_l.

Referenced by citext_eq(), citext_hash(), citext_ne(), citextcmp(), lower(), ltree_strncasecmp(), and str_tolower_z().

1470 {
1471  char *result;
1472 
1473  if (!buff)
1474  return NULL;
1475 
1476  /* C/POSIX collations use this path regardless of database encoding */
1477  if (lc_ctype_is_c(collid))
1478  {
1479  result = asc_tolower(buff, nbytes);
1480  }
1481 #ifdef USE_WIDE_UPPER_LOWER
1482  else if (pg_database_encoding_max_length() > 1)
1483  {
1484  pg_locale_t mylocale = 0;
1485  wchar_t *workspace;
1486  size_t curr_char;
1487  size_t result_size;
1488 
1489  if (collid != DEFAULT_COLLATION_OID)
1490  {
1491  if (!OidIsValid(collid))
1492  {
1493  /*
1494  * This typically means that the parser could not resolve a
1495  * conflict of implicit collations, so report it that way.
1496  */
1497  ereport(ERROR,
1498  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1499  errmsg("could not determine which collation to use for lower() function"),
1500  errhint("Use the COLLATE clause to set the collation explicitly.")));
1501  }
1502  mylocale = pg_newlocale_from_collation(collid);
1503  }
1504 
1505  /* Overflow paranoia */
1506  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1507  ereport(ERROR,
1508  (errcode(ERRCODE_OUT_OF_MEMORY),
1509  errmsg("out of memory")));
1510 
1511  /* Output workspace cannot have more codes than input bytes */
1512  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1513 
1514  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1515 
1516  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1517  {
1518 #ifdef HAVE_LOCALE_T
1519  if (mylocale)
1520  workspace[curr_char] = towlower_l(workspace[curr_char], mylocale);
1521  else
1522 #endif
1523  workspace[curr_char] = towlower(workspace[curr_char]);
1524  }
1525 
1526  /* Make result large enough; case change might change number of bytes */
1527  result_size = curr_char * pg_database_encoding_max_length() + 1;
1528  result = palloc(result_size);
1529 
1530  wchar2char(result, workspace, result_size, mylocale);
1531  pfree(workspace);
1532  }
1533 #endif /* USE_WIDE_UPPER_LOWER */
1534  else
1535  {
1536 #ifdef HAVE_LOCALE_T
1537  pg_locale_t mylocale = 0;
1538 #endif
1539  char *p;
1540 
1541  if (collid != DEFAULT_COLLATION_OID)
1542  {
1543  if (!OidIsValid(collid))
1544  {
1545  /*
1546  * This typically means that the parser could not resolve a
1547  * conflict of implicit collations, so report it that way.
1548  */
1549  ereport(ERROR,
1550  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1551  errmsg("could not determine which collation to use for lower() function"),
1552  errhint("Use the COLLATE clause to set the collation explicitly.")));
1553  }
1554 #ifdef HAVE_LOCALE_T
1555  mylocale = pg_newlocale_from_collation(collid);
1556 #endif
1557  }
1558 
1559  result = pnstrdup(buff, nbytes);
1560 
1561  /*
1562  * Note: we assume that tolower_l() will not be so broken as to need
1563  * an isupper_l() guard test. When using the default collation, we
1564  * apply the traditional Postgres behavior that forces ASCII-style
1565  * treatment of I/i, but in non-default collations you get exactly
1566  * what the collation says.
1567  */
1568  for (p = result; *p; p++)
1569  {
1570 #ifdef HAVE_LOCALE_T
1571  if (mylocale)
1572  *p = tolower_l((unsigned char) *p, mylocale);
1573  else
1574 #endif
1575  *p = pg_tolower((unsigned char) *p);
1576  }
1577  }
1578 
1579  return result;
1580 }
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
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:533
int pg_locale_t
Definition: pg_locale.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:68
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:1260
#define NULL
Definition: c.h:226
#define tolower_l
Definition: win32.h:334
#define towlower_l
Definition: win32.h:336
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1173
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1854
char* str_toupper ( const char *  buff,
size_t  nbytes,
Oid  collid 
)

Definition at line 1589 of file formatting.c.

References asc_toupper(), DEFAULT_COLLATION_OID, ereport, errcode(), errhint(), errmsg(), ERROR, lc_ctype_is_c(), NULL, OidIsValid, palloc(), pfree(), pg_database_encoding_max_length(), pg_newlocale_from_collation(), pg_toupper(), pnstrdup(), toupper_l, and towupper_l.

Referenced by str_toupper_z(), and upper().

1590 {
1591  char *result;
1592 
1593  if (!buff)
1594  return NULL;
1595 
1596  /* C/POSIX collations use this path regardless of database encoding */
1597  if (lc_ctype_is_c(collid))
1598  {
1599  result = asc_toupper(buff, nbytes);
1600  }
1601 #ifdef USE_WIDE_UPPER_LOWER
1602  else if (pg_database_encoding_max_length() > 1)
1603  {
1604  pg_locale_t mylocale = 0;
1605  wchar_t *workspace;
1606  size_t curr_char;
1607  size_t result_size;
1608 
1609  if (collid != DEFAULT_COLLATION_OID)
1610  {
1611  if (!OidIsValid(collid))
1612  {
1613  /*
1614  * This typically means that the parser could not resolve a
1615  * conflict of implicit collations, so report it that way.
1616  */
1617  ereport(ERROR,
1618  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1619  errmsg("could not determine which collation to use for upper() function"),
1620  errhint("Use the COLLATE clause to set the collation explicitly.")));
1621  }
1622  mylocale = pg_newlocale_from_collation(collid);
1623  }
1624 
1625  /* Overflow paranoia */
1626  if ((nbytes + 1) > (INT_MAX / sizeof(wchar_t)))
1627  ereport(ERROR,
1628  (errcode(ERRCODE_OUT_OF_MEMORY),
1629  errmsg("out of memory")));
1630 
1631  /* Output workspace cannot have more codes than input bytes */
1632  workspace = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t));
1633 
1634  char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale);
1635 
1636  for (curr_char = 0; workspace[curr_char] != 0; curr_char++)
1637  {
1638 #ifdef HAVE_LOCALE_T
1639  if (mylocale)
1640  workspace[curr_char] = towupper_l(workspace[curr_char], mylocale);
1641  else
1642 #endif
1643  workspace[curr_char] = towupper(workspace[curr_char]);
1644  }
1645 
1646  /* Make result large enough; case change might change number of bytes */
1647  result_size = curr_char * pg_database_encoding_max_length() + 1;
1648  result = palloc(result_size);
1649 
1650  wchar2char(result, workspace, result_size, mylocale);
1651  pfree(workspace);
1652  }
1653 #endif /* USE_WIDE_UPPER_LOWER */
1654  else
1655  {
1656 #ifdef HAVE_LOCALE_T
1657  pg_locale_t mylocale = 0;
1658 #endif
1659  char *p;
1660 
1661  if (collid != DEFAULT_COLLATION_OID)
1662  {
1663  if (!OidIsValid(collid))
1664  {
1665  /*
1666  * This typically means that the parser could not resolve a
1667  * conflict of implicit collations, so report it that way.
1668  */
1669  ereport(ERROR,
1670  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1671  errmsg("could not determine which collation to use for upper() function"),
1672  errhint("Use the COLLATE clause to set the collation explicitly.")));
1673  }
1674 #ifdef HAVE_LOCALE_T
1675  mylocale = pg_newlocale_from_collation(collid);
1676 #endif
1677  }
1678 
1679  result = pnstrdup(buff, nbytes);
1680 
1681  /*
1682  * Note: we assume that toupper_l() will not be so broken as to need
1683  * an islower_l() guard test. When using the default collation, we
1684  * apply the traditional Postgres behavior that forces ASCII-style
1685  * treatment of I/i, but in non-default collations you get exactly
1686  * what the collation says.
1687  */
1688  for (p = result; *p; p++)
1689  {
1690 #ifdef HAVE_LOCALE_T
1691  if (mylocale)
1692  *p = toupper_l((unsigned char) *p, mylocale);
1693  else
1694 #endif
1695  *p = pg_toupper((unsigned char) *p);
1696  }
1697  }
1698 
1699  return result;
1700 }
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1176
#define toupper_l
Definition: win32.h:335
int errcode(int sqlerrcode)
Definition: elog.c:575
#define OidIsValid(objectId)
Definition: c.h:533
int pg_locale_t
Definition: pg_locale.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:68
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1877
#define ereport(elevel, rest)
Definition: elog.h:122
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1260
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1173
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define towupper_l
Definition: win32.h:337