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

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 
struct  SplitTextOutputData
 

Macros

#define TEXTBUFLEN   1024
 
#define DatumGetVarStringP(X)   ((VarString *) PG_DETOAST_DATUM(X))
 
#define DatumGetVarStringPP(X)   ((VarString *) PG_DETOAST_DATUM_PACKED(X))
 
#define VAL(CH)   ((CH) - '0')
 
#define DIG(VAL)   ((VAL) + '0')
 
#define CmpCall(cmpfunc)
 
#define PG_STR_GET_BYTEA(str_)    DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#define 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 string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_combine (PG_FUNCTION_ARGS)
 
Datum string_agg_serialize (PG_FUNCTION_ARGS)
 
Datum string_agg_deserialize (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 
void initClosestMatch (ClosestMatchState *state, const char *source, int max_d)
 
void updateClosestMatch (ClosestMatchState *state, const char *candidate)
 
const char * getClosestMatch (ClosestMatchState *state)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 
static bool isxdigits_n (const char *instr, size_t n)
 
static unsigned int hexval (unsigned char c)
 
static unsigned int hexval_n (const char *instr, size_t n)
 
Datum unistr (PG_FUNCTION_ARGS)
 

Variables

int bytea_output = BYTEA_OUTPUT_HEX
 

Macro Definition Documentation

◆ ADVANCE_PARSE_POINTER

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

Definition at line 5583 of file varlena.c.

◆ CmpCall

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

◆ DatumGetVarStringP

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

Definition at line 114 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 115 of file varlena.c.

◆ DIG

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

Definition at line 274 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6123 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 2983 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

Definition at line 5581 of file varlena.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 112 of file varlena.c.

◆ VAL

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

Definition at line 273 of file varlena.c.

Typedef Documentation

◆ VarString

typedef struct varlena VarString

Definition at line 46 of file varlena.c.

Function Documentation

◆ appendStringInfoRegexpSubstr()

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

Definition at line 4103 of file varlena.c.

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

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

Referenced by replace_text_regexp().

◆ appendStringInfoText()

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

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4763 of file varlena.c.

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

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

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

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

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

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4779 of file varlena.c.

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

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

◆ bpcharfastcmp_c()

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

Definition at line 2046 of file varlena.c.

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

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

Referenced by varstr_sortsupport().

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

Definition at line 2697 of file varlena.c.

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

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

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

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2880 of file varlena.c.

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

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

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

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1828 of file varlena.c.

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

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

Referenced by gbt_textcmp().

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

Definition at line 2713 of file varlena.c.

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

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

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

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1843 of file varlena.c.

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

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

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2552 of file varlena.c.

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

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

◆ build_concat_foutcache()

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

Definition at line 5342 of file varlena.c.

5343 {
5344  FmgrInfo *foutcache;
5345  int i;
5346 
5347  /* We keep the info in fn_mcxt so it survives across calls */
5348  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5349  PG_NARGS() * sizeof(FmgrInfo));
5350 
5351  for (i = argidx; i < PG_NARGS(); i++)
5352  {
5353  Oid valtype;
5354  Oid typOutput;
5355  bool typIsVarlena;
5356 
5357  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5358  if (!OidIsValid(valtype))
5359  elog(ERROR, "could not determine data type of concat() input");
5360 
5361  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5362  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5363  }
5364 
5365  fcinfo->flinfo->fn_extra = foutcache;
5366 
5367  return foutcache;
5368 }
#define OidIsValid(objectId)
Definition: c.h:764
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1893
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:203
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2889
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 3148 of file varlena.c.

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

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

◆ bytea_catenate()

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

Definition at line 2950 of file varlena.c.

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

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

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

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

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

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 549 of file varlena.c.

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

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

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

3003 {
3005  PG_GETARG_INT32(1),
3006  PG_GETARG_INT32(2),
3007  false));
3008 }
#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 3016 of file varlena.c.

3017 {
3019  PG_GETARG_INT32(1),
3020  -1,
3021  true));
3022 }

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

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

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

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

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2935 of file varlena.c.

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

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3935 of file varlena.c.

3936 {
3937  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3938  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3939  int len1,
3940  len2;
3941  int cmp;
3942 
3943  len1 = VARSIZE_ANY_EXHDR(arg1);
3944  len2 = VARSIZE_ANY_EXHDR(arg2);
3945 
3946  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3947  if ((cmp == 0) && (len1 != len2))
3948  cmp = (len1 < len2) ? -1 : 1;
3949 
3950  PG_FREE_IF_COPY(arg1, 0);
3951  PG_FREE_IF_COPY(arg2, 1);
3952 
3954 }
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 3791 of file varlena.c.

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

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

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

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

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

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

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

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

Referenced by gbt_byteagt().

◆ byteain()

Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 288 of file varlena.c.

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

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

Referenced by CreateTriggerFiringOn(), and string_to_datum().

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

Definition at line 3875 of file varlena.c.

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

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

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

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

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

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

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

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

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 386 of file varlena.c.

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

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

Referenced by pg_mcv_list_out().

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 3092 of file varlena.c.

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

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

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

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

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

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

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

Definition at line 469 of file varlena.c.

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

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

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 488 of file varlena.c.

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

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

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

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3305 of file varlena.c.

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

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

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

◆ charlen_to_bytelen()

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

Definition at line 804 of file varlena.c.

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

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

Definition at line 1507 of file varlena.c.

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

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

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

◆ check_replace_text_has_escape()

static int check_replace_text_has_escape ( const text replace_text)
static

Definition at line 4070 of file varlena.c.

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

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

5382 {
5383  text *result;
5385  FmgrInfo *foutcache;
5386  bool first_arg = true;
5387  int i;
5388 
5389  /*
5390  * concat(VARIADIC some-array) is essentially equivalent to
5391  * array_to_text(), ie concat the array elements with the given separator.
5392  * So we just pass the case off to that code.
5393  */
5394  if (get_fn_expr_variadic(fcinfo->flinfo))
5395  {
5396  ArrayType *arr;
5397 
5398  /* Should have just the one argument */
5399  Assert(argidx == PG_NARGS() - 1);
5400 
5401  /* concat(VARIADIC NULL) is defined as NULL */
5402  if (PG_ARGISNULL(argidx))
5403  return NULL;
5404 
5405  /*
5406  * Non-null argument had better be an array. We assume that any call
5407  * context that could let get_fn_expr_variadic return true will have
5408  * checked that a VARIADIC-labeled parameter actually is an array. So
5409  * it should be okay to just Assert that it's an array rather than
5410  * doing a full-fledged error check.
5411  */
5413 
5414  /* OK, safe to fetch the array value */
5415  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5416 
5417  /*
5418  * And serialize the array. We tell array_to_text to ignore null
5419  * elements, which matches the behavior of the loop below.
5420  */
5421  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5422  }
5423 
5424  /* Normal case without explicit VARIADIC marker */
5425  initStringInfo(&str);
5426 
5427  /* Get output function info, building it if first time through */
5428  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5429  if (foutcache == NULL)
5430  foutcache = build_concat_foutcache(fcinfo, argidx);
5431 
5432  for (i = argidx; i < PG_NARGS(); i++)
5433  {
5434  if (!PG_ARGISNULL(i))
5435  {
5437 
5438  /* add separator if appropriate */
5439  if (first_arg)
5440  first_arg = false;
5441  else
5442  appendStringInfoString(&str, sepstr);
5443 
5444  /* call the appropriate type output function, append the result */
5446  OutputFunctionCall(&foutcache[i], value));
5447  }
5448  }
5449 
5450  result = cstring_to_text_with_len(str.data, str.len);
5451  pfree(str.data);
5452 
5453  return result;
5454 }
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2027
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2814
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5342

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

Referenced by text_concat(), and text_concat_ws().

◆ convert_to_base()

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

Definition at line 4927 of file varlena.c.

4928 {
4929  const char *digits = "0123456789abcdef";
4930 
4931  /* We size the buffer for to_bin's longest possible return value. */
4932  char buf[sizeof(uint64) * BITS_PER_BYTE];
4933  char *const end = buf + sizeof(buf);
4934  char *ptr = end;
4935 
4936  Assert(base > 1);
4937  Assert(base <= 16);
4938 
4939  do
4940  {
4941  *--ptr = digits[value % base];
4942  value /= base;
4943  } while (ptr > buf && value);
4944 
4945  return cstring_to_text_with_len(ptr, end - ptr);
4946 }
int digits
Definition: informix.c:666
#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 182 of file varlena.c.

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

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_minmax_multi_summary_out(), brin_page_type(), build_test_info_result(), build_test_match_result(), cash_words(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), executeDateTimeMethod(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_scalar(), hash_page_type(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_get_element(), jsonb_typeof(), JsonbValueAsText(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), ParameterAclCreate(), ParameterAclLookup(), pg_collation_actual_version(), pg_collation_for(), pg_column_compression(), pg_crypt(), pg_current_logfile(), pg_database_collation_actual_version(), pg_describe_object(), pg_export_snapshot(), pg_get_statisticsobjdef_expressions(), pg_get_wal_replay_pause_state(), pg_parameter_aclmask(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_stat_get_slru(), pg_stats_ext_mcvlist_items(), pg_tablespace_location(), pg_walfile_name(), pg_xact_status(), pgsql_version(), pgxml_result_to_text(), quote_ident(), quote_ident_cstr(), quote_nullable(), sepgsql_getcon(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), set_config_by_name(), show_all_file_settings(), show_config_by_name(), show_config_by_name_missing_ok(), ShowAllGUCConfig(), soundex(), split_part(), ssl_cipher(), ssl_client_dn(), ssl_issuer_dn(), ssl_version(), string_to_text(), test_canonicalize_path(), test_slru_page_read(), test_slru_page_readonly(), text_substring(), textin(), timeofday(), tsquerytree(), unaccent_dict(), upper(), xml_encode_special_chars(), xml_in(), and xml_recv().

◆ cstring_to_text_with_len()

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

Definition at line 194 of file varlena.c.

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

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

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), brin_minmax_multi_summary_out(), build_regexp_match_result(), build_regexp_split_result(), build_test_match_result(), compute_tsvector_stats(), concat_internal(), 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(), 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(), and xslt_process().

◆ getClosestMatch()

const char* getClosestMatch ( ClosestMatchState state)

Definition at line 6201 of file varlena.c.

6202 {
6203  Assert(state);
6204 
6205  return state->match;
6206 }

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

6372 {
6373  if (c >= '0' && c <= '9')
6374  return c - '0';
6375  if (c >= 'a' && c <= 'f')
6376  return c - 'a' + 0xA;
6377  if (c >= 'A' && c <= 'F')
6378  return c - 'A' + 0xA;
6379  elog(ERROR, "invalid hexadecimal digit");
6380  return 0; /* not reached */
6381 }
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 6387 of file varlena.c.

6388 {
6389  unsigned int result = 0;
6390 
6391  for (size_t i = 0; i < n; i++)
6392  result += hexval(instr[i]) << (4 * (n - i - 1));
6393 
6394  return result;
6395 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6371

References hexval(), and i.

Referenced by unistr().

◆ initClosestMatch()

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

Definition at line 6146 of file varlena.c.

6147 {
6148  Assert(state);
6149  Assert(max_d >= 0);
6150 
6151  state->source = source;
6152  state->min_d = -1;
6153  state->max_d = max_d;
6154  state->match = NULL;
6155 }
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 2794 of file varlena.c.

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

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

6362 {
6363  for (size_t i = 0; i < n; i++)
6364  if (!isxdigit((unsigned char) instr[i]))
6365  return false;
6366 
6367  return true;
6368 }

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5118 of file varlena.c.

5119 {
5120  StringInfo state;
5121  MemoryContext aggcontext;
5122  MemoryContext oldcontext;
5123 
5124  if (!AggCheckCallContext(fcinfo, &aggcontext))
5125  {
5126  /* cannot be called directly because of internal-type argument */
5127  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5128  }
5129 
5130  /*
5131  * Create state in aggregate context. It'll stay there across subsequent
5132  * calls.
5133  */
5134  oldcontext = MemoryContextSwitchTo(aggcontext);
5135  state = makeStringInfo();
5136  MemoryContextSwitchTo(oldcontext);
5137 
5138  return state;
5139 }
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 3379 of file varlena.c.

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

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

Referenced by nameiclike(), and nameicnlike().

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

Definition at line 2597 of file varlena.c.

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

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

◆ namefastcmp_c()

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

Definition at line 2079 of file varlena.c.

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

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

Referenced by varstr_sortsupport().

◆ namefastcmp_locale()

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

Definition at line 2122 of file varlena.c.

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

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

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 2753 of file varlena.c.

2754 {
2756 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2728
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2697

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 2747 of file varlena.c.

2748 {
2750 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2741 of file varlena.c.

2742 {
2744 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2735 of file varlena.c.

2736 {
2738 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2647 of file varlena.c.

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

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

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

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

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

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3993 of file varlena.c.

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

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

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

◆ replace_text_regexp()

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

Definition at line 4203 of file varlena.c.

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

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

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

◆ rest_of_char_same()

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

Definition at line 6110 of file varlena.c.

6111 {
6112  while (len > 0)
6113  {
6114  len--;
6115  if (s1[len] != s2[len])
6116  return false;
6117  }
6118  return true;
6119 }

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4365 of file varlena.c.

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

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

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

Definition at line 4588 of file varlena.c.

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

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

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

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

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

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

Referenced by load_libraries(), and PostmasterMain().

◆ SplitGUCList()

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

Definition at line 3702 of file varlena.c.

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

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

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

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

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

◆ string_agg_combine()

Datum string_agg_combine ( PG_FUNCTION_ARGS  )

Definition at line 5197 of file varlena.c.

5198 {
5199  StringInfo state1;
5200  StringInfo state2;
5201  MemoryContext agg_context;
5202 
5203  if (!AggCheckCallContext(fcinfo, &agg_context))
5204  elog(ERROR, "aggregate function called in non-aggregate context");
5205 
5206  state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5207  state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
5208 
5209  if (state2 == NULL)
5210  {
5211  /*
5212  * NULL state2 is easy, just return state1, which we know is already
5213  * in the agg_context
5214  */
5215  if (state1 == NULL)
5216  PG_RETURN_NULL();
5217  PG_RETURN_POINTER(state1);
5218  }
5219 
5220  if (state1 == NULL)
5221  {
5222  /* We must copy state2's data into the agg_context */
5223  MemoryContext old_context;
5224 
5225  old_context = MemoryContextSwitchTo(agg_context);
5226  state1 = makeStringAggState(fcinfo);
5227  appendBinaryStringInfo(state1, state2->data, state2->len);
5228  state1->cursor = state2->cursor;
5229  MemoryContextSwitchTo(old_context);
5230  }
5231  else if (state2->len > 0)
5232  {
5233  /* Combine ... state1->cursor does not change in this case */
5234  appendBinaryStringInfo(state1, state2->data, state2->len);
5235  }
5236 
5237  PG_RETURN_POINTER(state1);
5238 }

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

5279 {
5280  bytea *sstate;
5281  StringInfo result;
5283  char *data;
5284  int datalen;
5285 
5286  /* cannot be called directly because of internal-type argument */
5287  Assert(AggCheckCallContext(fcinfo, NULL));
5288 
5289  sstate = PG_GETARG_BYTEA_PP(0);
5290 
5291  /*
5292  * Copy the bytea into a StringInfo so that we can "receive" it using the
5293  * standard recv-function infrastructure.
5294  */
5295  initStringInfo(&buf);
5297  VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5298 
5299  result = makeStringAggState(fcinfo);
5300 
5301  /* cursor */
5302  result->cursor = pq_getmsgint(&buf, 4);
5303 
5304  /* data */
5305  datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
5306  data = (char *) pq_getmsgbytes(&buf, datalen);
5307  appendBinaryStringInfo(result, data, datalen);
5308 
5309  pq_getmsgend(&buf);
5310  pfree(buf.data);
5311 
5312  PG_RETURN_POINTER(result);
5313 }
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:418
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:638
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:511

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

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 5316 of file varlena.c.

5317 {
5318  StringInfo state;
5319 
5320  /* cannot be called directly because of internal-type argument */
5321  Assert(AggCheckCallContext(fcinfo, NULL));
5322 
5323  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5324 
5325  if (state != NULL)
5326  {
5327  /* As per comment in transfn, strip data before the cursor position */
5329  state->len - state->cursor));
5330  }
5331  else
5332  PG_RETURN_NULL();
5333 }

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

5248 {
5249  StringInfo state;
5251  bytea *result;
5252 
5253  /* cannot be called directly because of internal-type argument */
5254  Assert(AggCheckCallContext(fcinfo, NULL));
5255 
5257 
5258  pq_begintypsend(&buf);
5259 
5260  /* cursor */
5261  pq_sendint(&buf, state->cursor, 4);
5262 
5263  /* data */
5264  pq_sendbytes(&buf, state->data, state->len);
5265 
5266  result = pq_endtypsend(&buf);
5267 
5268  PG_RETURN_BYTEA_P(result);
5269 }
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendint(StringInfo buf, uint32 i, int b)
Definition: pqformat.h:172

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

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5142 of file varlena.c.

5143 {
5144  StringInfo state;
5145 
5146  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5147 
5148  /* Append the value unless null, preceding it with the delimiter. */
5149  if (!PG_ARGISNULL(1))
5150  {
5152  bool isfirst = false;
5153 
5154  /*
5155  * You might think we can just throw away the first delimiter, however
5156  * we must keep it as we may be a parallel worker doing partial
5157  * aggregation building a state to send to the main process. We need
5158  * to keep the delimiter of every aggregation so that the combine
5159  * function can properly join up the strings of two separately
5160  * partially aggregated results. The first delimiter is only stripped
5161  * off in the final function. To know how much to strip off the front
5162  * of the string, we store the length of the first delimiter in the
5163  * StringInfo's cursor field, which we don't otherwise need here.
5164  */
5165  if (state == NULL)
5166  {
5167  state = makeStringAggState(fcinfo);
5168  isfirst = true;
5169  }
5170 
5171  if (!PG_ARGISNULL(2))
5172  {
5173  text *delim = PG_GETARG_TEXT_PP(2);
5174 
5175  appendStringInfoText(state, delim);
5176  if (isfirst)
5177  state->cursor = VARSIZE_ANY_EXHDR(delim);
5178  }
5179 
5181  }
5182 
5183  /*
5184  * The transition type for string_agg() is declared to be "internal",
5185  * which is a pass-by-value type the same size as a pointer.
5186  */
5187  if (state)
5189  PG_RETURN_NULL();
5190 }

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

◆ text_catenate()

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

Definition at line 763 of file varlena.c.

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

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

Referenced by text_overlay(), and textcat().

◆ text_cmp()

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

Definition at line 1591 of file varlena.c.

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

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

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

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 5460 of file varlena.c.

5461 {
5462  text *result;
5463 
5464  result = concat_internal("", 0, fcinfo);
5465  if (result == NULL)
5466  PG_RETURN_NULL();
5467  PG_RETURN_TEXT_P(result);
5468 }
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5380

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5475 of file varlena.c.

5476 {
5477  char *sep;
5478  text *result;
5479 
5480  /* return NULL when separator is NULL */
5481  if (PG_ARGISNULL(0))
5482  PG_RETURN_NULL();
5484 
5485  result = concat_internal(sep, 1, fcinfo);
5486  if (result == NULL)
5487  PG_RETURN_NULL();
5488  PG_RETURN_TEXT_P(result);
5489 }

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

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

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

Referenced by text_format_nv().

◆ text_format_append_string()

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

Definition at line 6048 of file varlena.c.

6050 {
6051  bool align_to_left = false;
6052  int len;
6053 
6054  /* fast path for typical easy case */
6055  if (width == 0)
6056  {
6058  return;
6059  }
6060 
6061  if (width < 0)
6062  {
6063  /* Negative width: implicit '-' flag, then take absolute value */
6064  align_to_left = true;
6065  /* -INT_MIN is undefined */
6066  if (width <= INT_MIN)
6067  ereport(ERROR,
6068  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6069  errmsg("number is out of range")));
6070  width = -width;
6071  }
6072  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6073  align_to_left = true;
6074 
6075  len = pg_mbstrlen(str);
6076  if (align_to_left)
6077  {
6078  /* left justify */
6080  if (len < width)
6081  appendStringInfoSpaces(buf, width - len);
6082  }
6083  else
6084  {
6085  /* right justify */
6086  if (len < width)
6087  appendStringInfoSpaces(buf, width - len);
6089  }
6090 }
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1038
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5581

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

Referenced by text_format_string_conversion().

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 6100 of file varlena.c.

6101 {
6102  return text_format(fcinfo);
6103 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5596

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

5874 {
5875  bool found = false;
5876  const char *cp = *ptr;
5877  int val = 0;
5878 
5879  while (*cp >= '0' && *cp <= '9')
5880  {
5881  int8 digit = (*cp - '0');
5882 
5883  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5884  unlikely(pg_add_s32_overflow(val, digit, &val)))
5885  ereport(ERROR,
5886  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5887  errmsg("number is out of range")));
5888  ADVANCE_PARSE_POINTER(cp, end_ptr);
5889  found = true;
5890  }
5891 
5892  *ptr = cp;
5893  *value = val;
5894 
5895  return found;
5896 }
signed char int8
Definition: c.h:481
#define unlikely(x)
Definition: c.h:300
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140

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

Referenced by text_format_parse_format().

◆ text_format_parse_format()

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

Definition at line 5922 of file varlena.c.

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

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

6003 {
6004  char *str;
6005 
6006  /* Handle NULL arguments before trying to stringify the value. */
6007  if (isNull)
6008  {
6009  if (conversion == 's')
6010  text_format_append_string(buf, "", flags, width);
6011  else if (conversion == 'L')
6012  text_format_append_string(buf, "NULL", flags, width);
6013  else if (conversion == 'I')
6014  ereport(ERROR,
6015  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6016  errmsg("null values cannot be formatted as an SQL identifier")));
6017  return;
6018  }
6019 
6020  /* Stringify. */
6021  str = OutputFunctionCall(typOutputInfo, value);
6022 
6023  /* Escape. */
6024  if (conversion == 'I')
6025  {
6026  /* quote_identifier may or may not allocate a new string. */
6028  }
6029  else if (conversion == 'L')
6030  {
6031  char *qstr = quote_literal_cstr(str);
6032 
6033  text_format_append_string(buf, qstr, flags, width);
6034  /* quote_literal_cstr() always allocates a new string */
6035  pfree(qstr);
6036  }
6037  else
6038  text_format_append_string(buf, str, flags, width);
6039 
6040  /* Cleanup. */
6041  pfree(str);
6042 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11965
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6048

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

Referenced by text_format().

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1773 of file varlena.c.

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

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

Referenced by gbt_textge().

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1758 of file varlena.c.

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

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

Referenced by gbt_textgt().

◆ text_isequal()

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

Definition at line 4497 of file varlena.c.

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

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

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2568 of file varlena.c.

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

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

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1743 of file varlena.c.

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

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

Referenced by gbt_textle().

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 5496 of file varlena.c.

5497 {
5498  int n = PG_GETARG_INT32(1);
5499 
5500  if (n < 0)
5501  {
5502  text *str = PG_GETARG_TEXT_PP(0);
5503  const char *p = VARDATA_ANY(str);
5504  int len = VARSIZE_ANY_EXHDR(str);
5505  int rlen;
5506 
5507  n = pg_mbstrlen_with_len(p, len) + n;
5508  rlen = pg_mbcharcliplen(p, len, n);
5510  }
5511  else
5513 }
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1058
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1126
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:883

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

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 709 of file varlena.c.

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

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

Referenced by textlen(), and textoverlay_no_len().

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1728 of file varlena.c.

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

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

Referenced by gbt_textlt().

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3356 of file varlena.c.

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

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

◆ text_overlay()

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

Definition at line 1113 of file varlena.c.

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

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

Referenced by textoverlay(), and textoverlay_no_len().

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 2848 of file varlena.c.

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

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

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

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

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

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

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

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

◆ text_position()

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

Definition at line 1173 of file varlena.c.

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

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

Referenced by textpos().

◆ text_position_cleanup()

static void text_position_cleanup ( TextPositionState state)
static

Definition at line 1500 of file varlena.c.

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

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

◆ text_position_get_match_pos()

static int text_position_get_match_pos ( TextPositionState state)
static

Definition at line 1476 of file varlena.c.

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

References pg_mbstrlen_with_len().

Referenced by text_position().

◆ text_position_get_match_ptr()

static char * text_position_get_match_ptr ( TextPositionState state)
static

Definition at line 1465 of file varlena.c.

1466 {
1467  return state->last_match;
1468 }

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

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1333 of file varlena.c.

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

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

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

◆ text_position_next_internal()

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

Definition at line 1397 of file varlena.c.

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

References Assert().

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1492 of file varlena.c.

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

Referenced by split_part().

◆ text_position_setup()

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

Definition at line 1213 of file varlena.c.

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

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

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

◆ text_reverse()

Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 5541 of file varlena.c.

5542 {
5543  text *str = PG_GETARG_TEXT_PP(0);
5544  const char *p = VARDATA_ANY(str);
5545  int len = VARSIZE_ANY_EXHDR(str);
5546  const char *endp = p + len;
5547  text *result;
5548  char *dst;
5549 
5550  result = palloc(len + VARHDRSZ);
5551  dst = (char *) VARDATA(result) + len;
5552  SET_VARSIZE(result, len + VARHDRSZ);
5553 
5555  {
5556  /* multibyte version */
5557  while (p < endp)
5558  {
5559  int sz;
5560 
5561  sz = pg_mblen(p);
5562  dst -= sz;
5563  memcpy(dst, p, sz);
5564  p += sz;
5565  }
5566  }
5567  else
5568  {
5569  /* single byte version */
5570  while (p < endp)
5571  *(--dst) = *p++;
5572  }
5573 
5574  PG_RETURN_TEXT_P(result);
5575 }

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

◆ text_right()

Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 5520 of file varlena.c.

5521 {
5522  text *str = PG_GETARG_TEXT_PP(0);
5523  const char *p = VARDATA_ANY(str);
5524  int len = VARSIZE_ANY_EXHDR(str);
5525  int n = PG_GETARG_INT32(1);
5526  int off;
5527 
5528  if (n < 0)
5529  n = -n;
5530  else
5531  n = pg_mbstrlen_with_len(p, len) - n;
5532  off = pg_mbcharcliplen(p, len, n);
5533 
5535 }

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

◆ text_smaller()

Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2580 of file varlena.c.

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

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

◆ text_starts_with()

Datum text_starts_with ( PG_FUNCTION_ARGS  )

Definition at line 1788 of file varlena.c.

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

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

Referenced by spg_text_leaf_consistent().

◆ text_substr()

◆ text_substr_no_len()

Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 864 of file varlena.c.

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

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

◆ text_substring()

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

Definition at line 883 of file varlena.c.

884 {
886  int32 S = start; /* start position */
887  int32 S1; /* adjusted start position */
888  int32 L1; /* adjusted substring length */
889  int32 E; /* end position */
890 
891  /*
892  * SQL99 says S can be zero or negative, but we still must fetch from the
893  * start of the string.
894