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_category.h"
#include "common/unicode_norm.h"
#include "common/unicode_version.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 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)
 
static textconvert_to_base (uint64 value, int base)
 
Datum to_bin32 (PG_FUNCTION_ARGS)
 
Datum to_bin64 (PG_FUNCTION_ARGS)
 
Datum to_oct32 (PG_FUNCTION_ARGS)
 
Datum to_oct64 (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 pg_column_toast_chunk_id (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_version (PG_FUNCTION_ARGS)
 
Datum icu_unicode_version (PG_FUNCTION_ARGS)
 
Datum unicode_assigned (PG_FUNCTION_ARGS)
 
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:1317
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39

Definition at line 5625 of file varlena.c.

◆ CmpCall

#define CmpCall (   cmpfunc)
Value:
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:812
#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 2731 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 116 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 117 of file varlena.c.

◆ DIG

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

Definition at line 276 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6165 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 2986 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

Definition at line 5623 of file varlena.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 114 of file varlena.c.

◆ VAL

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

Definition at line 275 of file varlena.c.

Typedef Documentation

◆ VarString

typedef struct varlena VarString

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

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

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, charlen_to_bytelen(), idx(), replace_text(), regmatch_t::rm_eo, regmatch_t::rm_so, 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 4766 of file varlena.c.

4767 {
4769  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4770 
4771  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4772 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
#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:217
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4808

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

4810 {
4811  text *result;
4812  int nitems,
4813  *dims,
4814  ndims;
4815  Oid element_type;
4816  int typlen;
4817  bool typbyval;
4818  char typalign;
4820  bool printed = false;
4821  char *p;
4822  bits8 *bitmap;
4823  int bitmask;
4824  int i;
4825  ArrayMetaState *my_extra;
4826 
4827  ndims = ARR_NDIM(v);
4828  dims = ARR_DIMS(v);
4829  nitems = ArrayGetNItems(ndims, dims);
4830 
4831  /* if there are no elements, return an empty string */
4832  if (nitems == 0)
4833  return cstring_to_text_with_len("", 0);
4834 
4835  element_type = ARR_ELEMTYPE(v);
4836  initStringInfo(&buf);
4837 
4838  /*
4839  * We arrange to look up info about element type, including its output
4840  * conversion proc, only once per series of calls, assuming the element
4841  * type doesn't change underneath us.
4842  */
4843  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4844  if (my_extra == NULL)
4845  {
4846  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4847  sizeof(ArrayMetaState));
4848  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4849  my_extra->element_type = ~element_type;
4850  }
4851 
4852  if (my_extra->element_type != element_type)
4853  {
4854  /*
4855  * Get info about element type, including its output conversion proc
4856  */
4857  get_type_io_data(element_type, IOFunc_output,
4858  &my_extra->typlen, &my_extra->typbyval,
4859  &my_extra->typalign, &my_extra->typdelim,
4860  &my_extra->typioparam, &my_extra->typiofunc);
4861  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4862  fcinfo->flinfo->fn_mcxt);
4863  my_extra->element_type = element_type;
4864  }
4865  typlen = my_extra->typlen;
4866  typbyval = my_extra->typbyval;
4867  typalign = my_extra->typalign;
4868 
4869  p = ARR_DATA_PTR(v);
4870  bitmap = ARR_NULLBITMAP(v);
4871  bitmask = 1;
4872 
4873  for (i = 0; i < nitems; i++)
4874  {
4875  Datum itemvalue;
4876  char *value;
4877 
4878  /* Get source element, checking for NULL */
4879  if (bitmap && (*bitmap & bitmask) == 0)
4880  {
4881  /* if null_string is NULL, we just ignore null elements */
4882  if (null_string != NULL)
4883  {
4884  if (printed)
4885  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4886  else
4887  appendStringInfoString(&buf, null_string);
4888  printed = true;
4889  }
4890  }
4891  else
4892  {
4893  itemvalue = fetch_att(p, typbyval, typlen);
4894 
4895  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4896 
4897  if (printed)
4898  appendStringInfo(&buf, "%s%s", fldsep, value);
4899  else
4901  printed = true;
4902 
4903  p = att_addlength_pointer(p, typlen, p);
4904  p = (char *) att_align_nominal(p, typalign);
4905  }
4906 
4907  /* advance bitmap pointer if any */
4908  if (bitmap)
4909  {
4910  bitmask <<= 1;
4911  if (bitmask == 0x100)
4912  {
4913  bitmap++;
4914  bitmask = 1;
4915  }
4916  }
4917  }
4918 
4919  result = cstring_to_text_with_len(buf.data, buf.len);
4920  pfree(buf.data);
4921 
4922  return result;
4923 }
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define ARR_NULLBITMAP(a)
Definition: array.h:300
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:57
uint8 bits8
Definition: c.h:513
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1683
#define nitems(x)
Definition: indent.h:31
static struct @155 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:2325
@ IOFunc_output
Definition: lsyscache.h:36
void pfree(void *pointer)
Definition: mcxt.c:1520
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
static char * buf
Definition: pg_test_fsync.c:73
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:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Oid typioparam
Definition: array.h:243
char typalign
Definition: array.h:241
Oid typiofunc
Definition: array.h:244
int16 typlen
Definition: array.h:239
Oid element_type
Definition: array.h:238
FmgrInfo proc
Definition: array.h:245
char typdelim
Definition: array.h:242
bool typbyval
Definition: array.h:240
void * fn_extra
Definition: fmgr.h:64
MemoryContext fn_mcxt
Definition: fmgr.h:65
FmgrInfo * flinfo
Definition: fmgr.h:87
Definition: c.h:687
#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:196

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

4783 {
4784  ArrayType *v;
4785  char *fldsep;
4786  char *null_string;
4787 
4788  /* returns NULL when first or second parameter is NULL */
4789  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4790  PG_RETURN_NULL();
4791 
4792  v = PG_GETARG_ARRAYTYPE_P(0);
4793  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4794 
4795  /* NULL null string is passed through as a null pointer */
4796  if (!PG_ARGISNULL(2))
4797  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4798  else
4799  null_string = NULL;
4800 
4801  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4802 }
#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 2049 of file varlena.c.

2050 {
2051  BpChar *arg1 = DatumGetBpCharPP(x);
2052  BpChar *arg2 = DatumGetBpCharPP(y);
2053  char *a1p,
2054  *a2p;
2055  int len1,
2056  len2,
2057  result;
2058 
2059  a1p = VARDATA_ANY(arg1);
2060  a2p = VARDATA_ANY(arg2);
2061 
2062  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2063  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2064 
2065  result = memcmp(a1p, a2p, Min(len1, len2));
2066  if ((result == 0) && (len1 != len2))
2067  result = (len1 < len2) ? -1 : 1;
2068 
2069  /* We can't afford to leak memory here. */
2070  if (PointerGetDatum(arg1) != x)
2071  pfree(arg1);
2072  if (PointerGetDatum(arg2) != y)
2073  pfree(arg2);
2074 
2075  return result;
2076 }
#define Min(x, y)
Definition: c.h:1004
#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:676

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

2701 {
2702  Name arg1 = PG_GETARG_NAME(0);
2703  text *arg2 = PG_GETARG_TEXT_PP(1);
2704  int32 result;
2705 
2706  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2707  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2708  PG_GET_COLLATION());
2709 
2710  PG_FREE_IF_COPY(arg2, 1);
2711 
2712  PG_RETURN_INT32(result);
2713 }
#define NameStr(name)
Definition: c.h:746
signed int int32
Definition: c.h:494
#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:741
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1539

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

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

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

2900 {
2902  MemoryContext oldcontext;
2903 
2904  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2905 
2906  /* Use generic string SortSupport, forcing "C" collation */
2907  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
2908 
2909  MemoryContextSwitchTo(oldcontext);
2910 
2911  PG_RETURN_VOID();
2912 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
MemoryContextSwitchTo(old_ctx)
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:1873

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1831 of file varlena.c.

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

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

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

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

1847 {
1849  Oid collid = ssup->ssup_collation;
1850  MemoryContext oldcontext;
1851 
1852  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1853 
1854  /* Use generic string SortSupport */
1855  varstr_sortsupport(ssup, TEXTOID, collid);
1856 
1857  MemoryContextSwitchTo(oldcontext);
1858 
1859  PG_RETURN_VOID();
1860 }
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 2555 of file varlena.c.

2556 {
2557  /* Oid opcintype = PG_GETARG_OID(0); */
2559 
2561 
2562  if (lc_collate_is_c(collid) ||
2563  collid == DEFAULT_COLLATION_OID ||
2565  PG_RETURN_BOOL(true);
2566  else
2567  PG_RETURN_BOOL(false);
2568 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1054
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1317
static void check_collation_set(Oid collid)
Definition: varlena.c:1510

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

5385 {
5386  FmgrInfo *foutcache;
5387  int i;
5388 
5389  /* We keep the info in fn_mcxt so it survives across calls */
5390  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5391  PG_NARGS() * sizeof(FmgrInfo));
5392 
5393  for (i = argidx; i < PG_NARGS(); i++)
5394  {
5395  Oid valtype;
5396  Oid typOutput;
5397  bool typIsVarlena;
5398 
5399  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5400  if (!OidIsValid(valtype))
5401  elog(ERROR, "could not determine data type of concat() input");
5402 
5403  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5404  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5405  }
5406 
5407  fcinfo->flinfo->fn_extra = foutcache;
5408 
5409  return foutcache;
5410 }
#define OidIsValid(objectId)
Definition: c.h:775
#define elog(elevel,...)
Definition: elog.h:224
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1910
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:203
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2907
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 3151 of file varlena.c.

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

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

2954 {
2955  bytea *result;
2956  int len1,
2957  len2,
2958  len;
2959  char *ptr;
2960 
2961  len1 = VARSIZE_ANY_EXHDR(t1);
2962  len2 = VARSIZE_ANY_EXHDR(t2);
2963 
2964  /* paranoia ... probably should throw error instead? */
2965  if (len1 < 0)
2966  len1 = 0;
2967  if (len2 < 0)
2968  len2 = 0;
2969 
2970  len = len1 + len2 + VARHDRSZ;
2971  result = (bytea *) palloc(len);
2972 
2973  /* Set size of result string... */
2974  SET_VARSIZE(result, len);
2975 
2976  /* Fill data field of result string... */
2977  ptr = VARDATA(result);
2978  if (len1 > 0)
2979  memcpy(ptr, VARDATA_ANY(t1), len1);
2980  if (len2 > 0)
2981  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2982 
2983  return result;
2984 }
#define VARHDRSZ
Definition: c.h:692
void * palloc(Size size)
Definition: mcxt.c:1316
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 3118 of file varlena.c.

3119 {
3120  bytea *result;
3121  bytea *s1;
3122  bytea *s2;
3123  int sp_pl_sl;
3124 
3125  /*
3126  * Check for possible integer-overflow cases. For negative sp, throw a
3127  * "substring length" error because that's what should be expected
3128  * according to the spec's definition of OVERLAY().
3129  */
3130  if (sp <= 0)
3131  ereport(ERROR,
3132  (errcode(ERRCODE_SUBSTRING_ERROR),
3133  errmsg("negative substring length not allowed")));
3134  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3135  ereport(ERROR,
3136  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3137  errmsg("integer out of range")));
3138 
3139  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3140  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3141  result = bytea_catenate(s1, t2);
3142  result = bytea_catenate(result, s2);
3143 
3144  return result;
3145 }
#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:3028
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2953

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

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

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

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

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

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

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

3006 {
3008  PG_GETARG_INT32(1),
3009  PG_GETARG_INT32(2),
3010  false));
3011 }
#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 3019 of file varlena.c.

3020 {
3022  PG_GETARG_INT32(1),
3023  -1,
3024  true));
3025 }

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

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

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

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

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2938 of file varlena.c.

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

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3938 of file varlena.c.

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

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

3795 {
3796  Datum arg1 = PG_GETARG_DATUM(0);
3797  Datum arg2 = PG_GETARG_DATUM(1);
3798  bool result;
3799  Size len1,
3800  len2;
3801 
3802  /*
3803  * We can use a fast path for unequal lengths, which might save us from
3804  * having to detoast one or both values.
3805  */
3806  len1 = toast_raw_datum_size(arg1);
3807  len2 = toast_raw_datum_size(arg2);
3808  if (len1 != len2)
3809  result = false;
3810  else
3811  {
3812  bytea *barg1 = DatumGetByteaPP(arg1);
3813  bytea *barg2 = DatumGetByteaPP(arg2);
3814 
3815  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3816  len1 - VARHDRSZ) == 0);
3817 
3818  PG_FREE_IF_COPY(barg1, 0);
3819  PG_FREE_IF_COPY(barg2, 1);
3820  }
3821 
3822  PG_RETURN_BOOL(result);
3823 }
size_t Size
Definition: c.h:605
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 3918 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 388 of file varlena.c.

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

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

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

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

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

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

3166 {
3167  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3168  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3169  int pos;
3170  int px,
3171  p;
3172  int len1,
3173  len2;
3174  char *p1,
3175  *p2;
3176 
3177  len1 = VARSIZE_ANY_EXHDR(t1);
3178  len2 = VARSIZE_ANY_EXHDR(t2);
3179 
3180  if (len2 <= 0)
3181  PG_RETURN_INT32(1); /* result for empty pattern */
3182 
3183  p1 = VARDATA_ANY(t1);
3184  p2 = VARDATA_ANY(t2);
3185 
3186  pos = 0;
3187  px = (len1 - len2);
3188  for (p = 0; p <= px; p++)
3189  {
3190  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3191  {
3192  pos = p + 1;
3193  break;
3194  };
3195  p1++;
3196  };
3197 
3198  PG_RETURN_INT32(pos);
3199 }
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 471 of file varlena.c.

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

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

491 {
492  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
493 
494  PG_RETURN_BYTEA_P(vlena);
495 }
#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 3308 of file varlena.c.

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

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

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

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

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

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

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

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

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

5424 {
5425  text *result;
5427  FmgrInfo *foutcache;
5428  bool first_arg = true;
5429  int i;
5430 
5431  /*
5432  * concat(VARIADIC some-array) is essentially equivalent to
5433  * array_to_text(), ie concat the array elements with the given separator.
5434  * So we just pass the case off to that code.
5435  */
5436  if (get_fn_expr_variadic(fcinfo->flinfo))
5437  {
5438  ArrayType *arr;
5439 
5440  /* Should have just the one argument */
5441  Assert(argidx == PG_NARGS() - 1);
5442 
5443  /* concat(VARIADIC NULL) is defined as NULL */
5444  if (PG_ARGISNULL(argidx))
5445  return NULL;
5446 
5447  /*
5448  * Non-null argument had better be an array. We assume that any call
5449  * context that could let get_fn_expr_variadic return true will have
5450  * checked that a VARIADIC-labeled parameter actually is an array. So
5451  * it should be okay to just Assert that it's an array rather than
5452  * doing a full-fledged error check.
5453  */
5455 
5456  /* OK, safe to fetch the array value */
5457  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5458 
5459  /*
5460  * And serialize the array. We tell array_to_text to ignore null
5461  * elements, which matches the behavior of the loop below.
5462  */
5463  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5464  }
5465 
5466  /* Normal case without explicit VARIADIC marker */
5467  initStringInfo(&str);
5468 
5469  /* Get output function info, building it if first time through */
5470  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5471  if (foutcache == NULL)
5472  foutcache = build_concat_foutcache(fcinfo, argidx);
5473 
5474  for (i = argidx; i < PG_NARGS(); i++)
5475  {
5476  if (!PG_ARGISNULL(i))
5477  {
5479 
5480  /* add separator if appropriate */
5481  if (first_arg)
5482  first_arg = false;
5483  else
5484  appendStringInfoString(&str, sepstr);
5485 
5486  /* call the appropriate type output function, append the result */
5488  OutputFunctionCall(&foutcache[i], value));
5489  }
5490  }
5491 
5492  result = cstring_to_text_with_len(str.data, str.len);
5493  pfree(str.data);
5494 
5495  return result;
5496 }
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2044
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2832
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5384

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, str, and value.

Referenced by text_concat(), and text_concat_ws().

◆ convert_to_base()

static text* convert_to_base ( uint64  value,
int  base 
)
inlinestatic

Definition at line 4930 of file varlena.c.

4931 {
4932  const char *digits = "0123456789abcdef";
4933 
4934  /* We size the buffer for to_bin's longest possible return value. */
4935  char buf[sizeof(uint64) * BITS_PER_BYTE];
4936  char *const end = buf + sizeof(buf);
4937  char *ptr = end;
4938 
4939  Assert(base > 1);
4940  Assert(base <= 16);
4941 
4942  do
4943  {
4944  *--ptr = digits[value % base];
4945  value /= base;
4946  } while (ptr > buf && value);
4947 
4948  return cstring_to_text_with_len(ptr, end - ptr);
4949 }
int digits
Definition: informix.c:672
#define BITS_PER_BYTE

References Assert, BITS_PER_BYTE, buf, cstring_to_text_with_len(), digits, and value.

Referenced by to_bin32(), to_bin64(), to_hex32(), to_hex64(), to_oct32(), and to_oct64().

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 184 of file varlena.c.

185 {
186  return cstring_to_text_with_len(s, strlen(s));
187 }

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(), icu_unicode_version(), 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(), timetz_at_local(), tsquerytree(), unaccent_dict(), unicode_version(), 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 196 of file varlena.c.

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

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(), convert_to_base(), cstring_to_text(), daitch_mokotoff_coding(), datum_to_json(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), ExecEvalMergeSupportFunc(), ExecEvalXmlExpr(), executeDateTimeMethod(), executeLikeRegex(), find_or_create_child_node(), 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(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), unistr(), varchar(), varchar_input(), xmltext(), and xslt_process().

◆ getClosestMatch()

const char* getClosestMatch ( ClosestMatchState state)

Definition at line 6243 of file varlena.c.

6244 {
6245  Assert(state);
6246 
6247  return state->match;
6248 }

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

6473 {
6474  if (c >= '0' && c <= '9')
6475  return c - '0';
6476  if (c >= 'a' && c <= 'f')
6477  return c - 'a' + 0xA;
6478  if (c >= 'A' && c <= 'F')
6479  return c - 'A' + 0xA;
6480  elog(ERROR, "invalid hexadecimal digit");
6481  return 0; /* not reached */
6482 }
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 6488 of file varlena.c.

6489 {
6490  unsigned int result = 0;
6491 
6492  for (size_t i = 0; i < n; i++)
6493  result += hexval(instr[i]) << (4 * (n - i - 1));
6494 
6495  return result;
6496 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6472

References hexval(), and i.

Referenced by unistr().

◆ icu_unicode_version()

Datum icu_unicode_version ( PG_FUNCTION_ARGS  )

Definition at line 6302 of file varlena.c.

6303 {
6304 #ifdef USE_ICU
6305  PG_RETURN_TEXT_P(cstring_to_text(U_UNICODE_VERSION));
6306 #else
6307  PG_RETURN_NULL();
6308 #endif
6309 }
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References cstring_to_text(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

Referenced by main().

◆ initClosestMatch()

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

Definition at line 6188 of file varlena.c.

6189 {
6190  Assert(state);
6191  Assert(max_d >= 0);
6192 
6193  state->source = source;
6194  state->min_d = -1;
6195  state->max_d = max_d;
6196  state->match = NULL;
6197 }
static rewind_source * source
Definition: pg_rewind.c:89

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

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

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

6463 {
6464  for (size_t i = 0; i < n; i++)
6465  if (!isxdigit((unsigned char) instr[i]))
6466  return false;
6467 
6468  return true;
6469 }

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5162 of file varlena.c.

5163 {
5164  StringInfo state;
5165  MemoryContext aggcontext;
5166  MemoryContext oldcontext;
5167 
5168  if (!AggCheckCallContext(fcinfo, &aggcontext))
5169  {
5170  /* cannot be called directly because of internal-type argument */
5171  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5172  }
5173 
5174  /*
5175  * Create state in aggregate context. It'll stay there across subsequent
5176  * calls.
5177  */
5178  oldcontext = MemoryContextSwitchTo(aggcontext);
5179  state = makeStringInfo();
5180  MemoryContextSwitchTo(oldcontext);
5181 
5182  return state;
5183 }
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 3382 of file varlena.c.

3383 {
3384  Name s = PG_GETARG_NAME(0);
3385 
3387 }

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

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

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

2083 {
2084  Name arg1 = DatumGetName(x);
2085  Name arg2 = DatumGetName(y);
2086 
2087  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2088 }
#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 2125 of file varlena.c.

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

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

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 2756 of file varlena.c.

2757 {
2759 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2731
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2700

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 2750 of file varlena.c.

2751 {
2753 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2744 of file varlena.c.

2745 {
2747 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2738 of file varlena.c.

2739 {
2741 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2650 of file varlena.c.

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

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

5062 {
5063  int typlen;
5064  char *result;
5065  ToastCompressionId cmid;
5066 
5067  /* On first call, get the input type's typlen, and save at *fn_extra */
5068  if (fcinfo->flinfo->fn_extra == NULL)
5069  {
5070  /* Lookup the datatype of the supplied argument */
5071  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5072 
5073  typlen = get_typlen(argtypeid);
5074  if (typlen == 0) /* should not happen */
5075  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5076 
5077  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5078  sizeof(int));
5079  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5080  }
5081  else
5082  typlen = *((int *) fcinfo->flinfo->fn_extra);
5083 
5084  if (typlen != -1)
5085  PG_RETURN_NULL();
5086 
5087  /* get the compression method id stored in the compressed varlena */
5088  cmid = toast_get_compression_id((struct varlena *)
5090  if (cmid == TOAST_INVALID_COMPRESSION_ID)
5091  PG_RETURN_NULL();
5092 
5093  /* convert compression method id to compression method name */
5094  switch (cmid)
5095  {
5097  result = "pglz";
5098  break;
5100  result = "lz4";
5101  break;
5102  default:
5103  elog(ERROR, "invalid compression method id %d", cmid);
5104  }
5105 
5107 }
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2197
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 5014 of file varlena.c.

5015 {
5017  int32 result;
5018  int typlen;
5019 
5020  /* On first call, get the input type's typlen, and save at *fn_extra */
5021  if (fcinfo->flinfo->fn_extra == NULL)
5022  {
5023  /* Lookup the datatype of the supplied argument */
5024  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5025 
5026  typlen = get_typlen(argtypeid);
5027  if (typlen == 0) /* should not happen */
5028  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5029 
5030  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5031  sizeof(int));
5032  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5033  }
5034  else
5035  typlen = *((int *) fcinfo->flinfo->fn_extra);
5036 
5037  if (typlen == -1)
5038  {
5039  /* varlena type, possibly toasted */
5040  result = toast_datum_size(value);
5041  }
5042  else if (typlen == -2)
5043  {
5044  /* cstring */
5045  result = strlen(DatumGetCString(value)) + 1;
5046  }
5047  else
5048  {
5049  /* ordinary fixed-width type */
5050  result = typlen;
5051  }
5052 
5053  PG_RETURN_INT32(result);
5054 }
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.

◆ pg_column_toast_chunk_id()

Datum pg_column_toast_chunk_id ( PG_FUNCTION_ARGS  )

Definition at line 5114 of file varlena.c.

5115 {
5116  int typlen;
5117  struct varlena *attr;
5118  struct varatt_external toast_pointer;
5119 
5120  /* On first call, get the input type's typlen, and save at *fn_extra */
5121  if (fcinfo->flinfo->fn_extra == NULL)
5122  {
5123  /* Lookup the datatype of the supplied argument */
5124  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5125 
5126  typlen = get_typlen(argtypeid);
5127  if (typlen == 0) /* should not happen */
5128  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5129 
5130  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5131  sizeof(int));
5132  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5133  }
5134  else
5135  typlen = *((int *) fcinfo->flinfo->fn_extra);
5136 
5137  if (typlen != -1)
5138  PG_RETURN_NULL();
5139 
5140  attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
5141 
5142  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
5143  PG_RETURN_NULL();
5144 
5145  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
5146 
5147  PG_RETURN_OID(toast_pointer.va_valueid);
5148 }
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:22
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: varatt.h:290

References DatumGetPointer(), elog, ERROR, get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_OID, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, and VARATT_IS_EXTERNAL_ONDISK.

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3996 of file varlena.c.

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

References appendBinaryStringInfo(), appendStringInfoText(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), initStringInfo(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, 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 4206 of file varlena.c.

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

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

6153 {
6154  while (len > 0)
6155  {
6156  len--;
6157  if (s1[len] != s2[len])
6158  return false;
6159  }
6160  return true;
6161 }

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4368 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

◆ SplitIdentifierString()

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

Definition at line 3457 of file varlena.c.

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

◆ string_agg_combine()

Datum string_agg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5241 of file varlena.c.

5242 {
5243  StringInfo state1;
5244  StringInfo state2;
5245  MemoryContext agg_context;
5246 
5247  if (!AggCheckCallContext(fcinfo, &agg_context))
5248  elog(ERROR, "aggregate function called in non-aggregate context");
5249 
5250  state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5251  state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
5252 
5253  if (state2 == NULL)
5254  {
5255  /*
5256  * NULL state2 is easy, just return state1, which we know is already
5257  * in the agg_context
5258  */
5259  if (state1 == NULL)
5260  PG_RETURN_NULL();
5261  PG_RETURN_POINTER(state1);
5262  }
5263 
5264  if (state1 == NULL)
5265  {
5266  /* We must copy state2's data into the agg_context */
5267  MemoryContext old_context;
5268 
5269  old_context = MemoryContextSwitchTo(agg_context);
5270  state1 = makeStringAggState(fcinfo);
5271  appendBinaryStringInfo(state1, state2->data, state2->len);
5272  state1->cursor = state2->cursor;
5273  MemoryContextSwitchTo(old_context);
5274  }
5275  else if (state2->len > 0)
5276  {
5277  /* Combine ... state1->cursor does not change in this case */
5278  appendBinaryStringInfo(state1, state2->data, state2->len);
5279  }
5280 
5281  PG_RETURN_POINTER(state1);
5282 }

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

5323 {
5324  bytea *sstate;
5325  StringInfo result;
5327  char *data;
5328  int datalen;
5329 
5330  /* cannot be called directly because of internal-type argument */
5331  Assert(AggCheckCallContext(fcinfo, NULL));
5332 
5333  sstate = PG_GETARG_BYTEA_PP(0);
5334 
5335  /*
5336  * Initialize a StringInfo so that we can "receive" it using the standard
5337  * recv-function infrastructure.
5338  */
5340  VARSIZE_ANY_EXHDR(sstate));
5341 
5342  result = makeStringAggState(fcinfo);
5343 
5344  /* cursor */
5345  result->cursor = pq_getmsgint(&buf, 4);
5346 
5347  /* data */
5348  datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
5349  data = (char *) pq_getmsgbytes(&buf, datalen);
5350  appendBinaryStringInfo(result, data, datalen);
5351 
5352  pq_getmsgend(&buf);
5353 
5354  PG_RETURN_POINTER(result);
5355 }
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:635
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:508
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
Definition: stringinfo.h:130

References AggCheckCallContext(), appendBinaryStringInfo(), Assert, buf, StringInfoData::cursor, data, initReadOnlyStringInfo(), makeStringAggState(), 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 5358 of file varlena.c.

5359 {
5360  StringInfo state;
5361 
5362  /* cannot be called directly because of internal-type argument */
5363  Assert(AggCheckCallContext(fcinfo, NULL));
5364 
5365  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5366 
5367  if (state != NULL)
5368  {
5369  /* As per comment in transfn, strip data before the cursor position */
5371  state->len - state->cursor));
5372  }
5373  else
5374  PG_RETURN_NULL();
5375 }

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

5292 {
5293  StringInfo state;
5295  bytea *result;
5296 
5297  /* cannot be called directly because of internal-type argument */
5298  Assert(AggCheckCallContext(fcinfo, NULL));
5299 
5301 
5302  pq_begintypsend(&buf);
5303 
5304  /* cursor */
5305  pq_sendint(&buf, state->cursor, 4);
5306 
5307  /* data */
5308  pq_sendbytes(&buf, state->data, state->len);
5309 
5310  result = pq_endtypsend(&buf);
5311 
5312  PG_RETURN_BYTEA_P(result);
5313 }
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint(StringInfo buf, uint32 i, int b)
Definition: pqformat.h:171

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

5187 {
5188  StringInfo state;
5189 
5190  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5191 
5192  /* Append the value unless null, preceding it with the delimiter. */
5193  if (!PG_ARGISNULL(1))
5194  {
5196  bool isfirst = false;
5197 
5198  /*
5199  * You might think we can just throw away the first delimiter, however
5200  * we must keep it as we may be a parallel worker doing partial
5201  * aggregation building a state to send to the main process. We need
5202  * to keep the delimiter of every aggregation so that the combine
5203  * function can properly join up the strings of two separately
5204  * partially aggregated results. The first delimiter is only stripped
5205  * off in the final function. To know how much to strip off the front
5206  * of the string, we store the length of the first delimiter in the
5207  * StringInfo's cursor field, which we don't otherwise need here.
5208  */
5209  if (state == NULL)
5210  {
5211  state = makeStringAggState(fcinfo);
5212  isfirst = true;
5213  }
5214 
5215  if (!PG_ARGISNULL(2))
5216  {
5217  text *delim = PG_GETARG_TEXT_PP(2);
5218 
5219  appendStringInfoText(state, delim);
5220  if (isfirst)
5221  state->cursor = VARSIZE_ANY_EXHDR(delim);
5222  }
5223 
5225  }
5226 
5227  /*
5228  * The transition type for string_agg() is declared to be "internal",
5229  * which is a pass-by-value type the same size as a pointer.
5230  */
5231  if (state)
5233  PG_RETURN_NULL();
5234 }

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

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

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

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

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

5503 {
5504  text *result;
5505 
5506  result = concat_internal("", 0, fcinfo);
5507  if (result == NULL)
5508  PG_RETURN_NULL();
5509  PG_RETURN_TEXT_P(result);
5510 }
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5422

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5517 of file varlena.c.

5518 {
5519  char *sep;
5520  text *result;
5521 
5522  /* return NULL when separator is NULL */
5523  if (PG_ARGISNULL(0))
5524  PG_RETURN_NULL();
5526 
5527  result = concat_internal(sep, 1, fcinfo);
5528  if (result == NULL)
5529  PG_RETURN_NULL();
5530  PG_RETURN_TEXT_P(result);
5531 }

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

5639 {
5640  text *fmt;
5642  const char *cp;
5643  const char *start_ptr;
5644  const char *end_ptr;
5645  text *result;
5646  int arg;
5647  bool funcvariadic;
5648  int nargs;
5649  Datum *elements = NULL;
5650  bool *nulls = NULL;
5651  Oid element_type = InvalidOid;
5652  Oid prev_type = InvalidOid;
5653  Oid prev_width_type = InvalidOid;
5654  FmgrInfo typoutputfinfo;
5655  FmgrInfo typoutputinfo_width;
5656 
5657  /* When format string is null, immediately return null */
5658  if (PG_ARGISNULL(0))
5659  PG_RETURN_NULL();
5660 
5661  /* If argument is marked VARIADIC, expand array into elements */
5662  if (get_fn_expr_variadic(fcinfo->flinfo))
5663  {
5664  ArrayType *arr;
5665  int16 elmlen;
5666  bool elmbyval;
5667  char elmalign;
5668  int nitems;
5669 
5670  /* Should have just the one argument */
5671  Assert(PG_NARGS() == 2);
5672 
5673  /* If argument is NULL, we treat it as zero-length array */
5674  if (PG_ARGISNULL(1))
5675  nitems = 0;
5676  else
5677  {
5678  /*
5679  * Non-null argument had better be an array. We assume that any
5680  * call context that could let get_fn_expr_variadic return true
5681  * will have checked that a VARIADIC-labeled parameter actually is
5682  * an array. So it should be okay to just Assert that it's an
5683  * array rather than doing a full-fledged error check.
5684  */
5686 
5687  /* OK, safe to fetch the array value */
5688  arr = PG_GETARG_ARRAYTYPE_P(1);
5689 
5690  /* Get info about array element type */
5691  element_type = ARR_ELEMTYPE(arr);
5692  get_typlenbyvalalign(element_type,
5693  &elmlen, &elmbyval, &elmalign);
5694 
5695  /* Extract all array elements */
5696  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5697  &elements, &nulls, &nitems);
5698  }
5699 
5700  nargs = nitems + 1;
5701  funcvariadic = true;
5702  }
5703  else
5704  {
5705  /* Non-variadic case, we'll process the arguments individually */
5706  nargs = PG_NARGS();
5707  funcvariadic = false;
5708  }
5709 
5710  /* Setup for main loop. */
5711  fmt = PG_GETARG_TEXT_PP(0);
5712  start_ptr = VARDATA_ANY(fmt);
5713  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5714  initStringInfo(&str);
5715  arg = 1; /* next argument position to print */
5716 
5717  /* Scan format string, looking for conversion specifiers. */
5718  for (cp = start_ptr; cp < end_ptr; cp++)
5719  {
5720  int argpos;
5721  int widthpos;
5722  int flags;
5723  int width;
5724  Datum value;
5725  bool isNull;
5726  Oid typid;
5727 
5728  /*
5729  * If it's not the start of a conversion specifier, just copy it to
5730  * the output buffer.
5731  */
5732  if (*cp != '%')
5733  {
5735  continue;
5736  }
5737 
5738  ADVANCE_PARSE_POINTER(cp, end_ptr);
5739 
5740  /* Easy case: %% outputs a single % */
5741  if (*cp == '%')
5742  {
5744  continue;
5745  }
5746 
5747  /* Parse the optional portions of the format specifier */
5748  cp = text_format_parse_format(cp, end_ptr,
5749  &argpos, &widthpos,
5750  &flags, &width);
5751 
5752  /*
5753  * Next we should see the main conversion specifier. Whether or not
5754  * an argument position was present, it's known that at least one
5755  * character remains in the string at this point. Experience suggests
5756  * that it's worth checking that that character is one of the expected
5757  * ones before we try to fetch arguments, so as to produce the least
5758  * confusing response to a mis-formatted specifier.
5759  */
5760  if (strchr("sIL", *cp) == NULL)
5761  ereport(ERROR,
5762  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5763  errmsg("unrecognized format() type specifier \"%.*s\"",
5764  pg_mblen(cp), cp),
5765  errhint("For a single \"%%\" use \"%%%%\".")));
5766 
5767  /* If indirect width was specified, get its value */
5768  if (widthpos >= 0)
5769  {
5770  /* Collect the specified or next argument position */
5771  if (widthpos > 0)
5772  arg = widthpos;
5773  if (arg >= nargs)
5774  ereport(ERROR,
5775  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5776  errmsg("too few arguments for format()")));
5777 
5778  /* Get the value and type of the selected argument */
5779  if (!funcvariadic)
5780  {
5782  isNull = PG_ARGISNULL(arg);
5783  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5784  }
5785  else
5786  {
5787  value = elements[arg - 1];
5788  isNull = nulls[arg - 1];
5789  typid = element_type;
5790  }
5791  if (!OidIsValid(typid))
5792  elog(ERROR, "could not determine data type of format() input");
5793 
5794  arg++;
5795 
5796  /* We can treat NULL width the same as zero */
5797  if (isNull)
5798  width = 0;
5799  else if (typid == INT4OID)
5800  width = DatumGetInt32(value);
5801  else if (typid == INT2OID)
5802  width = DatumGetInt16(value);
5803  else
5804  {
5805  /* For less-usual datatypes, convert to text then to int */
5806  char *str;
5807 
5808  if (typid != prev_width_type)
5809  {
5810  Oid typoutputfunc;
5811  bool typIsVarlena;
5812 
5813  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5814  fmgr_info(typoutputfunc, &typoutputinfo_width);
5815  prev_width_type = typid;
5816  }
5817 
5818  str = OutputFunctionCall(&typoutputinfo_width, value);
5819 
5820  /* pg_strtoint32 will complain about bad data or overflow */
5821  width = pg_strtoint32(str);
5822 
5823  pfree(str);
5824  }
5825  }
5826 
5827  /* Collect the specified or next argument position */
5828  if (argpos > 0)
5829  arg = argpos;
5830  if (arg >= nargs)
5831  ereport(ERROR,
5832  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5833  errmsg("too few arguments for format()")));
5834 
5835  /* Get the value and type of the selected argument */
5836  if (!funcvariadic)
5837  {
5839  isNull = PG_ARGISNULL(arg);
5840  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5841  }
5842  else
5843  {
5844  value = elements[arg - 1];
5845  isNull = nulls[arg - 1];
5846  typid = element_type;
5847  }
5848  if (!OidIsValid(typid))
5849  elog(ERROR, "could not determine data type of format() input");
5850 
5851  arg++;
5852 
5853  /*
5854  * Get the appropriate typOutput function, reusing previous one if
5855  * same type as previous argument. That's particularly useful in the
5856  * variadic-array case, but often saves work even for ordinary calls.
5857  */
5858  if (typid != prev_type)
5859  {
5860  Oid typoutputfunc;
5861  bool typIsVarlena;
5862 
5863  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5864  fmgr_info(typoutputfunc, &typoutputfinfo);
5865  prev_type = typid;
5866  }
5867 
5868  /*
5869  * And now we can format the value.
5870  */
5871  switch (*cp)
5872  {
5873  case 's':
5874  case 'I':
5875  case 'L':
5876  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5877  value, isNull,
5878  flags, width);
5879  break;
5880  default:
5881  /* should not get here, because of previous check */
5882  ereport(ERROR,
5883  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5884  errmsg("unrecognized format() type specifier \"%.*s\"",
5885  pg_mblen(cp), cp),
5886  errhint("For a single \"%%\" use \"%%%%\".")));
5887  break;
5888  }
5889  }
5890 
5891  /* Don't need deconstruct_array results anymore. */
5892  if (elements != NULL)
5893  pfree(elements);
5894  if (nulls != NULL)
5895  pfree(nulls);
5896 
5897  /* Generate results. */
5898  result = cstring_to_text_with_len(str.data, str.len);
5899  pfree(str.data);
5900 
5901  PG_RETURN_TEXT_P(result);
5902 }
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3612
signed short int16
Definition: c.h:493
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:2271
int32 pg_strtoint32(const char *s)
Definition: numutils.c:383
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:204
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:6041
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:5964
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5625

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(), 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 6090 of file varlena.c.

6092 {
6093  bool align_to_left = false;
6094  int len;
6095 
6096  /* fast path for typical easy case */
6097  if (width == 0)
6098  {
6100  return;
6101  }
6102 
6103  if (width < 0)
6104  {
6105  /* Negative width: implicit '-' flag, then take absolute value */
6106  align_to_left = true;
6107  /* -INT_MIN is undefined */
6108  if (width <= INT_MIN)
6109  ereport(ERROR,
6110  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6111  errmsg("number is out of range")));
6112  width = -width;
6113  }
6114  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6115  align_to_left = true;
6116 
6117  len = pg_mbstrlen(str);
6118  if (align_to_left)
6119  {
6120  /* left justify */
6122  if (len < width)
6123  appendStringInfoSpaces(buf, width - len);
6124  }
6125  else
6126  {
6127  /* right justify */
6128  if (len < width)
6129  appendStringInfoSpaces(buf, width - len);
6131  }
6132 }
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1037
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:212
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5623

References appendStringInfoSpaces(), appendStringInfoString(), buf, ereport, errcode(), errmsg(), ERROR, len, pg_mbstrlen(), 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 6142 of file varlena.c.

6143 {
6144  return text_format(fcinfo);
6145 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5638

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

5916 {
5917  bool found = false;
5918  const char *cp = *ptr;
5919  int val = 0;
5920 
5921  while (*cp >= '0' && *cp <= '9')
5922  {
5923  int8 digit = (*cp - '0');
5924 
5925  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5926  unlikely(pg_add_s32_overflow(val, digit, &val)))
5927  ereport(ERROR,
5928  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5929  errmsg("number is out of range")));
5930  ADVANCE_PARSE_POINTER(cp, end_ptr);
5931  found = true;
5932  }
5933 
5934  *ptr = cp;
5935  *value = val;
5936 
5937  return found;
5938 }
signed char int8
Definition: c.h:492
#define unlikely(x)
Definition: c.h:311
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 5964 of file varlena.c.

5967 {
5968  const char *cp = start_ptr;
5969  int n;
5970 
5971  /* set defaults for output parameters */
5972  *argpos = -1;
5973  *widthpos = -1;
5974  *flags = 0;
5975  *width = 0;
5976 
5977  /* try to identify first number */
5978  if (text_format_parse_digits(&cp, end_ptr, &n))
5979  {
5980  if (*cp != '$')
5981  {
5982  /* Must be just a width and a type, so we're done */
5983  *width = n;
5984  return cp;
5985  }
5986  /* The number was argument position */
5987  *argpos = n;
5988  /* Explicit 0 for argument index is immediately refused */
5989  if (n == 0)
5990  ereport(ERROR,
5991  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5992  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5993  ADVANCE_PARSE_POINTER(cp, end_ptr);
5994  }
5995 
5996  /* Handle flags (only minus is supported now) */
5997  while (*cp == '-')
5998  {
5999  *flags |= TEXT_FORMAT_FLAG_MINUS;
6000  ADVANCE_PARSE_POINTER(cp, end_ptr);
6001  }
6002 
6003  if (*cp == '*')
6004  {
6005  /* Handle indirect width */
6006  ADVANCE_PARSE_POINTER(cp, end_ptr);
6007  if (text_format_parse_digits(&cp, end_ptr, &n))
6008  {
6009  /* number in this position must be closed by $ */
6010  if (*cp != '$')
6011  ereport(ERROR,
6012  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6013  errmsg("width argument position must be ended by \"$\"")));
6014  /* The number was width argument position */
6015  *widthpos = n;
6016  /* Explicit 0 for argument index is immediately refused */
6017  if (n == 0)
6018  ereport(ERROR,
6019  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6020  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6021  ADVANCE_PARSE_POINTER(cp, end_ptr);
6022  }
6023  else
6024  *widthpos = 0; /* width's argument position is unspecified */
6025  }
6026  else
6027  {
6028  /* Check for direct width specification */
6029  if (text_format_parse_digits(&cp, end_ptr, &n))
6030  *width = n;
6031  }
6032 
6033  /* cp should now be pointing at type character */
6034  return cp;
6035 }
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5915

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

6045 {
6046  char *str;
6047 
6048  /* Handle NULL arguments before trying to stringify the value. */
6049  if (isNull)
6050  {
6051  if (conversion == 's')
6052  text_format_append_string(buf, "", flags, width);
6053  else if (conversion == 'L')
6054  text_format_append_string(buf, "NULL", flags, width);
6055  else if (conversion == 'I')
6056  ereport(ERROR,
6057  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6058  errmsg("null values cannot be formatted as an SQL identifier")));
6059  return;
6060  }
6061 
6062  /* Stringify. */
6063  str = OutputFunctionCall(typOutputInfo, value);
6064 
6065  /* Escape. */
6066  if (conversion == 'I')
6067  {
6068  /* quote_identifier may or may not allocate a new string. */
6070  }
6071  else if (conversion == 'L')
6072  {
6073  char *qstr = quote_literal_cstr(str);
6074 
6075  text_format_append_string(buf, qstr, flags, width);
6076  /* quote_literal_cstr() always allocates a new string */
6077  pfree(qstr);
6078  }
6079  else
6080  text_format_append_string(buf, str, flags, width);
6081 
6082  /* Cleanup. */
6083  pfree(str);
6084 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12596
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6090

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

Referenced by text_format().

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1776 of file varlena.c.

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

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

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

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

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

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

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2571 of file varlena.c.

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

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

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

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

5539 {
5540  int n = PG_GETARG_INT32(1);
5541 
5542  if (n < 0)
5543  {
5544  text *str = PG_GETARG_TEXT_PP(0);
5545  const char *p = VARDATA_ANY(str);
5546  int len = VARSIZE_ANY_EXHDR(str);
5547  int rlen;
5548 
5549  n = pg_mbstrlen_with_len(p, len) + n;
5550  rlen = pg_mbcharcliplen(p, len, n);
5552  }
5553  else
5555 }
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1057
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1125
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:885

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, str, text_substring(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 711 of file varlena.c.

712 {
713  /* fastpath when max encoding length is one */
716  else
717  {
718  text *t = DatumGetTextPP(str);
719 
721  VARSIZE_ANY_EXHDR(t)));
722  }
723 }
#define DatumGetTextPP(X)
Definition: fmgr.h:292

References DatumGetTextPP, pg_database_encoding_max_length(), pg_mbstrlen_with_len(), PG_RETURN_INT32, 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 1731 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1504 {
1505  /* no cleanup needed */
1506 }

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

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

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

1469 {
1470  return state->last_match;
1471 }

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

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1336 of file varlena.c.

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

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

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

References Assert.

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1495 of file varlena.c.

1496 {
1497  state->last_match = NULL;
1498  state->refpoint = state->str1;
1499  state->refpos = 0;
1500 }

Referenced by split_part().

◆ text_position_setup()

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

Definition at line 1216 of file varlena.c.

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

References Assert, check_collation_set(), collid, ereport, errcode(), errmsg(), ERROR, for(), 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 5583 of file varlena.c.

5584 {
5585  text *str = PG_GETARG_TEXT_PP(0);
5586  const char *p = VARDATA_ANY(str);
5587  int len = VARSIZE_ANY_EXHDR(str);
5588  const char *endp = p + len;
5589  text *result;
5590  char *dst;
5591 
5592  result = palloc(len + VARHDRSZ);
5593  dst = (char *) VARDATA(result) + len;
5594  SET_VARSIZE(result, len + VARHDRSZ);
5595 
5597  {
5598  /* multibyte version */
5599  while (p < endp)
5600  {
5601  int sz;
5602 
5603  sz = pg_mblen(p);
5604  dst -= sz;
5605  memcpy(dst, p, sz);
5606  p += sz;
5607  }
5608  }
5609  else
5610  {
5611  /* single byte version */
5612  while (p < endp)
5613  *(--dst) = *p++;
5614  }
5615 
5616  PG_RETURN_TEXT_P(result);
5617 }

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

◆ text_right()

Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 5562 of file varlena.c.

5563 {
5564  text *str = PG_GETARG_TEXT_PP(0);
5565  const char *p = VARDATA_ANY(str);
5566  int len = VARSIZE_ANY_EXHDR(str);
5567  int n = PG_GETARG_INT32(1);
5568  int off;
5569 
5570  if (n < 0)
5571  n = -n;
5572  else
5573  n = pg_mbstrlen_with_len(p, len) - n;
5574  off = pg_mbcharcliplen(p, len, n);
5575 
5577 }

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

◆ text_smaller()

Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2583 of file varlena.c.

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

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

1792 {
1793  Datum arg1 = PG_GETARG_DATUM(0);
1794  Datum arg2 = PG_GETARG_DATUM(1);
1796  pg_locale_t mylocale = 0;
1797  bool result;
1798  Size len1,
1799  len2;
1800 
1802 
1803  if (!lc_collate_is_c(collid))
1804  mylocale = pg_newlocale_from_collation(collid);
1805 
1806  if (!pg_locale_deterministic(mylocale))
1807  ereport(ERROR,
1808  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1809  errmsg("nondeterministic collations are not supported for substring searches")));
1810 
1811  len1 = toast_raw_datum_size(arg1);
1812  len2 = toast_raw_datum_size(arg2);
1813  if (len2 > len1)
1814  re