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/hex.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)
 
static void text_position_reset (TextPositionState *state)
 
int varstr_cmp (const char *arg1, int len1, const char *arg2, int len2, Oid collid)
 
Datum texteq (PG_FUNCTION_ARGS)
 
Datum textne (PG_FUNCTION_ARGS)
 
Datum text_lt (PG_FUNCTION_ARGS)
 
Datum text_le (PG_FUNCTION_ARGS)
 
Datum text_gt (PG_FUNCTION_ARGS)
 
Datum text_ge (PG_FUNCTION_ARGS)
 
Datum text_starts_with (PG_FUNCTION_ARGS)
 
Datum bttextcmp (PG_FUNCTION_ARGS)
 
Datum bttextsortsupport (PG_FUNCTION_ARGS)
 
void varstr_sortsupport (SortSupport ssup, Oid typid, Oid collid)
 
Datum btvarstrequalimage (PG_FUNCTION_ARGS)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
Datum nameeqtext (PG_FUNCTION_ARGS)
 
Datum texteqname (PG_FUNCTION_ARGS)
 
Datum namenetext (PG_FUNCTION_ARGS)
 
Datum textnename (PG_FUNCTION_ARGS)
 
Datum btnametextcmp (PG_FUNCTION_ARGS)
 
Datum bttextnamecmp (PG_FUNCTION_ARGS)
 
Datum namelttext (PG_FUNCTION_ARGS)
 
Datum nameletext (PG_FUNCTION_ARGS)
 
Datum namegttext (PG_FUNCTION_ARGS)
 
Datum namegetext (PG_FUNCTION_ARGS)
 
Datum textltname (PG_FUNCTION_ARGS)
 
Datum textlename (PG_FUNCTION_ARGS)
 
Datum textgtname (PG_FUNCTION_ARGS)
 
Datum textgename (PG_FUNCTION_ARGS)
 
static int internal_text_pattern_compare (text *arg1, text *arg2)
 
Datum text_pattern_lt (PG_FUNCTION_ARGS)
 
Datum text_pattern_le (PG_FUNCTION_ARGS)
 
Datum text_pattern_ge (PG_FUNCTION_ARGS)
 
Datum text_pattern_gt (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_sortsupport (PG_FUNCTION_ARGS)
 
Datum byteaoctetlen (PG_FUNCTION_ARGS)
 
Datum byteacat (PG_FUNCTION_ARGS)
 
Datum bytea_substr (PG_FUNCTION_ARGS)
 
Datum bytea_substr_no_len (PG_FUNCTION_ARGS)
 
Datum byteaoverlay (PG_FUNCTION_ARGS)
 
Datum byteaoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum 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:1162
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:915

Definition at line 5627 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:814

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

◆ DatumGetUnknownPCopy

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

Definition at line 115 of file varlena.c.

◆ DatumGetVarStringP

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

Definition at line 120 of file varlena.c.

◆ DatumGetVarStringPP

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

Definition at line 121 of file varlena.c.

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

◆ DIG

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

Definition at line 281 of file varlena.c.

Referenced by byteaout().

◆ HEXBASE

#define HEXBASE   16

Definition at line 5205 of file varlena.c.

Referenced by to_hex32(), and to_hex64().

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 6167 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P

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

Definition at line 116 of file varlena.c.

◆ PG_GETARG_UNKNOWN_P_COPY

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

Definition at line 117 of file varlena.c.

◆ PG_RETURN_UNKNOWN_P

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 118 of file varlena.c.

◆ PG_STR_GET_BYTEA

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

Definition at line 3281 of file varlena.c.

Referenced by bytea_substring().

◆ REGEXP_REPLACE_BACKREF_CNT

#define REGEXP_REPLACE_BACKREF_CNT   10

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

Referenced by text_format_append_string(), and text_format_parse_format().

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 112 of file varlena.c.

Referenced by varstr_cmp(), and varstr_sortsupport().

◆ VAL

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

Definition at line 280 of file varlena.c.

Referenced by byteain().

Typedef Documentation

◆ unknown

typedef struct varlena unknown

Definition at line 46 of file varlena.c.

◆ VarString

typedef struct varlena VarString

Definition at line 47 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 4388 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().

4391 {
4392  const char *p = VARDATA_ANY(replace_text);
4393  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4394  int eml = pg_database_encoding_max_length();
4395 
4396  for (;;)
4397  {
4398  const char *chunk_start = p;
4399  int so;
4400  int eo;
4401 
4402  /* Find next escape char. */
4403  if (eml == 1)
4404  {
4405  for (; p < p_end && *p != '\\'; p++)
4406  /* nothing */ ;
4407  }
4408  else
4409  {
4410  for (; p < p_end && *p != '\\'; p += pg_mblen(p))
4411  /* nothing */ ;
4412  }
4413 
4414  /* Copy the text we just scanned over, if any. */
4415  if (p > chunk_start)
4416  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4417 
4418  /* Done if at end of string, else advance over escape char. */
4419  if (p >= p_end)
4420  break;
4421  p++;
4422 
4423  if (p >= p_end)
4424  {
4425  /* Escape at very end of input. Treat same as unexpected char */
4426  appendStringInfoChar(str, '\\');
4427  break;
4428  }
4429 
4430  if (*p >= '1' && *p <= '9')
4431  {
4432  /* Use the back reference of regexp. */
4433  int idx = *p - '0';
4434 
4435  so = pmatch[idx].rm_so;
4436  eo = pmatch[idx].rm_eo;
4437  p++;
4438  }
4439  else if (*p == '&')
4440  {
4441  /* Use the entire matched string. */
4442  so = pmatch[0].rm_so;
4443  eo = pmatch[0].rm_eo;
4444  p++;
4445  }
4446  else if (*p == '\\')
4447  {
4448  /* \\ means transfer one \ to output. */
4449  appendStringInfoChar(str, '\\');
4450  p++;
4451  continue;
4452  }
4453  else
4454  {
4455  /*
4456  * If escape char is not followed by any expected char, just treat
4457  * it as ordinary data to copy. (XXX would it be better to throw
4458  * an error?)
4459  */
4460  appendStringInfoChar(str, '\\');
4461  continue;
4462  }
4463 
4464  if (so != -1 && eo != -1)
4465  {
4466  /*
4467  * Copy the text that is back reference of regexp. Note so and eo
4468  * are counted in characters not bytes.
4469  */
4470  char *chunk_start;
4471  int chunk_len;
4472 
4473  Assert(so >= data_pos);
4474  chunk_start = start_ptr;
4475  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4476  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4477  appendBinaryStringInfo(str, chunk_start, chunk_len);
4478  }
4479  }
4480 }
#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:792
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define Assert(condition)
Definition: c.h:792
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 4266 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().

4267 {
4269 }
#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 5046 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().

5047 {
5049  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5050 
5051  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
5052 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5088
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
char * text_to_cstring(const text *t)
Definition: varlena.c:222

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

5090 {
5091  text *result;
5092  int nitems,
5093  *dims,
5094  ndims;
5095  Oid element_type;
5096  int typlen;
5097  bool typbyval;
5098  char typalign;
5100  bool printed = false;
5101  char *p;
5102  bits8 *bitmap;
5103  int bitmask;
5104  int i;
5105  ArrayMetaState *my_extra;
5106 
5107  ndims = ARR_NDIM(v);
5108  dims = ARR_DIMS(v);
5109  nitems = ArrayGetNItems(ndims, dims);
5110 
5111  /* if there are no elements, return an empty string */
5112  if (nitems == 0)
5113  return cstring_to_text_with_len("", 0);
5114 
5115  element_type = ARR_ELEMTYPE(v);
5116  initStringInfo(&buf);
5117 
5118  /*
5119  * We arrange to look up info about element type, including its output
5120  * conversion proc, only once per series of calls, assuming the element
5121  * type doesn't change underneath us.
5122  */
5123  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5124  if (my_extra == NULL)
5125  {
5126  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5127  sizeof(ArrayMetaState));
5128  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5129  my_extra->element_type = ~element_type;
5130  }
5131 
5132  if (my_extra->element_type != element_type)
5133  {
5134  /*
5135  * Get info about element type, including its output conversion proc
5136  */
5137  get_type_io_data(element_type, IOFunc_output,
5138  &my_extra->typlen, &my_extra->typbyval,
5139  &my_extra->typalign, &my_extra->typdelim,
5140  &my_extra->typioparam, &my_extra->typiofunc);
5141  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
5142  fcinfo->flinfo->fn_mcxt);
5143  my_extra->element_type = element_type;
5144  }
5145  typlen = my_extra->typlen;
5146  typbyval = my_extra->typbyval;
5147  typalign = my_extra->typalign;
5148 
5149  p = ARR_DATA_PTR(v);
5150  bitmap = ARR_NULLBITMAP(v);
5151  bitmask = 1;
5152 
5153  for (i = 0; i < nitems; i++)
5154  {
5155  Datum itemvalue;
5156  char *value;
5157 
5158  /* Get source element, checking for NULL */
5159  if (bitmap && (*bitmap & bitmask) == 0)
5160  {
5161  /* if null_string is NULL, we just ignore null elements */
5162  if (null_string != NULL)
5163  {
5164  if (printed)
5165  appendStringInfo(&buf, "%s%s", fldsep, null_string);
5166  else
5167  appendStringInfoString(&buf, null_string);
5168  printed = true;
5169  }
5170  }
5171  else
5172  {
5173  itemvalue = fetch_att(p, typbyval, typlen);
5174 
5175  value = OutputFunctionCall(&my_extra->proc, itemvalue);
5176 
5177  if (printed)
5178  appendStringInfo(&buf, "%s%s", fldsep, value);
5179  else
5180  appendStringInfoString(&buf, value);
5181  printed = true;
5182 
5183  p = att_addlength_pointer(p, typlen, p);
5184  p = (char *) att_align_nominal(p, typalign);
5185  }
5186 
5187  /* advance bitmap pointer if any */
5188  if (bitmap)
5189  {
5190  bitmask <<= 1;
5191  if (bitmask == 0x100)
5192  {
5193  bitmap++;
5194  bitmask = 1;
5195  }
5196  }
5197  }
5198 
5199  result = cstring_to_text_with_len(buf.data, buf.len);
5200  pfree(buf.data);
5201 
5202  return result;
5203 }
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
static struct @144 value
unsigned int Oid
Definition: postgres_ext.h:31
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1576
bool typbyval
Definition: array.h:233
void pfree(void *pointer)
Definition: mcxt.c:1057
char typalign
Definition: pg_type.h:176
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ARR_DIMS(a)
Definition: array.h:287
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define ARR_DATA_PTR(a)
Definition: array.h:315
int16 typlen
Definition: array.h:232
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
char typdelim
Definition: array.h:235
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:436
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
Oid typioparam
Definition: array.h:236
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:283
Oid typiofunc
Definition: array.h:237
char typalign
Definition: array.h:234
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:75
FmgrInfo proc
Definition: array.h:238
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
int i
Oid element_type
Definition: array.h:231
Definition: c.h:609
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_NULLBITMAP(a)
Definition: array.h:293
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:2245

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 5062 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().

5063 {
5064  ArrayType *v;
5065  char *fldsep;
5066  char *null_string;
5067 
5068  /* returns NULL when first or second parameter is NULL */
5069  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
5070  PG_RETURN_NULL();
5071 
5072  v = PG_GETARG_ARRAYTYPE_P(0);
5073  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5074 
5075  /* NULL null string is passed through as a null pointer */
5076  if (!PG_ARGISNULL(2))
5077  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
5078  else
5079  null_string = NULL;
5080 
5081  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
5082 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5088
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
char * text_to_cstring(const text *t)
Definition: varlena.c:222
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ bpcharfastcmp_c()

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

Definition at line 2225 of file varlena.c.

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

Referenced by varstr_sortsupport().

2226 {
2227  BpChar *arg1 = DatumGetBpCharPP(x);
2228  BpChar *arg2 = DatumGetBpCharPP(y);
2229  char *a1p,
2230  *a2p;
2231  int len1,
2232  len2,
2233  result;
2234 
2235  a1p = VARDATA_ANY(arg1);
2236  a2p = VARDATA_ANY(arg2);
2237 
2238  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2239  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2240 
2241  result = memcmp(a1p, a2p, Min(len1, len2));
2242  if ((result == 0) && (len1 != len2))
2243  result = (len1 < len2) ? -1 : 1;
2244 
2245  /* We can't afford to leak memory here. */
2246  if (PointerGetDatum(arg1) != x)
2247  pfree(arg1);
2248  if (PointerGetDatum(arg2) != y)
2249  pfree(arg2);
2250 
2251  return result;
2252 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PointerGetDatum(X)
Definition: postgres.h:556
#define Min(x, y)
Definition: c.h:974
void pfree(void *pointer)
Definition: mcxt.c:1057
int bpchartruelen(char *s, int len)
Definition: varchar.c:671
#define DatumGetBpCharPP(X)
Definition: fmgr.h:293
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

Definition at line 2995 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().

2996 {
2997  Name arg1 = PG_GETARG_NAME(0);
2998  text *arg2 = PG_GETARG_TEXT_PP(1);
2999  int32 result;
3000 
3001  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
3002  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
3003  PG_GET_COLLATION());
3004 
3005  PG_FREE_IF_COPY(arg2, 1);
3006 
3007  PG_RETURN_INT32(result);
3008 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:417
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1541
Definition: c.h:663
#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:669
Definition: c.h:609
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 3178 of file varlena.c.

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

3179 {
3180  text *arg1 = PG_GETARG_TEXT_PP(0);
3181  text *arg2 = PG_GETARG_TEXT_PP(1);
3182  int result;
3183 
3184  result = internal_text_pattern_compare(arg1, arg2);
3185 
3186  PG_FREE_IF_COPY(arg1, 0);
3187  PG_FREE_IF_COPY(arg2, 1);
3188 
3189  PG_RETURN_INT32(result);
3190 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:3092
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3194 of file varlena.c.

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

3195 {
3197  MemoryContext oldcontext;
3198 
3199  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3200 
3201  /* Use generic string SortSupport, forcing "C" collation */
3202  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
3203 
3204  MemoryContextSwitchTo(oldcontext);
3205 
3206  PG_RETURN_VOID();
3207 }
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:2026
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1984 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().

1985 {
1986  text *arg1 = PG_GETARG_TEXT_PP(0);
1987  text *arg2 = PG_GETARG_TEXT_PP(1);
1988  int32 result;
1989 
1990  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1991 
1992  PG_FREE_IF_COPY(arg1, 0);
1993  PG_FREE_IF_COPY(arg2, 1);
1994 
1995  PG_RETURN_INT32(result);
1996 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:417
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1757
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

Definition at line 3011 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().

3012 {
3013  text *arg1 = PG_GETARG_TEXT_PP(0);
3014  Name arg2 = PG_GETARG_NAME(1);
3015  int32 result;
3016 
3017  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
3018  NameStr(*arg2), strlen(NameStr(*arg2)),
3019  PG_GET_COLLATION());
3020 
3021  PG_FREE_IF_COPY(arg1, 0);
3022 
3023  PG_RETURN_INT32(result);
3024 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:417
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1541
Definition: c.h:663
#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:669
Definition: c.h:609
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1999 of file varlena.c.

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

2000 {
2002  Oid collid = ssup->ssup_collation;
2003  MemoryContext oldcontext;
2004 
2005  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2006 
2007  /* Use generic string SortSupport */
2008  varstr_sortsupport(ssup, TEXTOID, collid);
2009 
2010  MemoryContextSwitchTo(oldcontext);
2011 
2012  PG_RETURN_VOID();
2013 }
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:2026
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2850 of file varlena.c.

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

2851 {
2852  /* Oid opcintype = PG_GETARG_OID(0); */
2853  Oid collid = PG_GET_COLLATION();
2854 
2855  check_collation_set(collid);
2856 
2857  if (lc_collate_is_c(collid) ||
2858  collid == DEFAULT_COLLATION_OID ||
2860  PG_RETURN_BOOL(true);
2861  else
2862  PG_RETURN_BOOL(false);
2863 }
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:1326
static void check_collation_set(Oid collid)
Definition: varlena.c:1512
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ build_concat_foutcache()

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

Definition at line 5386 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().

5387 {
5388  FmgrInfo *foutcache;
5389  int i;
5390 
5391  /* We keep the info in fn_mcxt so it survives across calls */
5392  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5393  PG_NARGS() * sizeof(FmgrInfo));
5394 
5395  for (i = argidx; i < PG_NARGS(); i++)
5396  {
5397  Oid valtype;
5398  Oid typOutput;
5399  bool typIsVarlena;
5400 
5401  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5402  if (!OidIsValid(valtype))
5403  elog(ERROR, "could not determine data type of concat() input");
5404 
5405  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5406  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5407  }
5408 
5409  fcinfo->flinfo->fn_extra = foutcache;
5410 
5411  return foutcache;
5412 }
Definition: fmgr.h:56
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2827
MemoryContext fn_mcxt
Definition: fmgr.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:698
#define ERROR
Definition: elog.h:45
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1803
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:228
int i

◆ bytea_catenate()

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

Definition at line 3248 of file varlena.c.

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

Referenced by bytea_overlay(), and byteacat().

3249 {
3250  bytea *result;
3251  int len1,
3252  len2,
3253  len;
3254  char *ptr;
3255 
3256  len1 = VARSIZE_ANY_EXHDR(t1);
3257  len2 = VARSIZE_ANY_EXHDR(t2);
3258 
3259  /* paranoia ... probably should throw error instead? */
3260  if (len1 < 0)
3261  len1 = 0;
3262  if (len2 < 0)
3263  len2 = 0;
3264 
3265  len = len1 + len2 + VARHDRSZ;
3266  result = (bytea *) palloc(len);
3267 
3268  /* Set size of result string... */
3269  SET_VARSIZE(result, len);
3270 
3271  /* Fill data field of result string... */
3272  ptr = VARDATA(result);
3273  if (len1 > 0)
3274  memcpy(ptr, VARDATA_ANY(t1), len1);
3275  if (len2 > 0)
3276  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
3277 
3278  return result;
3279 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:615
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:609
#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 3413 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().

3414 {
3415  bytea *result;
3416  bytea *s1;
3417  bytea *s2;
3418  int sp_pl_sl;
3419 
3420  /*
3421  * Check for possible integer-overflow cases. For negative sp, throw a
3422  * "substring length" error because that's what should be expected
3423  * according to the spec's definition of OVERLAY().
3424  */
3425  if (sp <= 0)
3426  ereport(ERROR,
3427  (errcode(ERRCODE_SUBSTRING_ERROR),
3428  errmsg("negative substring length not allowed")));
3429  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3430  ereport(ERROR,
3431  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3432  errmsg("integer out of range")));
3433 
3434  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3435  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3436  result = bytea_catenate(s1, t2);
3437  result = bytea_catenate(result, s2);
3438 
3439  return result;
3440 }
#define PointerGetDatum(X)
Definition: postgres.h:556
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3248
int errcode(int sqlerrcode)
Definition: elog.c:704
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3323
#define ERROR
Definition: elog.h:45
char * s1
char * s2
#define ereport(elevel,...)
Definition: elog.h:155
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609

◆ bytea_sortsupport()

Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 4244 of file varlena.c.

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

4245 {
4247  MemoryContext oldcontext;
4248 
4249  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
4250 
4251  /* Use generic string SortSupport, forcing "C" collation */
4252  varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
4253 
4254  MemoryContextSwitchTo(oldcontext);
4255 
4256  PG_RETURN_VOID();
4257 }
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:2026
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 539 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.

540 {
542 
543  /* cannot be called directly because of internal-type argument */
544  Assert(AggCheckCallContext(fcinfo, NULL));
545 
546  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
547 
548  if (state != NULL)
549  {
550  bytea *result;
551 
552  result = (bytea *) palloc(state->len + VARHDRSZ);
553  SET_VARSIZE(result, state->len + VARHDRSZ);
554  memcpy(VARDATA(result), state->data, state->len);
555  PG_RETURN_BYTEA_P(result);
556  }
557  else
558  PG_RETURN_NULL();
559 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:615
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:792
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4582
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:609
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 507 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.

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

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 3300 of file varlena.c.

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

3301 {
3303  PG_GETARG_INT32(1),
3304  PG_GETARG_INT32(2),
3305  false));
3306 }
#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:371
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3323

◆ bytea_substr_no_len()

Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3314 of file varlena.c.

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

3315 {
3317  PG_GETARG_INT32(1),
3318  -1,
3319  true));
3320 }
#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:371
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:3323

◆ bytea_substring()

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

Definition at line 3323 of file varlena.c.

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

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

3327 {
3328  int32 S1; /* adjusted start position */
3329  int32 L1; /* adjusted substring length */
3330  int32 E; /* end position */
3331 
3332  /*
3333  * The logic here should generally match text_substring().
3334  */
3335  S1 = Max(S, 1);
3336 
3337  if (length_not_specified)
3338  {
3339  /*
3340  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3341  * end of the string if we pass it a negative value for length.
3342  */
3343  L1 = -1;
3344  }
3345  else if (L < 0)
3346  {
3347  /* SQL99 says to throw an error for E < S, i.e., negative length */
3348  ereport(ERROR,
3349  (errcode(ERRCODE_SUBSTRING_ERROR),
3350  errmsg("negative substring length not allowed")));
3351  L1 = -1; /* silence stupider compilers */
3352  }
3353  else if (pg_add_s32_overflow(S, L, &E))
3354  {
3355  /*
3356  * L could be large enough for S + L to overflow, in which case the
3357  * substring must run to end of string.
3358  */
3359  L1 = -1;
3360  }
3361  else
3362  {
3363  /*
3364  * A zero or negative value for the end position can happen if the
3365  * start was negative or one. SQL99 says to return a zero-length
3366  * string.
3367  */
3368  if (E < 1)
3369  return PG_STR_GET_BYTEA("");
3370 
3371  L1 = E - S1;
3372  }
3373 
3374  /*
3375  * If the start position is past the end of the string, SQL99 says to
3376  * return a zero-length string -- DatumGetByteaPSlice() will do that for
3377  * us. We need only convert S1 to zero-based starting position.
3378  */
3379  return DatumGetByteaPSlice(str, S1 - 1, L1);
3380 }
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:303
int errcode(int sqlerrcode)
Definition: elog.c:704
signed int int32
Definition: c.h:417
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:3281
#define ERROR
Definition: elog.h:45
#define S(n, x)
Definition: sha1.c:55
#define ereport(elevel,...)
Definition: elog.h:155
#define Max(x, y)
Definition: c.h:968
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 3233 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

3234 {
3235  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3236  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3237 
3239 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3248
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:609

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 4222 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().

4223 {
4224  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4225  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4226  int len1,
4227  len2;
4228  int cmp;
4229 
4230  len1 = VARSIZE_ANY_EXHDR(arg1);
4231  len2 = VARSIZE_ANY_EXHDR(arg2);
4232 
4233  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4234  if ((cmp == 0) && (len1 != len2))
4235  cmp = (len1 < len2) ? -1 : 1;
4236 
4237  PG_FREE_IF_COPY(arg1, 0);
4238  PG_FREE_IF_COPY(arg2, 1);
4239 
4240  PG_RETURN_INT32(cmp);
4241 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
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 4078 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().

4079 {
4080  Datum arg1 = PG_GETARG_DATUM(0);
4081  Datum arg2 = PG_GETARG_DATUM(1);
4082  bool result;
4083  Size len1,
4084  len2;
4085 
4086  /*
4087  * We can use a fast path for unequal lengths, which might save us from
4088  * having to detoast one or both values.
4089  */
4090  len1 = toast_raw_datum_size(arg1);
4091  len2 = toast_raw_datum_size(arg2);
4092  if (len1 != len2)
4093  result = false;
4094  else
4095  {
4096  bytea *barg1 = DatumGetByteaPP(arg1);
4097  bytea *barg2 = DatumGetByteaPP(arg2);
4098 
4099  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4100  len1 - VARHDRSZ) == 0);
4101 
4102  PG_FREE_IF_COPY(barg1, 0);
4103  PG_FREE_IF_COPY(barg2, 1);
4104  }
4105 
4106  PG_RETURN_BOOL(result);
4107 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:615
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:521
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:528
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ byteage()

Datum byteage ( PG_FUNCTION_ARGS  )

Definition at line 4202 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().

4203 {
4204  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4205  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4206  int len1,
4207  len2;
4208  int cmp;
4209 
4210  len1 = VARSIZE_ANY_EXHDR(arg1);
4211  len2 = VARSIZE_ANY_EXHDR(arg2);
4212 
4213  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4214 
4215  PG_FREE_IF_COPY(arg1, 0);
4216  PG_FREE_IF_COPY(arg2, 1);
4217 
4218  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
4219 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
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 3522 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.

3523 {
3524  bytea *v = PG_GETARG_BYTEA_PP(0);
3525  int64 n = PG_GETARG_INT64(1);
3526  int byteNo,
3527  bitNo;
3528  int len;
3529  int byte;
3530 
3531  len = VARSIZE_ANY_EXHDR(v);
3532 
3533  if (n < 0 || n >= (int64) len * 8)
3534  ereport(ERROR,
3535  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3536  errmsg("index %lld out of valid range, 0..%lld",
3537  (long long) n, (long long) len * 8 - 1)));
3538 
3539  /* n/8 is now known < len, so safe to cast to int */
3540  byteNo = (int) (n / 8);
3541  bitNo = (int) (n % 8);
3542 
3543  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3544 
3545  if (byte & (1 << bitNo))
3546  PG_RETURN_INT32(1);
3547  else
3548  PG_RETURN_INT32(0);
3549 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
#define byte(x, n)
Definition: rijndael.c:68
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

◆ byteaGetByte()

Datum byteaGetByte ( PG_FUNCTION_ARGS  )

Definition at line 3493 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.

3494 {
3495  bytea *v = PG_GETARG_BYTEA_PP(0);
3496  int32 n = PG_GETARG_INT32(1);
3497  int len;
3498  int byte;
3499 
3500  len = VARSIZE_ANY_EXHDR(v);
3501 
3502  if (n < 0 || n >= len)
3503  ereport(ERROR,
3504  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3505  errmsg("index %d out of valid range, 0..%d",
3506  n, len - 1)));
3507 
3508  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3509 
3510  PG_RETURN_INT32(byte);
3511 }
#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:354
int errcode(int sqlerrcode)
Definition: elog.c:704
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:45
#define byte(x, n)
Definition: rijndael.c:68
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609

◆ byteagt()

Datum byteagt ( PG_FUNCTION_ARGS  )

Definition at line 4182 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().

4183 {
4184  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4185  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4186  int len1,
4187  len2;
4188  int cmp;
4189 
4190  len1 = VARSIZE_ANY_EXHDR(arg1);
4191  len2 = VARSIZE_ANY_EXHDR(arg2);
4192 
4193  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4194 
4195  PG_FREE_IF_COPY(arg1, 0);
4196  PG_FREE_IF_COPY(arg2, 1);
4197 
4198  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
4199 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
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 295 of file varlena.c.

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

Referenced by CreateTrigger(), and string_to_datum().

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

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

Definition at line 4162 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().

4163 {
4164  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4165  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4166  int len1,
4167  len2;
4168  int cmp;
4169 
4170  len1 = VARSIZE_ANY_EXHDR(arg1);
4171  len2 = VARSIZE_ANY_EXHDR(arg2);
4172 
4173  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4174 
4175  PG_FREE_IF_COPY(arg1, 0);
4176  PG_FREE_IF_COPY(arg2, 1);
4177 
4178  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
4179 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
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 4142 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().

4143 {
4144  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4145  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4146  int len1,
4147  len2;
4148  int cmp;
4149 
4150  len1 = VARSIZE_ANY_EXHDR(arg1);
4151  len2 = VARSIZE_ANY_EXHDR(arg2);
4152 
4153  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4154 
4155  PG_FREE_IF_COPY(arg1, 0);
4156  PG_FREE_IF_COPY(arg2, 1);
4157 
4158  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
4159 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
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 4110 of file varlena.c.

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

4111 {
4112  Datum arg1 = PG_GETARG_DATUM(0);
4113  Datum arg2 = PG_GETARG_DATUM(1);
4114  bool result;
4115  Size len1,
4116  len2;
4117 
4118  /*
4119  * We can use a fast path for unequal lengths, which might save us from
4120  * having to detoast one or both values.
4121  */
4122  len1 = toast_raw_datum_size(arg1);
4123  len2 = toast_raw_datum_size(arg2);
4124  if (len1 != len2)
4125  result = true;
4126  else
4127  {
4128  bytea *barg1 = DatumGetByteaPP(arg1);
4129  bytea *barg2 = DatumGetByteaPP(arg2);
4130 
4131  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
4132  len1 - VARHDRSZ) != 0);
4133 
4134  PG_FREE_IF_COPY(barg1, 0);
4135  PG_FREE_IF_COPY(barg2, 1);
4136  }
4137 
4138  PG_RETURN_BOOL(result);
4139 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:615
#define DatumGetByteaPP(X)
Definition: fmgr.h:291
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:521
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:528
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ byteaoctetlen()

Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 3217 of file varlena.c.

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

3218 {
3219  Datum str = PG_GETARG_DATUM(0);
3220 
3221  /* We need not detoast the input at all */
3223 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:615
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:521
uintptr_t Datum
Definition: postgres.h:367

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 393 of file varlena.c.

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

Referenced by pg_mcv_list_out().

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

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 3390 of file varlena.c.

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

3391 {
3392  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3393  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3394  int sp = PG_GETARG_INT32(2); /* substring start position */
3395  int sl = PG_GETARG_INT32(3); /* substring length */
3396 
3397  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3398 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3413
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:609

◆ byteaoverlay_no_len()

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3401 of file varlena.c.

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

3402 {
3403  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3404  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3405  int sp = PG_GETARG_INT32(2); /* substring start position */
3406  int sl;
3407 
3408  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3409  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3410 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:3413
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609

◆ byteapos()

Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 3449 of file varlena.c.

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

3450 {
3451  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3452  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3453  int pos;
3454  int px,
3455  p;
3456  int len1,
3457  len2;
3458  char *p1,
3459  *p2;
3460 
3461  len1 = VARSIZE_ANY_EXHDR(t1);
3462  len2 = VARSIZE_ANY_EXHDR(t2);
3463 
3464  if (len2 <= 0)
3465  PG_RETURN_INT32(1); /* result for empty pattern */
3466 
3467  p1 = VARDATA_ANY(t1);
3468  p2 = VARDATA_ANY(t2);
3469 
3470  pos = 0;
3471  px = (len1 - len2);
3472  for (p = 0; p <= px; p++)
3473  {
3474  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3475  {
3476  pos = p + 1;
3477  break;
3478  };
3479  p1++;
3480  };
3481 
3482  PG_RETURN_INT32(pos);
3483 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
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:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

Definition at line 480 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.

481 {
483  bytea *result;
484  int nbytes;
485 
486  nbytes = buf->len - buf->cursor;
487  result = (bytea *) palloc(nbytes + VARHDRSZ);
488  SET_VARSIZE(result, nbytes + VARHDRSZ);
489  pq_copymsgbytes(buf, VARDATA(result), nbytes);
490  PG_RETURN_BYTEA_P(result);
491 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:615
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
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:609
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 499 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().

500 {
501  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
502 
503  PG_RETURN_BYTEA_P(vlena);
504 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
Definition: c.h:609

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3592 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.

3593 {
3594  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3595  int64 n = PG_GETARG_INT64(1);
3596  int32 newBit = PG_GETARG_INT32(2);
3597  int len;
3598  int oldByte,
3599  newByte;
3600  int byteNo,
3601  bitNo;
3602 
3603  len = VARSIZE(res) - VARHDRSZ;
3604 
3605  if (n < 0 || n >= (int64) len * 8)
3606  ereport(ERROR,
3607  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3608  errmsg("index %lld out of valid range, 0..%lld",
3609  (long long) n, (long long) len * 8 - 1)));
3610 
3611  /* n/8 is now known < len, so safe to cast to int */
3612  byteNo = (int) (n / 8);
3613  bitNo = (int) (n % 8);
3614 
3615  /*
3616  * sanity check!
3617  */
3618  if (newBit != 0 && newBit != 1)
3619  ereport(ERROR,
3620  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3621  errmsg("new bit must be 0 or 1")));
3622 
3623  /*
3624  * Update the byte.
3625  */
3626  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3627 
3628  if (newBit == 0)
3629  newByte = oldByte & (~(1 << bitNo));
3630  else
3631  newByte = oldByte | (1 << bitNo);
3632 
3633  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3634 
3635  PG_RETURN_BYTEA_P(res);
3636 }
#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:615
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:704
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

◆ byteaSetByte()

Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 3560 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.

3561 {
3562  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3563  int32 n = PG_GETARG_INT32(1);
3564  int32 newByte = PG_GETARG_INT32(2);
3565  int len;
3566 
3567  len = VARSIZE(res) - VARHDRSZ;
3568 
3569  if (n < 0 || n >= len)
3570  ereport(ERROR,
3571  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3572  errmsg("index %d out of valid range, 0..%d",
3573  n, len - 1)));
3574 
3575  /*
3576  * Now set the byte.
3577  */
3578  ((unsigned char *) VARDATA(res))[n] = newByte;
3579 
3580  PG_RETURN_BYTEA_P(res);
3581 }
#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:615
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:704
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609

◆ charlen_to_bytelen()

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

Definition at line 792 of file varlena.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

793 {
795  {
796  /* Optimization for single-byte encodings */
797  return n;
798  }
799  else
800  {
801  const char *s;
802 
803  for (s = p; n > 0; n--)
804  s += pg_mblen(s);
805 
806  return s - p;
807  }
808 }
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 1512 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().

1513 {
1514  if (!OidIsValid(collid))
1515  {
1516  /*
1517  * This typically means that the parser could not resolve a conflict
1518  * of implicit collations, so report it that way.
1519  */
1520  ereport(ERROR,
1521  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1522  errmsg("could not determine which collation to use for string comparison"),
1523  errhint("Use the COLLATE clause to set the collation explicitly.")));
1524  }
1525 }
int errhint(const char *fmt,...)
Definition: elog.c:1162
int errcode(int sqlerrcode)
Definition: elog.c:704
#define OidIsValid(objectId)
Definition: c.h:698
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ check_replace_text_has_escape_char()

static bool check_replace_text_has_escape_char ( const text replace_text)
static

Definition at line 4355 of file varlena.c.

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

Referenced by replace_text_regexp().

4356 {
4357  const char *p = VARDATA_ANY(replace_text);
4358  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4359 
4361  {
4362  for (; p < p_end; p++)
4363  {
4364  if (*p == '\\')
4365  return true;
4366  }
4367  }
4368  else
4369  {
4370  for (; p < p_end; p += pg_mblen(p))
4371  {
4372  if (*p == '\\')
4373  return true;
4374  }
4375  }
4376 
4377  return false;
4378 }
#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 5424 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().

5426 {
5427  text *result;
5429  FmgrInfo *foutcache;
5430  bool first_arg = true;
5431  int i;
5432 
5433  /*
5434  * concat(VARIADIC some-array) is essentially equivalent to
5435  * array_to_text(), ie concat the array elements with the given separator.
5436  * So we just pass the case off to that code.
5437  */
5438  if (get_fn_expr_variadic(fcinfo->flinfo))
5439  {
5440  ArrayType *arr;
5441 
5442  /* Should have just the one argument */
5443  Assert(argidx == PG_NARGS() - 1);
5444 
5445  /* concat(VARIADIC NULL) is defined as NULL */
5446  if (PG_ARGISNULL(argidx))
5447  return NULL;
5448 
5449  /*
5450  * Non-null argument had better be an array. We assume that any call
5451  * context that could let get_fn_expr_variadic return true will have
5452  * checked that a VARIADIC-labeled parameter actually is an array. So
5453  * it should be okay to just Assert that it's an array rather than
5454  * doing a full-fledged error check.
5455  */
5457 
5458  /* OK, safe to fetch the array value */
5459  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5460 
5461  /*
5462  * And serialize the array. We tell array_to_text to ignore null
5463  * elements, which matches the behavior of the loop below.
5464  */
5465  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5466  }
5467 
5468  /* Normal case without explicit VARIADIC marker */
5469  initStringInfo(&str);
5470 
5471  /* Get output function info, building it if first time through */
5472  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5473  if (foutcache == NULL)
5474  foutcache = build_concat_foutcache(fcinfo, argidx);
5475 
5476  for (i = argidx; i < PG_NARGS(); i++)
5477  {
5478  if (!PG_ARGISNULL(i))
5479  {
5481 
5482  /* add separator if appropriate */
5483  if (first_arg)
5484  first_arg = false;
5485  else
5486  appendStringInfoString(&str, sepstr);
5487 
5488  /* call the appropriate type output function, append the result */
5490  OutputFunctionCall(&foutcache[i], value));
5491  }
5492  }
5493 
5494  result = cstring_to_text_with_len(str.data, str.len);
5495  pfree(str.data);
5496 
5497  return result;
5498 }
Definition: fmgr.h:56
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
static struct @144 value
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1937
#define OidIsValid(objectId)
Definition: c.h:698
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1576
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
void pfree(void *pointer)
Definition: mcxt.c:1057
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1803
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5088
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:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:792
#define PG_NARGS()
Definition: fmgr.h:203
void * fn_extra
Definition: fmgr.h:64
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2752
int i
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5386
Definition: c.h:609

◆ cstring_to_text()

text* cstring_to_text ( const char *  s)

Definition at line 189 of file varlena.c.

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_page_type(), build_test_info_result(), build_test_match_result(), cash_words(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), executeDateTimeMethod(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_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_client_dn(), ssl_issuer_dn(), ssl_version(), string_to_text(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), unaccent_dict(), upper(), xml_encode_special_chars(), xml_in(), and xml_recv().

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

◆ cstring_to_text_with_len()

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

Definition at line 201 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(), build_test_match_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_subscript_fetch(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array(), json_build_array_noargs(), json_build_object(), json_build_object_noargs(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_pretty(), JsonbValueAsText(), leftmostvalue_text(), LogicalOutputWrite(), parse_ident(), pg_gen_salt(), pg_gen_salt_rounds(), 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().

202 {
203  text *result = (text *) palloc(len + VARHDRSZ);
204 
205  SET_VARSIZE(result, len + VARHDRSZ);
206  memcpy(VARDATA(result), s, len);
207 
208  return result;
209 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:615
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:609
#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 3092 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().

3093 {
3094  int result;
3095  int len1,
3096  len2;
3097 
3098  len1 = VARSIZE_ANY_EXHDR(arg1);
3099  len2 = VARSIZE_ANY_EXHDR(arg2);
3100 
3101  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3102  if (result != 0)
3103  return result;
3104  else if (len1 < len2)
3105  return -1;
3106  else if (len1 > len2)
3107  return 1;
3108  else
3109  return 0;
3110 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:974
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5314 of file varlena.c.

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

5315 {
5316  StringInfo state;
5317  MemoryContext aggcontext;
5318  MemoryContext oldcontext;
5319 
5320  if (!AggCheckCallContext(fcinfo, &aggcontext))
5321  {
5322  /* cannot be called directly because of internal-type argument */
5323  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5324  }
5325 
5326  /*
5327  * Create state in aggregate context. It'll stay there across subsequent
5328  * calls.
5329  */
5330  oldcontext = MemoryContextSwitchTo(aggcontext);
5331  state = makeStringInfo();
5332  MemoryContextSwitchTo(oldcontext);
5333 
5334  return state;
5335 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:45
ts_parserstate state
Definition: tsquery.c:81
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4582
#define elog(elevel,...)
Definition: elog.h:228

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3666 of file varlena.c.

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

Referenced by nameiclike(), and nameicnlike().

3667 {
3668  Name s = PG_GETARG_NAME(0);
3669 
3671 }
Definition: c.h:663
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
#define NameStr(name)
Definition: c.h:669
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

Definition at line 2895 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().

2896 {
2897  Name arg1 = PG_GETARG_NAME(0);
2898  text *arg2 = PG_GETARG_TEXT_PP(1);
2899  size_t len1 = strlen(NameStr(*arg1));
2900  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2901  Oid collid = PG_GET_COLLATION();
2902  bool result;
2903 
2904  check_collation_set(collid);
2905 
2906  if (collid == C_COLLATION_OID)
2907  result = (len1 == len2 &&
2908  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2909  else
2910  result = (varstr_cmp(NameStr(*arg1), len1,
2911  VARDATA_ANY(arg2), len2,
2912  collid) == 0);
2913 
2914  PG_FREE_IF_COPY(arg2, 1);
2915 
2916  PG_RETURN_BOOL(result);
2917 }
#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:309
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1541
static void check_collation_set(Oid collid)
Definition: varlena.c:1512
Definition: c.h:663
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#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:669
Definition: c.h:609
#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 2258 of file varlena.c.

References DatumGetName, NAMEDATALEN, and NameStr.

Referenced by varstr_sortsupport().

2259 {
2260  Name arg1 = DatumGetName(x);
2261  Name arg2 = DatumGetName(y);
2262 
2263  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2264 }
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:585
Definition: c.h:663
#define NameStr(name)
Definition: c.h:669

◆ namefastcmp_locale()

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

Definition at line 2301 of file varlena.c.

References DatumGetName, NameStr, and varstrfastcmp_locale().

Referenced by varstr_sortsupport().

2302 {
2303  Name arg1 = DatumGetName(x);
2304  Name arg2 = DatumGetName(y);
2305 
2306  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2307  NameStr(*arg2), strlen(NameStr(*arg2)),
2308  ssup);
2309 }
#define DatumGetName(X)
Definition: postgres.h:585
Definition: c.h:663
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2315
#define NameStr(name)
Definition: c.h:669

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 3051 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3052 {
3054 }
#define CmpCall(cmpfunc)
Definition: varlena.c:3026
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2995

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 3045 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3046 {
3048 }
#define CmpCall(cmpfunc)
Definition: varlena.c:3026
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2995

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 3039 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3040 {
3042 }
#define CmpCall(cmpfunc)
Definition: varlena.c:3026
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2995

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 3033 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3034 {
3036 }
#define CmpCall(cmpfunc)
Definition: varlena.c:3026
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2995

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2945 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().

2946 {
2947  Name arg1 = PG_GETARG_NAME(0);
2948  text *arg2 = PG_GETARG_TEXT_PP(1);
2949  size_t len1 = strlen(NameStr(*arg1));
2950  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2951  Oid collid = PG_GET_COLLATION();
2952  bool result;
2953 
2954  check_collation_set(collid);
2955 
2956  if (collid == C_COLLATION_OID)
2957  result = !(len1 == len2 &&
2958  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2959  else
2960  result = !(varstr_cmp(NameStr(*arg1), len1,
2961  VARDATA_ANY(arg2), len2,
2962  collid) == 0);
2963 
2964  PG_FREE_IF_COPY(arg2, 1);
2965 
2966  PG_RETURN_BOOL(result);
2967 }
#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:309
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1541
static void check_collation_set(Oid collid)
Definition: varlena.c:1512
Definition: c.h:663
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#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:669
Definition: c.h:609
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 5260 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.

5261 {
5263  int32 result;
5264  int typlen;
5265 
5266  /* On first call, get the input type's typlen, and save at *fn_extra */
5267  if (fcinfo->flinfo->fn_extra == NULL)
5268  {
5269  /* Lookup the datatype of the supplied argument */
5270  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5271 
5272  typlen = get_typlen(argtypeid);
5273  if (typlen == 0) /* should not happen */
5274  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5275 
5276  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5277  sizeof(int));
5278  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5279  }
5280  else
5281  typlen = *((int *) fcinfo->flinfo->fn_extra);
5282 
5283  if (typlen == -1)
5284  {
5285  /* varlena type, possibly toasted */
5286  result = toast_datum_size(value);
5287  }
5288  else if (typlen == -2)
5289  {
5290  /* cstring */
5291  result = strlen(DatumGetCString(value)) + 1;
5292  }
5293  else
5294  {
5295  /* ordinary fixed-width type */
5296  result = typlen;
5297  }
5298 
5299  PG_RETURN_INT32(result);
5300 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
static struct @144 value
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:417
Size toast_datum_size(Datum value)
Definition: detoast.c:577
#define ERROR
Definition: elog.h:45
#define DatumGetCString(X)
Definition: postgres.h:566
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1803
uintptr_t Datum
Definition: postgres.h:367
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2117
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:228

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 4280 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().

4281 {
4282  text *src_text = PG_GETARG_TEXT_PP(0);
4283  text *from_sub_text = PG_GETARG_TEXT_PP(1);
4284  text *to_sub_text = PG_GETARG_TEXT_PP(2);
4285  int src_text_len;
4286  int from_sub_text_len;
4288  text *ret_text;
4289  int chunk_len;
4290  char *curr_ptr;
4291  char *start_ptr;
4293  bool found;
4294 
4295  src_text_len = VARSIZE_ANY_EXHDR(src_text);
4296  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4297 
4298  /* Return unmodified source string if empty source or pattern */
4299  if (src_text_len < 1 || from_sub_text_len < 1)
4300  {
4301  PG_RETURN_TEXT_P(src_text);
4302  }
4303 
4304  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4305 
4306  found = text_position_next(&state);
4307 
4308  /* When the from_sub_text is not found, there is nothing to do. */
4309  if (!found)
4310  {
4311  text_position_cleanup(&state);
4312  PG_RETURN_TEXT_P(src_text);
4313  }
4314  curr_ptr = text_position_get_match_ptr(&state);
4315  start_ptr = VARDATA_ANY(src_text);
4316 
4317  initStringInfo(&str);
4318 
4319  do
4320  {
4322 
4323  /* copy the data skipped over by last text_position_next() */
4324  chunk_len = curr_ptr - start_ptr;
4325  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4326 
4327  appendStringInfoText(&str, to_sub_text);
4328 
4329  start_ptr = curr_ptr + from_sub_text_len;
4330 
4331  found = text_position_next(&state);
4332  if (found)
4333  curr_ptr = text_position_get_match_ptr(&state);
4334  }
4335  while (found);
4336 
4337  /* copy trailing data */
4338  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4339  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4340 
4341  text_position_cleanup(&state);
4342 
4343  ret_text = cstring_to_text_with_len(str.data, str.len);
4344  pfree(str.data);
4345 
4346  PG_RETURN_TEXT_P(ret_text);
4347 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1330
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1462
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1057
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4266
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
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:1505
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1201
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
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 4493 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().

4495 {
4496  text *ret_text;
4497  regex_t *re = (regex_t *) regexp;
4498  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4501  pg_wchar *data;
4502  size_t data_len;
4503  int search_start;
4504  int data_pos;
4505  char *start_ptr;
4506  bool have_escape;
4507 
4508  initStringInfo(&buf);
4509 
4510  /* Convert data string to wide characters. */
4511  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4512  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4513 
4514  /* Check whether replace_text has escape char. */
4515  have_escape = check_replace_text_has_escape_char(replace_text);
4516 
4517  /* start_ptr points to the data_pos'th character of src_text */
4518  start_ptr = (char *) VARDATA_ANY(src_text);
4519  data_pos = 0;
4520 
4521  search_start = 0;
4522  while (search_start <= data_len)
4523  {
4524  int regexec_result;
4525 
4527 
4528  regexec_result = pg_regexec(re,
4529  data,
4530  data_len,
4531  search_start,
4532  NULL, /* no details */
4534  pmatch,
4535  0);
4536 
4537  if (regexec_result == REG_NOMATCH)
4538  break;
4539 
4540  if (regexec_result != REG_OKAY)
4541  {
4542  char errMsg[100];
4543 
4545  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4546  ereport(ERROR,
4547  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4548  errmsg("regular expression failed: %s", errMsg)));
4549  }
4550 
4551  /*
4552  * Copy the text to the left of the match position. Note we are given
4553  * character not byte indexes.
4554  */
4555  if (pmatch[0].rm_so - data_pos > 0)
4556  {
4557  int chunk_len;
4558 
4559  chunk_len = charlen_to_bytelen(start_ptr,
4560  pmatch[0].rm_so - data_pos);
4561  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4562 
4563  /*
4564  * Advance start_ptr over that text, to avoid multiple rescans of
4565  * it if the replace_text contains multiple back-references.
4566  */
4567  start_ptr += chunk_len;
4568  data_pos = pmatch[0].rm_so;
4569  }
4570 
4571  /*
4572  * Copy the replace_text. Process back references when the
4573  * replace_text has escape characters.
4574  */
4575  if (have_escape)
4576  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4577  start_ptr, data_pos);
4578  else
4579  appendStringInfoText(&buf, replace_text);
4580 
4581  /* Advance start_ptr and data_pos over the matched text. */
4582  start_ptr += charlen_to_bytelen(start_ptr,
4583  pmatch[0].rm_eo - data_pos);
4584  data_pos = pmatch[0].rm_eo;
4585 
4586  /*
4587  * When global option is off, replace the first instance only.
4588  */
4589  if (!glob)
4590  break;
4591 
4592  /*
4593  * Advance search position. Normally we start the next search at the
4594  * end of the previous match; but if the match was of zero length, we
4595  * have to advance by one character, or we'd just find the same match
4596  * again.
4597  */
4598  search_start = data_pos;
4599  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4600  search_start++;
4601  }
4602 
4603  /*
4604  * Copy the text to the right of the last match.
4605  */
4606  if (data_pos < data_len)
4607  {
4608  int chunk_len;
4609 
4610  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4611  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4612  }
4613 
4614  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4615  pfree(buf.data);
4616  pfree(data);
4617 
4618  return ret_text;
4619 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
int errcode(int sqlerrcode)
Definition: elog.c:704
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:45
static bool check_replace_text_has_escape_char(const text *replace_text)
Definition: varlena.c:4355
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:792
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4266
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
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:4482
#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:155
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:4388
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:915
#define REG_NOMATCH
Definition: regex.h:138
Definition: c.h:609
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
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 6154 of file varlena.c.

Referenced by varstr_levenshtein().

6155 {
6156  while (len > 0)
6157  {
6158  len--;
6159  if (s1[len] != s2[len])
6160  return false;
6161  }
6162  return true;
6163 }
char * s1
char * s2

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 4627 of file varlena.c.

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

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

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

Definition at line 4871 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().

4872 {
4873  text *inputstring;
4874  text *fldsep;
4875  text *null_string;
4876  Oid collation = PG_GET_COLLATION();
4877  int inputstring_len;
4878  int fldsep_len;
4879  char *start_ptr;
4880  text *result_text;
4881 
4882  /* when input string is NULL, then result is NULL too */
4883  if (PG_ARGISNULL(0))
4884  return false;
4885 
4886  inputstring = PG_GETARG_TEXT_PP(0);
4887 
4888  /* fldsep can be NULL */
4889  if (!PG_ARGISNULL(1))
4890  fldsep = PG_GETARG_TEXT_PP(1);
4891  else
4892  fldsep = NULL;
4893 
4894  /* null_string can be NULL or omitted */
4895  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4896  null_string = PG_GETARG_TEXT_PP(2);
4897  else
4898  null_string = NULL;
4899 
4900  if (fldsep != NULL)
4901  {
4902  /*
4903  * Normal case with non-null fldsep. Use the text_position machinery
4904  * to search for occurrences of fldsep.
4905  */
4907 
4908  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4909  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4910 
4911  /* return empty set for empty input string */
4912  if (inputstring_len < 1)
4913  return true;
4914 
4915  /* empty field separator: return input string as a one-element set */
4916  if (fldsep_len < 1)
4917  {
4918  split_text_accum_result(tstate, inputstring,
4919  null_string, collation);
4920  return true;
4921  }
4922 
4923  text_position_setup(inputstring, fldsep, collation, &state);
4924 
4925  start_ptr = VARDATA_ANY(inputstring);
4926 
4927  for (;;)
4928  {
4929  bool found;
4930  char *end_ptr;
4931  int chunk_len;
4932 
4934 
4935  found = text_position_next(&state);
4936  if (!found)
4937  {
4938  /* fetch last field */
4939  chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4940  end_ptr = NULL; /* not used, but some compilers complain */
4941  }
4942  else
4943  {
4944  /* fetch non-last field */
4945  end_ptr = text_position_get_match_ptr(&state);
4946  chunk_len = end_ptr - start_ptr;
4947  }
4948 
4949  /* build a temp text datum to pass to split_text_accum_result */
4950  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4951 
4952  /* stash away this field */
4953  split_text_accum_result(tstate, result_text,
4954  null_string, collation);
4955 
4956  pfree(result_text);
4957 
4958  if (!found)
4959  break;
4960 
4961  start_ptr = end_ptr + fldsep_len;
4962  }
4963 
4964  text_position_cleanup(&state);
4965  }
4966  else
4967  {
4968  /*
4969  * When fldsep is NULL, each character in the input string becomes a
4970  * separate element in the result set. The separator is effectively
4971  * the space between characters.
4972  */
4973  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4974 
4975  start_ptr = VARDATA_ANY(inputstring);
4976 
4977  while (inputstring_len > 0)
4978  {
4979  int chunk_len = pg_mblen(start_ptr);
4980 
4982 
4983  /* build a temp text datum to pass to split_text_accum_result */
4984  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4985 
4986  /* stash away this field */
4987  split_text_accum_result(tstate, result_text,
4988  null_string, collation);
4989 
4990  pfree(result_text);
4991 
4992  start_ptr += chunk_len;
4993  inputstring_len -= chunk_len;
4994  }
4995  }
4996 
4997  return true;
4998 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1330
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1462
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:309
void pfree(void *pointer)
Definition: mcxt.c:1057
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
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:1505
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition: varlena.c:5007
#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:1201
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100

◆ split_text_accum_result()

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

Definition at line 5007 of file varlena.c.

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

Referenced by split_text().

5011 {
5012  bool is_null = false;
5013 
5014  if (null_string && text_isequal(field_value, null_string, collation))
5015  is_null = true;
5016 
5017  if (tstate->tupstore)
5018  {
5019  Datum values[1];
5020  bool nulls[1];
5021 
5022  values[0] = PointerGetDatum(field_value);
5023  nulls[0] = is_null;
5024 
5026  tstate->tupdesc,
5027  values,
5028  nulls);
5029  }
5030  else
5031  {
5032  tstate->astate = accumArrayResult(tstate->astate,
5033  PointerGetDatum(field_value),
5034  is_null,
5035  TEXTOID,
5037  }
5038 }
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:104
TupleDesc tupdesc
Definition: varlena.c:105
static bool text_isequal(text *txt1, text *txt2, Oid collid)
Definition: varlena.c:4759
ArrayBuildState * astate
Definition: varlena.c:103
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:5085

◆ SplitDirectoriesString()

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

Definition at line 3868 of file varlena.c.

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

Referenced by load_libraries(), and PostmasterMain().

3870 {
3871  char *nextp = rawstring;
3872  bool done = false;
3873 
3874  *namelist = NIL;
3875 
3876  while (scanner_isspace(*nextp))
3877  nextp++; /* skip leading whitespace */
3878 
3879  if (*nextp == '\0')
3880  return true; /* allow empty string */
3881 
3882  /* At the top of the loop, we are at start of a new directory. */
3883  do
3884  {
3885  char *curname;
3886  char *endp;
3887 
3888  if (*nextp == '"')
3889  {
3890  /* Quoted name --- collapse quote-quote pairs */
3891  curname = nextp + 1;
3892  for (;;)
3893  {
3894  endp = strchr(nextp + 1, '"');
3895  if (endp == NULL)
3896  return false; /* mismatched quotes */
3897  if (endp[1] != '"')
3898  break; /* found end of quoted name */
3899  /* Collapse adjacent quotes into one quote, and look again */
3900  memmove(endp, endp + 1, strlen(endp));
3901  nextp = endp;
3902  }
3903  /* endp now points at the terminating quote */
3904  nextp = endp + 1;
3905  }
3906  else
3907  {
3908  /* Unquoted name --- extends to separator or end of string */
3909  curname = endp = nextp;
3910  while (*nextp && *nextp != separator)
3911  {
3912  /* trailing whitespace should not be included in name */
3913  if (!scanner_isspace(*nextp))
3914  endp = nextp + 1;
3915  nextp++;
3916  }
3917  if (curname == endp)
3918  return false; /* empty unquoted name not allowed */
3919  }
3920 
3921  while (scanner_isspace(*nextp))
3922  nextp++; /* skip trailing whitespace */
3923 
3924  if (*nextp == separator)
3925  {
3926  nextp++;
3927  while (scanner_isspace(*nextp))
3928  nextp++; /* skip leading whitespace for next */
3929  /* we expect another name, so done remains false */
3930  }
3931  else if (*nextp == '\0')
3932  done = true;
3933  else
3934  return false; /* invalid syntax */
3935 
3936  /* Now safe to overwrite separator with a null */
3937  *endp = '\0';
3938 
3939  /* Truncate path if it's overlength */
3940  if (strlen(curname) >= MAXPGPATH)
3941  curname[MAXPGPATH - 1] = '\0';
3942 
3943  /*
3944  * Finished isolating current name --- add it to list
3945  */
3946  curname = pstrdup(curname);
3947  canonicalize_path(curname);
3948  *namelist = lappend(*namelist, curname);
3949 
3950  /* Loop back if we didn't reach end of string */
3951  } while (!done);
3952 
3953  return true;
3954 }
#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 3989 of file varlena.c.

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

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

3991 {
3992  char *nextp = rawstring;
3993  bool done = false;
3994 
3995  *namelist = NIL;
3996 
3997  while (scanner_isspace(*nextp))
3998  nextp++; /* skip leading whitespace */
3999 
4000  if (*nextp == '\0')
4001  return true; /* allow empty string */
4002 
4003  /* At the top of the loop, we are at start of a new identifier. */
4004  do
4005  {
4006  char *curname;
4007  char *endp;
4008 
4009  if (*nextp == '"')
4010  {
4011  /* Quoted name --- collapse quote-quote pairs */
4012  curname = nextp + 1;
4013  for (;;)
4014  {
4015  endp = strchr(nextp + 1, '"');
4016  if (endp == NULL)
4017  return false; /* mismatched quotes */
4018  if (endp[1] != '"')
4019  break; /* found end of quoted name */
4020  /* Collapse adjacent quotes into one quote, and look again */
4021  memmove(endp, endp + 1, strlen(endp));
4022  nextp = endp;
4023  }
4024  /* endp now points at the terminating quote */
4025  nextp = endp + 1;
4026  }
4027  else
4028  {
4029  /* Unquoted name --- extends to separator or whitespace */
4030  curname = nextp;
4031  while (*nextp && *nextp != separator &&
4032  !scanner_isspace(*nextp))
4033  nextp++;
4034  endp = nextp;
4035  if (curname == nextp)
4036  return false; /* empty unquoted name not allowed */
4037  }
4038 
4039  while (scanner_isspace(*nextp))
4040  nextp++; /* skip trailing whitespace */
4041 
4042  if (*nextp == separator)
4043  {
4044  nextp++;
4045  while (scanner_isspace(*nextp))
4046  nextp++; /* skip leading whitespace for next */
4047  /* we expect another name, so done remains false */
4048  }
4049  else if (*nextp == '\0')
4050  done = true;
4051  else
4052  return false; /* invalid syntax */
4053 
4054  /* Now safe to overwrite separator with a null */
4055  *endp = '\0';
4056 
4057  /*
4058  * Finished isolating current name --- add it to list
4059  */
4060  *namelist = lappend(*namelist, curname);
4061 
4062  /* Loop back if we didn't reach end of string */
4063  } while (!done);
4064 
4065  return true;
4066 }
#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 3741 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().

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

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 5364 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.

5365 {
5366  StringInfo state;
5367 
5368  /* cannot be called directly because of internal-type argument */
5369  Assert(AggCheckCallContext(fcinfo, NULL));
5370 
5371  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5372 
5373  if (state != NULL)
5375  else
5376  PG_RETURN_NULL();
5377 }
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:201
ts_parserstate state
Definition: tsquery.c:81
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:792
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4582
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5338 of file varlena.c.

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

5339 {
5340  StringInfo state;
5341 
5342  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5343 
5344  /* Append the value unless null. */
5345  if (!PG_ARGISNULL(1))
5346  {
5347  /* On the first time through, we ignore the delimiter. */
5348  if (state == NULL)
5349  state = makeStringAggState(fcinfo);
5350  else if (!PG_ARGISNULL(2))
5351  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
5352 
5353  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
5354  }
5355 
5356  /*
5357  * The transition type for string_agg() is declared to be "internal",
5358  * which is a pass-by-value type the same size as a pointer.
5359  */
5360  PG_RETURN_POINTER(state);
5361 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4266
ts_parserstate state
Definition: tsquery.c:81
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5314

◆ text_catenate()

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

Definition at line 751 of file varlena.c.

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

Referenced by text_overlay(), and textcat().

752 {
753  text *result;
754  int len1,
755  len2,
756  len;
757  char *ptr;
758 
759  len1 = VARSIZE_ANY_EXHDR(t1);
760  len2 = VARSIZE_ANY_EXHDR(t2);
761 
762  /* paranoia ... probably should throw error instead? */
763  if (len1 < 0)
764  len1 = 0;
765  if (len2 < 0)
766  len2 = 0;
767 
768  len = len1 + len2 + VARHDRSZ;
769  result = (text *) palloc(len);
770 
771  /* Set size of result string... */
772  SET_VARSIZE(result, len);
773 
774  /* Fill data field of result string... */
775  ptr = VARDATA(result);
776  if (len1 > 0)
777  memcpy(ptr, VARDATA_ANY(t1), len1);
778  if (len2 > 0)
779  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
780 
781  return result;
782 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:615
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
Definition: c.h:609
#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 1757 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().

1758 {
1759  char *a1p,
1760  *a2p;
1761  int len1,
1762  len2;
1763 
1764  a1p = VARDATA_ANY(arg1);
1765  a2p = VARDATA_ANY(arg2);
1766 
1767  len1 = VARSIZE_ANY_EXHDR(arg1);
1768  len2 = VARSIZE_ANY_EXHDR(arg2);
1769 
1770  return varstr_cmp(a1p, len1, a2p, len2, collid);
1771 }
#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:1541
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 5504 of file varlena.c.

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

5505 {
5506  text *result;
5507 
5508  result = concat_internal("", 0, fcinfo);
5509  if (result == NULL)
5510  PG_RETURN_NULL();
5511  PG_RETURN_TEXT_P(result);
5512 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5424
Definition: c.h:609
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5519 of file varlena.c.

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

5520 {
5521  char *sep;
5522  text *result;
5523 
5524  /* return NULL when separator is NULL */
5525  if (PG_ARGISNULL(0))
5526  PG_RETURN_NULL();
5528 
5529  result = concat_internal(sep, 1, fcinfo);
5530  if (result == NULL)
5531  PG_RETURN_NULL();
5532  PG_RETURN_TEXT_P(result);
5533 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5424
char * text_to_cstring(const text *t)
Definition: varlena.c:222
Definition: c.h:609
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 5640 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().

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

◆ text_format_append_string()

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

Definition at line 6092 of file varlena.c.

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

Referenced by text_format_string_conversion().

6094 {
6095  bool align_to_left = false;
6096  int len;
6097 
6098  /* fast path for typical easy case */
6099  if (width == 0)
6100  {
6102  return;
6103  }
6104 
6105  if (width < 0)
6106  {
6107  /* Negative width: implicit '-' flag, then take absolute value */
6108  align_to_left = true;
6109  /* -INT_MIN is undefined */
6110  if (width <= INT_MIN)
6111  ereport(ERROR,
6112  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6113  errmsg("number is out of range")));
6114  width = -width;
6115  }
6116  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6117  align_to_left = true;
6118 
6119  len = pg_mbstrlen(str);
6120  if (align_to_left)
6121  {
6122  /* left justify */
6124  if (len < width)
6125  appendStringInfoSpaces(buf, width - len);
6126  }
6127  else
6128  {
6129  /* right justify */
6130  if (len < width)
6131  appendStringInfoSpaces(buf, width - len);
6133  }
6134 }
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
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:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5625

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 6144 of file varlena.c.

References text_format().

6145 {
6146  return text_format(fcinfo);
6147 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5640

◆ text_format_parse_digits()

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

Definition at line 5917 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().

5918 {
5919  bool found = false;
5920  const char *cp = *ptr;
5921  int val = 0;
5922 
5923  while (*cp >= '0' && *cp <= '9')
5924  {
5925  int8 digit = (*cp - '0');
5926 
5927  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5928  unlikely(pg_add_s32_overflow(val, digit, &val)))
5929  ereport(ERROR,
5930  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5931  errmsg("number is out of range")));
5932  ADVANCE_PARSE_POINTER(cp, end_ptr);
5933  found = true;
5934  }
5935 
5936  *ptr = cp;
5937  *value = val;
5938 
5939  return found;
5940 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5627
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
static struct @144 value
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
signed char int8
Definition: c.h:415
#define ereport(elevel,...)
Definition: elog.h:155
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define unlikely(x)
Definition: c.h:261
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 5966 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().

5969 {
5970  const char *cp = start_ptr;
5971  int n;
5972 
5973  /* set defaults for output parameters */
5974  *argpos = -1;
5975  *widthpos = -1;
5976  *flags = 0;
5977  *width = 0;
5978 
5979  /* try to identify first number */
5980  if (text_format_parse_digits(&cp, end_ptr, &n))
5981  {
5982  if (*cp != '$')
5983  {
5984  /* Must be just a width and a type, so we're done */
5985  *width = n;
5986  return cp;
5987  }
5988  /* The number was argument position */
5989  *argpos = n;
5990  /* Explicit 0 for argument index is immediately refused */
5991  if (n == 0)
5992  ereport(ERROR,
5993  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5994  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5995  ADVANCE_PARSE_POINTER(cp, end_ptr);
5996  }
5997 
5998  /* Handle flags (only minus is supported now) */
5999  while (*cp == '-')
6000  {
6001  *flags |= TEXT_FORMAT_FLAG_MINUS;
6002  ADVANCE_PARSE_POINTER(cp, end_ptr);
6003  }
6004 
6005  if (*cp == '*')
6006  {
6007  /* Handle indirect width */
6008  ADVANCE_PARSE_POINTER(cp, end_ptr);
6009  if (text_format_parse_digits(&cp, end_ptr, &n))
6010  {
6011  /* number in this position must be closed by $ */
6012  if (*cp != '$')
6013  ereport(ERROR,
6014  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6015  errmsg("width argument position must be ended by \"$\"")));
6016  /* The number was width argument position */
6017  *widthpos = n;
6018  /* Explicit 0 for argument index is immediately refused */
6019  if (n == 0)
6020  ereport(ERROR,
6021  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6022  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6023  ADVANCE_PARSE_POINTER(cp, end_ptr);
6024  }
6025  else
6026  *widthpos = 0; /* width's argument position is unspecified */
6027  }
6028  else
6029  {
6030  /* Check for direct width specification */
6031  if (text_format_parse_digits(&cp, end_ptr, &n))
6032  *width = n;
6033  }
6034 
6035  /* cp should now be pointing at type character */
6036  return cp;
6037 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5627
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5917
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5625

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

6047 {
6048  char *str;
6049 
6050  /* Handle NULL arguments before trying to stringify the value. */
6051  if (isNull)
6052  {
6053  if (conversion == 's')
6054  text_format_append_string(buf, "", flags, width);
6055  else if (conversion == 'L')
6056  text_format_append_string(buf, "NULL", flags, width);
6057  else if (conversion == 'I')
6058  ereport(ERROR,
6059  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6060  errmsg("null values cannot be formatted as an SQL identifier")));
6061  return;
6062  }
6063 
6064  /* Stringify. */
6065  str = OutputFunctionCall(typOutputInfo, value);
6066 
6067  /* Escape. */
6068  if (conversion == 'I')
6069  {
6070  /* quote_identifier may or may not allocate a new string. */
6071  text_format_append_string(buf, quote_identifier(str), flags, width);
6072  }
6073  else if (conversion == 'L')
6074  {
6075  char *qstr = quote_literal_cstr(str);
6076 
6077  text_format_append_string(buf, qstr, flags, width);
6078  /* quote_literal_cstr() always allocates a new string */
6079  pfree(qstr);
6080  }
6081  else
6082  text_format_append_string(buf, str, flags, width);
6083 
6084  /* Cleanup. */
6085  pfree(str);
6086 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10933
static struct @144 value
int errcode(int sqlerrcode)
Definition: elog.c:704
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6092
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1576
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1929 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().

1930 {
1931  text *arg1 = PG_GETARG_TEXT_PP(0);
1932  text *arg2 = PG_GETARG_TEXT_PP(1);
1933  bool result;
1934 
1935  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1936 
1937  PG_FREE_IF_COPY(arg1, 0);
1938  PG_FREE_IF_COPY(arg2, 1);
1939 
1940  PG_RETURN_BOOL(result);
1941 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1757
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1914 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().

1915 {
1916  text *arg1 = PG_GETARG_TEXT_PP(0);
1917  text *arg2 = PG_GETARG_TEXT_PP(1);
1918  bool result;
1919 
1920  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1921 
1922  PG_FREE_IF_COPY(arg1, 0);
1923  PG_FREE_IF_COPY(arg2, 1);
1924 
1925  PG_RETURN_BOOL(result);
1926 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1757
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:609

◆ text_isequal()

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

Definition at line 4759 of file varlena.c.

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

Referenced by split_text_accum_result().

4760 {
4762  collid,
4763  PointerGetDatum(txt1),
4764  PointerGetDatum(txt2)));
4765 }
#define PointerGetDatum(X)
Definition: postgres.h:556
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1782
#define DatumGetBool(X)
Definition: postgres.h:393
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:814

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2866 of file varlena.c.

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

2867 {
2868  text *arg1 = PG_GETARG_TEXT_PP(0);
2869  text *arg2 = PG_GETARG_TEXT_PP(1);
2870  text *result;
2871 
2872  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2873 
2874  PG_RETURN_TEXT_P(result);
2875 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1757
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:609

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1899 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().

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

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 5540 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.

5541 {
5542  int n = PG_GETARG_INT32(1);
5543 
5544  if (n < 0)
5545  {
5546  text *str = PG_GETARG_TEXT_PP(0);
5547  const char *p = VARDATA_ANY(str);
5548  int len = VARSIZE_ANY_EXHDR(str);
5549  int rlen;
5550 
5551  n = pg_mbstrlen_with_len(p, len) + n;
5552  rlen = pg_mbcharcliplen(p, len, n);
5554  }
5555  else
5557 }
#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:309
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:871
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 697 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().

698 {
699  /* fastpath when max encoding length is one */
702  else
703  {
704  text *t = DatumGetTextPP(str);
705 
707  VARSIZE_ANY_EXHDR(t)));
708  }
709 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARHDRSZ
Definition: c.h:615
#define DatumGetTextPP(X)
Definition: fmgr.h:292
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:941
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:521
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1436
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:609

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1884 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().

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

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3643 of file varlena.c.

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

3644 {
3645  text *s = PG_GETARG_TEXT_PP(0);
3646  Name result;
3647  int len;
3648 
3649  len = VARSIZE_ANY_EXHDR(s);
3650 
3651  /* Truncate oversize input */
3652  if (len >= NAMEDATALEN)
3653  len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3654 
3655  /* We use palloc0 here to ensure result is zero-padded */
3656  result = (Name) palloc0(NAMEDATALEN);
3657  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3658 
3659  PG_RETURN_NAME(result);
3660 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:967
Definition: c.h:663
void * palloc0(Size size)
Definition: mcxt.c:981
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:669
Definition: c.h:609
NameData * Name
Definition: c.h:667
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363

◆ text_overlay()

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

Definition at line 1101 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().

1102 {
1103  text *result;
1104  text *s1;
1105  text *s2;
1106  int sp_pl_sl;
1107 
1108  /*
1109  * Check for possible integer-overflow cases. For negative sp, throw a
1110  * "substring length" error because that's what should be expected
1111  * according to the spec's definition of OVERLAY().
1112  */
1113  if (sp <= 0)
1114  ereport(ERROR,
1115  (errcode(ERRCODE_SUBSTRING_ERROR),
1116  errmsg("negative substring length not allowed")));
1117  if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1118  ereport(ERROR,
1119  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1120  errmsg("integer out of range")));
1121 
1122  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1123  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1124  result = text_catenate(s1, t2);
1125  result = text_catenate(result, s2);
1126 
1127  return result;
1128 }
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
char * s1
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:871
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:751
char * s2
#define ereport(elevel,...)
Definition: elog.h:155
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 3146 of file varlena.c.

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

3147 {
3148  text *arg1 = PG_GETARG_TEXT_PP(0);
3149  text *arg2 = PG_GETARG_TEXT_PP(1);
3150  int result;
3151 
3152  result = internal_text_pattern_compare(arg1, arg2);
3153 
3154  PG_FREE_IF_COPY(arg1, 0);
3155  PG_FREE_IF_COPY(arg2, 1);
3156 
3157  PG_RETURN_BOOL(result >= 0);
3158 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definiti