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/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 DatumGetUnknownP(X)   ((unknown *) PG_DETOAST_DATUM(X))
 
#define DatumGetUnknownPCopy(X)   ((unknown *) PG_DETOAST_DATUM_COPY(X))
 
#define PG_GETARG_UNKNOWN_P(n)   DatumGetUnknownP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_UNKNOWN_P_COPY(n)   DatumGetUnknownPCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_UNKNOWN_P(x)   PG_RETURN_POINTER(x)
 
#define DatumGetVarStringP(X)   ((VarString *) PG_DETOAST_DATUM(X))
 
#define DatumGetVarStringPP(X)   ((VarString *) PG_DETOAST_DATUM_PACKED(X))
 
#define VAL(CH)   ((CH) - '0')
 
#define DIG(VAL)   ((VAL) + '0')
 
#define CmpCall(cmpfunc)
 
#define PG_STR_GET_BYTEA(str_)    DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#define HEXBASE   16
 
#define TEXT_FORMAT_FLAG_MINUS   0x0001 /* is minus flag present? */
 
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
 
#define LEVENSHTEIN_LESS_EQUAL
 

Typedefs

typedef struct varlena unknown
 
typedef struct varlena VarString
 

Functions

static int varstrfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int bpcharfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int varlenafastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int varstrfastcmp_locale (char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
 
static Datum varstr_abbrev_convert (Datum original, SortSupport ssup)
 
static bool varstr_abbrev_abort (int memtupcount, SortSupport ssup)
 
static int32 text_length (Datum str)
 
static texttext_catenate (text *t1, text *t2)
 
static texttext_substring (Datum str, int32 start, int32 length, bool length_not_specified)
 
static texttext_overlay (text *t1, text *t2, int sp, int sl)
 
static int text_position (text *t1, text *t2, Oid collid)
 
static void text_position_setup (text *t1, text *t2, Oid collid, TextPositionState *state)
 
static bool text_position_next (TextPositionState *state)
 
static char * text_position_next_internal (char *start_ptr, TextPositionState *state)
 
static char * text_position_get_match_ptr (TextPositionState *state)
 
static int text_position_get_match_pos (TextPositionState *state)
 
static void text_position_cleanup (TextPositionState *state)
 
static void check_collation_set (Oid collid)
 
static int text_cmp (text *arg1, text *arg2, Oid collid)
 
static byteabytea_catenate (bytea *t1, bytea *t2)
 
static byteabytea_substring (Datum str, int S, int L, bool length_not_specified)
 
static byteabytea_overlay (bytea *t1, bytea *t2, int sp, int sl)
 
static void appendStringInfoText (StringInfo str, const text *t)
 
static bool split_text (FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
 
static void split_text_accum_result (SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
 
static textarray_to_text_internal (FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
 
static StringInfo makeStringAggState (FunctionCallInfo fcinfo)
 
static bool text_format_parse_digits (const char **ptr, const char *end_ptr, int *value)
 
static const char * text_format_parse_format (const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
 
static void text_format_string_conversion (StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
 
static void text_format_append_string (StringInfo buf, const char *str, int flags, int width)
 
textcstring_to_text (const char *s)
 
textcstring_to_text_with_len (const char *s, int len)
 
char * text_to_cstring (const text *t)
 
void text_to_cstring_buffer (const text *src, char *dst, size_t dst_len)
 
Datum byteain (PG_FUNCTION_ARGS)
 
Datum byteaout (PG_FUNCTION_ARGS)
 
Datum bytearecv (PG_FUNCTION_ARGS)
 
Datum byteasend (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum textin (PG_FUNCTION_ARGS)
 
Datum textout (PG_FUNCTION_ARGS)
 
Datum textrecv (PG_FUNCTION_ARGS)
 
Datum textsend (PG_FUNCTION_ARGS)
 
Datum unknownin (PG_FUNCTION_ARGS)
 
Datum unknownout (PG_FUNCTION_ARGS)
 
Datum unknownrecv (PG_FUNCTION_ARGS)
 
Datum unknownsend (PG_FUNCTION_ARGS)
 
Datum textlen (PG_FUNCTION_ARGS)
 
Datum textoctetlen (PG_FUNCTION_ARGS)
 
Datum textcat (PG_FUNCTION_ARGS)
 
static int charlen_to_bytelen (const char *p, int n)
 
Datum text_substr (PG_FUNCTION_ARGS)
 
Datum text_substr_no_len (PG_FUNCTION_ARGS)
 
Datum textoverlay (PG_FUNCTION_ARGS)
 
Datum textoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum textpos (PG_FUNCTION_ARGS)
 
static void text_position_reset (TextPositionState *state)
 
int varstr_cmp (const char *arg1, int len1, const char *arg2, int len2, Oid collid)
 
Datum texteq (PG_FUNCTION_ARGS)
 
Datum textne (PG_FUNCTION_ARGS)
 
Datum text_lt (PG_FUNCTION_ARGS)
 
Datum text_le (PG_FUNCTION_ARGS)
 
Datum text_gt (PG_FUNCTION_ARGS)
 
Datum text_ge (PG_FUNCTION_ARGS)
 
Datum text_starts_with (PG_FUNCTION_ARGS)
 
Datum bttextcmp (PG_FUNCTION_ARGS)
 
Datum bttextsortsupport (PG_FUNCTION_ARGS)
 
void varstr_sortsupport (SortSupport ssup, Oid typid, Oid collid)
 
Datum btvarstrequalimage (PG_FUNCTION_ARGS)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
Datum nameeqtext (PG_FUNCTION_ARGS)
 
Datum texteqname (PG_FUNCTION_ARGS)
 
Datum namenetext (PG_FUNCTION_ARGS)
 
Datum textnename (PG_FUNCTION_ARGS)
 
Datum btnametextcmp (PG_FUNCTION_ARGS)
 
Datum bttextnamecmp (PG_FUNCTION_ARGS)
 
Datum namelttext (PG_FUNCTION_ARGS)
 
Datum nameletext (PG_FUNCTION_ARGS)
 
Datum namegttext (PG_FUNCTION_ARGS)
 
Datum namegetext (PG_FUNCTION_ARGS)
 
Datum textltname (PG_FUNCTION_ARGS)
 
Datum textlename (PG_FUNCTION_ARGS)
 
Datum textgtname (PG_FUNCTION_ARGS)
 
Datum textgename (PG_FUNCTION_ARGS)
 
static int internal_text_pattern_compare (text *arg1, text *arg2)
 
Datum text_pattern_lt (PG_FUNCTION_ARGS)
 
Datum text_pattern_le (PG_FUNCTION_ARGS)
 
Datum text_pattern_ge (PG_FUNCTION_ARGS)
 
Datum text_pattern_gt (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_sortsupport (PG_FUNCTION_ARGS)
 
Datum byteaoctetlen (PG_FUNCTION_ARGS)
 
Datum byteacat (PG_FUNCTION_ARGS)
 
Datum bytea_substr (PG_FUNCTION_ARGS)
 
Datum bytea_substr_no_len (PG_FUNCTION_ARGS)
 
Datum byteaoverlay (PG_FUNCTION_ARGS)
 
Datum byteaoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum bytea_bit_count (PG_FUNCTION_ARGS)
 
Datum byteapos (PG_FUNCTION_ARGS)
 
Datum byteaGetByte (PG_FUNCTION_ARGS)
 
Datum byteaGetBit (PG_FUNCTION_ARGS)
 
Datum byteaSetByte (PG_FUNCTION_ARGS)
 
Datum byteaSetBit (PG_FUNCTION_ARGS)
 
Datum text_name (PG_FUNCTION_ARGS)
 
Datum name_text (PG_FUNCTION_ARGS)
 
ListtextToQualifiedNameList (text *textval)
 
bool SplitIdentifierString (char *rawstring, char separator, List **namelist)
 
bool SplitDirectoriesString (char *rawstring, char separator, List **namelist)
 
bool SplitGUCList (char *rawstring, char separator, List **namelist)
 
Datum byteaeq (PG_FUNCTION_ARGS)
 
Datum byteane (PG_FUNCTION_ARGS)
 
Datum bytealt (PG_FUNCTION_ARGS)
 
Datum byteale (PG_FUNCTION_ARGS)
 
Datum byteagt (PG_FUNCTION_ARGS)
 
Datum byteage (PG_FUNCTION_ARGS)
 
Datum byteacmp (PG_FUNCTION_ARGS)
 
Datum bytea_sortsupport (PG_FUNCTION_ARGS)
 
Datum replace_text (PG_FUNCTION_ARGS)
 
static int check_replace_text_has_escape (const text *replace_text)
 
static void appendStringInfoRegexpSubstr (StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
 
textreplace_text_regexp (text *src_text, text *pattern_text, text *replace_text, int cflags, Oid collation, int search_start, int n)
 
Datum split_part (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2, Oid collid)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_null (PG_FUNCTION_ARGS)
 
Datum text_to_table (PG_FUNCTION_ARGS)
 
Datum text_to_table_null (PG_FUNCTION_ARGS)
 
Datum array_to_text (PG_FUNCTION_ARGS)
 
Datum array_to_text_null (PG_FUNCTION_ARGS)
 
Datum to_hex32 (PG_FUNCTION_ARGS)
 
Datum to_hex64 (PG_FUNCTION_ARGS)
 
Datum pg_column_size (PG_FUNCTION_ARGS)
 
Datum pg_column_compression (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_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)
 
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:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33

Definition at line 5659 of file varlena.c.

◆ CmpCall

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

Definition at line 2989 of file varlena.c.

◆ DatumGetUnknownP

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

Definition at line 114 of file varlena.c.

◆ DatumGetUnknownPCopy

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

Definition at line 115 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 120 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 121 of file varlena.c.

◆ DIG

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

Definition at line 280 of file varlena.c.

◆ HEXBASE

#define HEXBASE   16

Definition at line 5184 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6199 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P

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

Definition at line 116 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P_COPY

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

Definition at line 117 of file varlena.c.

◆ PG_RETURN_UNKNOWN_P

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 118 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 3244 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

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

Typedef Documentation

◆ unknown

typedef struct varlena unknown

Definition at line 45 of file varlena.c.

◆ VarString

typedef struct varlena VarString

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

4367 {
4368  const char *p = VARDATA_ANY(replace_text);
4369  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4370 
4371  while (p < p_end)
4372  {
4373  const char *chunk_start = p;
4374  int so;
4375  int eo;
4376 
4377  /* Find next escape char, if any. */
4378  p = memchr(p, '\\', p_end - p);
4379  if (p == NULL)
4380  p = p_end;
4381 
4382  /* Copy the text we just scanned over, if any. */
4383  if (p > chunk_start)
4384  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4385 
4386  /* Done if at end of string, else advance over escape char. */
4387  if (p >= p_end)
4388  break;
4389  p++;
4390 
4391  if (p >= p_end)
4392  {
4393  /* Escape at very end of input. Treat same as unexpected char */
4394  appendStringInfoChar(str, '\\');
4395  break;
4396  }
4397 
4398  if (*p >= '1' && *p <= '9')
4399  {
4400  /* Use the back reference of regexp. */
4401  int idx = *p - '0';
4402 
4403  so = pmatch[idx].rm_so;
4404  eo = pmatch[idx].rm_eo;
4405  p++;
4406  }
4407  else if (*p == '&')
4408  {
4409  /* Use the entire matched string. */
4410  so = pmatch[0].rm_so;
4411  eo = pmatch[0].rm_eo;
4412  p++;
4413  }
4414  else if (*p == '\\')
4415  {
4416  /* \\ means transfer one \ to output. */
4417  appendStringInfoChar(str, '\\');
4418  p++;
4419  continue;
4420  }
4421  else
4422  {
4423  /*
4424  * If escape char is not followed by any expected char, just treat
4425  * it as ordinary data to copy. (XXX would it be better to throw
4426  * an error?)
4427  */
4428  appendStringInfoChar(str, '\\');
4429  continue;
4430  }
4431 
4432  if (so >= 0 && eo >= 0)
4433  {
4434  /*
4435  * Copy the text that is back reference of regexp. Note so and eo
4436  * are counted in characters not bytes.
4437  */
4438  char *chunk_start;
4439  int chunk_len;
4440 
4441  Assert(so >= data_pos);
4442  chunk_start = start_ptr;
4443  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4444  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4445  appendBinaryStringInfo(str, chunk_start, chunk_len);
4446  }
4447  }
4448 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Assert(fmt[strlen(fmt) - 1] !='\n')
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void appendBinaryStringInfo(StringInfo str, const char *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
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:785
Datum replace_text(PG_FUNCTION_ARGS)
Definition: varlena.c:4254

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

5026 {
5028  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5029 
5030  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
5031 }
#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:221
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5067

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

5069 {
5070  text *result;
5071  int nitems,
5072  *dims,
5073  ndims;
5074  Oid element_type;
5075  int typlen;
5076  bool typbyval;
5077  char typalign;
5079  bool printed = false;
5080  char *p;
5081  bits8 *bitmap;
5082  int bitmask;
5083  int i;
5084  ArrayMetaState *my_extra;
5085 
5086  ndims = ARR_NDIM(v);
5087  dims = ARR_DIMS(v);
5088  nitems = ArrayGetNItems(ndims, dims);
5089 
5090  /* if there are no elements, return an empty string */
5091  if (nitems == 0)
5092  return cstring_to_text_with_len("", 0);
5093 
5094  element_type = ARR_ELEMTYPE(v);
5095  initStringInfo(&buf);
5096 
5097  /*
5098  * We arrange to look up info about element type, including its output
5099  * conversion proc, only once per series of calls, assuming the element
5100  * type doesn't change underneath us.
5101  */
5102  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5103  if (my_extra == NULL)
5104  {
5105  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5106  sizeof(ArrayMetaState));
5107  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5108  my_extra->element_type = ~element_type;
5109  }
5110 
5111  if (my_extra->element_type != element_type)
5112  {
5113  /*
5114  * Get info about element type, including its output conversion proc
5115  */
5116  get_type_io_data(element_type, IOFunc_output,
5117  &my_extra->typlen, &my_extra->typbyval,
5118  &my_extra->typalign, &my_extra->typdelim,
5119  &my_extra->typioparam, &my_extra->typiofunc);
5120  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
5121  fcinfo->flinfo->fn_mcxt);
5122  my_extra->element_type = element_type;
5123  }
5124  typlen = my_extra->typlen;
5125  typbyval = my_extra->typbyval;
5126  typalign = my_extra->typalign;
5127 
5128  p = ARR_DATA_PTR(v);
5129  bitmap = ARR_NULLBITMAP(v);
5130  bitmask = 1;
5131 
5132  for (i = 0; i < nitems; i++)
5133  {
5134  Datum itemvalue;
5135  char *value;
5136 
5137  /* Get source element, checking for NULL */
5138  if (bitmap && (*bitmap & bitmask) == 0)
5139  {
5140  /* if null_string is NULL, we just ignore null elements */
5141  if (null_string != NULL)
5142  {
5143  if (printed)
5144  appendStringInfo(&buf, "%s%s", fldsep, null_string);
5145  else
5146  appendStringInfoString(&buf, null_string);
5147  printed = true;
5148  }
5149  }
5150  else
5151  {
5152  itemvalue = fetch_att(p, typbyval, typlen);
5153 
5154  value = OutputFunctionCall(&my_extra->proc, itemvalue);
5155 
5156  if (printed)
5157  appendStringInfo(&buf, "%s%s", fldsep, value);
5158  else
5160  printed = true;
5161 
5162  p = att_addlength_pointer(p, typlen, p);
5163  p = (char *) att_align_nominal(p, typalign);
5164  }
5165 
5166  /* advance bitmap pointer if any */
5167  if (bitmap)
5168  {
5169  bitmask <<= 1;
5170  if (bitmask == 0x100)
5171  {
5172  bitmap++;
5173  bitmask = 1;
5174  }
5175  }
5176  }
5177 
5178  result = cstring_to_text_with_len(buf.data, buf.len);
5179  pfree(buf.data);
5180 
5181  return result;
5182 }
#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:459
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1559
static struct @151 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:2282
@ IOFunc_output
Definition: lsyscache.h:36
void pfree(void *pointer)
Definition: mcxt.c:1175
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
static char * buf
Definition: pg_test_fsync.c:67
char typalign
Definition: pg_type.h:176
uintptr_t Datum
Definition: postgres.h:411
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:633
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200

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

5042 {
5043  ArrayType *v;
5044  char *fldsep;
5045  char *null_string;
5046 
5047  /* returns NULL when first or second parameter is NULL */
5048  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
5049  PG_RETURN_NULL();
5050 
5051  v = PG_GETARG_ARRAYTYPE_P(0);
5052  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5053 
5054  /* NULL null string is passed through as a null pointer */
5055  if (!PG_ARGISNULL(2))
5056  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
5057  else
5058  null_string = NULL;
5059 
5060  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
5061 }
#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 2209 of file varlena.c.

2210 {
2211  BpChar *arg1 = DatumGetBpCharPP(x);
2212  BpChar *arg2 = DatumGetBpCharPP(y);
2213  char *a1p,
2214  *a2p;
2215  int len1,
2216  len2,
2217  result;
2218 
2219  a1p = VARDATA_ANY(arg1);
2220  a2p = VARDATA_ANY(arg2);
2221 
2222  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2223  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2224 
2225  result = memcmp(a1p, a2p, Min(len1, len2));
2226  if ((result == 0) && (len1 != len2))
2227  result = (len1 < len2) ? -1 : 1;
2228 
2229  /* We can't afford to leak memory here. */
2230  if (PointerGetDatum(arg1) != x)
2231  pfree(arg1);
2232  if (PointerGetDatum(arg2) != y)
2233  pfree(arg2);
2234 
2235  return result;
2236 }
#define Min(x, y)
Definition: c.h:997
#define DatumGetBpCharPP(X)
Definition: fmgr.h:293
int y
Definition: isn.c:72
int x
Definition: isn.c:71
#define PointerGetDatum(X)
Definition: postgres.h:600
int bpchartruelen(char *s, int len)
Definition: varchar.c:671

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

2959 {
2960  Name arg1 = PG_GETARG_NAME(0);
2961  text *arg2 = PG_GETARG_TEXT_PP(1);
2962  int32 result;
2963 
2964  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2965  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2966  PG_GET_COLLATION());
2967 
2968  PG_FREE_IF_COPY(arg2, 1);
2969 
2970  PG_RETURN_INT32(result);
2971 }
#define NameStr(name)
Definition: c.h:692
signed int int32
Definition: c.h:440
#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:687
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1517

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

3142 {
3143  text *arg1 = PG_GETARG_TEXT_PP(0);
3144  text *arg2 = PG_GETARG_TEXT_PP(1);
3145  int result;
3146 
3147  result = internal_text_pattern_compare(arg1, arg2);
3148 
3149  PG_FREE_IF_COPY(arg1, 0);
3150  PG_FREE_IF_COPY(arg2, 1);
3151 
3152  PG_RETURN_INT32(result);
3153 }
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3055

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

3158 {
3160  MemoryContext oldcontext;
3161 
3162  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3163 
3164  /* Use generic string SortSupport, forcing "C" collation */
3165  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
3166 
3167  MemoryContextSwitchTo(oldcontext);
3168 
3169  PG_RETURN_VOID();
3170 }
#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:109
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:2011

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1969 of file varlena.c.

1970 {
1971  text *arg1 = PG_GETARG_TEXT_PP(0);
1972  text *arg2 = PG_GETARG_TEXT_PP(1);
1973  int32 result;
1974 
1975  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1976 
1977  PG_FREE_IF_COPY(arg1, 0);
1978  PG_FREE_IF_COPY(arg2, 1);
1979 
1980  PG_RETURN_INT32(result);
1981 }
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1732

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

2975 {
2976  text *arg1 = PG_GETARG_TEXT_PP(0);
2977  Name arg2 = PG_GETARG_NAME(1);
2978  int32 result;
2979 
2980  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
2981  NameStr(*arg2), strlen(NameStr(*arg2)),
2982  PG_GET_COLLATION());
2983 
2984  PG_FREE_IF_COPY(arg1, 0);
2985 
2986  PG_RETURN_INT32(result);
2987 }

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

1985 {
1987  Oid collid = ssup->ssup_collation;
1988  MemoryContext oldcontext;
1989 
1990  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1991 
1992  /* Use generic string SortSupport */
1993  varstr_sortsupport(ssup, TEXTOID, collid);
1994 
1995  MemoryContextSwitchTo(oldcontext);
1996 
1997  PG_RETURN_VOID();
1998 }

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

2814 {
2815  /* Oid opcintype = PG_GETARG_OID(0); */
2816  Oid collid = PG_GET_COLLATION();
2817 
2818  check_collation_set(collid);
2819 
2820  if (lc_collate_is_c(collid) ||
2821  collid == DEFAULT_COLLATION_OID ||
2823  PG_RETURN_BOOL(true);
2824  else
2825  PG_RETURN_BOOL(false);
2826 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1079
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1326
static void check_collation_set(Oid collid)
Definition: varlena.c:1488

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

5419 {
5420  FmgrInfo *foutcache;
5421  int i;
5422 
5423  /* We keep the info in fn_mcxt so it survives across calls */
5424  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5425  PG_NARGS() * sizeof(FmgrInfo));
5426 
5427  for (i = argidx; i < PG_NARGS(); i++)
5428  {
5429  Oid valtype;
5430  Oid typOutput;
5431  bool typIsVarlena;
5432 
5433  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5434  if (!OidIsValid(valtype))
5435  elog(ERROR, "could not determine data type of concat() input");
5436 
5437  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5438  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5439  }
5440 
5441  fcinfo->flinfo->fn_extra = foutcache;
5442 
5443  return foutcache;
5444 }
#define OidIsValid(objectId)
Definition: c.h:721
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1786
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:203
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2864
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 3409 of file varlena.c.

3410 {
3411  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3412 
3414 }
#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 3211 of file varlena.c.

3212 {
3213  bytea *result;
3214  int len1,
3215  len2,
3216  len;
3217  char *ptr;
3218 
3219  len1 = VARSIZE_ANY_EXHDR(t1);
3220  len2 = VARSIZE_ANY_EXHDR(t2);
3221 
3222  /* paranoia ... probably should throw error instead? */
3223  if (len1 < 0)
3224  len1 = 0;
3225  if (len2 < 0)
3226  len2 = 0;
3227 
3228  len = len1 + len2 + VARHDRSZ;
3229  result = (bytea *) palloc(len);
3230 
3231  /* Set size of result string... */
3232  SET_VARSIZE(result, len);
3233 
3234  /* Fill data field of result string... */
3235  ptr = VARDATA(result);
3236  if (len1 > 0)
3237  memcpy(ptr, VARDATA_ANY(t1), len1);
3238  if (len2 > 0)
3239  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
3240 
3241  return result;
3242 }
#define VARHDRSZ
Definition: c.h:638
void * palloc(Size size)
Definition: mcxt.c:1068
const void size_t len
#define VARDATA(PTR)
Definition: postgres.h:315
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

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

3377 {
3378  bytea *result;
3379  bytea *s1;
3380  bytea *s2;
3381  int sp_pl_sl;
3382 
3383  /*
3384  * Check for possible integer-overflow cases. For negative sp, throw a
3385  * "substring length" error because that's what should be expected
3386  * according to the spec's definition of OVERLAY().
3387  */
3388  if (sp <= 0)
3389  ereport(ERROR,
3390  (errcode(ERRCODE_SUBSTRING_ERROR),
3391  errmsg("negative substring length not allowed")));
3392  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3393  ereport(ERROR,
3394  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3395  errmsg("integer out of range")));
3396 
3397  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3398  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3399  result = bytea_catenate(s1, t2);
3400  result = bytea_catenate(result, s2);
3401 
3402  return result;
3403 }
#define ereport(elevel,...)
Definition: elog.h:143
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:3286
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3211

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

4219 {
4221  MemoryContext oldcontext;
4222 
4223  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
4224 
4225  /* Use generic string SortSupport, forcing "C" collation */
4226  varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
4227 
4228  MemoryContextSwitchTo(oldcontext);
4229 
4230  PG_RETURN_VOID();
4231 }

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

533 {
535 
536  /* cannot be called directly because of internal-type argument */
537  Assert(AggCheckCallContext(fcinfo, NULL));
538 
539  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
540 
541  if (state != NULL)
542  {
543  bytea *result;
544 
545  result = (bytea *) palloc(state->len + VARHDRSZ);
546  SET_VARSIZE(result, state->len + VARHDRSZ);
547  memcpy(VARDATA(result), state->data, state->len);
548  PG_RETURN_BYTEA_P(result);
549  }
550  else
551  PG_RETURN_NULL();
552 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4487
StringInfoData * StringInfo
Definition: stringinfo.h:44
Definition: regguts.h:318

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

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 500 of file varlena.c.

501 {
503 
504  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
505 
506  /* Append the value unless null. */
507  if (!PG_ARGISNULL(1))
508  {
510 
511  /* On the first time through, we ignore the delimiter. */
512  if (state == NULL)
513  state = makeStringAggState(fcinfo);
514  else if (!PG_ARGISNULL(2))
515  {
516  bytea *delim = PG_GETARG_BYTEA_PP(2);
517 
519  }
520 
522  }
523 
524  /*
525  * The transition type for string_agg() is declared to be "internal",
526  * which is a pass-by-value type the same size as a pointer.
527  */
529 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5346

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

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 3263 of file varlena.c.

3264 {
3266  PG_GETARG_INT32(1),
3267  PG_GETARG_INT32(2),
3268  false));
3269 }
#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 3277 of file varlena.c.

3278 {
3280  PG_GETARG_INT32(1),
3281  -1,
3282  true));
3283 }

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

3290 {
3291  int32 S1; /* adjusted start position */
3292  int32 L1; /* adjusted substring length */
3293  int32 E; /* end position */
3294 
3295  /*
3296  * The logic here should generally match text_substring().
3297  */
3298  S1 = Max(S, 1);
3299 
3300  if (length_not_specified)
3301  {
3302  /*
3303  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3304  * end of the string if we pass it a negative value for length.
3305  */
3306  L1 = -1;
3307  }
3308  else if (L < 0)
3309  {
3310  /* SQL99 says to throw an error for E < S, i.e., negative length */
3311  ereport(ERROR,
3312  (errcode(ERRCODE_SUBSTRING_ERROR),
3313  errmsg("negative substring length not allowed")));
3314  L1 = -1; /* silence stupider compilers */
3315  }
3316  else if (pg_add_s32_overflow(S, L, &E))
3317  {
3318  /*
3319  * L could be large enough for S + L to overflow, in which case the
3320  * substring must run to end of string.
3321  */
3322  L1 = -1;
3323  }
3324  else
3325  {
3326  /*
3327  * A zero or negative value for the end position can happen if the
3328  * start was negative or one. SQL99 says to return a zero-length
3329  * string.
3330  */
3331  if (E < 1)
3332  return PG_STR_GET_BYTEA("");
3333 
3334  L1 = E - S1;
3335  }
3336 
3337  /*
3338  * If the start position is past the end of the string, SQL99 says to
3339  * return a zero-length string -- DatumGetByteaPSlice() will do that for
3340  * us. We need only convert S1 to zero-based starting position.
3341  */
3342  return DatumGetByteaPSlice(str, S1 - 1, L1);
3343 }
#define Max(x, y)
Definition: c.h:991
#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:3244

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

3197 {
3198  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3199  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3200 
3202 }

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 4196 of file varlena.c.

4197 {
4198  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4199  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4200  int len1,
4201  len2;
4202  int cmp;
4203 
4204  len1 = VARSIZE_ANY_EXHDR(arg1);
4205  len2 = VARSIZE_ANY_EXHDR(arg2);
4206 
4207  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4208  if ((cmp == 0) && (len1 != len2))
4209  cmp = (len1 < len2) ? -1 : 1;
4210 
4211  PG_FREE_IF_COPY(arg1, 0);
4212  PG_FREE_IF_COPY(arg2, 1);
4213 
4215 }
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

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

Referenced by gbt_bitcmp(), and gbt_byteacmp().

◆ byteaeq()

Datum byteaeq ( PG_FUNCTION_ARGS  )

Definition at line 4052 of file varlena.c.

4053 {
4054  Datum arg1 = PG_GETARG_DATUM(0);
4055  Datum arg2 = PG_GETARG_DATUM(1);
4056  bool result;
4057  Size len1,
4058  len2;
4059 
4060  /*
4061  * We can use a fast path for unequal lengths, which might save us from
4062  * having to detoast one or both values.
4063  */
4064  len1 = toast_raw_datum_size(arg1);
4065  len2 = toast_raw_datum_size(arg2);
4066  if (len1 != len2)
4067  result = false;
4068  else
4069  {
4070  bytea *barg1 = DatumGetByteaPP(arg1);
4071  bytea *barg2 = DatumGetByteaPP(arg2);
4072 
4073  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4074  len1 - VARHDRSZ) == 0);
4075 
4076  PG_FREE_IF_COPY(barg1, 0);
4077  PG_FREE_IF_COPY(barg2, 1);
4078  }
4079 
4080  PG_RETURN_BOOL(result);
4081 }
size_t Size
Definition: c.h:551
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 4176 of file varlena.c.

4177 {
4178  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4179  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4180  int len1,
4181  len2;
4182  int cmp;
4183 
4184  len1 = VARSIZE_ANY_EXHDR(arg1);
4185  len2 = VARSIZE_ANY_EXHDR(arg2);
4186 
4187  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4188 
4189  PG_FREE_IF_COPY(arg1, 0);
4190  PG_FREE_IF_COPY(arg2, 1);
4191 
4192  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
4193 }

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

3497 {
3498  bytea *v = PG_GETARG_BYTEA_PP(0);
3499  int64 n = PG_GETARG_INT64(1);
3500  int byteNo,
3501  bitNo;
3502  int len;
3503  int byte;
3504 
3505  len = VARSIZE_ANY_EXHDR(v);
3506 
3507  if (n < 0 || n >= (int64) len * 8)
3508  ereport(ERROR,
3509  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3510  errmsg("index %lld out of valid range, 0..%lld",
3511  (long long) n, (long long) len * 8 - 1)));
3512 
3513  /* n/8 is now known < len, so safe to cast to int */
3514  byteNo = (int) (n / 8);
3515  bitNo = (int) (n % 8);
3516 
3517  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3518 
3519  if (byte & (1 << bitNo))
3520  PG_RETURN_INT32(1);
3521  else
3522  PG_RETURN_INT32(0);
3523 }
#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 3467 of file varlena.c.

3468 {
3469  bytea *v = PG_GETARG_BYTEA_PP(0);
3470  int32 n = PG_GETARG_INT32(1);
3471  int len;
3472  int byte;
3473 
3474  len = VARSIZE_ANY_EXHDR(v);
3475 
3476  if (n < 0 || n >= len)
3477  ereport(ERROR,
3478  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3479  errmsg("index %d out of valid range, 0..%d",
3480  n, len - 1)));
3481 
3482  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3483 
3484  PG_RETURN_INT32(byte);
3485 }

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

4157 {
4158  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4159  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4160  int len1,
4161  len2;
4162  int cmp;
4163 
4164  len1 = VARSIZE_ANY_EXHDR(arg1);
4165  len2 = VARSIZE_ANY_EXHDR(arg2);
4166 
4167  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4168 
4169  PG_FREE_IF_COPY(arg1, 0);
4170  PG_FREE_IF_COPY(arg2, 1);
4171 
4172  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
4173 }

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

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

References ereport, errcode(), errmsg(), ERROR, hex_decode(), 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 4136 of file varlena.c.

4137 {
4138  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4139  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4140  int len1,
4141  len2;
4142  int cmp;
4143 
4144  len1 = VARSIZE_ANY_EXHDR(arg1);
4145  len2 = VARSIZE_ANY_EXHDR(arg2);
4146 
4147  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4148 
4149  PG_FREE_IF_COPY(arg1, 0);
4150  PG_FREE_IF_COPY(arg2, 1);
4151 
4152  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
4153 }

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

4117 {
4118  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4119  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4120  int len1,
4121  len2;
4122  int cmp;
4123 
4124  len1 = VARSIZE_ANY_EXHDR(arg1);
4125  len2 = VARSIZE_ANY_EXHDR(arg2);
4126 
4127  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4128 
4129  PG_FREE_IF_COPY(arg1, 0);
4130  PG_FREE_IF_COPY(arg2, 1);
4131 
4132  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
4133 }

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

4085 {
4086  Datum arg1 = PG_GETARG_DATUM(0);
4087  Datum arg2 = PG_GETARG_DATUM(1);
4088  bool result;
4089  Size len1,
4090  len2;
4091 
4092  /*
4093  * We can use a fast path for unequal lengths, which might save us from
4094  * having to detoast one or both values.
4095  */
4096  len1 = toast_raw_datum_size(arg1);
4097  len2 = toast_raw_datum_size(arg2);
4098  if (len1 != len2)
4099  result = true;
4100  else
4101  {
4102  bytea *barg1 = DatumGetByteaPP(arg1);
4103  bytea *barg2 = DatumGetByteaPP(arg2);
4104 
4105  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4106  len1 - VARHDRSZ) != 0);
4107 
4108  PG_FREE_IF_COPY(barg1, 0);
4109  PG_FREE_IF_COPY(barg2, 1);
4110  }
4111 
4112  PG_RETURN_BOOL(result);
4113 }

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

3181 {
3182  Datum str = PG_GETARG_DATUM(0);
3183 
3184  /* We need not detoast the input at all */
3186 }

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

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

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

3354 {
3355  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3356  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3357  int sp = PG_GETARG_INT32(2); /* substring start position */
3358  int sl = PG_GETARG_INT32(3); /* substring length */
3359 
3360  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3361 }
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3376

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

3365 {
3366  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3367  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3368  int sp = PG_GETARG_INT32(2); /* substring start position */
3369  int sl;
3370 
3371  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3372  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3373 }

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

3424 {
3425  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3426  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3427  int pos;
3428  int px,
3429  p;
3430  int len1,
3431  len2;
3432  char *p1,
3433  *p2;
3434 
3435  len1 = VARSIZE_ANY_EXHDR(t1);
3436  len2 = VARSIZE_ANY_EXHDR(t2);
3437 
3438  if (len2 <= 0)
3439  PG_RETURN_INT32(1); /* result for empty pattern */
3440 
3441  p1 = VARDATA_ANY(t1);
3442  p2 = VARDATA_ANY(t2);
3443 
3444  pos = 0;
3445  px = (len1 - len2);
3446  for (p = 0; p <= px; p++)
3447  {
3448  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3449  {
3450  pos = p + 1;
3451  break;
3452  };
3453  p1++;
3454  };
3455 
3456  PG_RETURN_INT32(pos);
3457 }
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 473 of file varlena.c.

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

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

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

3567 {
3569  int64 n = PG_GETARG_INT64(1);
3570  int32 newBit = PG_GETARG_INT32(2);
3571  int len;
3572  int oldByte,
3573  newByte;
3574  int byteNo,
3575  bitNo;
3576 
3577  len = VARSIZE(res) - VARHDRSZ;
3578 
3579  if (n < 0 || n >= (int64) len * 8)
3580  ereport(ERROR,
3581  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3582  errmsg("index %lld out of valid range, 0..%lld",
3583  (long long) n, (long long) len * 8 - 1)));
3584 
3585  /* n/8 is now known < len, so safe to cast to int */
3586  byteNo = (int) (n / 8);
3587  bitNo = (int) (n % 8);
3588 
3589  /*
3590  * sanity check!
3591  */
3592  if (newBit != 0 && newBit != 1)
3593  ereport(ERROR,
3594  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3595  errmsg("new bit must be 0 or 1")));
3596 
3597  /*
3598  * Update the byte.
3599  */
3600  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3601 
3602  if (newBit == 0)
3603  newByte = oldByte & (~(1 << bitNo));
3604  else
3605  newByte = oldByte | (1 << bitNo);
3606 
3607  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3608 
3610 }
#define VARSIZE(PTR)
Definition: postgres.h:316

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

3535 {
3537  int32 n = PG_GETARG_INT32(1);
3538  int32 newByte = PG_GETARG_INT32(2);
3539  int len;
3540 
3541  len = VARSIZE(res) - VARHDRSZ;
3542 
3543  if (n < 0 || n >= len)
3544  ereport(ERROR,
3545  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3546  errmsg("index %d out of valid range, 0..%d",
3547  n, len - 1)));
3548 
3549  /*
3550  * Now set the byte.
3551  */
3552  ((unsigned char *) VARDATA(res))[n] = newByte;
3553 
3555 }

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

786 {
788  {
789  /* Optimization for single-byte encodings */
790  return n;
791  }
792  else
793  {
794  const char *s;
795 
796  for (s = p; n > 0; n--)
797  s += pg_mblen(s);
798 
799  return s - p;
800  }
801 }
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966

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

1489 {
1490  if (!OidIsValid(collid))
1491  {
1492  /*
1493  * This typically means that the parser could not resolve a conflict
1494  * of implicit collations, so report it that way.
1495  */
1496  ereport(ERROR,
1497  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1498  errmsg("could not determine which collation to use for string comparison"),
1499  errhint("Use the COLLATE clause to set the collation explicitly.")));
1500  }
1501 }

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

4332 {
4333  int result = 0;
4334  const char *p = VARDATA_ANY(replace_text);
4335  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4336 
4337  while (p < p_end)
4338  {
4339  /* Find next escape char, if any. */
4340  p = memchr(p, '\\', p_end - p);
4341  if (p == NULL)
4342  break;
4343  p++;
4344  /* Note: a backslash at the end doesn't require extra processing. */
4345  if (p < p_end)
4346  {
4347  if (*p >= '1' && *p <= '9')
4348  return 2; /* Found a submatch specifier, so done */
4349  result = 1; /* Found some other sequence, keep looking */
4350  p++;
4351  }
4352  }
4353  return result;
4354 }

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

5458 {
5459  text *result;
5461  FmgrInfo *foutcache;
5462  bool first_arg = true;
5463  int i;
5464 
5465  /*
5466  * concat(VARIADIC some-array) is essentially equivalent to
5467  * array_to_text(), ie concat the array elements with the given separator.
5468  * So we just pass the case off to that code.
5469  */
5470  if (get_fn_expr_variadic(fcinfo->flinfo))
5471  {
5472  ArrayType *arr;
5473 
5474  /* Should have just the one argument */
5475  Assert(argidx == PG_NARGS() - 1);
5476 
5477  /* concat(VARIADIC NULL) is defined as NULL */
5478  if (PG_ARGISNULL(argidx))
5479  return NULL;
5480 
5481  /*
5482  * Non-null argument had better be an array. We assume that any call
5483  * context that could let get_fn_expr_variadic return true will have
5484  * checked that a VARIADIC-labeled parameter actually is an array. So
5485  * it should be okay to just Assert that it's an array rather than
5486  * doing a full-fledged error check.
5487  */
5489 
5490  /* OK, safe to fetch the array value */
5491  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5492 
5493  /*
5494  * And serialize the array. We tell array_to_text to ignore null
5495  * elements, which matches the behavior of the loop below.
5496  */
5497  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5498  }
5499 
5500  /* Normal case without explicit VARIADIC marker */
5501  initStringInfo(&str);
5502 
5503  /* Get output function info, building it if first time through */
5504  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5505  if (foutcache == NULL)
5506  foutcache = build_concat_foutcache(fcinfo, argidx);
5507 
5508  for (i = argidx; i < PG_NARGS(); i++)
5509  {
5510  if (!PG_ARGISNULL(i))
5511  {
5513 
5514  /* add separator if appropriate */
5515  if (first_arg)
5516  first_arg = false;
5517  else
5518  appendStringInfoString(&str, sepstr);
5519 
5520  /* call the appropriate type output function, append the result */
5522  OutputFunctionCall(&foutcache[i], value));
5523  }
5524  }
5525 
5526  result = cstring_to_text_with_len(str.data, str.len);
5527  pfree(str.data);
5528 
5529  return result;
5530 }
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1920
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2789
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5418

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

Referenced by text_concat(), and text_concat_ws().

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 188 of file varlena.c.

189 {
190  return cstring_to_text_with_len(s, strlen(s));
191 }

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(), hstore_to_json(), hstore_to_json_loose(), 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_function_sqlbody(), 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(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), unaccent_dict(), upper(), xml_encode_special_chars(), xml_in(), and xml_recv().

◆ cstring_to_text_with_len()

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

Definition at line 200 of file varlena.c.

201 {
202  text *result = (text *) palloc(len + VARHDRSZ);
203 
204  SET_VARSIZE(result, len + VARHDRSZ);
205  memcpy(VARDATA(result), s, len);
206 
207  return result;
208 }

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

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), build_regexp_match_result(), build_regexp_split_result(), build_test_match_result(), compute_tsvector_stats(), concat_internal(), cstring_to_text(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), ExecEvalXmlExpr(), ExecPrepareJsonItemCoercion(), executeDateTimeMethod(), executeLikeRegex(), fsm_page_contents(), get_array_element_end(), get_array_end(), get_object_end(), get_object_field_end(), get_scalar(), gin_extract_tsquery(), gin_extract_tsvector(), hstore_akeys(), hstore_avals(), hstore_each(), hstore_fetchval(), hstore_skeys(), hstore_slice_to_array(), hstore_subscript_fetch(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array_noargs(), json_build_array_worker(), json_build_object_noargs(), json_build_object_worker(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_pretty(), JsonbValueAsText(), leftmostvalue_text(), LogicalOutputWrite(), parse_ident(), pg_gen_salt(), pg_gen_salt_rounds(), replace_text(), replace_text_regexp(), row_to_json(), row_to_json_pretty(), serialize_deflist(), split_part(), split_text(), ssl_extension_info(), string_agg_finalfn(), stringinfo_to_xmltype(), text_format(), text_left(), text_right(), textrecv(), to_json_worker(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), unistr(), varchar(), varchar_input(), and xslt_process().

◆ hexval()

static unsigned int hexval ( unsigned char  c)
static

Definition at line 6365 of file varlena.c.

6366 {
6367  if (c >= '0' && c <= '9')
6368  return c - '0';
6369  if (c >= 'a' && c <= 'f')
6370  return c - 'a' + 0xA;
6371  if (c >= 'A' && c <= 'F')
6372  return c - 'A' + 0xA;
6373  elog(ERROR, "invalid hexadecimal digit");
6374  return 0; /* not reached */
6375 }
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 6381 of file varlena.c.

6382 {
6383  unsigned int result = 0;
6384 
6385  for (size_t i = 0; i < n; i++)
6386  result += hexval(instr[i]) << (4 * (n - i - 1));
6387 
6388  return result;
6389 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6365

References hexval(), and i.

Referenced by unistr().

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 3055 of file varlena.c.

3056 {
3057  int result;
3058  int len1,
3059  len2;
3060 
3061  len1 = VARSIZE_ANY_EXHDR(arg1);
3062  len2 = VARSIZE_ANY_EXHDR(arg2);
3063 
3064  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3065  if (result != 0)
3066  return result;
3067  else if (len1 < len2)
3068  return -1;
3069  else if (len1 > len2)
3070  return 1;
3071  else
3072  return 0;
3073 }

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

6356 {
6357  for (size_t i = 0; i < n; i++)
6358  if (!isxdigit((unsigned char) instr[i]))
6359  return false;
6360 
6361  return true;
6362 }

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5346 of file varlena.c.

5347 {
5348  StringInfo state;
5349  MemoryContext aggcontext;
5350  MemoryContext oldcontext;
5351 
5352  if (!AggCheckCallContext(fcinfo, &aggcontext))
5353  {
5354  /* cannot be called directly because of internal-type argument */
5355  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5356  }
5357 
5358  /*
5359  * Create state in aggregate context. It'll stay there across subsequent
5360  * calls.
5361  */
5362  oldcontext = MemoryContextSwitchTo(aggcontext);
5363  state = makeStringInfo();
5364  MemoryContextSwitchTo(oldcontext);
5365 
5366  return state;
5367 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3640 of file varlena.c.

3641 {
3642  Name s = PG_GETARG_NAME(0);
3643 
3645 }
text * cstring_to_text(const char *s)
Definition: varlena.c:188

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

2859 {
2860  Name arg1 = PG_GETARG_NAME(0);
2861  text *arg2 = PG_GETARG_TEXT_PP(1);
2862  size_t len1 = strlen(NameStr(*arg1));
2863  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2864  Oid collid = PG_GET_COLLATION();
2865  bool result;
2866 
2867  check_collation_set(collid);
2868 
2869  if (collid == C_COLLATION_OID)
2870  result = (len1 == len2 &&
2871  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2872  else
2873  result = (varstr_cmp(NameStr(*arg1), len1,
2874  VARDATA_ANY(arg2), len2,
2875  collid) == 0);
2876 
2877  PG_FREE_IF_COPY(arg2, 1);
2878 
2879  PG_RETURN_BOOL(result);
2880 }

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

2243 {
2244  Name arg1 = DatumGetName(x);
2245  Name arg2 = DatumGetName(y);
2246 
2247  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2248 }
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:629

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

2286 {
2287  Name arg1 = DatumGetName(x);
2288  Name arg2 = DatumGetName(y);
2289 
2290  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2291  NameStr(*arg2), strlen(NameStr(*arg2)),
2292  ssup);
2293 }
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2299

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

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 3014 of file varlena.c.

3015 {
3017 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2989
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2958

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 3008 of file varlena.c.

3009 {
3011 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 3002 of file varlena.c.

3003 {
3005 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2996 of file varlena.c.

2997 {
2999 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2908 of file varlena.c.

2909 {
2910  Name arg1 = PG_GETARG_NAME(0);
2911  text *arg2 = PG_GETARG_TEXT_PP(1);
2912  size_t len1 = strlen(NameStr(*arg1));
2913  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2914  Oid collid = PG_GET_COLLATION();
2915  bool result;
2916 
2917  check_collation_set(collid);
2918 
2919  if (collid == C_COLLATION_OID)
2920  result = !(len1 == len2 &&
2921  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2922  else
2923  result = !(varstr_cmp(NameStr(*arg1), len1,
2924  VARDATA_ANY(arg2), len2,
2925  collid) == 0);
2926 
2927  PG_FREE_IF_COPY(arg2, 1);
2928 
2929  PG_RETURN_BOOL(result);
2930 }

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

5287 {
5288  int typlen;
5289  char *result;
5290  ToastCompressionId cmid;
5291 
5292  /* On first call, get the input type's typlen, and save at *fn_extra */
5293  if (fcinfo->flinfo->fn_extra == NULL)
5294  {
5295  /* Lookup the datatype of the supplied argument */
5296  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5297 
5298  typlen = get_typlen(argtypeid);
5299  if (typlen == 0) /* should not happen */
5300  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5301 
5302  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5303  sizeof(int));
5304  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5305  }
5306  else
5307  typlen = *((int *) fcinfo->flinfo->fn_extra);
5308 
5309  if (typlen != -1)
5310  PG_RETURN_NULL();
5311 
5312  /* get the compression method id stored in the compressed varlena */
5313  cmid = toast_get_compression_id((struct varlena *)
5315  if (cmid == TOAST_INVALID_COMPRESSION_ID)
5316  PG_RETURN_NULL();
5317 
5318  /* convert compression method id to compression method name */
5319  switch (cmid)
5320  {
5322  result = "pglz";
5323  break;
5325  result = "lz4";
5326  break;
5327  default:
5328  elog(ERROR, "invalid compression method id %d", cmid);
5329  }
5330 
5332 }
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2154
#define DatumGetPointer(X)
Definition: postgres.h:593
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 5239 of file varlena.c.

5240 {
5242  int32 result;
5243  int typlen;
5244 
5245  /* On first call, get the input type's typlen, and save at *fn_extra */
5246  if (fcinfo->flinfo->fn_extra == NULL)
5247  {
5248  /* Lookup the datatype of the supplied argument */
5249  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5250 
5251  typlen = get_typlen(argtypeid);
5252  if (typlen == 0) /* should not happen */
5253  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5254 
5255  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5256  sizeof(int));
5257  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5258  }
5259  else
5260  typlen = *((int *) fcinfo->flinfo->fn_extra);
5261 
5262  if (typlen == -1)
5263  {
5264  /* varlena type, possibly toasted */
5265  result = toast_datum_size(value);
5266  }
5267  else if (typlen == -2)
5268  {
5269  /* cstring */
5270  result = strlen(DatumGetCString(value)) + 1;
5271  }
5272  else
5273  {
5274  /* ordinary fixed-width type */
5275  result = typlen;
5276  }
5277 
5278  PG_RETURN_INT32(result);
5279 }
Size toast_datum_size(Datum value)
Definition: detoast.c:601
#define DatumGetCString(X)
Definition: postgres.h:610

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

4255 {
4256  text *src_text = PG_GETARG_TEXT_PP(0);
4257  text *from_sub_text = PG_GETARG_TEXT_PP(1);
4258  text *to_sub_text = PG_GETARG_TEXT_PP(2);
4259  int src_text_len;
4260  int from_sub_text_len;
4262  text *ret_text;
4263  int chunk_len;
4264  char *curr_ptr;
4265  char *start_ptr;
4267  bool found;
4268 
4269  src_text_len = VARSIZE_ANY_EXHDR(src_text);
4270  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4271 
4272  /* Return unmodified source string if empty source or pattern */
4273  if (src_text_len < 1 || from_sub_text_len < 1)
4274  {
4275  PG_RETURN_TEXT_P(src_text);
4276  }
4277 
4278  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4279 
4280  found = text_position_next(&state);
4281 
4282  /* When the from_sub_text is not found, there is nothing to do. */
4283  if (!found)
4284  {
4286  PG_RETURN_TEXT_P(src_text);
4287  }
4288  curr_ptr = text_position_get_match_ptr(&state);
4289  start_ptr = VARDATA_ANY(src_text);
4290 
4291  initStringInfo(&str);
4292 
4293  do
4294  {
4296 
4297  /* copy the data skipped over by last text_position_next() */
4298  chunk_len = curr_ptr - start_ptr;
4299  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4300 
4301  appendStringInfoText(&str, to_sub_text);
4302 
4303  start_ptr = curr_ptr + from_sub_text_len;
4304 
4305  found = text_position_next(&state);
4306  if (found)
4307  curr_ptr = text_position_get_match_ptr(&state);
4308  }
4309  while (found);
4310 
4311  /* copy trailing data */
4312  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4313  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4314 
4316 
4317  ret_text = cstring_to_text_with_len(str.data, str.len);
4318  pfree(str.data);
4319 
4320  PG_RETURN_TEXT_P(ret_text);
4321 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1194
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1481
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4240
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1446
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1314

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

4468 {
4469  text *ret_text;
4470  regex_t *re;
4471  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4472  int nmatches = 0;
4474  regmatch_t pmatch[10]; /* main match, plus \1 to \9 */
4475  int nmatch = lengthof(pmatch);
4476  pg_wchar *data;
4477  size_t data_len;
4478  int data_pos;
4479  char *start_ptr;
4480  int escape_status;
4481 
4482  initStringInfo(&buf);
4483 
4484  /* Convert data string to wide characters. */
4485  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4486  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4487 
4488  /* Check whether replace_text has escapes, especially regexp submatches. */
4490 
4491  /* If no regexp submatches, we can use REG_NOSUB. */
4492  if (escape_status < 2)
4493  {
4494  cflags |= REG_NOSUB;
4495  /* Also tell pg_regexec we only want the whole-match location. */
4496  nmatch = 1;
4497  }
4498 
4499  /* Prepare the regexp. */
4500  re = RE_compile_and_cache(pattern_text, cflags, collation);
4501 
4502  /* start_ptr points to the data_pos'th character of src_text */
4503  start_ptr = (char *) VARDATA_ANY(src_text);
4504  data_pos = 0;
4505 
4506  while (search_start <= data_len)
4507  {
4508  int regexec_result;
4509 
4511 
4512  regexec_result = pg_regexec(re,
4513  data,
4514  data_len,
4515  search_start,
4516  NULL, /* no details */
4517  nmatch,
4518  pmatch,
4519  0);
4520 
4521  if (regexec_result == REG_NOMATCH)
4522  break;
4523 
4524  if (regexec_result != REG_OKAY)
4525  {
4526  char errMsg[100];
4527 
4529  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4530  ereport(ERROR,
4531  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4532  errmsg("regular expression failed: %s", errMsg)));
4533  }
4534 
4535  /*
4536  * Count matches, and decide whether to replace this match.
4537  */
4538  nmatches++;
4539  if (n > 0 && nmatches != n)
4540  {
4541  /*
4542  * No, so advance search_start, but not start_ptr/data_pos. (Thus,
4543  * we treat the matched text as if it weren't matched, and copy it
4544  * to the output later.)
4545  */
4546  search_start = pmatch[0].rm_eo;
4547  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4548  search_start++;
4549  continue;
4550  }
4551 
4552  /*
4553  * Copy the text to the left of the match position. Note we are given
4554  * character not byte indexes.
4555  */
4556  if (pmatch[0].rm_so - data_pos > 0)
4557  {
4558  int chunk_len;
4559 
4560  chunk_len = charlen_to_bytelen(start_ptr,
4561  pmatch[0].rm_so - data_pos);
4562  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4563 
4564  /*
4565  * Advance start_ptr over that text, to avoid multiple rescans of
4566  * it if the replace_text contains multiple back-references.
4567  */
4568  start_ptr += chunk_len;
4569  data_pos = pmatch[0].rm_so;
4570  }
4571 
4572  /*
4573  * Copy the replace_text, processing escapes if any are present.
4574  */
4575  if (escape_status > 0)
4577  start_ptr, data_pos);
4578  else
4580 
4581  /* Advance start_ptr and data_pos over the matched text. */
4582  start_ptr += charlen_to_bytelen(start_ptr,
4583  pmatch[0].rm_eo - data_pos);
4584  data_pos = pmatch[0].rm_eo;
4585 
4586  /*
4587  * If we only want to replace one occurrence, we're done.
4588  */
4589  if (n > 0)
4590  break;
4591 
4592  /*
4593  * Advance search position. Normally we start the next search at the
4594  * end of the previous match; but if the match was of zero length, we
4595  * have to advance by one character, or we'd just find the same match
4596  * again.
4597  */
4598  search_start = data_pos;
4599  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4600  search_start++;
4601  }
4602 
4603  /*
4604  * Copy the text to the right of the last match.
4605  */
4606  if (data_pos < data_len)
4607  {
4608  int chunk_len;
4609 
4610  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4611  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4612  }
4613 
4614  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4615  pfree(buf.data);
4616  pfree(data);
4617 
4618  return ret_text;
4619 }
#define lengthof(array)
Definition: c.h:745
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:929
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:176
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
Definition: regex.h:56
static int check_replace_text_has_escape(const text *replace_text)
Definition: varlena.c:4331
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:4364

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

6187 {
6188  while (len > 0)
6189  {
6190  len--;
6191  if (s1[len] != s2[len])
6192  return false;
6193  }
6194  return true;
6195 }

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4627 of file varlena.c.

4628 {
4629  text *inputstring = PG_GETARG_TEXT_PP(0);
4630  text *fldsep = PG_GETARG_TEXT_PP(1);
4631  int fldnum = PG_GETARG_INT32(2);
4632  int inputstring_len;
4633  int fldsep_len;
4635  char *start_ptr;
4636  char *end_ptr;
4637  text *result_text;
4638  bool found;
4639 
4640  /* field number is 1 based */
4641  if (fldnum == 0)
4642  ereport(ERROR,
4643  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4644  errmsg("field position must not be zero")));
4645 
4646  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4647  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4648 
4649  /* return empty string for empty input string */
4650  if (inputstring_len < 1)
4652 
4653  /* handle empty field separator */
4654  if (fldsep_len < 1)
4655  {
4656  /* if first or last field, return input string, else empty string */
4657  if (fldnum == 1 || fldnum == -1)
4658  PG_RETURN_TEXT_P(inputstring);
4659  else
4661  }
4662 
4663  /* find the first field separator */
4664  text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4665 
4666  found = text_position_next(&state);
4667 
4668  /* special case if fldsep not found at all */
4669  if (!found)
4670  {
4672  /* if first or last field, return input string, else empty string */
4673  if (fldnum == 1 || fldnum == -1)
4674  PG_RETURN_TEXT_P(inputstring);
4675  else
4677  }
4678 
4679  /*
4680  * take care of a negative field number (i.e. count from the right) by
4681  * converting to a positive field number; we need total number of fields
4682  */
4683  if (fldnum < 0)
4684  {
4685  /* we found a fldsep, so there are at least two fields */
4686  int numfields = 2;
4687 
4688  while (text_position_next(&state))
4689  numfields++;
4690 
4691  /* special case of last field does not require an extra pass */
4692  if (fldnum == -1)
4693  {
4694  start_ptr = text_position_get_match_ptr(&state) + fldsep_len;
4695  end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
4698  end_ptr - start_ptr));
4699  }
4700 
4701  /* else, convert fldnum to positive notation */
4702  fldnum += numfields + 1;
4703 
4704  /* if nonexistent field, return empty string */
4705  if (fldnum <= 0)
4706  {
4709  }
4710 
4711  /* reset to pointing at first match, but now with positive fldnum */
4713  found = text_position_next(&state);
4714  Assert(found);
4715  }
4716 
4717  /* identify bounds of first field */
4718  start_ptr = VARDATA_ANY(inputstring);
4719  end_ptr = text_position_get_match_ptr(&state);
4720 
4721  while (found && --fldnum > 0)
4722  {
4723  /* identify bounds of next field */
4724  start_ptr = end_ptr + fldsep_len;
4725  found = text_position_next(&state);
4726  if (found)
4727  end_ptr = text_position_get_match_ptr(&state);
4728  }
4729 
4731 
4732  if (fldnum > 0)
4733  {
4734  /* N'th field separator not found */
4735  /* if last field requested, return it, else empty string */
4736  if (fldnum == 1)
4737  {
4738  int last_len = start_ptr - VARDATA_ANY(inputstring);
4739 
4740  result_text = cstring_to_text_with_len(start_ptr,
4741  inputstring_len - last_len);
4742  }
4743  else
4744  result_text = cstring_to_text("");
4745  }
4746  else
4747  {
4748  /* non-last field requested */
4749  result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4750  }
4751 
4752  PG_RETURN_TEXT_P(result_text);
4753 }
static void text_position_reset(TextPositionState *state)
Definition: varlena.c:1473

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

4851 {
4852  text *inputstring;
4853  text *fldsep;
4854  text *null_string;
4855  Oid collation = PG_GET_COLLATION();
4856  int inputstring_len;
4857  int fldsep_len;
4858  char *start_ptr;
4859  text *result_text;
4860 
4861  /* when input string is NULL, then result is NULL too */
4862  if (PG_ARGISNULL(0))
4863  return false;
4864 
4865  inputstring = PG_GETARG_TEXT_PP(0);
4866 
4867  /* fldsep can be NULL */
4868  if (!PG_ARGISNULL(1))
4869  fldsep = PG_GETARG_TEXT_PP(1);
4870  else
4871  fldsep = NULL;
4872 
4873  /* null_string can be NULL or omitted */
4874  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4875  null_string = PG_GETARG_TEXT_PP(2);
4876  else
4877  null_string = NULL;
4878 
4879  if (fldsep != NULL)
4880  {
4881  /*
4882  * Normal case with non-null fldsep. Use the text_position machinery
4883  * to search for occurrences of fldsep.
4884  */
4886 
4887  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4888  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4889 
4890  /* return empty set for empty input string */
4891  if (inputstring_len < 1)
4892  return true;
4893 
4894  /* empty field separator: return input string as a one-element set */
4895  if (fldsep_len < 1)
4896  {
4897  split_text_accum_result(tstate, inputstring,
4898  null_string, collation);
4899  return true;
4900  }
4901 
4902  text_position_setup(inputstring, fldsep, collation, &state);
4903 
4904  start_ptr = VARDATA_ANY(inputstring);
4905 
4906  for (;;)
4907  {
4908  bool found;
4909  char *end_ptr;
4910  int chunk_len;
4911 
4913 
4914  found = text_position_next(&state);
4915  if (!found)
4916  {
4917  /* fetch last field */
4918  chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4919  end_ptr = NULL; /* not used, but some compilers complain */
4920  }
4921  else
4922  {
4923  /* fetch non-last field */
4924  end_ptr = text_position_get_match_ptr(&state);
4925  chunk_len = end_ptr - start_ptr;
4926  }
4927 
4928  /* build a temp text datum to pass to split_text_accum_result */
4929  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4930 
4931  /* stash away this field */
4932  split_text_accum_result(tstate, result_text,
4933  null_string, collation);
4934 
4935  pfree(result_text);
4936 
4937  if (!found)
4938  break;
4939 
4940  start_ptr = end_ptr + fldsep_len;
4941  }
4942 
4944  }
4945  else
4946  {
4947  /*
4948  * When fldsep is NULL, each character in the input string becomes a
4949  * separate element in the result set. The separator is effectively
4950  * the space between characters.
4951  */
4952  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4953 
4954  start_ptr = VARDATA_ANY(inputstring);
4955 
4956  while (inputstring_len > 0)
4957  {
4958  int chunk_len = pg_mblen(start_ptr);
4959 
4961 
4962  /* build a temp text datum to pass to split_text_accum_result */
4963  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4964 
4965  /* stash away this field */
4966  split_text_accum_result(tstate, result_text,
4967  null_string, collation);
4968 
4969  pfree(result_text);
4970 
4971  start_ptr += chunk_len;
4972  inputstring_len -= chunk_len;
4973  }
4974  }
4975 
4976  return true;
4977 }
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition: varlena.c:4986

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

4990 {
4991  bool is_null = false;
4992 
4993  if (null_string && text_isequal(field_value, null_string, collation))
4994  is_null = true;
4995 
4996  if (tstate->tupstore)
4997  {
4998  Datum values[1];
4999  bool nulls[1];
5000 
5001  values[0] = PointerGetDatum(field_value);
5002  nulls[0] = is_null;
5003 
5005  tstate->tupdesc,
5006  values,
5007  nulls);
5008  }
5009  else
5010  {
5011  tstate->astate = accumArrayResult(tstate->astate,
5012  PointerGetDatum(field_value),
5013  is_null,
5014  TEXTOID,
5016  }
5017 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5278
static Datum values[MAXATTR]
Definition: bootstrap.c:156
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
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:4759

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

3844 {
3845  char *nextp = rawstring;
3846  bool done = false;
3847 
3848  *namelist = NIL;
3849 
3850  while (scanner_isspace(*nextp))
3851  nextp++; /* skip leading whitespace */
3852 
3853  if (*nextp == '\0')
3854  return true; /* allow empty string */
3855 
3856  /* At the top of the loop, we are at start of a new directory. */
3857  do
3858  {
3859  char *curname;
3860  char *endp;
3861 
3862  if (*nextp == '"')
3863  {
3864  /* Quoted name --- collapse quote-quote pairs */
3865  curname = nextp + 1;
3866  for (;;)
3867  {
3868  endp = strchr(nextp + 1, '"');
3869  if (endp == NULL)
3870  return false; /* mismatched quotes */
3871  if (endp[1] != '"')
3872  break; /* found end of quoted name */
3873  /* Collapse adjacent quotes into one quote, and look again */
3874  memmove(endp, endp + 1, strlen(endp));
3875  nextp = endp;
3876  }
3877  /* endp now points at the terminating quote */
3878  nextp = endp + 1;
3879  }
3880  else
3881  {
3882  /* Unquoted name --- extends to separator or end of string */
3883  curname = endp = nextp;
3884  while (*nextp && *nextp != separator)
3885  {
3886  /* trailing whitespace should not be included in name */
3887  if (!scanner_isspace(*nextp))
3888  endp = nextp + 1;
3889  nextp++;
3890  }
3891  if (curname == endp)
3892  return false; /* empty unquoted name not allowed */
3893  }
3894 
3895  while (scanner_isspace(*nextp))
3896  nextp++; /* skip trailing whitespace */
3897 
3898  if (*nextp == separator)
3899  {
3900  nextp++;
3901  while (scanner_isspace(*nextp))
3902  nextp++; /* skip leading whitespace for next */
3903  /* we expect another name, so done remains false */
3904  }
3905  else if (*nextp == '\0')
3906  done = true;
3907  else
3908  return false; /* invalid syntax */
3909 
3910  /* Now safe to overwrite separator with a null */
3911  *endp = '\0';
3912 
3913  /* Truncate path if it's overlength */
3914  if (strlen(curname) >= MAXPGPATH)
3915  curname[MAXPGPATH - 1] = '\0';
3916 
3917  /*
3918  * Finished isolating current name --- add it to list
3919  */
3920  curname = pstrdup(curname);
3921  canonicalize_path(curname);
3922  *namelist = lappend(*namelist, curname);
3923 
3924  /* Loop back if we didn't reach end of string */
3925  } while (!done);
3926 
3927  return true;
3928 }
List * lappend(List *list, void *datum)
Definition: list.c:338
char * pstrdup(const char *in)
Definition: mcxt.c:1305
#define MAXPGPATH
#define NIL
Definition: pg_list.h:66
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 3963 of file varlena.c.

3965 {
3966  char *nextp = rawstring;
3967  bool done = false;
3968 
3969  *namelist = NIL;
3970 
3971  while (scanner_isspace(*nextp))
3972  nextp++; /* skip leading whitespace */
3973 
3974  if (*nextp == '\0')
3975  return true; /* allow empty string */
3976 
3977  /* At the top of the loop, we are at start of a new identifier. */
3978  do
3979  {
3980  char *curname;
3981  char *endp;
3982 
3983  if (*nextp == '"')
3984  {
3985  /* Quoted name --- collapse quote-quote pairs */
3986  curname = nextp + 1;
3987  for (;;)
3988  {
3989  endp = strchr(nextp + 1, '"');
3990  if (endp == NULL)
3991  return false; /* mismatched quotes */
3992  if (endp[1] != '"')
3993  break; /* found end of quoted name */
3994  /* Collapse adjacent quotes into one quote, and look again */
3995  memmove(endp, endp + 1, strlen(endp));
3996  nextp = endp;
3997  }
3998  /* endp now points at the terminating quote */
3999  nextp = endp + 1;
4000  }
4001  else
4002  {
4003  /* Unquoted name --- extends to separator or whitespace */
4004  curname = nextp;
4005  while (*nextp && *nextp != separator &&
4006  !scanner_isspace(*nextp))
4007  nextp++;
4008  endp = nextp;
4009  if (curname == nextp)
4010  return false; /* empty unquoted name not allowed */
4011  }
4012 
4013  while (scanner_isspace(*nextp))
4014  nextp++; /* skip trailing whitespace */
4015 
4016  if (*nextp == separator)
4017  {
4018  nextp++;
4019  while (scanner_isspace(*nextp))
4020  nextp++; /* skip leading whitespace for next */
4021  /* we expect another name, so done remains false */
4022  }
4023  else if (*nextp == '\0')
4024  done = true;
4025  else
4026  return false; /* invalid syntax */
4027 
4028  /* Now safe to overwrite separator with a null */
4029  *endp = '\0';
4030 
4031  /*
4032  * Finished isolating current name --- add it to list
4033  */
4034  *namelist = lappend(*namelist, curname);
4035 
4036  /* Loop back if we didn't reach end of string */
4037  } while (!done);
4038 
4039  return true;
4040 }

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

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

◆ SplitIdentifierString()

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

Definition at line 3715 of file varlena.c.

3717 {
3718  char *nextp = rawstring;
3719  bool done = false;
3720 
3721  *namelist = NIL;
3722 
3723  while (scanner_isspace(*nextp))
3724  nextp++; /* skip leading whitespace */
3725 
3726  if (*nextp == '\0')
3727  return true; /* allow empty string */
3728 
3729  /* At the top of the loop, we are at start of a new identifier. */
3730  do
3731  {
3732  char *curname;
3733  char *endp;
3734 
3735  if (*nextp == '"')
3736  {
3737  /* Quoted name --- collapse quote-quote pairs, no downcasing */
3738  curname = nextp + 1;
3739  for (;;)
3740  {
3741  endp = strchr(nextp + 1, '"');
3742  if (endp == NULL)
3743  return false; /* mismatched quotes */
3744  if (endp[1] != '"')
3745  break; /* found end of quoted name */
3746  /* Collapse adjacent quotes into one quote, and look again */
3747  memmove(endp, endp + 1, strlen(endp));
3748  nextp = endp;
3749  }
3750  /* endp now points at the terminating quote */
3751  nextp = endp + 1;
3752  }
3753  else
3754  {
3755  /* Unquoted name --- extends to separator or whitespace */
3756  char *downname;
3757  int len;
3758 
3759  curname = nextp;
3760  while (*nextp && *nextp != separator &&
3761  !scanner_isspace(*nextp))
3762  nextp++;
3763  endp = nextp;
3764  if (curname == nextp)
3765  return false; /* empty unquoted name not allowed */
3766 
3767  /*
3768  * Downcase the identifier, using same code as main lexer does.
3769  *
3770  * XXX because we want to overwrite the input in-place, we cannot
3771  * support a downcasing transformation that increases the string
3772  * length. This is not a problem given the current implementation
3773  * of downcase_truncate_identifier, but we'll probably have to do
3774  * something about this someday.
3775  */
3776  len = endp - curname;
3777  downname = downcase_truncate_identifier(curname, len, false);
3778  Assert(strlen(downname) <= len);
3779  strncpy(curname, downname, len); /* strncpy is required here */
3780  pfree(downname);
3781  }
3782 
3783  while (scanner_isspace(*nextp))
3784  nextp++; /* skip trailing whitespace */
3785 
3786  if (*nextp == separator)
3787  {
3788  nextp++;
3789  while (scanner_isspace(*nextp))
3790  nextp++; /* skip leading whitespace for next */
3791  /* we expect another name, so done remains false */
3792  }
3793  else if (*nextp == '\0')
3794  done = true;
3795  else
3796  return false; /* invalid syntax */
3797 
3798  /* Now safe to overwrite separator with a null */
3799  *endp = '\0';
3800 
3801  /* Truncate name if it's overlength */
3802  truncate_identifier(curname, strlen(curname), false);
3803 
3804  /*
3805  * Finished isolating current name --- add it to list
3806  */
3807  *namelist = lappend(*namelist, curname);
3808 
3809  /* Loop back if we didn't reach end of string */
3810  } while (!done);
3811 
3812  return true;
3813 }
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_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_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 5396 of file varlena.c.

5397 {
5398  StringInfo state;
5399 
5400  /* cannot be called directly because of internal-type argument */
5401  Assert(AggCheckCallContext(fcinfo, NULL));
5402 
5403  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5404 
5405  if (state != NULL)
5407  else
5408  PG_RETURN_NULL();
5409 }

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

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5370 of file varlena.c.

5371 {
5372  StringInfo state;
5373 
5374  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5375 
5376  /* Append the value unless null. */
5377  if (!PG_ARGISNULL(1))
5378  {
5379  /* On the first time through, we ignore the delimiter. */
5380  if (state == NULL)
5381  state = makeStringAggState(fcinfo);
5382  else if (!PG_ARGISNULL(2))
5383  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
5384 
5385  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
5386  }
5387 
5388  /*
5389  * The transition type for string_agg() is declared to be "internal",
5390  * which is a pass-by-value type the same size as a pointer.
5391  */
5393 }

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

◆ text_catenate()

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

Definition at line 744 of file varlena.c.

745 {
746  text *result;
747  int len1,
748  len2,
749  len;
750  char *ptr;
751 
752  len1 = VARSIZE_ANY_EXHDR(t1);
753  len2 = VARSIZE_ANY_EXHDR(t2);
754 
755  /* paranoia ... probably should throw error instead? */
756  if (len1 < 0)
757  len1 = 0;
758  if (len2 < 0)
759  len2 = 0;
760 
761  len = len1 + len2 + VARHDRSZ;
762  result = (text *) palloc(len);
763 
764  /* Set size of result string... */
765  SET_VARSIZE(result, len);
766 
767  /* Fill data field of result string... */
768  ptr = VARDATA(result);
769  if (len1 > 0)
770  memcpy(ptr, VARDATA_ANY(t1), len1);
771  if (len2 > 0)
772  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
773 
774  return result;
775 }

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

1733 {
1734  char *a1p,
1735  *a2p;
1736  int len1,
1737  len2;
1738 
1739  a1p = VARDATA_ANY(arg1);
1740  a2p = VARDATA_ANY(arg2);
1741 
1742  len1 = VARSIZE_ANY_EXHDR(arg1);
1743  len2 = VARSIZE_ANY_EXHDR(arg2);
1744 
1745  return varstr_cmp(a1p, len1, a2p, len2, collid);
1746 }

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

5537 {
5538  text *result;
5539 
5540  result = concat_internal("", 0, fcinfo);
5541  if (result == NULL)
5542  PG_RETURN_NULL();
5543  PG_RETURN_TEXT_P(result);
5544 }
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5456

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5551 of file varlena.c.

5552 {
5553  char *sep;
5554  text *result;
5555 
5556  /* return NULL when separator is NULL */
5557  if (PG_ARGISNULL(0))
5558  PG_RETURN_NULL();
5560 
5561  result = concat_internal(sep, 1, fcinfo);
5562  if (result == NULL)
5563  PG_RETURN_NULL();
5564  PG_RETURN_TEXT_P(result);
5565 }

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

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

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

6126 {
6127  bool align_to_left = false;
6128  int len;
6129 
6130  /* fast path for typical easy case */
6131  if (width == 0)
6132  {
6134  return;
6135  }
6136 
6137  if (width < 0)
6138  {
6139  /* Negative width: implicit '-' flag, then take absolute value */
6140  align_to_left = true;
6141  /* -INT_MIN is undefined */
6142  if (width <= INT_MIN)
6143  ereport(ERROR,
6144  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6145  errmsg("number is out of range")));
6146  width = -width;
6147  }
6148  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6149  align_to_left = true;
6150 
6151  len = pg_mbstrlen(str);
6152  if (align_to_left)
6153  {
6154  /* left justify */
6156  if (len < width)
6157  appendStringInfoSpaces(buf, width - len);
6158  }
6159  else
6160  {
6161  /* right justify */
6162  if (len < width)
6163  appendStringInfoSpaces(buf, width - len);
6165  }
6166 }
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:980
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5657

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

6177 {
6178  return text_format(fcinfo);
6179 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5672

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

5950 {
5951  bool found = false;
5952  const char *cp = *ptr;
5953  int val = 0;
5954 
5955  while (*cp >= '0' && *cp <= '9')
5956  {
5957  int8 digit = (*cp - '0');
5958 
5959  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5960  unlikely(pg_add_s32_overflow(val, digit, &val)))
5961  ereport(ERROR,
5962  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5963  errmsg("number is out of range")));
5964  ADVANCE_PARSE_POINTER(cp, end_ptr);
5965  found = true;
5966  }
5967 
5968  *ptr = cp;
5969  *value = val;
5970 
5971  return found;
5972 }
signed char int8
Definition: c.h:438
#define unlikely(x)
Definition: c.h:284
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 5998 of file varlena.c.

6001 {
6002  const char *cp = start_ptr;
6003  int n;
6004 
6005  /* set defaults for output parameters */
6006  *argpos = -1;
6007  *widthpos = -1;
6008  *flags = 0;
6009  *width = 0;
6010 
6011  /* try to identify first number */
6012  if (text_format_parse_digits(&cp, end_ptr, &n))
6013  {
6014  if (*cp != '$')
6015  {
6016  /* Must be just a width and a type, so we're done */
6017  *width = n;
6018  return cp;
6019  }
6020  /* The number was argument position */
6021  *argpos = n;
6022  /* Explicit 0 for argument index is immediately refused */
6023  if (n == 0)
6024  ereport(ERROR,
6025  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6026  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6027  ADVANCE_PARSE_POINTER(cp, end_ptr);
6028  }
6029 
6030  /* Handle flags (only minus is supported now) */
6031  while (*cp == '-')
6032  {
6033  *flags |= TEXT_FORMAT_FLAG_MINUS;
6034  ADVANCE_PARSE_POINTER(cp, end_ptr);
6035  }
6036 
6037  if (*cp == '*')
6038  {
6039  /* Handle indirect width */
6040  ADVANCE_PARSE_POINTER(cp, end_ptr);
6041  if (text_format_parse_digits(&cp, end_ptr, &n))
6042  {
6043  /* number in this position must be closed by $ */
6044  if (*cp != '$')
6045  ereport(ERROR,
6046  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6047  errmsg("width argument position must be ended by \"$\"")));
6048  /* The number was width argument position */
6049  *widthpos = n;
6050  /* Explicit 0 for argument index is immediately refused */
6051  if (n == 0)
6052  ereport(ERROR,
6053  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6054  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6055  ADVANCE_PARSE_POINTER(cp, end_ptr);
6056  }
6057  else
6058  *widthpos = 0; /* width's argument position is unspecified */
6059  }
6060  else
6061  {
6062  /* Check for direct width specification */
6063  if (text_format_parse_digits(&cp, end_ptr, &n))
6064  *width = n;
6065  }
6066 
6067  /* cp should now be pointing at type character */
6068  return cp;
6069 }
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5949

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

6079 {
6080  char *str;
6081 
6082  /* Handle NULL arguments before trying to stringify the value. */
6083  if (isNull)
6084  {
6085  if (conversion == 's')
6086  text_format_append_string(buf, "", flags, width);
6087  else if (conversion == 'L')
6088  text_format_append_string(buf, "NULL", flags, width);
6089  else if (conversion == 'I')
6090  ereport(ERROR,
6091  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6092  errmsg("null values cannot be formatted as an SQL identifier")));
6093  return;
6094  }
6095 
6096  /* Stringify. */
6097  str = OutputFunctionCall(typOutputInfo, value);
6098 
6099  /* Escape. */
6100  if (conversion == 'I')
6101  {
6102  /* quote_identifier may or may not allocate a new string. */
6104  }
6105  else if (conversion == 'L')
6106  {
6107  char *qstr = quote_literal_cstr(str);
6108 
6109  text_format_append_string(buf, qstr, flags, width);
6110  /* quote_literal_cstr() always allocates a new string */
6111  pfree(qstr);
6112  }
6113  else
6114  text_format_append_string(buf, str, flags, width);
6115 
6116  /* Cleanup. */
6117  pfree(str);
6118 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12189
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6124

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

1915 {
1916  text *arg1 = PG_GETARG_TEXT_PP(0);
1917  text *arg2 = PG_GETARG_TEXT_PP(1);
1918  bool result;
1919 
1920  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1921 
1922  PG_FREE_IF_COPY(arg1, 0);
1923  PG_FREE_IF_COPY(arg2, 1);
1924 
1925  PG_RETURN_BOOL(result);
1926 }

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

1900 {
1901  text *arg1 = PG_GETARG_TEXT_PP(0);
1902  text *arg2 = PG_GETARG_TEXT_PP(1);
1903  bool result;
1904 
1905  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1906 
1907  PG_FREE_IF_COPY(arg1, 0);
1908  PG_FREE_IF_COPY(arg2, 1);
1909 
1910  PG_RETURN_BOOL(result);
1911 }

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

4760 {
4762  collid,
4763  PointerGetDatum(txt1),
4764  PointerGetDatum(txt2)));
4765 }
#define DatumGetBool(X)
Definition: postgres.h:437
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1757

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

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2829 of file varlena.c.

2830 {
2831  text *arg1 = PG_GETARG_TEXT_PP(0);
2832  text *arg2 = PG_GETARG_TEXT_PP(1);
2833  text *result;
2834 
2835  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2836 
2837  PG_RETURN_TEXT_P(result);
2838 }

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

1885 {
1886  text *arg1 = PG_GETARG_TEXT_PP(0);
1887  text *arg2 = PG_GETARG_TEXT_PP(1);
1888  bool result;
1889 
1890  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1891 
1892  PG_FREE_IF_COPY(arg1, 0);
1893  PG_FREE_IF_COPY(arg2, 1);
1894 
1895  PG_RETURN_BOOL(result);
1896 }

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

5573 {
5574  int n = PG_GETARG_INT32(1);
5575 
5576  if (n < 0)
5577  {
5578  text *str = PG_GETARG_TEXT_PP(0);
5579  const char *p = VARDATA_ANY(str);
5580  int len = VARSIZE_ANY_EXHDR(str);
5581  int rlen;
5582 
5583  n = pg_mbstrlen_with_len(p, len) + n;
5584  rlen = pg_mbcharcliplen(p, len, n);
5586  }
5587  else
5589 }
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1068
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:864

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

691 {
692  /* fastpath when max encoding length is one */
695  else
696  {
697  text *t = DatumGetTextPP(str);
698 
700  VARSIZE_ANY_EXHDR(t)));
701  }
702 }
#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 1869 of file varlena.c.

1870 {
1871  text *arg1 = PG_GETARG_TEXT_PP(0);
1872  text *arg2 = PG_GETARG_TEXT_PP(1);
1873  bool result;
1874 
1875  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1876 
1877  PG_FREE_IF_COPY(arg1, 0);
1878  PG_FREE_IF_COPY(arg2, 1);
1879 
1880  PG_RETURN_BOOL(result);
1881 }

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

3618 {
3619  text *s = PG_GETARG_TEXT_PP(0);
3620  Name result;
3621  int len;
3622 
3623  len = VARSIZE_ANY_EXHDR(s);
3624 
3625  /* Truncate oversize input */
3626  if (len >= NAMEDATALEN)
3628 
3629  /* We use palloc0 here to ensure result is zero-padded */
3630  result = (Name) palloc0(NAMEDATALEN);
3631  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3632 
3633  PG_RETURN_NAME(result);
3634 }
NameData * Name
Definition: c.h:690
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1026
void * palloc0(Size size)
Definition: mcxt.c:1099

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

1095 {
1096  text *result;
1097  text *s1;
1098  text *s2;
1099  int sp_pl_sl;
1100 
1101  /*
1102  * Check for possible integer-overflow cases. For negative sp, throw a
1103  * "substring length" error because that's what should be expected
1104  * according to the spec's definition of OVERLAY().
1105  */
1106  if (sp <= 0)
1107  ereport(ERROR,
1108  (errcode(ERRCODE_SUBSTRING_ERROR),
1109  errmsg("negative substring length not allowed")));
1110  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1111  ereport(ERROR,
1112  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1113  errmsg("integer out of range")));
1114 
1115  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1116  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1117  result = text_catenate(s1, t2);
1118  result = text_catenate(result, s2);
1119 
1120  return result;
1121 }
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:744

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

3110 {
3111  text *arg1 = PG_GETARG_TEXT_PP(0);
3112  text *arg2 = PG_GETARG_TEXT_PP(1);
3113  int result;
3114 
3115  result = internal_text_pattern_compare(arg1, arg2);
3116 
3117  PG_FREE_IF_COPY(arg1, 0);
3118  PG_FREE_IF_COPY(arg2, 1);
3119 
3120  PG_RETURN_BOOL(result >= 0);
3121 }

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

3126 {
3127  text *arg1 = PG_GETARG_TEXT_PP(0);
3128  text *arg2 = PG_GETARG_TEXT_PP(1);
3129  int result;
3130 
3131  result = internal_text_pattern_compare(arg1, arg2);
3132 
3133  PG_FREE_IF_COPY(arg1, 0);
3134  PG_FREE_IF_COPY(arg2, 1);
3135 
3136  PG_RETURN_BOOL(result > 0);
3137 }

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

3094 {
3095  text *arg1 = PG_GETARG_TEXT_PP(0);
3096  text *arg2 = PG_GETARG_TEXT_PP(1);
3097  int result;
3098 
3099  result = internal_text_pattern_compare(arg1, arg2);
3100 
3101  PG_FREE_IF_COPY(arg1, 0);
3102  PG_FREE_IF_COPY(arg2, 1);
3103 
3104  PG_RETURN_BOOL(result <= 0);
3105 }

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

3078 {
3079  text *arg1 = PG_GETARG_TEXT_PP(0);
3080  text *arg2 = PG_GETARG_TEXT_PP(1);
3081  int result;
3082 
3083  result = internal_text_pattern_compare(arg1, arg2);
3084 
3085  PG_FREE_IF_COPY(arg1, 0);
3086  PG_FREE_IF_COPY(arg2, 1);
3087 
3088  PG_RETURN_BOOL(result < 0);
3089 }

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

1155 {
1157  int result;
1158 
1159  /* Empty needle always matches at position 1 */
1160  if (VARSIZE_ANY_EXHDR(t2) < 1)
1161  return 1;
1162 
1163  /* Otherwise, can't match if haystack is shorter than needle */
1164  if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2))
1165  return 0;
1166 
1167  text_position_setup(t1, t2, collid, &state);
1168  if (!text_position_next(&state))
1169  result = 0;
1170  else
1173  return result;
1174 }
static int text_position_get_match_pos(TextPositionState *state)
Definition: varlena.c:1457

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

1482 {
1483  /* no cleanup needed */
1484 }

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

1458 {
1459  /* Convert the byte position to char position. */
1460  state->refpos += pg_mbstrlen_with_len(state->refpoint,
1461  state->last_match - state->refpoint);
1462  state->refpoint = state->last_match;
1463  return state->refpos + 1;
1464 }

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

1447 {
1448  return state->last_match;
1449 }

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

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1314 of file varlena.c.

1315 {
1316  int needle_len = state->len2;
1317  char *start_ptr;
1318  char *matchptr;
1319 
1320  if (needle_len <= 0)
1321  return false; /* result for empty pattern */
1322 
1323  /* Start from the point right after the previous match. */
1324  if (state->last_match)
1325  start_ptr = state->last_match + needle_len;
1326  else
1327  start_ptr = state->str1;
1328 
1329 retry:
1330  matchptr = text_position_next_internal(start_ptr, state);
1331 
1332  if (!matchptr)
1333  return false;
1334 
1335  /*
1336  * Found a match for the byte sequence. If this is a multibyte encoding,
1337  * where one character's byte sequence can appear inside a longer
1338  * multi-byte character, we need to verify that the match was at a
1339  * character boundary, not in the middle of a multi-byte character.
1340  */
1341  if (state->is_multibyte_char_in_char)
1342  {
1343  /* Walk one character at a time, until we reach the match. */
1344 
1345  /* the search should never move backwards. */
1346  Assert(state->refpoint <= matchptr);
1347 
1348  while (state->refpoint < matchptr)
1349  {
1350  /* step to next character. */
1351  state->refpoint += pg_mblen(state->refpoint);
1352  state->refpos++;
1353 
1354  /*
1355  * If we stepped over the match's start position, then it was a
1356  * false positive, where the byte sequence appeared in the middle
1357  * of a multi-byte character. Skip it, and continue the search at
1358  * the next character boundary.
1359  */
1360  if (state->refpoint > matchptr)
1361  {
1362  start_ptr = state->refpoint;
1363  goto retry;
1364  }
1365  }
1366  }
1367 
1368  state->last_match = matchptr;
1369  return true;
1370 }
static char * text_position_next_internal(char *start_ptr, TextPositionState *state)
Definition: varlena.c:1378

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

1379 {
1380  int haystack_len = state->len1;
1381  int needle_len = state->len2;
1382  int skiptablemask = state->skiptablemask;
1383  const char *haystack = state->str1;
1384  const char *needle = state->str2;
1385  const char *haystack_end = &haystack[haystack_len];
1386  const char *hptr;
1387 
1388  Assert(start_ptr >= haystack && start_ptr <= haystack_end);
1389 
1390  if (needle_len == 1)
1391  {
1392  /* No point in using B-M-H for a one-character needle */
1393  char nchar = *needle;
1394 
1395  hptr = start_ptr;
1396  while (hptr < haystack_end)
1397  {
1398  if (*hptr == nchar)
1399  return (char *) hptr;
1400  hptr++;
1401  }
1402  }
1403  else
1404  {
1405  const char *needle_last = &needle[needle_len - 1];
1406 
1407  /* Start at startpos plus the length of the needle */
1408  hptr = start_ptr + needle_len - 1;
1409  while (hptr < haystack_end)
1410  {
1411  /* Match the needle scanning *backward* */
1412  const char *nptr;
1413  const char *p;
1414 
1415  nptr = needle_last;
1416  p = hptr;
1417  while (*nptr == *p)
1418  {
1419  /* Matched it all? If so, return 1-based position */
1420  if (nptr == needle)
1421  return (char *) p;
1422  nptr--, p--;
1423  }
1424 
1425  /*
1426  * No match, so use the haystack char at hptr to decide how far to
1427  * advance. If the needle had any occurrence of that character
1428  * (or more precisely, one sharing the same skiptable entry)
1429  * before its last character, then we advance far enough to align
1430  * the last such needle character with that haystack position.
1431  * Otherwise we can advance by the whole needle length.
1432  */
1433  hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1434  }
1435  }
1436 
1437  return 0; /* not found */
1438 }

References Assert().

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1473 of file varlena.c.

1474 {
1475  state->last_match = NULL;
1476  state->refpoint = state->str1;
1477  state->refpos = 0;
1478 }

Referenced by split_part().

◆ text_position_setup()

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

Definition at line 1194 of file varlena.c.

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

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

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

◆ text_reverse()

Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 5617 of file varlena.c.

5618 {
5619  text *str = PG_GETARG_TEXT_PP(0);
5620  const char *p = VARDATA_ANY(str);
5621  int len = VARSIZE_ANY_EXHDR(str);
5622  const char *endp = p + len;
5623  text *result;
5624  char *dst;
5625 
5626  result = palloc(len + VARHDRSZ);
5627  dst = (char *) VARDATA(result) + len;
5628  SET_VARSIZE(result, len + VARHDRSZ);
5629 
5631  {
5632  /* multibyte version */
5633  while (p < endp)
5634  {
5635  int sz;
5636 
5637  sz = pg_mblen(p);
5638  dst -= sz;
5639  memcpy(dst, p, sz);
5640  p += sz;
5641  }
5642  }
5643  else
5644  {
5645  /* single byte version */
5646  while (p < endp)
5647  *(--dst) = *p++;
5648  }
5649 
5650  PG_RETURN_TEXT_P(result);
5651 }

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

5597 {
5598  text *str = PG_GETARG_TEXT_PP(0);
5599  const char *p = VARDATA_ANY(str);
5600  int len = VARSIZE_ANY_EXHDR(str);
5601  int n = PG_GETARG_INT32(1);
5602  int off;
5603 
5604  if (n < 0)
5605  n = -n;
5606  else
5607  n = pg_mbstrlen_with_len(p, len) - n;
5608  off = pg_mbcharcliplen(p, len, n);
5609 
5611 }

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

2842 {
2843  text *arg1 = PG_GETARG_TEXT_PP(0);
2844  text *arg2 = PG_GETARG_TEXT_PP(1);
2845  text *result;
2846 
2847  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2848 
2849  PG_RETURN_TEXT_P(result);
2850 }

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

1930 {
1931  Datum arg1 = PG_GETARG_DATUM(0);
1932  Datum arg2 = PG_GETARG_DATUM(1);
1933  Oid collid = PG_GET_COLLATION();
1934  pg_locale_t mylocale = 0;
1935  bool result;
1936  Size len1,
1937  len2;
1938 
1939  check_collation_set(collid);
1940 
1941  if (!lc_collate_is_c(collid))
1942  mylocale = pg_newlocale_from_collation(collid);
1943 
1944  if (mylocale && !mylocale->deterministic)
1945  ereport(ERROR,
1946  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1947  errmsg("nondeterministic collations are not supported for substring searches")));
1948 
1949  len1 = toast_raw_datum_size(arg1);
1950  len2 = toast_raw_datum_size(arg2);
1951  if (len2 > len1)
1952  result = false;
1953  else
1954  {
1955  text *targ1 = text_substring(arg1, 1, len2, false);
1956  text *targ2 = DatumGetTextPP(arg2);
1957 
1958  result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1959  VARSIZE_ANY_EXHDR(targ2)) == 0);
1960 
1961  PG_FREE_IF_COPY(targ1, 0);
1962  PG_FREE_IF_COPY(targ2, 1);
1963  }
1964 
1965  PG_RETURN_BOOL(result);
1966 }

References check_collation_set(), DatumGetTextPP, pg_locale_struct::deterministic, ereport, errcode(), errmsg(), ERROR, lc_collate_is_c(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_DATUM, 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 845 of file varlena.c.

846 {
848  PG_GETARG_INT32(1),
849  -1, true));
850 }

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

865 {
867  int32 S = start; /* start position */
868  int32 S1; /* adjusted start position */
869  int32 L1; /* adjusted substring length */
870  int32 E; /* end position */
871 
872  /*
873  * SQL99 says S can be zero or negative, but we still must fetch from the
874  * start of the string.
875  */
876  S1 = Max(S, 1);
877 
878  /* life is easy if the encoding max length is 1 */
879  if (eml == 1)
880  {
881  if (length_not_specified) /* special case - get length to end of
882  * string */
883  L1 = -1;
884  else if (length < 0)
885  {
886  /* SQL99 says to throw an error for E < S, i.e., negative length */
887  ereport(ERROR,
888  (errcode(ERRCODE_SUBSTRING_ERROR),
889  errmsg("negative substring length not allowed")));
890  L1 = -1; /* silence stupider compilers */
891  }
892  else if (pg_add_s32_overflow(S, length, &E))
893  {
894  /*
895  * L could be large enough for S + L to overflow, in which case
896  * the substring must run to end of string.
897  */
898  L1 = -1;
899  }
900  else
901  {
902  /*
903  * A zero or negative value for the end position can happen if the
904  * start was negative or one. SQL99 says to return a zero-length
905  * string.
906  */
907  if (E < 1)
908  return cstring_to_text("");
909 
910  L1 = E - S1;
911  }
912 
913  /*
914  * If the start position is past the end of the string, SQL99 says to
915  * return a zero-length string -- DatumGetTextPSlice() will do that
916  * for us. We need only convert S1 to zero-based starting position.
917  */
918  return DatumGetTextPSlice(str, S1 - 1, L1);
919  }
920  else if (eml > 1)
921  {
922  /*
923  * When encoding max length is > 1, we can't get LC without
924  * detoasting, so we'll grab a conservatively large slice now and go
925  * back later to do the right thing
926  */
927  int32 slice_start;
928  int32 slice_size;
929  int32 slice_strlen;
930  text *slice;
931  int32 E1;
932  int32 i;
933  char *p;
934  char *s;
935  text *ret;
936 
937  /*
938  * We need to start at position zero because there is no way to know
939  * in advance which byte offset corresponds to the supplied start
940  * position.
941  */
942  slice_start = 0;
943 
944  if (length_not_specified) /* special case - get length to end of
945  * string */
946  slice_size = L1 = -1;
947  else if (length < 0)
948  {
949  /* SQL99 says to throw an error for E < S, i.e., negative length */
950  ereport(ERROR,
951  (errcode(ERRCODE_SUBSTRING_ERROR),
952  errmsg("negative substring length not allowed")));
953  slice_size = L1 = -1; /* silence stupider compilers */
954  }
955  else if (pg_add_s32_overflow(S, length, &E))
956  {
957  /*
958  * L could be large enough for S + L to overflow, in which case
959  * the substring must run to end of string.
960  */
961  slice_size = L1 = -1;
962  }
963  else
964  {
965  /*
966  * A zero or negative value for the end position can happen if the
967  * start was negative or one. SQL99 says to return a zero-length
968  * string.
969  */
970  if (E < 1)
971  return cstring_to_text("");
972 
973  /*
974  * if E is past the end of the string, the tuple toaster will
975  * truncate the length for us
976  */
977  L1 = E - S1;
978 
979  /*
980  * Total slice size in bytes can't be any longer than the start
981  * position plus substring length times the encoding max length.
982  * If that overflows, we can just use -1.
983  */
984  if (pg_mul_s32_overflow(E, eml, &slice_size))
985  slice_size = -1;
986  }
987 
988  /*
989  * If we're working with an untoasted source, no need to do an extra
990  * copying step.
991  */
994  slice = DatumGetTextPSlice(str, slice_start, slice_size);
995  else
996  slice = (text *) DatumGetPointer(str);
997 
998  /* see if we got back an empty string */
999  if (VARSIZE_ANY_EXHDR(slice) == 0)
1000  {
1001  if (slice != (text *) DatumGetPointer(str))
1002  pfree(slice);
1003  return cstring_to_text("");
1004  }
1005 
1006  /* Now we can get the actual length of the slice in MB characters */
1007  slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
1008  VARSIZE_ANY_EXHDR(slice));
1009 
1010  /*
1011  * Check that the start position wasn't > slice_strlen. If so, SQL99
1012  * says to return a zero-length string.
1013  */
1014  if (S1 > slice_strlen)
1015  {
1016  if (slice != (text *) DatumGetPointer(str))
1017  pfree(slice);
1018  return cstring_to_text("");
1019  }
1020 
1021  /*
1022  * Adjust L1 and E1 now that we know the slice string length. Again
1023  * remember that S1 is one based, and slice_start is zero based.
1024  */
1025  if (L1 > -1)
1026  E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
1027  else
1028  E1 = slice_start + 1 + slice_strlen;
1029 
1030  /*
1031  * Find the start position in the slice; remember S1 is not zero based
1032  */
1033  p = VARDATA_ANY(slice);
1034  for (i = 0; i < S1 - 1; i++)
1035  p += pg_mblen(p);
1036 
1037  /* hang onto a pointer to our start position */
1038  s = p;
1039 
1040  /*
1041  * Count the actual bytes used by the substring of the requested
1042  * length.
1043  */
1044  for (i = S1; i < E1; i++)
1045  p += pg_mblen(p);
1046 
1047  ret = (text *) palloc(VARHDRSZ + (p - s));
1048  SET_VARSIZE(ret, VARHDRSZ + (p - s));
1049  memcpy(VARDATA(ret), s, (p - s));
1050 
1051  if (slice != (text *) DatumGetPointer(str))
1052  pfree(slice);
1053 
1054  return ret;
1055  }
1056  else
1057  elog(ERROR, "invalid backend encoding: encoding max length < 1");
1058 
1059  /* not reached: suppress compiler warning */
1060  return NULL;
1061 }
#define DatumGetTextPSlice(X, m, n)
Definition: fmgr.h:304
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:325
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326

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

Referenced by text_left(), text_overlay(), text_starts_with(), text_substr(), and text_substr_no_len().

◆ text_to_array()

Datum text_to_array ( PG_FUNCTION_ARGS  )

Definition at line 4773 of file varlena.c.

4774 {
4775  SplitTextOutputData tstate;
4776 
4777  /* For array output, tstate should start as all zeroes */
4778  memset(&tstate, 0, sizeof(tstate));
4779 
4780  if (!split_text(fcinfo, &tstate))
4781  PG_RETURN_NULL();
4782 
4783  if (tstate.astate == NULL)
4785 
4788 }
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3526
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5342
static bool split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
Definition: varlena.c:4850

References SplitTextOutputData::astate, construct_empty_array(), CurrentMemoryContext, makeArrayResult(), PG_RETURN_ARRAYTYPE_P, PG_RETURN_NULL, and split_text().

Referenced by text_to_array_null().

◆ text_to_array_null()

Datum text_to_array_null ( PG_FUNCTION_ARGS  )

Definition at line 4799 of file varlena.c.

4800 {
4801  return text_to_array(fcinfo);
4802 }
Datum text_to_array(PG_FUNCTION_ARGS)
Definition: varlena.c:4773

References text_to_array().

◆ text_to_cstring()

char* text_to_cstring ( const text t)

Definition at line 221 of file varlena.c.

222 {
223  /* must cast away the const, unfortunately */
224  text *tunpacked = pg_detoast_datum_packed(unconstify(text *, t));
225  int len = VARSIZE_ANY_EXHDR(tunpacked);
226  char *result;
227 
228  result = (char *) palloc(len + 1);
229  memcpy(result, VARDATA_ANY(tunpacked), len);
230  result[len] = '\0';
231 
232  if (tunpacked != t)
233  pfree(tunpacked);
234 
235  return result;
236 }
#define unconstify(underlying_type, expr)
Definition: c.h:1251
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition: fmgr.c:1740

References len, palloc(), pfree(), pg_detoast_datum_packed(), unconstify, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by array_to_text(), array_to_text_null(), binary_upgrade_create_empty_extension(), binary_upgrade_set_missing_value(), connectby_text(), connectby_text_serial(), convert_and_check_filename(), convert_any_priv_string(), convert_column_name(), convert_database_name(), convert_foreign_data_wrapper_name(), convert_function_name(), convert_language_name(), convert_schema_name(), convert_server_name(), convert_tablespace_name(), convert_type_name(), crosstab(), crosstab_hash(), cursor_to_xml(), cursor_to_xmlschema(), database_to_xml(), database_to_xml_and_xmlschema(), database_to_xmlschema(), datetime_to_char_body(), datum_to_json(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_fetch(), dblink_get_notify(),