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

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 
struct  SplitTextOutputData
 

Macros

#define TEXTBUFLEN   1024
 
#define 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)
 
void initClosestMatch (ClosestMatchState *state, const char *source, int max_d)
 
void updateClosestMatch (ClosestMatchState *state, const char *candidate)
 
const char * getClosestMatch (ClosestMatchState *state)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 
static bool isxdigits_n (const char *instr, size_t n)
 
static unsigned int hexval (unsigned char c)
 
static unsigned int hexval_n (const char *instr, size_t n)
 
Datum unistr (PG_FUNCTION_ARGS)
 

Variables

int bytea_output = BYTEA_OUTPUT_HEX
 

Macro Definition Documentation

◆ ADVANCE_PARSE_POINTER

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

Definition at line 5656 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
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:550

Definition at line 2986 of file varlena.c.

◆ DatumGetUnknownP

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

Definition at line 115 of file varlena.c.

◆ DatumGetUnknownPCopy

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

Definition at line 116 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 121 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 122 of file varlena.c.

◆ DIG

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

Definition at line 281 of file varlena.c.

◆ HEXBASE

#define HEXBASE   16

Definition at line 5181 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6196 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P

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

Definition at line 117 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P_COPY

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

Definition at line 118 of file varlena.c.

◆ PG_RETURN_UNKNOWN_P

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 119 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 3241 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

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

Definition at line 5654 of file varlena.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 113 of file varlena.c.

◆ VAL

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

Definition at line 280 of file varlena.c.

Typedef Documentation

◆ unknown

typedef struct varlena unknown

Definition at line 46 of file varlena.c.

◆ VarString

typedef struct varlena VarString

Definition at line 46 of file varlena.c.

Function Documentation

◆ appendStringInfoRegexpSubstr()

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

Definition at line 4361 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1970 of file varlena.c.

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

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

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

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

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

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

2811 {
2812  /* Oid opcintype = PG_GETARG_OID(0); */
2813  Oid collid = PG_GET_COLLATION();
2814 
2815  check_collation_set(collid);
2816 
2817  if (lc_collate_is_c(collid) ||
2818  collid == DEFAULT_COLLATION_OID ||
2820  PG_RETURN_BOOL(true);
2821  else
2822  PG_RETURN_BOOL(false);
2823 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1080
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1299
static void check_collation_set(Oid collid)
Definition: varlena.c:1489

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

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

References elog(), ERROR, FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_fn_expr_argtype(), getTypeOutputInfo(), i, MemoryContextAlloc(), OidIsValid, and PG_NARGS.

Referenced by concat_internal().

◆ bytea_bit_count()

Datum bytea_bit_count ( PG_FUNCTION_ARGS  )

Definition at line 3406 of file varlena.c.

3407 {
3408  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3409 
3411 }
#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 3208 of file varlena.c.

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

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

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

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

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

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

534 {
536 
537  /* cannot be called directly because of internal-type argument */
538  Assert(AggCheckCallContext(fcinfo, NULL));
539 
540  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
541 
542  if (state != NULL)
543  {
544  bytea *result;
545 
546  result = (bytea *) palloc(state->len + VARHDRSZ);
547  SET_VARSIZE(result, state->len + VARHDRSZ);
548  memcpy(VARDATA(result), state->data, state->len);
549  PG_RETURN_BYTEA_P(result);
550  }
551  else
552  PG_RETURN_NULL();
553 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4509
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 501 of file varlena.c.

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

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

3261 {
3263  PG_GETARG_INT32(1),
3264  PG_GETARG_INT32(2),
3265  false));
3266 }
#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 3274 of file varlena.c.

3275 {
3277  PG_GETARG_INT32(1),
3278  -1,
3279  true));
3280 }

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

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

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

3194 {
3195  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3196  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3197 
3199 }

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 4193 of file varlena.c.

4194 {
4195  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4196  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4197  int len1,
4198  len2;
4199  int cmp;
4200 
4201  len1 = VARSIZE_ANY_EXHDR(arg1);
4202  len2 = VARSIZE_ANY_EXHDR(arg2);
4203 
4204  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4205  if ((cmp == 0) && (len1 != len2))
4206  cmp = (len1 < len2) ? -1 : 1;
4207 
4208  PG_FREE_IF_COPY(arg1, 0);
4209  PG_FREE_IF_COPY(arg2, 1);
4210 
4212 }
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 4049 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3178 {
3179  Datum str = PG_GETARG_DATUM(0);
3180 
3181  /* We need not detoast the input at all */
3183 }

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

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

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

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

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

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

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

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

475 {
477  bytea *result;
478  int nbytes;
479 
480  nbytes = buf->len - buf->cursor;
481  result = (bytea *) palloc(nbytes + VARHDRSZ);
482  SET_VARSIZE(result, nbytes + VARHDRSZ);
483  pq_copymsgbytes(buf, VARDATA(result), nbytes);
484  PG_RETURN_BYTEA_P(result);
485 }
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 493 of file varlena.c.

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

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

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

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

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

787 {
789  {
790  /* Optimization for single-byte encodings */
791  return n;
792  }
793  else
794  {
795  const char *s;
796 
797  for (s = p; n > 0; n--)
798  s += pg_mblen(s);
799 
800  return s - p;
801  }
802 }
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 1489 of file varlena.c.

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

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

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

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

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

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

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

References cstring_to_text_with_len().

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

◆ cstring_to_text_with_len()

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

Definition at line 201 of file varlena.c.

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

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

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

◆ getClosestMatch()

const char* getClosestMatch ( ClosestMatchState state)

Definition at line 6274 of file varlena.c.

6275 {
6276  Assert(state);
6277 
6278  return state->match;
6279 }

References Assert().

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

◆ hexval()

static unsigned int hexval ( unsigned char  c)
static

Definition at line 6444 of file varlena.c.

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

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

References hexval(), and i.

Referenced by unistr().

◆ initClosestMatch()

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

Definition at line 6219 of file varlena.c.

6220 {
6221  Assert(state);
6222  Assert(max_d >= 0);
6223 
6224  state->source = source;
6225  state->min_d = -1;
6226  state->max_d = max_d;
6227  state->match = NULL;
6228 }
static rewind_source * source
Definition: pg_rewind.c:81

References Assert(), and source.

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

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 3052 of file varlena.c.

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

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

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

References i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5343 of file varlena.c.

5344 {
5345  StringInfo state;
5346  MemoryContext aggcontext;
5347  MemoryContext oldcontext;
5348 
5349  if (!AggCheckCallContext(fcinfo, &aggcontext))
5350  {
5351  /* cannot be called directly because of internal-type argument */
5352  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5353  }
5354 
5355  /*
5356  * Create state in aggregate context. It'll stay there across subsequent
5357  * calls.
5358  */
5359  oldcontext = MemoryContextSwitchTo(aggcontext);
5360  state = makeStringInfo();
5361  MemoryContextSwitchTo(oldcontext);
5362 
5363  return state;
5364 }
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 3637 of file varlena.c.

3638 {
3639  Name s = PG_GETARG_NAME(0);
3640 
3642 }
text * cstring_to_text(const char *s)
Definition: varlena.c:189

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

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

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

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

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

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

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

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 3011 of file varlena.c.

3012 {
3014 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2986
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2955

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 3005 of file varlena.c.

3006 {
3008 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2999 of file varlena.c.

3000 {
3002 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2993 of file varlena.c.

2994 {
2996 }

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2905 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4624 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5548 of file varlena.c.

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

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

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

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

6123 {
6124  bool align_to_left = false;
6125  int len;
6126 
6127  /* fast path for typical easy case */
6128  if (width == 0)
6129  {
6131  return;
6132  }
6133 
6134  if (width < 0)
6135  {
6136  /* Negative width: implicit '-' flag, then take absolute value */
6137  align_to_left = true;
6138  /* -INT_MIN is undefined */
6139  if (width <= INT_MIN)
6140  ereport(ERROR,
6141  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6142  errmsg("number is out of range")));
6143  width = -width;
6144  }
6145  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6146  align_to_left = true;
6147 
6148  len = pg_mbstrlen(str);
6149  if (align_to_left)
6150  {
6151  /* left justify */
6153  if (len < width)
6154  appendStringInfoSpaces(buf, width - len);
6155  }
6156  else
6157  {
6158  /* right justify */
6159  if (len < width)
6160  appendStringInfoSpaces(buf, width - len);
6162  }
6163 }
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:5654

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

6174 {
6175  return text_format(fcinfo);
6176 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5669

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

5947 {
5948  bool found = false;
5949  const char *cp = *ptr;
5950  int val = 0;
5951 
5952  while (*cp >= '0' && *cp <= '9')
5953  {
5954  int8 digit = (*cp - '0');
5955 
5956  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5957  unlikely(pg_add_s32_overflow(val, digit, &val)))
5958  ereport(ERROR,
5959  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5960  errmsg("number is out of range")));
5961  ADVANCE_PARSE_POINTER(cp, end_ptr);
5962  found = true;
5963  }
5964 
5965  *ptr = cp;
5966  *value = val;
5967 
5968  return found;
5969 }
signed char int8
Definition: c.h:428
#define unlikely(x)
Definition: c.h:295
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140

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

Referenced by text_format_parse_format().

◆ text_format_parse_format()

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

Definition at line 5995 of file varlena.c.

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

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

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

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

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

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

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

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

4757 {
4759  collid,
4760  PointerGetDatum(txt1),
4761  PointerGetDatum(txt2)));
4762 }
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1758

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

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2826 of file varlena.c.

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

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

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

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

5570 {
5571  int n = PG_GETARG_INT32(1);
5572 
5573  if (n < 0)
5574  {
5575  text *str = PG_GETARG_TEXT_PP(0);
5576  const char *p = VARDATA_ANY(str);
5577  int len = VARSIZE_ANY_EXHDR(str);
5578  int rlen;
5579 
5580  n = pg_mbstrlen_with_len(p, len) + n;
5581  rlen = pg_mbcharcliplen(p, len, n);
5583  }
5584  else
5586 }
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:865

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

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

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

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

3615 {
3616  text *s = PG_GETARG_TEXT_PP(0);
3617  Name result;
3618  int len;
3619 
3620  len = VARSIZE_ANY_EXHDR(s);
3621 
3622  /* Truncate oversize input */
3623  if (len >= NAMEDATALEN)
3625 
3626  /* We use palloc0 here to ensure result is zero-padded */
3627  result = (Name) palloc0(NAMEDATALEN);
3628  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3629 
3630  PG_RETURN_NAME(result);
3631 }
NameData * Name
Definition: c.h:680
#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:1230

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1448 {
1449  return state->last_match;
1450 }

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

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1315 of file varlena.c.

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

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

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

References Assert().

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1474 of file varlena.c.

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

Referenced by split_part().

◆ text_position_setup()

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

Definition at line 1195 of file varlena.c.

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

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

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

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

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

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

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

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

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

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

847 {
849  PG_GETARG_INT32(1),
850  -1, true));
851 }

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

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

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

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

References SplitTextOutputData::astate, construct_empty_array(), CurrentMemoryContext, makeArrayResult(), PG_RETURN_ARRAYTYPE_P, PG_RETURN_DATUM, 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 4796 of file varlena.c.

4797 {
4798  return