PostgreSQL Source Code git master
Loading...
Searching...
No Matches
formatting.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

charstr_tolower (const char *buff, size_t nbytes, Oid collid)
 
charstr_toupper (const char *buff, size_t nbytes, Oid collid)
 
charstr_initcap (const char *buff, size_t nbytes, Oid collid)
 
charstr_casefold (const char *buff, size_t nbytes, Oid collid)
 
charasc_tolower (const char *buff, size_t nbytes)
 
charasc_toupper (const char *buff, size_t nbytes)
 
charasc_initcap (const char *buff, size_t nbytes)
 
Datum parse_datetime (text *date_txt, text *fmt, Oid collid, bool strict, Oid *typid, int32 *typmod, int *tz, struct Node *escontext)
 
bool datetime_format_has_tz (const char *fmt_str)
 

Function Documentation

◆ asc_initcap()

char * asc_initcap ( const char buff,
size_t  nbytes 
)
extern

Definition at line 1925 of file formatting.c.

1926{
1927 char *result;
1928 int wasalnum = false;
1929
1930 if (!buff)
1931 return NULL;
1932
1933 result = pnstrdup(buff, nbytes);
1934
1935 for (char *p = result; *p; p++)
1936 {
1937 char c;
1938
1939 if (wasalnum)
1940 *p = c = pg_ascii_tolower((unsigned char) *p);
1941 else
1942 *p = c = pg_ascii_toupper((unsigned char) *p);
1943 /* we don't trust isalnum() here */
1944 wasalnum = ((c >= 'A' && c <= 'Z') ||
1945 (c >= 'a' && c <= 'z') ||
1946 (c >= '0' && c <= '9'));
1947 }
1948
1949 return result;
1950}
uint32 result
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1792
static unsigned char pg_ascii_tolower(unsigned char ch)
Definition port.h:188
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition port.h:177
char * c
static int fb(int x)

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

Referenced by str_initcap().

◆ asc_tolower()

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

Definition at line 1881 of file formatting.c.

1882{
1883 char *result;
1884
1885 if (!buff)
1886 return NULL;
1887
1888 result = pnstrdup(buff, nbytes);
1889
1890 for (char *p = result; *p; p++)
1891 *p = pg_ascii_tolower((unsigned char) *p);
1892
1893 return result;
1894}

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

Referenced by asc_tolower_z(), str_casefold(), and str_tolower().

◆ asc_toupper()

char * asc_toupper ( const char buff,
size_t  nbytes 
)
extern

Definition at line 1903 of file formatting.c.

1904{
1905 char *result;
1906
1907 if (!buff)
1908 return NULL;
1909
1910 result = pnstrdup(buff, nbytes);
1911
1912 for (char *p = result; *p; p++)
1913 *p = pg_ascii_toupper((unsigned char) *p);
1914
1915 return result;
1916}

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

Referenced by asc_toupper_z(), and str_toupper().

◆ datetime_format_has_tz()

bool datetime_format_has_tz ( const char fmt_str)
extern

Definition at line 4318 of file formatting.c.

4319{
4320 bool incache;
4321 size_t fmt_len = strlen(fmt_str);
4322 int result;
4324
4325 if (fmt_len > DCH_CACHE_SIZE)
4326 {
4327 /*
4328 * Allocate new memory if format picture is bigger than static cache
4329 * and do not use cache (call parser always)
4330 */
4331 incache = false;
4332
4333 format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
4334
4337 }
4338 else
4339 {
4340 /*
4341 * Use cache buffers
4342 */
4344
4345 incache = true;
4346 format = ent->format;
4347 }
4348
4350
4351 if (!incache)
4352 pfree(format);
4353
4354 return result & DCH_ZONED;
4355}
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition formatting.c:976
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num)
#define DCH_FLAG
Definition formatting.c:90
static const KeySuffix DCH_suff[]
Definition formatting.c:601
#define DCH_CACHE_SIZE
Definition formatting.c:374
#define DCH_ZONED
static int DCH_datetime_type(FormatNode *node)
static const KeyWord DCH_keywords[]
Definition formatting.c:805
static DCHCacheEntry * DCH_cache_fetch(const char *str, bool std)
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
static char format

References DCH_cache_fetch(), DCH_CACHE_SIZE, DCH_datetime_type(), DCH_FLAG, DCH_index, DCH_keywords, DCH_suff, DCH_ZONED, fb(), format, palloc(), parse_format(), pfree(), and result.

Referenced by jspIsMutableWalker().

◆ parse_datetime()

Datum parse_datetime ( text date_txt,
text fmt,
Oid  collid,
bool  strict,
Oid typid,
int32 typmod,
int tz,
struct Node escontext 
)
extern

Definition at line 4157 of file formatting.c.

4160{
4161 struct pg_tm tm;
4162 struct fmt_tz ftz;
4163 fsec_t fsec;
4164 int fprec;
4165 uint32 flags;
4166
4167 if (!do_to_timestamp(date_txt, fmt, collid, strict,
4168 &tm, &fsec, &ftz, &fprec, &flags, escontext))
4169 return (Datum) 0;
4170
4171 *typmod = fprec ? fprec : -1; /* fractional part precision */
4172
4173 if (flags & DCH_DATED)
4174 {
4175 if (flags & DCH_TIMED)
4176 {
4177 if (flags & DCH_ZONED)
4178 {
4180
4181 if (ftz.has_tz)
4182 {
4183 *tz = ftz.gmtoffset;
4184 }
4185 else
4186 {
4187 /*
4188 * Time zone is present in format string, but not in input
4189 * string. Assuming do_to_timestamp() triggers no error
4190 * this should be possible only in non-strict case.
4191 */
4192 Assert(!strict);
4193
4194 ereturn(escontext, (Datum) 0,
4196 errmsg("missing time zone in input string for type timestamptz")));
4197 }
4198
4199 if (tm2timestamp(&tm, fsec, tz, &result) != 0)
4200 ereturn(escontext, (Datum) 0,
4202 errmsg("timestamptz out of range")));
4203
4204 AdjustTimestampForTypmod(&result, *typmod, escontext);
4205
4206 *typid = TIMESTAMPTZOID;
4208 }
4209 else
4210 {
4212
4213 if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
4214 ereturn(escontext, (Datum) 0,
4216 errmsg("timestamp out of range")));
4217
4218 AdjustTimestampForTypmod(&result, *typmod, escontext);
4219
4220 *typid = TIMESTAMPOID;
4221 return TimestampGetDatum(result);
4222 }
4223 }
4224 else
4225 {
4226 if (flags & DCH_ZONED)
4227 {
4228 ereturn(escontext, (Datum) 0,
4230 errmsg("datetime format is zoned but not timed")));
4231 }
4232 else
4233 {
4235
4236 /* Prevent overflow in Julian-day routines */
4238 ereturn(escontext, (Datum) 0,
4240 errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
4241
4244
4245 /* Now check for just-out-of-range dates */
4246 if (!IS_VALID_DATE(result))
4247 ereturn(escontext, (Datum) 0,
4249 errmsg("date out of range: \"%s\"", text_to_cstring(date_txt))));
4250
4251 *typid = DATEOID;
4252 return DateADTGetDatum(result);
4253 }
4254 }
4255 }
4256 else if (flags & DCH_TIMED)
4257 {
4258 if (flags & DCH_ZONED)
4259 {
4261
4262 if (ftz.has_tz)
4263 {
4264 *tz = ftz.gmtoffset;
4265 }
4266 else
4267 {
4268 /*
4269 * Time zone is present in format string, but not in input
4270 * string. Assuming do_to_timestamp() triggers no error this
4271 * should be possible only in non-strict case.
4272 */
4273 Assert(!strict);
4274
4275 ereturn(escontext, (Datum) 0,
4277 errmsg("missing time zone in input string for type timetz")));
4278 }
4279
4280 if (tm2timetz(&tm, fsec, *tz, result) != 0)
4281 ereturn(escontext, (Datum) 0,
4283 errmsg("timetz out of range")));
4284
4285 AdjustTimeForTypmod(&result->time, *typmod);
4286
4287 *typid = TIMETZOID;
4288 return TimeTzADTPGetDatum(result);
4289 }
4290 else
4291 {
4293
4294 if (tm2time(&tm, fsec, &result) != 0)
4295 ereturn(escontext, (Datum) 0,
4297 errmsg("time out of range")));
4298
4299 AdjustTimeForTypmod(&result, *typmod);
4300
4301 *typid = TIMEOID;
4302 return TimeADTGetDatum(result);
4303 }
4304 }
4305 else
4306 {
4307 ereturn(escontext, (Datum) 0,
4309 errmsg("datetime format is not dated and not timed")));
4310 }
4311}
int date2j(int year, int month, int day)
Definition datetime.c:297
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition timestamp.c:360
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition timestamp.c:2000
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
Oid collid
int64 Timestamp
Definition timestamp.h:38
int64 TimestampTz
Definition timestamp.h:39
#define IS_VALID_DATE(d)
Definition timestamp.h:262
int32 fsec_t
Definition timestamp.h:41
#define IS_VALID_JULIAN(y, m, d)
Definition timestamp.h:227
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition date.c:1505
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition date.c:2352
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition date.c:1734
static Datum DateADTGetDatum(DateADT X)
Definition date.h:78
int32 DateADT
Definition date.h:21
static Datum TimeTzADTPGetDatum(const TimeTzADT *X)
Definition date.h:90
int64 TimeADT
Definition date.h:23
static Datum TimeADTGetDatum(TimeADT X)
Definition date.h:84
int errcode(int sqlerrcode)
Definition elog.c:875
#define ereturn(context, dummy_value,...)
Definition elog.h:280
#define palloc_object(type)
Definition fe_memutils.h:74
#define DCH_DATED
static bool do_to_timestamp(const text *date_txt, const text *fmt, Oid collid, bool std, struct pg_tm *tm, fsec_t *fsec, struct fmt_tz *tz, int *fprec, uint32 *flags, Node *escontext)
#define DCH_TIMED
static struct pg_tm tm
Definition localtime.c:104
static char * errmsg
uint64_t Datum
Definition postgres.h:70
int gmtoffset
Definition formatting.c:448
Definition pgtime.h:35
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_year
Definition pgtime.h:41
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
static Datum TimestampGetDatum(Timestamp X)
Definition timestamp.h:46
char * text_to_cstring(const text *t)
Definition varlena.c:217

References AdjustTimeForTypmod(), AdjustTimestampForTypmod(), Assert, collid, date2j(), DateADTGetDatum(), DCH_DATED, DCH_TIMED, DCH_ZONED, do_to_timestamp(), ereturn, errcode(), errmsg, fb(), fmt_tz::gmtoffset, IS_VALID_DATE, IS_VALID_JULIAN, palloc_object, POSTGRES_EPOCH_JDATE, result, text_to_cstring(), TimeADTGetDatum(), TimestampGetDatum(), TimestampTzGetDatum(), TimeTzADTPGetDatum(), tm, tm2time(), tm2timestamp(), tm2timetz(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by executeDateTimeMethod().

◆ str_casefold()

char * str_casefold ( const char buff,
size_t  nbytes,
Oid  collid 
)
extern

Definition at line 1812 of file formatting.c.

1813{
1814 char *result;
1816
1817 if (!buff)
1818 return NULL;
1819
1820 if (!OidIsValid(collid))
1821 {
1822 /*
1823 * This typically means that the parser could not resolve a conflict
1824 * of implicit collations, so report it that way.
1825 */
1826 ereport(ERROR,
1828 errmsg("could not determine which collation to use for %s function",
1829 "casefold()"),
1830 errhint("Use the COLLATE clause to set the collation explicitly.")));
1831 }
1832
1834 ereport(ERROR,
1836 errmsg("Unicode case folding can only be performed if server encoding is UTF8")));
1837
1839
1840 /* C/POSIX collations use this path regardless of database encoding */
1841 if (mylocale->ctype_is_c)
1842 {
1843 result = asc_tolower(buff, nbytes);
1844 }
1845 else
1846 {
1847 const char *src = buff;
1848 size_t srclen = nbytes;
1849 size_t dstsize;
1850 char *dst;
1851 size_t needed;
1852
1853 /* first try buffer of equal size plus terminating NUL */
1854 dstsize = srclen + 1;
1855 dst = palloc(dstsize);
1856
1858 if (needed + 1 > dstsize)
1859 {
1860 /* grow buffer if needed and retry */
1861 dstsize = needed + 1;
1862 dst = repalloc(dst, dstsize);
1864 Assert(needed + 1 <= dstsize);
1865 }
1866
1867 Assert(dst[needed] == '\0');
1868 result = dst;
1869 }
1870
1871 return result;
1872}
#define OidIsValid(objectId)
Definition c.h:858
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
char * asc_tolower(const char *buff, size_t nbytes)
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1388
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition pg_locale.c:1189
size_t pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition pg_locale.c:1348

References asc_tolower(), Assert, collid, ereport, errcode(), errhint(), errmsg, ERROR, fb(), GetDatabaseEncoding(), OidIsValid, palloc(), pg_newlocale_from_collation(), pg_strfold(), PG_UTF8, repalloc(), and result.

Referenced by casefold().

◆ str_initcap()

char * str_initcap ( const char buff,
size_t  nbytes,
Oid  collid 
)
extern

Definition at line 1748 of file formatting.c.

1749{
1750 char *result;
1752
1753 if (!buff)
1754 return NULL;
1755
1756 if (!OidIsValid(collid))
1757 {
1758 /*
1759 * This typically means that the parser could not resolve a conflict
1760 * of implicit collations, so report it that way.
1761 */
1762 ereport(ERROR,
1764 errmsg("could not determine which collation to use for %s function",
1765 "initcap()"),
1766 errhint("Use the COLLATE clause to set the collation explicitly.")));
1767 }
1768
1770
1771 /* C/POSIX collations use this path regardless of database encoding */
1772 if (mylocale->ctype_is_c)
1773 {
1774 result = asc_initcap(buff, nbytes);
1775 }
1776 else
1777 {
1778 const char *src = buff;
1779 size_t srclen = nbytes;
1780 size_t dstsize;
1781 char *dst;
1782 size_t needed;
1783
1784 /* first try buffer of equal size plus terminating NUL */
1785 dstsize = srclen + 1;
1786 dst = palloc(dstsize);
1787
1789 if (needed + 1 > dstsize)
1790 {
1791 /* grow buffer if needed and retry */
1792 dstsize = needed + 1;
1793 dst = repalloc(dst, dstsize);
1795 Assert(needed + 1 <= dstsize);
1796 }
1797
1798 Assert(dst[needed] == '\0');
1799 result = dst;
1800 }
1801
1802 return result;
1803}
char * asc_initcap(const char *buff, size_t nbytes)
size_t pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition pg_locale.c:1328

References asc_initcap(), Assert, collid, ereport, errcode(), errhint(), errmsg, ERROR, fb(), OidIsValid, palloc(), pg_newlocale_from_collation(), pg_strtitle(), repalloc(), and result.

Referenced by initcap(), and str_initcap_z().

◆ str_tolower()

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

Definition at line 1620 of file formatting.c.

1621{
1622 char *result;
1624
1625 if (!buff)
1626 return NULL;
1627
1628 if (!OidIsValid(collid))
1629 {
1630 /*
1631 * This typically means that the parser could not resolve a conflict
1632 * of implicit collations, so report it that way.
1633 */
1634 ereport(ERROR,
1636 errmsg("could not determine which collation to use for %s function",
1637 "lower()"),
1638 errhint("Use the COLLATE clause to set the collation explicitly.")));
1639 }
1640
1642
1643 /* C/POSIX collations use this path regardless of database encoding */
1644 if (mylocale->ctype_is_c)
1645 {
1646 result = asc_tolower(buff, nbytes);
1647 }
1648 else
1649 {
1650 const char *src = buff;
1651 size_t srclen = nbytes;
1652 size_t dstsize;
1653 char *dst;
1654 size_t needed;
1655
1656 /* first try buffer of equal size plus terminating NUL */
1657 dstsize = srclen + 1;
1658 dst = palloc(dstsize);
1659
1661 if (needed + 1 > dstsize)
1662 {
1663 /* grow buffer if needed and retry */
1664 dstsize = needed + 1;
1665 dst = repalloc(dst, dstsize);
1667 Assert(needed + 1 <= dstsize);
1668 }
1669
1670 Assert(dst[needed] == '\0');
1671 result = dst;
1672 }
1673
1674 return result;
1675}
size_t pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition pg_locale.c:1318

References asc_tolower(), Assert, collid, ereport, errcode(), errhint(), errmsg, ERROR, fb(), OidIsValid, palloc(), pg_newlocale_from_collation(), pg_strlower(), repalloc(), and result.

Referenced by citext_eq(), citext_hash(), citext_hash_extended(), citext_ne(), citextcmp(), convertPgWchar(), dispell_init(), dispell_lexize(), dsimple_init(), dsimple_lexize(), dsnowball_init(), dsnowball_lexize(), dsynonym_init(), dsynonym_lexize(), dxsyn_lexize(), generate_trgm_only(), generate_wildcard_trgm(), internal_citext_pattern_cmp(), lower(), lowerstr_ctx(), NIImportAffixes(), read_dictionary(), seq_search_localized(), and str_tolower_z().

◆ str_toupper()

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

Definition at line 1684 of file formatting.c.

1685{
1686 char *result;
1688
1689 if (!buff)
1690 return NULL;
1691
1692 if (!OidIsValid(collid))
1693 {
1694 /*
1695 * This typically means that the parser could not resolve a conflict
1696 * of implicit collations, so report it that way.
1697 */
1698 ereport(ERROR,
1700 errmsg("could not determine which collation to use for %s function",
1701 "upper()"),
1702 errhint("Use the COLLATE clause to set the collation explicitly.")));
1703 }
1704
1706
1707 /* C/POSIX collations use this path regardless of database encoding */
1708 if (mylocale->ctype_is_c)
1709 {
1710 result = asc_toupper(buff, nbytes);
1711 }
1712 else
1713 {
1714 const char *src = buff;
1715 size_t srclen = nbytes;
1716 size_t dstsize;
1717 char *dst;
1718 size_t needed;
1719
1720 /* first try buffer of equal size plus terminating NUL */
1721 dstsize = srclen + 1;
1722 dst = palloc(dstsize);
1723
1725 if (needed + 1 > dstsize)
1726 {
1727 /* grow buffer if needed and retry */
1728 dstsize = needed + 1;
1729 dst = repalloc(dst, dstsize);
1731 Assert(needed + 1 <= dstsize);
1732 }
1733
1734 Assert(dst[needed] == '\0');
1735 result = dst;
1736 }
1737
1738 return result;
1739}
char * asc_toupper(const char *buff, size_t nbytes)
size_t pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition pg_locale.c:1338

References asc_toupper(), Assert, collid, ereport, errcode(), errhint(), errmsg, ERROR, fb(), OidIsValid, palloc(), pg_newlocale_from_collation(), pg_strupper(), repalloc(), and result.

Referenced by MakeUpper(), seq_search_localized(), str_toupper_z(), and upper().