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:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39

Definition at line 5600 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 2706 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 6140 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 2961 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

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

4084 {
4085  const char *p = VARDATA_ANY(replace_text);
4086  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4087 
4088  while (p < p_end)
4089  {
4090  const char *chunk_start = p;
4091  int so;
4092  int eo;
4093 
4094  /* Find next escape char, if any. */
4095  p = memchr(p, '\\', p_end - p);
4096  if (p == NULL)
4097  p = p_end;
4098 
4099  /* Copy the text we just scanned over, if any. */
4100  if (p > chunk_start)
4101  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4102 
4103  /* Done if at end of string, else advance over escape char. */
4104  if (p >= p_end)
4105  break;
4106  p++;
4107 
4108  if (p >= p_end)
4109  {
4110  /* Escape at very end of input. Treat same as unexpected char */
4111  appendStringInfoChar(str, '\\');
4112  break;
4113  }
4114 
4115  if (*p >= '1' && *p <= '9')
4116  {
4117  /* Use the back reference of regexp. */
4118  int idx = *p - '0';
4119 
4120  so = pmatch[idx].rm_so;
4121  eo = pmatch[idx].rm_eo;
4122  p++;
4123  }
4124  else if (*p == '&')
4125  {
4126  /* Use the entire matched string. */
4127  so = pmatch[0].rm_so;
4128  eo = pmatch[0].rm_eo;
4129  p++;
4130  }
4131  else if (*p == '\\')
4132  {
4133  /* \\ means transfer one \ to output. */
4134  appendStringInfoChar(str, '\\');
4135  p++;
4136  continue;
4137  }
4138  else
4139  {
4140  /*
4141  * If escape char is not followed by any expected char, just treat
4142  * it as ordinary data to copy. (XXX would it be better to throw
4143  * an error?)
4144  */
4145  appendStringInfoChar(str, '\\');
4146  continue;
4147  }
4148 
4149  if (so >= 0 && eo >= 0)
4150  {
4151  /*
4152  * Copy the text that is back reference of regexp. Note so and eo
4153  * are counted in characters not bytes.
4154  */
4155  char *chunk_start;
4156  int chunk_len;
4157 
4158  Assert(so >= data_pos);
4159  chunk_start = start_ptr;
4160  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4161  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4162  appendBinaryStringInfo(str, chunk_start, chunk_len);
4163  }
4164  }
4165 }
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
#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:3971

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, charlen_to_bytelen(), idx(), replace_text(), 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 4741 of file varlena.c.

4742 {
4744  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4745 
4746  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4747 }
#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:4783

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

4785 {
4786  text *result;
4787  int nitems,
4788  *dims,
4789  ndims;
4790  Oid element_type;
4791  int typlen;
4792  bool typbyval;
4793  char typalign;
4795  bool printed = false;
4796  char *p;
4797  bits8 *bitmap;
4798  int bitmask;
4799  int i;
4800  ArrayMetaState *my_extra;
4801 
4802  ndims = ARR_NDIM(v);
4803  dims = ARR_DIMS(v);
4804  nitems = ArrayGetNItems(ndims, dims);
4805 
4806  /* if there are no elements, return an empty string */
4807  if (nitems == 0)
4808  return cstring_to_text_with_len("", 0);
4809 
4810  element_type = ARR_ELEMTYPE(v);
4811  initStringInfo(&buf);
4812 
4813  /*
4814  * We arrange to look up info about element type, including its output
4815  * conversion proc, only once per series of calls, assuming the element
4816  * type doesn't change underneath us.
4817  */
4818  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4819  if (my_extra == NULL)
4820  {
4821  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4822  sizeof(ArrayMetaState));
4823  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4824  my_extra->element_type = ~element_type;
4825  }
4826 
4827  if (my_extra->element_type != element_type)
4828  {
4829  /*
4830  * Get info about element type, including its output conversion proc
4831  */
4832  get_type_io_data(element_type, IOFunc_output,
4833  &my_extra->typlen, &my_extra->typbyval,
4834  &my_extra->typalign, &my_extra->typdelim,
4835  &my_extra->typioparam, &my_extra->typiofunc);
4836  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4837  fcinfo->flinfo->fn_mcxt);
4838  my_extra->element_type = element_type;
4839  }
4840  typlen = my_extra->typlen;
4841  typbyval = my_extra->typbyval;
4842  typalign = my_extra->typalign;
4843 
4844  p = ARR_DATA_PTR(v);
4845  bitmap = ARR_NULLBITMAP(v);
4846  bitmask = 1;
4847 
4848  for (i = 0; i < nitems; i++)
4849  {
4850  Datum itemvalue;
4851  char *value;
4852 
4853  /* Get source element, checking for NULL */
4854  if (bitmap && (*bitmap & bitmask) == 0)
4855  {
4856  /* if null_string is NULL, we just ignore null elements */
4857  if (null_string != NULL)
4858  {
4859  if (printed)
4860  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4861  else
4862  appendStringInfoString(&buf, null_string);
4863  printed = true;
4864  }
4865  }
4866  else
4867  {
4868  itemvalue = fetch_att(p, typbyval, typlen);
4869 
4870  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4871 
4872  if (printed)
4873  appendStringInfo(&buf, "%s%s", fldsep, value);
4874  else
4876  printed = true;
4877 
4878  p = att_addlength_pointer(p, typlen, p);
4879  p = (char *) att_align_nominal(p, typalign);
4880  }
4881 
4882  /* advance bitmap pointer if any */
4883  if (bitmap)
4884  {
4885  bitmask <<= 1;
4886  if (bitmask == 0x100)
4887  {
4888  bitmap++;
4889  bitmask = 1;
4890  }
4891  }
4892  }
4893 
4894  result = cstring_to_text_with_len(buf.data, buf.len);
4895  pfree(buf.data);
4896 
4897  return result;
4898 }
#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 @157 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:1521
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
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 4757 of file varlena.c.

4758 {
4759  ArrayType *v;
4760  char *fldsep;
4761  char *null_string;
4762 
4763  /* returns NULL when first or second parameter is NULL */
4764  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4765  PG_RETURN_NULL();
4766 
4767  v = PG_GETARG_ARRAYTYPE_P(0);
4768  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4769 
4770  /* NULL null string is passed through as a null pointer */
4771  if (!PG_ARGISNULL(2))
4772  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4773  else
4774  null_string = NULL;
4775 
4776  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4777 }
#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 2030 of file varlena.c.

2031 {
2032  BpChar *arg1 = DatumGetBpCharPP(x);
2033  BpChar *arg2 = DatumGetBpCharPP(y);
2034  char *a1p,
2035  *a2p;
2036  int len1,
2037  len2,
2038  result;
2039 
2040  a1p = VARDATA_ANY(arg1);
2041  a2p = VARDATA_ANY(arg2);
2042 
2043  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2044  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2045 
2046  result = memcmp(a1p, a2p, Min(len1, len2));
2047  if ((result == 0) && (len1 != len2))
2048  result = (len1 < len2) ? -1 : 1;
2049 
2050  /* We can't afford to leak memory here. */
2051  if (PointerGetDatum(arg1) != x)
2052  pfree(arg1);
2053  if (PointerGetDatum(arg2) != y)
2054  pfree(arg2);
2055 
2056  return result;
2057 }
#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 2675 of file varlena.c.

2676 {
2677  Name arg1 = PG_GETARG_NAME(0);
2678  text *arg2 = PG_GETARG_TEXT_PP(1);
2679  int32 result;
2680 
2681  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2682  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2683  PG_GET_COLLATION());
2684 
2685  PG_FREE_IF_COPY(arg2, 1);
2686 
2687  PG_RETURN_INT32(result);
2688 }
#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:1538

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

2859 {
2860  text *arg1 = PG_GETARG_TEXT_PP(0);
2861  text *arg2 = PG_GETARG_TEXT_PP(1);
2862  int result;
2863 
2864  result = internal_text_pattern_compare(arg1, arg2);
2865 
2866  PG_FREE_IF_COPY(arg1, 0);
2867  PG_FREE_IF_COPY(arg2, 1);
2868 
2869  PG_RETURN_INT32(result);
2870 }
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2772

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

2875 {
2877  MemoryContext oldcontext;
2878 
2879  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2880 
2881  /* Use generic string SortSupport, forcing "C" collation */
2882  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
2883 
2884  MemoryContextSwitchTo(oldcontext);
2885 
2886  PG_RETURN_VOID();
2887 }
#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:1856

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1814 of file varlena.c.

1815 {
1816  text *arg1 = PG_GETARG_TEXT_PP(0);
1817  text *arg2 = PG_GETARG_TEXT_PP(1);
1818  int32 result;
1819 
1820  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1821 
1822  PG_FREE_IF_COPY(arg1, 0);
1823  PG_FREE_IF_COPY(arg2, 1);
1824 
1825  PG_RETURN_INT32(result);
1826 }
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1586

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

2692 {
2693  text *arg1 = PG_GETARG_TEXT_PP(0);
2694  Name arg2 = PG_GETARG_NAME(1);
2695  int32 result;
2696 
2697  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
2698  NameStr(*arg2), strlen(NameStr(*arg2)),
2699  PG_GET_COLLATION());
2700 
2701  PG_FREE_IF_COPY(arg1, 0);
2702 
2703  PG_RETURN_INT32(result);
2704 }

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

1830 {
1832  Oid collid = ssup->ssup_collation;
1833  MemoryContext oldcontext;
1834 
1835  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1836 
1837  /* Use generic string SortSupport */
1838  varstr_sortsupport(ssup, TEXTOID, collid);
1839 
1840  MemoryContextSwitchTo(oldcontext);
1841 
1842  PG_RETURN_VOID();
1843 }
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 2532 of file varlena.c.

2533 {
2534  /* Oid opcintype = PG_GETARG_OID(0); */
2537 
2539 
2541 
2542  PG_RETURN_BOOL(locale->deterministic);
2543 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static char * locale
Definition: initdb.c:140
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1510
static void check_collation_set(Oid collid)
Definition: varlena.c:1509

References check_collation_set(), collid, locale, PG_GET_COLLATION, pg_newlocale_from_collation(), and PG_RETURN_BOOL.

◆ build_concat_foutcache()

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

Definition at line 5359 of file varlena.c.

5360 {
5361  FmgrInfo *foutcache;
5362  int i;
5363 
5364  /* We keep the info in fn_mcxt so it survives across calls */
5365  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5366  PG_NARGS() * sizeof(FmgrInfo));
5367 
5368  for (i = argidx; i < PG_NARGS(); i++)
5369  {
5370  Oid valtype;
5371  Oid typOutput;
5372  bool typIsVarlena;
5373 
5374  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5375  if (!OidIsValid(valtype))
5376  elog(ERROR, "could not determine data type of concat() input");
5377 
5378  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5379  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5380  }
5381 
5382  fcinfo->flinfo->fn_extra = foutcache;
5383 
5384  return foutcache;
5385 }
#define OidIsValid(objectId)
Definition: c.h:775
#define elog(elevel,...)
Definition: elog.h:225
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 3126 of file varlena.c.

3127 {
3128  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3129 
3131 }
#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 2928 of file varlena.c.

2929 {
2930  bytea *result;
2931  int len1,
2932  len2,
2933  len;
2934  char *ptr;
2935 
2936  len1 = VARSIZE_ANY_EXHDR(t1);
2937  len2 = VARSIZE_ANY_EXHDR(t2);
2938 
2939  /* paranoia ... probably should throw error instead? */
2940  if (len1 < 0)
2941  len1 = 0;
2942  if (len2 < 0)
2943  len2 = 0;
2944 
2945  len = len1 + len2 + VARHDRSZ;
2946  result = (bytea *) palloc(len);
2947 
2948  /* Set size of result string... */
2949  SET_VARSIZE(result, len);
2950 
2951  /* Fill data field of result string... */
2952  ptr = VARDATA(result);
2953  if (len1 > 0)
2954  memcpy(ptr, VARDATA_ANY(t1), len1);
2955  if (len2 > 0)
2956  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2957 
2958  return result;
2959 }
#define VARHDRSZ
Definition: c.h:692
void * palloc(Size size)
Definition: mcxt.c:1317
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 3093 of file varlena.c.

3094 {
3095  bytea *result;
3096  bytea *s1;
3097  bytea *s2;
3098  int sp_pl_sl;
3099 
3100  /*
3101  * Check for possible integer-overflow cases. For negative sp, throw a
3102  * "substring length" error because that's what should be expected
3103  * according to the spec's definition of OVERLAY().
3104  */
3105  if (sp <= 0)
3106  ereport(ERROR,
3107  (errcode(ERRCODE_SUBSTRING_ERROR),
3108  errmsg("negative substring length not allowed")));
3109  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3110  ereport(ERROR,
3111  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3112  errmsg("integer out of range")));
3113 
3114  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3115  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3116  result = bytea_catenate(s1, t2);
3117  result = bytea_catenate(result, s2);
3118 
3119  return result;
3120 }
#define ereport(elevel,...)
Definition: elog.h:149
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:135
char * s1
char * s2
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3003
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2928

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

3936 {
3938  MemoryContext oldcontext;
3939 
3940  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3941 
3942  /* Use generic string SortSupport, forcing "C" collation */
3943  varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
3944 
3945  MemoryContextSwitchTo(oldcontext);
3946 
3947  PG_RETURN_VOID();
3948 }

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:5137

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

2981 {
2983  PG_GETARG_INT32(1),
2984  PG_GETARG_INT32(2),
2985  false));
2986 }
#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 2994 of file varlena.c.

2995 {
2997  PG_GETARG_INT32(1),
2998  -1,
2999  true));
3000 }

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

3007 {
3008  int32 S1; /* adjusted start position */
3009  int32 L1; /* adjusted substring length */
3010  int32 E; /* end position */
3011 
3012  /*
3013  * The logic here should generally match text_substring().
3014  */
3015  S1 = Max(S, 1);
3016 
3017  if (length_not_specified)
3018  {
3019  /*
3020  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3021  * end of the string if we pass it a negative value for length.
3022  */
3023  L1 = -1;
3024  }
3025  else if (L < 0)
3026  {
3027  /* SQL99 says to throw an error for E < S, i.e., negative length */
3028  ereport(ERROR,
3029  (errcode(ERRCODE_SUBSTRING_ERROR),
3030  errmsg("negative substring length not allowed")));
3031  L1 = -1; /* silence stupider compilers */
3032  }
3033  else if (pg_add_s32_overflow(S, L, &E))
3034  {
3035  /*
3036  * L could be large enough for S + L to overflow, in which case the
3037  * substring must run to end of string.
3038  */
3039  L1 = -1;
3040  }
3041  else
3042  {
3043  /*
3044  * A zero or negative value for the end position can happen if the
3045  * start was negative or one. SQL99 says to return a zero-length
3046  * string.
3047  */
3048  if (E < 1)
3049  return PG_STR_GET_BYTEA("");
3050 
3051  L1 = E - S1;
3052  }
3053 
3054  /*
3055  * If the start position is past the end of the string, SQL99 says to
3056  * return a zero-length string -- DatumGetByteaPSlice() will do that for
3057  * us. We need only convert S1 to zero-based starting position.
3058  */
3059  return DatumGetByteaPSlice(str, S1 - 1, L1);
3060 }
#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:2961

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

2914 {
2915  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2916  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2917 
2919 }

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3913 of file varlena.c.

3914 {
3915  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3916  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3917  int len1,
3918  len2;
3919  int cmp;
3920 
3921  len1 = VARSIZE_ANY_EXHDR(arg1);
3922  len2 = VARSIZE_ANY_EXHDR(arg2);
3923 
3924  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3925  if ((cmp == 0) && (len1 != len2))
3926  cmp = (len1 < len2) ? -1 : 1;
3927 
3928  PG_FREE_IF_COPY(arg1, 0);
3929  PG_FREE_IF_COPY(arg2, 1);
3930 
3932 }
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 3769 of file varlena.c.

3770 {
3771  Datum arg1 = PG_GETARG_DATUM(0);
3772  Datum arg2 = PG_GETARG_DATUM(1);
3773  bool result;
3774  Size len1,
3775  len2;
3776 
3777  /*
3778  * We can use a fast path for unequal lengths, which might save us from
3779  * having to detoast one or both values.
3780  */
3781  len1 = toast_raw_datum_size(arg1);
3782  len2 = toast_raw_datum_size(arg2);
3783  if (len1 != len2)
3784  result = false;
3785  else
3786  {
3787  bytea *barg1 = DatumGetByteaPP(arg1);
3788  bytea *barg2 = DatumGetByteaPP(arg2);
3789 
3790  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3791  len1 - VARHDRSZ) == 0);
3792 
3793  PG_FREE_IF_COPY(barg1, 0);
3794  PG_FREE_IF_COPY(barg2, 1);
3795  }
3796 
3797  PG_RETURN_BOOL(result);
3798 }
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 3893 of file varlena.c.

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

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

3214 {
3215  bytea *v = PG_GETARG_BYTEA_PP(0);
3216  int64 n = PG_GETARG_INT64(1);
3217  int byteNo,
3218  bitNo;
3219  int len;
3220  int byte;
3221 
3222  len = VARSIZE_ANY_EXHDR(v);
3223 
3224  if (n < 0 || n >= (int64) len * 8)
3225  ereport(ERROR,
3226  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3227  errmsg("index %lld out of valid range, 0..%lld",
3228  (long long) n, (long long) len * 8 - 1)));
3229 
3230  /* n/8 is now known < len, so safe to cast to int */
3231  byteNo = (int) (n / 8);
3232  bitNo = (int) (n % 8);
3233 
3234  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3235 
3236  if (byte & (1 << bitNo))
3237  PG_RETURN_INT32(1);
3238  else
3239  PG_RETURN_INT32(0);
3240 }
#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 3184 of file varlena.c.

3185 {
3186  bytea *v = PG_GETARG_BYTEA_PP(0);
3187  int32 n = PG_GETARG_INT32(1);
3188  int len;
3189  int byte;
3190 
3191  len = VARSIZE_ANY_EXHDR(v);
3192 
3193  if (n < 0 || n >= len)
3194  ereport(ERROR,
3195  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3196  errmsg("index %d out of valid range, 0..%d",
3197  n, len - 1)));
3198 
3199  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3200 
3201  PG_RETURN_INT32(byte);
3202 }

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

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

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:277
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 3853 of file varlena.c.

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

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

3834 {
3835  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3836  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3837  int len1,
3838  len2;
3839  int cmp;
3840 
3841  len1 = VARSIZE_ANY_EXHDR(arg1);
3842  len2 = VARSIZE_ANY_EXHDR(arg2);
3843 
3844  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3845 
3846  PG_FREE_IF_COPY(arg1, 0);
3847  PG_FREE_IF_COPY(arg2, 1);
3848 
3849  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3850 }

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

3802 {
3803  Datum arg1 = PG_GETARG_DATUM(0);
3804  Datum arg2 = PG_GETARG_DATUM(1);
3805  bool result;
3806  Size len1,
3807  len2;
3808 
3809  /*
3810  * We can use a fast path for unequal lengths, which might save us from
3811  * having to detoast one or both values.
3812  */
3813  len1 = toast_raw_datum_size(arg1);
3814  len2 = toast_raw_datum_size(arg2);
3815  if (len1 != len2)
3816  result = true;
3817  else
3818  {
3819  bytea *barg1 = DatumGetByteaPP(arg1);
3820  bytea *barg2 = DatumGetByteaPP(arg2);
3821 
3822  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3823  len1 - VARHDRSZ) != 0);
3824 
3825  PG_FREE_IF_COPY(barg1, 0);
3826  PG_FREE_IF_COPY(barg2, 1);
3827  }
3828 
3829  PG_RETURN_BOOL(result);
3830 }

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

2898 {
2899  Datum str = PG_GETARG_DATUM(0);
2900 
2901  /* We need not detoast the input at all */
2903 }

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:689
#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 3070 of file varlena.c.

3071 {
3072  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3073  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3074  int sp = PG_GETARG_INT32(2); /* substring start position */
3075  int sl = PG_GETARG_INT32(3); /* substring length */
3076 
3077  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3078 }
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3093

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

3082 {
3083  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3084  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3085  int sp = PG_GETARG_INT32(2); /* substring start position */
3086  int sl;
3087 
3088  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3089  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3090 }

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

3141 {
3142  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3143  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3144  int pos;
3145  int px,
3146  p;
3147  int len1,
3148  len2;
3149  char *p1,
3150  *p2;
3151 
3152  len1 = VARSIZE_ANY_EXHDR(t1);
3153  len2 = VARSIZE_ANY_EXHDR(t2);
3154 
3155  if (len2 <= 0)
3156  PG_RETURN_INT32(1); /* result for empty pattern */
3157 
3158  p1 = VARDATA_ANY(t1);
3159  p2 = VARDATA_ANY(t2);
3160 
3161  pos = 0;
3162  px = (len1 - len2);
3163  for (p = 0; p <= px; p++)
3164  {
3165  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3166  {
3167  pos = p + 1;
3168  break;
3169  };
3170  p1++;
3171  };
3172 
3173  PG_RETURN_INT32(pos);
3174 }
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 3283 of file varlena.c.

3284 {
3286  int64 n = PG_GETARG_INT64(1);
3287  int32 newBit = PG_GETARG_INT32(2);
3288  int len;
3289  int oldByte,
3290  newByte;
3291  int byteNo,
3292  bitNo;
3293 
3294  len = VARSIZE(res) - VARHDRSZ;
3295 
3296  if (n < 0 || n >= (int64) len * 8)
3297  ereport(ERROR,
3298  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3299  errmsg("index %lld out of valid range, 0..%lld",
3300  (long long) n, (long long) len * 8 - 1)));
3301 
3302  /* n/8 is now known < len, so safe to cast to int */
3303  byteNo = (int) (n / 8);
3304  bitNo = (int) (n % 8);
3305 
3306  /*
3307  * sanity check!
3308  */
3309  if (newBit != 0 && newBit != 1)
3310  ereport(ERROR,
3311  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3312  errmsg("new bit must be 0 or 1")));
3313 
3314  /*
3315  * Update the byte.
3316  */
3317  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3318 
3319  if (newBit == 0)
3320  newByte = oldByte & (~(1 << bitNo));
3321  else
3322  newByte = oldByte | (1 << bitNo);
3323 
3324  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3325 
3327 }
#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 3251 of file varlena.c.

3252 {
3254  int32 n = PG_GETARG_INT32(1);
3255  int32 newByte = PG_GETARG_INT32(2);
3256  int len;
3257 
3258  len = VARSIZE(res) - VARHDRSZ;
3259 
3260  if (n < 0 || n >= len)
3261  ereport(ERROR,
3262  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3263  errmsg("index %d out of valid range, 0..%d",
3264  n, len - 1)));
3265 
3266  /*
3267  * Now set the byte.
3268  */
3269  ((unsigned char *) VARDATA(res))[n] = newByte;
3270 
3272 }

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

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

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

4049 {
4050  int result = 0;
4051  const char *p = VARDATA_ANY(replace_text);
4052  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4053 
4054  while (p < p_end)
4055  {
4056  /* Find next escape char, if any. */
4057  p = memchr(p, '\\', p_end - p);
4058  if (p == NULL)
4059  break;
4060  p++;
4061  /* Note: a backslash at the end doesn't require extra processing. */
4062  if (p < p_end)
4063  {
4064  if (*p >= '1' && *p <= '9')
4065  return 2; /* Found a submatch specifier, so done */
4066  result = 1; /* Found some other sequence, keep looking */
4067  p++;
4068  }
4069  }
4070  return result;
4071 }

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

5399 {
5400  text *result;
5402  FmgrInfo *foutcache;
5403  bool first_arg = true;
5404  int i;
5405 
5406  /*
5407  * concat(VARIADIC some-array) is essentially equivalent to
5408  * array_to_text(), ie concat the array elements with the given separator.
5409  * So we just pass the case off to that code.
5410  */
5411  if (get_fn_expr_variadic(fcinfo->flinfo))
5412  {
5413  ArrayType *arr;
5414 
5415  /* Should have just the one argument */
5416  Assert(argidx == PG_NARGS() - 1);
5417 
5418  /* concat(VARIADIC NULL) is defined as NULL */
5419  if (PG_ARGISNULL(argidx))
5420  return NULL;
5421 
5422  /*
5423  * Non-null argument had better be an array. We assume that any call
5424  * context that could let get_fn_expr_variadic return true will have
5425  * checked that a VARIADIC-labeled parameter actually is an array. So
5426  * it should be okay to just Assert that it's an array rather than
5427  * doing a full-fledged error check.
5428  */
5430 
5431  /* OK, safe to fetch the array value */
5432  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5433 
5434  /*
5435  * And serialize the array. We tell array_to_text to ignore null
5436  * elements, which matches the behavior of the loop below.
5437  */
5438  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5439  }
5440 
5441  /* Normal case without explicit VARIADIC marker */
5442  initStringInfo(&str);
5443 
5444  /* Get output function info, building it if first time through */
5445  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5446  if (foutcache == NULL)
5447  foutcache = build_concat_foutcache(fcinfo, argidx);
5448 
5449  for (i = argidx; i < PG_NARGS(); i++)
5450  {
5451  if (!PG_ARGISNULL(i))
5452  {
5454 
5455  /* add separator if appropriate */
5456  if (first_arg)
5457  first_arg = false;
5458  else
5459  appendStringInfoString(&str, sepstr);
5460 
5461  /* call the appropriate type output function, append the result */
5463  OutputFunctionCall(&foutcache[i], value));
5464  }
5465  }
5466 
5467  result = cstring_to_text_with_len(str.data, str.len);
5468  pfree(str.data);
5469 
5470  return result;
5471 }
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:5359

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

4906 {
4907  const char *digits = "0123456789abcdef";
4908 
4909  /* We size the buffer for to_bin's longest possible return value. */
4910  char buf[sizeof(uint64) * BITS_PER_BYTE];
4911  char *const end = buf + sizeof(buf);
4912  char *ptr = end;
4913 
4914  Assert(base > 1);
4915  Assert(base <= 16);
4916 
4917  do
4918  {
4919  *--ptr = digits[value % base];
4920  value /= base;
4921  } while (ptr > buf && value);
4922 
4923  return cstring_to_text_with_len(ptr, end - ptr);
4924 }
int digits
Definition: informix.c:691
#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(), 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(), cash_words(), 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(), xmltotext_with_options(), and xslt_process().

◆ getClosestMatch()

const char* getClosestMatch ( ClosestMatchState state)

Definition at line 6218 of file varlena.c.

6219 {
6220  Assert(state);
6221 
6222  return state->match;
6223 }

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

6448 {
6449  if (c >= '0' && c <= '9')
6450  return c - '0';
6451  if (c >= 'a' && c <= 'f')
6452  return c - 'a' + 0xA;
6453  if (c >= 'A' && c <= 'F')
6454  return c - 'A' + 0xA;
6455  elog(ERROR, "invalid hexadecimal digit");
6456  return 0; /* not reached */
6457 }
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 6463 of file varlena.c.

6464 {
6465  unsigned int result = 0;
6466 
6467  for (size_t i = 0; i < n; i++)
6468  result += hexval(instr[i]) << (4 * (n - i - 1));
6469 
6470  return result;
6471 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6447

References hexval(), and i.

Referenced by unistr().

◆ icu_unicode_version()

Datum icu_unicode_version ( PG_FUNCTION_ARGS  )

Definition at line 6277 of file varlena.c.

6278 {
6279 #ifdef USE_ICU
6280  PG_RETURN_TEXT_P(cstring_to_text(U_UNICODE_VERSION));
6281 #else
6282  PG_RETURN_NULL();
6283 #endif
6284 }
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 6163 of file varlena.c.

6164 {
6165  Assert(state);
6166  Assert(max_d >= 0);
6167 
6168  state->source = source;
6169  state->min_d = -1;
6170  state->max_d = max_d;
6171  state->match = NULL;
6172 }
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 2772 of file varlena.c.

2773 {
2774  int result;
2775  int len1,
2776  len2;
2777 
2778  len1 = VARSIZE_ANY_EXHDR(arg1);
2779  len2 = VARSIZE_ANY_EXHDR(arg2);
2780 
2781  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2782  if (result != 0)
2783  return result;
2784  else if (len1 < len2)
2785  return -1;
2786  else if (len1 > len2)
2787  return 1;
2788  else
2789  return 0;
2790 }

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

6438 {
6439  for (size_t i = 0; i < n; i++)
6440  if (!isxdigit((unsigned char) instr[i]))
6441  return false;
6442 
6443  return true;
6444 }

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5137 of file varlena.c.

5138 {
5139  StringInfo state;
5140  MemoryContext aggcontext;
5141  MemoryContext oldcontext;
5142 
5143  if (!AggCheckCallContext(fcinfo, &aggcontext))
5144  {
5145  /* cannot be called directly because of internal-type argument */
5146  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5147  }
5148 
5149  /*
5150  * Create state in aggregate context. It'll stay there across subsequent
5151  * calls.
5152  */
5153  oldcontext = MemoryContextSwitchTo(aggcontext);
5154  state = makeStringInfo();
5155  MemoryContextSwitchTo(oldcontext);
5156 
5157  return state;
5158 }
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 3357 of file varlena.c.

3358 {
3359  Name s = PG_GETARG_NAME(0);
3360 
3362 }

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

2576 {
2577  Name arg1 = PG_GETARG_NAME(0);
2578  text *arg2 = PG_GETARG_TEXT_PP(1);
2579  size_t len1 = strlen(NameStr(*arg1));
2580  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2582  bool result;
2583 
2585 
2586  if (collid == C_COLLATION_OID)
2587  result = (len1 == len2 &&
2588  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2589  else
2590  result = (varstr_cmp(NameStr(*arg1), len1,
2591  VARDATA_ANY(arg2), len2,
2592  collid) == 0);
2593 
2594  PG_FREE_IF_COPY(arg2, 1);
2595 
2596  PG_RETURN_BOOL(result);
2597 }

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

2064 {
2065  Name arg1 = DatumGetName(x);
2066  Name arg2 = DatumGetName(y);
2067 
2068  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2069 }
#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 2106 of file varlena.c.

2107 {
2108  Name arg1 = DatumGetName(x);
2109  Name arg2 = DatumGetName(y);
2110 
2111  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2112  NameStr(*arg2), strlen(NameStr(*arg2)),
2113  ssup);
2114 }
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2120

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

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 2731 of file varlena.c.

2732 {
2734 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2706
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2675

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 2725 of file varlena.c.

2726 {
2728 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2719 of file varlena.c.

2720 {
2722 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2713 of file varlena.c.

2714 {
2716 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2625 of file varlena.c.

2626 {
2627  Name arg1 = PG_GETARG_NAME(0);
2628  text *arg2 = PG_GETARG_TEXT_PP(1);
2629  size_t len1 = strlen(NameStr(*arg1));
2630  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2632  bool result;
2633 
2635 
2636  if (collid == C_COLLATION_OID)
2637  result = !(len1 == len2 &&
2638  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2639  else
2640  result = !(varstr_cmp(NameStr(*arg1), len1,
2641  VARDATA_ANY(arg2), len2,
2642  collid) == 0);
2643 
2644  PG_FREE_IF_COPY(arg2, 1);
2645 
2646  PG_RETURN_BOOL(result);
2647 }

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

5037 {
5038  int typlen;
5039  char *result;
5040  ToastCompressionId cmid;
5041 
5042  /* On first call, get the input type's typlen, and save at *fn_extra */
5043  if (fcinfo->flinfo->fn_extra == NULL)
5044  {
5045  /* Lookup the datatype of the supplied argument */
5046  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5047 
5048  typlen = get_typlen(argtypeid);
5049  if (typlen == 0) /* should not happen */
5050  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5051 
5052  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5053  sizeof(int));
5054  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5055  }
5056  else
5057  typlen = *((int *) fcinfo->flinfo->fn_extra);
5058 
5059  if (typlen != -1)
5060  PG_RETURN_NULL();
5061 
5062  /* get the compression method id stored in the compressed varlena */
5063  cmid = toast_get_compression_id((struct varlena *)
5065  if (cmid == TOAST_INVALID_COMPRESSION_ID)
5066  PG_RETURN_NULL();
5067 
5068  /* convert compression method id to compression method name */
5069  switch (cmid)
5070  {
5072  result = "pglz";
5073  break;
5075  result = "lz4";
5076  break;
5077  default:
5078  elog(ERROR, "invalid compression method id %d", cmid);
5079  }
5080 
5082 }
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 4989 of file varlena.c.

4990 {
4992  int32 result;
4993  int typlen;
4994 
4995  /* On first call, get the input type's typlen, and save at *fn_extra */
4996  if (fcinfo->flinfo->fn_extra == NULL)
4997  {
4998  /* Lookup the datatype of the supplied argument */
4999  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5000 
5001  typlen = get_typlen(argtypeid);
5002  if (typlen == 0) /* should not happen */
5003  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5004 
5005  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5006  sizeof(int));
5007  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5008  }
5009  else
5010  typlen = *((int *) fcinfo->flinfo->fn_extra);
5011 
5012  if (typlen == -1)
5013  {
5014  /* varlena type, possibly toasted */
5015  result = toast_datum_size(value);
5016  }
5017  else if (typlen == -2)
5018  {
5019  /* cstring */
5020  result = strlen(DatumGetCString(value)) + 1;
5021  }
5022  else
5023  {
5024  /* ordinary fixed-width type */
5025  result = typlen;
5026  }
5027 
5028  PG_RETURN_INT32(result);
5029 }
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 5089 of file varlena.c.

5090 {
5091  int typlen;
5092  struct varlena *attr;
5093  struct varatt_external toast_pointer;
5094 
5095  /* On first call, get the input type's typlen, and save at *fn_extra */
5096  if (fcinfo->flinfo->fn_extra == NULL)
5097  {
5098  /* Lookup the datatype of the supplied argument */
5099  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5100 
5101  typlen = get_typlen(argtypeid);
5102  if (typlen == 0) /* should not happen */
5103  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5104 
5105  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5106  sizeof(int));
5107  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5108  }
5109  else
5110  typlen = *((int *) fcinfo->flinfo->fn_extra);
5111 
5112  if (typlen != -1)
5113  PG_RETURN_NULL();
5114 
5115  attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
5116 
5117  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
5118  PG_RETURN_NULL();
5119 
5120  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
5121 
5122  PG_RETURN_OID(toast_pointer.va_valueid);
5123 }
#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 3971 of file varlena.c.

3972 {
3973  text *src_text = PG_GETARG_TEXT_PP(0);
3974  text *from_sub_text = PG_GETARG_TEXT_PP(1);
3975  text *to_sub_text = PG_GETARG_TEXT_PP(2);
3976  int src_text_len;
3977  int from_sub_text_len;
3979  text *ret_text;
3980  int chunk_len;
3981  char *curr_ptr;
3982  char *start_ptr;
3984  bool found;
3985 
3986  src_text_len = VARSIZE_ANY_EXHDR(src_text);
3987  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
3988 
3989  /* Return unmodified source string if empty source or pattern */
3990  if (src_text_len < 1 || from_sub_text_len < 1)
3991  {
3992  PG_RETURN_TEXT_P(src_text);
3993  }
3994 
3995  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
3996 
3997  found = text_position_next(&state);
3998 
3999  /* When the from_sub_text is not found, there is nothing to do. */
4000  if (!found)
4001  {
4003  PG_RETURN_TEXT_P(src_text);
4004  }
4005  curr_ptr = text_position_get_match_ptr(&state);
4006  start_ptr = VARDATA_ANY(src_text);
4007 
4008  initStringInfo(&str);
4009 
4010  do
4011  {
4013 
4014  /* copy the data skipped over by last text_position_next() */
4015  chunk_len = curr_ptr - start_ptr;
4016  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4017 
4018  appendStringInfoText(&str, to_sub_text);
4019 
4020  start_ptr = curr_ptr + from_sub_text_len;
4021 
4022  found = text_position_next(&state);
4023  if (found)
4024  curr_ptr = text_position_get_match_ptr(&state);
4025  }
4026  while (found);
4027 
4028  /* copy trailing data */
4029  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4030  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4031 
4033 
4034  ret_text = cstring_to_text_with_len(str.data, str.len);
4035  pfree(str.data);
4036 
4037  PG_RETURN_TEXT_P(ret_text);
4038 }
#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:1502
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3957
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1467
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1335

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

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

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, regex_t, regmatch_t, replace_text(), 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 6127 of file varlena.c.

6128 {
6129  while (len > 0)
6130  {
6131  len--;
6132  if (s1[len] != s2[len])
6133  return false;
6134  }
6135  return true;
6136 }

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4343 of file varlena.c.

4344 {
4345  text *inputstring = PG_GETARG_TEXT_PP(0);
4346  text *fldsep = PG_GETARG_TEXT_PP(1);
4347  int fldnum = PG_GETARG_INT32(2);
4348  int inputstring_len;
4349  int fldsep_len;
4351  char *start_ptr;
4352  char *end_ptr;
4353  text *result_text;
4354  bool found;
4355 
4356  /* field number is 1 based */
4357  if (fldnum == 0)
4358  ereport(ERROR,
4359  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4360  errmsg("field position must not be zero")));
4361 
4362  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4363  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4364 
4365  /* return empty string for empty input string */
4366  if (inputstring_len < 1)
4368 
4369  /* handle empty field separator */
4370  if (fldsep_len < 1)
4371  {
4372  /* if first or last field, return input string, else empty string */
4373  if (fldnum == 1 || fldnum == -1)
4374  PG_RETURN_TEXT_P(inputstring);
4375  else
4377  }
4378 
4379  /* find the first field separator */
4380  text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4381 
4382  found = text_position_next(&state);
4383 
4384  /* special case if fldsep not found at all */
4385  if (!found)
4386  {
4388  /* if first or last field, return input string, else empty string */
4389  if (fldnum == 1 || fldnum == -1)
4390  PG_RETURN_TEXT_P(inputstring);
4391  else
4393  }
4394 
4395  /*
4396  * take care of a negative field number (i.e. count from the right) by
4397  * converting to a positive field number; we need total number of fields
4398  */
4399  if (fldnum < 0)
4400  {
4401  /* we found a fldsep, so there are at least two fields */
4402  int numfields = 2;
4403 
4404  while (text_position_next(&state))
4405  numfields++;
4406 
4407  /* special case of last field does not require an extra pass */
4408  if (fldnum == -1)
4409  {
4410  start_ptr = text_position_get_match_ptr(&state) + fldsep_len;
4411  end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
4414  end_ptr - start_ptr));
4415  }
4416 
4417  /* else, convert fldnum to positive notation */
4418  fldnum += numfields + 1;
4419 
4420  /* if nonexistent field, return empty string */
4421  if (fldnum <= 0)
4422  {
4425  }
4426 
4427  /* reset to pointing at first match, but now with positive fldnum */
4429  found = text_position_next(&state);
4430  Assert(found);
4431  }
4432 
4433  /* identify bounds of first field */
4434  start_ptr = VARDATA_ANY(inputstring);
4435  end_ptr = text_position_get_match_ptr(&state);
4436 
4437  while (found && --fldnum > 0)
4438  {
4439  /* identify bounds of next field */
4440  start_ptr = end_ptr + fldsep_len;
4441  found = text_position_next(&state);
4442  if (found)
4443  end_ptr = text_position_get_match_ptr(&state);
4444  }
4445 
4447 
4448  if (fldnum > 0)
4449  {
4450  /* N'th field separator not found */
4451  /* if last field requested, return it, else empty string */
4452  if (fldnum == 1)
4453  {
4454  int last_len = start_ptr - VARDATA_ANY(inputstring);
4455 
4456  result_text = cstring_to_text_with_len(start_ptr,
4457  inputstring_len - last_len);
4458  }
4459  else
4460  result_text = cstring_to_text("");
4461  }
4462  else
4463  {
4464  /* non-last field requested */
4465  result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4466  }
4467 
4468  PG_RETURN_TEXT_P(result_text);
4469 }
static void text_position_reset(TextPositionState *state)
Definition: varlena.c:1494

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

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

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

4706 {
4707  bool is_null = false;
4708 
4709  if (null_string && text_isequal(field_value, null_string, collation))
4710  is_null = true;
4711 
4712  if (tstate->tupstore)
4713  {
4714  Datum values[1];
4715  bool nulls[1];
4716 
4717  values[0] = PointerGetDatum(field_value);
4718  nulls[0] = is_null;
4719 
4721  tstate->tupdesc,
4722  values,
4723  nulls);
4724  }
4725  else
4726  {
4727  tstate->astate = accumArrayResult(tstate->astate,
4728  PointerGetDatum(field_value),
4729  is_null,
4730  TEXTOID,
4732  }
4733 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5338
static Datum values[MAXATTR]
Definition: bootstrap.c:150
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:784
static bool text_isequal(text *txt1, text *txt2, Oid collid)
Definition: varlena.c:4475

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

3561 {
3562  char *nextp = rawstring;
3563  bool done = false;
3564 
3565  *namelist = NIL;
3566 
3567  while (scanner_isspace(*nextp))
3568  nextp++; /* skip leading whitespace */
3569 
3570  if (*nextp == '\0')
3571  return true; /* allow empty string */
3572 
3573  /* At the top of the loop, we are at start of a new directory. */
3574  do
3575  {
3576  char *curname;
3577  char *endp;
3578 
3579  if (*nextp == '"')
3580  {
3581  /* Quoted name --- collapse quote-quote pairs */
3582  curname = nextp + 1;
3583  for (;;)
3584  {
3585  endp = strchr(nextp + 1, '"');
3586  if (endp == NULL)
3587  return false; /* mismatched quotes */
3588  if (endp[1] != '"')
3589  break; /* found end of quoted name */
3590  /* Collapse adjacent quotes into one quote, and look again */
3591  memmove(endp, endp + 1, strlen(endp));
3592  nextp = endp;
3593  }
3594  /* endp now points at the terminating quote */
3595  nextp = endp + 1;
3596  }
3597  else
3598  {
3599  /* Unquoted name --- extends to separator or end of string */
3600  curname = endp = nextp;
3601  while (*nextp && *nextp != separator)
3602  {
3603  /* trailing whitespace should not be included in name */
3604  if (!scanner_isspace(*nextp))
3605  endp = nextp + 1;
3606  nextp++;
3607  }
3608  if (curname == endp)
3609  return false; /* empty unquoted name not allowed */
3610  }
3611 
3612  while (scanner_isspace(*nextp))
3613  nextp++; /* skip trailing whitespace */
3614 
3615  if (*nextp == separator)
3616  {
3617  nextp++;
3618  while (scanner_isspace(*nextp))
3619  nextp++; /* skip leading whitespace for next */
3620  /* we expect another name, so done remains false */
3621  }
3622  else if (*nextp == '\0')
3623  done = true;
3624  else
3625  return false; /* invalid syntax */
3626 
3627  /* Now safe to overwrite separator with a null */
3628  *endp = '\0';
3629 
3630  /* Truncate path if it's overlength */
3631  if (strlen(curname) >= MAXPGPATH)
3632  curname[MAXPGPATH - 1] = '\0';
3633 
3634  /*
3635  * Finished isolating current name --- add it to list
3636  */
3637  curname = pstrdup(curname);
3638  canonicalize_path(curname);
3639  *namelist = lappend(*namelist, curname);
3640 
3641  /* Loop back if we didn't reach end of string */
3642  } while (!done);
3643 
3644  return true;
3645 }
List * lappend(List *list, void *datum)
Definition: list.c:339
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define MAXPGPATH
#define NIL
Definition: pg_list.h:68
void canonicalize_path(char *path)
Definition: path.c:265
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 3680 of file varlena.c.

3682 {
3683  char *nextp = rawstring;
3684  bool done = false;
3685 
3686  *namelist = NIL;
3687 
3688  while (scanner_isspace(*nextp))
3689  nextp++; /* skip leading whitespace */
3690 
3691  if (*nextp == '\0')
3692  return true; /* allow empty string */
3693 
3694  /* At the top of the loop, we are at start of a new identifier. */
3695  do
3696  {
3697  char *curname;
3698  char *endp;
3699 
3700  if (*nextp == '"')
3701  {
3702  /* Quoted name --- collapse quote-quote pairs */
3703  curname = nextp + 1;
3704  for (;;)
3705  {
3706  endp = strchr(nextp + 1, '"');
3707  if (endp == NULL)
3708  return false; /* mismatched quotes */
3709  if (endp[1] != '"')
3710  break; /* found end of quoted name */
3711  /* Collapse adjacent quotes into one quote, and look again */
3712  memmove(endp, endp + 1, strlen(endp));
3713  nextp = endp;
3714  }
3715  /* endp now points at the terminating quote */
3716  nextp = endp + 1;
3717  }
3718  else
3719  {
3720  /* Unquoted name --- extends to separator or whitespace */
3721  curname = nextp;
3722  while (*nextp && *nextp != separator &&
3723  !scanner_isspace(*nextp))
3724  nextp++;
3725  endp = nextp;
3726  if (curname == nextp)
3727  return false; /* empty unquoted name not allowed */
3728  }
3729 
3730  while (scanner_isspace(*nextp))
3731  nextp++; /* skip trailing whitespace */
3732 
3733  if (*nextp == separator)
3734  {
3735  nextp++;
3736  while (scanner_isspace(*nextp))
3737  nextp++; /* skip leading whitespace for next */
3738  /* we expect another name, so done remains false */
3739  }
3740  else if (*nextp == '\0')
3741  done = true;
3742  else
3743  return false; /* invalid syntax */
3744 
3745  /* Now safe to overwrite separator with a null */
3746  *endp = '\0';
3747 
3748  /*
3749  * Finished isolating current name --- add it to list
3750  */
3751  *namelist = lappend(*namelist, curname);
3752 
3753  /* Loop back if we didn't reach end of string */
3754  } while (!done);
3755 
3756  return true;
3757 }

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

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

◆ string_agg_combine()

Datum string_agg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5216 of file varlena.c.

5217 {
5218  StringInfo state1;
5219  StringInfo state2;
5220  MemoryContext agg_context;
5221 
5222  if (!AggCheckCallContext(fcinfo, &agg_context))
5223  elog(ERROR, "aggregate function called in non-aggregate context");
5224 
5225  state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5226  state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
5227 
5228  if (state2 == NULL)
5229  {
5230  /*
5231  * NULL state2 is easy, just return state1, which we know is already
5232  * in the agg_context
5233  */
5234  if (state1 == NULL)
5235  PG_RETURN_NULL();
5236  PG_RETURN_POINTER(state1);
5237  }
5238 
5239  if (state1 == NULL)
5240  {
5241  /* We must copy state2's data into the agg_context */
5242  MemoryContext old_context;
5243 
5244  old_context = MemoryContextSwitchTo(agg_context);
5245  state1 = makeStringAggState(fcinfo);
5246  appendBinaryStringInfo(state1, state2->data, state2->len);
5247  state1->cursor = state2->cursor;
5248  MemoryContextSwitchTo(old_context);
5249  }
5250  else if (state2->len > 0)
5251  {
5252  /* Combine ... state1->cursor does not change in this case */
5253  appendBinaryStringInfo(state1, state2->data, state2->len);
5254  }
5255 
5256  PG_RETURN_POINTER(state1);
5257 }

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

5298 {
5299  bytea *sstate;
5300  StringInfo result;
5302  char *data;
5303  int datalen;
5304 
5305  /* cannot be called directly because of internal-type argument */
5306  Assert(AggCheckCallContext(fcinfo, NULL));
5307 
5308  sstate = PG_GETARG_BYTEA_PP(0);
5309 
5310  /*
5311  * Initialize a StringInfo so that we can "receive" it using the standard
5312  * recv-function infrastructure.
5313  */
5315  VARSIZE_ANY_EXHDR(sstate));
5316 
5317  result = makeStringAggState(fcinfo);
5318 
5319  /* cursor */
5320  result->cursor = pq_getmsgint(&buf, 4);
5321 
5322  /* data */
5323  datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
5324  data = (char *) pq_getmsgbytes(&buf, datalen);
5325  appendBinaryStringInfo(result, data, datalen);
5326 
5327  pq_getmsgend(&buf);
5328 
5329  PG_RETURN_POINTER(result);
5330 }
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 5333 of file varlena.c.

5334 {
5335  StringInfo state;
5336 
5337  /* cannot be called directly because of internal-type argument */
5338  Assert(AggCheckCallContext(fcinfo, NULL));
5339 
5340  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5341 
5342  if (state != NULL)
5343  {
5344  /* As per comment in transfn, strip data before the cursor position */
5346  state->len - state->cursor));
5347  }
5348  else
5349  PG_RETURN_NULL();
5350 }

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

5267 {
5268  StringInfo state;
5270  bytea *result;
5271 
5272  /* cannot be called directly because of internal-type argument */
5273  Assert(AggCheckCallContext(fcinfo, NULL));
5274 
5276 
5277  pq_begintypsend(&buf);
5278 
5279  /* cursor */
5280  pq_sendint(&buf, state->cursor, 4);
5281 
5282  /* data */
5283  pq_sendbytes(&buf, state->data, state->len);
5284 
5285  result = pq_endtypsend(&buf);
5286 
5287  PG_RETURN_BYTEA_P(result);
5288 }
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 5161 of file varlena.c.

5162 {
5163  StringInfo state;
5164 
5165  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5166 
5167  /* Append the value unless null, preceding it with the delimiter. */
5168  if (!PG_ARGISNULL(1))
5169  {
5171  bool isfirst = false;
5172 
5173  /*
5174  * You might think we can just throw away the first delimiter, however
5175  * we must keep it as we may be a parallel worker doing partial
5176  * aggregation building a state to send to the main process. We need
5177  * to keep the delimiter of every aggregation so that the combine
5178  * function can properly join up the strings of two separately
5179  * partially aggregated results. The first delimiter is only stripped
5180  * off in the final function. To know how much to strip off the front
5181  * of the string, we store the length of the first delimiter in the
5182  * StringInfo's cursor field, which we don't otherwise need here.
5183  */
5184  if (state == NULL)
5185  {
5186  state = makeStringAggState(fcinfo);
5187  isfirst = true;
5188  }
5189 
5190  if (!PG_ARGISNULL(2))
5191  {
5192  text *delim = PG_GETARG_TEXT_PP(2);
5193 
5194  appendStringInfoText(state, delim);
5195  if (isfirst)
5196  state->cursor = VARSIZE_ANY_EXHDR(delim);
5197  }
5198 
5200  }
5201 
5202  /*
5203  * The transition type for string_agg() is declared to be "internal",
5204  * which is a pass-by-value type the same size as a pointer.
5205  */
5206  if (state)
5208  PG_RETURN_NULL();
5209 }

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

1587 {
1588  char *a1p,
1589  *a2p;
1590  int len1,
1591  len2;
1592 
1593  a1p = VARDATA_ANY(arg1);
1594  a2p = VARDATA_ANY(arg2);
1595 
1596  len1 = VARSIZE_ANY_EXHDR(arg1);
1597  len2 = VARSIZE_ANY_EXHDR(arg2);
1598 
1599  return varstr_cmp(a1p, len1, a2p, len2, collid);
1600 }

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

5478 {
5479  text *result;
5480 
5481  result = concat_internal("", 0, fcinfo);
5482  if (result == NULL)
5483  PG_RETURN_NULL();
5484  PG_RETURN_TEXT_P(result);
5485 }
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5397

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5492 of file varlena.c.

5493 {
5494  char *sep;
5495  text *result;
5496 
5497  /* return NULL when separator is NULL */
5498  if (PG_ARGISNULL(0))
5499  PG_RETURN_NULL();
5501 
5502  result = concat_internal(sep, 1, fcinfo);
5503  if (result == NULL)
5504  PG_RETURN_NULL();
5505  PG_RETURN_TEXT_P(result);
5506 }

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

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

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

6067 {
6068  bool align_to_left = false;
6069  int len;
6070 
6071  /* fast path for typical easy case */
6072  if (width == 0)
6073  {
6075  return;
6076  }
6077 
6078  if (width < 0)
6079  {
6080  /* Negative width: implicit '-' flag, then take absolute value */
6081  align_to_left = true;
6082  /* -INT_MIN is undefined */
6083  if (width <= INT_MIN)
6084  ereport(ERROR,
6085  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6086  errmsg("number is out of range")));
6087  width = -width;
6088  }
6089  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6090  align_to_left = true;
6091 
6092  len = pg_mbstrlen(str);
6093  if (align_to_left)
6094  {
6095  /* left justify */
6097  if (len < width)
6098  appendStringInfoSpaces(buf, width - len);
6099  }
6100  else
6101  {
6102  /* right justify */
6103  if (len < width)
6104  appendStringInfoSpaces(buf, width - len);
6106  }
6107 }
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:5598

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

6118 {
6119  return text_format(fcinfo);
6120 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5613

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

5891 {
5892  bool found = false;
5893  const char *cp = *ptr;
5894  int val = 0;
5895 
5896  while (*cp >= '0' && *cp <= '9')
5897  {
5898  int8 digit = (*cp - '0');
5899 
5900  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5901  unlikely(pg_add_s32_overflow(val, digit, &val)))
5902  ereport(ERROR,
5903  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5904  errmsg("number is out of range")));
5905  ADVANCE_PARSE_POINTER(cp, end_ptr);
5906  found = true;
5907  }
5908 
5909  *ptr = cp;
5910  *value = val;
5911 
5912  return found;
5913 }
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:171

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

5942 {
5943  const char *cp = start_ptr;
5944  int n;
5945 
5946  /* set defaults for output parameters */
5947  *argpos = -1;
5948  *widthpos = -1;
5949  *flags = 0;
5950  *width = 0;
5951 
5952  /* try to identify first number */
5953  if (text_format_parse_digits(&cp, end_ptr, &n))
5954  {
5955  if (*cp != '$')
5956  {
5957  /* Must be just a width and a type, so we're done */
5958  *width = n;
5959  return cp;
5960  }
5961  /* The number was argument position */
5962  *argpos = n;
5963  /* Explicit 0 for argument index is immediately refused */
5964  if (n == 0)
5965  ereport(ERROR,
5966  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5967  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5968  ADVANCE_PARSE_POINTER(cp, end_ptr);
5969  }
5970 
5971  /* Handle flags (only minus is supported now) */
5972  while (*cp == '-')
5973  {
5974  *flags |= TEXT_FORMAT_FLAG_MINUS;
5975  ADVANCE_PARSE_POINTER(cp, end_ptr);
5976  }
5977 
5978  if (*cp == '*')
5979  {
5980  /* Handle indirect width */
5981  ADVANCE_PARSE_POINTER(cp, end_ptr);
5982  if (text_format_parse_digits(&cp, end_ptr, &n))
5983  {
5984  /* number in this position must be closed by $ */
5985  if (*cp != '$')
5986  ereport(ERROR,
5987  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5988  errmsg("width argument position must be ended by \"$\"")));
5989  /* The number was width argument position */
5990  *widthpos = n;
5991  /* Explicit 0 for argument index is immediately refused */
5992  if (n == 0)
5993  ereport(ERROR,
5994  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5995  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5996  ADVANCE_PARSE_POINTER(cp, end_ptr);
5997  }
5998  else
5999  *widthpos = 0; /* width's argument position is unspecified */
6000  }
6001  else
6002  {
6003  /* Check for direct width specification */
6004  if (text_format_parse_digits(&cp, end_ptr, &n))
6005  *width = n;
6006  }
6007 
6008  /* cp should now be pointing at type character */
6009  return cp;
6010 }
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5890

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

6020 {
6021  char *str;
6022 
6023  /* Handle NULL arguments before trying to stringify the value. */
6024  if (isNull)
6025  {
6026  if (conversion == 's')
6027  text_format_append_string(buf, "", flags, width);
6028  else if (conversion == 'L')
6029  text_format_append_string(buf, "NULL", flags, width);
6030  else if (conversion == 'I')
6031  ereport(ERROR,
6032  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6033  errmsg("null values cannot be formatted as an SQL identifier")));
6034  return;
6035  }
6036 
6037  /* Stringify. */
6038  str = OutputFunctionCall(typOutputInfo, value);
6039 
6040  /* Escape. */
6041  if (conversion == 'I')
6042  {
6043  /* quote_identifier may or may not allocate a new string. */
6045  }
6046  else if (conversion == 'L')
6047  {
6048  char *qstr = quote_literal_cstr(str);
6049 
6050  text_format_append_string(buf, qstr, flags, width);
6051  /* quote_literal_cstr() always allocates a new string */
6052  pfree(qstr);
6053  }
6054  else
6055  text_format_append_string(buf, str, flags, width);
6056 
6057  /* Cleanup. */
6058  pfree(str);
6059 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12835
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6065

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

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

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

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

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

4476 {
4478  collid,
4479  PointerGetDatum(txt1),
4480  PointerGetDatum(txt2)));
4481 }
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1611

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

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2546 of file varlena.c.

2547 {
2548  text *arg1 = PG_GETARG_TEXT_PP(0);
2549  text *arg2 = PG_GETARG_TEXT_PP(1);
2550  text *result;
2551 
2552  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2553 
2554  PG_RETURN_TEXT_P(result);
2555 }

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

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

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

5514 {
5515  int n = PG_GETARG_INT32(1);
5516 
5517  if (n < 0)
5518  {
5519  text *str = PG_GETARG_TEXT_PP(0);
5520  const char *p = VARDATA_ANY(str);
5521  int len = VARSIZE_ANY_EXHDR(str);
5522  int rlen;
5523 
5524  n = pg_mbstrlen_with_len(p, len) + n;
5525  rlen = pg_mbcharcliplen(p, len, n);
5527  }
5528  else
5530 }
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 1715 of file varlena.c.

1716 {
1717  text *arg1 = PG_GETARG_TEXT_PP(0);
1718  text *arg2 = PG_GETARG_TEXT_PP(1);
1719  bool result;
1720 
1721  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1722 
1723  PG_FREE_IF_COPY(arg1, 0);
1724  PG_FREE_IF_COPY(arg2, 1);
1725 
1726  PG_RETURN_BOOL(result);
1727 }

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

3335 {
3336  text *s = PG_GETARG_TEXT_PP(0);
3337  Name result;
3338  int len;
3339 
3340  len = VARSIZE_ANY_EXHDR(s);
3341 
3342  /* Truncate oversize input */
3343  if (len >= NAMEDATALEN)
3345 
3346  /* We use palloc0 here to ensure result is zero-padded */
3347  result = (Name) palloc0(NAMEDATALEN);
3348  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3349 
3350  PG_RETURN_NAME(result);
3351 }
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:1347

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

2827 {
2828  text *arg1 = PG_GETARG_TEXT_PP(0);
2829  text *arg2 = PG_GETARG_TEXT_PP(1);
2830  int result;
2831 
2832  result = internal_text_pattern_compare(arg1, arg2);
2833 
2834  PG_FREE_IF_COPY(arg1, 0);
2835  PG_FREE_IF_COPY(arg2, 1);
2836 
2837  PG_RETURN_BOOL(result >= 0);
2838 }

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

2843 {
2844  text *arg1 = PG_GETARG_TEXT_PP(0);
2845  text *arg2 = PG_GETARG_TEXT_PP(1);
2846  int result;
2847 
2848  result = internal_text_pattern_compare(arg1, arg2);
2849 
2850  PG_FREE_IF_COPY(arg1, 0);
2851  PG_FREE_IF_COPY(arg2, 1);
2852 
2853  PG_RETURN_BOOL(result > 0);
2854 }

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

2811 {
2812  text *arg1 = PG_GETARG_TEXT_PP(0);
2813  text *arg2 = PG_GETARG_TEXT_PP(1);
2814  int result;
2815 
2816  result = internal_text_pattern_compare(arg1, arg2);
2817 
2818  PG_FREE_IF_COPY(arg1, 0);
2819  PG_FREE_IF_COPY(arg2, 1);
2820 
2821  PG_RETURN_BOOL(result <= 0);
2822 }

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

2795 {
2796  text *arg1 = PG_GETARG_TEXT_PP(0);
2797  text *arg2 = PG_GETARG_TEXT_PP(1);
2798  int result;
2799 
2800  result = internal_text_pattern_compare(arg1, arg2);
2801 
2802  PG_FREE_IF_COPY(arg1, 0);
2803  PG_FREE_IF_COPY(arg2, 1);
2804 
2805  PG_RETURN_BOOL(result < 0);
2806 }

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:1478

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

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

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

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

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

1468 {
1469  return state->last_match;
1470 }

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

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1335 of file varlena.c.

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

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

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

References Assert.

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1494 of file varlena.c.

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

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

References Assert, check_collation_set(), collid, pg_locale_struct::deterministic, ereport, errcode(), errmsg(), ERROR, for(), GetDatabaseEncoding(), i, pg_database_encoding_max_length(), 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 5558 of file varlena.c.

5559 {
5560  text *str = PG_GETARG_TEXT_PP(0);
5561  const char *p = VARDATA_ANY(str);
5562  int len = VARSIZE_ANY_EXHDR(str);
5563  const char *endp = p + len;
5564  text *result;
5565  char *dst;
5566 
5567  result = palloc(len + VARHDRSZ);
5568  dst = (char *) VARDATA(result) + len;
5569  SET_VARSIZE(result, len + VARHDRSZ);
5570 
5572  {
5573  /* multibyte version */
5574  while (p < endp)
5575  {
5576  int sz;
5577 
5578  sz = pg_mblen(p);
5579  dst -= sz;
5580  memcpy(dst, p, sz);
5581  p += sz;
5582  }
5583  }
5584  else
5585  {
5586  /* single byte version */
5587  while (p < endp)
5588  *(--dst) = *p++;
5589  }
5590 
5591  PG_RETURN_TEXT_P(result);
5592 }

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

5538 {
5539  text *str = PG_GETARG_TEXT_PP(0);
5540  const char *p = VARDATA_ANY(str);
5541  int len = VARSIZE_ANY_EXHDR(str);
5542  int n = PG_GETARG_INT32(1);
5543  int off;
5544 
5545  if (n < 0)
5546  n = -n;
5547  else
5548  n = pg_mbstrlen_with_len(p, len) - n;
5549  off = pg_mbcharcliplen(p, len, n);
5550 
5552 }

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

2559 {
2560  text *arg1 = PG_GETARG_TEXT_PP(0);
2561  text *arg2 = PG_GETARG_TEXT_PP(1);
2562  text *result;
2563 
2564  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2565 
2566  PG_RETURN_TEXT_P(result);
2567 }

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

1776 {
1777  Datum arg1 = PG_GETARG_DATUM(0);
1778  Datum arg2 = PG_GETARG_DATUM(1);
1780  pg_locale_t mylocale;
1781  bool result;
1782  Size len1,
1783  len2;
1784 
1786 
1787  mylocale = pg_newlocale_from_collation(collid);
1788 
1789  if (!mylocale->deterministic)
1790  ereport(ERROR,
1791  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1792  errmsg("nondeterministic collations are not supported for substring searches")));
1793 
1794  len1 = toast_raw_datum_size(arg1);
1795  len2 = toast_raw_datum_size(arg2);
1796  if (len2 > len1)
1797  result = false;
1798  else
1799  {
1800  text *targ1 = text_substring(arg1, 1, len2, false);
1801  text *targ2 = DatumGetTextPP(arg2);
1802 
1803  result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1804  VARSIZE_ANY_EXHDR(targ2)) == 0);
1805 
1806  PG_FREE_IF_COPY(targ1, 0);
1807  PG_FREE_IF_COPY(targ2, 1);
1808  }
1809 
1810  PG_RETURN_BOOL(result);
1811 }

References check_collation_set(),