PostgreSQL Source Code  git master
varlena.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "access/detoast.h"
#include "access/toast_compression.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/int.h"
#include "common/unicode_norm.h"
#include "funcapi.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "parser/scansup.h"
#include "port/pg_bswap.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/guc.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
 
struct  SplitTextOutputData
 

Macros

#define TEXTBUFLEN   1024
 
#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 CmpCall(cmpfunc)
 
#define PG_STR_GET_BYTEA(str_)    DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#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 VarString
 

Functions

static int varstrfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int bpcharfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int varlenafastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int varstrfastcmp_locale (char *a1p, int len1, char *a2p, int len2, 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, Oid collid)
 
static void text_position_setup (text *t1, text *t2, Oid collid, TextPositionState *state)
 
static bool text_position_next (TextPositionState *state)
 
static char * text_position_next_internal (char *start_ptr, TextPositionState *state)
 
static char * text_position_get_match_ptr (TextPositionState *state)
 
static int text_position_get_match_pos (TextPositionState *state)
 
static void text_position_cleanup (TextPositionState *state)
 
static void check_collation_set (Oid collid)
 
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 bool split_text (FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
 
static void split_text_accum_result (SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
 
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)
 
static void text_position_reset (TextPositionState *state)
 
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 typid, Oid collid)
 
Datum btvarstrequalimage (PG_FUNCTION_ARGS)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
Datum nameeqtext (PG_FUNCTION_ARGS)
 
Datum texteqname (PG_FUNCTION_ARGS)
 
Datum namenetext (PG_FUNCTION_ARGS)
 
Datum textnename (PG_FUNCTION_ARGS)
 
Datum btnametextcmp (PG_FUNCTION_ARGS)
 
Datum bttextnamecmp (PG_FUNCTION_ARGS)
 
Datum namelttext (PG_FUNCTION_ARGS)
 
Datum nameletext (PG_FUNCTION_ARGS)
 
Datum namegttext (PG_FUNCTION_ARGS)
 
Datum namegetext (PG_FUNCTION_ARGS)
 
Datum textltname (PG_FUNCTION_ARGS)
 
Datum textlename (PG_FUNCTION_ARGS)
 
Datum textgtname (PG_FUNCTION_ARGS)
 
Datum textgename (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 bytea_bit_count (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)
 
bool SplitGUCList (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 int check_replace_text_has_escape (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, text *pattern_text, text *replace_text, int cflags, Oid collation, int search_start, int n)
 
Datum split_part (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2, Oid collid)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_null (PG_FUNCTION_ARGS)
 
Datum text_to_table (PG_FUNCTION_ARGS)
 
Datum text_to_table_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 pg_column_compression (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_combine (PG_FUNCTION_ARGS)
 
Datum string_agg_serialize (PG_FUNCTION_ARGS)
 
Datum string_agg_deserialize (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)
 
void initClosestMatch (ClosestMatchState *state, const char *source, int max_d)
 
void updateClosestMatch (ClosestMatchState *state, const char *candidate)
 
const char * getClosestMatch (ClosestMatchState *state)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 
static bool isxdigits_n (const char *instr, size_t n)
 
static unsigned int hexval (unsigned char c)
 
static unsigned int hexval_n (const char *instr, size_t n)
 
Datum unistr (PG_FUNCTION_ARGS)
 

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:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39

Definition at line 5551 of file varlena.c.

◆ CmpCall

#define CmpCall (   cmpfunc)
Value:
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:784
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GET_COLLATION()
Definition: fmgr.h:198
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202

Definition at line 2729 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 114 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 115 of file varlena.c.

◆ DIG

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

Definition at line 274 of file varlena.c.

◆ HEXBASE

#define HEXBASE   16

Definition at line 4924 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6091 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 2984 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

Definition at line 5549 of file varlena.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 112 of file varlena.c.

◆ VAL

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

Definition at line 273 of file varlena.c.

Typedef Documentation

◆ VarString

typedef struct varlena VarString

Definition at line 46 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 4104 of file varlena.c.

4107 {
4108  const char *p = VARDATA_ANY(replace_text);
4109  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4110 
4111  while (p < p_end)
4112  {
4113  const char *chunk_start = p;
4114  int so;
4115  int eo;
4116 
4117  /* Find next escape char, if any. */
4118  p = memchr(p, '\\', p_end - p);
4119  if (p == NULL)
4120  p = p_end;
4121 
4122  /* Copy the text we just scanned over, if any. */
4123  if (p > chunk_start)
4124  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4125 
4126  /* Done if at end of string, else advance over escape char. */
4127  if (p >= p_end)
4128  break;
4129  p++;
4130 
4131  if (p >= p_end)
4132  {
4133  /* Escape at very end of input. Treat same as unexpected char */
4134  appendStringInfoChar(str, '\\');
4135  break;
4136  }
4137 
4138  if (*p >= '1' && *p <= '9')
4139  {
4140  /* Use the back reference of regexp. */
4141  int idx = *p - '0';
4142 
4143  so = pmatch[idx].rm_so;
4144  eo = pmatch[idx].rm_eo;
4145  p++;
4146  }
4147  else if (*p == '&')
4148  {
4149  /* Use the entire matched string. */
4150  so = pmatch[0].rm_so;
4151  eo = pmatch[0].rm_eo;
4152  p++;
4153  }
4154  else if (*p == '\\')
4155  {
4156  /* \\ means transfer one \ to output. */
4157  appendStringInfoChar(str, '\\');
4158  p++;
4159  continue;
4160  }
4161  else
4162  {
4163  /*
4164  * If escape char is not followed by any expected char, just treat
4165  * it as ordinary data to copy. (XXX would it be better to throw
4166  * an error?)
4167  */
4168  appendStringInfoChar(str, '\\');
4169  continue;
4170  }
4171 
4172  if (so >= 0 && eo >= 0)
4173  {
4174  /*
4175  * Copy the text that is back reference of regexp. Note so and eo
4176  * are counted in characters not bytes.
4177  */
4178  char *chunk_start;
4179  int chunk_len;
4180 
4181  Assert(so >= data_pos);
4182  chunk_start = start_ptr;
4183  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4184  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4185  appendBinaryStringInfo(str, chunk_start, chunk_len);
4186  }
4187  }
4188 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Assert(fmt[strlen(fmt) - 1] !='\n')
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:227
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
regoff_t rm_eo
Definition: regex.h:86
regoff_t rm_so
Definition: regex.h:85
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:804
Datum replace_text(PG_FUNCTION_ARGS)
Definition: varlena.c:3994

References appendBinaryStringInfo(), appendStringInfoChar(), Assert(), charlen_to_bytelen(), idx(), replace_text(), regmatch_t::rm_eo, regmatch_t::rm_so, generate_unaccent_rules::str, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

◆ appendStringInfoText()

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

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4765 of file varlena.c.

4766 {
4768  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4769 
4770  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4771 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
char * text_to_cstring(const text *t)
Definition: varlena.c:215
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4807

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

◆ 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 4807 of file varlena.c.

4809 {
4810  text *result;
4811  int nitems,
4812  *dims,
4813  ndims;
4814  Oid element_type;
4815  int typlen;
4816  bool typbyval;
4817  char typalign;
4819  bool printed = false;
4820  char *p;
4821  bits8 *bitmap;
4822  int bitmask;
4823  int i;
4824  ArrayMetaState *my_extra;
4825 
4826  ndims = ARR_NDIM(v);
4827  dims = ARR_DIMS(v);
4828  nitems = ArrayGetNItems(ndims, dims);
4829 
4830  /* if there are no elements, return an empty string */
4831  if (nitems == 0)
4832  return cstring_to_text_with_len("", 0);
4833 
4834  element_type = ARR_ELEMTYPE(v);
4835  initStringInfo(&buf);
4836 
4837  /*
4838  * We arrange to look up info about element type, including its output
4839  * conversion proc, only once per series of calls, assuming the element
4840  * type doesn't change underneath us.
4841  */
4842  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4843  if (my_extra == NULL)
4844  {
4845  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4846  sizeof(ArrayMetaState));
4847  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4848  my_extra->element_type = ~element_type;
4849  }
4850 
4851  if (my_extra->element_type != element_type)
4852  {
4853  /*
4854  * Get info about element type, including its output conversion proc
4855  */
4856  get_type_io_data(element_type, IOFunc_output,
4857  &my_extra->typlen, &my_extra->typbyval,
4858  &my_extra->typalign, &my_extra->typdelim,
4859  &my_extra->typioparam, &my_extra->typiofunc);
4860  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4861  fcinfo->flinfo->fn_mcxt);
4862  my_extra->element_type = element_type;
4863  }
4864  typlen = my_extra->typlen;
4865  typbyval = my_extra->typbyval;
4866  typalign = my_extra->typalign;
4867 
4868  p = ARR_DATA_PTR(v);
4869  bitmap = ARR_NULLBITMAP(v);
4870  bitmask = 1;
4871 
4872  for (i = 0; i < nitems; i++)
4873  {
4874  Datum itemvalue;
4875  char *value;
4876 
4877  /* Get source element, checking for NULL */
4878  if (bitmap && (*bitmap & bitmask) == 0)
4879  {
4880  /* if null_string is NULL, we just ignore null elements */
4881  if (null_string != NULL)
4882  {
4883  if (printed)
4884  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4885  else
4886  appendStringInfoString(&buf, null_string);
4887  printed = true;
4888  }
4889  }
4890  else
4891  {
4892  itemvalue = fetch_att(p, typbyval, typlen);
4893 
4894  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4895 
4896  if (printed)
4897  appendStringInfo(&buf, "%s%s", fldsep, value);
4898  else
4900  printed = true;
4901 
4902  p = att_addlength_pointer(p, typlen, p);
4903  p = (char *) att_align_nominal(p, typalign);
4904  }
4905 
4906  /* advance bitmap pointer if any */
4907  if (bitmap)
4908  {
4909  bitmask <<= 1;
4910  if (bitmask == 0x100)
4911  {
4912  bitmap++;
4913  bitmask = 1;
4914  }
4915  }
4916  }
4917 
4918  result = cstring_to_text_with_len(buf.data, buf.len);
4919  pfree(buf.data);
4920 
4921  return result;
4922 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_NULLBITMAP(a)
Definition: array.h:293
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_DIMS(a)
Definition: array.h:287
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
uint8 bits8
Definition: c.h:497
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1655
#define nitems(x)
Definition: indent.h:31
static struct @145 value
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
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:2283
@ IOFunc_output
Definition: lsyscache.h:36
void pfree(void *pointer)
Definition: mcxt.c:1436
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1005
static char * buf
Definition: pg_test_fsync.c:67
char typalign
Definition: pg_type.h:176
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Oid typioparam
Definition: array.h:236
char typalign
Definition: array.h:234
Oid typiofunc
Definition: array.h:237
int16 typlen
Definition: array.h:232
Oid element_type
Definition: array.h:231
FmgrInfo proc
Definition: array.h:238
char typdelim
Definition: array.h:235
bool typbyval
Definition: array.h:233
void * fn_extra
Definition: fmgr.h:64
MemoryContext fn_mcxt
Definition: fmgr.h:65
FmgrInfo * flinfo
Definition: fmgr.h:87
Definition: c.h:671
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:129
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:157
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition: tupmacs.h:52
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:194

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(), ArrayMetaState::element_type, fetch_att(), FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_type_io_data(), i, if(), initStringInfo(), IOFunc_output, MemoryContextAlloc(), nitems, OutputFunctionCall(), pfree(), ArrayMetaState::proc, typalign, ArrayMetaState::typalign, ArrayMetaState::typbyval, ArrayMetaState::typdelim, ArrayMetaState::typiofunc, ArrayMetaState::typioparam, ArrayMetaState::typlen, and value.

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

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4781 of file varlena.c.

4782 {
4783  ArrayType *v;
4784  char *fldsep;
4785  char *null_string;
4786 
4787  /* returns NULL when first or second parameter is NULL */
4788  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4789  PG_RETURN_NULL();
4790 
4791  v = PG_GETARG_ARRAYTYPE_P(0);
4792  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4793 
4794  /* NULL null string is passed through as a null pointer */
4795  if (!PG_ARGISNULL(2))
4796  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4797  else
4798  null_string = NULL;
4799 
4800  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4801 }
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_NULL()
Definition: fmgr.h:345

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().

◆ bpcharfastcmp_c()

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

Definition at line 2046 of file varlena.c.

2047 {
2048  BpChar *arg1 = DatumGetBpCharPP(x);
2049  BpChar *arg2 = DatumGetBpCharPP(y);
2050  char *a1p,
2051  *a2p;
2052  int len1,
2053  len2,
2054  result;
2055 
2056  a1p = VARDATA_ANY(arg1);
2057  a2p = VARDATA_ANY(arg2);
2058 
2059  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2060  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2061 
2062  result = memcmp(a1p, a2p, Min(len1, len2));
2063  if ((result == 0) && (len1 != len2))
2064  result = (len1 < len2) ? -1 : 1;
2065 
2066  /* We can't afford to leak memory here. */
2067  if (PointerGetDatum(arg1) != x)
2068  pfree(arg1);
2069  if (PointerGetDatum(arg2) != y)
2070  pfree(arg2);
2071 
2072  return result;
2073 }
#define Min(x, y)
Definition: c.h:988
#define DatumGetBpCharPP(X)
Definition: fmgr.h:293
int y
Definition: isn.c:72
int x
Definition: isn.c:71
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
int bpchartruelen(char *s, int len)
Definition: varchar.c:677

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

Referenced by varstr_sortsupport().

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

Definition at line 2698 of file varlena.c.

2699 {
2700  Name arg1 = PG_GETARG_NAME(0);
2701  text *arg2 = PG_GETARG_TEXT_PP(1);
2702  int32 result;
2703 
2704  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2705  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2706  PG_GET_COLLATION());
2707 
2708  PG_FREE_IF_COPY(arg2, 1);
2709 
2710  PG_RETURN_INT32(result);
2711 }
#define NameStr(name)
Definition: c.h:730
signed int int32
Definition: c.h:478
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Definition: c.h:725
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1536

References NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

Referenced by namegetext(), namegttext(), nameletext(), and namelttext().

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2881 of file varlena.c.

2882 {
2883  text *arg1 = PG_GETARG_TEXT_PP(0);
2884  text *arg2 = PG_GETARG_TEXT_PP(1);
2885  int result;
2886 
2887  result = internal_text_pattern_compare(arg1, arg2);
2888 
2889  PG_FREE_IF_COPY(arg1, 0);
2890  PG_FREE_IF_COPY(arg2, 1);
2891 
2892  PG_RETURN_INT32(result);
2893 }
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2795

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

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2897 of file varlena.c.

2898 {
2900  MemoryContext oldcontext;
2901 
2902  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2903 
2904  /* Use generic string SortSupport, forcing "C" collation */
2905  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
2906 
2907  MemoryContextSwitchTo(oldcontext);
2908 
2909  PG_RETURN_VOID();
2910 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1870

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1828 of file varlena.c.

1829 {
1830  text *arg1 = PG_GETARG_TEXT_PP(0);
1831  text *arg2 = PG_GETARG_TEXT_PP(1);
1832  int32 result;
1833 
1834  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1835 
1836  PG_FREE_IF_COPY(arg1, 0);
1837  PG_FREE_IF_COPY(arg2, 1);
1838 
1839  PG_RETURN_INT32(result);
1840 }
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1591

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

Referenced by gbt_textcmp().

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

Definition at line 2714 of file varlena.c.

2715 {
2716  text *arg1 = PG_GETARG_TEXT_PP(0);
2717  Name arg2 = PG_GETARG_NAME(1);
2718  int32 result;
2719 
2720  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
2721  NameStr(*arg2), strlen(NameStr(*arg2)),
2722  PG_GET_COLLATION());
2723 
2724  PG_FREE_IF_COPY(arg1, 0);
2725 
2726  PG_RETURN_INT32(result);
2727 }

References NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

Referenced by textgename(), textgtname(), textlename(), and textltname().

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1843 of file varlena.c.

1844 {
1846  Oid collid = ssup->ssup_collation;
1847  MemoryContext oldcontext;
1848 
1849  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1850 
1851  /* Use generic string SortSupport */
1852  varstr_sortsupport(ssup, TEXTOID, collid);
1853 
1854  MemoryContextSwitchTo(oldcontext);
1855 
1856  PG_RETURN_VOID();
1857 }
Oid collid

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

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2553 of file varlena.c.

2554 {
2555  /* Oid opcintype = PG_GETARG_OID(0); */
2557 
2559 
2560  if (lc_collate_is_c(collid) ||
2561  collid == DEFAULT_COLLATION_OID ||
2563  PG_RETURN_BOOL(true);
2564  else
2565  PG_RETURN_BOOL(false);
2566 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1080
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1324
static void check_collation_set(Oid collid)
Definition: varlena.c:1507

References check_collation_set(), collid, get_collation_isdeterministic(), lc_collate_is_c(), PG_GET_COLLATION, and PG_RETURN_BOOL.

◆ build_concat_foutcache()

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

Definition at line 5310 of file varlena.c.

5311 {
5312  FmgrInfo *foutcache;
5313  int i;
5314 
5315  /* We keep the info in fn_mcxt so it survives across calls */
5316  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5317  PG_NARGS() * sizeof(FmgrInfo));
5318 
5319  for (i = argidx; i < PG_NARGS(); i++)
5320  {
5321  Oid valtype;
5322  Oid typOutput;
5323  bool typIsVarlena;
5324 
5325  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5326  if (!OidIsValid(valtype))
5327  elog(ERROR, "could not determine data type of concat() input");
5328 
5329  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5330  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5331  }
5332 
5333  fcinfo->flinfo->fn_extra = foutcache;
5334 
5335  return foutcache;
5336 }
#define OidIsValid(objectId)
Definition: c.h:759
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1882
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:203
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2865
Definition: fmgr.h:57

References elog(), ERROR, FunctionCallInfoBaseData::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().

◆ bytea_bit_count()

Datum bytea_bit_count ( PG_FUNCTION_ARGS  )

Definition at line 3149 of file varlena.c.

3150 {
3151  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3152 
3154 }
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:296

References PG_GETARG_BYTEA_PP, pg_popcount(), PG_RETURN_INT64, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ bytea_catenate()

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

Definition at line 2951 of file varlena.c.

2952 {
2953  bytea *result;
2954  int len1,
2955  len2,
2956  len;
2957  char *ptr;
2958 
2959  len1 = VARSIZE_ANY_EXHDR(t1);
2960  len2 = VARSIZE_ANY_EXHDR(t2);
2961 
2962  /* paranoia ... probably should throw error instead? */
2963  if (len1 < 0)
2964  len1 = 0;
2965  if (len2 < 0)
2966  len2 = 0;
2967 
2968  len = len1 + len2 + VARHDRSZ;
2969  result = (bytea *) palloc(len);
2970 
2971  /* Set size of result string... */
2972  SET_VARSIZE(result, len);
2973 
2974  /* Fill data field of result string... */
2975  ptr = VARDATA(result);
2976  if (len1 > 0)
2977  memcpy(ptr, VARDATA_ANY(t1), len1);
2978  if (len2 > 0)
2979  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2980 
2981  return result;
2982 }
#define VARHDRSZ
Definition: c.h:676
void * palloc(Size size)
Definition: mcxt.c:1210
const void size_t len
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

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

Referenced by bytea_overlay(), and byteacat().

◆ bytea_overlay()

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

Definition at line 3116 of file varlena.c.

3117 {
3118  bytea *result;
3119  bytea *s1;
3120  bytea *s2;
3121  int sp_pl_sl;
3122 
3123  /*
3124  * Check for possible integer-overflow cases. For negative sp, throw a
3125  * "substring length" error because that's what should be expected
3126  * according to the spec's definition of OVERLAY().
3127  */
3128  if (sp <= 0)
3129  ereport(ERROR,
3130  (errcode(ERRCODE_SUBSTRING_ERROR),
3131  errmsg("negative substring length not allowed")));
3132  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3133  ereport(ERROR,
3134  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3135  errmsg("integer out of range")));
3136 
3137  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3138  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3139  result = bytea_catenate(s1, t2);
3140  result = bytea_catenate(result, s2);
3141 
3142  return result;
3143 }
#define ereport(elevel,...)
Definition: elog.h:149
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
char * s1
char * s2
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3026
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2951

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

Referenced by byteaoverlay(), and byteaoverlay_no_len().

◆ bytea_sortsupport()

Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3958 of file varlena.c.

3959 {
3961  MemoryContext oldcontext;
3962 
3963  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3964 
3965  /* Use generic string SortSupport, forcing "C" collation */
3966  varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
3967 
3968  MemoryContextSwitchTo(oldcontext);
3969 
3970  PG_RETURN_VOID();
3971 }

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

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 549 of file varlena.c.

550 {
552 
553  /* cannot be called directly because of internal-type argument */
554  Assert(AggCheckCallContext(fcinfo, NULL));
555 
556  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
557 
558  if (state != NULL)
559  {
560  /* As per comment in transfn, strip data before the cursor position */
561  bytea *result;
562  int strippedlen = state->len - state->cursor;
563 
564  result = (bytea *) palloc(strippedlen + VARHDRSZ);
565  SET_VARSIZE(result, strippedlen + VARHDRSZ);
566  memcpy(VARDATA(result), &state->data[state->cursor], strippedlen);
567  PG_RETURN_BYTEA_P(result);
568  }
569  else
570  PG_RETURN_NULL();
571 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4514
StringInfoData * StringInfo
Definition: stringinfo.h:44
Definition: regguts.h:318

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

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 496 of file varlena.c.

497 {
499 
500  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
501 
502  /* Append the value unless null, preceding it with the delimiter. */
503  if (!PG_ARGISNULL(1))
504  {
506  bool isfirst = false;
507 
508  /*
509  * You might think we can just throw away the first delimiter, however
510  * we must keep it as we may be a parallel worker doing partial
511  * aggregation building a state to send to the main process. We need
512  * to keep the delimiter of every aggregation so that the combine
513  * function can properly join up the strings of two separately
514  * partially aggregated results. The first delimiter is only stripped
515  * off in the final function. To know how much to strip off the front
516  * of the string, we store the length of the first delimiter in the
517  * StringInfo's cursor field, which we don't otherwise need here.
518  */
519  if (state == NULL)
520  {
521  state = makeStringAggState(fcinfo);
522  isfirst = true;
523  }
524 
525  if (!PG_ARGISNULL(2))
526  {
527  bytea *delim = PG_GETARG_BYTEA_PP(2);
528 
530  VARSIZE_ANY_EXHDR(delim));
531  if (isfirst)
532  state->cursor = VARSIZE_ANY_EXHDR(delim);
533  }
534 
537  }
538 
539  /*
540  * The transition type for string_agg() is declared to be "internal",
541  * which is a pass-by-value type the same size as a pointer.
542  */
543  if (state)
545  PG_RETURN_NULL();
546 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5086

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

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 3003 of file varlena.c.

3004 {
3006  PG_GETARG_INT32(1),
3007  PG_GETARG_INT32(2),
3008  false));
3009 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

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

◆ bytea_substr_no_len()

Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3017 of file varlena.c.

3018 {
3020  PG_GETARG_INT32(1),
3021  -1,
3022  true));
3023 }

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

◆ bytea_substring()

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

Definition at line 3026 of file varlena.c.

3030 {
3031  int32 S1; /* adjusted start position */
3032  int32 L1; /* adjusted substring length */
3033  int32 E; /* end position */
3034 
3035  /*
3036  * The logic here should generally match text_substring().
3037  */
3038  S1 = Max(S, 1);
3039 
3040  if (length_not_specified)
3041  {
3042  /*
3043  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3044  * end of the string if we pass it a negative value for length.
3045  */
3046  L1 = -1;
3047  }
3048  else if (L < 0)
3049  {
3050  /* SQL99 says to throw an error for E < S, i.e., negative length */
3051  ereport(ERROR,
3052  (errcode(ERRCODE_SUBSTRING_ERROR),
3053  errmsg("negative substring length not allowed")));
3054  L1 = -1; /* silence stupider compilers */
3055  }
3056  else if (pg_add_s32_overflow(S, L, &E))
3057  {
3058  /*
3059  * L could be large enough for S + L to overflow, in which case the
3060  * substring must run to end of string.
3061  */
3062  L1 = -1;
3063  }
3064  else
3065  {
3066  /*
3067  * A zero or negative value for the end position can happen if the
3068  * start was negative or one. SQL99 says to return a zero-length
3069  * string.
3070  */
3071  if (E < 1)
3072  return PG_STR_GET_BYTEA("");
3073 
3074  L1 = E - S1;
3075  }
3076 
3077  /*
3078  * If the start position is past the end of the string, SQL99 says to
3079  * return a zero-length string -- DatumGetByteaPSlice() will do that for
3080  * us. We need only convert S1 to zero-based starting position.
3081  */
3082  return DatumGetByteaPSlice(str, S1 - 1, L1);
3083 }
#define Max(x, y)
Definition: c.h:982
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:303
#define S(n, x)
Definition: sha1.c:73
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:2984

References DatumGetByteaPSlice, ereport, errcode(), errmsg(), ERROR, Max, pg_add_s32_overflow(), PG_STR_GET_BYTEA, S, and generate_unaccent_rules::str.

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

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2936 of file varlena.c.

2937 {
2938  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2939  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2940 
2942 }

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3936 of file varlena.c.

3937 {
3938  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3939  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3940  int len1,
3941  len2;
3942  int cmp;
3943 
3944  len1 = VARSIZE_ANY_EXHDR(arg1);
3945  len2 = VARSIZE_ANY_EXHDR(arg2);
3946 
3947  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3948  if ((cmp == 0) && (len1 != len2))
3949  cmp = (len1 < len2) ? -1 : 1;
3950 
3951  PG_FREE_IF_COPY(arg1, 0);
3952  PG_FREE_IF_COPY(arg2, 1);
3953 
3955 }
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

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

Referenced by gbt_bitcmp(), and gbt_byteacmp().

◆ byteaeq()

Datum byteaeq ( PG_FUNCTION_ARGS  )

Definition at line 3792 of file varlena.c.

3793 {
3794  Datum arg1 = PG_GETARG_DATUM(0);
3795  Datum arg2 = PG_GETARG_DATUM(1);
3796  bool result;
3797  Size len1,
3798  len2;
3799 
3800  /*
3801  * We can use a fast path for unequal lengths, which might save us from
3802  * having to detoast one or both values.
3803  */
3804  len1 = toast_raw_datum_size(arg1);
3805  len2 = toast_raw_datum_size(arg2);
3806  if (len1 != len2)
3807  result = false;
3808  else
3809  {
3810  bytea *barg1 = DatumGetByteaPP(arg1);
3811  bytea *barg2 = DatumGetByteaPP(arg2);
3812 
3813  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3814  len1 - VARHDRSZ) == 0);
3815 
3816  PG_FREE_IF_COPY(barg1, 0);
3817  PG_FREE_IF_COPY(barg2, 1);
3818  }
3819 
3820  PG_RETURN_BOOL(result);
3821 }
size_t Size
Definition: c.h:589
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:545
#define DatumGetByteaPP(X)
Definition: fmgr.h:291

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

Referenced by gbt_byteaeq().

◆ byteage()

Datum byteage ( PG_FUNCTION_ARGS  )

Definition at line 3916 of file varlena.c.

3917 {
3918  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3919  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3920  int len1,
3921  len2;
3922  int cmp;
3923 
3924  len1 = VARSIZE_ANY_EXHDR(arg1);
3925  len2 = VARSIZE_ANY_EXHDR(arg2);
3926 
3927  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3928 
3929  PG_FREE_IF_COPY(arg1, 0);
3930  PG_FREE_IF_COPY(arg2, 1);
3931 
3932  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
3933 }

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

Referenced by gbt_byteage().

◆ byteaGetBit()

Datum byteaGetBit ( PG_FUNCTION_ARGS  )

Definition at line 3236 of file varlena.c.

3237 {
3238  bytea *v = PG_GETARG_BYTEA_PP(0);
3239  int64 n = PG_GETARG_INT64(1);
3240  int byteNo,
3241  bitNo;
3242  int len;
3243  int byte;
3244 
3245  len = VARSIZE_ANY_EXHDR(v);
3246 
3247  if (n < 0 || n >= (int64) len * 8)
3248  ereport(ERROR,
3249  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3250  errmsg("index %lld out of valid range, 0..%lld",
3251  (long long) n, (long long) len * 8 - 1)));
3252 
3253  /* n/8 is now known < len, so safe to cast to int */
3254  byteNo = (int) (n / 8);
3255  bitNo = (int) (n % 8);
3256 
3257  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3258 
3259  if (byte & (1 << bitNo))
3260  PG_RETURN_INT32(1);
3261  else
3262  PG_RETURN_INT32(0);
3263 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

References ereport, errcode(), errmsg(), ERROR, len, PG_GETARG_BYTEA_PP, PG_GETARG_INT64, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ byteaGetByte()

Datum byteaGetByte ( PG_FUNCTION_ARGS  )

Definition at line 3207 of file varlena.c.

3208 {
3209  bytea *v = PG_GETARG_BYTEA_PP(0);
3210  int32 n = PG_GETARG_INT32(1);
3211  int len;
3212  int byte;
3213 
3214  len = VARSIZE_ANY_EXHDR(v);
3215 
3216  if (n < 0 || n >= len)
3217  ereport(ERROR,
3218  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3219  errmsg("index %d out of valid range, 0..%d",
3220  n, len - 1)));
3221 
3222  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3223 
3224  PG_RETURN_INT32(byte);
3225 }

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

◆ byteagt()

Datum byteagt ( PG_FUNCTION_ARGS  )

Definition at line 3896 of file varlena.c.

3897 {
3898  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3899  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3900  int len1,
3901  len2;
3902  int cmp;
3903 
3904  len1 = VARSIZE_ANY_EXHDR(arg1);
3905  len2 = VARSIZE_ANY_EXHDR(arg2);
3906 
3907  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3908 
3909  PG_FREE_IF_COPY(arg1, 0);
3910  PG_FREE_IF_COPY(arg2, 1);
3911 
3912  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
3913 }

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

Referenced by gbt_byteagt().

◆ byteain()

Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 288 of file varlena.c.

289 {
290  char *inputText = PG_GETARG_CSTRING(0);
291  Node *escontext = fcinfo->context;
292  char *tp;
293  char *rp;
294  int bc;
295  bytea *result;
296 
297  /* Recognize hex input */
298  if (inputText[0] == '\\' && inputText[1] == 'x')
299  {
300  size_t len = strlen(inputText);
301 
302  bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */
303  result = palloc(bc);
304  bc = hex_decode_safe(inputText + 2, len - 2, VARDATA(result),
305  escontext);
306  SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */
307 
308  PG_RETURN_BYTEA_P(result);
309  }
310 
311  /* Else, it's the traditional escaped style */
312  for (bc = 0, tp = inputText; *tp != '\0'; bc++)
313  {
314  if (tp[0] != '\\')
315  tp++;
316  else if ((tp[0] == '\\') &&
317  (tp[1] >= '0' && tp[1] <= '3') &&
318  (tp[2] >= '0' && tp[2] <= '7') &&
319  (tp[3] >= '0' && tp[3] <= '7'))
320  tp += 4;
321  else if ((tp[0] == '\\') &&
322  (tp[1] == '\\'))
323  tp += 2;
324  else
325  {
326  /*
327  * one backslash, not followed by another or ### valid octal
328  */
329  ereturn(escontext, (Datum) 0,
330  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
331  errmsg("invalid input syntax for type %s", "bytea")));
332  }
333  }
334 
335  bc += VARHDRSZ;
336 
337  result = (bytea *) palloc(bc);
338  SET_VARSIZE(result, bc);
339 
340  tp = inputText;
341  rp = VARDATA(result);
342  while (*tp != '\0')
343  {
344  if (tp[0] != '\\')
345  *rp++ = *tp++;
346  else if ((tp[0] == '\\') &&
347  (tp[1] >= '0' && tp[1] <= '3') &&
348  (tp[2] >= '0' && tp[2] <= '7') &&
349  (tp[3] >= '0' && tp[3] <= '7'))
350  {
351  bc = VAL(tp[1]);
352  bc <<= 3;
353  bc += VAL(tp[2]);
354  bc <<= 3;
355  *rp++ = bc + VAL(tp[3]);
356 
357  tp += 4;
358  }
359  else if ((tp[0] == '\\') &&
360  (tp[1] == '\\'))
361  {
362  *rp++ = '\\';
363  tp += 2;
364  }
365  else
366  {
367  /*
368  * We should never get here. The first pass should not allow it.
369  */
370  ereturn(escontext, (Datum) 0,
371  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
372  errmsg("invalid input syntax for type %s", "bytea")));
373  }
374  }
375 
376  PG_RETURN_BYTEA_P(result);
377 }
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
uint64 hex_decode_safe(const char *src, size_t len, char *dst, Node *escontext)
Definition: encode.c:196
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
Definition: nodes.h:129
#define VAL(CH)
Definition: varlena.c:273

References ereturn, errcode(), errmsg(), hex_decode_safe(), len, palloc(), PG_GETARG_CSTRING, PG_RETURN_BYTEA_P, SET_VARSIZE, VAL, VARDATA, and VARHDRSZ.

Referenced by CreateTriggerFiringOn(), and string_to_datum().

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

Definition at line 3876 of file varlena.c.

3877 {
3878  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3879  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3880  int len1,
3881  len2;
3882  int cmp;
3883 
3884  len1 = VARSIZE_ANY_EXHDR(arg1);
3885  len2 = VARSIZE_ANY_EXHDR(arg2);
3886 
3887  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3888 
3889  PG_FREE_IF_COPY(arg1, 0);
3890  PG_FREE_IF_COPY(arg2, 1);
3891 
3892  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
3893 }

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

Referenced by gbt_byteale().

◆ bytealt()

Datum bytealt ( PG_FUNCTION_ARGS  )

Definition at line 3856 of file varlena.c.

3857 {
3858  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3859  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3860  int len1,
3861  len2;
3862  int cmp;
3863 
3864  len1 = VARSIZE_ANY_EXHDR(arg1);
3865  len2 = VARSIZE_ANY_EXHDR(arg2);
3866 
3867  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3868 
3869  PG_FREE_IF_COPY(arg1, 0);
3870  PG_FREE_IF_COPY(arg2, 1);
3871 
3872  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3873 }

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

Referenced by gbt_bytealt().

◆ byteane()

Datum byteane ( PG_FUNCTION_ARGS  )

Definition at line 3824 of file varlena.c.

3825 {
3826  Datum arg1 = PG_GETARG_DATUM(0);
3827  Datum arg2 = PG_GETARG_DATUM(1);
3828  bool result;
3829  Size len1,
3830  len2;
3831 
3832  /*
3833  * We can use a fast path for unequal lengths, which might save us from
3834  * having to detoast one or both values.
3835  */
3836  len1 = toast_raw_datum_size(arg1);
3837  len2 = toast_raw_datum_size(arg2);
3838  if (len1 != len2)
3839  result = true;
3840  else
3841  {
3842  bytea *barg1 = DatumGetByteaPP(arg1);
3843  bytea *barg2 = DatumGetByteaPP(arg2);
3844 
3845  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3846  len1 - VARHDRSZ) != 0);
3847 
3848  PG_FREE_IF_COPY(barg1, 0);
3849  PG_FREE_IF_COPY(barg2, 1);
3850  }
3851 
3852  PG_RETURN_BOOL(result);
3853 }

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

◆ byteaoctetlen()

Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 2920 of file varlena.c.

2921 {
2922  Datum str = PG_GETARG_DATUM(0);
2923 
2924  /* We need not detoast the input at all */
2926 }

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

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 386 of file varlena.c.

387 {
388  bytea *vlena = PG_GETARG_BYTEA_PP(0);
389  char *result;
390  char *rp;
391 
393  {
394  /* Print hex format */
395  rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
396  *rp++ = '\\';
397  *rp++ = 'x';
398  rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
399  }
400  else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
401  {
402  /* Print traditional escaped format */
403  char *vp;
404  uint64 len;
405  int i;
406 
407  len = 1; /* empty string has 1 char */
408  vp = VARDATA_ANY(vlena);
409  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
410  {
411  if (*vp == '\\')
412  len += 2;
413  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
414  len += 4;
415  else
416  len++;
417  }
418 
419  /*
420  * In principle len can't overflow uint32 if the input fit in 1GB, but
421  * for safety let's check rather than relying on palloc's internal
422  * check.
423  */
424  if (len > MaxAllocSize)
425  ereport(ERROR,
426  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
427  errmsg_internal("result of bytea output conversion is too large")));
428  rp = result = (char *) palloc(len);
429 
430  vp = VARDATA_ANY(vlena);
431  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
432  {
433  if (*vp == '\\')
434  {
435  *rp++ = '\\';
436  *rp++ = '\\';
437  }
438  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
439  {
440  int val; /* holds unprintable chars */
441 
442  val = *vp;
443  rp[0] = '\\';
444  rp[3] = DIG(val & 07);
445  val >>= 3;
446  rp[2] = DIG(val & 07);
447  val >>= 3;
448  rp[1] = DIG(val & 03);
449  rp += 4;
450  }
451  else
452  *rp++ = *vp;
453  }
454  }
455  else
456  {
457  elog(ERROR, "unrecognized bytea_output setting: %d",
458  bytea_output);
459  rp = result = NULL; /* keep compiler quiet */
460  }
461  *rp = '\0';
462  PG_RETURN_CSTRING(result);
463 }
@ BYTEA_OUTPUT_HEX
Definition: bytea.h:22
@ BYTEA_OUTPUT_ESCAPE
Definition: bytea.h:21
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
uint64 hex_encode(const char *src, size_t len, char *dst)
Definition: encode.c:162
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
long val
Definition: informix.c:664
#define MaxAllocSize
Definition: memutils.h:40
int bytea_output
Definition: varlena.c:46
#define DIG(VAL)
Definition: varlena.c:274

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

Referenced by pg_mcv_list_out().

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 3093 of file varlena.c.

3094 {
3095  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3096  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3097  int sp = PG_GETARG_INT32(2); /* substring start position */
3098  int sl = PG_GETARG_INT32(3); /* substring length */
3099 
3100  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3101 }
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3116

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

◆ byteaoverlay_no_len()

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3104 of file varlena.c.

3105 {
3106  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3107  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3108  int sp = PG_GETARG_INT32(2); /* substring start position */
3109  int sl;
3110 
3111  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3112  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3113 }

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

◆ byteapos()

Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 3163 of file varlena.c.

3164 {
3165  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3166  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3167  int pos;
3168  int px,
3169  p;
3170  int len1,
3171  len2;
3172  char *p1,
3173  *p2;
3174 
3175  len1 = VARSIZE_ANY_EXHDR(t1);
3176  len2 = VARSIZE_ANY_EXHDR(t2);
3177 
3178  if (len2 <= 0)
3179  PG_RETURN_INT32(1); /* result for empty pattern */
3180 
3181  p1 = VARDATA_ANY(t1);
3182  p2 = VARDATA_ANY(t2);
3183 
3184  pos = 0;
3185  px = (len1 - len2);
3186  for (p = 0; p <= px; p++)
3187  {
3188  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3189  {
3190  pos = p + 1;
3191  break;
3192  };
3193  p1++;
3194  };
3195 
3196  PG_RETURN_INT32(pos);
3197 }
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)

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

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

Definition at line 469 of file varlena.c.

470 {
472  bytea *result;
473  int nbytes;
474 
475  nbytes = buf->len - buf->cursor;
476  result = (bytea *) palloc(nbytes + VARHDRSZ);
477  SET_VARSIZE(result, nbytes + VARHDRSZ);
478  pq_copymsgbytes(buf, VARDATA(result), nbytes);
479  PG_RETURN_BYTEA_P(result);
480 }
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:531

References buf, palloc(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_copymsgbytes(), SET_VARSIZE, VARDATA, and VARHDRSZ.

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 488 of file varlena.c.

489 {
490  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
491 
492  PG_RETURN_BYTEA_P(vlena);
493 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

Referenced by brin_bloom_summary_send(), brin_minmax_multi_summary_send(), pg_dependencies_send(), pg_mcv_list_send(), and pg_ndistinct_send().

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3306 of file varlena.c.

3307 {
3309  int64 n = PG_GETARG_INT64(1);
3310  int32 newBit = PG_GETARG_INT32(2);
3311  int len;
3312  int oldByte,
3313  newByte;
3314  int byteNo,
3315  bitNo;
3316 
3317  len = VARSIZE(res) - VARHDRSZ;
3318 
3319  if (n < 0 || n >= (int64) len * 8)
3320  ereport(ERROR,
3321  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3322  errmsg("index %lld out of valid range, 0..%lld",
3323  (long long) n, (long long) len * 8 - 1)));
3324 
3325  /* n/8 is now known < len, so safe to cast to int */
3326  byteNo = (int) (n / 8);
3327  bitNo = (int) (n % 8);
3328 
3329  /*
3330  * sanity check!
3331  */
3332  if (newBit != 0 && newBit != 1)
3333  ereport(ERROR,
3334  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3335  errmsg("new bit must be 0 or 1")));
3336 
3337  /*
3338  * Update the byte.
3339  */
3340  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3341 
3342  if (newBit == 0)
3343  newByte = oldByte & (~(1 << bitNo));
3344  else
3345  newByte = oldByte | (1 << bitNo);
3346 
3347  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3348 
3350 }
#define VARSIZE(PTR)
Definition: varatt.h:279

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

◆ byteaSetByte()

Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 3274 of file varlena.c.

3275 {
3277  int32 n = PG_GETARG_INT32(1);
3278  int32 newByte = PG_GETARG_INT32(2);
3279  int len;
3280 
3281  len = VARSIZE(res) - VARHDRSZ;
3282 
3283  if (n < 0 || n >= len)
3284  ereport(ERROR,
3285  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3286  errmsg("index %d out of valid range, 0..%d",
3287  n, len - 1)));
3288 
3289  /*
3290  * Now set the byte.
3291  */
3292  ((unsigned char *) VARDATA(res))[n] = newByte;
3293 
3295 }

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

◆ charlen_to_bytelen()

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

Definition at line 804 of file varlena.c.

805 {
807  {
808  /* Optimization for single-byte encodings */
809  return n;
810  }
811  else
812  {
813  const char *s;
814 
815  for (s = p; n > 0; n--)
816  s += pg_mblen(s);
817 
818  return s - p;
819  }
820 }
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1553
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

Definition at line 1507 of file varlena.c.

1508 {
1509  if (!OidIsValid(collid))
1510  {
1511  /*
1512  * This typically means that the parser could not resolve a conflict
1513  * of implicit collations, so report it that way.
1514  */
1515  ereport(ERROR,
1516  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1517  errmsg("could not determine which collation to use for string comparison"),
1518  errhint("Use the COLLATE clause to set the collation explicitly.")));
1519  }
1520 }

References collid, ereport, errcode(), errhint(), errmsg(), ERROR, and OidIsValid.

Referenced by btvarstrequalimage(), nameeqtext(), namenetext(), text_position_setup(), text_starts_with(), texteq(), texteqname(), textne(), textnename(), varstr_cmp(), and varstr_sortsupport().

◆ check_replace_text_has_escape()

static int check_replace_text_has_escape ( const text replace_text)
static

Definition at line 4071 of file varlena.c.

4072 {
4073  int result = 0;
4074  const char *p = VARDATA_ANY(replace_text);
4075  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4076 
4077  while (p < p_end)
4078  {
4079  /* Find next escape char, if any. */
4080  p = memchr(p, '\\', p_end - p);
4081  if (p == NULL)
4082  break;
4083  p++;
4084  /* Note: a backslash at the end doesn't require extra processing. */
4085  if (p < p_end)
4086  {
4087  if (*p >= '1' && *p <= '9')
4088  return 2; /* Found a submatch specifier, so done */
4089  result = 1; /* Found some other sequence, keep looking */
4090  p++;
4091  }
4092  }
4093  return result;
4094 }

References replace_text(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

◆ concat_internal()

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

Definition at line 5348 of file varlena.c.

5350 {
5351  text *result;
5353  FmgrInfo *foutcache;
5354  bool first_arg = true;
5355  int i;
5356 
5357  /*
5358  * concat(VARIADIC some-array) is essentially equivalent to
5359  * array_to_text(), ie concat the array elements with the given separator.
5360  * So we just pass the case off to that code.
5361  */
5362  if (get_fn_expr_variadic(fcinfo->flinfo))
5363  {
5364  ArrayType *arr;
5365 
5366  /* Should have just the one argument */
5367  Assert(argidx == PG_NARGS() - 1);
5368 
5369  /* concat(VARIADIC NULL) is defined as NULL */
5370  if (PG_ARGISNULL(argidx))
5371  return NULL;
5372 
5373  /*
5374  * Non-null argument had better be an array. We assume that any call
5375  * context that could let get_fn_expr_variadic return true will have
5376  * checked that a VARIADIC-labeled parameter actually is an array. So
5377  * it should be okay to just Assert that it's an array rather than
5378  * doing a full-fledged error check.
5379  */
5381 
5382  /* OK, safe to fetch the array value */
5383  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5384 
5385  /*
5386  * And serialize the array. We tell array_to_text to ignore null
5387  * elements, which matches the behavior of the loop below.
5388  */
5389  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5390  }
5391 
5392  /* Normal case without explicit VARIADIC marker */
5393  initStringInfo(&str);
5394 
5395  /* Get output function info, building it if first time through */
5396  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5397  if (foutcache == NULL)
5398  foutcache = build_concat_foutcache(fcinfo, argidx);
5399 
5400  for (i = argidx; i < PG_NARGS(); i++)
5401  {
5402  if (!PG_ARGISNULL(i))
5403  {
5405 
5406  /* add separator if appropriate */
5407  if (first_arg)
5408  first_arg = false;
5409  else
5410  appendStringInfoString(&str, sepstr);
5411 
5412  /* call the appropriate type output function, append the result */
5414  OutputFunctionCall(&foutcache[i], value));
5415  }
5416  }
5417 
5418  result = cstring_to_text_with_len(str.data, str.len);
5419  pfree(str.data);
5420 
5421  return result;
5422 }
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2016
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2790
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5310

References appendStringInfoString(), array_to_text_internal(), Assert(), build_concat_foutcache(), cstring_to_text_with_len(), FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, if(), initStringInfo(), 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().

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 182 of file varlena.c.

183 {
184  return cstring_to_text_with_len(s, strlen(s));
185 }

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_minmax_multi_summary_out(), brin_page_type(), build_test_info_result(), build_test_match_result(), 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(), executeDateTimeMethod(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_scalar(), hash_page_type(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_get_element(), jsonb_typeof(), JsonbValueAsText(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), ParameterAclCreate(), ParameterAclLookup(), pg_collation_actual_version(), pg_collation_for(), pg_column_compression(), pg_crypt(), pg_current_logfile(), pg_database_collation_actual_version(), pg_describe_object(), pg_export_snapshot(), pg_get_statisticsobjdef_expressions(), pg_get_wal_replay_pause_state(), pg_parameter_aclmask(), 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_stat_get_slru(), pg_stats_ext_mcvlist_items(), pg_tablespace_location(), pg_walfile_name(), pg_xact_status(), 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(), split_part(), ssl_cipher(), ssl_client_dn(), ssl_issuer_dn(), ssl_version(), string_to_text(), test_canonicalize_path(), test_slru_page_read(), test_slru_page_readonly(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), unaccent_dict(), upper(), xml_encode_special_chars(), xml_in(), and xml_recv().

◆ cstring_to_text_with_len()

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

Definition at line 194 of file varlena.c.

195 {
196  text *result = (text *) palloc(len + VARHDRSZ);
197 
198  SET_VARSIZE(result, len + VARHDRSZ);
199  memcpy(VARDATA(result), s, len);
200 
201  return result;
202 }

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

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), brin_minmax_multi_summary_out(), build_regexp_match_result(), build_regexp_split_result(), build_test_match_result(), compute_tsvector_stats(), concat_internal(), cstring_to_text(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), ExecEvalXmlExpr(), executeDateTimeMethod(), executeLikeRegex(), fsm_page_contents(), get_array_element_end(), get_array_end(), 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_subscript_fetch(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array_noargs(), json_build_array_worker(), json_build_object_noargs(), json_build_object_worker(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_pretty(), JsonbValueAsText(), leftmostvalue_text(), LogicalOutputWrite(), parse_ident(), pg_gen_salt(), pg_gen_salt_rounds(), pg_get_function_sqlbody(), replace_text(), replace_text_regexp(), row_to_json(), row_to_json_pretty(), serialize_deflist(), split_part(), split_text(), ssl_extension_info(), string_agg_finalfn(), stringinfo_to_xmltype(), text_format(), text_left(), text_right(), textrecv(), to_json(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), unistr(), varchar(), varchar_input(), and xslt_process().

◆ getClosestMatch()

const char* getClosestMatch ( ClosestMatchState state)

Definition at line 6169 of file varlena.c.

6170 {
6171  Assert(state);
6172 
6173  return state->match;
6174 }

References Assert().

Referenced by dblink_fdw_validator(), file_fdw_validator(), postgres_fdw_validator(), and postgresql_fdw_validator().

◆ hexval()

static unsigned int hexval ( unsigned char  c)
static

Definition at line 6339 of file varlena.c.

6340 {
6341  if (c >= '0' && c <= '9')
6342  return c - '0';
6343  if (c >= 'a' && c <= 'f')
6344  return c - 'a' + 0xA;
6345  if (c >= 'A' && c <= 'F')
6346  return c - 'A' + 0xA;
6347  elog(ERROR, "invalid hexadecimal digit");
6348  return 0; /* not reached */
6349 }
char * c

References elog(), and ERROR.

Referenced by hexval_n().

◆ hexval_n()

static unsigned int hexval_n ( const char *  instr,
size_t  n 
)
static

Definition at line 6355 of file varlena.c.

6356 {
6357  unsigned int result = 0;
6358 
6359  for (size_t i = 0; i < n; i++)
6360  result += hexval(instr[i]) << (4 * (n - i - 1));
6361 
6362  return result;
6363 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6339

References hexval(), and i.

Referenced by unistr().

◆ initClosestMatch()

void initClosestMatch ( ClosestMatchState state,
const char *  source,
int  max_d 
)

Definition at line 6114 of file varlena.c.

6115 {
6116  Assert(state);
6117  Assert(max_d >= 0);
6118 
6119  state->source = source;
6120  state->min_d = -1;
6121  state->max_d = max_d;
6122  state->match = NULL;
6123 }
static rewind_source * source
Definition: pg_rewind.c:87

References Assert(), and source.

Referenced by dblink_fdw_validator(), file_fdw_validator(), postgres_fdw_validator(), and postgresql_fdw_validator().

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 2795 of file varlena.c.

2796 {
2797  int result;
2798  int len1,
2799  len2;
2800 
2801  len1 = VARSIZE_ANY_EXHDR(arg1);
2802  len2 = VARSIZE_ANY_EXHDR(arg2);
2803 
2804  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2805  if (result != 0)
2806  return result;
2807  else if (len1 < len2)
2808  return -1;
2809  else if (len1 > len2)
2810  return 1;
2811  else
2812  return 0;
2813 }

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().

◆ isxdigits_n()

static bool isxdigits_n ( const char *  instr,
size_t  n 
)
static

Definition at line 6329 of file varlena.c.

6330 {
6331  for (size_t i = 0; i < n; i++)
6332  if (!isxdigit((unsigned char) instr[i]))
6333  return false;
6334 
6335  return true;
6336 }

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5086 of file varlena.c.

5087 {
5088  StringInfo state;
5089  MemoryContext aggcontext;
5090  MemoryContext oldcontext;
5091 
5092  if (!AggCheckCallContext(fcinfo, &aggcontext))
5093  {
5094  /* cannot be called directly because of internal-type argument */
5095  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5096  }
5097 
5098  /*
5099  * Create state in aggregate context. It'll stay there across subsequent
5100  * calls.
5101  */
5102  oldcontext = MemoryContextSwitchTo(aggcontext);
5103  state = makeStringInfo();
5104  MemoryContextSwitchTo(oldcontext);
5105 
5106  return state;
5107 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41

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

Referenced by bytea_string_agg_transfn(), string_agg_combine(), string_agg_deserialize(), and string_agg_transfn().

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3380 of file varlena.c.

3381 {
3382  Name s = PG_GETARG_NAME(0);
3383 
3385 }
text * cstring_to_text(const char *s)
Definition: varlena.c:182

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

Referenced by nameiclike(), and nameicnlike().

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

Definition at line 2598 of file varlena.c.

2599 {
2600  Name arg1 = PG_GETARG_NAME(0);
2601  text *arg2 = PG_GETARG_TEXT_PP(1);
2602  size_t len1 = strlen(NameStr(*arg1));
2603  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2605  bool result;
2606 
2608 
2609  if (collid == C_COLLATION_OID)
2610  result = (len1 == len2 &&
2611  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2612  else
2613  result = (varstr_cmp(NameStr(*arg1), len1,
2614  VARDATA_ANY(arg2), len2,
2615  collid) == 0);
2616 
2617  PG_FREE_IF_COPY(arg2, 1);
2618 
2619  PG_RETURN_BOOL(result);
2620 }

References check_collation_set(), collid, NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

◆ namefastcmp_c()

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

Definition at line 2079 of file varlena.c.

2080 {
2081  Name arg1 = DatumGetName(x);
2082  Name arg2 = DatumGetName(y);
2083 
2084  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2085 }
#define NAMEDATALEN
static Name DatumGetName(Datum X)
Definition: postgres.h:360

References DatumGetName(), NAMEDATALEN, NameStr, x, and y.

Referenced by varstr_sortsupport().

◆ namefastcmp_locale()

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

Definition at line 2122 of file varlena.c.

2123 {
2124  Name arg1 = DatumGetName(x);
2125  Name arg2 = DatumGetName(y);
2126 
2127  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2128  NameStr(*arg2), strlen(NameStr(*arg2)),
2129  ssup);
2130 }
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2136

References DatumGetName(), NameStr, varstrfastcmp_locale(), x, and y.

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 2754 of file varlena.c.

2755 {
2757 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2729
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2698

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 2748 of file varlena.c.

2749 {
2751 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2742 of file varlena.c.

2743 {
2745 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2736 of file varlena.c.

2737 {
2739 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2648 of file varlena.c.

2649 {
2650  Name arg1 = PG_GETARG_NAME(0);
2651  text *arg2 = PG_GETARG_TEXT_PP(1);
2652  size_t len1 = strlen(NameStr(*arg1));
2653  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2655  bool result;
2656 
2658 
2659  if (collid == C_COLLATION_OID)
2660  result = !(len1 == len2 &&
2661  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2662  else
2663  result = !(varstr_cmp(NameStr(*arg1), len1,
2664  VARDATA_ANY(arg2), len2,
2665  collid) == 0);
2666 
2667  PG_FREE_IF_COPY(arg2, 1);
2668 
2669  PG_RETURN_BOOL(result);
2670 }

References check_collation_set(), collid, NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

◆ pg_column_compression()

Datum pg_column_compression ( PG_FUNCTION_ARGS  )

Definition at line 5026 of file varlena.c.

5027 {
5028  int typlen;
5029  char *result;
5030  ToastCompressionId cmid;
5031 
5032  /* On first call, get the input type's typlen, and save at *fn_extra */
5033  if (fcinfo->flinfo->fn_extra == NULL)
5034  {
5035  /* Lookup the datatype of the supplied argument */
5036  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5037 
5038  typlen = get_typlen(argtypeid);
5039  if (typlen == 0) /* should not happen */
5040  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5041 
5042  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5043  sizeof(int));
5044  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5045  }
5046  else
5047  typlen = *((int *) fcinfo->flinfo->fn_extra);
5048 
5049  if (typlen != -1)
5050  PG_RETURN_NULL();
5051 
5052  /* get the compression method id stored in the compressed varlena */
5053  cmid = toast_get_compression_id((struct varlena *)
5055  if (cmid == TOAST_INVALID_COMPRESSION_ID)
5056  PG_RETURN_NULL();
5057 
5058  /* convert compression method id to compression method name */
5059  switch (cmid)
5060  {
5062  result = "pglz";
5063  break;
5065  result = "lz4";
5066  break;
5067  default:
5068  elog(ERROR, "invalid compression method id %d", cmid);
5069  }
5070 
5072 }
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2155
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
ToastCompressionId toast_get_compression_id(struct varlena *attr)
ToastCompressionId
@ TOAST_INVALID_COMPRESSION_ID
@ TOAST_LZ4_COMPRESSION_ID
@ TOAST_PGLZ_COMPRESSION_ID

References cstring_to_text(), DatumGetPointer(), elog(), ERROR, get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_TEXT_P, toast_get_compression_id(), TOAST_INVALID_COMPRESSION_ID, TOAST_LZ4_COMPRESSION_ID, and TOAST_PGLZ_COMPRESSION_ID.

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 4979 of file varlena.c.

4980 {
4982  int32 result;
4983  int typlen;
4984 
4985  /* On first call, get the input type's typlen, and save at *fn_extra */
4986  if (fcinfo->flinfo->fn_extra == NULL)
4987  {
4988  /* Lookup the datatype of the supplied argument */
4989  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4990 
4991  typlen = get_typlen(argtypeid);
4992  if (typlen == 0) /* should not happen */
4993  elog(ERROR, "cache lookup failed for type %u", argtypeid);
4994 
4995  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4996  sizeof(int));
4997  *((int *) fcinfo->flinfo->fn_extra) = typlen;
4998  }
4999  else
5000  typlen = *((int *) fcinfo->flinfo->fn_extra);
5001 
5002  if (typlen == -1)
5003  {
5004  /* varlena type, possibly toasted */
5005  result = toast_datum_size(value);
5006  }
5007  else if (typlen == -2)
5008  {
5009  /* cstring */
5010  result = strlen(DatumGetCString(value)) + 1;
5011  }
5012  else
5013  {
5014  /* ordinary fixed-width type */
5015  result = typlen;
5016  }
5017 
5018  PG_RETURN_INT32(result);
5019 }
Size toast_datum_size(Datum value)
Definition: detoast.c:601
static char * DatumGetCString(Datum X)
Definition: postgres.h:335

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

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3994 of file varlena.c.

3995 {
3996  text *src_text = PG_GETARG_TEXT_PP(0);
3997  text *from_sub_text = PG_GETARG_TEXT_PP(1);
3998  text *to_sub_text = PG_GETARG_TEXT_PP(2);
3999  int src_text_len;
4000  int from_sub_text_len;
4002  text *ret_text;
4003  int chunk_len;
4004  char *curr_ptr;
4005  char *start_ptr;
4007  bool found;
4008 
4009  src_text_len = VARSIZE_ANY_EXHDR(src_text);
4010  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4011 
4012  /* Return unmodified source string if empty source or pattern */
4013  if (src_text_len < 1 || from_sub_text_len < 1)
4014  {
4015  PG_RETURN_TEXT_P(src_text);
4016  }
4017 
4018  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4019 
4020  found = text_position_next(&state);
4021 
4022  /* When the from_sub_text is not found, there is nothing to do. */
4023  if (!found)
4024  {
4026  PG_RETURN_TEXT_P(src_text);
4027  }
4028  curr_ptr = text_position_get_match_ptr(&state);
4029  start_ptr = VARDATA_ANY(src_text);
4030 
4031  initStringInfo(&str);
4032 
4033  do
4034  {
4036 
4037  /* copy the data skipped over by last text_position_next() */
4038  chunk_len = curr_ptr - start_ptr;
4039  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4040 
4041  appendStringInfoText(&str, to_sub_text);
4042 
4043  start_ptr = curr_ptr + from_sub_text_len;
4044 
4045  found = text_position_next(&state);
4046  if (found)
4047  curr_ptr = text_position_get_match_ptr(&state);
4048  }
4049  while (found);
4050 
4051  /* copy trailing data */
4052  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4053  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4054 
4056 
4057  ret_text = cstring_to_text_with_len(str.data, str.len);
4058  pfree(str.data);
4059 
4060  PG_RETURN_TEXT_P(ret_text);
4061 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1213
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1500
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3980
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1465
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1333

References appendBinaryStringInfo(), appendStringInfoText(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), initStringInfo(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, generate_unaccent_rules::str, text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_setup(), VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by appendStringInfoRegexpSubstr(), check_replace_text_has_escape(), execute_extension_script(), and replace_text_regexp().

◆ replace_text_regexp()

text* replace_text_regexp ( text src_text,
text pattern_text,
text replace_text,
int  cflags,
Oid  collation,
int  search_start,
int  n 
)

Definition at line 4204 of file varlena.c.

4208 {
4209  text *ret_text;
4210  regex_t *re;
4211  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4212  int nmatches = 0;
4214  regmatch_t pmatch[10]; /* main match, plus \1 to \9 */
4215  int nmatch = lengthof(pmatch);
4216  pg_wchar *data;
4217  size_t data_len;
4218  int data_pos;
4219  char *start_ptr;
4220  int escape_status;
4221 
4222  initStringInfo(&buf);
4223 
4224  /* Convert data string to wide characters. */
4225  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4226  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4227 
4228  /* Check whether replace_text has escapes, especially regexp submatches. */
4230 
4231  /* If no regexp submatches, we can use REG_NOSUB. */
4232  if (escape_status < 2)
4233  {
4234  cflags |= REG_NOSUB;
4235  /* Also tell pg_regexec we only want the whole-match location. */
4236  nmatch = 1;
4237  }
4238 
4239  /* Prepare the regexp. */
4240  re = RE_compile_and_cache(pattern_text, cflags, collation);
4241 
4242  /* start_ptr points to the data_pos'th character of src_text */
4243  start_ptr = (char *) VARDATA_ANY(src_text);
4244  data_pos = 0;
4245 
4246  while (search_start <= data_len)
4247  {
4248  int regexec_result;
4249 
4251 
4252  regexec_result = pg_regexec(re,
4253  data,
4254  data_len,
4255  search_start,
4256  NULL, /* no details */
4257  nmatch,
4258  pmatch,
4259  0);
4260 
4261  if (regexec_result == REG_NOMATCH)
4262  break;
4263 
4264  if (regexec_result != REG_OKAY)
4265  {
4266  char errMsg[100];
4267 
4269  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4270  ereport(ERROR,
4271  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4272  errmsg("regular expression failed: %s", errMsg)));
4273  }
4274 
4275  /*
4276  * Count matches, and decide whether to replace this match.
4277  */
4278  nmatches++;
4279  if (n > 0 && nmatches != n)
4280  {
4281  /*
4282  * No, so advance search_start, but not start_ptr/data_pos. (Thus,
4283  * we treat the matched text as if it weren't matched, and copy it
4284  * to the output later.)
4285  */
4286  search_start = pmatch[0].rm_eo;
4287  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4288  search_start++;
4289  continue;
4290  }
4291 
4292  /*
4293  * Copy the text to the left of the match position. Note we are given
4294  * character not byte indexes.
4295  */
4296  if (pmatch[0].rm_so - data_pos > 0)
4297  {
4298  int chunk_len;
4299 
4300  chunk_len = charlen_to_bytelen(start_ptr,
4301  pmatch[0].rm_so - data_pos);
4302  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4303 
4304  /*
4305  * Advance start_ptr over that text, to avoid multiple rescans of
4306  * it if the replace_text contains multiple back-references.
4307  */
4308  start_ptr += chunk_len;
4309  data_pos = pmatch[0].rm_so;
4310  }
4311 
4312  /*
4313  * Copy the replace_text, processing escapes if any are present.
4314  */
4315  if (escape_status > 0)
4317  start_ptr, data_pos);
4318  else
4320 
4321  /* Advance start_ptr and data_pos over the matched text. */
4322  start_ptr += charlen_to_bytelen(start_ptr,
4323  pmatch[0].rm_eo - data_pos);
4324  data_pos = pmatch[0].rm_eo;
4325 
4326  /*
4327  * If we only want to replace one occurrence, we're done.
4328  */
4329  if (n > 0)
4330  break;
4331 
4332  /*
4333  * Advance search position. Normally we start the next search at the
4334  * end of the previous match; but if the match was of zero length, we
4335  * have to advance by one character, or we'd just find the same match
4336  * again.
4337  */
4338  search_start = data_pos;
4339  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4340  search_start++;
4341  }
4342 
4343  /*
4344  * Copy the text to the right of the last match.
4345  */
4346  if (data_pos < data_len)
4347  {
4348  int chunk_len;
4349 
4350  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4351  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4352  }
4353 
4354  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4355  pfree(buf.data);
4356  pfree(data);
4357 
4358  return ret_text;
4359 }
#define lengthof(array)
Definition: c.h:772
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:987
const void * data
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define REG_NOMATCH
Definition: regex.h:138
#define REG_OKAY
Definition: regex.h:137
#define REG_NOSUB
Definition: regex.h:107
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:185
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
Definition: regex.h:56
static int check_replace_text_has_escape(const text *replace_text)
Definition: varlena.c:4071
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:4104

References appendBinaryStringInfo(), appendStringInfoRegexpSubstr(), appendStringInfoText(), buf, charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, check_replace_text_has_escape(), cstring_to_text_with_len(), data, ereport, errcode(), errmsg(), ERROR, initStringInfo(), lengthof, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regerror(), pg_regexec(), RE_compile_and_cache(), REG_NOMATCH, REG_NOSUB, REG_OKAY, replace_text(), regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by textregexreplace(), textregexreplace_extended(), and textregexreplace_noopt().

◆ rest_of_char_same()

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

Definition at line 6078 of file varlena.c.

6079 {
6080  while (len > 0)
6081  {
6082  len--;
6083  if (s1[len] != s2[len])
6084  return false;
6085  }
6086  return true;
6087 }

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4367 of file varlena.c.

4368 {
4369  text *inputstring = PG_GETARG_TEXT_PP(0);
4370  text *fldsep = PG_GETARG_TEXT_PP(1);
4371  int fldnum = PG_GETARG_INT32(2);
4372  int inputstring_len;
4373  int fldsep_len;
4375  char *start_ptr;
4376  char *end_ptr;
4377  text *result_text;
4378  bool found;
4379 
4380  /* field number is 1 based */
4381  if (fldnum == 0)
4382  ereport(ERROR,
4383  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4384  errmsg("field position must not be zero")));
4385 
4386  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4387  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4388 
4389  /* return empty string for empty input string */
4390  if (inputstring_len < 1)
4392 
4393  /* handle empty field separator */
4394  if (fldsep_len < 1)
4395  {
4396  /* if first or last field, return input string, else empty string */
4397  if (fldnum == 1 || fldnum == -1)
4398  PG_RETURN_TEXT_P(inputstring);
4399  else
4401  }
4402 
4403  /* find the first field separator */
4404  text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4405 
4406  found = text_position_next(&state);
4407 
4408  /* special case if fldsep not found at all */
4409  if (!found)
4410  {
4412  /* if first or last field, return input string, else empty string */
4413  if (fldnum == 1 || fldnum == -1)
4414  PG_RETURN_TEXT_P(inputstring);
4415  else
4417  }
4418 
4419  /*
4420  * take care of a negative field number (i.e. count from the right) by
4421  * converting to a positive field number; we need total number of fields
4422  */
4423  if (fldnum < 0)
4424  {
4425  /* we found a fldsep, so there are at least two fields */
4426  int numfields = 2;
4427 
4428  while (text_position_next(&state))
4429  numfields++;
4430 
4431  /* special case of last field does not require an extra pass */
4432  if (fldnum == -1)
4433  {
4434  start_ptr = text_position_get_match_ptr(&state) + fldsep_len;
4435  end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
4438  end_ptr - start_ptr));
4439  }
4440 
4441  /* else, convert fldnum to positive notation */
4442  fldnum += numfields + 1;
4443 
4444  /* if nonexistent field, return empty string */
4445  if (fldnum <= 0)
4446  {
4449  }
4450 
4451  /* reset to pointing at first match, but now with positive fldnum */
4453  found = text_position_next(&state);
4454  Assert(found);
4455  }
4456 
4457  /* identify bounds of first field */
4458  start_ptr = VARDATA_ANY(inputstring);
4459  end_ptr = text_position_get_match_ptr(&state);
4460 
4461  while (found && --fldnum > 0)
4462  {
4463  /* identify bounds of next field */
4464  start_ptr = end_ptr + fldsep_len;
4465  found = text_position_next(&state);
4466  if (found)
4467  end_ptr = text_position_get_match_ptr(&state);
4468  }
4469 
4471 
4472  if (fldnum > 0)
4473  {
4474  /* N'th field separator not found */
4475  /* if last field requested, return it, else empty string */
4476  if (fldnum == 1)
4477  {
4478  int last_len = start_ptr - VARDATA_ANY(inputstring);
4479 
4480  result_text = cstring_to_text_with_len(start_ptr,
4481  inputstring_len - last_len);
4482  }
4483  else
4484  result_text = cstring_to_text("");
4485  }
4486  else
4487  {
4488  /* non-last field requested */
4489  result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4490  }
4491 
4492  PG_RETURN_TEXT_P(result_text);
4493 }
static void text_position_reset(TextPositionState *state)
Definition: varlena.c:1492

References Assert(), cstring_to_text(), cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_reset(), text_position_setup(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

Definition at line 4590 of file varlena.c.

4591 {
4592  text *inputstring;
4593  text *fldsep;
4594  text *null_string;
4595  Oid collation = PG_GET_COLLATION();
4596  int inputstring_len;
4597  int fldsep_len;
4598  char *start_ptr;
4599  text *result_text;
4600 
4601  /* when input string is NULL, then result is NULL too */
4602  if (PG_ARGISNULL(0))
4603  return false;
4604 
4605  inputstring = PG_GETARG_TEXT_PP(0);
4606 
4607  /* fldsep can be NULL */
4608  if (!PG_ARGISNULL(1))
4609  fldsep = PG_GETARG_TEXT_PP(1);
4610  else
4611  fldsep = NULL;
4612 
4613  /* null_string can be NULL or omitted */
4614  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4615  null_string = PG_GETARG_TEXT_PP(2);
4616  else
4617  null_string = NULL;
4618 
4619  if (fldsep != NULL)
4620  {
4621  /*
4622  * Normal case with non-null fldsep. Use the text_position machinery
4623  * to search for occurrences of fldsep.
4624  */
4626 
4627  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4628  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4629 
4630  /* return empty set for empty input string */
4631  if (inputstring_len < 1)
4632  return true;
4633 
4634  /* empty field separator: return input string as a one-element set */
4635  if (fldsep_len < 1)
4636  {
4637  split_text_accum_result(tstate, inputstring,
4638  null_string, collation);
4639  return true;
4640  }
4641 
4642  text_position_setup(inputstring, fldsep, collation, &state);
4643 
4644  start_ptr = VARDATA_ANY(inputstring);
4645 
4646  for (;;)
4647  {
4648  bool found;
4649  char *end_ptr;
4650  int chunk_len;
4651 
4653 
4654  found = text_position_next(&state);
4655  if (!found)
4656  {
4657  /* fetch last field */
4658  chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4659  end_ptr = NULL; /* not used, but some compilers complain */
4660  }
4661  else
4662  {
4663  /* fetch non-last field */
4664  end_ptr = text_position_get_match_ptr(&state);
4665  chunk_len = end_ptr - start_ptr;
4666  }
4667 
4668  /* build a temp text datum to pass to split_text_accum_result */
4669  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4670 
4671  /* stash away this field */
4672  split_text_accum_result(tstate, result_text,
4673  null_string, collation);
4674 
4675  pfree(result_text);
4676 
4677  if (!found)
4678  break;
4679 
4680  start_ptr = end_ptr + fldsep_len;
4681  }
4682 
4684  }
4685  else
4686  {
4687  /*
4688  * When fldsep is NULL, each character in the input string becomes a
4689  * separate element in the result set. The separator is effectively
4690  * the space between characters.
4691  */
4692  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4693 
4694  start_ptr = VARDATA_ANY(inputstring);
4695 
4696  while (inputstring_len > 0)
4697  {
4698  int chunk_len = pg_mblen(start_ptr);
4699 
4701 
4702  /* build a temp text datum to pass to split_text_accum_result */
4703  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4704 
4705  /* stash away this field */
4706  split_text_accum_result(tstate, result_text,
4707  null_string, collation);
4708 
4709  pfree(result_text);
4710 
4711  start_ptr += chunk_len;
4712  inputstring_len -= chunk_len;
4713  }
4714  }
4715 
4716  return true;
4717 }
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition: varlena.c:4726

References CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), pfree(), PG_ARGISNULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, pg_mblen(), PG_NARGS, split_text_accum_result(), text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_setup(), VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by text_to_array(), and text_to_table().

◆ split_text_accum_result()

static void split_text_accum_result ( SplitTextOutputData tstate,
text field_value,
text null_string,
Oid  collation 
)
static

Definition at line 4726 of file varlena.c.

4730 {
4731  bool is_null = false;
4732 
4733  if (null_string && text_isequal(field_value, null_string, collation))
4734  is_null = true;
4735 
4736  if (tstate->tupstore)
4737  {
4738  Datum values[1];
4739  bool nulls[1];
4740 
4741  values[0] = PointerGetDatum(field_value);
4742  nulls[0] = is_null;
4743 
4745  tstate->tupdesc,
4746  values,
4747  nulls);
4748  }
4749  else
4750  {
4751  tstate->astate = accumArrayResult(tstate->astate,
4752  PointerGetDatum(field_value),
4753  is_null,
4754  TEXTOID,
4756  }
4757 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5319
static Datum values[MAXATTR]
Definition: bootstrap.c:156
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
TupleDesc tupdesc
Definition: varlena.c:105
ArrayBuildState * astate
Definition: varlena.c:103
Tuplestorestate * tupstore
Definition: varlena.c:104
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static bool text_isequal(text *txt1, text *txt2, Oid collid)
Definition: varlena.c:4499

References accumArrayResult(), SplitTextOutputData::astate, CurrentMemoryContext, PointerGetDatum(), text_isequal(), SplitTextOutputData::tupdesc, tuplestore_putvalues(), SplitTextOutputData::tupstore, and values.

Referenced by split_text().

◆ SplitDirectoriesString()

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

Definition at line 3582 of file varlena.c.

3584 {
3585  char *nextp = rawstring;
3586  bool done = false;
3587 
3588  *namelist = NIL;
3589 
3590  while (scanner_isspace(*nextp))
3591  nextp++; /* skip leading whitespace */
3592 
3593  if (*nextp == '\0')
3594  return true; /* allow empty string */
3595 
3596  /* At the top of the loop, we are at start of a new directory. */
3597  do
3598  {
3599  char *curname;
3600  char *endp;
3601 
3602  if (*nextp == '"')
3603  {
3604  /* Quoted name --- collapse quote-quote pairs */
3605  curname = nextp + 1;
3606  for (;;)
3607  {
3608  endp = strchr(nextp + 1, '"');
3609  if (endp == NULL)
3610  return false; /* mismatched quotes */
3611  if (endp[1] != '"')
3612  break; /* found end of quoted name */
3613  /* Collapse adjacent quotes into one quote, and look again */
3614  memmove(endp, endp + 1, strlen(endp));
3615  nextp = endp;
3616  }
3617  /* endp now points at the terminating quote */
3618  nextp = endp + 1;
3619  }
3620  else
3621  {
3622  /* Unquoted name --- extends to separator or end of string */
3623  curname = endp = nextp;
3624  while (*nextp && *nextp != separator)
3625  {
3626  /* trailing whitespace should not be included in name */
3627  if (!scanner_isspace(*nextp))
3628  endp = nextp + 1;
3629  nextp++;
3630  }
3631  if (curname == endp)
3632  return false; /* empty unquoted name not allowed */
3633  }
3634 
3635  while (scanner_isspace(*nextp))
3636  nextp++; /* skip trailing whitespace */
3637 
3638  if (*nextp == separator)
3639  {
3640  nextp++;
3641  while (scanner_isspace(*nextp))
3642  nextp++; /* skip leading whitespace for next */
3643  /* we expect another name, so done remains false */
3644  }
3645  else if (*nextp == '\0')
3646  done = true;
3647  else
3648  return false; /* invalid syntax */
3649 
3650  /* Now safe to overwrite separator with a null */
3651  *endp = '\0';
3652 
3653  /* Truncate path if it's overlength */
3654  if (strlen(curname) >= MAXPGPATH)
3655  curname[MAXPGPATH - 1] = '\0';
3656 
3657  /*
3658  * Finished isolating current name --- add it to list
3659  */
3660  curname = pstrdup(curname);
3661  canonicalize_path(curname);
3662  *namelist = lappend(*namelist, curname);
3663 
3664  /* Loop back if we didn't reach end of string */
3665  } while (!done);
3666 
3667  return true;
3668 }
List * lappend(List *list, void *datum)
Definition: list.c:338
char * pstrdup(const char *in)
Definition: mcxt.c:1624
#define MAXPGPATH
#define NIL
Definition: pg_list.h:68
void canonicalize_path(char *path)
Definition: path.c:264
bool scanner_isspace(char ch)
Definition: scansup.c:117

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

Referenced by load_libraries(), and PostmasterMain().

◆ SplitGUCList()

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

Definition at line 3703 of file varlena.c.

3705 {
3706  char *nextp = rawstring;
3707  bool done = false;
3708 
3709  *namelist = NIL;
3710 
3711  while (scanner_isspace(*nextp))
3712  nextp++; /* skip leading whitespace */
3713 
3714  if (*nextp == '\0')
3715  return true; /* allow empty string */
3716 
3717  /* At the top of the loop, we are at start of a new identifier. */
3718  do
3719  {
3720  char *curname;
3721  char *endp;
3722 
3723  if (*nextp == '"')
3724  {
3725  /* Quoted name --- collapse quote-quote pairs */
3726  curname = nextp + 1;
3727  for (;;)
3728  {
3729  endp = strchr(nextp + 1, '"');
3730  if (endp == NULL)
3731  return false; /* mismatched quotes */
3732  if (endp[1] != '"')
3733  break; /* found end of quoted name */
3734  /* Collapse adjacent quotes into one quote, and look again */
3735  memmove(endp, endp + 1, strlen(endp));
3736  nextp = endp;
3737  }
3738  /* endp now points at the terminating quote */
3739  nextp = endp + 1;
3740  }
3741  else
3742  {
3743  /* Unquoted name --- extends to separator or whitespace */
3744  curname = nextp;
3745  while (*nextp && *nextp != separator &&
3746  !scanner_isspace(*nextp))
3747  nextp++;
3748  endp = nextp;
3749  if (curname == nextp)
3750  return false; /* empty unquoted name not allowed */
3751  }
3752 
3753  while (scanner_isspace(*nextp))
3754  nextp++; /* skip trailing whitespace */
3755 
3756  if (*nextp == separator)
3757  {
3758  nextp++;
3759  while (scanner_isspace(*nextp))
3760  nextp++; /* skip leading whitespace for next */
3761  /* we expect another name, so done remains false */
3762  }
3763  else if (*nextp == '\0')
3764  done = true;
3765  else
3766  return false; /* invalid syntax */
3767 
3768  /* Now safe to overwrite separator with a null */
3769  *endp = '\0';
3770 
3771  /*
3772  * Finished isolating current name --- add it to list
3773  */
3774  *namelist = lappend(*namelist, curname);
3775 
3776  /* Loop back if we didn't reach end of string */
3777  } while (!done);
3778 
3779  return true;
3780 }

References lappend(), NIL, and scanner_isspace().

Referenced by dumpFunc(), parse_hba_auth_opt(), pg_get_functiondef(), and PostmasterMain().

◆ SplitIdentifierString()

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

Definition at line 3455 of file varlena.c.

3457 {
3458  char *nextp = rawstring;
3459  bool done = false;
3460 
3461  *namelist = NIL;
3462 
3463  while (scanner_isspace(*nextp))
3464  nextp++; /* skip leading whitespace */
3465 
3466  if (*nextp == '\0')
3467  return true; /* allow empty string */
3468 
3469  /* At the top of the loop, we are at start of a new identifier. */
3470  do
3471  {
3472  char *curname;
3473  char *endp;
3474 
3475  if (*nextp == '"')
3476  {
3477  /* Quoted name --- collapse quote-quote pairs, no downcasing */
3478  curname = nextp + 1;
3479  for (;;)
3480  {
3481  endp = strchr(nextp + 1, '"');
3482  if (endp == NULL)
3483  return false; /* mismatched quotes */
3484  if (endp[1] != '"')
3485  break; /* found end of quoted name */
3486  /* Collapse adjacent quotes into one quote, and look again */
3487  memmove(endp, endp + 1, strlen(endp));
3488  nextp = endp;
3489  }
3490  /* endp now points at the terminating quote */
3491  nextp = endp + 1;
3492  }
3493  else
3494  {
3495  /* Unquoted name --- extends to separator or whitespace */
3496  char *downname;
3497  int len;
3498 
3499  curname = nextp;
3500  while (*nextp && *nextp != separator &&
3501  !scanner_isspace(*nextp))
3502  nextp++;
3503  endp = nextp;
3504  if (curname == nextp)
3505  return false; /* empty unquoted name not allowed */
3506 
3507  /*
3508  * Downcase the identifier, using same code as main lexer does.
3509  *
3510  * XXX because we want to overwrite the input in-place, we cannot
3511  * support a downcasing transformation that increases the string
3512  * length. This is not a problem given the current implementation
3513  * of downcase_truncate_identifier, but we'll probably have to do
3514  * something about this someday.
3515  */
3516  len = endp - curname;
3517  downname = downcase_truncate_identifier(curname, len, false);
3518  Assert(strlen(downname) <= len);
3519  strncpy(curname, downname, len); /* strncpy is required here */
3520  pfree(downname);
3521  }
3522 
3523  while (scanner_isspace(*nextp))
3524  nextp++; /* skip trailing whitespace */
3525 
3526  if (*nextp == separator)
3527  {
3528  nextp++;
3529  while (scanner_isspace(*nextp))
3530  nextp++; /* skip leading whitespace for next */
3531  /* we expect another name, so done remains false */
3532  }
3533  else if (*nextp == '\0')
3534  done = true;
3535  else
3536  return false; /* invalid syntax */
3537 
3538  /* Now safe to overwrite separator with a null */
3539  *endp = '\0';
3540 
3541  /* Truncate name if it's overlength */
3542  truncate_identifier(curname, strlen(curname), false);
3543 
3544  /*
3545  * Finished isolating current name --- add it to list
3546  */
3547  *namelist = lappend(*namelist, curname);
3548 
3549  /* Loop back if we didn't reach end of string */
3550  } while (!done);
3551 
3552  return true;
3553 }
void truncate_identifier(char *ident, int len, bool warn)
Definition: scansup.c:93
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37

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

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

◆ string_agg_combine()

Datum string_agg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5165 of file varlena.c.

5166 {
5167  StringInfo state1;
5168  StringInfo state2;
5169  MemoryContext agg_context;
5170 
5171  if (!AggCheckCallContext(fcinfo, &agg_context))
5172  elog(ERROR, "aggregate function called in non-aggregate context");
5173 
5174  state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5175  state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
5176 
5177  if (state2 == NULL)
5178  {
5179  /*
5180  * NULL state2 is easy, just return state1, which we know is already
5181  * in the agg_context
5182  */
5183  if (state1 == NULL)
5184  PG_RETURN_NULL();
5185  PG_RETURN_POINTER(state1);
5186  }
5187 
5188  if (state1 == NULL)
5189  {
5190  /* We must copy state2's data into the agg_context */
5191  MemoryContext old_context;
5192 
5193  old_context = MemoryContextSwitchTo(agg_context);
5194  state1 = makeStringAggState(fcinfo);
5195  appendBinaryStringInfo(state1, state2->data, state2->len);
5196  state1->cursor = state2->cursor;
5197  MemoryContextSwitchTo(old_context);
5198  }
5199  else if (state2->len > 0)
5200  {
5201  /* Combine ... state1->cursor does not change in this case */
5202  appendBinaryStringInfo(state1, state2->data, state2->len);
5203  }
5204 
5205  PG_RETURN_POINTER(state1);
5206 }

References AggCheckCallContext(), appendBinaryStringInfo(), StringInfoData::cursor, StringInfoData::data, elog(), ERROR, StringInfoData::len, makeStringAggState(), MemoryContextSwitchTo(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_POINTER.

◆ string_agg_deserialize()

Datum string_agg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 5246 of file varlena.c.

5247 {
5248  bytea *sstate;
5249  StringInfo result;
5251  char *data;
5252  int datalen;
5253 
5254  /* cannot be called directly because of internal-type argument */
5255  Assert(AggCheckCallContext(fcinfo, NULL));
5256 
5257  sstate = PG_GETARG_BYTEA_PP(0);
5258 
5259  /*
5260  * Copy the bytea into a StringInfo so that we can "receive" it using the
5261  * standard recv-function infrastructure.
5262  */
5263  initStringInfo(&buf);
5265  VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5266 
5267  result = makeStringAggState(fcinfo);
5268 
5269  /* cursor */
5270  result->cursor = pq_getmsgint(&buf, 4);
5271 
5272  /* data */
5273  datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
5274  data = (char *) pq_getmsgbytes(&buf, datalen);
5275  appendBinaryStringInfo(result, data, datalen);
5276 
5277  pq_getmsgend(&buf);
5278  pfree(buf.data);
5279 
5280  PG_RETURN_POINTER(result);
5281 }
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:418
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:638
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:511

References AggCheckCallContext(), appendBinaryStringInfo(), Assert(), buf, StringInfoData::cursor, data, initStringInfo(), makeStringAggState(), pfree(), PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, pq_getmsgbytes(), pq_getmsgend(), pq_getmsgint(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 5284 of file varlena.c.

5285 {
5286  StringInfo state;
5287 
5288  /* cannot be called directly because of internal-type argument */
5289  Assert(AggCheckCallContext(fcinfo, NULL));
5290 
5291  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5292 
5293  if (state != NULL)
5294  {
5295  /* As per comment in transfn, strip data before the cursor position */
5297  state->len - state->cursor));
5298  }
5299  else
5300  PG_RETURN_NULL();
5301 }

References AggCheckCallContext(), Assert(), cstring_to_text_with_len(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ string_agg_serialize()

Datum string_agg_serialize ( PG_FUNCTION_ARGS  )

Definition at line 5215 of file varlena.c.

5216 {
5217  StringInfo state;
5219  bytea *result;
5220 
5221  /* cannot be called directly because of internal-type argument */
5222  Assert(AggCheckCallContext(fcinfo, NULL));
5223 
5225 
5226  pq_begintypsend(&buf);
5227 
5228  /* cursor */
5229  pq_sendint(&buf, state->cursor, 4);
5230 
5231  /* data */
5232  pq_sendbytes(&buf, state->data, state->len);
5233 
5234  result = pq_endtypsend(&buf);
5235 
5236  PG_RETURN_BYTEA_P(result);
5237 }
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendint(StringInfo buf, uint32 i, int b)
Definition: pqformat.h:172

References AggCheckCallContext(), Assert(), buf, PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbytes(), and pq_sendint().

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5110 of file varlena.c.

5111 {
5112  StringInfo state;
5113 
5114  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5115 
5116  /* Append the value unless null, preceding it with the delimiter. */
5117  if (!PG_ARGISNULL(1))
5118  {
5120  bool isfirst = false;
5121 
5122  /*
5123  * You might think we can just throw away the first delimiter, however
5124  * we must keep it as we may be a parallel worker doing partial
5125  * aggregation building a state to send to the main process. We need
5126  * to keep the delimiter of every aggregation so that the combine
5127  * function can properly join up the strings of two separately
5128  * partially aggregated results. The first delimiter is only stripped
5129  * off in the final function. To know how much to strip off the front
5130  * of the string, we store the length of the first delimiter in the
5131  * StringInfo's cursor field, which we don't otherwise need here.
5132  */
5133  if (state == NULL)
5134  {
5135  state = makeStringAggState(fcinfo);
5136  isfirst = true;
5137  }
5138 
5139  if (!PG_ARGISNULL(2))
5140  {
5141  text *delim = PG_GETARG_TEXT_PP(2);
5142 
5143  appendStringInfoText(state, delim);
5144  if (isfirst)
5145  state->cursor = VARSIZE_ANY_EXHDR(delim);
5146  }
5147 
5149  }
5150 
5151  /*
5152  * The transition type for string_agg() is declared to be "internal",
5153  * which is a pass-by-value type the same size as a pointer.
5154  */
5155  if (state)
5157  PG_RETURN_NULL();
5158 }

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

◆ text_catenate()

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

Definition at line 763 of file varlena.c.

764 {
765  text *result;
766  int len1,
767  len2,
768  len;
769  char *ptr;
770 
771  len1 = VARSIZE_ANY_EXHDR(t1);
772  len2 = VARSIZE_ANY_EXHDR(t2);
773 
774  /* paranoia ... probably should throw error instead? */
775  if (len1 < 0)
776  len1 = 0;
777  if (len2 < 0)
778  len2 = 0;
779 
780  len = len1 + len2 + VARHDRSZ;
781  result = (text *) palloc(len);
782 
783  /* Set size of result string... */
784  SET_VARSIZE(result, len);
785 
786  /* Fill data field of result string... */
787  ptr = VARDATA(result);
788  if (len1 > 0)
789  memcpy(ptr, VARDATA_ANY(t1), len1);
790  if (len2 > 0)
791  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
792 
793  return result;
794 }

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

Referenced by text_overlay(), and textcat().

◆ text_cmp()

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

Definition at line 1591 of file varlena.c.

1592 {
1593  char *a1p,
1594  *a2p;
1595  int len1,
1596  len2;
1597 
1598  a1p = VARDATA_ANY(arg1);
1599  a2p = VARDATA_ANY(arg2);
1600 
1601  len1 = VARSIZE_ANY_EXHDR(arg1);
1602  len2 = VARSIZE_ANY_EXHDR(arg2);
1603 
1604  return varstr_cmp(a1p, len1, a2p, len2, collid);
1605 }

References collid, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

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

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 5428 of file varlena.c.

5429 {
5430  text *result;
5431 
5432  result = concat_internal("", 0, fcinfo);
5433  if (result == NULL)
5434  PG_RETURN_NULL();
5435  PG_RETURN_TEXT_P(result);
5436 }
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5348

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5443 of file varlena.c.

5444 {
5445  char *sep;
5446  text *result;
5447 
5448  /* return NULL when separator is NULL */
5449  if (PG_ARGISNULL(0))
5450  PG_RETURN_NULL();
5452 
5453  result = concat_internal(sep, 1, fcinfo);
5454  if (result == NULL)
5455  PG_RETURN_NULL();
5456  PG_RETURN_TEXT_P(result);
5457 }

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

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 5564 of file varlena.c.

5565 {
5566  text *fmt;
5568  const char *cp;
5569  const char *start_ptr;
5570  const char *end_ptr;
5571  text *result;
5572  int arg;
5573  bool funcvariadic;
5574  int nargs;
5575  Datum *elements = NULL;
5576  bool *nulls = NULL;
5577  Oid element_type = InvalidOid;
5578  Oid prev_type = InvalidOid;
5579  Oid prev_width_type = InvalidOid;
5580  FmgrInfo typoutputfinfo;
5581  FmgrInfo typoutputinfo_width;
5582 
5583  /* When format string is null, immediately return null */
5584  if (PG_ARGISNULL(0))
5585  PG_RETURN_NULL();
5586 
5587  /* If argument is marked VARIADIC, expand array into elements */
5588  if (get_fn_expr_variadic(fcinfo->flinfo))
5589  {
5590  ArrayType *arr;
5591  int16 elmlen;
5592  bool elmbyval;
5593  char elmalign;
5594  int nitems;
5595 
5596  /* Should have just the one argument */
5597  Assert(PG_NARGS() == 2);
5598 
5599  /* If argument is NULL, we treat it as zero-length array */
5600  if (PG_ARGISNULL(1))
5601  nitems = 0;
5602  else
5603  {
5604  /*
5605  * Non-null argument had better be an array. We assume that any
5606  * call context that could let get_fn_expr_variadic return true
5607  * will have checked that a VARIADIC-labeled parameter actually is
5608  * an array. So it should be okay to just Assert that it's an
5609  * array rather than doing a full-fledged error check.
5610  */
5612 
5613  /* OK, safe to fetch the array value */
5614  arr = PG_GETARG_ARRAYTYPE_P(1);
5615 
5616  /* Get info about array element type */
5617  element_type = ARR_ELEMTYPE(arr);
5618  get_typlenbyvalalign(element_type,
5619  &elmlen, &elmbyval, &elmalign);
5620 
5621  /* Extract all array elements */
5622  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5623  &elements, &nulls, &nitems);
5624  }
5625 
5626  nargs = nitems + 1;
5627  funcvariadic = true;
5628  }
5629  else
5630  {
5631  /* Non-variadic case, we'll process the arguments individually */
5632  nargs = PG_NARGS();
5633  funcvariadic = false;
5634  }
5635 
5636  /* Setup for main loop. */
5637  fmt = PG_GETARG_TEXT_PP(0);
5638  start_ptr = VARDATA_ANY(fmt);
5639  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5640  initStringInfo(&str);
5641  arg = 1; /* next argument position to print */
5642 
5643  /* Scan format string, looking for conversion specifiers. */
5644  for (cp = start_ptr; cp < end_ptr; cp++)
5645  {
5646  int argpos;
5647  int widthpos;
5648  int flags;
5649  int width;
5650  Datum value;
5651  bool isNull;
5652  Oid typid;
5653 
5654  /*
5655  * If it's not the start of a conversion specifier, just copy it to
5656  * the output buffer.
5657  */
5658  if (*cp != '%')
5659  {
5661  continue;
5662  }
5663 
5664  ADVANCE_PARSE_POINTER(cp, end_ptr);
5665 
5666  /* Easy case: %% outputs a single % */
5667  if (*cp == '%')
5668  {
5670  continue;
5671  }
5672 
5673  /* Parse the optional portions of the format specifier */
5674  cp = text_format_parse_format(cp, end_ptr,
5675  &argpos, &widthpos,
5676  &flags, &width);
5677 
5678  /*
5679  * Next we should see the main conversion specifier. Whether or not
5680  * an argument position was present, it's known that at least one
5681  * character remains in the string at this point. Experience suggests
5682  * that it's worth checking that that character is one of the expected
5683  * ones before we try to fetch arguments, so as to produce the least
5684  * confusing response to a mis-formatted specifier.
5685  */
5686  if (strchr("sIL", *cp) == NULL)
5687  ereport(ERROR,
5688  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5689  errmsg("unrecognized format() type specifier \"%.*s\"",
5690  pg_mblen(cp), cp),
5691  errhint("For a single \"%%\" use \"%%%%\".")));
5692 
5693  /* If indirect width was specified, get its value */
5694  if (widthpos >= 0)
5695  {
5696  /* Collect the specified or next argument position */
5697  if (widthpos > 0)
5698  arg = widthpos;
5699  if (arg >= nargs)
5700  ereport(ERROR,
5701  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5702  errmsg("too few arguments for format()")));
5703 
5704  /* Get the value and type of the selected argument */
5705  if (!funcvariadic)
5706  {
5708  isNull = PG_ARGISNULL(arg);
5709  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5710  }
5711  else
5712  {
5713  value = elements[arg - 1];
5714  isNull = nulls[arg - 1];
5715  typid = element_type;
5716  }
5717  if (!OidIsValid(typid))
5718  elog(ERROR, "could not determine data type of format() input");
5719 
5720  arg++;
5721 
5722  /* We can treat NULL width the same as zero */
5723  if (isNull)
5724  width = 0;
5725  else if (typid == INT4OID)
5726  width = DatumGetInt32(value);
5727  else if (typid == INT2OID)
5728  width = DatumGetInt16(value);
5729  else
5730  {
5731  /* For less-usual datatypes, convert to text then to int */
5732  char *str;
5733 
5734  if (typid != prev_width_type)
5735  {
5736  Oid typoutputfunc;
5737  bool typIsVarlena;
5738 
5739  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5740  fmgr_info(typoutputfunc, &typoutputinfo_width);
5741  prev_width_type = typid;
5742  }
5743 
5744  str = OutputFunctionCall(&typoutputinfo_width, value);
5745 
5746  /* pg_strtoint32 will complain about bad data or overflow */
5747  width = pg_strtoint32(str);
5748 
5749  pfree(str);
5750  }
5751  }
5752 
5753  /* Collect the specified or next argument position */
5754  if (argpos > 0)
5755  arg = argpos;
5756  if (arg >= nargs)
5757  ereport(ERROR,
5758  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5759  errmsg("too few arguments for format()")));
5760 
5761  /* Get the value and type of the selected argument */
5762  if (!funcvariadic)
5763  {
5765  isNull = PG_ARGISNULL(arg);
5766  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5767  }
5768  else
5769  {
5770  value = elements[arg - 1];
5771  isNull = nulls[arg - 1];
5772  typid = element_type;
5773  }
5774  if (!OidIsValid(typid))
5775  elog(ERROR, "could not determine data type of format() input");
5776 
5777  arg++;
5778 
5779  /*
5780  * Get the appropriate typOutput function, reusing previous one if
5781  * same type as previous argument. That's particularly useful in the
5782  * variadic-array case, but often saves work even for ordinary calls.
5783  */
5784  if (typid != prev_type)
5785  {
5786  Oid typoutputfunc;
5787  bool typIsVarlena;
5788 
5789  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5790  fmgr_info(typoutputfunc, &typoutputfinfo);
5791  prev_type = typid;
5792  }
5793 
5794  /*
5795  * And now we can format the value.
5796  */
5797  switch (*cp)
5798  {
5799  case 's':
5800  case 'I':
5801  case 'L':
5802  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5803  value, isNull,
5804  flags, width);
5805  break;
5806  default:
5807  /* should not get here, because of previous check */
5808  ereport(ERROR,
5809  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5810  errmsg("unrecognized format() type specifier \"%.*s\"",
5811  pg_mblen(cp), cp),
5812  errhint("For a single \"%%\" use \"%%%%\".")));
5813  break;
5814  }
5815  }
5816 
5817  /* Don't need deconstruct_array results anymore. */
5818  if (elements != NULL)
5819  pfree(elements);
5820  if (nulls != NULL)
5821  pfree(nulls);
5822 
5823  /* Generate results. */
5824  result = cstring_to_text_with_len(str.data, str.len);
5825  pfree(str.data);
5826 
5827  PG_RETURN_TEXT_P(result);
5828 }
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3600
signed short int16
Definition: c.h:477
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
static void const char * fmt
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2229
int32 pg_strtoint32(const char *s)
Definition: numutils.c:291
void * arg
static int16 DatumGetInt16(Datum X)
Definition: postgres.h:162
#define InvalidOid
Definition: postgres_ext.h:36
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:5967
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:5890
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5551

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

Referenced by text_format_nv().

◆ text_format_append_string()

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

Definition at line 6016 of file varlena.c.

6018 {
6019  bool align_to_left = false;
6020  int len;
6021 
6022  /* fast path for typical easy case */
6023  if (width == 0)
6024  {
6026  return;
6027  }
6028 
6029  if (width < 0)
6030  {
6031  /* Negative width: implicit '-' flag, then take absolute value */
6032  align_to_left = true;
6033  /* -INT_MIN is undefined */
6034  if (width <= INT_MIN)
6035  ereport(ERROR,
6036  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6037  errmsg("number is out of range")));
6038  width = -width;
6039  }
6040  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6041  align_to_left = true;
6042 
6043  len = pg_mbstrlen(str);
6044  if (align_to_left)
6045  {
6046  /* left justify */
6048  if (len < width)
6049  appendStringInfoSpaces(buf, width - len);
6050  }
6051  else
6052  {
6053  /* right justify */
6054  if (len < width)
6055  appendStringInfoSpaces(buf, width - len);
6057  }
6058 }
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1038
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5549

References appendStringInfoSpaces(), appendStringInfoString(), buf, ereport, errcode(), errmsg(), ERROR, len, pg_mbstrlen(), generate_unaccent_rules::str, and TEXT_FORMAT_FLAG_MINUS.

Referenced by text_format_string_conversion().

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 6068 of file varlena.c.

6069 {
6070  return text_format(fcinfo);
6071 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5564

References text_format().

◆ text_format_parse_digits()

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

Definition at line 5841 of file varlena.c.

5842 {
5843  bool found = false;
5844  const char *cp = *ptr;
5845  int val = 0;
5846 
5847  while (*cp >= '0' && *cp <= '9')
5848  {
5849  int8 digit = (*cp - '0');
5850 
5851  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5852  unlikely(pg_add_s32_overflow(val, digit, &val)))
5853  ereport(ERROR,
5854  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5855  errmsg("number is out of range")));
5856  ADVANCE_PARSE_POINTER(cp, end_ptr);
5857  found = true;
5858  }
5859 
5860  *ptr = cp;
5861  *value = val;
5862 
5863  return found;
5864 }
signed char int8
Definition: c.h:476
#define unlikely(x)
Definition: c.h:295
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140

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

Referenced by text_format_parse_format().

◆ 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 5890 of file varlena.c.

5893 {
5894  const char *cp = start_ptr;
5895  int n;
5896 
5897  /* set defaults for output parameters */
5898  *argpos = -1;
5899  *widthpos = -1;
5900  *flags = 0;
5901  *width = 0;
5902 
5903  /* try to identify first number */
5904  if (text_format_parse_digits(&cp, end_ptr, &n))
5905  {
5906  if (*cp != '$')
5907  {
5908  /* Must be just a width and a type, so we're done */
5909  *width = n;
5910  return cp;
5911  }
5912  /* The number was argument position */
5913  *argpos = n;
5914  /* Explicit 0 for argument index is immediately refused */
5915  if (n == 0)
5916  ereport(ERROR,
5917  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5918  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5919  ADVANCE_PARSE_POINTER(cp, end_ptr);
5920  }
5921 
5922  /* Handle flags (only minus is supported now) */
5923  while (*cp == '-')
5924  {
5925  *flags |= TEXT_FORMAT_FLAG_MINUS;
5926  ADVANCE_PARSE_POINTER(cp, end_ptr);
5927  }
5928 
5929  if (*cp == '*')
5930  {
5931  /* Handle indirect width */
5932  ADVANCE_PARSE_POINTER(cp, end_ptr);
5933  if (text_format_parse_digits(&cp, end_ptr, &n))
5934  {
5935  /* number in this position must be closed by $ */
5936  if (*cp != '$')
5937  ereport(ERROR,
5938  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5939  errmsg("width argument position must be ended by \"$\"")));
5940  /* The number was width argument position */
5941  *widthpos = n;
5942  /* Explicit 0 for argument index is immediately refused */
5943  if (n == 0)
5944  ereport(ERROR,
5945  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5946  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5947  ADVANCE_PARSE_POINTER(cp, end_ptr);
5948  }
5949  else
5950  *widthpos = 0; /* width's argument position is unspecified */
5951  }
5952  else
5953  {
5954  /* Check for direct width specification */
5955  if (text_format_parse_digits(&cp, end_ptr, &n))
5956  *width = n;
5957  }
5958 
5959  /* cp should now be pointing at type character */
5960  return cp;
5961 }
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5841

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

Referenced by text_format().

◆ 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 5967 of file varlena.c.

5971 {
5972  char *str;
5973 
5974  /* Handle NULL arguments before trying to stringify the value. */
5975  if (isNull)
5976  {
5977  if (conversion == 's')
5978  text_format_append_string(buf, "", flags, width);
5979  else if (conversion == 'L')
5980  text_format_append_string(buf, "NULL", flags, width);
5981  else if (conversion == 'I')
5982  ereport(ERROR,
5983  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5984  errmsg("null values cannot be formatted as an SQL identifier")));
5985  return;
5986  }
5987 
5988  /* Stringify. */
5989  str = OutputFunctionCall(typOutputInfo, value);
5990 
5991  /* Escape. */
5992  if (conversion == 'I')
5993  {
5994  /* quote_identifier may or may not allocate a new string. */
5996  }
5997  else if (conversion == 'L')
5998  {
5999  char *qstr = quote_literal_cstr(str);
6000 
6001  text_format_append_string(buf, qstr, flags, width);
6002  /* quote_literal_cstr() always allocates a new string */
6003  pfree(qstr);
6004  }
6005  else
6006  text_format_append_string(buf, str, flags, width);
6007 
6008  /* Cleanup. */
6009  pfree(str);
6010 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11751
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6016

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

Referenced by text_format().

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1773 of file varlena.c.

1774 {
1775  text *arg1 = PG_GETARG_TEXT_PP(0);
1776  text *arg2 = PG_GETARG_TEXT_PP(1);
1777  bool result;
1778 
1779  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1780 
1781  PG_FREE_IF_COPY(arg1, 0);
1782  PG_FREE_IF_COPY(arg2, 1);
1783 
1784  PG_RETURN_BOOL(result);
1785 }

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

Referenced by gbt_textge().

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1758 of file varlena.c.

1759 {
1760  text *arg1 = PG_GETARG_TEXT_PP(0);
1761  text *arg2 = PG_GETARG_TEXT_PP(1);
1762  bool result;
1763 
1764  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1765 
1766  PG_FREE_IF_COPY(arg1, 0);
1767  PG_FREE_IF_COPY(arg2, 1);
1768 
1769  PG_RETURN_BOOL(result);
1770 }

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

Referenced by gbt_textgt().

◆ text_isequal()

static bool text_isequal ( text txt1,
text txt2,
Oid  collid 
)
static

Definition at line 4499 of file varlena.c.

4500 {
4502  collid,
4503  PointerGetDatum(txt1),
4504  PointerGetDatum(txt2)));
4505 }
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1616

References collid, DatumGetBool(), DirectFunctionCall2Coll(), PointerGetDatum(), and texteq().

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2569 of file varlena.c.

2570 {
2571  text *arg1 = PG_GETARG_TEXT_PP(0);
2572  text *arg2 = PG_GETARG_TEXT_PP(1);
2573  text *result;
2574 
2575  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2576 
2577  PG_RETURN_TEXT_P(result);
2578 }

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

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1743 of file varlena.c.

1744 {
1745  text *arg1 = PG_GETARG_TEXT_PP(0);
1746  text *arg2 = PG_GETARG_TEXT_PP(1);
1747  bool result;
1748 
1749  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1750 
1751  PG_FREE_IF_COPY(arg1, 0);
1752  PG_FREE_IF_COPY(arg2, 1);
1753 
1754  PG_RETURN_BOOL(result);
1755 }

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

Referenced by gbt_textle().

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 5464 of file varlena.c.

5465 {
5466  int n = PG_GETARG_INT32(1);
5467 
5468  if (n < 0)
5469  {
5470  text *str = PG_GETARG_TEXT_PP(0);
5471  const char *p = VARDATA_ANY(str);
5472  int len = VARSIZE_ANY_EXHDR(str);
5473  int rlen;
5474 
5475  n = pg_mbstrlen_with_len(p, len) + n;
5476  rlen = pg_mbcharcliplen(p, len, n);
5478  }
5479  else
5481 }
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1058
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1126
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:883

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

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 709 of file varlena.c.

710 {
711  /* fastpath when max encoding length is one */
714  else
715  {
716  text *t = DatumGetTextPP(str);
717 
719  VARSIZE_ANY_EXHDR(t)));
720  }
721 }
#define DatumGetTextPP(X)
Definition: fmgr.h:292

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

Referenced by textlen(), and textoverlay_no_len().

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1728 of file varlena.c.

1729 {
1730  text *arg1 = PG_GETARG_TEXT_PP(0);
1731  text *arg2 = PG_GETARG_TEXT_PP(1);
1732  bool result;
1733 
1734  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1735 
1736  PG_FREE_IF_COPY(arg1, 0);
1737  PG_FREE_IF_COPY(arg2, 1);
1738 
1739  PG_RETURN_BOOL(result);
1740 }

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

Referenced by gbt_textlt().

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3357 of file varlena.c.

3358 {
3359  text *s = PG_GETARG_TEXT_PP(0);
3360  Name result;
3361  int len;
3362 
3363  len = VARSIZE_ANY_EXHDR(s);
3364 
3365  /* Truncate oversize input */
3366  if (len >= NAMEDATALEN)
3368 
3369  /* We use palloc0 here to ensure result is zero-padded */
3370  result = (Name) palloc0(NAMEDATALEN);
3371  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3372 
3373  PG_RETURN_NAME(result);
3374 }
NameData * Name
Definition: c.h:728
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1084
void * palloc0(Size size)
Definition: mcxt.c:1241

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

◆ text_overlay()

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

Definition at line 1113 of file varlena.c.

1114 {
1115  text *result;
1116  text *s1;
1117  text *s2;
1118  int sp_pl_sl;
1119 
1120  /*
1121  * Check for possible integer-overflow cases. For negative sp, throw a
1122  * "substring length" error because that's what should be expected
1123  * according to the spec's definition of OVERLAY().
1124  */
1125  if (sp <= 0)
1126  ereport(ERROR,
1127  (errcode(ERRCODE_SUBSTRING_ERROR),
1128  errmsg("negative substring length not allowed")));
1129  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1130  ereport(ERROR,
1131  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1132  errmsg("integer out of range")));
1133 
1134  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1135  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1136  result = text_catenate(s1, t2);
1137  result = text_catenate(result, s2);
1138 
1139  return result;
1140 }
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:763

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

Referenced by textoverlay(), and textoverlay_no_len().

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 2849 of file varlena.c.

2850 {
2851  text *arg1 = PG_GETARG_TEXT_PP(0);
2852  text *arg2 = PG_GETARG_TEXT_PP(1);
2853  int result;
2854 
2855  result = internal_text_pattern_compare(arg1, arg2);
2856 
2857  PG_FREE_IF_COPY(arg1, 0);
2858  PG_FREE_IF_COPY(arg2, 1);
2859 
2860  PG_RETURN_BOOL(result >= 0);
2861 }

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

◆ text_pattern_gt()

Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 2865 of file varlena.c.

2866 {
2867  text *arg1 = PG_GETARG_TEXT_PP(0);
2868  text *arg2 = PG_GETARG_TEXT_PP(1);
2869  int result;
2870 
2871  result = internal_text_pattern_compare(arg1, arg2);
2872 
2873  PG_FREE_IF_COPY(arg1, 0);
2874  PG_FREE_IF_COPY(arg2, 1);
2875 
2876  PG_RETURN_BOOL(result > 0);
2877 }

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

◆ text_pattern_le()

Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 2833 of file varlena.c.

2834 {
2835  text *arg1 = PG_GETARG_TEXT_PP(0);
2836  text *arg2 = PG_GETARG_TEXT_PP(1);
2837  int result;
2838 
2839  result = internal_text_pattern_compare(arg1, arg2);
2840 
2841  PG_FREE_IF_COPY(arg1, 0);
2842  PG_FREE_IF_COPY(arg2, 1);
2843 
2844  PG_RETURN_BOOL(result <= 0);
2845 }

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

◆ text_pattern_lt()

Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 2817 of file varlena.c.

2818 {
2819  text *arg1 = PG_GETARG_TEXT_PP(0);
2820  text *arg2 = PG_GETARG_TEXT_PP(1);
2821  int result;
2822 
2823  result = internal_text_pattern_compare(arg1, arg2);
2824 
2825  PG_FREE_IF_COPY(arg1, 0);
2826  PG_FREE_IF_COPY(arg2, 1);
2827 
2828  PG_RETURN_BOOL(result < 0);
2829 }

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

◆ text_position()

static int text_position ( text t1,
text t2,
Oid  collid 
)
static

Definition at line 1173 of file varlena.c.

1174 {
1176  int result;
1177 
1178  /* Empty needle always matches at position 1 */
1179  if (VARSIZE_ANY_EXHDR(t2) < 1)
1180  return 1;
1181 
1182  /* Otherwise, can't match if haystack is shorter than needle */
1183  if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2))
1184  return 0;
1185 
1186  text_position_setup(t1, t2, collid, &state);
1187  if (!text_position_next(&state))
1188  result = 0;
1189  else
1192  return result;
1193 }
static int text_position_get_match_pos(TextPositionState *state)
Definition: varlena.c:1476

References collid, text_position_cleanup(), text_position_get_match_pos(), text_position_next(), text_position_setup(), and VARSIZE_ANY_EXHDR.

Referenced by textpos().

◆ text_position_cleanup()

static void text_position_cleanup ( TextPositionState state)
static

Definition at line 1500 of file varlena.c.

1501 {
1502  /* no cleanup needed */
1503 }

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

◆ text_position_get_match_pos()

static int text_position_get_match_pos ( TextPositionState state)
static

Definition at line 1476 of file varlena.c.

1477 {
1478  /* Convert the byte position to char position. */
1479  state->refpos += pg_mbstrlen_with_len(state->refpoint,
1480  state->last_match - state->refpoint);
1481  state->refpoint = state->last_match;
1482  return state->refpos + 1;
1483 }

References pg_mbstrlen_with_len().

Referenced by text_position().

◆ text_position_get_match_ptr()

static char * text_position_get_match_ptr ( TextPositionState state)
static

Definition at line 1465 of file varlena.c.

1466 {
1467  return state->last_match;
1468 }

Referenced by replace_text(), split_part(), and split_text().

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1333 of file varlena.c.

1334 {
1335  int needle_len = state->len2;
1336  char *start_ptr;
1337  char *matchptr;
1338 
1339  if (needle_len <= 0)
1340  return false; /* result for empty pattern */
1341 
1342  /* Start from the point right after the previous match. */
1343  if (state->last_match)
1344  start_ptr = state->last_match + needle_len;
1345  else
1346  start_ptr = state->str1;
1347 
1348 retry:
1349  matchptr = text_position_next_internal(start_ptr, state);
1350 
1351  if (!matchptr)
1352  return false;
1353 
1354  /*
1355  * Found a match for the byte sequence. If this is a multibyte encoding,
1356  * where one character's byte sequence can appear inside a longer
1357  * multi-byte character, we need to verify that the match was at a
1358  * character boundary, not in the middle of a multi-byte character.
1359  */
1360  if (state->is_multibyte_char_in_char)
1361  {
1362  /* Walk one character at a time, until we reach the match. */
1363 
1364  /* the search should never move backwards. */
1365  Assert(state->refpoint <= matchptr);
1366 
1367  while (state->refpoint < matchptr)
1368  {
1369  /* step to next character. */
1370  state->refpoint += pg_mblen(state->refpoint);
1371  state->refpos++;
1372 
1373  /*
1374  * If we stepped over the match's start position, then it was a
1375  * false positive, where the byte sequence appeared in the middle
1376  * of a multi-byte character. Skip it, and continue the search at
1377  * the next character boundary.
1378  */
1379  if (state->refpoint > matchptr)
1380  {
1381  start_ptr = state->refpoint;
1382  goto retry;
1383  }
1384  }
1385  }
1386 
1387  state->last_match = matchptr;
1388  return true;
1389 }
static char * text_position_next_internal(char *start_ptr, TextPositionState *state)
Definition: varlena.c:1397

References Assert(), pg_mblen(), and text_position_next_internal().

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

◆ text_position_next_internal()

static char * text_position_next_internal ( char *  start_ptr,
TextPositionState state 
)
static

Definition at line 1397 of file varlena.c.

1398 {
1399  int haystack_len = state->len1;
1400  int needle_len = state->len2;
1401  int skiptablemask = state->skiptablemask;
1402  const char *haystack = state->str1;
1403  const char *needle = state->str2;
1404  const char *haystack_end = &haystack[haystack_len];
1405  const char *hptr;
1406 
1407  Assert(start_ptr >= haystack && start_ptr <= haystack_end);
1408 
1409  if (needle_len == 1)
1410  {
1411  /* No point in using B-M-H for a one-character needle */
1412  char nchar = *needle;
1413 
1414  hptr = start_ptr;
1415  while (hptr < haystack_end)
1416  {
1417  if (*hptr == nchar)
1418  return (char *) hptr;
1419  hptr++;
1420  }
1421  }
1422  else
1423  {
1424  const char *needle_last = &needle[needle_len - 1];
1425 
1426  /* Start at startpos plus the length of the needle */
1427  hptr = start_ptr + needle_len - 1;
1428  while (hptr < haystack_end)
1429  {
1430  /* Match the needle scanning *backward* */
1431  const char *nptr;
1432  const char *p;
1433 
1434  nptr = needle_last;
1435  p = hptr;
1436  while (*nptr == *p)
1437  {
1438  /* Matched it all? If so, return 1-based position */
1439  if (nptr == needle)
1440  return (char *) p;
1441  nptr--, p--;
1442  }
1443 
1444  /*
1445  * No match, so use the haystack char at hptr to decide how far to
1446  * advance. If the needle had any occurrence of that character
1447  * (or more precisely, one sharing the same skiptable entry)
1448  * before its last character, then we advance far enough to align
1449  * the last such needle character with that haystack position.
1450  * Otherwise we can advance by the whole needle length.
1451  */
1452  hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1453  }
1454  }
1455 
1456  return 0; /* not found */
1457 }

References Assert().

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1492 of file varlena.c.

1493 {
1494  state->last_match = NULL;
1495  state->refpoint = state->str1;
1496  state->refpos = 0;
1497 }

Referenced by split_part().

◆ text_position_setup()

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

Definition at line 1213 of file varlena.c.

1214 {
1215  int len1 = VARSIZE_ANY_EXHDR(t1);
1216  int len2 = VARSIZE_ANY_EXHDR(t2);
1217  pg_locale_t mylocale = 0;
1218 
1220 
1221  if (!lc_collate_is_c(collid))
1222  mylocale = pg_newlocale_from_collation(collid);
1223 
1224  if (!pg_locale_deterministic(mylocale))
1225  ereport(ERROR,
1226  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1227  errmsg("nondeterministic collations are not supported for substring searches")));
1228 
1229  Assert(len1 > 0);
1230  Assert(len2 > 0);
1231 
1232  /*
1233  * Even with a multi-byte encoding, we perform the search using the raw
1234  * byte sequence, ignoring multibyte issues. For UTF-8, that works fine,
1235  * because in UTF-8 the byte sequence of one character cannot contain
1236  * another character. For other multi-byte encodings, we do the search
1237  * initially as a simple byte search, ignoring multibyte issues, but
1238  * verify afterwards that the match we found is at a character boundary,
1239  * and continue the search if it was a false match.
1240  */
1242  state->is_multibyte_char_in_char = false;
1243  else if (GetDatabaseEncoding() == PG_UTF8)
1244  state->is_multibyte_char_in_char = false;
1245  else
1246  state->is_multibyte_char_in_char = true;
1247 
1248  state->str1 = VARDATA_ANY(t1);
1249  state->str2 = VARDATA_ANY(t2);
1250  state->len1 = len1;
1251  state->len2 = len2;
1252  state->last_match = NULL;
1253  state->refpoint = state->str1;
1254  state->refpos = 0;
1255 
1256  /*
1257  * Prepare the skip table for Boyer-Moore-Horspool searching. In these
1258  * notes we use the terminology that the "haystack" is the string to be
1259  * searched (t1) and the "needle" is the pattern being sought (t2).
1260  *
1261  * If the needle is empty or bigger than the haystack then there is no
1262  * point in wasting cycles initializing the table. We also choose not to
1263  * use B-M-H for needles of length 1, since the skip table can't possibly
1264  * save anything in that case.
1265  */
1266  if (len1 >= len2 && len2 > 1)
1267  {
1268  int searchlength = len1 - len2;
1269  int skiptablemask;
1270  int last;
1271  int i;
1272  const char *str2 = state->str2;
1273 
1274  /*
1275  * First we must determine how much of the skip table to use. The
1276  * declaration of TextPositionState allows up to 256 elements, but for
1277  * short search problems we don't really want to have to initialize so
1278  * many elements --- it would take too long in comparison to the
1279  * actual search time. So we choose a useful skip table size based on
1280  * the haystack length minus the needle length. The closer the needle
1281  * length is to the haystack length the less useful skipping becomes.
1282  *
1283  * Note: since we use bit-masking to select table elements, the skip
1284  * table size MUST be a power of 2, and so the mask must be 2^N-1.
1285  */
1286  if (searchlength < 16)
1287  skiptablemask = 3;
1288  else if (searchlength < 64)
1289  skiptablemask = 7;
1290  else if (searchlength < 128)
1291  skiptablemask = 15;
1292  else if (searchlength < 512)
1293  skiptablemask = 31;
1294  else if (searchlength < 2048)
1295  skiptablemask = 63;
1296  else if (searchlength < 4096)
1297  skiptablemask = 127;
1298  else
1299  skiptablemask = 255;
1300  state->skiptablemask = skiptablemask;
1301 
1302  /*
1303  * Initialize the skip table. We set all elements to the needle
1304  * length, since this is the correct skip distance for any character
1305  * not found in the needle.
1306  */
1307  for (i = 0; i <= skiptablemask; i++)
1308  state->skiptable[i] = len2;
1309 
1310  /*
1311  * Now examine the needle. For each character except the last one,
1312  * set the corresponding table element to the appropriate skip
1313  * distance. Note that when two characters share the same skip table
1314  * entry, the one later in the needle must determine the skip
1315  * distance.
1316  */
1317  last = len2 - 1;
1318 
1319  for (i = 0; i < last; i++)
1320  state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1321  }
1322 }
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1537
bool pg_locale_deterministic(pg_locale_t locale)
Definition: pg_locale.c:1513
@ PG_UTF8
Definition: pg_wchar.h:232

References Assert(), check_collation_set(), collid, ereport, errcode(), errmsg(), ERROR, GetDatabaseEncoding(), i, lc_collate_is_c(), pg_database_encoding_max_length(), pg_locale_deterministic(), pg_newlocale_from_collation(), PG_UTF8, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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

◆ text_reverse()

Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 5509 of file varlena.c.

5510 {
5511  text *str = PG_GETARG_TEXT_PP(0);
5512  const char *p = VARDATA_ANY(str);
5513  int len = VARSIZE_ANY_EXHDR(str);
5514  const char *endp = p + len;
5515  text *result;
5516  char *dst;
5517 
5518  result = palloc(len + VARHDRSZ);
5519  dst = (char *) VARDATA(result) + len;
5520  SET_VARSIZE(result, len + VARHDRSZ);
5521 
5523  {
5524  /* multibyte version */
5525  while (p < endp)
5526  {
5527  int sz;
5528 
5529  sz = pg_mblen(p);
5530  dst -= sz;
5531  memcpy(dst, p, sz);
5532  p += sz;
5533  }
5534  }
5535  else
5536  {
5537  /* single byte version */
5538  while (p < endp)
5539  *(--dst) = *p++;
5540  }
5541 
5542  PG_RETURN_TEXT_P(result);
5543 }

References len, 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.

◆ text_right()

Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 5488 of file varlena.c.

5489 {
5490  text *str = PG_GETARG_TEXT_PP(0);
5491  const char *p = VARDATA_ANY(str);
5492  int len = VARSIZE_ANY_EXHDR(str);
5493  int n = PG_GETARG_INT32(1);
5494  int off;
5495 
5496  if (n < 0)
5497  n = -n;
5498  else
5499  n = pg_mbstrlen_with_len(p, len) - n;
5500  off = pg_mbcharcliplen(p, len, n);
5501 
5503 }

References cstring_to_text_with_len(), 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.

◆ text_smaller()

Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2581 of file varlena.c.

2582 {
2583  text *arg1 = PG_GETARG_TEXT_PP(0);
2584  text *arg2 = PG_GETARG_TEXT_PP(1);
2585  text *result;
2586 
2587  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2588 
2589  PG_RETURN_TEXT_P(result);
2590 }

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

◆ text_starts_with()

Datum text_starts_with ( PG_FUNCTION_ARGS  )

Definition at line 1788 of file varlena.c.

1789 {
1790  Datum arg1 = PG_GETARG_DATUM(0);
1791  Datum arg2 = PG_GETARG_DATUM(1);
1793  pg_locale_t mylocale = 0;
1794  bool result;
1795  Size len1,
1796  len2;
1797 
1799 
1800  if (!lc_collate_is_c(collid))
1801  mylocale = pg_newlocale_from_collation(collid);
1802 
1803  if (!pg_locale_deterministic(mylocale))
1804  ereport(ERROR,
1805  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1806  errmsg("nondeterministic collations are not supported for substring searches")));
1807 
1808  len1 = toast_raw_datum_size(arg1);
1809  len2 = toast_raw_datum_size(arg2);
1810  if (len2 > len1)
1811  result = false;
1812  else
1813  {
1814  text *targ1 = text_substring(arg1, 1, len2, false);
1815  text *targ2 = DatumGetTextPP(arg2);
1816 
1817  result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1818  VARSIZE_ANY_EXHDR(targ2)) == 0);
1819 
1820  PG_FREE_IF_COPY(targ1, 0);
1821  PG_FREE_IF_COPY(targ2, 1);
1822  }
1823 
1824  PG_RETURN_BOOL(result);
1825 }

References check_collation_set(), collid, DatumGetTextPP, ereport, errcode(), errmsg(), ERROR, lc_collate_is_c(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_DATUM, pg_locale_deterministic(), pg_newlocale_from_collation(), PG_RETURN_BOOL, text_substring(), toast_raw_datum_size(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by spg_text_leaf_consistent().

◆ text_substr()

◆ text_substr_no_len()

Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 864 of file varlena.c.

865 {
867  PG_GETARG_INT32(1),
868  -1, true));
869 }

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

◆ text_substring()

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

Definition at line 883 of file varlena.c.

884 {
886  int32 S = start; /* start position */
887  int32 S1; /* adjusted start position */
888  int32 L1; /* adjusted substring length */
889  int32 E; /* end position */
890 
891  /*
892  * SQL99 says S can be zero or negative, but we still must fetch from the
893  * start of the string.
894  */
895  S1 = Max(S, 1);
896 
897  /* life is easy if the encoding max length is 1 */
898  if (eml == 1)
899  {
900  if (length_not_specified) /* special case - get length to end of
901  * string */
902  L1 = -1;
903  else if (length < 0)
904  {
905  /* SQL99 says to throw an error for E < S, i.e., negative length */
906  ereport(ERROR,
907  (errcode(ERRCODE_SUBSTRING_ERROR),
908  errmsg("negative substring length not allowed")));
909  L1 = -1; /* silence stupider compilers */
910  }
911  else if (pg_add_s32_overflow(S, length, &E))
912  {
913  /*
914  * L could be large enough for S + L to overflow, in which case
915  * the substring must run to end of string.
916  */
917  L1 = -1;
918  }
919  else
920  {
921  /*
922  * A zero or negative value for the end position can happen if the
923  * start was negative or one. SQL99 says to return a zero-length
924  * string.
925  */
926  if (E < 1)
927  return cstring_to_text("");
928 
929  L1 = E - S1;
930  }
931 
932  /*
933  * If the start position is past the end of the string, SQL99 says to
934  * return a zero-length string -- DatumGetTextPSlice() will do that
935  * for us. We need only convert S1 to zero-based starting position.
936  */
937  return DatumGetTextPSlice(str, S1 - 1, L1);
938  }
939  else if (eml > 1)
940  {
941  /*
942  * When encoding max length is > 1, we can't get LC without
943  * detoasting, so we'll grab a conservatively large slice now and go
944  * back later to do the right thing
945  */
946  int32 slice_star