PostgreSQL Source Code  git master
varlena.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "access/detoast.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 "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "parser/scansup.h"
#include "port/pg_bswap.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#include "utils/bytea.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/sortsupport.h"
#include "utils/varlena.h"
#include "levenshtein.c"
Include dependency graph for varlena.c:

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 
struct  SplitTextOutputData
 

Macros

#define TEXTBUFLEN   1024
 
#define DatumGetUnknownP(X)   ((unknown *) PG_DETOAST_DATUM(X))
 
#define DatumGetUnknownPCopy(X)   ((unknown *) PG_DETOAST_DATUM_COPY(X))
 
#define PG_GETARG_UNKNOWN_P(n)   DatumGetUnknownP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_UNKNOWN_P_COPY(n)   DatumGetUnknownPCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_UNKNOWN_P(x)   PG_RETURN_POINTER(x)
 
#define DatumGetVarStringP(X)   ((VarString *) PG_DETOAST_DATUM(X))
 
#define DatumGetVarStringPP(X)   ((VarString *) PG_DETOAST_DATUM_PACKED(X))
 
#define VAL(CH)   ((CH) - '0')
 
#define DIG(VAL)   ((VAL) + '0')
 
#define CmpCall(cmpfunc)
 
#define PG_STR_GET_BYTEA(str_)   DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#define REGEXP_REPLACE_BACKREF_CNT   10
 
#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 int varstrcmp_abbrev (Datum x, Datum y, 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)
 
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 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 bool check_replace_text_has_escape_char (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, void *regexp, text *replace_text, bool glob)
 
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 string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 

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:1068
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:821

Definition at line 5549 of file varlena.c.

Referenced by text_format(), text_format_parse_digits(), and text_format_parse_format().

◆ CmpCall

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

Definition at line 2995 of file varlena.c.

Referenced by namegetext(), namegttext(), nameletext(), namelttext(), textgename(), textgtname(), textlename(), and textltname().

◆ DatumGetUnknownP

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

Definition at line 113 of file varlena.c.

◆ DatumGetUnknownPCopy

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

Definition at line 114 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 119 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 120 of file varlena.c.

Referenced by varlenafastcmp_locale(), varstr_abbrev_convert(), and varstrfastcmp_c().

◆ DIG

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

Definition at line 280 of file varlena.c.

Referenced by byteaout().

◆ HEXBASE

#define HEXBASE   16

Definition at line 5127 of file varlena.c.

Referenced by to_hex32(), and to_hex64().

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6089 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P

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

Definition at line 115 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P_COPY

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

Definition at line 116 of file varlena.c.

◆ PG_RETURN_UNKNOWN_P

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 117 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 3250 of file varlena.c.

Referenced by bytea_substring().

◆ REGEXP_REPLACE_BACKREF_CNT

#define REGEXP_REPLACE_BACKREF_CNT   10

Definition at line 4443 of file varlena.c.

Referenced by replace_text_regexp().

◆ TEXT_FORMAT_FLAG_MINUS

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

Definition at line 5547 of file varlena.c.

Referenced by text_format_append_string(), and text_format_parse_format().

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 111 of file varlena.c.

Referenced by varstr_cmp(), and varstr_sortsupport().

◆ VAL

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

Definition at line 279 of file varlena.c.

Referenced by byteain().

Typedef Documentation

◆ unknown

typedef struct varlena unknown

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

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, charlen_to_bytelen(), idx(), pg_database_encoding_max_length(), pg_mblen(), regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

4352 {
4353  const char *p = VARDATA_ANY(replace_text);
4354  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4355  int eml = pg_database_encoding_max_length();
4356 
4357  for (;;)
4358  {
4359  const char *chunk_start = p;
4360  int so;
4361  int eo;
4362 
4363  /* Find next escape char. */
4364  if (eml == 1)
4365  {
4366  for (; p < p_end && *p != '\\'; p++)
4367  /* nothing */ ;
4368  }
4369  else
4370  {
4371  for (; p < p_end && *p != '\\'; p += pg_mblen(p))
4372  /* nothing */ ;
4373  }
4374 
4375  /* Copy the text we just scanned over, if any. */
4376  if (p > chunk_start)
4377  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4378 
4379  /* Done if at end of string, else advance over escape char. */
4380  if (p >= p_end)
4381  break;
4382  p++;
4383 
4384  if (p >= p_end)
4385  {
4386  /* Escape at very end of input. Treat same as unexpected char */
4387  appendStringInfoChar(str, '\\');
4388  break;
4389  }
4390 
4391  if (*p >= '1' && *p <= '9')
4392  {
4393  /* Use the back reference of regexp. */
4394  int idx = *p - '0';
4395 
4396  so = pmatch[idx].rm_so;
4397  eo = pmatch[idx].rm_eo;
4398  p++;
4399  }
4400  else if (*p == '&')
4401  {
4402  /* Use the entire matched string. */
4403  so = pmatch[0].rm_so;
4404  eo = pmatch[0].rm_eo;
4405  p++;
4406  }
4407  else if (*p == '\\')
4408  {
4409  /* \\ means transfer one \ to output. */
4410  appendStringInfoChar(str, '\\');
4411  p++;
4412  continue;
4413  }
4414  else
4415  {
4416  /*
4417  * If escape char is not followed by any expected char, just treat
4418  * it as ordinary data to copy. (XXX would it be better to throw
4419  * an error?)
4420  */
4421  appendStringInfoChar(str, '\\');
4422  continue;
4423  }
4424 
4425  if (so != -1 && eo != -1)
4426  {
4427  /*
4428  * Copy the text that is back reference of regexp. Note so and eo
4429  * are counted in characters not bytes.
4430  */
4431  char *chunk_start;
4432  int chunk_len;
4433 
4434  Assert(so >= data_pos);
4435  chunk_start = start_ptr;
4436  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4437  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4438  appendBinaryStringInfo(str, chunk_start, chunk_len);
4439  }
4440  }
4441 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
regoff_t rm_eo
Definition: regex.h:86
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:785
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define Assert(condition)
Definition: c.h:746
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1436
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ appendStringInfoText()

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

Definition at line 4227 of file varlena.c.

References appendBinaryStringInfo(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text(), replace_text_regexp(), string_agg_transfn(), xml_send(), xmlcomment(), and XmlTableGetValue().

4228 {
4230 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4968 of file varlena.c.

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

4969 {
4971  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4972 
4973  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4974 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5010
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
char * text_to_cstring(const text *t)
Definition: varlena.c:221

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

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(), StringInfoData::data, ArrayMetaState::element_type, fetch_att, FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_type_io_data(), i, initStringInfo(), IOFunc_output, StringInfoData::len, 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().

5012 {
5013  text *result;
5014  int nitems,
5015  *dims,
5016  ndims;
5017  Oid element_type;
5018  int typlen;
5019  bool typbyval;
5020  char typalign;
5022  bool printed = false;
5023  char *p;
5024  bits8 *bitmap;
5025  int bitmask;
5026  int i;
5027  ArrayMetaState *my_extra;
5028 
5029  ndims = ARR_NDIM(v);
5030  dims = ARR_DIMS(v);
5031  nitems = ArrayGetNItems(ndims, dims);
5032 
5033  /* if there are no elements, return an empty string */
5034  if (nitems == 0)
5035  return cstring_to_text_with_len("", 0);
5036 
5037  element_type = ARR_ELEMTYPE(v);
5038  initStringInfo(&buf);
5039 
5040  /*
5041  * We arrange to look up info about element type, including its output
5042  * conversion proc, only once per series of calls, assuming the element
5043  * type doesn't change underneath us.
5044  */
5045  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5046  if (my_extra == NULL)
5047  {
5048  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5049  sizeof(ArrayMetaState));
5050  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5051  my_extra->element_type = ~element_type;
5052  }
5053 
5054  if (my_extra->element_type != element_type)
5055  {
5056  /*
5057  * Get info about element type, including its output conversion proc
5058  */
5059  get_type_io_data(element_type, IOFunc_output,
5060  &my_extra->typlen, &my_extra->typbyval,
5061  &my_extra->typalign, &my_extra->typdelim,
5062  &my_extra->typioparam, &my_extra->typiofunc);
5063  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
5064  fcinfo->flinfo->fn_mcxt);
5065  my_extra->element_type = element_type;
5066  }
5067  typlen = my_extra->typlen;
5068  typbyval = my_extra->typbyval;
5069  typalign = my_extra->typalign;
5070 
5071  p = ARR_DATA_PTR(v);
5072  bitmap = ARR_NULLBITMAP(v);
5073  bitmask = 1;
5074 
5075  for (i = 0; i < nitems; i++)
5076  {
5077  Datum itemvalue;
5078  char *value;
5079 
5080  /* Get source element, checking for NULL */
5081  if (bitmap && (*bitmap & bitmask) == 0)
5082  {
5083  /* if null_string is NULL, we just ignore null elements */
5084  if (null_string != NULL)
5085  {
5086  if (printed)
5087  appendStringInfo(&buf, "%s%s", fldsep, null_string);
5088  else
5089  appendStringInfoString(&buf, null_string);
5090  printed = true;
5091  }
5092  }
5093  else
5094  {
5095  itemvalue = fetch_att(p, typbyval, typlen);
5096 
5097  value = OutputFunctionCall(&my_extra->proc, itemvalue);
5098 
5099  if (printed)
5100  appendStringInfo(&buf, "%s%s", fldsep, value);
5101  else
5102  appendStringInfoString(&buf, value);
5103  printed = true;
5104 
5105  p = att_addlength_pointer(p, typlen, p);
5106  p = (char *) att_align_nominal(p, typalign);
5107  }
5108 
5109  /* advance bitmap pointer if any */
5110  if (bitmap)
5111  {
5112  bitmask <<= 1;
5113  if (bitmask == 0x100)
5114  {
5115  bitmap++;
5116  bitmask = 1;
5117  }
5118  }
5119  }
5120 
5121  result = cstring_to_text_with_len(buf.data, buf.len);
5122  pfree(buf.data);
5123 
5124  return result;
5125 }
MemoryContext fn_mcxt
Definition: fmgr.h:65
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:148
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
unsigned int Oid
Definition: postgres_ext.h:31
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
bool typbyval
Definition: array.h:228
void pfree(void *pointer)
Definition: mcxt.c:1057
char typalign
Definition: pg_type.h:170
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ARR_DIMS(a)
Definition: array.h:282
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define ARR_DATA_PTR(a)
Definition: array.h:310
int16 typlen
Definition: array.h:227
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
char typdelim
Definition: array.h:230
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:176
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uint8 bits8
Definition: c.h:382
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
static struct @143 value
Oid typioparam
Definition: array.h:231
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:278
Oid typiofunc
Definition: array.h:232
char typalign
Definition: array.h:229
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
FmgrInfo proc
Definition: array.h:233
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
int i
Oid element_type
Definition: array.h:226
Definition: c.h:563
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define ARR_NULLBITMAP(a)
Definition: array.h:288
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:2213

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4984 of file varlena.c.

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().

4985 {
4986  ArrayType *v;
4987  char *fldsep;
4988  char *null_string;
4989 
4990  /* returns NULL when first or second parameter is NULL */
4991  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4992  PG_RETURN_NULL();
4993 
4994  v = PG_GETARG_ARRAYTYPE_P(0);
4995  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4996 
4997  /* NULL null string is passed through as a null pointer */
4998  if (!PG_ARGISNULL(2))
4999  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
5000  else
5001  null_string = NULL;
5002 
5003  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
5004 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5010
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
char * text_to_cstring(const text *t)
Definition: varlena.c:221
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ bpcharfastcmp_c()

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

Definition at line 2194 of file varlena.c.

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

Referenced by varstr_sortsupport().

2195 {
2196  BpChar *arg1 = DatumGetBpCharPP(x);
2197  BpChar *arg2 = DatumGetBpCharPP(y);
2198  char *a1p,
2199  *a2p;
2200  int len1,
2201  len2,
2202  result;
2203 
2204  a1p = VARDATA_ANY(arg1);
2205  a2p = VARDATA_ANY(arg2);
2206 
2207  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2208  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2209 
2210  result = memcmp(a1p, a2p, Min(len1, len2));
2211  if ((result == 0) && (len1 != len2))
2212  result = (len1 < len2) ? -1 : 1;
2213 
2214  /* We can't afford to leak memory here. */
2215  if (PointerGetDatum(arg1) != x)
2216  pfree(arg1);
2217  if (PointerGetDatum(arg2) != y)
2218  pfree(arg2);
2219 
2220  return result;
2221 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PointerGetDatum(X)
Definition: postgres.h:556
#define Min(x, y)
Definition: c.h:928
void pfree(void *pointer)
Definition: mcxt.c:1057
int bpchartruelen(char *s, int len)
Definition: varchar.c:671
#define DatumGetBpCharPP(X)
Definition: fmgr.h:292
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

Definition at line 2964 of file varlena.c.

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().

2965 {
2966  Name arg1 = PG_GETARG_NAME(0);
2967  text *arg2 = PG_GETARG_TEXT_PP(1);
2968  int32 result;
2969 
2970  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2971  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2972  PG_GET_COLLATION());
2973 
2974  PG_FREE_IF_COPY(arg2, 1);
2975 
2976  PG_RETURN_INT32(result);
2977 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:363
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1510
Definition: c.h:617
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:623
Definition: c.h:563
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 3147 of file varlena.c.

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

3148 {
3149  text *arg1 = PG_GETARG_TEXT_PP(0);
3150  text *arg2 = PG_GETARG_TEXT_PP(1);
3151  int result;
3152 
3153  result = internal_text_pattern_compare(arg1, arg2);
3154 
3155  PG_FREE_IF_COPY(arg1, 0);
3156  PG_FREE_IF_COPY(arg2, 1);
3157 
3158  PG_RETURN_INT32(result);
3159 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3061
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3163 of file varlena.c.

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

3164 {
3166  MemoryContext oldcontext;
3167 
3168  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3169 
3170  /* Use generic string SortSupport, forcing "C" collation */
3171  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
3172 
3173  MemoryContextSwitchTo(oldcontext);
3174 
3175  PG_RETURN_VOID();
3176 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1995
#define PG_RETURN_VOID()
Definition: fmgr.h:348

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1953 of file varlena.c.

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

Referenced by gbt_textcmp(), and leftmostvalue_text().

1954 {
1955  text *arg1 = PG_GETARG_TEXT_PP(0);
1956  text *arg2 = PG_GETARG_TEXT_PP(1);
1957  int32 result;
1958 
1959  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1960 
1961  PG_FREE_IF_COPY(arg1, 0);
1962  PG_FREE_IF_COPY(arg2, 1);
1963 
1964  PG_RETURN_INT32(result);
1965 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:363
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

Definition at line 2980 of file varlena.c.

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().

2981 {
2982  text *arg1 = PG_GETARG_TEXT_PP(0);
2983  Name arg2 = PG_GETARG_NAME(1);
2984  int32 result;
2985 
2986  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
2987  NameStr(*arg2), strlen(NameStr(*arg2)),
2988  PG_GET_COLLATION());
2989 
2990  PG_FREE_IF_COPY(arg1, 0);
2991 
2992  PG_RETURN_INT32(result);
2993 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:363
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1510
Definition: c.h:617
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:623
Definition: c.h:563
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1968 of file varlena.c.

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

1969 {
1971  Oid collid = ssup->ssup_collation;
1972  MemoryContext oldcontext;
1973 
1974  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1975 
1976  /* Use generic string SortSupport */
1977  varstr_sortsupport(ssup, TEXTOID, collid);
1978 
1979  MemoryContextSwitchTo(oldcontext);
1980 
1981  PG_RETURN_VOID();
1982 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1995
#define PG_RETURN_VOID()
Definition: fmgr.h:348

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2819 of file varlena.c.

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

2820 {
2821  /* Oid opcintype = PG_GETARG_OID(0); */
2822  Oid collid = PG_GET_COLLATION();
2823 
2824  check_collation_set(collid);
2825 
2826  if (lc_collate_is_c(collid) ||
2827  collid == DEFAULT_COLLATION_OID ||
2829  PG_RETURN_BOOL(true);
2830  else
2831  PG_RETURN_BOOL(false);
2832 }
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1052
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:198
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1347
static void check_collation_set(Oid collid)
Definition: varlena.c:1481
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ build_concat_foutcache()

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

Definition at line 5308 of file varlena.c.

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().

5309 {
5310  FmgrInfo *foutcache;
5311  int i;
5312 
5313  /* We keep the info in fn_mcxt so it survives across calls */
5314  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5315  PG_NARGS() * sizeof(FmgrInfo));
5316 
5317  for (i = argidx; i < PG_NARGS(); i++)
5318  {
5319  Oid valtype;
5320  Oid typOutput;
5321  bool typIsVarlena;
5322 
5323  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5324  if (!OidIsValid(valtype))
5325  elog(ERROR, "could not determine data type of concat() input");
5326 
5327  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5328  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5329  }
5330 
5331  fcinfo->flinfo->fn_extra = foutcache;
5332 
5333  return foutcache;
5334 }
Definition: fmgr.h:56
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2784
MemoryContext fn_mcxt
Definition: fmgr.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1804
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
FmgrInfo * flinfo
Definition: fmgr.h:87
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:203
void * fn_extra
Definition: fmgr.h:64
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:214
int i

◆ bytea_catenate()

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

Definition at line 3217 of file varlena.c.

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

Referenced by bytea_overlay(), and byteacat().

3218 {
3219  bytea *result;
3220  int len1,
3221  len2,
3222  len;
3223  char *ptr;
3224 
3225  len1 = VARSIZE_ANY_EXHDR(t1);
3226  len2 = VARSIZE_ANY_EXHDR(t2);
3227 
3228  /* paranoia ... probably should throw error instead? */
3229  if (len1 < 0)
3230  len1 = 0;
3231  if (len2 < 0)
3232  len2 = 0;
3233 
3234  len = len1 + len2 + VARHDRSZ;
3235  result = (bytea *) palloc(len);
3236 
3237  /* Set size of result string... */
3238  SET_VARSIZE(result, len);
3239 
3240  /* Fill data field of result string... */
3241  ptr = VARDATA(result);
3242  if (len1 > 0)
3243  memcpy(ptr, VARDATA_ANY(t1), len1);
3244  if (len2 > 0)
3245  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
3246 
3247  return result;
3248 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:569
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:563
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ bytea_overlay()

static bytea * bytea_overlay ( bytea t1,
bytea t2,
int  sp,
int  sl 
)
static

Definition at line 3374 of file varlena.c.

References bytea_catenate(), bytea_substring(), ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), PointerGetDatum, s1, and s2.

Referenced by byteaoverlay(), and byteaoverlay_no_len().

3375 {
3376  bytea *result;
3377  bytea *s1;
3378  bytea *s2;
3379  int sp_pl_sl;
3380 
3381  /*
3382  * Check for possible integer-overflow cases. For negative sp, throw a
3383  * "substring length" error because that's what should be expected
3384  * according to the spec's definition of OVERLAY().
3385  */
3386  if (sp <= 0)
3387  ereport(ERROR,
3388  (errcode(ERRCODE_SUBSTRING_ERROR),
3389  errmsg("negative substring length not allowed")));
3390  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3391  ereport(ERROR,
3392  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3393  errmsg("integer out of range")));
3394 
3395  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3396  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3397  result = bytea_catenate(s1, t2);
3398  result = bytea_catenate(result, s2);
3399 
3400  return result;
3401 }
#define PointerGetDatum(X)
Definition: postgres.h:556
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3217
int errcode(int sqlerrcode)
Definition: elog.c:610
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3292
#define ERROR
Definition: elog.h:43
char * s1
char * s2
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563

◆ bytea_sortsupport()

Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 4205 of file varlena.c.

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

4206 {
4208  MemoryContext oldcontext;
4209 
4210  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
4211 
4212  /* Use generic string SortSupport, forcing "C" collation */
4213  varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
4214 
4215  MemoryContextSwitchTo(oldcontext);
4216 
4217  PG_RETURN_VOID();
4218 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1995
#define PG_RETURN_VOID()
Definition: fmgr.h:348

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 532 of file varlena.c.

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

533 {
535 
536  /* cannot be called directly because of internal-type argument */
537  Assert(AggCheckCallContext(fcinfo, NULL));
538 
539  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
540 
541  if (state != NULL)
542  {
543  bytea *result;
544 
545  result = (bytea *) palloc(state->len + VARHDRSZ);
546  SET_VARSIZE(result, state->len + VARHDRSZ);
547  memcpy(VARDATA(result), state->data, state->len);
548  PG_RETURN_BYTEA_P(result);
549  }
550  else
551  PG_RETURN_NULL();
552 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:569
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:746
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4778
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:563
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 500 of file varlena.c.

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

501 {
503 
504  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
505 
506  /* Append the value unless null. */
507  if (!PG_ARGISNULL(1))
508  {
510 
511  /* On the first time through, we ignore the delimiter. */
512  if (state == NULL)
513  state = makeStringAggState(fcinfo);
514  else if (!PG_ARGISNULL(2))
515  {
516  bytea *delim = PG_GETARG_BYTEA_PP(2);
517 
519  }
520 
522  }
523 
524  /*
525  * The transition type for string_agg() is declared to be "internal",
526  * which is a pass-by-value type the same size as a pointer.
527  */
528  PG_RETURN_POINTER(state);
529 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
ts_parserstate state
Definition: tsquery.c:81
static struct @143 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5236
Definition: c.h:563
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 3269 of file varlena.c.

References bytea_substring(), PG_GETARG_DATUM, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

3270 {
3272  PG_GETARG_INT32(1),
3273  PG_GETARG_INT32(2),
3274  false));
3275 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3292

◆ bytea_substr_no_len()

Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3283 of file varlena.c.

References bytea_substring(), PG_GETARG_DATUM, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

3284 {
3286  PG_GETARG_INT32(1),
3287  -1,
3288  true));
3289 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3292

◆ bytea_substring()

static bytea * bytea_substring ( Datum  str,
int  S,
int  L,
bool  length_not_specified 
)
static

Definition at line 3292 of file varlena.c.

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

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

3296 {
3297  int S1; /* adjusted start position */
3298  int L1; /* adjusted substring length */
3299 
3300  S1 = Max(S, 1);
3301 
3302  if (length_not_specified)
3303  {
3304  /*
3305  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3306  * end of the string if we pass it a negative value for length.
3307  */
3308  L1 = -1;
3309  }
3310  else
3311  {
3312  /* end position */
3313  int E = S + L;
3314 
3315  /*
3316  * A negative value for L is the only way for the end position to be
3317  * before the start. SQL99 says to throw an error.
3318  */
3319  if (E < S)
3320  ereport(ERROR,
3321  (errcode(ERRCODE_SUBSTRING_ERROR),
3322  errmsg("negative substring length not allowed")));
3323 
3324  /*
3325  * A zero or negative value for the end position can happen if the
3326  * start was negative or one. SQL99 says to return a zero-length
3327  * string.
3328  */
3329  if (E < 1)
3330  return PG_STR_GET_BYTEA("");
3331 
3332  L1 = E - S1;
3333  }
3334 
3335  /*
3336  * If the start position is past the end of the string, SQL99 says to
3337  * return a zero-length string -- DatumGetByteaPSlice() will do that for
3338  * us. Convert to zero-based starting position
3339  */
3340  return DatumGetByteaPSlice(str, S1 - 1, L1);
3341 }
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:302
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:3250
#define ERROR
Definition: elog.h:43
#define S(n, x)
Definition: sha1.c:55
#define ereport(elevel,...)
Definition: elog.h:144
#define Max(x, y)
Definition: c.h:922
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 3202 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

3203 {
3204  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3205  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3206 
3208 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3217
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
Definition: c.h:563

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 4183 of file varlena.c.

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

Referenced by gbt_bitcmp(), gbt_byteacmp(), and leftmostvalue_char().

4184 {
4185  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4186  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4187  int len1,
4188  len2;
4189  int cmp;
4190 
4191  len1 = VARSIZE_ANY_EXHDR(arg1);
4192  len2 = VARSIZE_ANY_EXHDR(arg2);
4193 
4194  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4195  if ((cmp == 0) && (len1 != len2))
4196  cmp = (len1 < len2) ? -1 : 1;
4197 
4198  PG_FREE_IF_COPY(arg1, 0);
4199  PG_FREE_IF_COPY(arg2, 1);
4200 
4201  PG_RETURN_INT32(cmp);
4202 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteaeq()

Datum byteaeq ( PG_FUNCTION_ARGS  )

Definition at line 4039 of file varlena.c.

References DatumGetByteaPP, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

Referenced by gbt_byteaeq().

4040 {
4041  Datum arg1 = PG_GETARG_DATUM(0);
4042  Datum arg2 = PG_GETARG_DATUM(1);
4043  bool result;
4044  Size len1,
4045  len2;
4046 
4047  /*
4048  * We can use a fast path for unequal lengths, which might save us from
4049  * having to detoast one or both values.
4050  */
4051  len1 = toast_raw_datum_size(arg1);
4052  len2 = toast_raw_datum_size(arg2);
4053  if (len1 != len2)
4054  result = false;
4055  else
4056  {
4057  bytea *barg1 = DatumGetByteaPP(arg1);
4058  bytea *barg2 = DatumGetByteaPP(arg2);
4059 
4060  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4061  len1 - VARHDRSZ) == 0);
4062 
4063  PG_FREE_IF_COPY(barg1, 0);
4064  PG_FREE_IF_COPY(barg2, 1);
4065  }
4066 
4067  PG_RETURN_BOOL(result);
4068 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:569
#define DatumGetByteaPP(X)
Definition: fmgr.h:290
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:502
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:474
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ byteage()

Datum byteage ( PG_FUNCTION_ARGS  )

Definition at line 4163 of file varlena.c.

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

Referenced by gbt_byteage().

4164 {
4165  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4166  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4167  int len1,
4168  len2;
4169  int cmp;
4170 
4171  len1 = VARSIZE_ANY_EXHDR(arg1);
4172  len2 = VARSIZE_ANY_EXHDR(arg2);
4173 
4174  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4175 
4176  PG_FREE_IF_COPY(arg1, 0);
4177  PG_FREE_IF_COPY(arg2, 1);
4178 
4179  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
4180 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteaGetBit()

Datum byteaGetBit ( PG_FUNCTION_ARGS  )

Definition at line 3483 of file varlena.c.

References byte, ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT64, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3484 {
3485  bytea *v = PG_GETARG_BYTEA_PP(0);
3486  int64 n = PG_GETARG_INT64(1);
3487  int byteNo,
3488  bitNo;
3489  int len;
3490  int byte;
3491 
3492  len = VARSIZE_ANY_EXHDR(v);
3493 
3494  if (n < 0 || n >= (int64) len * 8)
3495  ereport(ERROR,
3496  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3497  errmsg("index %lld out of valid range, 0..%lld",
3498  (long long) n, (long long) len * 8 - 1)));
3499 
3500  /* n/8 is now known < len, so safe to cast to int */
3501  byteNo = (int) (n / 8);
3502  bitNo = (int) (n % 8);
3503 
3504  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3505 
3506  if (byte & (1 << bitNo))
3507  PG_RETURN_INT32(1);
3508  else
3509  PG_RETURN_INT32(0);
3510 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define byte(x, n)
Definition: rijndael.c:68
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282

◆ byteaGetByte()

Datum byteaGetByte ( PG_FUNCTION_ARGS  )

Definition at line 3454 of file varlena.c.

References byte, ereport, errcode(), errmsg(), ERROR, PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3455 {
3456  bytea *v = PG_GETARG_BYTEA_PP(0);
3457  int32 n = PG_GETARG_INT32(1);
3458  int len;
3459  int byte;
3460 
3461  len = VARSIZE_ANY_EXHDR(v);
3462 
3463  if (n < 0 || n >= len)
3464  ereport(ERROR,
3465  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3466  errmsg("index %d out of valid range, 0..%d",
3467  n, len - 1)));
3468 
3469  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3470 
3471  PG_RETURN_INT32(byte);
3472 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:363
#define ERROR
Definition: elog.h:43
#define byte(x, n)
Definition: rijndael.c:68
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563

◆ byteagt()

Datum byteagt ( PG_FUNCTION_ARGS  )

Definition at line 4143 of file varlena.c.

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

Referenced by gbt_byteagt().

4144 {
4145  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4146  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4147  int len1,
4148  len2;
4149  int cmp;
4150 
4151  len1 = VARSIZE_ANY_EXHDR(arg1);
4152  len2 = VARSIZE_ANY_EXHDR(arg2);
4153 
4154  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4155 
4156  PG_FREE_IF_COPY(arg1, 0);
4157  PG_FREE_IF_COPY(arg2, 1);
4158 
4159  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
4160 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteain()

Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 294 of file varlena.c.

References ereport, errcode(), errmsg(), ERROR, hex_decode(), palloc(), PG_GETARG_CSTRING, PG_RETURN_BYTEA_P, SET_VARSIZE, VAL, VARDATA, and VARHDRSZ.

Referenced by CreateTrigger(), and string_to_datum().

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

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

Definition at line 4123 of file varlena.c.

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

Referenced by gbt_byteale().

4124 {
4125  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4126  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4127  int len1,
4128  len2;
4129  int cmp;
4130 
4131  len1 = VARSIZE_ANY_EXHDR(arg1);
4132  len2 = VARSIZE_ANY_EXHDR(arg2);
4133 
4134  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4135 
4136  PG_FREE_IF_COPY(arg1, 0);
4137  PG_FREE_IF_COPY(arg2, 1);
4138 
4139  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
4140 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bytealt()

Datum bytealt ( PG_FUNCTION_ARGS  )

Definition at line 4103 of file varlena.c.

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

Referenced by gbt_bytealt().

4104 {
4105  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4106  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4107  int len1,
4108  len2;
4109  int cmp;
4110 
4111  len1 = VARSIZE_ANY_EXHDR(arg1);
4112  len2 = VARSIZE_ANY_EXHDR(arg2);
4113 
4114  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4115 
4116  PG_FREE_IF_COPY(arg1, 0);
4117  PG_FREE_IF_COPY(arg2, 1);
4118 
4119  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
4120 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ byteane()

Datum byteane ( PG_FUNCTION_ARGS  )

Definition at line 4071 of file varlena.c.

References DatumGetByteaPP, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_RETURN_BOOL, toast_raw_datum_size(), VARDATA_ANY, and VARHDRSZ.

4072 {
4073  Datum arg1 = PG_GETARG_DATUM(0);
4074  Datum arg2 = PG_GETARG_DATUM(1);
4075  bool result;
4076  Size len1,
4077  len2;
4078 
4079  /*
4080  * We can use a fast path for unequal lengths, which might save us from
4081  * having to detoast one or both values.
4082  */
4083  len1 = toast_raw_datum_size(arg1);
4084  len2 = toast_raw_datum_size(arg2);
4085  if (len1 != len2)
4086  result = true;
4087  else
4088  {
4089  bytea *barg1 = DatumGetByteaPP(arg1);
4090  bytea *barg2 = DatumGetByteaPP(arg2);
4091 
4092  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4093  len1 - VARHDRSZ) != 0);
4094 
4095  PG_FREE_IF_COPY(barg1, 0);
4096  PG_FREE_IF_COPY(barg2, 1);
4097  }
4098 
4099  PG_RETURN_BOOL(result);
4100 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:569
#define DatumGetByteaPP(X)
Definition: fmgr.h:290
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:502
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:474
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ byteaoctetlen()

Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 3186 of file varlena.c.

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

3187 {
3188  Datum str = PG_GETARG_DATUM(0);
3189 
3190  /* We need not detoast the input at all */
3192 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:569
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:502
uintptr_t Datum
Definition: postgres.h:367

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 390 of file varlena.c.

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

Referenced by pg_mcv_list_out().

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

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 3351 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, and PG_RETURN_BYTEA_P.

3352 {
3353  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3354  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3355  int sp = PG_GETARG_INT32(2); /* substring start position */
3356  int sl = PG_GETARG_INT32(3); /* substring length */
3357 
3358  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3359 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3374
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
Definition: c.h:563

◆ byteaoverlay_no_len()

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3362 of file varlena.c.

References bytea_overlay(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_BYTEA_P, and VARSIZE_ANY_EXHDR.

3363 {
3364  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3365  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3366  int sp = PG_GETARG_INT32(2); /* substring start position */
3367  int sl;
3368 
3369  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3370  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3371 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3374
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563

◆ byteapos()

Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 3410 of file varlena.c.

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

3411 {
3412  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3413  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3414  int pos;
3415  int px,
3416  p;
3417  int len1,
3418  len2;
3419  char *p1,
3420  *p2;
3421 
3422  len1 = VARSIZE_ANY_EXHDR(t1);
3423  len2 = VARSIZE_ANY_EXHDR(t2);
3424 
3425  if (len2 <= 0)
3426  PG_RETURN_INT32(1); /* result for empty pattern */
3427 
3428  p1 = VARDATA_ANY(t1);
3429  p2 = VARDATA_ANY(t2);
3430 
3431  pos = 0;
3432  px = (len1 - len2);
3433  for (p = 0; p <= px; p++)
3434  {
3435  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3436  {
3437  pos = p + 1;
3438  break;
3439  };
3440  p1++;
3441  };
3442 
3443  PG_RETURN_INT32(pos);
3444 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:307
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

Definition at line 473 of file varlena.c.

References buf, StringInfoData::cursor, StringInfoData::len, palloc(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_copymsgbytes(), SET_VARSIZE, VARDATA, and VARHDRSZ.

474 {
476  bytea *result;
477  int nbytes;
478 
479  nbytes = buf->len - buf->cursor;
480  result = (bytea *) palloc(nbytes + VARHDRSZ);
481  SET_VARSIZE(result, nbytes + VARHDRSZ);
482  pq_copymsgbytes(buf, VARDATA(result), nbytes);
483  PG_RETURN_BYTEA_P(result);
484 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:569
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
static char * buf
Definition: pg_test_fsync.c:68
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:563
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 492 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

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

493 {
494  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
495 
496  PG_RETURN_BYTEA_P(vlena);
497 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:313
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
Definition: c.h:563

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3553 of file varlena.c.

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

3554 {
3555  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3556  int64 n = PG_GETARG_INT64(1);
3557  int32 newBit = PG_GETARG_INT32(2);
3558  int len;
3559  int oldByte,
3560  newByte;
3561  int byteNo,
3562  bitNo;
3563 
3564  len = VARSIZE(res) - VARHDRSZ;
3565 
3566  if (n < 0 || n >= (int64) len * 8)
3567  ereport(ERROR,
3568  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3569  errmsg("index %lld out of valid range, 0..%lld",
3570  (long long) n, (long long) len * 8 - 1)));
3571 
3572  /* n/8 is now known < len, so safe to cast to int */
3573  byteNo = (int) (n / 8);
3574  bitNo = (int) (n % 8);
3575 
3576  /*
3577  * sanity check!
3578  */
3579  if (newBit != 0 && newBit != 1)
3580  ereport(ERROR,
3581  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3582  errmsg("new bit must be 0 or 1")));
3583 
3584  /*
3585  * Update the byte.
3586  */
3587  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3588 
3589  if (newBit == 0)
3590  newByte = oldByte & (~(1 << bitNo));
3591  else
3592  newByte = oldByte | (1 << bitNo);
3593 
3594  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3595 
3596  PG_RETURN_BYTEA_P(res);
3597 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:569
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:313
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
signed int int32
Definition: c.h:363
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282

◆ byteaSetByte()

Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 3521 of file varlena.c.

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

3522 {
3523  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3524  int32 n = PG_GETARG_INT32(1);
3525  int32 newByte = PG_GETARG_INT32(2);
3526  int len;
3527 
3528  len = VARSIZE(res) - VARHDRSZ;
3529 
3530  if (n < 0 || n >= len)
3531  ereport(ERROR,
3532  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3533  errmsg("index %d out of valid range, 0..%d",
3534  n, len - 1)));
3535 
3536  /*
3537  * Now set the byte.
3538  */
3539  ((unsigned char *) VARDATA(res))[n] = newByte;
3540 
3541  PG_RETURN_BYTEA_P(res);
3542 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:569
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:313
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
signed int int32
Definition: c.h:363
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563

◆ charlen_to_bytelen()

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

Definition at line 785 of file varlena.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

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

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

Definition at line 1481 of file varlena.c.

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().

1482 {
1483  if (!OidIsValid(collid))
1484  {
1485  /*
1486  * This typically means that the parser could not resolve a conflict
1487  * of implicit collations, so report it that way.
1488  */
1489  ereport(ERROR,
1490  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1491  errmsg("could not determine which collation to use for string comparison"),
1492  errhint("Use the COLLATE clause to set the collation explicitly.")));
1493  }
1494 }
int errhint(const char *fmt,...)
Definition: elog.c:1068
int errcode(int sqlerrcode)
Definition: elog.c:610
#define OidIsValid(objectId)
Definition: c.h:652
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ check_replace_text_has_escape_char()

static bool check_replace_text_has_escape_char ( const text replace_text)
static

Definition at line 4316 of file varlena.c.

References pg_database_encoding_max_length(), pg_mblen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

4317 {
4318  const char *p = VARDATA_ANY(replace_text);
4319  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4320 
4322  {
4323  for (; p < p_end; p++)
4324  {
4325  if (*p == '\\')
4326  return true;
4327  }
4328  }
4329  else
4330  {
4331  for (; p < p_end; p += pg_mblen(p))
4332  {
4333  if (*p == '\\')
4334  return true;
4335  }
4336  }
4337 
4338  return false;
4339 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1436
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ concat_internal()

static text* concat_internal ( const char *  sepstr,
int  argidx,
FunctionCallInfo  fcinfo 
)
static

Definition at line 5346 of file varlena.c.

References appendStringInfoString(), array_to_text_internal(), Assert, build_concat_foutcache(), cstring_to_text_with_len(), StringInfoData::data, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, initStringInfo(), StringInfoData::len, 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().

5348 {
5349  text *result;
5351  FmgrInfo *foutcache;
5352  bool first_arg = true;
5353  int i;
5354 
5355  /*
5356  * concat(VARIADIC some-array) is essentially equivalent to
5357  * array_to_text(), ie concat the array elements with the given separator.
5358  * So we just pass the case off to that code.
5359  */
5360  if (get_fn_expr_variadic(fcinfo->flinfo))
5361  {
5362  ArrayType *arr;
5363 
5364  /* Should have just the one argument */
5365  Assert(argidx == PG_NARGS() - 1);
5366 
5367  /* concat(VARIADIC NULL) is defined as NULL */
5368  if (PG_ARGISNULL(argidx))
5369  return NULL;
5370 
5371  /*
5372  * Non-null argument had better be an array. We assume that any call
5373  * context that could let get_fn_expr_variadic return true will have
5374  * checked that a VARIADIC-labeled parameter actually is an array. So
5375  * it should be okay to just Assert that it's an array rather than
5376  * doing a full-fledged error check.
5377  */
5379 
5380  /* OK, safe to fetch the array value */
5381  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5382 
5383  /*
5384  * And serialize the array. We tell array_to_text to ignore null
5385  * elements, which matches the behavior of the loop below.
5386  */
5387  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5388  }
5389 
5390  /* Normal case without explicit VARIADIC marker */
5391  initStringInfo(&str);
5392 
5393  /* Get output function info, building it if first time through */
5394  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5395  if (foutcache == NULL)
5396  foutcache = build_concat_foutcache(fcinfo, argidx);
5397 
5398  for (i = argidx; i < PG_NARGS(); i++)
5399  {
5400  if (!PG_ARGISNULL(i))
5401  {
5403 
5404  /* add separator if appropriate */
5405  if (first_arg)
5406  first_arg = false;
5407  else
5408  appendStringInfoString(&str, sepstr);
5409 
5410  /* call the appropriate type output function, append the result */
5412  OutputFunctionCall(&foutcache[i], value));
5413  }
5414  }
5415 
5416  result = cstring_to_text_with_len(str.data, str.len);
5417  pfree(str.data);
5418 
5419  return result;
5420 }
Definition: fmgr.h:56
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1938
#define OidIsValid(objectId)
Definition: c.h:652
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
void pfree(void *pointer)
Definition: mcxt.c:1057
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1804
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5010
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
static struct @143 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:746
#define PG_NARGS()
Definition: fmgr.h:203
void * fn_extra
Definition: fmgr.h:64
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2709
int i
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5308
Definition: c.h:563

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 188 of file varlena.c.

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_page_type(), 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_jsonb_path_all(), get_scalar(), hash_page_type(), hstore_to_json(), hstore_to_json_loose(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_typeof(), JsonbValueAsText(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), pg_collation_actual_version(), pg_collation_for(), pg_crypt(), pg_current_logfile(), pg_describe_object(), pg_export_snapshot(), 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_replication_slots(), 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(), SPI_sql_row_to_xmlelement(), split_part(), ssl_cipher(), ssl_version(), string_to_text(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), unaccent_dict(), upper(), X509_NAME_to_text(), xml_encode_special_chars(), xml_in(), and xml_recv().

189 {
190  return cstring_to_text_with_len(s, strlen(s));
191 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200

◆ cstring_to_text_with_len()

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

Definition at line 200 of file varlena.c.

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

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), build_regexp_match_result(), build_regexp_split_result(), compute_tsvector_stats(), concat_internal(), cstring_to_text(), cstring_to_xmltype(), 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_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(), 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(), varchar(), varchar_input(), and xslt_process().

201 {
202  text *result = (text *) palloc(len + VARHDRSZ);
203 
204  SET_VARSIZE(result, len + VARHDRSZ);
205  memcpy(VARDATA(result), s, len);
206 
207  return result;
208 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:569
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:563
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 3061 of file varlena.c.

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().

3062 {
3063  int result;
3064  int len1,
3065  len2;
3066 
3067  len1 = VARSIZE_ANY_EXHDR(arg1);
3068  len2 = VARSIZE_ANY_EXHDR(arg2);
3069 
3070  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3071  if (result != 0)
3072  return result;
3073  else if (len1 < len2)
3074  return -1;
3075  else if (len1 > len2)
3076  return 1;
3077  else
3078  return 0;
3079 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:928
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5236 of file varlena.c.

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

5237 {
5238  StringInfo state;
5239  MemoryContext aggcontext;
5240  MemoryContext oldcontext;
5241 
5242  if (!AggCheckCallContext(fcinfo, &aggcontext))
5243  {
5244  /* cannot be called directly because of internal-type argument */
5245  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5246  }
5247 
5248  /*
5249  * Create state in aggregate context. It'll stay there across subsequent
5250  * calls.
5251  */
5252  oldcontext = MemoryContextSwitchTo(aggcontext);
5253  state = makeStringInfo();
5254  MemoryContextSwitchTo(oldcontext);
5255 
5256  return state;
5257 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:43
ts_parserstate state
Definition: tsquery.c:81
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4778
#define elog(elevel,...)
Definition: elog.h:214

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3627 of file varlena.c.

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

Referenced by nameiclike(), and nameicnlike().

3628 {
3629  Name s = PG_GETARG_NAME(0);
3630 
3632 }
Definition: c.h:617
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
#define NameStr(name)
Definition: c.h:623
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

Definition at line 2864 of file varlena.c.

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().

2865 {
2866  Name arg1 = PG_GETARG_NAME(0);
2867  text *arg2 = PG_GETARG_TEXT_PP(1);
2868  size_t len1 = strlen(NameStr(*arg1));
2869  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2870  Oid collid = PG_GET_COLLATION();
2871  bool result;
2872 
2873  check_collation_set(collid);
2874 
2875  if (collid == C_COLLATION_OID)
2876  result = (len1 == len2 &&
2877  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2878  else
2879  result = (varstr_cmp(NameStr(*arg1), len1,
2880  VARDATA_ANY(arg2), len2,
2881  collid) == 0);
2882 
2883  PG_FREE_IF_COPY(arg2, 1);
2884 
2885  PG_RETURN_BOOL(result);
2886 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1510
static void check_collation_set(Oid collid)
Definition: varlena.c:1481
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:623
Definition: c.h:563
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ namefastcmp_c()

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

Definition at line 2227 of file varlena.c.

References DatumGetName, NAMEDATALEN, and NameStr.

Referenced by varstr_sortsupport().

2228 {
2229  Name arg1 = DatumGetName(x);
2230  Name arg2 = DatumGetName(y);
2231 
2232  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2233 }
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:585
Definition: c.h:617
#define NameStr(name)
Definition: c.h:623

◆ namefastcmp_locale()

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

Definition at line 2270 of file varlena.c.

References DatumGetName, NameStr, and varstrfastcmp_locale().

Referenced by varstr_sortsupport().

2271 {
2272  Name arg1 = DatumGetName(x);
2273  Name arg2 = DatumGetName(y);
2274 
2275  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2276  NameStr(*arg2), strlen(NameStr(*arg2)),
2277  ssup);
2278 }
#define DatumGetName(X)
Definition: postgres.h:585
Definition: c.h:617
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2284
#define NameStr(name)
Definition: c.h:623

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 3020 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3021 {
3023 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2995
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2964

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 3014 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3015 {
3017 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2995
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2964

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 3008 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3009 {
3011 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2995
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2964

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 3002 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3003 {
3005 }
#define CmpCall(cmpfunc)
Definition: varlena.c:2995
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2964

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2914 of file varlena.c.

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().

2915 {
2916  Name arg1 = PG_GETARG_NAME(0);
2917  text *arg2 = PG_GETARG_TEXT_PP(1);
2918  size_t len1 = strlen(NameStr(*arg1));
2919  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2920  Oid collid = PG_GET_COLLATION();
2921  bool result;
2922 
2923  check_collation_set(collid);
2924 
2925  if (collid == C_COLLATION_OID)
2926  result = !(len1 == len2 &&
2927  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2928  else
2929  result = !(varstr_cmp(NameStr(*arg1), len1,
2930  VARDATA_ANY(arg2), len2,
2931  collid) == 0);
2932 
2933  PG_FREE_IF_COPY(arg2, 1);
2934 
2935  PG_RETURN_BOOL(result);
2936 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1510
static void check_collation_set(Oid collid)
Definition: varlena.c:1481
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:623
Definition: c.h:563
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 5182 of file varlena.c.

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

5183 {
5185  int32 result;
5186  int typlen;
5187 
5188  /* On first call, get the input type's typlen, and save at *fn_extra */
5189  if (fcinfo->flinfo->fn_extra == NULL)
5190  {
5191  /* Lookup the datatype of the supplied argument */
5192  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5193 
5194  typlen = get_typlen(argtypeid);
5195  if (typlen == 0) /* should not happen */
5196  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5197 
5198  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5199  sizeof(int));
5200  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5201  }
5202  else
5203  typlen = *((int *) fcinfo->flinfo->fn_extra);
5204 
5205  if (typlen == -1)
5206  {
5207  /* varlena type, possibly toasted */
5208  result = toast_datum_size(value);
5209  }
5210  else if (typlen == -2)
5211  {
5212  /* cstring */
5213  result = strlen(DatumGetCString(value)) + 1;
5214  }
5215  else
5216  {
5217  /* ordinary fixed-width type */
5218  result = typlen;
5219  }
5220 
5221  PG_RETURN_INT32(result);
5222 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:363
Size toast_datum_size(Datum value)
Definition: detoast.c:558
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:566
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1804
uintptr_t Datum
Definition: postgres.h:367
static struct @143 value
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2085
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:214

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 4241 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoText(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), StringInfoData::data, initStringInfo(), StringInfoData::len, 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 execute_extension_script().

4242 {
4243  text *src_text = PG_GETARG_TEXT_PP(0);
4244  text *from_sub_text = PG_GETARG_TEXT_PP(1);
4245  text *to_sub_text = PG_GETARG_TEXT_PP(2);
4246  int src_text_len;
4247  int from_sub_text_len;
4249  text *ret_text;
4250  int chunk_len;
4251  char *curr_ptr;
4252  char *start_ptr;
4254  bool found;
4255 
4256  src_text_len = VARSIZE_ANY_EXHDR(src_text);
4257  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4258 
4259  /* Return unmodified source string if empty source or pattern */
4260  if (src_text_len < 1 || from_sub_text_len < 1)
4261  {
4262  PG_RETURN_TEXT_P(src_text);
4263  }
4264 
4265  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4266 
4267  found = text_position_next(&state);
4268 
4269  /* When the from_sub_text is not found, there is nothing to do. */
4270  if (!found)
4271  {
4272  text_position_cleanup(&state);
4273  PG_RETURN_TEXT_P(src_text);
4274  }
4275  curr_ptr = text_position_get_match_ptr(&state);
4276  start_ptr = VARDATA_ANY(src_text);
4277 
4278  initStringInfo(&str);
4279 
4280  do
4281  {
4283 
4284  /* copy the data skipped over by last text_position_next() */
4285  chunk_len = curr_ptr - start_ptr;
4286  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4287 
4288  appendStringInfoText(&str, to_sub_text);
4289 
4290  start_ptr = curr_ptr + from_sub_text_len;
4291 
4292  found = text_position_next(&state);
4293  if (found)
4294  curr_ptr = text_position_get_match_ptr(&state);
4295  }
4296  while (found);
4297 
4298  /* copy trailing data */
4299  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4300  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4301 
4302  text_position_cleanup(&state);
4303 
4304  ret_text = cstring_to_text_with_len(str.data, str.len);
4305  pfree(str.data);
4306 
4307  PG_RETURN_TEXT_P(ret_text);
4308 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1313
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1446
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
void pfree(void *pointer)
Definition: mcxt.c:1057
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4227
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
ts_parserstate state
Definition: tsquery.c:81
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1475
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1184
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ replace_text_regexp()

text* replace_text_regexp ( text src_text,
void *  regexp,
text replace_text,
bool  glob 
)

Definition at line 4454 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoRegexpSubstr(), appendStringInfoText(), buf, charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, check_replace_text_has_escape_char(), cstring_to_text_with_len(), StringInfoData::data, ereport, errcode(), errmsg(), ERROR, initStringInfo(), StringInfoData::len, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regerror(), pg_regexec(), REG_NOMATCH, REG_OKAY, REGEXP_REPLACE_BACKREF_CNT, regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by textregexreplace(), and textregexreplace_noopt().

4456 {
4457  text *ret_text;
4458  regex_t *re = (regex_t *) regexp;
4459  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4462  pg_wchar *data;
4463  size_t data_len;
4464  int search_start;
4465  int data_pos;
4466  char *start_ptr;
4467  bool have_escape;
4468 
4469  initStringInfo(&buf);
4470 
4471  /* Convert data string to wide characters. */
4472  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4473  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4474 
4475  /* Check whether replace_text has escape char. */
4476  have_escape = check_replace_text_has_escape_char(replace_text);
4477 
4478  /* start_ptr points to the data_pos'th character of src_text */
4479  start_ptr = (char *) VARDATA_ANY(src_text);
4480  data_pos = 0;
4481 
4482  search_start = 0;
4483  while (search_start <= data_len)
4484  {
4485  int regexec_result;
4486 
4488 
4489  regexec_result = pg_regexec(re,
4490  data,
4491  data_len,
4492  search_start,
4493  NULL, /* no details */
4495  pmatch,
4496  0);
4497 
4498  if (regexec_result == REG_NOMATCH)
4499  break;
4500 
4501  if (regexec_result != REG_OKAY)
4502  {
4503  char errMsg[100];
4504 
4506  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4507  ereport(ERROR,
4508  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4509  errmsg("regular expression failed: %s", errMsg)));
4510  }
4511 
4512  /*
4513  * Copy the text to the left of the match position. Note we are given
4514  * character not byte indexes.
4515  */
4516  if (pmatch[0].rm_so - data_pos > 0)
4517  {
4518  int chunk_len;
4519 
4520  chunk_len = charlen_to_bytelen(start_ptr,
4521  pmatch[0].rm_so - data_pos);
4522  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4523 
4524  /*
4525  * Advance start_ptr over that text, to avoid multiple rescans of
4526  * it if the replace_text contains multiple back-references.
4527  */
4528  start_ptr += chunk_len;
4529  data_pos = pmatch[0].rm_so;
4530  }
4531 
4532  /*
4533  * Copy the replace_text. Process back references when the
4534  * replace_text has escape characters.
4535  */
4536  if (have_escape)
4537  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4538  start_ptr, data_pos);
4539  else
4540  appendStringInfoText(&buf, replace_text);
4541 
4542  /* Advance start_ptr and data_pos over the matched text. */
4543  start_ptr += charlen_to_bytelen(start_ptr,
4544  pmatch[0].rm_eo - data_pos);
4545  data_pos = pmatch[0].rm_eo;
4546 
4547  /*
4548  * When global option is off, replace the first instance only.
4549  */
4550  if (!glob)
4551  break;
4552 
4553  /*
4554  * Advance search position. Normally we start the next search at the
4555  * end of the previous match; but if the match was of zero length, we
4556  * have to advance by one character, or we'd just find the same match
4557  * again.
4558  */
4559  search_start = data_pos;
4560  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4561  search_start++;
4562  }
4563 
4564  /*
4565  * Copy the text to the right of the last match.
4566  */
4567  if (data_pos < data_len)
4568  {
4569  int chunk_len;
4570 
4571  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4572  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4573  }
4574 
4575  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4576  pfree(buf.data);
4577  pfree(data);
4578 
4579  return ret_text;
4580 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
int errcode(int sqlerrcode)
Definition: elog.c:610
regoff_t rm_eo
Definition: regex.h:86
void pfree(void *pointer)
Definition: mcxt.c:1057
#define REG_OKAY
Definition: regex.h:137
#define ERROR
Definition: elog.h:43
static bool check_replace_text_has_escape_char(const text *replace_text)
Definition: varlena.c:4316
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:785
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4227
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
unsigned int pg_wchar
Definition: mbprint.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define REGEXP_REPLACE_BACKREF_CNT
Definition: varlena.c:4443
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:870
#define ereport(elevel,...)
Definition: elog.h:144
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:4349
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:172
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define REG_NOMATCH
Definition: regex.h:138
Definition: c.h:563
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
Definition: regex.h:55

◆ rest_of_char_same()

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

Definition at line 6076 of file varlena.c.

Referenced by varstr_levenshtein().

6077 {
6078  while (len > 0)
6079  {
6080  len--;
6081  if (s1[len] != s2[len])
6082  return false;
6083  }
6084  return true;
6085 }
char * s1
char * s2

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4589 of file varlena.c.

References 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_setup(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4590 {
4591  text *inputstring = PG_GETARG_TEXT_PP(0);
4592  text *fldsep = PG_GETARG_TEXT_PP(1);
4593  int fldnum = PG_GETARG_INT32(2);
4594  int inputstring_len;
4595  int fldsep_len;
4597  char *start_ptr;
4598  char *end_ptr;
4599  text *result_text;
4600  bool found;
4601 
4602  /* field number is 1 based */
4603  if (fldnum < 1)
4604  ereport(ERROR,
4605  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4606  errmsg("field position must be greater than zero")));
4607 
4608  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4609  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4610 
4611  /* return empty string for empty input string */
4612  if (inputstring_len < 1)
4614 
4615  /* empty field separator */
4616  if (fldsep_len < 1)
4617  {
4618  /* if first field, return input string, else empty string */
4619  if (fldnum == 1)
4620  PG_RETURN_TEXT_P(inputstring);
4621  else
4623  }
4624 
4625  text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4626 
4627  /* identify bounds of first field */
4628  start_ptr = VARDATA_ANY(inputstring);
4629  found = text_position_next(&state);
4630 
4631  /* special case if fldsep not found at all */
4632  if (!found)
4633  {
4634  text_position_cleanup(&state);
4635  /* if field 1 requested, return input string, else empty string */
4636  if (fldnum == 1)
4637  PG_RETURN_TEXT_P(inputstring);
4638  else
4640  }
4641  end_ptr = text_position_get_match_ptr(&state);
4642 
4643  while (found && --fldnum > 0)
4644  {
4645  /* identify bounds of next field */
4646  start_ptr = end_ptr + fldsep_len;
4647  found = text_position_next(&state);
4648  if (found)
4649  end_ptr = text_position_get_match_ptr(&state);
4650  }
4651 
4652  text_position_cleanup(&state);
4653 
4654  if (fldnum > 0)
4655  {
4656  /* N'th field separator not found */
4657  /* if last field requested, return it, else empty string */
4658  if (fldnum == 1)
4659  {
4660  int last_len = start_ptr - VARDATA_ANY(inputstring);
4661 
4662  result_text = cstring_to_text_with_len(start_ptr,
4663  inputstring_len - last_len);
4664  }
4665  else
4666  result_text = cstring_to_text("");
4667  }
4668  else
4669  {
4670  /* non-last field requested */
4671  result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4672  }
4673 
4674  PG_RETURN_TEXT_P(result_text);
4675 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:610
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1313
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1446
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define ERROR
Definition: elog.h:43
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
ts_parserstate state
Definition: tsquery.c:81
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1475
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1184
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

Definition at line 4793 of file varlena.c.

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().

4794 {
4795  text *inputstring;
4796  text *fldsep;
4797  text *null_string;
4798  Oid collation = PG_GET_COLLATION();
4799  int inputstring_len;
4800  int fldsep_len;
4801  char *start_ptr;
4802  text *result_text;
4803 
4804  /* when input string is NULL, then result is NULL too */
4805  if (PG_ARGISNULL(0))
4806  return false;
4807 
4808  inputstring = PG_GETARG_TEXT_PP(0);
4809 
4810  /* fldsep can be NULL */
4811  if (!PG_ARGISNULL(1))
4812  fldsep = PG_GETARG_TEXT_PP(1);
4813  else
4814  fldsep = NULL;
4815 
4816  /* null_string can be NULL or omitted */
4817  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4818  null_string = PG_GETARG_TEXT_PP(2);
4819  else
4820  null_string = NULL;
4821 
4822  if (fldsep != NULL)
4823  {
4824  /*
4825  * Normal case with non-null fldsep. Use the text_position machinery
4826  * to search for occurrences of fldsep.
4827  */
4829 
4830  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4831  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4832 
4833  /* return empty set for empty input string */
4834  if (inputstring_len < 1)
4835  return true;
4836 
4837  /* empty field separator: return input string as a one-element set */
4838  if (fldsep_len < 1)
4839  {
4840  split_text_accum_result(tstate, inputstring,
4841  null_string, collation);
4842  return true;
4843  }
4844 
4845  text_position_setup(inputstring, fldsep, collation, &state);
4846 
4847  start_ptr = VARDATA_ANY(inputstring);
4848 
4849  for (;;)
4850  {
4851  bool found;
4852  char *end_ptr;
4853  int chunk_len;
4854 
4856 
4857  found = text_position_next(&state);
4858  if (!found)
4859  {
4860  /* fetch last field */
4861  chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4862  end_ptr = NULL; /* not used, but some compilers complain */
4863  }
4864  else
4865  {
4866  /* fetch non-last field */
4867  end_ptr = text_position_get_match_ptr(&state);
4868  chunk_len = end_ptr - start_ptr;
4869  }
4870 
4871  /* build a temp text datum to pass to split_text_accum_result */
4872  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4873 
4874  /* stash away this field */
4875  split_text_accum_result(tstate, result_text,
4876  null_string, collation);
4877 
4878  pfree(result_text);
4879 
4880  if (!found)
4881  break;
4882 
4883  start_ptr = end_ptr + fldsep_len;
4884  }
4885 
4886  text_position_cleanup(&state);
4887  }
4888  else
4889  {
4890  /*
4891  * When fldsep is NULL, each character in the input string becomes a
4892  * separate element in the result set. The separator is effectively
4893  * the space between characters.
4894  */
4895  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4896 
4897  start_ptr = VARDATA_ANY(inputstring);
4898 
4899  while (inputstring_len > 0)
4900  {
4901  int chunk_len = pg_mblen(start_ptr);
4902 
4904 
4905  /* build a temp text datum to pass to split_text_accum_result */
4906  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4907 
4908  /* stash away this field */
4909  split_text_accum_result(tstate, result_text,
4910  null_string, collation);
4911 
4912  pfree(result_text);
4913 
4914  start_ptr += chunk_len;
4915  inputstring_len -= chunk_len;
4916  }
4917  }
4918 
4919  return true;
4920 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1313
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1446
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
void pfree(void *pointer)
Definition: mcxt.c:1057
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
ts_parserstate state
Definition: tsquery.c:81
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1475
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition: varlena.c:4929
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_NARGS()
Definition: fmgr.h:203
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1184
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ split_text_accum_result()

static void split_text_accum_result ( SplitTextOutputData tstate,
text field_value,
text null_string,
Oid  collation 
)
static

Definition at line 4929 of file varlena.c.

References accumArrayResult(), SplitTextOutputData::astate, CurrentMemoryContext, PointerGetDatum, text_isequal(), SplitTextOutputData::tupdesc, tuplestore_putvalues(), SplitTextOutputData::tupstore, and values.

Referenced by split_text().

4933 {
4934  bool is_null = false;
4935 
4936  if (null_string && text_isequal(field_value, null_string, collation))
4937  is_null = true;
4938 
4939  if (tstate->tupstore)
4940  {
4941  Datum values[1];
4942  bool nulls[1];
4943 
4944  values[0] = PointerGetDatum(field_value);
4945  nulls[0] = is_null;
4946 
4948  tstate->tupdesc,
4949  values,
4950  nulls);
4951  }
4952  else
4953  {
4954  tstate->astate = accumArrayResult(tstate->astate,
4955  PointerGetDatum(field_value),
4956  is_null,
4957  TEXTOID,
4959  }
4960 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define PointerGetDatum(X)
Definition: postgres.h:556
Tuplestorestate * tupstore
Definition: varlena.c:103
TupleDesc tupdesc
Definition: varlena.c:104
static bool text_isequal(text *txt1, text *txt2, Oid collid)
Definition: varlena.c:4681
ArrayBuildState * astate
Definition: varlena.c:102
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5080

◆ SplitDirectoriesString()

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

Definition at line 3829 of file varlena.c.

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

Referenced by load_libraries(), and PostmasterMain().

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

◆ SplitGUCList()

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

Definition at line 3950 of file varlena.c.

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

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

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

◆ SplitIdentifierString()

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

Definition at line 3702 of file varlena.c.

References Assert, downcase_truncate_identifier(), lappend(), 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().

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

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 5286 of file varlena.c.

References AggCheckCallContext(), Assert, cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

5287 {
5288  StringInfo state;
5289 
5290  /* cannot be called directly because of internal-type argument */
5291  Assert(AggCheckCallContext(fcinfo, NULL));
5292 
5293  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5294 
5295  if (state != NULL)
5297  else
5298  PG_RETURN_NULL();
5299 }
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
ts_parserstate state
Definition: tsquery.c:81
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:746
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4778
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5260 of file varlena.c.

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

5261 {
5262  StringInfo state;
5263 
5264  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5265 
5266  /* Append the value unless null. */
5267  if (!PG_ARGISNULL(1))
5268  {
5269  /* On the first time through, we ignore the delimiter. */
5270  if (state == NULL)
5271  state = makeStringAggState(fcinfo);
5272  else if (!PG_ARGISNULL(2))
5273  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
5274 
5275  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
5276  }
5277 
5278  /*
5279  * The transition type for string_agg() is declared to be "internal",
5280  * which is a pass-by-value type the same size as a pointer.
5281  */
5282  PG_RETURN_POINTER(state);
5283 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4227
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5236

◆ text_catenate()

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

Definition at line 744 of file varlena.c.

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

Referenced by text_overlay(), and textcat().

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

◆ text_cmp()

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

Definition at line 1726 of file varlena.c.

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().

1727 {
1728  char *a1p,
1729  *a2p;
1730  int len1,
1731  len2;
1732 
1733  a1p = VARDATA_ANY(arg1);
1734  a2p = VARDATA_ANY(arg2);
1735 
1736  len1 = VARSIZE_ANY_EXHDR(arg1);
1737  len2 = VARSIZE_ANY_EXHDR(arg2);
1738 
1739  return varstr_cmp(a1p, len1, a2p, len2, collid);
1740 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1510
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 5426 of file varlena.c.

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

5427 {
5428  text *result;
5429 
5430  result = concat_internal("", 0, fcinfo);
5431  if (result == NULL)
5432  PG_RETURN_NULL();
5433  PG_RETURN_TEXT_P(result);
5434 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5346
Definition: c.h:563
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5441 of file varlena.c.

References concat_internal(), PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

5442 {
5443  char *sep;
5444  text *result;
5445 
5446  /* return NULL when separator is NULL */
5447  if (PG_ARGISNULL(0))
5448  PG_RETURN_NULL();
5450 
5451  result = concat_internal(sep, 1, fcinfo);
5452  if (result == NULL)
5453  PG_RETURN_NULL();
5454  PG_RETURN_TEXT_P(result);
5455 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5346
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:563
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 5562 of file varlena.c.

References ADVANCE_PARSE_POINTER, appendStringInfoCharMacro, arg, ARR_ELEMTYPE, Assert, cstring_to_text_with_len(), StringInfoData::data, DatumGetInt16, DatumGetInt32, deconstruct_array(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, fmgr_info(), get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), get_typlenbyvalalign(), getTypeOutputInfo(), initStringInfo(), InvalidOid, StringInfoData::len, 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().

5563 {
5564  text *fmt;
5566  const char *cp;
5567  const char *start_ptr;
5568  const char *end_ptr;
5569  text *result;
5570  int arg;
5571  bool funcvariadic;
5572  int nargs;
5573  Datum *elements = NULL;
5574  bool *nulls = NULL;
5575  Oid element_type = InvalidOid;
5576  Oid prev_type = InvalidOid;
5577  Oid prev_width_type = InvalidOid;
5578  FmgrInfo typoutputfinfo;
5579  FmgrInfo typoutputinfo_width;
5580 
5581  /* When format string is null, immediately return null */
5582  if (PG_ARGISNULL(0))
5583  PG_RETURN_NULL();
5584 
5585  /* If argument is marked VARIADIC, expand array into elements */
5586  if (get_fn_expr_variadic(fcinfo->flinfo))
5587  {
5588  ArrayType *arr;
5589  int16 elmlen;
5590  bool elmbyval;
5591  char elmalign;
5592  int nitems;
5593 
5594  /* Should have just the one argument */
5595  Assert(PG_NARGS() == 2);
5596 
5597  /* If argument is NULL, we treat it as zero-length array */
5598  if (PG_ARGISNULL(1))
5599  nitems = 0;
5600  else
5601  {
5602  /*
5603  * Non-null argument had better be an array. We assume that any
5604  * call context that could let get_fn_expr_variadic return true
5605  * will have checked that a VARIADIC-labeled parameter actually is
5606  * an array. So it should be okay to just Assert that it's an
5607  * array rather than doing a full-fledged error check.
5608  */
5610 
5611  /* OK, safe to fetch the array value */
5612  arr = PG_GETARG_ARRAYTYPE_P(1);
5613 
5614  /* Get info about array element type */
5615  element_type = ARR_ELEMTYPE(arr);
5616  get_typlenbyvalalign(element_type,
5617  &elmlen, &elmbyval, &elmalign);
5618 
5619  /* Extract all array elements */
5620  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5621  &elements, &nulls, &nitems);
5622  }
5623 
5624  nargs = nitems + 1;
5625  funcvariadic = true;
5626  }
5627  else
5628  {
5629  /* Non-variadic case, we'll process the arguments individually */
5630  nargs = PG_NARGS();
5631  funcvariadic = false;
5632  }
5633 
5634  /* Setup for main loop. */
5635  fmt = PG_GETARG_TEXT_PP(0);
5636  start_ptr = VARDATA_ANY(fmt);
5637  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5638  initStringInfo(&str);
5639  arg = 1; /* next argument position to print */
5640 
5641  /* Scan format string, looking for conversion specifiers. */
5642  for (cp = start_ptr; cp < end_ptr; cp++)
5643  {
5644  int argpos;
5645  int widthpos;
5646  int flags;
5647  int width;
5648  Datum value;
5649  bool isNull;
5650  Oid typid;
5651 
5652  /*
5653  * If it's not the start of a conversion specifier, just copy it to
5654  * the output buffer.
5655  */
5656  if (*cp != '%')
5657  {
5658  appendStringInfoCharMacro(&str, *cp);
5659  continue;
5660  }
5661 
5662  ADVANCE_PARSE_POINTER(cp, end_ptr);
5663 
5664  /* Easy case: %% outputs a single % */
5665  if (*cp == '%')
5666  {
5667  appendStringInfoCharMacro(&str, *cp);
5668  continue;
5669  }
5670 
5671  /* Parse the optional portions of the format specifier */
5672  cp = text_format_parse_format(cp, end_ptr,
5673  &argpos, &widthpos,
5674  &flags, &width);
5675 
5676  /*
5677  * Next we should see the main conversion specifier. Whether or not
5678  * an argument position was present, it's known that at least one
5679  * character remains in the string at this point. Experience suggests
5680  * that it's worth checking that that character is one of the expected
5681  * ones before we try to fetch arguments, so as to produce the least
5682  * confusing response to a mis-formatted specifier.
5683  */
5684  if (strchr("sIL", *cp) == NULL)
5685  ereport(ERROR,
5686  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5687  errmsg("unrecognized format() type specifier \"%.*s\"",
5688  pg_mblen(cp), cp),
5689  errhint("For a single \"%%\" use \"%%%%\".")));
5690 
5691  /* If indirect width was specified, get its value */
5692  if (widthpos >= 0)
5693  {
5694  /* Collect the specified or next argument position */
5695  if (widthpos > 0)
5696  arg = widthpos;
5697  if (arg >= nargs)
5698  ereport(ERROR,
5699  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5700  errmsg("too few arguments for format()")));
5701 
5702  /* Get the value and type of the selected argument */
5703  if (!funcvariadic)
5704  {
5705  value = PG_GETARG_DATUM(arg);
5706  isNull = PG_ARGISNULL(arg);
5707  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5708  }
5709  else
5710  {
5711  value = elements[arg - 1];
5712  isNull = nulls[arg - 1];
5713  typid = element_type;
5714  }
5715  if (!OidIsValid(typid))
5716  elog(ERROR, "could not determine data type of format() input");
5717 
5718  arg++;
5719 
5720  /* We can treat NULL width the same as zero */
5721  if (isNull)
5722  width = 0;
5723  else if (typid == INT4OID)
5724  width = DatumGetInt32(value);
5725  else if (typid == INT2OID)
5726  width = DatumGetInt16(value);
5727  else
5728  {
5729  /* For less-usual datatypes, convert to text then to int */
5730  char *str;
5731 
5732  if (typid != prev_width_type)
5733  {
5734  Oid typoutputfunc;
5735  bool typIsVarlena;
5736 
5737  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5738  fmgr_info(typoutputfunc, &typoutputinfo_width);
5739  prev_width_type = typid;
5740  }
5741 
5742  str = OutputFunctionCall(&typoutputinfo_width, value);
5743 
5744  /* pg_strtoint32 will complain about bad data or overflow */
5745  width = pg_strtoint32(str);
5746 
5747  pfree(str);
5748  }
5749  }
5750 
5751  /* Collect the specified or next argument position */
5752  if (argpos > 0)
5753  arg = argpos;
5754  if (arg >= nargs)
5755  ereport(ERROR,
5756  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5757  errmsg("too few arguments for format()")));
5758 
5759  /* Get the value and type of the selected argument */
5760  if (!funcvariadic)
5761  {
5762  value = PG_GETARG_DATUM(arg);
5763  isNull = PG_ARGISNULL(arg);
5764  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5765  }
5766  else
5767  {
5768  value = elements[arg - 1];
5769  isNull = nulls[arg - 1];
5770  typid = element_type;
5771  }
5772  if (!OidIsValid(typid))
5773  elog(ERROR, "could not determine data type of format() input");
5774 
5775  arg++;
5776 
5777  /*
5778  * Get the appropriate typOutput function, reusing previous one if
5779  * same type as previous argument. That's particularly useful in the
5780  * variadic-array case, but often saves work even for ordinary calls.
5781  */
5782  if (typid != prev_type)
5783  {
5784  Oid typoutputfunc;
5785  bool typIsVarlena;
5786 
5787  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5788  fmgr_info(typoutputfunc, &typoutputfinfo);
5789  prev_type = typid;
5790  }
5791 
5792  /*
5793  * And now we can format the value.
5794  */
5795  switch (*cp)
5796  {
5797  case 's':
5798  case 'I':
5799  case 'L':
5800  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5801  value, isNull,
5802  flags, width);
5803  break;
5804  default:
5805  /* should not get here, because of previous check */
5806  ereport(ERROR,
5807  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5808  errmsg("unrecognized format() type specifier \"%.*s\"",
5809  pg_mblen(cp), cp),
5810  errhint("For a single \"%%\" use \"%%%%\".")));
5811  break;
5812  }
5813  }
5814 
5815  /* Don't need deconstruct_array results anymore. */
5816  if (elements != NULL)
5817  pfree(elements);
5818  if (nulls != NULL)
5819  pfree(nulls);
5820 
5821  /* Generate results. */
5822  result = cstring_to_text_with_len(str.data, str.len);
5823  pfree(str.data);
5824 
5825  PG_RETURN_TEXT_P(result);
5826 }
signed short int16
Definition: c.h:362
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5549
Definition: fmgr.h:56
int errhint(const char *fmt,...)
Definition: elog.c:1068
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2784
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define DatumGetInt32(X)
Definition: postgres.h:472
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:5965
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2159
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1938
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1804
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
#define DatumGetInt16(X)
Definition: postgres.h:444
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:746
#define PG_NARGS()
Definition: fmgr.h:203
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
int32 pg_strtoint32(const char *s)
Definition: numutils.c:263
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2709
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
void * arg
Definition: c.h:563
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:5888
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ text_format_append_string()

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

Definition at line 6014 of file varlena.c.

References appendStringInfoSpaces(), appendStringInfoString(), ereport, errcode(), errmsg(), ERROR, pg_mbstrlen(), and TEXT_FORMAT_FLAG_MINUS.

Referenced by text_format_string_conversion().

6016 {
6017  bool align_to_left = false;
6018  int len;
6019 
6020  /* fast path for typical easy case */
6021  if (width == 0)
6022  {
6024  return;
6025  }
6026 
6027  if (width < 0)
6028  {
6029  /* Negative width: implicit '-' flag, then take absolute value */
6030  align_to_left = true;
6031  /* -INT_MIN is undefined */
6032  if (width <= INT_MIN)
6033  ereport(ERROR,
6034  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6035  errmsg("number is out of range")));
6036  width = -width;
6037  }
6038  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6039  align_to_left = true;
6040 
6041  len = pg_mbstrlen(str);
6042  if (align_to_left)
6043  {
6044  /* left justify */
6046  if (len < width)
6047  appendStringInfoSpaces(buf, width - len);
6048  }
6049  else
6050  {
6051  /* right justify */
6052  if (len < width)
6053  appendStringInfoSpaces(buf, width - len);
6055  }
6056 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:921
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5547

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 6066 of file varlena.c.

References text_format().

6067 {
6068  return text_format(fcinfo);
6069 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5562

◆ text_format_parse_digits()

static bool text_format_parse_digits ( const char **  ptr,
const char *  end_ptr,
int *  value 
)
static

Definition at line 5839 of file varlena.c.

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

Referenced by text_format_parse_format().

5840 {
5841  bool found = false;
5842  const char *cp = *ptr;
5843  int val = 0;
5844 
5845  while (*cp >= '0' && *cp <= '9')
5846  {
5847  int8 digit = (*cp - '0');
5848 
5849  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5850  unlikely(pg_add_s32_overflow(val, digit, &val)))
5851  ereport(ERROR,
5852  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5853  errmsg("number is out of range")));
5854  ADVANCE_PARSE_POINTER(cp, end_ptr);
5855  found = true;
5856  }
5857 
5858  *ptr = cp;
5859  *value = val;
5860 
5861  return found;
5862 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5549
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
signed char int8
Definition: c.h:361
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define unlikely(x)
Definition: c.h:207
long val
Definition: informix.c:664

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

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, TEXT_FORMAT_FLAG_MINUS, and text_format_parse_digits().

Referenced by text_format().

5891 {
5892  const char *cp = start_ptr;
5893  int n;
5894 
5895  /* set defaults for output parameters */
5896  *argpos = -1;
5897  *widthpos = -1;
5898  *flags = 0;
5899  *width = 0;
5900 
5901  /* try to identify first number */
5902  if (text_format_parse_digits(&cp, end_ptr, &n))
5903  {
5904  if (*cp != '$')
5905  {
5906  /* Must be just a width and a type, so we're done */
5907  *width = n;
5908  return cp;
5909  }
5910  /* The number was argument position */
5911  *argpos = n;
5912  /* Explicit 0 for argument index is immediately refused */
5913  if (n == 0)
5914  ereport(ERROR,
5915  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5916  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5917  ADVANCE_PARSE_POINTER(cp, end_ptr);
5918  }
5919 
5920  /* Handle flags (only minus is supported now) */
5921  while (*cp == '-')
5922  {
5923  *flags |= TEXT_FORMAT_FLAG_MINUS;
5924  ADVANCE_PARSE_POINTER(cp, end_ptr);
5925  }
5926 
5927  if (*cp == '*')
5928  {
5929  /* Handle indirect width */
5930  ADVANCE_PARSE_POINTER(cp, end_ptr);
5931  if (text_format_parse_digits(&cp, end_ptr, &n))
5932  {
5933  /* number in this position must be closed by $ */
5934  if (*cp != '$')
5935  ereport(ERROR,
5936  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5937  errmsg("width argument position must be ended by \"$\"")));
5938  /* The number was width argument position */
5939  *widthpos = n;
5940  /* Explicit 0 for argument index is immediately refused */
5941  if (n == 0)
5942  ereport(ERROR,
5943  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5944  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5945  ADVANCE_PARSE_POINTER(cp, end_ptr);
5946  }
5947  else
5948  *widthpos = 0; /* width's argument position is unspecified */
5949  }
5950  else
5951  {
5952  /* Check for direct width specification */
5953  if (text_format_parse_digits(&cp, end_ptr, &n))
5954  *width = n;
5955  }
5956 
5957  /* cp should now be pointing at type character */
5958  return cp;
5959 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5549
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5839
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5547

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

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

Referenced by text_format().

5969 {
5970  char *str;
5971 
5972  /* Handle NULL arguments before trying to stringify the value. */
5973  if (isNull)
5974  {
5975  if (conversion == 's')
5976  text_format_append_string(buf, "", flags, width);
5977  else if (conversion == 'L')
5978  text_format_append_string(buf, "NULL", flags, width);
5979  else if (conversion == 'I')
5980  ereport(ERROR,
5981  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5982  errmsg("null values cannot be formatted as an SQL identifier")));
5983  return;
5984  }
5985 
5986  /* Stringify. */
5987  str = OutputFunctionCall(typOutputInfo, value);
5988 
5989  /* Escape. */
5990  if (conversion == 'I')
5991  {
5992  /* quote_identifier may or may not allocate a new string. */
5993  text_format_append_string(buf, quote_identifier(str), flags, width);
5994  }
5995  else if (conversion == 'L')
5996  {
5997  char *qstr = quote_literal_cstr(str);
5998 
5999  text_format_append_string(buf, qstr, flags, width);
6000  /* quote_literal_cstr() always allocates a new string */
6001  pfree(qstr);
6002  }
6003  else
6004  text_format_append_string(buf, str, flags, width);
6005 
6006  /* Cleanup. */
6007  pfree(str);
6008 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10709
int errcode(int sqlerrcode)
Definition: elog.c:610
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6014
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1898 of file varlena.c.

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

Referenced by gbt_textge().

1899 {
1900  text *arg1 = PG_GETARG_TEXT_PP(0);
1901  text *arg2 = PG_GETARG_TEXT_PP(1);
1902  bool result;
1903 
1904  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1905 
1906  PG_FREE_IF_COPY(arg1, 0);
1907  PG_FREE_IF_COPY(arg2, 1);
1908 
1909  PG_RETURN_BOOL(result);
1910 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1883 of file varlena.c.

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

Referenced by gbt_textgt().

1884 {
1885  text *arg1 = PG_GETARG_TEXT_PP(0);
1886  text *arg2 = PG_GETARG_TEXT_PP(1);
1887  bool result;
1888 
1889  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1890 
1891  PG_FREE_IF_COPY(arg1, 0);
1892  PG_FREE_IF_COPY(arg2, 1);
1893 
1894  PG_RETURN_BOOL(result);
1895 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_isequal()

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

Definition at line 4681 of file varlena.c.

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

Referenced by split_text_accum_result().

4682 {
4684  collid,
4685  PointerGetDatum(txt1),
4686  PointerGetDatum(txt2)));
4687 }
#define PointerGetDatum(X)
Definition: postgres.h:556
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1751
#define DatumGetBool(X)
Definition: postgres.h:393
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:815

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2835 of file varlena.c.

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

2836 {
2837  text *arg1 = PG_GETARG_TEXT_PP(0);
2838  text *arg2 = PG_GETARG_TEXT_PP(1);
2839  text *result;
2840 
2841  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2842 
2843  PG_RETURN_TEXT_P(result);
2844 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
Definition: c.h:563

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1868 of file varlena.c.

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

Referenced by gbt_textle().

1869 {
1870  text *arg1 = PG_GETARG_TEXT_PP(0);
1871  text *arg2 = PG_GETARG_TEXT_PP(1);
1872  bool result;
1873 
1874  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1875 
1876  PG_FREE_IF_COPY(arg1, 0);
1877  PG_FREE_IF_COPY(arg2, 1);
1878 
1879  PG_RETURN_BOOL(result);
1880 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 5462 of file varlena.c.

References cstring_to_text_with_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.

5463 {
5464  int n = PG_GETARG_INT32(1);
5465 
5466  if (n < 0)
5467  {
5468  text *str = PG_GETARG_TEXT_PP(0);
5469  const char *p = VARDATA_ANY(str);
5470  int len = VARSIZE_ANY_EXHDR(str);
5471  int rlen;
5472 
5473  n = pg_mbstrlen_with_len(p, len) + n;
5474  rlen = pg_mbcharcliplen(p, len, n);
5476  }
5477  else
5479 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1009
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:941
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:864
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 690 of file varlena.c.

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

Referenced by textlen(), and textoverlay_no_len().

691 {
692  /* fastpath when max encoding length is one */
695  else
696  {
697  text *t = DatumGetTextPP(str);
698 
700  VARSIZE_ANY_EXHDR(t)));
701  }
702 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARHDRSZ
Definition: c.h:569
#define DatumGetTextPP(X)
Definition: fmgr.h:291
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:941
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:502
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1436
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:563

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1853 of file varlena.c.

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

Referenced by gbt_textlt().

1854 {
1855  text *arg1 = PG_GETARG_TEXT_PP(0);
1856  text *arg2 = PG_GETARG_TEXT_PP(1);
1857  bool result;
1858 
1859  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1860 
1861  PG_FREE_IF_COPY(arg1, 0);
1862  PG_FREE_IF_COPY(arg2, 1);
1863 
1864  PG_RETURN_BOOL(result);
1865 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1726
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3604 of file varlena.c.

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

3605 {
3606  text *s = PG_GETARG_TEXT_PP(0);
3607  Name result;
3608  int len;
3609 
3610  len = VARSIZE_ANY_EXHDR(s);
3611 
3612  /* Truncate oversize input */
3613  if (len >= NAMEDATALEN)
3614  len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3615 
3616  /* We use palloc0 here to ensure result is zero-padded */
3617  result = (Name) palloc0(NAMEDATALEN);
3618  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3619 
3620  PG_RETURN_NAME(result);
3621 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:967
Definition: c.h:617
void * palloc0(Size size)
Definition: mcxt.c:981
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:623
Definition: c.h:563
NameData * Name
Definition: c.h:621
#define PG_RETURN_NAME(x)
Definition: fmgr.h:362

◆ text_overlay()

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

Definition at line 1084 of file varlena.c.

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

Referenced by textoverlay(), and textoverlay_no_len().

1085 {
1086  text *result;
1087  text *s1;
1088  text *s2;
1089  int sp_pl_sl;
1090 
1091  /*
1092  * Check for possible integer-overflow cases. For negative sp, throw a
1093  * "substring length" error because that's what should be expected
1094  * according to the spec's definition of OVERLAY().
1095  */
1096  if (sp <= 0)
1097  ereport(ERROR,
1098  (errcode(ERRCODE_SUBSTRING_ERROR),
1099  errmsg("negative substring length not allowed")));
1100  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1101  ereport(ERROR,
1102  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1103  errmsg("integer out of range")));
1104 
1105  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1106  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1107  result = text_catenate(s1, t2);
1108  result = text_catenate(result, s2);
1109 
1110  return result;
1111 }
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
char * s1
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:864
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:744
char * s2
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:821
Definition: c.h:563

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 3115 of file varlena.c.

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

3116 {
3117  text *arg1 = PG_GETARG_TEXT_PP(0);
3118  text *arg2 = PG_GETARG_TEXT_PP(1);
3119  int result;
3120 
3121  result = internal_text_pattern_compare(arg1, arg2);
3122 
3123  PG_FREE_IF_COPY(arg1, 0);
3124  PG_FREE_IF_COPY(arg2, 1);
3125 
3126  PG_RETURN_BOOL(result >= 0);
3127 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3061
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_pattern_gt()

Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 3131 of file varlena.c.

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

3132 {
3133  text *arg1 = PG_GETARG_TEXT_PP(0);
3134  text *arg2 = PG_GETARG_TEXT_PP(1);
3135  int result;
3136 
3137  result = internal_text_pattern_compare(arg1, arg2);
3138 
3139  PG_FREE_IF_COPY(arg1, 0);
3140  PG_FREE_IF_COPY(arg2, 1);
3141 
3142  PG_RETURN_BOOL(result > 0);
3143 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3061
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_pattern_le()

Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 3099 of file varlena.c.

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

3100 {
3101  text *arg1 = PG_GETARG_TEXT_PP(0);
3102  text *arg2 = PG_GETARG_TEXT_PP(1);
3103  int result;
3104 
3105  result = internal_text_pattern_compare(arg1, arg2);
3106 
3107  PG_FREE_IF_COPY(arg1, 0);
3108  PG_FREE_IF_COPY(arg2, 1);
3109 
3110  PG_RETURN_BOOL(result <= 0);
3111 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3061
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:563

◆ text_pattern_lt()

Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 3083 of file varlena.c.

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

3084 {