PostgreSQL Source Code  git master
varlena.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "access/hash.h"
#include "access/tuptoaster.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "parser/scansup.h"
#include "port/pg_bswap.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/sortsupport.h"
#include "utils/varlena.h"
#include "levenshtein.c"
Include dependency graph for varlena.c:

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 

Macros

#define TEXTBUFLEN   1024
 
#define DatumGetUnknownP(X)   ((unknown *) PG_DETOAST_DATUM(X))
 
#define DatumGetUnknownPCopy(X)   ((unknown *) PG_DETOAST_DATUM_COPY(X))
 
#define PG_GETARG_UNKNOWN_P(n)   DatumGetUnknownP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_UNKNOWN_P_COPY(n)   DatumGetUnknownPCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_UNKNOWN_P(x)   PG_RETURN_POINTER(x)
 
#define DatumGetVarStringP(X)   ((VarString *) PG_DETOAST_DATUM(X))
 
#define DatumGetVarStringPP(X)   ((VarString *) PG_DETOAST_DATUM_PACKED(X))
 
#define VAL(CH)   ((CH) - '0')
 
#define DIG(VAL)   ((VAL) + '0')
 
#define PG_STR_GET_BYTEA(str_)   DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#define REGEXP_REPLACE_BACKREF_CNT   10
 
#define HEXBASE   16
 
#define TEXT_FORMAT_FLAG_MINUS   0x0001 /* is minus flag present? */
 
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
 
#define LEVENSHTEIN_LESS_EQUAL
 

Typedefs

typedef struct varlena unknown
 
typedef struct varlena VarString
 

Functions

static int varstrfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int bpcharfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int varstrfastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int varstrcmp_abbrev (Datum x, Datum y, SortSupport ssup)
 
static Datum varstr_abbrev_convert (Datum original, SortSupport ssup)
 
static bool varstr_abbrev_abort (int memtupcount, SortSupport ssup)
 
static int32 text_length (Datum str)
 
static texttext_catenate (text *t1, text *t2)
 
static texttext_substring (Datum str, int32 start, int32 length, bool length_not_specified)
 
static texttext_overlay (text *t1, text *t2, int sp, int sl)
 
static int text_position (text *t1, text *t2)
 
static void text_position_setup (text *t1, text *t2, TextPositionState *state)
 
static int text_position_next (int start_pos, TextPositionState *state)
 
static void text_position_cleanup (TextPositionState *state)
 
static int text_cmp (text *arg1, text *arg2, Oid collid)
 
static byteabytea_catenate (bytea *t1, bytea *t2)
 
static byteabytea_substring (Datum str, int S, int L, bool length_not_specified)
 
static byteabytea_overlay (bytea *t1, bytea *t2, int sp, int sl)
 
static void appendStringInfoText (StringInfo str, const text *t)
 
static Datum text_to_array_internal (PG_FUNCTION_ARGS)
 
static textarray_to_text_internal (FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
 
static StringInfo makeStringAggState (FunctionCallInfo fcinfo)
 
static bool text_format_parse_digits (const char **ptr, const char *end_ptr, int *value)
 
static const char * text_format_parse_format (const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
 
static void text_format_string_conversion (StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
 
static void text_format_append_string (StringInfo buf, const char *str, int flags, int width)
 
textcstring_to_text (const char *s)
 
textcstring_to_text_with_len (const char *s, int len)
 
char * text_to_cstring (const text *t)
 
void text_to_cstring_buffer (const text *src, char *dst, size_t dst_len)
 
Datum byteain (PG_FUNCTION_ARGS)
 
Datum byteaout (PG_FUNCTION_ARGS)
 
Datum bytearecv (PG_FUNCTION_ARGS)
 
Datum byteasend (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum textin (PG_FUNCTION_ARGS)
 
Datum textout (PG_FUNCTION_ARGS)
 
Datum textrecv (PG_FUNCTION_ARGS)
 
Datum textsend (PG_FUNCTION_ARGS)
 
Datum unknownin (PG_FUNCTION_ARGS)
 
Datum unknownout (PG_FUNCTION_ARGS)
 
Datum unknownrecv (PG_FUNCTION_ARGS)
 
Datum unknownsend (PG_FUNCTION_ARGS)
 
Datum textlen (PG_FUNCTION_ARGS)
 
Datum textoctetlen (PG_FUNCTION_ARGS)
 
Datum textcat (PG_FUNCTION_ARGS)
 
static int charlen_to_bytelen (const char *p, int n)
 
Datum text_substr (PG_FUNCTION_ARGS)
 
Datum text_substr_no_len (PG_FUNCTION_ARGS)
 
Datum textoverlay (PG_FUNCTION_ARGS)
 
Datum textoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum textpos (PG_FUNCTION_ARGS)
 
int varstr_cmp (const char *arg1, int len1, const char *arg2, int len2, Oid collid)
 
Datum texteq (PG_FUNCTION_ARGS)
 
Datum textne (PG_FUNCTION_ARGS)
 
Datum text_lt (PG_FUNCTION_ARGS)
 
Datum text_le (PG_FUNCTION_ARGS)
 
Datum text_gt (PG_FUNCTION_ARGS)
 
Datum text_ge (PG_FUNCTION_ARGS)
 
Datum text_starts_with (PG_FUNCTION_ARGS)
 
Datum bttextcmp (PG_FUNCTION_ARGS)
 
Datum bttextsortsupport (PG_FUNCTION_ARGS)
 
void varstr_sortsupport (SortSupport ssup, Oid collid, bool bpchar)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
static int internal_text_pattern_compare (text *arg1, text *arg2)
 
Datum text_pattern_lt (PG_FUNCTION_ARGS)
 
Datum text_pattern_le (PG_FUNCTION_ARGS)
 
Datum text_pattern_ge (PG_FUNCTION_ARGS)
 
Datum text_pattern_gt (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_sortsupport (PG_FUNCTION_ARGS)
 
Datum byteaoctetlen (PG_FUNCTION_ARGS)
 
Datum byteacat (PG_FUNCTION_ARGS)
 
Datum bytea_substr (PG_FUNCTION_ARGS)
 
Datum bytea_substr_no_len (PG_FUNCTION_ARGS)
 
Datum byteaoverlay (PG_FUNCTION_ARGS)
 
Datum byteaoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum byteapos (PG_FUNCTION_ARGS)
 
Datum byteaGetByte (PG_FUNCTION_ARGS)
 
Datum byteaGetBit (PG_FUNCTION_ARGS)
 
Datum byteaSetByte (PG_FUNCTION_ARGS)
 
Datum byteaSetBit (PG_FUNCTION_ARGS)
 
Datum text_name (PG_FUNCTION_ARGS)
 
Datum name_text (PG_FUNCTION_ARGS)
 
ListtextToQualifiedNameList (text *textval)
 
bool SplitIdentifierString (char *rawstring, char separator, List **namelist)
 
bool SplitDirectoriesString (char *rawstring, char separator, List **namelist)
 
Datum byteaeq (PG_FUNCTION_ARGS)
 
Datum byteane (PG_FUNCTION_ARGS)
 
Datum bytealt (PG_FUNCTION_ARGS)
 
Datum byteale (PG_FUNCTION_ARGS)
 
Datum byteagt (PG_FUNCTION_ARGS)
 
Datum byteage (PG_FUNCTION_ARGS)
 
Datum byteacmp (PG_FUNCTION_ARGS)
 
Datum bytea_sortsupport (PG_FUNCTION_ARGS)
 
Datum replace_text (PG_FUNCTION_ARGS)
 
static bool check_replace_text_has_escape_char (const text *replace_text)
 
static void appendStringInfoRegexpSubstr (StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
 
textreplace_text_regexp (text *src_text, void *regexp, text *replace_text, bool glob)
 
Datum split_text (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_null (PG_FUNCTION_ARGS)
 
Datum array_to_text (PG_FUNCTION_ARGS)
 
Datum array_to_text_null (PG_FUNCTION_ARGS)
 
Datum to_hex32 (PG_FUNCTION_ARGS)
 
Datum to_hex64 (PG_FUNCTION_ARGS)
 
Datum pg_column_size (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 

Variables

int bytea_output = BYTEA_OUTPUT_HEX
 

Macro Definition Documentation

◆ ADVANCE_PARSE_POINTER

#define ADVANCE_PARSE_POINTER (   ptr,
  end_ptr 
)
Value:
do { \
if (++(ptr) >= (end_ptr)) \
ereport(ERROR, \
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
errmsg("unterminated format() type specifier"), \
errhint("For a single \"%%\" use \"%%%%\"."))); \
} while (0)
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:797

Definition at line 4963 of file varlena.c.

Referenced by text_format(), text_format_parse_digits(), and text_format_parse_format().

◆ DatumGetUnknownP

#define DatumGetUnknownP (   X)    ((unknown *) PG_DETOAST_DATUM(X))

Definition at line 85 of file varlena.c.

◆ DatumGetUnknownPCopy

#define DatumGetUnknownPCopy (   X)    ((unknown *) PG_DETOAST_DATUM_COPY(X))

Definition at line 86 of file varlena.c.

◆ DatumGetVarStringP

#define DatumGetVarStringP (   X)    ((VarString *) PG_DETOAST_DATUM(X))

Definition at line 91 of file varlena.c.

◆ DatumGetVarStringPP

#define DatumGetVarStringPP (   X)    ((VarString *) PG_DETOAST_DATUM_PACKED(X))

Definition at line 92 of file varlena.c.

Referenced by varstr_abbrev_convert(), varstrfastcmp_c(), and varstrfastcmp_locale().

◆ DIG

#define DIG (   VAL)    ((VAL) + '0')

Definition at line 241 of file varlena.c.

Referenced by byteaout().

◆ HEXBASE

#define HEXBASE   16

Definition at line 4545 of file varlena.c.

Referenced by to_hex32(), and to_hex64().

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 5503 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P

#define PG_GETARG_UNKNOWN_P (   n)    DatumGetUnknownP(PG_GETARG_DATUM(n))

Definition at line 87 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P_COPY

#define PG_GETARG_UNKNOWN_P_COPY (   n)    DatumGetUnknownPCopy(PG_GETARG_DATUM(n))

Definition at line 88 of file varlena.c.

◆ PG_RETURN_UNKNOWN_P

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 89 of file varlena.c.

◆ PG_STR_GET_BYTEA

#define PG_STR_GET_BYTEA (   str_)    DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))

Definition at line 2840 of file varlena.c.

Referenced by bytea_substring().

◆ REGEXP_REPLACE_BACKREF_CNT

#define REGEXP_REPLACE_BACKREF_CNT   10

Definition at line 3927 of file varlena.c.

Referenced by replace_text_regexp().

◆ TEXT_FORMAT_FLAG_MINUS

#define TEXT_FORMAT_FLAG_MINUS   0x0001 /* is minus flag present? */

Definition at line 4961 of file varlena.c.

Referenced by text_format_append_string(), and text_format_parse_format().

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 83 of file varlena.c.

Referenced by varstr_cmp(), and varstr_sortsupport().

◆ VAL

#define VAL (   CH)    ((CH) - '0')

Definition at line 240 of file varlena.c.

Referenced by byteain().

Typedef Documentation

◆ unknown

Definition at line 43 of file varlena.c.

◆ VarString

Definition at line 44 of file varlena.c.

Function Documentation

◆ appendStringInfoRegexpSubstr()

static void appendStringInfoRegexpSubstr ( StringInfo  str,
text replace_text,
regmatch_t pmatch,
char *  start_ptr,
int  data_pos 
)
static

Definition at line 3833 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, charlen_to_bytelen(), idx(), pg_database_encoding_max_length(), pg_mblen(), regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

3836 {
3837  const char *p = VARDATA_ANY(replace_text);
3838  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3839  int eml = pg_database_encoding_max_length();
3840 
3841  for (;;)
3842  {
3843  const char *chunk_start = p;
3844  int so;
3845  int eo;
3846 
3847  /* Find next escape char. */
3848  if (eml == 1)
3849  {
3850  for (; p < p_end && *p != '\\'; p++)
3851  /* nothing */ ;
3852  }
3853  else
3854  {
3855  for (; p < p_end && *p != '\\'; p += pg_mblen(p))
3856  /* nothing */ ;
3857  }
3858 
3859  /* Copy the text we just scanned over, if any. */
3860  if (p > chunk_start)
3861  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
3862 
3863  /* Done if at end of string, else advance over escape char. */
3864  if (p >= p_end)
3865  break;
3866  p++;
3867 
3868  if (p >= p_end)
3869  {
3870  /* Escape at very end of input. Treat same as unexpected char */
3871  appendStringInfoChar(str, '\\');
3872  break;
3873  }
3874 
3875  if (*p >= '1' && *p <= '9')
3876  {
3877  /* Use the back reference of regexp. */
3878  int idx = *p - '0';
3879 
3880  so = pmatch[idx].rm_so;
3881  eo = pmatch[idx].rm_eo;
3882  p++;
3883  }
3884  else if (*p == '&')
3885  {
3886  /* Use the entire matched string. */
3887  so = pmatch[0].rm_so;
3888  eo = pmatch[0].rm_eo;
3889  p++;
3890  }
3891  else if (*p == '\\')
3892  {
3893  /* \\ means transfer one \ to output. */
3894  appendStringInfoChar(str, '\\');
3895  p++;
3896  continue;
3897  }
3898  else
3899  {
3900  /*
3901  * If escape char is not followed by any expected char, just treat
3902  * it as ordinary data to copy. (XXX would it be better to throw
3903  * an error?)
3904  */
3905  appendStringInfoChar(str, '\\');
3906  continue;
3907  }
3908 
3909  if (so != -1 && eo != -1)
3910  {
3911  /*
3912  * Copy the text that is back reference of regexp. Note so and eo
3913  * are counted in characters not bytes.
3914  */
3915  char *chunk_start;
3916  int chunk_len;
3917 
3918  Assert(so >= data_pos);
3919  chunk_start = start_ptr;
3920  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
3921  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
3922  appendBinaryStringInfo(str, chunk_start, chunk_len);
3923  }
3924  }
3925 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
regoff_t rm_eo
Definition: regex.h:86
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:735
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
#define Assert(condition)
Definition: c.h:699
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ appendStringInfoText()

static void appendStringInfoText ( StringInfo  str,
const text t 
)
static

Definition at line 3703 of file varlena.c.

References appendBinaryStringInfo(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text(), replace_text_regexp(), string_agg_transfn(), xml_send(), xmlcomment(), and XmlTableGetValue().

3704 {
3706 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4386 of file varlena.c.

References array_to_text_internal(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_to_cstring().

4387 {
4389  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4390 
4391  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4392 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4428
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
char * text_to_cstring(const text *t)
Definition: varlena.c:182

◆ array_to_text_internal()

static text * array_to_text_internal ( FunctionCallInfo  fcinfo,
ArrayType v,
const char *  fldsep,
const char *  null_string 
)
static

Definition at line 4428 of file varlena.c.

References appendStringInfo(), appendStringInfoString(), ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, ARR_NULLBITMAP, ArrayGetNItems(), att_addlength_pointer, att_align_nominal, buf, cstring_to_text_with_len(), StringInfoData::data, ArrayMetaState::element_type, fetch_att, FunctionCallInfoData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_type_io_data(), i, initStringInfo(), IOFunc_output, StringInfoData::len, MemoryContextAlloc(), OutputFunctionCall(), pfree(), ArrayMetaState::proc, typalign, ArrayMetaState::typalign, typbyval, ArrayMetaState::typbyval, ArrayMetaState::typdelim, ArrayMetaState::typiofunc, ArrayMetaState::typioparam, typlen, ArrayMetaState::typlen, and value.

Referenced by array_to_text(), array_to_text_null(), and concat_internal().

4430 {
4431  text *result;
4432  int nitems,
4433  *dims,
4434  ndims;
4435  Oid element_type;
4436  int typlen;
4437  bool typbyval;
4438  char typalign;
4440  bool printed = false;
4441  char *p;
4442  bits8 *bitmap;
4443  int bitmask;
4444  int i;
4445  ArrayMetaState *my_extra;
4446 
4447  ndims = ARR_NDIM(v);
4448  dims = ARR_DIMS(v);
4449  nitems = ArrayGetNItems(ndims, dims);
4450 
4451  /* if there are no elements, return an empty string */
4452  if (nitems == 0)
4453  return cstring_to_text_with_len("", 0);
4454 
4455  element_type = ARR_ELEMTYPE(v);
4456  initStringInfo(&buf);
4457 
4458  /*
4459  * We arrange to look up info about element type, including its output
4460  * conversion proc, only once per series of calls, assuming the element
4461  * type doesn't change underneath us.
4462  */
4463  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4464  if (my_extra == NULL)
4465  {
4466  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4467  sizeof(ArrayMetaState));
4468  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4469  my_extra->element_type = ~element_type;
4470  }
4471 
4472  if (my_extra->element_type != element_type)
4473  {
4474  /*
4475  * Get info about element type, including its output conversion proc
4476  */
4477  get_type_io_data(element_type, IOFunc_output,
4478  &my_extra->typlen, &my_extra->typbyval,
4479  &my_extra->typalign, &my_extra->typdelim,
4480  &my_extra->typioparam, &my_extra->typiofunc);
4481  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4482  fcinfo->flinfo->fn_mcxt);
4483  my_extra->element_type = element_type;
4484  }
4485  typlen = my_extra->typlen;
4486  typbyval = my_extra->typbyval;
4487  typalign = my_extra->typalign;
4488 
4489  p = ARR_DATA_PTR(v);
4490  bitmap = ARR_NULLBITMAP(v);
4491  bitmask = 1;
4492 
4493  for (i = 0; i < nitems; i++)
4494  {
4495  Datum itemvalue;
4496  char *value;
4497 
4498  /* Get source element, checking for NULL */
4499  if (bitmap && (*bitmap & bitmask) == 0)
4500  {
4501  /* if null_string is NULL, we just ignore null elements */
4502  if (null_string != NULL)
4503  {
4504  if (printed)
4505  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4506  else
4507  appendStringInfoString(&buf, null_string);
4508  printed = true;
4509  }
4510  }
4511  else
4512  {
4513  itemvalue = fetch_att(p, typbyval, typlen);
4514 
4515  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4516 
4517  if (printed)
4518  appendStringInfo(&buf, "%s%s", fldsep, value);
4519  else
4520  appendStringInfoString(&buf, value);
4521  printed = true;
4522 
4523  p = att_addlength_pointer(p, typlen, p);
4524  p = (char *) att_align_nominal(p, typalign);
4525  }
4526 
4527  /* advance bitmap pointer if any */
4528  if (bitmap)
4529  {
4530  bitmask <<= 1;
4531  if (bitmask == 0x100)
4532  {
4533  bitmap++;
4534  bitmask = 1;
4535  }
4536  }
4537  }
4538 
4539  result = cstring_to_text_with_len(buf.data, buf.len);
4540  pfree(buf.data);
4541 
4542  return result;
4543 }
MemoryContext fn_mcxt
Definition: fmgr.h:65
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
int16 typlen
Definition: pg_type.h:55
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
unsigned int Oid
Definition: postgres_ext.h:31
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
bool typbyval
Definition: array.h:225
FmgrInfo * flinfo
Definition: fmgr.h:79
void pfree(void *pointer)
Definition: mcxt.c:1031
char typalign
Definition: pg_type.h:167
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ARR_DIMS(a)
Definition: array.h:279
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define ARR_DATA_PTR(a)
Definition: array.h:307
int16 typlen
Definition: array.h:224
static char * buf
Definition: pg_test_fsync.c:67
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
char typdelim
Definition: array.h:227
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uint8 bits8
Definition: c.h:332
uintptr_t Datum
Definition: postgres.h:367
static struct @131 value
Oid typioparam
Definition: array.h:228
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:275
Oid typiofunc
Definition: array.h:229
char typalign
Definition: array.h:226
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:71
FmgrInfo proc
Definition: array.h:230
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
int i
Oid element_type
Definition: array.h:223
Definition: c.h:516
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define ARR_NULLBITMAP(a)
Definition: array.h:285
void get_type_io_data(Oid typid, IOFuncSelector which_func, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *func)
Definition: lsyscache.c:2079
bool typbyval
Definition: pg_type.h:65

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4402 of file varlena.c.

References array_to_text_internal(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

4403 {
4404  ArrayType *v;
4405  char *fldsep;
4406  char *null_string;
4407 
4408  /* returns NULL when first or second parameter is NULL */
4409  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4410  PG_RETURN_NULL();
4411 
4412  v = PG_GETARG_ARRAYTYPE_P(0);
4413  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4414 
4415  /* NULL null string is passed through as a null pointer */
4416  if (!PG_ARGISNULL(2))
4417  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4418  else
4419  null_string = NULL;
4420 
4421  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4422 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4428
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ bpcharfastcmp_c()

static int bpcharfastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 2028 of file varlena.c.

References bpchartruelen(), DatumGetBpCharPP, Min, pfree(), PointerGetDatum, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by varstr_sortsupport().

2029 {
2030  BpChar *arg1 = DatumGetBpCharPP(x);
2031  BpChar *arg2 = DatumGetBpCharPP(y);
2032  char *a1p,
2033  *a2p;
2034  int len1,
2035  len2,
2036  result;
2037 
2038  a1p = VARDATA_ANY(arg1);
2039  a2p = VARDATA_ANY(arg2);
2040 
2041  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2042  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2043 
2044  result = memcmp(a1p, a2p, Min(len1, len2));
2045  if ((result == 0) && (len1 != len2))
2046  result = (len1 < len2) ? -1 : 1;
2047 
2048  /* We can't afford to leak memory here. */
2049  if (PointerGetDatum(arg1) != x)
2050  pfree(arg1);
2051  if (PointerGetDatum(arg2) != y)
2052  pfree(arg2);
2053 
2054  return result;
2055 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PointerGetDatum(X)
Definition: postgres.h:541
#define Min(x, y)
Definition: c.h:857
void pfree(void *pointer)
Definition: mcxt.c:1031
int bpchartruelen(char *s, int len)
Definition: varchar.c:660
#define DatumGetBpCharPP(X)
Definition: fmgr.h:262
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2737 of file varlena.c.

References internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_INT32.

2738 {
2739  text *arg1 = PG_GETARG_TEXT_PP(0);
2740  text *arg2 = PG_GETARG_TEXT_PP(1);
2741  int result;
2742 
2743  result = internal_text_pattern_compare(arg1, arg2);
2744 
2745  PG_FREE_IF_COPY(arg1, 0);
2746  PG_FREE_IF_COPY(arg2, 1);
2747 
2748  PG_RETURN_INT32(result);
2749 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2651
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2753 of file varlena.c.

References MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, and varstr_sortsupport().

2754 {
2756  MemoryContext oldcontext;
2757 
2758  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2759 
2760  /* Use generic string SortSupport, forcing "C" collation */
2761  varstr_sortsupport(ssup, C_COLLATION_OID, false);
2762 
2763  MemoryContextSwitchTo(oldcontext);
2764 
2765  PG_RETURN_VOID();
2766 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1835
#define PG_RETURN_VOID()
Definition: fmgr.h:314

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1793 of file varlena.c.

References PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_INT32, and text_cmp().

Referenced by gbt_textcmp(), and leftmostvalue_text().

1794 {
1795  text *arg1 = PG_GETARG_TEXT_PP(0);
1796  text *arg2 = PG_GETARG_TEXT_PP(1);
1797  int32 result;
1798 
1799  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1800 
1801  PG_FREE_IF_COPY(arg1, 0);
1802  PG_FREE_IF_COPY(arg2, 1);
1803 
1804  PG_RETURN_INT32(result);
1805 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
#define PG_GET_COLLATION()
Definition: fmgr.h:168
signed int int32
Definition: c.h:313
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1808 of file varlena.c.

References MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and varstr_sortsupport().

1809 {
1811  Oid collid = ssup->ssup_collation;
1812  MemoryContext oldcontext;
1813 
1814  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1815 
1816  /* Use generic string SortSupport */
1817  varstr_sortsupport(ssup, collid, false);
1818 
1819  MemoryContextSwitchTo(oldcontext);
1820 
1821  PG_RETURN_VOID();
1822 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1835
#define PG_RETURN_VOID()
Definition: fmgr.h:314

◆ build_concat_foutcache()

static FmgrInfo* build_concat_foutcache ( FunctionCallInfo  fcinfo,
int  argidx 
)
static

Definition at line 4726 of file varlena.c.

References elog, ERROR, FunctionCallInfoData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_fn_expr_argtype(), getTypeOutputInfo(), i, MemoryContextAlloc(), OidIsValid, and PG_NARGS.

Referenced by concat_internal().

4727 {
4728  FmgrInfo *foutcache;
4729  int i;
4730 
4731  /* We keep the info in fn_mcxt so it survives across calls */
4732  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4733  PG_NARGS() * sizeof(FmgrInfo));
4734 
4735  for (i = argidx; i < PG_NARGS(); i++)
4736  {
4737  Oid valtype;
4738  Oid typOutput;
4739  bool typIsVarlena;
4740 
4741  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
4742  if (!OidIsValid(valtype))
4743  elog(ERROR, "could not determine data type of concat() input");
4744 
4745  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
4746  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
4747  }
4748 
4749  fcinfo->flinfo->fn_extra = foutcache;
4750 
4751  return foutcache;
4752 }
Definition: fmgr.h:56
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2650
MemoryContext fn_mcxt
Definition: fmgr.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
FmgrInfo * flinfo
Definition: fmgr.h:79
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1995
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:173
void * fn_extra
Definition: fmgr.h:64
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
int i
#define elog
Definition: elog.h:219

◆ bytea_catenate()

static bytea * bytea_catenate ( bytea t1,
bytea t2 
)
static

Definition at line 2807 of file varlena.c.

References palloc(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by bytea_overlay(), and byteacat().

2808 {
2809  bytea *result;
2810  int len1,
2811  len2,
2812  len;
2813  char *ptr;
2814 
2815  len1 = VARSIZE_ANY_EXHDR(t1);
2816  len2 = VARSIZE_ANY_EXHDR(t2);
2817 
2818  /* paranoia ... probably should throw error instead? */
2819  if (len1 < 0)
2820  len1 = 0;
2821  if (len2 < 0)
2822  len2 = 0;
2823 
2824  len = len1 + len2 + VARHDRSZ;
2825  result = (bytea *) palloc(len);
2826 
2827  /* Set size of result string... */
2828  SET_VARSIZE(result, len);
2829 
2830  /* Fill data field of result string... */
2831  ptr = VARDATA(result);
2832  if (len1 > 0)
2833  memcpy(ptr, VARDATA_ANY(t1), len1);
2834  if (len2 > 0)
2835  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2836 
2837  return result;
2838 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bytea_overlay()

static bytea * bytea_overlay ( bytea t1,
bytea t2,
int  sp,
int  sl 
)
static

Definition at line 2964 of file varlena.c.

References bytea_catenate(), bytea_substring(), ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), PointerGetDatum, s1, and s2.

Referenced by byteaoverlay(), and byteaoverlay_no_len().

2965 {
2966  bytea *result;
2967  bytea *s1;
2968  bytea *s2;
2969  int sp_pl_sl;
2970 
2971  /*
2972  * Check for possible integer-overflow cases. For negative sp, throw a
2973  * "substring length" error because that's what should be expected
2974  * according to the spec's definition of OVERLAY().
2975  */
2976  if (sp <= 0)
2977  ereport(ERROR,
2978  (errcode(ERRCODE_SUBSTRING_ERROR),
2979  errmsg("negative substring length not allowed")));
2980  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
2981  ereport(ERROR,
2982  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2983  errmsg("integer out of range")));
2984 
2985  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
2986  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
2987  result = bytea_catenate(s1, t2);
2988  result = bytea_catenate(result, s2);
2989 
2990  return result;
2991 }
#define PointerGetDatum(X)
Definition: postgres.h:541
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2807
int errcode(int sqlerrcode)
Definition: elog.c:575
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2882
#define ERROR
Definition: elog.h:43
char * s1
#define ereport(elevel, rest)
Definition: elog.h:122
char * s2
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:98
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ bytea_sortsupport()

Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3681 of file varlena.c.

References MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, and varstr_sortsupport().

3682 {
3684  MemoryContext oldcontext;
3685 
3686  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3687 
3688  /* Use generic string SortSupport, forcing "C" collation */
3689  varstr_sortsupport(ssup, C_COLLATION_OID, false);
3690 
3691  MemoryContextSwitchTo(oldcontext);
3692 
3693  PG_RETURN_VOID();
3694 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1835
#define PG_RETURN_VOID()
Definition: fmgr.h:314

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 482 of file varlena.c.

References AggCheckCallContext(), Assert, StringInfoData::data, StringInfoData::len, palloc(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BYTEA_P, PG_RETURN_NULL, SET_VARSIZE, VARDATA, and VARHDRSZ.

483 {
485 
486  /* cannot be called directly because of internal-type argument */
487  Assert(AggCheckCallContext(fcinfo, NULL));
488 
489  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
490 
491  if (state != NULL)
492  {
493  bytea *result;
494 
495  result = (bytea *) palloc(state->len + VARHDRSZ);
496  SET_VARSIZE(result, state->len + VARHDRSZ);
497  memcpy(VARDATA(result), state->data, state->len);
498  PG_RETURN_BYTEA_P(result);
499  }
500  else
501  PG_RETURN_NULL();
502 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define Assert(condition)
Definition: c.h:699
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 450 of file varlena.c.

References appendBinaryStringInfo(), makeStringAggState(), PG_ARGISNULL, PG_GETARG_BYTEA_PP, PG_GETARG_POINTER, PG_RETURN_POINTER, value, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

451 {
453 
454  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
455 
456  /* Append the value unless null. */
457  if (!PG_ARGISNULL(1))
458  {
460 
461  /* On the first time through, we ignore the delimiter. */
462  if (state == NULL)
463  state = makeStringAggState(fcinfo);
464  else if (!PG_ARGISNULL(2))
465  {
466  bytea *delim = PG_GETARG_BYTEA_PP(2);
467 
469  }
470 
472  }
473 
474  /*
475  * The transition type for string_agg() is declared to be "internal",
476  * which is a pass-by-value type the same size as a pointer.
477  */
478  PG_RETURN_POINTER(state);
479 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
ts_parserstate state
Definition: tsquery.c:81
static struct @131 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4654
Definition: c.h:516
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 2859 of file varlena.c.

References bytea_substring(), PG_GETARG_DATUM, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

2860 {
2862  PG_GETARG_INT32(1),
2863  PG_GETARG_INT32(2),
2864  false));
2865 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2882

◆ bytea_substr_no_len()

Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2873 of file varlena.c.

References bytea_substring(), PG_GETARG_DATUM, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

2874 {
2876  PG_GETARG_INT32(1),
2877  -1,
2878  true));
2879 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2882

◆ bytea_substring()

static bytea * bytea_substring ( Datum  str,
int  S,
int  L,
bool  length_not_specified 
)
static

Definition at line 2882 of file varlena.c.

References DatumGetByteaPSlice, ereport, errcode(), errmsg(), ERROR, Max, and PG_STR_GET_BYTEA.

Referenced by bytea_overlay(), bytea_substr(), and bytea_substr_no_len().

2886 {
2887  int S1; /* adjusted start position */
2888  int L1; /* adjusted substring length */
2889 
2890  S1 = Max(S, 1);
2891 
2892  if (length_not_specified)
2893  {
2894  /*
2895  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
2896  * end of the string if we pass it a negative value for length.
2897  */
2898  L1 = -1;
2899  }
2900  else
2901  {
2902  /* end position */
2903  int E = S + L;
2904 
2905  /*
2906  * A negative value for L is the only way for the end position to be
2907  * before the start. SQL99 says to throw an error.
2908  */
2909  if (E < S)
2910  ereport(ERROR,
2911  (errcode(ERRCODE_SUBSTRING_ERROR),
2912  errmsg("negative substring length not allowed")));
2913 
2914  /*
2915  * A zero or negative value for the end position can happen if the
2916  * start was negative or one. SQL99 says to return a zero-length
2917  * string.
2918  */
2919  if (E < 1)
2920  return PG_STR_GET_BYTEA("");
2921 
2922  L1 = E - S1;
2923  }
2924 
2925  /*
2926  * If the start position is past the end of the string, SQL99 says to
2927  * return a zero-length string -- DatumGetByteaPSlice() will do that for
2928  * us. Convert to zero-based starting position
2929  */
2930  return DatumGetByteaPSlice(str, S1 - 1, L1);
2931 }
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:272
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:2840
#define ERROR
Definition: elog.h:43
#define S(n, x)
Definition: sha1.c:55
#define ereport(elevel, rest)
Definition: elog.h:122
#define Max(x, y)
Definition: c.h:851
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2792 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

2793 {
2794  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2795  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2796 
2798 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2807
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
Definition: c.h:516

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3659 of file varlena.c.

References cmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_bitcmp(), gbt_byteacmp(), and leftmostvalue_char().

3660 {
3661  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3662  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3663  int len1,
3664  len2;
3665  int cmp;
3666 
3667  len1 = VARSIZE_ANY_EXHDR(arg1);
3668  len2 = VARSIZE_ANY_EXHDR(arg2);
3669 
3670  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3671  if ((cmp == 0) && (len1 != len2))
3672  cmp = (len1 < len2) ? -1 : 1;
3673 
3674  PG_FREE_IF_COPY(arg1, 0);
3675  PG_FREE_IF_COPY(arg2, 1);
3676 
3677  PG_RETURN_INT32(cmp);
3678 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteaeq()

Datum byteaeq ( PG_FUNCTION_ARGS  )

Definition at line 3515 of file varlena.c.

References DatumGetByteaPP, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

Referenced by gbt_byteaeq().

3516 {
3517  Datum arg1 = PG_GETARG_DATUM(0);
3518  Datum arg2 = PG_GETARG_DATUM(1);
3519  bool result;
3520  Size len1,
3521  len2;
3522 
3523  /*
3524  * We can use a fast path for unequal lengths, which might save us from
3525  * having to detoast one or both values.
3526  */
3527  len1 = toast_raw_datum_size(arg1);
3528  len2 = toast_raw_datum_size(arg2);
3529  if (len1 != len2)
3530  result = false;
3531  else
3532  {
3533  bytea *barg1 = DatumGetByteaPP(arg1);
3534  bytea *barg2 = DatumGetByteaPP(arg2);
3535 
3536  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3537  len1 - VARHDRSZ) == 0);
3538 
3539  PG_FREE_IF_COPY(barg1, 0);
3540  PG_FREE_IF_COPY(barg2, 1);
3541  }
3542 
3543  PG_RETURN_BOOL(result);
3544 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define VARHDRSZ
Definition: c.h:522
#define DatumGetByteaPP(X)
Definition: fmgr.h:260
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:433
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ byteage()

Datum byteage ( PG_FUNCTION_ARGS  )

Definition at line 3639 of file varlena.c.

References cmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteage().

3640 {
3641  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3642  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3643  int len1,
3644  len2;
3645  int cmp;
3646 
3647  len1 = VARSIZE_ANY_EXHDR(arg1);
3648  len2 = VARSIZE_ANY_EXHDR(arg2);
3649 
3650  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3651 
3652  PG_FREE_IF_COPY(arg1, 0);
3653  PG_FREE_IF_COPY(arg2, 1);
3654 
3655  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
3656 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteaGetBit()

Datum byteaGetBit ( PG_FUNCTION_ARGS  )

Definition at line 3073 of file varlena.c.

References byte, ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3074 {
3075  bytea *v = PG_GETARG_BYTEA_PP(0);
3076  int32 n = PG_GETARG_INT32(1);
3077  int byteNo,
3078  bitNo;
3079  int len;
3080  int byte;
3081 
3082  len = VARSIZE_ANY_EXHDR(v);
3083 
3084  if (n < 0 || n >= len * 8)
3085  ereport(ERROR,
3086  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3087  errmsg("index %d out of valid range, 0..%d",
3088  n, len * 8 - 1)));
3089 
3090  byteNo = n / 8;
3091  bitNo = n % 8;
3092 
3093  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3094 
3095  if (byte & (1 << bitNo))
3096  PG_RETURN_INT32(1);
3097  else
3098  PG_RETURN_INT32(0);
3099 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ byteaGetByte()

Datum byteaGetByte ( PG_FUNCTION_ARGS  )

Definition at line 3044 of file varlena.c.

References byte, ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3045 {
3046  bytea *v = PG_GETARG_BYTEA_PP(0);
3047  int32 n = PG_GETARG_INT32(1);
3048  int len;
3049  int byte;
3050 
3051  len = VARSIZE_ANY_EXHDR(v);
3052 
3053  if (n < 0 || n >= len)
3054  ereport(ERROR,
3055  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3056  errmsg("index %d out of valid range, 0..%d",
3057  n, len - 1)));
3058 
3059  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3060 
3061  PG_RETURN_INT32(byte);
3062 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ byteagt()

Datum byteagt ( PG_FUNCTION_ARGS  )

Definition at line 3619 of file varlena.c.

References cmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteagt().

3620 {
3621  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3622  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3623  int len1,
3624  len2;
3625  int cmp;
3626 
3627  len1 = VARSIZE_ANY_EXHDR(arg1);
3628  len2 = VARSIZE_ANY_EXHDR(arg2);
3629 
3630  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3631 
3632  PG_FREE_IF_COPY(arg1, 0);
3633  PG_FREE_IF_COPY(arg2, 1);
3634 
3635  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
3636 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteain()

Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 255 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, hex_decode(), palloc(), PG_GETARG_CSTRING, PG_RETURN_BYTEA_P, SET_VARSIZE, VAL, VARDATA, and VARHDRSZ.

Referenced by CreateTrigger(), and string_to_datum().

256 {
257  char *inputText = PG_GETARG_CSTRING(0);
258  char *tp;
259  char *rp;
260  int bc;
261  bytea *result;
262 
263  /* Recognize hex input */
264  if (inputText[0] == '\\' && inputText[1] == 'x')
265  {
266  size_t len = strlen(inputText);
267 
268  bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */
269  result = palloc(bc);
270  bc = hex_decode(inputText + 2, len - 2, VARDATA(result));
271  SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */
272 
273  PG_RETURN_BYTEA_P(result);
274  }
275 
276  /* Else, it's the traditional escaped style */
277  for (bc = 0, tp = inputText; *tp != '\0'; bc++)
278  {
279  if (tp[0] != '\\')
280  tp++;
281  else if ((tp[0] == '\\') &&
282  (tp[1] >= '0' && tp[1] <= '3') &&
283  (tp[2] >= '0' && tp[2] <= '7') &&
284  (tp[3] >= '0' && tp[3] <= '7'))
285  tp += 4;
286  else if ((tp[0] == '\\') &&
287  (tp[1] == '\\'))
288  tp += 2;
289  else
290  {
291  /*
292  * one backslash, not followed by another or ### valid octal
293  */
294  ereport(ERROR,
295  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
296  errmsg("invalid input syntax for type %s", "bytea")));
297  }
298  }
299 
300  bc += VARHDRSZ;
301 
302  result = (bytea *) palloc(bc);
303  SET_VARSIZE(result, bc);
304 
305  tp = inputText;
306  rp = VARDATA(result);
307  while (*tp != '\0')
308  {
309  if (tp[0] != '\\')
310  *rp++ = *tp++;
311  else if ((tp[0] == '\\') &&
312  (tp[1] >= '0' && tp[1] <= '3') &&
313  (tp[2] >= '0' && tp[2] <= '7') &&
314  (tp[3] >= '0' && tp[3] <= '7'))
315  {
316  bc = VAL(tp[1]);
317  bc <<= 3;
318  bc += VAL(tp[2]);
319  bc <<= 3;
320  *rp++ = bc + VAL(tp[3]);
321 
322  tp += 4;
323  }
324  else if ((tp[0] == '\\') &&
325  (tp[1] == '\\'))
326  {
327  *rp++ = '\\';
328  tp += 2;
329  }
330  else
331  {
332  /*
333  * We should never get here. The first pass should not allow it.
334  */
335  ereport(ERROR,
336  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
337  errmsg("invalid input syntax for type %s", "bytea")));
338  }
339  }
340 
341  PG_RETURN_BYTEA_P(result);
342 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
unsigned hex_decode(const char *src, unsigned len, char *dst)
Definition: encode.c:156
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247
#define VAL(CH)
Definition: varlena.c:240
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

Definition at line 3599 of file varlena.c.

References cmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_byteale().

3600 {
3601  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3602  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3603  int len1,
3604  len2;
3605  int cmp;
3606 
3607  len1 = VARSIZE_ANY_EXHDR(arg1);
3608  len2 = VARSIZE_ANY_EXHDR(arg2);
3609 
3610  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3611 
3612  PG_FREE_IF_COPY(arg1, 0);
3613  PG_FREE_IF_COPY(arg2, 1);
3614 
3615  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
3616 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bytealt()

Datum bytealt ( PG_FUNCTION_ARGS  )

Definition at line 3579 of file varlena.c.

References cmp(), Min, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gbt_bytealt().

3580 {
3581  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3582  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3583  int len1,
3584  len2;
3585  int cmp;
3586 
3587  len1 = VARSIZE_ANY_EXHDR(arg1);
3588  len2 = VARSIZE_ANY_EXHDR(arg2);
3589 
3590  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3591 
3592  PG_FREE_IF_COPY(arg1, 0);
3593  PG_FREE_IF_COPY(arg2, 1);
3594 
3595  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3596 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteane()

Datum byteane ( PG_FUNCTION_ARGS  )

Definition at line 3547 of file varlena.c.

References DatumGetByteaPP, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

3548 {
3549  Datum arg1 = PG_GETARG_DATUM(0);
3550  Datum arg2 = PG_GETARG_DATUM(1);
3551  bool result;
3552  Size len1,
3553  len2;
3554 
3555  /*
3556  * We can use a fast path for unequal lengths, which might save us from
3557  * having to detoast one or both values.
3558  */
3559  len1 = toast_raw_datum_size(arg1);
3560  len2 = toast_raw_datum_size(arg2);
3561  if (len1 != len2)
3562  result = true;
3563  else
3564  {
3565  bytea *barg1 = DatumGetByteaPP(arg1);
3566  bytea *barg2 = DatumGetByteaPP(arg2);
3567 
3568  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3569  len1 - VARHDRSZ) != 0);
3570 
3571  PG_FREE_IF_COPY(barg1, 0);
3572  PG_FREE_IF_COPY(barg2, 1);
3573  }
3574 
3575  PG_RETURN_BOOL(result);
3576 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define VARHDRSZ
Definition: c.h:522
#define DatumGetByteaPP(X)
Definition: fmgr.h:260
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:433
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ byteaoctetlen()

Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 2776 of file varlena.c.

References PG_GETARG_DATUM, PG_RETURN_INT32, generate_unaccent_rules::str, toast_raw_datum_size(), and VARHDRSZ.

2777 {
2778  Datum str = PG_GETARG_DATUM(0);
2779 
2780  /* We need not detoast the input at all */
2782 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define VARHDRSZ
Definition: c.h:522
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
uintptr_t Datum
Definition: postgres.h:367

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 351 of file varlena.c.

References bytea_output, BYTEA_OUTPUT_ESCAPE, BYTEA_OUTPUT_HEX, DIG, elog, ERROR, hex_encode(), i, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_CSTRING, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by patternsel(), and prefix_quals().

352 {
353  bytea *vlena = PG_GETARG_BYTEA_PP(0);
354  char *result;
355  char *rp;
356 
358  {
359  /* Print hex format */
360  rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
361  *rp++ = '\\';
362  *rp++ = 'x';
363  rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
364  }
365  else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
366  {
367  /* Print traditional escaped format */
368  char *vp;
369  int len;
370  int i;
371 
372  len = 1; /* empty string has 1 char */
373  vp = VARDATA_ANY(vlena);
374  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
375  {
376  if (*vp == '\\')
377  len += 2;
378  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
379  len += 4;
380  else
381  len++;
382  }
383  rp = result = (char *) palloc(len);
384  vp = VARDATA_ANY(vlena);
385  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
386  {
387  if (*vp == '\\')
388  {
389  *rp++ = '\\';
390  *rp++ = '\\';
391  }
392  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
393  {
394  int val; /* holds unprintable chars */
395 
396  val = *vp;
397  rp[0] = '\\';
398  rp[3] = DIG(val & 07);
399  val >>= 3;
400  rp[2] = DIG(val & 07);
401  val >>= 3;
402  rp[1] = DIG(val & 03);
403  rp += 4;
404  }
405  else
406  *rp++ = *vp;
407  }
408  }
409  else
410  {
411  elog(ERROR, "unrecognized bytea_output setting: %d",
412  bytea_output);
413  rp = result = NULL; /* keep compiler quiet */
414  }
415  *rp = '\0';
416  PG_RETURN_CSTRING(result);
417 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned hex_encode(const char *src, unsigned len, char *dst)
Definition: encode.c:126
#define ERROR
Definition: elog.h:43
int bytea_output
Definition: varlena.c:41
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int i
Definition: c.h:516
#define elog
Definition: elog.h:219
long val
Definition: informix.c:689
#define DIG(VAL)
Definition: varlena.c:241

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 2941 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

2942 {
2943  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2944  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2945  int sp = PG_GETARG_INT32(2); /* substring start position */
2946  int sl = PG_GETARG_INT32(3); /* substring length */
2947 
2948  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2949 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2964
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
Definition: c.h:516

◆ byteaoverlay_no_len()

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2952 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_BYTEA_P, and VARSIZE_ANY_EXHDR.

2953 {
2954  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2955  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2956  int sp = PG_GETARG_INT32(2); /* substring start position */
2957  int sl;
2958 
2959  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
2960  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2961 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2964
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ byteapos()

Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 3000 of file varlena.c.

References PG_GETARG_BYTEA_PP, PG_RETURN_INT32, px(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3001 {
3002  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3003  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3004  int pos;
3005  int px,
3006  p;
3007  int len1,
3008  len2;
3009  char *p1,
3010  *p2;
3011 
3012  len1 = VARSIZE_ANY_EXHDR(t1);
3013  len2 = VARSIZE_ANY_EXHDR(t2);
3014 
3015  if (len2 <= 0)
3016  PG_RETURN_INT32(1); /* result for empty pattern */
3017 
3018  p1 = VARDATA_ANY(t1);
3019  p2 = VARDATA_ANY(t2);
3020 
3021  pos = 0;
3022  px = (len1 - len2);
3023  for (p = 0; p <= px; p++)
3024  {
3025  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3026  {
3027  pos = p + 1;
3028  break;
3029  };
3030  p1++;
3031  };
3032 
3033  PG_RETURN_INT32(pos);
3034 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:277
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

Definition at line 423 of file varlena.c.

References buf, StringInfoData::cursor, StringInfoData::len, palloc(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_copymsgbytes(), SET_VARSIZE, VARDATA, and VARHDRSZ.

424 {
426  bytea *result;
427  int nbytes;
428 
429  nbytes = buf->len - buf->cursor;
430  result = (bytea *) palloc(nbytes + VARHDRSZ);
431  SET_VARSIZE(result, nbytes + VARHDRSZ);
432  pq_copymsgbytes(buf, VARDATA(result), nbytes);
433  PG_RETURN_BYTEA_P(result);
434 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
static char * buf
Definition: pg_test_fsync.c:67
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 442 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

Referenced by pg_dependencies_send(), and pg_ndistinct_send().

443 {
444  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
445 
446  PG_RETURN_BYTEA_P(vlena);
447 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:283
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
Definition: c.h:516

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3142 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_P_COPY, PG_GETARG_INT32, PG_RETURN_BYTEA_P, VARDATA, VARHDRSZ, and VARSIZE.

3143 {
3144  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3145  int32 n = PG_GETARG_INT32(1);
3146  int32 newBit = PG_GETARG_INT32(2);
3147  int len;
3148  int oldByte,
3149  newByte;
3150  int byteNo,
3151  bitNo;
3152 
3153  len = VARSIZE(res) - VARHDRSZ;
3154 
3155  if (n < 0 || n >= len * 8)
3156  ereport(ERROR,
3157  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3158  errmsg("index %d out of valid range, 0..%d",
3159  n, len * 8 - 1)));
3160 
3161  byteNo = n / 8;
3162  bitNo = n % 8;
3163 
3164  /*
3165  * sanity check!
3166  */
3167  if (newBit != 0 && newBit != 1)
3168  ereport(ERROR,
3169  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3170  errmsg("new bit must be 0 or 1")));
3171 
3172  /*
3173  * Update the byte.
3174  */
3175  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3176 
3177  if (newBit == 0)
3178  newByte = oldByte & (~(1 << bitNo));
3179  else
3180  newByte = oldByte | (1 << bitNo);
3181 
3182  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3183 
3184  PG_RETURN_BYTEA_P(res);
3185 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:522
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:283
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ byteaSetByte()

Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 3110 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_P_COPY, PG_GETARG_INT32, PG_RETURN_BYTEA_P, VARDATA, VARHDRSZ, and VARSIZE.

3111 {
3112  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3113  int32 n = PG_GETARG_INT32(1);
3114  int32 newByte = PG_GETARG_INT32(2);
3115  int len;
3116 
3117  len = VARSIZE(res) - VARHDRSZ;
3118 
3119  if (n < 0 || n >= len)
3120  ereport(ERROR,
3121  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3122  errmsg("index %d out of valid range, 0..%d",
3123  n, len - 1)));
3124 
3125  /*
3126  * Now set the byte.
3127  */
3128  ((unsigned char *) VARDATA(res))[n] = newByte;
3129 
3130  PG_RETURN_BYTEA_P(res);
3131 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:522
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:283
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ charlen_to_bytelen()

static int charlen_to_bytelen ( const char *  p,
int  n 
)
static

Definition at line 735 of file varlena.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), replace_text(), replace_text_regexp(), and text_to_array_internal().

736 {
738  {
739  /* Optimization for single-byte encodings */
740  return n;
741  }
742  else
743  {
744  const char *s;
745 
746  for (s = p; n > 0; n--)
747  s += pg_mblen(s);
748 
749  return s - p;
750  }
751 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760

◆ check_replace_text_has_escape_char()

static bool check_replace_text_has_escape_char ( const text replace_text)
static

Definition at line 3800 of file varlena.c.

References pg_database_encoding_max_length(), pg_mblen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

3801 {
3802  const char *p = VARDATA_ANY(replace_text);
3803  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3804 
3806  {
3807  for (; p < p_end; p++)
3808  {
3809  if (*p == '\\')
3810  return true;
3811  }
3812  }
3813  else
3814  {
3815  for (; p < p_end; p += pg_mblen(p))
3816  {
3817  if (*p == '\\')
3818  return true;
3819  }
3820  }
3821 
3822  return false;
3823 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ concat_internal()

static text* concat_internal ( const char *  sepstr,
int  argidx,
FunctionCallInfo  fcinfo 
)
static

Definition at line 4764 of file varlena.c.

References appendStringInfoString(), array_to_text_internal(), Assert, build_concat_foutcache(), cstring_to_text_with_len(), StringInfoData::data, FunctionCallInfoData::flinfo, FmgrInfo::fn_extra, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, initStringInfo(), StringInfoData::len, OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_NARGS, generate_unaccent_rules::str, and value.

Referenced by text_concat(), and text_concat_ws().

4766 {
4767  text *result;
4769  FmgrInfo *foutcache;
4770  bool first_arg = true;
4771  int i;
4772 
4773  /*
4774  * concat(VARIADIC some-array) is essentially equivalent to
4775  * array_to_text(), ie concat the array elements with the given separator.
4776  * So we just pass the case off to that code.
4777  */
4778  if (get_fn_expr_variadic(fcinfo->flinfo))
4779  {
4780  ArrayType *arr;
4781 
4782  /* Should have just the one argument */
4783  Assert(argidx == PG_NARGS() - 1);
4784 
4785  /* concat(VARIADIC NULL) is defined as NULL */
4786  if (PG_ARGISNULL(argidx))
4787  return NULL;
4788 
4789  /*
4790  * Non-null argument had better be an array. We assume that any call
4791  * context that could let get_fn_expr_variadic return true will have
4792  * checked that a VARIADIC-labeled parameter actually is an array. So
4793  * it should be okay to just Assert that it's an array rather than
4794  * doing a full-fledged error check.
4795  */
4797 
4798  /* OK, safe to fetch the array value */
4799  arr = PG_GETARG_ARRAYTYPE_P(argidx);
4800 
4801  /*
4802  * And serialize the array. We tell array_to_text to ignore null
4803  * elements, which matches the behavior of the loop below.
4804  */
4805  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
4806  }
4807 
4808  /* Normal case without explicit VARIADIC marker */
4809  initStringInfo(&str);
4810 
4811  /* Get output function info, building it if first time through */
4812  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
4813  if (foutcache == NULL)
4814  foutcache = build_concat_foutcache(fcinfo, argidx);
4815 
4816  for (i = argidx; i < PG_NARGS(); i++)
4817  {
4818  if (!PG_ARGISNULL(i))
4819  {
4821 
4822  /* add separator if appropriate */
4823  if (first_arg)
4824  first_arg = false;
4825  else
4826  appendStringInfoString(&str, sepstr);
4827 
4828  /* call the appropriate type output function, append the result */
4830  OutputFunctionCall(&foutcache[i], value));
4831  }
4832  }
4833 
4834  result = cstring_to_text_with_len(str.data, str.len);
4835  pfree(str.data);
4836 
4837  return result;
4838 }
Definition: fmgr.h:56
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2129
#define OidIsValid(objectId)
Definition: c.h:605
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
FmgrInfo * flinfo
Definition: fmgr.h:79
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
void pfree(void *pointer)
Definition: mcxt.c:1031
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1995
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4428
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:367
static struct @131 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define Assert(condition)
Definition: c.h:699
#define PG_NARGS()
Definition: fmgr.h:173
void * fn_extra
Definition: fmgr.h:64
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2575
int i
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:4726
Definition: c.h:516

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 149 of file varlena.c.

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_page_type(), cash_words(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_jsonb_path_all(), get_scalar(), hash_page_type(), hstore_to_json(), hstore_to_json_loose(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_array_element_text(), jsonb_object_field_text(), jsonb_typeof(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), pg_collation_actual_version(), pg_collation_for(), pg_crypt(), pg_current_logfile(), pg_describe_object(), pg_export_snapshot(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_tablespace_location(), pg_walfile_name(), pgsql_version(), pgxml_result_to_text(), quote_ident(), quote_ident_cstr(), quote_nullable(), sepgsql_getcon(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), set_config_by_name(), show_all_file_settings(), show_config_by_name(), show_config_by_name_missing_ok(), ShowAllGUCConfig(), soundex(), SPI_sql_row_to_xmlelement(), split_text(), ssl_cipher(), ssl_version(), string_to_text(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), txid_status(), unaccent_dict(), upper(), X509_NAME_to_text(), xml_encode_special_chars(), xml_in(), and xml_recv().

150 {
151  return cstring_to_text_with_len(s, strlen(s));
152 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161

◆ cstring_to_text_with_len()

text* cstring_to_text_with_len ( const char *  s,
int  len 
)

Definition at line 161 of file varlena.c.

References palloc(), SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), compute_tsvector_stats(), concat_internal(), cstring_to_text(), cstring_to_xmltype(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), elements_worker_jsonb(), ExecEvalXmlExpr(), fsm_page_contents(), get_array_element_end(), get_array_end(), get_jsonb_path_all(), get_object_end(), get_object_field_end(), get_scalar(), gin_extract_tsquery(), gin_extract_tsvector(), hstore_akeys(), hstore_avals(), hstore_each(), hstore_fetchval(), hstore_skeys(), hstore_slice_to_array(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array(), json_build_array_noargs(), json_build_object(), json_build_object_noargs(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_array_element_text(), jsonb_object_field_text(), jsonb_pretty(), leftmostvalue_text(), LogicalOutputWrite(), parse_ident(), pg_gen_salt(), pg_gen_salt_rounds(), replace_text(), replace_text_regexp(), row_to_json(), row_to_json_pretty(), serialize_deflist(), ssl_extension_info(), string_agg_finalfn(), stringinfo_to_xmltype(), text_format(), text_left(), text_right(), text_to_array_internal(), textrecv(), to_json(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), varchar(), varchar_input(), and xslt_process().

162 {
163  text *result = (text *) palloc(len + VARHDRSZ);
164 
165  SET_VARSIZE(result, len + VARHDRSZ);
166  memcpy(VARDATA(result), s, len);
167 
168  return result;
169 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 2651 of file varlena.c.

References Min, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by bttext_pattern_cmp(), text_pattern_ge(), text_pattern_gt(), text_pattern_le(), and text_pattern_lt().

2652 {
2653  int result;
2654  int len1,
2655  len2;
2656 
2657  len1 = VARSIZE_ANY_EXHDR(arg1);
2658  len2 = VARSIZE_ANY_EXHDR(arg2);
2659 
2660  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2661  if (result != 0)
2662  return result;
2663  else if (len1 < len2)
2664  return -1;
2665  else if (len1 > len2)
2666  return 1;
2667  else
2668  return 0;
2669 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:857
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 4654 of file varlena.c.

References AggCheckCallContext(), elog, ERROR, makeStringInfo(), and MemoryContextSwitchTo().

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

4655 {
4656  StringInfo state;
4657  MemoryContext aggcontext;
4658  MemoryContext oldcontext;
4659 
4660  if (!AggCheckCallContext(fcinfo, &aggcontext))
4661  {
4662  /* cannot be called directly because of internal-type argument */
4663  elog(ERROR, "string_agg_transfn called in non-aggregate context");
4664  }
4665 
4666  /*
4667  * Create state in aggregate context. It'll stay there across subsequent
4668  * calls.
4669  */
4670  oldcontext = MemoryContextSwitchTo(aggcontext);
4671  state = makeStringInfo();
4672  MemoryContextSwitchTo(oldcontext);
4673 
4674  return state;
4675 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:43
ts_parserstate state
Definition: tsquery.c:81
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
#define elog
Definition: elog.h:219

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3215 of file varlena.c.

References cstring_to_text(), NameStr, PG_GETARG_NAME, and PG_RETURN_TEXT_P.

Referenced by nameiclike(), and nameicnlike().

3216 {
3217  Name s = PG_GETARG_NAME(0);
3218 
3220 }
Definition: c.h:570
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define NameStr(name)
Definition: c.h:576
#define PG_GETARG_NAME(n)
Definition: fmgr.h:248

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 4600 of file varlena.c.

References DatumGetCString, elog, ERROR, get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_INT32, toast_datum_size(), typlen, and value.

4601 {
4603  int32 result;
4604  int typlen;
4605 
4606  /* On first call, get the input type's typlen, and save at *fn_extra */
4607  if (fcinfo->flinfo->fn_extra == NULL)
4608  {
4609  /* Lookup the datatype of the supplied argument */
4610  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4611 
4612  typlen = get_typlen(argtypeid);
4613  if (typlen == 0) /* should not happen */
4614  elog(ERROR, "cache lookup failed for type %u", argtypeid);
4615 
4616  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4617  sizeof(int));
4618  *((int *) fcinfo->flinfo->fn_extra) = typlen;
4619  }
4620  else
4621  typlen = *((int *) fcinfo->flinfo->fn_extra);
4622 
4623  if (typlen == -1)
4624  {
4625  /* varlena type, possibly toasted */
4626  result = toast_datum_size(value);
4627  }
4628  else if (typlen == -2)
4629  {
4630  /* cstring */
4631  result = strlen(DatumGetCString(value)) + 1;
4632  }
4633  else
4634  {
4635  /* ordinary fixed-width type */
4636  result = typlen;
4637  }
4638 
4639  PG_RETURN_INT32(result);
4640 }
int16 typlen
Definition: pg_type.h:55
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:551
Size toast_datum_size(Datum value)
Definition: tuptoaster.c:409
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1995
uintptr_t Datum
Definition: postgres.h:367
static struct @131 value
int16 get_typlen(Oid typid)
Definition: lsyscache.c:1951
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
#define elog
Definition: elog.h:219

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3717 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoText(), charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), StringInfoData::data, initStringInfo(), StringInfoData::len, TextPositionState::len1, TextPositionState::len2, pfree(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, generate_unaccent_rules::str, text_position_cleanup(), text_position_next(), text_position_setup(), VARDATA_ANY, and VARSIZE_ANY.

Referenced by execute_extension_script().

3718 {
3719  text *src_text = PG_GETARG_TEXT_PP(0);
3720  text *from_sub_text = PG_GETARG_TEXT_PP(1);
3721  text *to_sub_text = PG_GETARG_TEXT_PP(2);
3722  int src_text_len;
3723  int from_sub_text_len;
3725  text *ret_text;
3726  int start_posn;
3727  int curr_posn;
3728  int chunk_len;
3729  char *start_ptr;
3731 
3732  text_position_setup(src_text, from_sub_text, &state);
3733 
3734  /*
3735  * Note: we check the converted string length, not the original, because
3736  * they could be different if the input contained invalid encoding.
3737  */
3738  src_text_len = state.len1;
3739  from_sub_text_len = state.len2;
3740 
3741  /* Return unmodified source string if empty source or pattern */
3742  if (src_text_len < 1 || from_sub_text_len < 1)
3743  {
3744  text_position_cleanup(&state);
3745  PG_RETURN_TEXT_P(src_text);
3746  }
3747 
3748  start_posn = 1;
3749  curr_posn = text_position_next(1, &state);
3750 
3751  /* When the from_sub_text is not found, there is nothing to do. */
3752  if (curr_posn == 0)
3753  {
3754  text_position_cleanup(&state);
3755  PG_RETURN_TEXT_P(src_text);
3756  }
3757 
3758  /* start_ptr points to the start_posn'th character of src_text */
3759  start_ptr = VARDATA_ANY(src_text);
3760 
3761  initStringInfo(&str);
3762 
3763  do
3764  {
3766 
3767  /* copy the data skipped over by last text_position_next() */
3768  chunk_len = charlen_to_bytelen(start_ptr, curr_posn - start_posn);
3769  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3770 
3771  appendStringInfoText(&str, to_sub_text);
3772 
3773  start_posn = curr_posn;
3774  start_ptr += chunk_len;
3775  start_posn += from_sub_text_len;
3776  start_ptr += charlen_to_bytelen(start_ptr, from_sub_text_len);
3777 
3778  curr_posn = text_position_next(start_posn, &state);
3779  }
3780  while (curr_posn > 0);
3781 
3782  /* copy trailing data */
3783  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
3784  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3785 
3786  text_position_cleanup(&state);
3787 
3788  ret_text = cstring_to_text_with_len(str.data, str.len);
3789  pfree(str.data);
3790 
3791  PG_RETURN_TEXT_P(ret_text);
3792 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1118
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
void pfree(void *pointer)
Definition: mcxt.c:1031
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1230
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:735
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3703
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
ts_parserstate state
Definition: tsquery.c:81
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1364
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
Definition: c.h:516
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ replace_text_regexp()

text* replace_text_regexp ( text src_text,
void *  regexp,
text replace_text,
bool  glob 
)

Definition at line 3938 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoRegexpSubstr(), appendStringInfoText(), buf, charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, check_replace_text_has_escape_char(), cstring_to_text_with_len(), StringInfoData::data, ereport, errcode(), errmsg(), ERROR, initStringInfo(), StringInfoData::len, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regerror(), pg_regexec(), REG_NOMATCH, REG_OKAY, REGEXP_REPLACE_BACKREF_CNT, regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by textregexreplace(), and textregexreplace_noopt().

3940 {
3941  text *ret_text;
3942  regex_t *re = (regex_t *) regexp;
3943  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
3946  pg_wchar *data;
3947  size_t data_len;
3948  int search_start;
3949  int data_pos;
3950  char *start_ptr;
3951  bool have_escape;
3952 
3953  initStringInfo(&buf);
3954 
3955  /* Convert data string to wide characters. */
3956  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
3957  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
3958 
3959  /* Check whether replace_text has escape char. */
3960  have_escape = check_replace_text_has_escape_char(replace_text);
3961 
3962  /* start_ptr points to the data_pos'th character of src_text */
3963  start_ptr = (char *) VARDATA_ANY(src_text);
3964  data_pos = 0;
3965 
3966  search_start = 0;
3967  while (search_start <= data_len)
3968  {
3969  int regexec_result;
3970 
3972 
3973  regexec_result = pg_regexec(re,
3974  data,
3975  data_len,
3976  search_start,
3977  NULL, /* no details */
3979  pmatch,
3980  0);
3981 
3982  if (regexec_result == REG_NOMATCH)
3983  break;
3984 
3985  if (regexec_result != REG_OKAY)
3986  {
3987  char errMsg[100];
3988 
3990  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
3991  ereport(ERROR,
3992  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
3993  errmsg("regular expression failed: %s", errMsg)));
3994  }
3995 
3996  /*
3997  * Copy the text to the left of the match position. Note we are given
3998  * character not byte indexes.
3999  */
4000  if (pmatch[0].rm_so - data_pos > 0)
4001  {
4002  int chunk_len;
4003 
4004  chunk_len = charlen_to_bytelen(start_ptr,
4005  pmatch[0].rm_so - data_pos);
4006  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4007 
4008  /*
4009  * Advance start_ptr over that text, to avoid multiple rescans of
4010  * it if the replace_text contains multiple back-references.
4011  */
4012  start_ptr += chunk_len;
4013  data_pos = pmatch[0].rm_so;
4014  }
4015 
4016  /*
4017  * Copy the replace_text. Process back references when the
4018  * replace_text has escape characters.
4019  */
4020  if (have_escape)
4021  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4022  start_ptr, data_pos);
4023  else
4024  appendStringInfoText(&buf, replace_text);
4025 
4026  /* Advance start_ptr and data_pos over the matched text. */
4027  start_ptr += charlen_to_bytelen(start_ptr,
4028  pmatch[0].rm_eo - data_pos);
4029  data_pos = pmatch[0].rm_eo;
4030 
4031  /*
4032  * When global option is off, replace the first instance only.
4033  */
4034  if (!glob)
4035  break;
4036 
4037  /*
4038  * Advance search position. Normally we start the next search at the
4039  * end of the previous match; but if the match was of zero length, we
4040  * have to advance by one character, or we'd just find the same match
4041  * again.
4042  */
4043  search_start = data_pos;
4044  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4045  search_start++;
4046  }
4047 
4048  /*
4049  * Copy the text to the right of the last match.
4050  */
4051  if (data_pos < data_len)
4052  {
4053  int chunk_len;
4054 
4055  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4056  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4057  }
4058 
4059  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4060  pfree(buf.data);
4061  pfree(data);
4062 
4063  return ret_text;
4064 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
int errcode(int sqlerrcode)
Definition: elog.c:575
regoff_t rm_eo
Definition: regex.h:86
void pfree(void *pointer)
Definition: mcxt.c:1031
#define REG_OKAY
Definition: regex.h:137
#define ERROR
Definition: elog.h:43
static bool check_replace_text_has_escape_char(const text *replace_text)
Definition: varlena.c:3800
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:735
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3703
static char * buf
Definition: pg_test_fsync.c:67
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned int pg_wchar
Definition: mbprint.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define REGEXP_REPLACE_BACKREF_CNT
Definition: varlena.c:3927
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:723
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:3833
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition: regexec.c:172
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define REG_NOMATCH
Definition: regex.h:138
Definition: c.h:516
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
Definition: regex.h:55

◆ rest_of_char_same()

static bool rest_of_char_same ( const char *  s1,
const char *  s2,
int  len 
)
inlinestatic

Definition at line 5490 of file varlena.c.

Referenced by varstr_levenshtein().

5491 {
5492  while (len > 0)
5493  {
5494  len--;
5495  if (s1[len] != s2[len])
5496  return false;
5497  }
5498  return true;
5499 }
char * s1
char * s2

◆ split_text()

Datum split_text ( PG_FUNCTION_ARGS  )

Definition at line 4073 of file varlena.c.

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, TextPositionState::len1, TextPositionState::len2, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, PointerGetDatum, text_position_cleanup(), text_position_next(), text_position_setup(), and text_substring().

4074 {
4075  text *inputstring = PG_GETARG_TEXT_PP(0);
4076  text *fldsep = PG_GETARG_TEXT_PP(1);
4077  int fldnum = PG_GETARG_INT32(2);
4078  int inputstring_len;
4079  int fldsep_len;
4081  int start_posn;
4082  int end_posn;
4083  text *result_text;
4084 
4085  /* field number is 1 based */
4086  if (fldnum < 1)
4087  ereport(ERROR,
4088  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4089  errmsg("field position must be greater than zero")));
4090 
4091  text_position_setup(inputstring, fldsep, &state);
4092 
4093  /*
4094  * Note: we check the converted string length, not the original, because
4095  * they could be different if the input contained invalid encoding.
4096  */
4097  inputstring_len = state.len1;
4098  fldsep_len = state.len2;
4099 
4100  /* return empty string for empty input string */
4101  if (inputstring_len < 1)
4102  {
4103  text_position_cleanup(&state);
4105  }
4106 
4107  /* empty field separator */
4108  if (fldsep_len < 1)
4109  {
4110  text_position_cleanup(&state);
4111  /* if first field, return input string, else empty string */
4112  if (fldnum == 1)
4113  PG_RETURN_TEXT_P(inputstring);
4114  else
4116  }
4117 
4118  /* identify bounds of first field */
4119  start_posn = 1;
4120  end_posn = text_position_next(1, &state);
4121 
4122  /* special case if fldsep not found at all */
4123  if (end_posn == 0)
4124  {
4125  text_position_cleanup(&state);
4126  /* if field 1 requested, return input string, else empty string */
4127  if (fldnum == 1)
4128  PG_RETURN_TEXT_P(inputstring);
4129  else
4131  }
4132 
4133  while (end_posn > 0 && --fldnum > 0)
4134  {
4135  /* identify bounds of next field */
4136  start_posn = end_posn + fldsep_len;
4137  end_posn = text_position_next(start_posn, &state);
4138  }
4139 
4140  text_position_cleanup(&state);
4141 
4142  if (fldnum > 0)
4143  {
4144  /* N'th field separator not found */
4145  /* if last field requested, return it, else empty string */
4146  if (fldnum == 1)
4147  result_text = text_substring(PointerGetDatum(inputstring),
4148  start_posn,
4149  -1,
4150  true);
4151  else
4152  result_text = cstring_to_text("");
4153  }
4154  else
4155  {
4156  /* non-last field requested */
4157  result_text = text_substring(PointerGetDatum(inputstring),
4158  start_posn,
4159  end_posn - start_posn,
4160  false);
4161  }
4162 
4163  PG_RETURN_TEXT_P(result_text);
4164 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PointerGetDatum(X)
Definition: postgres.h:541
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1118
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define ERROR
Definition: elog.h:43
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1230
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define ereport(elevel, rest)
Definition: elog.h:122
ts_parserstate state
Definition: tsquery.c:81
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1364
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ SplitDirectoriesString()

bool SplitDirectoriesString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3417 of file varlena.c.

References canonicalize_path(), lappend(), MAXPGPATH, memmove, NIL, pstrdup(), and scanner_isspace().

Referenced by load_libraries(), and PostmasterMain().

3419 {
3420  char *nextp = rawstring;
3421  bool done = false;
3422 
3423  *namelist = NIL;
3424 
3425  while (scanner_isspace(*nextp))
3426  nextp++; /* skip leading whitespace */
3427 
3428  if (*nextp == '\0')
3429  return true; /* allow empty string */
3430 
3431  /* At the top of the loop, we are at start of a new directory. */
3432  do
3433  {
3434  char *curname;
3435  char *endp;
3436 
3437  if (*nextp == '"')
3438  {
3439  /* Quoted name --- collapse quote-quote pairs */
3440  curname = nextp + 1;
3441  for (;;)
3442  {
3443  endp = strchr(nextp + 1, '"');
3444  if (endp == NULL)
3445  return false; /* mismatched quotes */
3446  if (endp[1] != '"')
3447  break; /* found end of quoted name */
3448  /* Collapse adjacent quotes into one quote, and look again */
3449  memmove(endp, endp + 1, strlen(endp));
3450  nextp = endp;
3451  }
3452  /* endp now points at the terminating quote */
3453  nextp = endp + 1;
3454  }
3455  else
3456  {
3457  /* Unquoted name --- extends to separator or end of string */
3458  curname = endp = nextp;
3459  while (*nextp && *nextp != separator)
3460  {
3461  /* trailing whitespace should not be included in name */
3462  if (!scanner_isspace(*nextp))
3463  endp = nextp + 1;
3464  nextp++;
3465  }
3466  if (curname == endp)
3467  return false; /* empty unquoted name not allowed */
3468  }
3469 
3470  while (scanner_isspace(*nextp))
3471  nextp++; /* skip trailing whitespace */
3472 
3473  if (*nextp == separator)
3474  {
3475  nextp++;
3476  while (scanner_isspace(*nextp))
3477  nextp++; /* skip leading whitespace for next */
3478  /* we expect another name, so done remains false */
3479  }
3480  else if (*nextp == '\0')
3481  done = true;
3482  else
3483  return false; /* invalid syntax */
3484 
3485  /* Now safe to overwrite separator with a null */
3486  *endp = '\0';
3487 
3488  /* Truncate path if it's overlength */
3489  if (strlen(curname) >= MAXPGPATH)
3490  curname[MAXPGPATH - 1] = '\0';
3491 
3492  /*
3493  * Finished isolating current name --- add it to list
3494  */
3495  curname = pstrdup(curname);
3496  canonicalize_path(curname);
3497  *namelist = lappend(*namelist, curname);
3498 
3499  /* Loop back if we didn't reach end of string */
3500  } while (!done);
3501 
3502  return true;
3503 }
#define NIL
Definition: pg_list.h:69
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void canonicalize_path(char *path)
Definition: path.c:254
#define MAXPGPATH
#define memmove(d, s, c)
Definition: c.h:1135
List * lappend(List *list, void *datum)
Definition: list.c:128
bool scanner_isspace(char ch)
Definition: scansup.c:221

◆ SplitIdentifierString()

bool SplitIdentifierString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3290 of file varlena.c.

References Assert, downcase_truncate_identifier(), lappend(), memmove, NIL, pfree(), scanner_isspace(), and truncate_identifier().

Referenced by check_datestyle(), check_log_destination(), check_search_path(), check_temp_tablespaces(), check_wal_consistency_checking(), ExtractExtensionList(), parse_extension_control_file(), parse_hba_auth_opt(), parse_output_parameters(), parse_publication_options(), plpgsql_extra_checks_check_hook(), PostmasterMain(), PrepareTempTablespaces(), recomputeNamespacePath(), stringToQualifiedNameList(), and textToQualifiedNameList().

3292 {
3293  char *nextp = rawstring;
3294  bool done = false;
3295 
3296  *namelist = NIL;
3297 
3298  while (scanner_isspace(*nextp))
3299  nextp++; /* skip leading whitespace */
3300 
3301  if (*nextp == '\0')
3302  return true; /* allow empty string */
3303 
3304  /* At the top of the loop, we are at start of a new identifier. */
3305  do
3306  {
3307  char *curname;
3308  char *endp;
3309 
3310  if (*nextp == '"')
3311  {
3312  /* Quoted name --- collapse quote-quote pairs, no downcasing */
3313  curname = nextp + 1;
3314  for (;;)
3315  {
3316  endp = strchr(nextp + 1, '"');
3317  if (endp == NULL)
3318  return false; /* mismatched quotes */
3319  if (endp[1] != '"')
3320  break; /* found end of quoted name */
3321  /* Collapse adjacent quotes into one quote, and look again */
3322  memmove(endp, endp + 1, strlen(endp));
3323  nextp = endp;
3324  }
3325  /* endp now points at the terminating quote */
3326  nextp = endp + 1;
3327  }
3328  else
3329  {
3330  /* Unquoted name --- extends to separator or whitespace */
3331  char *downname;
3332  int len;
3333 
3334  curname = nextp;
3335  while (*nextp && *nextp != separator &&
3336  !scanner_isspace(*nextp))
3337  nextp++;
3338  endp = nextp;
3339  if (curname == nextp)
3340  return false; /* empty unquoted name not allowed */
3341 
3342  /*
3343  * Downcase the identifier, using same code as main lexer does.
3344  *
3345  * XXX because we want to overwrite the input in-place, we cannot
3346  * support a downcasing transformation that increases the string
3347  * length. This is not a problem given the current implementation
3348  * of downcase_truncate_identifier, but we'll probably have to do
3349  * something about this someday.
3350  */
3351  len = endp - curname;
3352  downname = downcase_truncate_identifier(curname, len, false);
3353  Assert(strlen(downname) <= len);
3354  strncpy(curname, downname, len); /* strncpy is required here */
3355  pfree(downname);
3356  }
3357 
3358  while (scanner_isspace(*nextp))
3359  nextp++; /* skip trailing whitespace */
3360 
3361  if (*nextp == separator)
3362  {
3363  nextp++;
3364  while (scanner_isspace(*nextp))
3365  nextp++; /* skip leading whitespace for next */
3366  /* we expect another name, so done remains false */
3367  }
3368  else if (*nextp == '\0')
3369  done = true;
3370  else
3371  return false; /* invalid syntax */
3372 
3373  /* Now safe to overwrite separator with a null */
3374  *endp = '\0';
3375 
3376  /* Truncate name if it's overlength */
3377  truncate_identifier(curname, strlen(curname), false);
3378 
3379  /*
3380  * Finished isolating current name --- add it to list
3381  */
3382  *namelist = lappend(*namelist, curname);
3383 
3384  /* Loop back if we didn't reach end of string */
3385  } while (!done);
3386 
3387  return true;
3388 }
#define NIL
Definition: pg_list.h:69
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
void truncate_identifier(char *ident, int len, bool warn)
Definition: scansup.c:187
void pfree(void *pointer)
Definition: mcxt.c:1031
#define memmove(d, s, c)
Definition: c.h:1135
List * lappend(List *list, void *datum)
Definition: list.c:128
bool scanner_isspace(char ch)
Definition: scansup.c:221
#define Assert(condition)
Definition: c.h:699

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 4704 of file varlena.c.

References AggCheckCallContext(), Assert, cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

4705 {
4706  StringInfo state;
4707 
4708  /* cannot be called directly because of internal-type argument */
4709  Assert(AggCheckCallContext(fcinfo, NULL));
4710 
4711  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4712 
4713  if (state != NULL)
4715  else
4716  PG_RETURN_NULL();
4717 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
ts_parserstate state
Definition: tsquery.c:81
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define Assert(condition)
Definition: c.h:699
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 4678 of file varlena.c.

References appendStringInfoText(), makeStringAggState(), PG_ARGISNULL, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, and PG_RETURN_POINTER.

4679 {
4680  StringInfo state;
4681 
4682  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4683 
4684  /* Append the value unless null. */
4685  if (!PG_ARGISNULL(1))
4686  {
4687  /* On the first time through, we ignore the delimiter. */
4688  if (state == NULL)
4689  state = makeStringAggState(fcinfo);
4690  else if (!PG_ARGISNULL(2))
4691  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
4692 
4693  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
4694  }
4695 
4696  /*
4697  * The transition type for string_agg() is declared to be "internal",
4698  * which is a pass-by-value type the same size as a pointer.
4699  */
4700  PG_RETURN_POINTER(state);
4701 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3703
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4654

◆ text_catenate()

static text * text_catenate ( text t1,
text t2 
)
static

Definition at line 694 of file varlena.c.

References palloc(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by text_overlay(), and textcat().

695 {
696  text *result;
697  int len1,
698  len2,
699  len;
700  char *ptr;
701 
702  len1 = VARSIZE_ANY_EXHDR(t1);
703  len2 = VARSIZE_ANY_EXHDR(t2);
704 
705  /* paranoia ... probably should throw error instead? */
706  if (len1 < 0)
707  len1 = 0;
708  if (len2 < 0)
709  len2 = 0;
710 
711  len = len1 + len2 + VARHDRSZ;
712  result = (text *) palloc(len);
713 
714  /* Set size of result string... */
715  SET_VARSIZE(result, len);
716 
717  /* Fill data field of result string... */
718  ptr = VARDATA(result);
719  if (len1 > 0)
720  memcpy(ptr, VARDATA_ANY(t1), len1);
721  if (len2 > 0)
722  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
723 
724  return result;
725 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ text_cmp()

static int text_cmp ( text arg1,
text arg2,
Oid  collid 
)
static

Definition at line 1616 of file varlena.c.

References VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

Referenced by bttextcmp(), text_ge(), text_gt(), text_larger(), text_le(), text_lt(), and text_smaller().

1617 {
1618  char *a1p,
1619  *a2p;
1620  int len1,
1621  len2;
1622 
1623  a1p = VARDATA_ANY(arg1);
1624  a2p = VARDATA_ANY(arg2);
1625 
1626  len1 = VARSIZE_ANY_EXHDR(arg1);
1627  len2 = VARSIZE_ANY_EXHDR(arg2);
1628 
1629  return varstr_cmp(a1p, len1, a2p, len2, collid);
1630 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1381
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 4844 of file varlena.c.

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

4845 {
4846  text *result;
4847 
4848  result = concat_internal("", 0, fcinfo);
4849  if (result == NULL)
4850  PG_RETURN_NULL();
4851  PG_RETURN_TEXT_P(result);
4852 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4764
Definition: c.h:516
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 4859 of file varlena.c.

References concat_internal(), PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

4860 {
4861  char *sep;
4862  text *result;
4863 
4864  /* return NULL when separator is NULL */
4865  if (PG_ARGISNULL(0))
4866  PG_RETURN_NULL();
4868 
4869  result = concat_internal(sep, 1, fcinfo);
4870  if (result == NULL)
4871  PG_RETURN_NULL();
4872  PG_RETURN_TEXT_P(result);
4873 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4764
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:516
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 4976 of file varlena.c.

References ADVANCE_PARSE_POINTER, appendStringInfoCharMacro, arg, ARR_ELEMTYPE, Assert, cstring_to_text_with_len(), StringInfoData::data, DatumGetInt16, DatumGetInt32, deconstruct_array(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, fmgr_info(), get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), get_typlenbyvalalign(), getTypeOutputInfo(), initStringInfo(), InvalidOid, StringInfoData::len, OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, pg_atoi(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, generate_unaccent_rules::str, text_format_parse_format(), text_format_string_conversion(), value, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by text_format_nv().

4977 {
4978  text *fmt;
4980  const char *cp;
4981  const char *start_ptr;
4982  const char *end_ptr;
4983  text *result;
4984  int arg;
4985  bool funcvariadic;
4986  int nargs;
4987  Datum *elements = NULL;
4988  bool *nulls = NULL;
4989  Oid element_type = InvalidOid;
4990  Oid prev_type = InvalidOid;
4991  Oid prev_width_type = InvalidOid;
4992  FmgrInfo typoutputfinfo;
4993  FmgrInfo typoutputinfo_width;
4994 
4995  /* When format string is null, immediately return null */
4996  if (PG_ARGISNULL(0))
4997  PG_RETURN_NULL();
4998 
4999  /* If argument is marked VARIADIC, expand array into elements */
5000  if (get_fn_expr_variadic(fcinfo->flinfo))
5001  {
5002  ArrayType *arr;
5003  int16 elmlen;
5004  bool elmbyval;
5005  char elmalign;
5006  int nitems;
5007 
5008  /* Should have just the one argument */
5009  Assert(PG_NARGS() == 2);
5010 
5011  /* If argument is NULL, we treat it as zero-length array */
5012  if (PG_ARGISNULL(1))
5013  nitems = 0;
5014  else
5015  {
5016  /*
5017  * Non-null argument had better be an array. We assume that any
5018  * call context that could let get_fn_expr_variadic return true
5019  * will have checked that a VARIADIC-labeled parameter actually is
5020  * an array. So it should be okay to just Assert that it's an
5021  * array rather than doing a full-fledged error check.
5022  */
5024 
5025  /* OK, safe to fetch the array value */
5026  arr = PG_GETARG_ARRAYTYPE_P(1);
5027 
5028  /* Get info about array element type */
5029  element_type = ARR_ELEMTYPE(arr);
5030  get_typlenbyvalalign(element_type,
5031  &elmlen, &elmbyval, &elmalign);
5032 
5033  /* Extract all array elements */
5034  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5035  &elements, &nulls, &nitems);
5036  }
5037 
5038  nargs = nitems + 1;
5039  funcvariadic = true;
5040  }
5041  else
5042  {
5043  /* Non-variadic case, we'll process the arguments individually */
5044  nargs = PG_NARGS();
5045  funcvariadic = false;
5046  }
5047 
5048  /* Setup for main loop. */
5049  fmt = PG_GETARG_TEXT_PP(0);
5050  start_ptr = VARDATA_ANY(fmt);
5051  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5052  initStringInfo(&str);
5053  arg = 1; /* next argument position to print */
5054 
5055  /* Scan format string, looking for conversion specifiers. */
5056  for (cp = start_ptr; cp < end_ptr; cp++)
5057  {
5058  int argpos;
5059  int widthpos;
5060  int flags;
5061  int width;
5062  Datum value;
5063  bool isNull;
5064  Oid typid;
5065 
5066  /*
5067  * If it's not the start of a conversion specifier, just copy it to
5068  * the output buffer.
5069  */
5070  if (*cp != '%')
5071  {
5072  appendStringInfoCharMacro(&str, *cp);
5073  continue;
5074  }
5075 
5076  ADVANCE_PARSE_POINTER(cp, end_ptr);
5077 
5078  /* Easy case: %% outputs a single % */
5079  if (*cp == '%')
5080  {
5081  appendStringInfoCharMacro(&str, *cp);
5082  continue;
5083  }
5084 
5085  /* Parse the optional portions of the format specifier */
5086  cp = text_format_parse_format(cp, end_ptr,
5087  &argpos, &widthpos,
5088  &flags, &width);
5089 
5090  /*
5091  * Next we should see the main conversion specifier. Whether or not
5092  * an argument position was present, it's known that at least one
5093  * character remains in the string at this point. Experience suggests
5094  * that it's worth checking that that character is one of the expected
5095  * ones before we try to fetch arguments, so as to produce the least
5096  * confusing response to a mis-formatted specifier.
5097  */
5098  if (strchr("sIL", *cp) == NULL)
5099  ereport(ERROR,
5100  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5101  errmsg("unrecognized format() type specifier \"%c\"",
5102  *cp),
5103  errhint("For a single \"%%\" use \"%%%%\".")));
5104 
5105  /* If indirect width was specified, get its value */
5106  if (widthpos >= 0)
5107  {
5108  /* Collect the specified or next argument position */
5109  if (widthpos > 0)
5110  arg = widthpos;
5111  if (arg >= nargs)
5112  ereport(ERROR,
5113  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5114  errmsg("too few arguments for format()")));
5115 
5116  /* Get the value and type of the selected argument */
5117  if (!funcvariadic)
5118  {
5119  value = PG_GETARG_DATUM(arg);
5120  isNull = PG_ARGISNULL(arg);
5121  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5122  }
5123  else
5124  {
5125  value = elements[arg - 1];
5126  isNull = nulls[arg - 1];
5127  typid = element_type;
5128  }
5129  if (!OidIsValid(typid))
5130  elog(ERROR, "could not determine data type of format() input");
5131 
5132  arg++;
5133 
5134  /* We can treat NULL width the same as zero */
5135  if (isNull)
5136  width = 0;
5137  else if (typid == INT4OID)
5138  width = DatumGetInt32(value);
5139  else if (typid == INT2OID)
5140  width = DatumGetInt16(value);
5141  else
5142  {
5143  /* For less-usual datatypes, convert to text then to int */
5144  char *str;
5145 
5146  if (typid != prev_width_type)
5147  {
5148  Oid typoutputfunc;
5149  bool typIsVarlena;
5150 
5151  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5152  fmgr_info(typoutputfunc, &typoutputinfo_width);
5153  prev_width_type = typid;
5154  }
5155 
5156  str = OutputFunctionCall(&typoutputinfo_width, value);
5157 
5158  /* pg_atoi will complain about bad data or overflow */
5159  width = pg_atoi(str, sizeof(int), '\0');
5160 
5161  pfree(str);
5162  }
5163  }
5164 
5165  /* Collect the specified or next argument position */
5166  if (argpos > 0)
5167  arg = argpos;
5168  if (arg >= nargs)
5169  ereport(ERROR,
5170  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5171  errmsg("too few arguments for format()")));
5172 
5173  /* Get the value and type of the selected argument */
5174  if (!funcvariadic)
5175  {
5176  value = PG_GETARG_DATUM(arg);
5177  isNull = PG_ARGISNULL(arg);
5178  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5179  }
5180  else
5181  {
5182  value = elements[arg - 1];
5183  isNull = nulls[arg - 1];
5184  typid = element_type;
5185  }
5186  if (!OidIsValid(typid))
5187  elog(ERROR, "could not determine data type of format() input");
5188 
5189  arg++;
5190 
5191  /*
5192  * Get the appropriate typOutput function, reusing previous one if
5193  * same type as previous argument. That's particularly useful in the
5194  * variadic-array case, but often saves work even for ordinary calls.
5195  */
5196  if (typid != prev_type)
5197  {
5198  Oid typoutputfunc;
5199  bool typIsVarlena;
5200 
5201  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5202  fmgr_info(typoutputfunc, &typoutputfinfo);
5203  prev_type = typid;
5204  }
5205 
5206  /*
5207  * And now we can format the value.
5208  */
5209  switch (*cp)
5210  {
5211  case 's':
5212  case 'I':
5213  case 'L':
5214  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5215  value, isNull,
5216  flags, width);
5217  break;
5218  default:
5219  /* should not get here, because of previous check */
5220  ereport(ERROR,
5221  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5222  errmsg("unrecognized format() type specifier \"%c\"",
5223  *cp),
5224  errhint("For a single \"%%\" use \"%%%%\".")));
5225  break;
5226  }
5227  }
5228 
5229  /* Don't need deconstruct_array results anymore. */
5230  if (elements != NULL)
5231  pfree(elements);
5232  if (nulls != NULL)
5233  pfree(nulls);
5234 
5235  /* Generate results. */
5236  result = cstring_to_text_with_len(str.data, str.len);
5237  pfree(str.data);
5238 
5239  PG_RETURN_TEXT_P(result);
5240 }
signed short int16
Definition: c.h:312
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4963
Definition: fmgr.h:56
int errhint(const char *fmt,...)
Definition: elog.c:987
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2650
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define DatumGetInt32(X)
Definition: postgres.h:457
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:5379
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2025
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2129
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:127
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1995
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define DatumGetInt16(X)
Definition: postgres.h:429
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:367
static struct @131 value
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define Assert(condition)
Definition: c.h:699
#define PG_NARGS()
Definition: fmgr.h:173
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2575
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Definition: c.h:516
#define elog
Definition: elog.h:219
static const char * text_format_parse_format(const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
Definition: varlena.c:5302
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:310
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37

◆ text_format_append_string()

static void text_format_append_string ( StringInfo  buf,
const char *  str,
int  flags,
int  width 
)
static

Definition at line 5428 of file varlena.c.

References appendStringInfoSpaces(), appendStringInfoString(), ereport, errcode(), errmsg(), ERROR, pg_mbstrlen(), and TEXT_FORMAT_FLAG_MINUS.

Referenced by text_format_string_conversion().

5430 {
5431  bool align_to_left = false;
5432  int len;
5433 
5434  /* fast path for typical easy case */
5435  if (width == 0)
5436  {
5438  return;
5439  }
5440 
5441  if (width < 0)
5442  {
5443  /* Negative width: implicit '-' flag, then take absolute value */
5444  align_to_left = true;
5445  /* -INT_MIN is undefined */
5446  if (width <= INT_MIN)
5447  ereport(ERROR,
5448  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5449  errmsg("number is out of range")));
5450  width = -width;
5451  }
5452  else if (flags & TEXT_FORMAT_FLAG_MINUS)
5453  align_to_left = true;
5454 
5455  len = pg_mbstrlen(str);
5456  if (align_to_left)
5457  {
5458  /* left justify */
5460  if (len < width)
5461  appendStringInfoSpaces(buf, width - len);
5462  }
5463  else
5464  {
5465  /* right justify */
5466  if (len < width)
5467  appendStringInfoSpaces(buf, width - len);
5469  }
5470 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:187
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:774
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4961

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 5480 of file varlena.c.

References text_format().

5481 {
5482  return text_format(fcinfo);
5483 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:4976

◆ text_format_parse_digits()

static bool text_format_parse_digits ( const char **  ptr,
const char *  end_ptr,
int *  value 
)
static

Definition at line 5253 of file varlena.c.

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), pg_mul_s32_overflow(), unlikely, and val.

Referenced by text_format_parse_format().

5254 {
5255  bool found = false;
5256  const char *cp = *ptr;
5257  int val = 0;
5258 
5259  while (*cp >= '0' && *cp <= '9')
5260  {
5261  int8 digit = (*cp - '0');
5262 
5263  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5264  unlikely(pg_add_s32_overflow(val, digit, &val)))
5265  ereport(ERROR,
5266  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5267  errmsg("number is out of range")));
5268  ADVANCE_PARSE_POINTER(cp, end_ptr);
5269  found = true;
5270  }
5271 
5272  *ptr = cp;
5273  *value = val;
5274 
5275  return found;
5276 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4963
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:144
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
signed char int8
Definition: c.h:311
static struct @131 value
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:98
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:208
long val
Definition: informix.c:689

◆ text_format_parse_format()

static const char * text_format_parse_format ( const char *  start_ptr,
const char *  end_ptr,
int *  argpos,
int *  widthpos,
int *  flags,
int *  width 
)
static

Definition at line 5302 of file varlena.c.

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, TEXT_FORMAT_FLAG_MINUS, and text_format_parse_digits().

Referenced by text_format().

5305 {
5306  const char *cp = start_ptr;
5307  int n;
5308 
5309  /* set defaults for output parameters */
5310  *argpos = -1;
5311  *widthpos = -1;
5312  *flags = 0;
5313  *width = 0;
5314 
5315  /* try to identify first number */
5316  if (text_format_parse_digits(&cp, end_ptr, &n))
5317  {
5318  if (*cp != '$')
5319  {
5320  /* Must be just a width and a type, so we're done */
5321  *width = n;
5322  return cp;
5323  }
5324  /* The number was argument position */
5325  *argpos = n;
5326  /* Explicit 0 for argument index is immediately refused */
5327  if (n == 0)
5328  ereport(ERROR,
5329  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5330  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5331  ADVANCE_PARSE_POINTER(cp, end_ptr);
5332  }
5333 
5334  /* Handle flags (only minus is supported now) */
5335  while (*cp == '-')
5336  {
5337  *flags |= TEXT_FORMAT_FLAG_MINUS;
5338  ADVANCE_PARSE_POINTER(cp, end_ptr);
5339  }
5340 
5341  if (*cp == '*')
5342  {
5343  /* Handle indirect width */
5344  ADVANCE_PARSE_POINTER(cp, end_ptr);
5345  if (text_format_parse_digits(&cp, end_ptr, &n))
5346  {
5347  /* number in this position must be closed by $ */
5348  if (*cp != '$')
5349  ereport(ERROR,
5350  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5351  errmsg("width argument position must be ended by \"$\"")));
5352  /* The number was width argument position */
5353  *widthpos = n;
5354  /* Explicit 0 for argument index is immediately refused */
5355  if (n == 0)
5356  ereport(ERROR,
5357  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5358  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5359  ADVANCE_PARSE_POINTER(cp, end_ptr);
5360  }
5361  else
5362  *widthpos = 0; /* width's argument position is unspecified */
5363  }
5364  else
5365  {
5366  /* Check for direct width specification */
5367  if (text_format_parse_digits(&cp, end_ptr, &n))
5368  *width = n;
5369  }
5370 
5371  /* cp should now be pointing at type character */
5372  return cp;
5373 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4963
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5253
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4961

◆ text_format_string_conversion()

static void text_format_string_conversion ( StringInfo  buf,
char  conversion,
FmgrInfo typOutputInfo,
Datum  value,
bool  isNull,
int  flags,
int  width 
)
static

Definition at line 5379 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, OutputFunctionCall(), pfree(), quote_identifier(), quote_literal_cstr(), generate_unaccent_rules::str, and text_format_append_string().

Referenced by text_format().

5383 {
5384  char *str;
5385 
5386  /* Handle NULL arguments before trying to stringify the value. */
5387  if (isNull)
5388  {
5389  if (conversion == 's')
5390  text_format_append_string(buf, "", flags, width);
5391  else if (conversion == 'L')
5392  text_format_append_string(buf, "NULL", flags, width);
5393  else if (conversion == 'I')
5394  ereport(ERROR,
5395  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5396  errmsg("null values cannot be formatted as an SQL identifier")));
5397  return;
5398  }
5399 
5400  /* Stringify. */
5401  str = OutputFunctionCall(typOutputInfo, value);
5402 
5403  /* Escape. */
5404  if (conversion == 'I')
5405  {
5406  /* quote_identifier may or may not allocate a new string. */
5407  text_format_append_string(buf, quote_identifier(str), flags, width);
5408  }
5409  else if (conversion == 'L')
5410  {
5411  char *qstr = quote_literal_cstr(str);
5412 
5413  text_format_append_string(buf, qstr, flags, width);
5414  /* quote_literal_cstr() always allocates a new string */
5415  pfree(qstr);
5416  }
5417  else
5418  text_format_append_string(buf, str, flags, width);
5419 
5420  /* Cleanup. */
5421  pfree(str);
5422 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10488
int errcode(int sqlerrcode)
Definition: elog.c:575
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:5428
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static struct @131 value
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1750 of file varlena.c.

References PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textge().

1751 {
1752  text *arg1 = PG_GETARG_TEXT_PP(0);
1753  text *arg2 = PG_GETARG_TEXT_PP(1);
1754  bool result;
1755 
1756  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1757 
1758  PG_FREE_IF_COPY(arg1, 0);
1759  PG_FREE_IF_COPY(arg2, 1);
1760 
1761  PG_RETURN_BOOL(result);
1762 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1735 of file varlena.c.

References PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textgt().

1736 {
1737  text *arg1 = PG_GETARG_TEXT_PP(0);
1738  text *arg2 = PG_GETARG_TEXT_PP(1);
1739  bool result;
1740 
1741  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1742 
1743  PG_FREE_IF_COPY(arg1, 0);
1744  PG_FREE_IF_COPY(arg2, 1);
1745 
1746  PG_RETURN_BOOL(result);
1747 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_isequal()

static bool text_isequal ( text txt1,
text txt2 
)
static

Definition at line 4170 of file varlena.c.

References DatumGetBool, DirectFunctionCall2, PointerGetDatum, and texteq().

Referenced by text_to_array_internal().

4171 {
4173  PointerGetDatum(txt1),
4174  PointerGetDatum(txt2)));
4175 }
#define PointerGetDatum(X)
Definition: postgres.h:541
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1641
#define DatumGetBool(X)
Definition: postgres.h:378
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2618 of file varlena.c.

References PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_cmp().

2619 {
2620  text *arg1 = PG_GETARG_TEXT_PP(0);
2621  text *arg2 = PG_GETARG_TEXT_PP(1);
2622  text *result;
2623 
2624  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2625 
2626  PG_RETURN_TEXT_P(result);
2627 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
Definition: c.h:516

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1720 of file varlena.c.

References PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textle().

1721 {
1722  text *arg1 = PG_GETARG_TEXT_PP(0);
1723  text *arg2 = PG_GETARG_TEXT_PP(1);
1724  bool result;
1725 
1726  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1727 
1728  PG_FREE_IF_COPY(arg1, 0);
1729  PG_FREE_IF_COPY(arg2, 1);
1730 
1731  PG_RETURN_BOOL(result);
1732 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 4880 of file varlena.c.

References cstring_to_text_with_len(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, generate_unaccent_rules::str, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4881 {
4882  text *str = PG_GETARG_TEXT_PP(0);
4883  const char *p = VARDATA_ANY(str);
4884  int len = VARSIZE_ANY_EXHDR(str);
4885  int n = PG_GETARG_INT32(1);
4886  int rlen;
4887 
4888  if (n < 0)
4889  n = pg_mbstrlen_with_len(p, len) + n;
4890  rlen = pg_mbcharcliplen(p, len, n);
4891 
4893 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 640 of file varlena.c.

References DatumGetTextPP, pg_database_encoding_max_length(), pg_mbstrlen_with_len(), PG_RETURN_INT32, toast_raw_datum_size(), VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by textlen(), and textoverlay_no_len().

641 {
642  /* fastpath when max encoding length is one */
645  else
646  {
647  text *t = DatumGetTextPP(str);
648 
650  VARSIZE_ANY_EXHDR(t)));
651  }
652 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARHDRSZ
Definition: c.h:522
#define DatumGetTextPP(X)
Definition: fmgr.h:261
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1705 of file varlena.c.

References PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textlt().

1706 {
1707  text *arg1 = PG_GETARG_TEXT_PP(0);
1708  text *arg2 = PG_GETARG_TEXT_PP(1);
1709  bool result;
1710 
1711  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1712 
1713  PG_FREE_IF_COPY(arg1, 0);
1714  PG_FREE_IF_COPY(arg2, 1);
1715 
1716  PG_RETURN_BOOL(result);
1717 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3192 of file varlena.c.

References NAMEDATALEN, NameStr, palloc0(), PG_GETARG_TEXT_PP, pg_mbcliplen(), PG_RETURN_NAME, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3193 {
3194  text *s = PG_GETARG_TEXT_PP(0);
3195  Name result;
3196  int len;
3197 
3198  len = VARSIZE_ANY_EXHDR(s);
3199 
3200  /* Truncate oversize input */
3201  if (len >= NAMEDATALEN)
3202  len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3203 
3204  /* We use palloc0 here to ensure result is zero-padded */
3205  result = (Name) palloc0(NAMEDATALEN);
3206  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3207 
3208  PG_RETURN_NAME(result);
3209 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:820
Definition: c.h:570
void * palloc0(Size size)
Definition: mcxt.c:955
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:576
Definition: c.h:516
NameData * Name
Definition: c.h:574
#define PG_RETURN_NAME(x)
Definition: fmgr.h:328

◆ text_overlay()

static text * text_overlay ( text t1,
text t2,
int  sp,
int  sl 
)
static

Definition at line 1034 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), PointerGetDatum, s1, s2, text_catenate(), and text_substring().

Referenced by textoverlay(), and textoverlay_no_len().

1035 {
1036  text *result;
1037  text *s1;
1038  text *s2;
1039  int sp_pl_sl;
1040 
1041  /*
1042  * Check for possible integer-overflow cases. For negative sp, throw a
1043  * "substring length" error because that's what should be expected
1044  * according to the spec's definition of OVERLAY().
1045  */
1046  if (sp <= 0)
1047  ereport(ERROR,
1048  (errcode(ERRCODE_SUBSTRING_ERROR),
1049  errmsg("negative substring length not allowed")));
1050  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1051  ereport(ERROR,
1052  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1053  errmsg("integer out of range")));
1054 
1055  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1056  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1057  result = text_catenate(s1, t2);
1058  result = text_catenate(result, s2);
1059 
1060  return result;
1061 }
#define PointerGetDatum(X)
Definition: postgres.h:541
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
char * s1
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:694
#define ereport(elevel, rest)
Definition: elog.h:122
char * s2
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:98
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 2705 of file varlena.c.

References internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

2706 {
2707  text *arg1 = PG_GETARG_TEXT_PP(0);
2708  text *arg2 = PG_GETARG_TEXT_PP(1);
2709  int result;
2710 
2711  result = internal_text_pattern_compare(arg1, arg2);
2712 
2713  PG_FREE_IF_COPY(arg1, 0);
2714  PG_FREE_IF_COPY(arg2, 1);
2715 
2716  PG_RETURN_BOOL(result >= 0);
2717 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2651
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_pattern_gt()

Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 2721 of file varlena.c.

References internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

2722 {
2723  text *arg1 = PG_GETARG_TEXT_PP(0);
2724  text *arg2 = PG_GETARG_TEXT_PP(1);
2725  int result;
2726 
2727  result = internal_text_pattern_compare(arg1, arg2);
2728 
2729  PG_FREE_IF_COPY(arg1, 0);
2730  PG_FREE_IF_COPY(arg2, 1);
2731 
2732  PG_RETURN_BOOL(result > 0);
2733 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2651
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_pattern_le()

Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 2689 of file varlena.c.

References internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

2690 {
2691  text *arg1 = PG_GETARG_TEXT_PP(0);
2692  text *arg2 = PG_GETARG_TEXT_PP(1);
2693  int result;
2694 
2695  result = internal_text_pattern_compare(arg1, arg2);
2696 
2697  PG_FREE_IF_COPY(arg1, 0);
2698  PG_FREE_IF_COPY(arg2, 1);
2699 
2700  PG_RETURN_BOOL(result <= 0);
2701 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2651
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_pattern_lt()

Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 2673 of file varlena.c.

References internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

2674 {
2675  text *arg1 = PG_GETARG_TEXT_PP(0);
2676  text *arg2 = PG_GETARG_TEXT_PP(1);
2677  int result;
2678 
2679  result = internal_text_pattern_compare(arg1, arg2);
2680 
2681  PG_FREE_IF_COPY(arg1, 0);
2682  PG_FREE_IF_COPY(arg2, 1);
2683 
2684  PG_RETURN_BOOL(result < 0);
2685 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2651
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
Definition: c.h:516

◆ text_position()

static int text_position ( text t1,
text t2 
)
static

Definition at line 1094 of file varlena.c.

References text_position_cleanup(), text_position_next(), and text_position_setup().

Referenced by textpos().

1095 {
1097  int result;
1098 
1099  text_position_setup(t1, t2, &state);
1100  result = text_position_next(1, &state);
1101  text_position_cleanup(&state);
1102  return result;
1103 }
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1118
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1230
ts_parserstate state
Definition: tsquery.c:81
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1364

◆ text_position_cleanup()

static void text_position_cleanup ( TextPositionState state)
static

Definition at line 1364 of file varlena.c.

References pfree(), TextPositionState::use_wchar, TextPositionState::wstr1, and TextPositionState::wstr2.

Referenced by replace_text(), split_text(), text_position(), and text_to_array_internal().

1365 {
1366  if (state->use_wchar)
1367  {
1368  pfree(state->wstr1);
1369  pfree(state->wstr2);
1370  }
1371 }
pg_wchar * wstr2
Definition: varlena.c:52
void pfree(void *pointer)
Definition: mcxt.c:1031
pg_wchar * wstr1
Definition: varlena.c:51

◆ text_position_next()

static int text_position_next ( int  start_pos,
TextPositionState state 
)
static

Definition at line 1230 of file varlena.c.

References Assert, TextPositionState::len1, TextPositionState::len2, TextPositionState::skiptable, TextPositionState::skiptablemask, TextPositionState::str1, TextPositionState::str2, TextPositionState::use_wchar, TextPositionState::wstr1, and TextPositionState::wstr2.

Referenced by replace_text(), split_text(), text_position(), and text_to_array_internal().

1231 {
1232  int haystack_len = state->len1;
1233  int needle_len = state->len2;
1234  int skiptablemask = state->skiptablemask;
1235 
1236  Assert(start_pos > 0); /* else caller error */
1237 
1238  if (needle_len <= 0)
1239  return start_pos; /* result for empty pattern */
1240 
1241  start_pos--; /* adjust for zero based arrays */
1242 
1243  /* Done if the needle can't possibly fit */
1244  if (haystack_len < start_pos + needle_len)
1245  return 0;
1246 
1247  if (!state->use_wchar)
1248  {
1249  /* simple case - single byte encoding */
1250  const char *haystack = state->str1;
1251  const char *needle = state->str2;
1252  const char *haystack_end = &haystack[haystack_len];
1253  const char *hptr;
1254 
1255  if (needle_len == 1)
1256  {
1257  /* No point in using B-M-H for a one-character needle */
1258  char nchar = *needle;
1259 
1260  hptr = &haystack[start_pos];
1261  while (hptr < haystack_end)
1262  {
1263  if (*hptr == nchar)
1264  return hptr - haystack + 1;
1265  hptr++;
1266  }
1267  }
1268  else
1269  {
1270  const char *needle_last = &needle[needle_len - 1];
1271 
1272  /* Start at startpos plus the length of the needle */
1273  hptr = &haystack[start_pos + needle_len - 1];
1274  while (hptr < haystack_end)
1275  {
1276  /* Match the needle scanning *backward* */
1277  const char *nptr;
1278  const char *p;
1279 
1280  nptr = needle_last;
1281  p = hptr;
1282  while (*nptr == *p)
1283  {
1284  /* Matched it all? If so, return 1-based position */
1285  if (nptr == needle)
1286  return p - haystack + 1;
1287  nptr--, p--;
1288  }
1289 
1290  /*
1291  * No match, so use the haystack char at hptr to decide how
1292  * far to advance. If the needle had any occurrence of that
1293  * character (or more precisely, one sharing the same
1294  * skiptable entry) before its last character, then we advance
1295  * far enough to align the last such needle character with
1296  * that haystack position. Otherwise we can advance by the
1297  * whole needle length.
1298  */
1299  hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1300  }
1301  }
1302  }
1303  else
1304  {
1305  /* The multibyte char version. This works exactly the same way. */
1306  const pg_wchar *haystack = state->wstr1;
1307  const pg_wchar *needle = state->wstr2;
1308  const pg_wchar *haystack_end = &haystack[haystack_len];
1309  const pg_wchar *hptr;
1310 
1311  if (needle_len == 1)
1312  {
1313  /* No point in using B-M-H for a one-character needle */
1314  pg_wchar nchar = *needle;
1315 
1316  hptr = &haystack[start_pos];
1317  while (hptr < haystack_end)
1318  {
1319  if (*hptr == nchar)
1320  return hptr - haystack + 1;
1321  hptr++;
1322  }
1323  }
1324  else
1325  {
1326  const pg_wchar *needle_last = &needle[needle_len - 1];
1327 
1328  /* Start at startpos plus the length of the needle */
1329  hptr = &haystack[start_pos + needle_len - 1];
1330  while (hptr < haystack_end)
1331  {
1332  /* Match the needle scanning *backward* */
1333  const pg_wchar *nptr;
1334  const pg_wchar *p;
1335 
1336  nptr = needle_last;
1337  p = hptr;
1338  while (*nptr == *p)
1339  {
1340  /* Matched it all? If so, return 1-based position */
1341  if (nptr == needle)
1342  return p - haystack + 1;
1343  nptr--, p--;
1344  }
1345 
1346  /*
1347  * No match, so use the haystack char at hptr to decide how
1348  * far to advance. If the needle had any occurrence of that
1349  * character (or more precisely, one sharing the same
1350  * skiptable entry) before its last character, then we advance
1351  * far enough to align the last such needle character with
1352  * that haystack position. Otherwise we can advance by the
1353  * whole needle length.
1354  */
1355  hptr += state->skiptable[*hptr & skiptablemask];
1356  }
1357  }
1358  }
1359 
1360  return 0; /* not found */
1361 }
pg_wchar * wstr2
Definition: varlena.c:52
unsigned int pg_wchar
Definition: mbprint.c:31
int skiptable[256]
Definition: varlena.c:57
#define Assert(condition)
Definition: c.h:699
pg_wchar * wstr1
Definition: varlena.c:51

◆ text_position_setup()

static void text_position_setup ( text t1,
text t2,
TextPositionState state 
)
static

Definition at line 1118 of file varlena.c.

References i, TextPositionState::len1, TextPositionState::len2, palloc(), pg_database_encoding_max_length(), pg_mb2wchar_with_len(), TextPositionState::skiptable, TextPositionState::skiptablemask, TextPositionState::str1, TextPositionState::str2, TextPositionState::use_wchar, VARDATA_ANY, VARSIZE_ANY_EXHDR, TextPositionState::wstr1, and TextPositionState::wstr2.

Referenced by replace_text(), split_text(), text_position(), and text_to_array_internal().

1119 {
1120  int len1 = VARSIZE_ANY_EXHDR(t1);
1121  int len2 = VARSIZE_ANY_EXHDR(t2);
1122 
1124  {
1125  /* simple case - single byte encoding */
1126  state->use_wchar = false;
1127  state->str1 = VARDATA_ANY(t1);
1128  state->str2 = VARDATA_ANY(t2);
1129  state->len1 = len1;
1130  state->len2 = len2;
1131  }
1132  else
1133  {
1134  /* not as simple - multibyte encoding */
1135  pg_wchar *p1,
1136  *p2;
1137 
1138  p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));
1139  len1 = pg_mb2wchar_with_len(VARDATA_ANY(t1), p1, len1);
1140  p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));
1141  len2 = pg_mb2wchar_with_len(VARDATA_ANY(t2), p2, len2);
1142 
1143  state->use_wchar = true;
1144  state->wstr1 = p1;
1145  state->wstr2 = p2;
1146  state->len1 = len1;
1147  state->len2 = len2;
1148  }
1149 
1150  /*
1151  * Prepare the skip table for Boyer-Moore-Horspool searching. In these
1152  * notes we use the terminology that the "haystack" is the string to be
1153  * searched (t1) and the "needle" is the pattern being sought (t2).
1154  *
1155  * If the needle is empty or bigger than the haystack then there is no
1156  * point in wasting cycles initializing the table. We also choose not to
1157  * use B-M-H for needles of length 1, since the skip table can't possibly
1158  * save anything in that case.
1159  */
1160  if (len1 >= len2 && len2 > 1)
1161  {
1162  int searchlength = len1 - len2;
1163  int skiptablemask;
1164  int last;
1165  int i;
1166 
1167  /*
1168  * First we must determine how much of the skip table to use. The
1169  * declaration of TextPositionState allows up to 256 elements, but for
1170  * short search problems we don't really want to have to initialize so
1171  * many elements --- it would take too long in comparison to the
1172  * actual search time. So we choose a useful skip table size based on
1173  * the haystack length minus the needle length. The closer the needle
1174  * length is to the haystack length the less useful skipping becomes.
1175  *
1176  * Note: since we use bit-masking to select table elements, the skip
1177  * table size MUST be a power of 2, and so the mask must be 2^N-1.
1178  */
1179  if (searchlength < 16)
1180  skiptablemask = 3;
1181  else if (searchlength < 64)
1182  skiptablemask = 7;
1183  else if (searchlength < 128)
1184  skiptablemask = 15;
1185  else if (searchlength < 512)
1186  skiptablemask = 31;
1187  else if (searchlength < 2048)
1188  skiptablemask = 63;
1189  else if (searchlength < 4096)
1190  skiptablemask = 127;
1191  else
1192  skiptablemask = 255;
1193  state->skiptablemask = skiptablemask;
1194 
1195  /*
1196  * Initialize the skip table. We set all elements to the needle
1197  * length, since this is the correct skip distance for any character
1198  * not found in the needle.
1199  */
1200  for (i = 0; i <= skiptablemask; i++)
1201  state->skiptable[i] = len2;
1202 
1203  /*
1204  * Now examine the needle. For each character except the last one,
1205  * set the corresponding table element to the appropriate skip
1206  * distance. Note that when two characters share the same skip table
1207  * entry, the one later in the needle must determine the skip
1208  * distance.
1209  */
1210  last = len2 - 1;
1211 
1212  if (!state->use_wchar)
1213  {
1214  const char *str2 = state->str2;
1215 
1216  for (i = 0; i < last; i++)
1217  state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1218  }
1219  else
1220  {
1221  const pg_wchar *wstr2 = state->wstr2;
1222 
1223  for (i = 0; i < last; i++)
1224  state->skiptable[wstr2[i] & skiptablemask] = last - i;
1225  }
1226  }
1227 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
pg_wchar * wstr2
Definition: varlena.c:52
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
unsigned int pg_wchar
Definition: mbprint.c:31
int skiptable[256]
Definition: varlena.c:57
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:723
pg_wchar * wstr1
Definition: varlena.c:51
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int i

◆ text_reverse()

Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 4921 of file varlena.c.

References palloc(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, pg_mblen(), PG_RETURN_TEXT_P, SET_VARSIZE, generate_unaccent_rules::str, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

4922 {
4923  text *str = PG_GETARG_TEXT_PP(0);
4924  const char *p = VARDATA_ANY(str);
4925  int len = VARSIZE_ANY_EXHDR(str);
4926  const char *endp = p + len;
4927  text *result;
4928  char *dst;
4929 
4930  result = palloc(len + VARHDRSZ);
4931  dst = (char *) VARDATA(result) + len;
4932  SET_VARSIZE(result, len + VARHDRSZ);
4933 
4935  {
4936  /* multibyte version */
4937  while (p < endp)
4938  {
4939  int sz;
4940 
4941  sz = pg_mblen(p);
4942  dst -= sz;
4943  memcpy(dst, p, sz);
4944  p += sz;
4945  }
4946  }
4947  else
4948  {
4949  /* single byte version */
4950  while (p < endp)
4951  *(--dst) = *p++;
4952  }
4953 
4954  PG_RETURN_TEXT_P(result);
4955 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
Definition: c.h:516
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ text_right()

Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 4900 of file varlena.c.

References cstring_to_text_with_len(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, generate_unaccent_rules::str, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4901 {
4902  text *str = PG_GETARG_TEXT_PP(0);
4903  const char *p = VARDATA_ANY(str);
4904  int len = VARSIZE_ANY_EXHDR(str);
4905  int n = PG_GETARG_INT32(1);
4906  int off;
4907 
4908  if (n < 0)
4909  n = -n;
4910  else
4911  n = pg_mbstrlen_with_len(p, len) - n;
4912  off = pg_mbcharcliplen(p, len, n);
4913 
4914  PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
4915 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ text_smaller()

Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2630 of file varlena.c.

References PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_cmp().

2631 {
2632  text *arg1 = PG_GETARG_TEXT_PP(0);
2633  text *arg2 = PG_GETARG_TEXT_PP(1);
2634  text *result;
2635 
2636  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2637 
2638  PG_RETURN_TEXT_P(result);
2639 }
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1616
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
Definition: c.h:516

◆ text_starts_with()

Datum text_starts_with ( PG_FUNCTION_ARGS  )

Definition at line 1765 of file varlena.c.

References DatumGetTextPP, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by spg_text_leaf_consistent().

1766 {
1767  Datum arg1 = PG_GETARG_DATUM(0);
1768  Datum arg2 = PG_GETARG_DATUM(1);
1769  bool result;
1770  Size len1,
1771  len2;
1772 
1773  len1 = toast_raw_datum_size(arg1);
1774  len2 = toast_raw_datum_size(arg2);
1775  if (len2 > len1)
1776  result = false;
1777  else
1778  {
1779  text *targ1 = DatumGetTextPP(arg1);
1780  text *targ2 = DatumGetTextPP(arg2);
1781 
1782  result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1783  VARSIZE_ANY_EXHDR(targ2)) == 0);
1784 
1785  PG_FREE_IF_COPY(targ1, 0);
1786  PG_FREE_IF_COPY(targ2, 1);
1787  }
1788 
1789  PG_RETURN_BOOL(result);
1790 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define DatumGetTextPP(X)
Definition: fmgr.h:261
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:433
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ text_substr()

Datum text_substr ( PG_FUNCTION_ARGS  )

Definition at line 781 of file varlena.c.

References PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and text_substring().

Referenced by build_regexp_match_result(), build_regexp_split_result(), and textregexsubstr().

782 {
784  PG_GETARG_INT32(1),
785  PG_GETARG_INT32(2),
786  false));
787 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336

◆ text_substr_no_len()

Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 795 of file varlena.c.

References PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and text_substring().

Referenced by build_regexp_split_result().

796 {
798  PG_GETARG_INT32(1),
799  -1, true));
800 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336

◆ text_substring()

static text * text_substring ( Datum  str,
int32  start,
int32  length,
bool  length_not_specified 
)
static

Definition at line 814 of file varlena.c.

References cstring_to_text(), DatumGetPointer, DatumGetTextPSlice, elog, ereport, errcode(), errmsg(), ERROR, i, length(), Max, Min, palloc(), pfree(), pg_database_encoding_max_length(), pg_mblen(), pg_mbstrlen_with_len(), S, SET_VARSIZE, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by split_text(), text_overlay(), text_substr(), and text_substr_no_len().

815 {
817  int32 S = start; /* start position */
818  int32 S1; /* adjusted start position */
819  int32 L1; /* adjusted substring length */
820 
821  /* life is easy if the encoding max length is 1 */
822  if (eml == 1)
823  {
824  S1 = Max(S, 1);
825 
826  if (length_not_specified) /* special case - get length to end of
827  * string */
828  L1 = -1;
829  else
830  {
831  /* end position */
832  int E = S + length;
833 
834  /*
835  * A negative value for L is the only way for the end position to
836  * be before the start. SQL99 says to throw an error.
837  */
838  if (E < S)
839  ereport(ERROR,
840  (errcode(ERRCODE_SUBSTRING_ERROR),
841  errmsg("negative substring length not allowed")));
842 
843  /*
844  * A zero or negative value for the end position can happen if the
845  * start was negative or one. SQL99 says to return a zero-length
846  * string.
847  */
848  if (E < 1)
849  return cstring_to_text("");
850 
851  L1 = E - S1;
852  }
853 
854  /*
855  * If the start position is past the end of the string, SQL99 says to
856  * return a zero-length string -- PG_GETARG_TEXT_P_SLICE() will do
857  * that for us. Convert to zero-based starting position
858  */
859  return DatumGetTextPSlice(str, S1 - 1, L1);
860  }
861  else if (eml > 1)
862  {
863  /*
864  * When encoding max length is > 1, we can't get LC without
865  * detoasting, so we'll grab a conservatively large slice now and go
866  * back later to do the right thing
867  */
868  int32 slice_start;
869  int32 slice_size;
870  int32 slice_strlen;
871  text *slice;
872  int32 E1;
873  int32 i;
874  char *p;
875  char *s;
876  text *ret;
877 
878  /*
879  * if S is past the end of the string, the tuple toaster will return a
880  * zero-length string to us
881  */
882  S1 = Max(S, 1);
883 
884  /*
885  * We need to start at position zero because there is no way to know
886  * in advance which byte offset corresponds to the supplied start
887  * position.
888  */
889  slice_start = 0;
890 
891  if (length_not_specified) /* special case - get length to end of
892  * string */
893  slice_size = L1 = -1;
894  else
895  {
896  int E = S + length;
897 
898  /*
899  * A negative value for L is the only way for the end position to
900  * be before the start. SQL99 says to throw an error.
901  */
902  if (E < S)
903  ereport(ERROR,
904  (errcode(ERRCODE_SUBSTRING_ERROR),
905  errmsg("negative substring length not allowed")));
906 
907  /*
908  * A zero or negative value for the end position can happen if the
909  * start was negative or one. SQL99 says to return a zero-length
910  * string.
911  */
912  if (E < 1)
913  return cstring_to_text("");
914 
915  /*
916  * if E is past the end of the string, the tuple toaster will
917  * truncate the length for us
918  */
919  L1 = E - S1;
920 
921  /*
922  * Total slice size in bytes can't be any longer than the start
923  * position plus substring length times the encoding max length.
924  */
925  slice_size = (S1 + L1) * eml;
926  }
927 
928  /*
929  * If we're working with an untoasted source, no need to do an extra
930  * copying step.
931  */
934  slice = DatumGetTextPSlice(str, slice_start, slice_size);
935  else
936  slice = (text *) DatumGetPointer(str);
937 
938  /* see if we got back an empty string */
939  if (VARSIZE_ANY_EXHDR(slice) == 0)
940  {
941  if (slice != (text *) DatumGetPointer(str))
942  pfree(slice);
943  return cstring_to_text("");
944  }
945 
946  /* Now we can get the actual length of the slice in MB characters */
947  slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
948  VARSIZE_ANY_EXHDR(slice));
949 
950  /*
951  * Check that the start position wasn't > slice_strlen. If so, SQL99
952  * says to return a zero-length string.
953  */
954  if (S1 > slice_strlen)
955  {
956  if (slice != (text *) DatumGetPointer(str))
957  pfree(slice);
958  return cstring_to_text("");
959  }
960 
961  /*
962  * Adjust L1 and E1 now that we know the slice string length. Again
963  * remember that S1 is one based, and slice_start is zero based.
964  */
965  if (L1 > -1)
966  E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
967  else
968  E1 = slice_start + 1 + slice_strlen;
969 
970  /*
971  * Find the start position in the slice; remember S1 is not zero based
972  */
973  p = VARDATA_ANY(slice);
974  for (i = 0; i < S1 - 1; i++)
975  p += pg_mblen(p);
976 
977  /* hang onto a pointer to our start position */
978  s = p;
979 
980  /*
981  * Count the actual bytes used by the substring of the requested
982  * length.
983  */
984  for (i = S1; i < E1; i++)
985  p += pg_mblen(p);
986 
987  ret = (text *) palloc(VARHDRSZ + (p - s));
988  SET_VARSIZE(ret, VARHDRSZ + (p - s));
989  memcpy(VARDATA(ret), s, (p - s));
990 
991  if (slice != (text *) DatumGetPointer(str))
992  pfree(slice);
993 
994  return ret;
995  }
996  else
997  elog(ERROR, "invalid backend encoding: encoding max length < 1");
998 
999  /* not reached: suppress compiler warning */
1000  return NULL;
1001 }
int length(const List *list)
Definition: list.c:1333
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define DatumGetTextPSlice(X, m, n)
Definition: fmgr.h:273
#define VARHDRSZ
Definition: c.h:522
#define Min(x, y)
Definition: c.h:857
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
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 S(n, x)
Definition: sha1.c:55
#define ereport(elevel, rest)
Definition: elog.h:122
#define Max(x, y)
Definition: c.h:851
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define DatumGetPointer(X)
Definition: postgres.h:534
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924