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

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 
struct  SplitTextOutputData
 

Macros

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

Typedefs

typedef struct varlena unknown
 
typedef struct varlena VarString
 

Functions

static int varstrfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int bpcharfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int varlenafastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int varstrfastcmp_locale (char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
 
static 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 bytea_bit_count (PG_FUNCTION_ARGS)
 
Datum byteapos (PG_FUNCTION_ARGS)
 
Datum byteaGetByte (PG_FUNCTION_ARGS)
 
Datum byteaGetBit (PG_FUNCTION_ARGS)
 
Datum byteaSetByte (PG_FUNCTION_ARGS)
 
Datum byteaSetBit (PG_FUNCTION_ARGS)
 
Datum text_name (PG_FUNCTION_ARGS)
 
Datum name_text (PG_FUNCTION_ARGS)
 
ListtextToQualifiedNameList (text *textval)
 
bool SplitIdentifierString (char *rawstring, char separator, List **namelist)
 
bool SplitDirectoriesString (char *rawstring, char separator, List **namelist)
 
bool SplitGUCList (char *rawstring, char separator, List **namelist)
 
Datum byteaeq (PG_FUNCTION_ARGS)
 
Datum byteane (PG_FUNCTION_ARGS)
 
Datum bytealt (PG_FUNCTION_ARGS)
 
Datum byteale (PG_FUNCTION_ARGS)
 
Datum byteagt (PG_FUNCTION_ARGS)
 
Datum byteage (PG_FUNCTION_ARGS)
 
Datum byteacmp (PG_FUNCTION_ARGS)
 
Datum bytea_sortsupport (PG_FUNCTION_ARGS)
 
Datum replace_text (PG_FUNCTION_ARGS)
 
static int check_replace_text_has_escape (const text *replace_text)
 
static void appendStringInfoRegexpSubstr (StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
 
textreplace_text_regexp (text *src_text, text *pattern_text, text *replace_text, int cflags, Oid collation, int search_start, int n)
 
Datum split_part (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2, Oid collid)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_null (PG_FUNCTION_ARGS)
 
Datum text_to_table (PG_FUNCTION_ARGS)
 
Datum text_to_table_null (PG_FUNCTION_ARGS)
 
Datum array_to_text (PG_FUNCTION_ARGS)
 
Datum array_to_text_null (PG_FUNCTION_ARGS)
 
Datum to_hex32 (PG_FUNCTION_ARGS)
 
Datum to_hex64 (PG_FUNCTION_ARGS)
 
Datum pg_column_size (PG_FUNCTION_ARGS)
 
Datum pg_column_compression (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 
static bool isxdigits_n (const char *instr, size_t n)
 
static unsigned int hexval (unsigned char c)
 
static unsigned int hexval_n (const char *instr, size_t n)
 
Datum unistr (PG_FUNCTION_ARGS)
 

Variables

int bytea_output = BYTEA_OUTPUT_HEX
 

Macro Definition Documentation

◆ ADVANCE_PARSE_POINTER

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

Definition at line 5711 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:516
#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:811

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

Referenced by to_hex32(), and to_hex64().

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

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

Referenced by bytea_substring().

◆ TEXT_FORMAT_FLAG_MINUS

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

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

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

Referenced by replace_text_regexp().

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

4272 {
4274 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

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

5078 {
5080  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5081 
5082  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
5083 }
#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:5119
#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 5119 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().

5121 {
5122  text *result;
5123  int nitems,
5124  *dims,
5125  ndims;
5126  Oid element_type;
5127  int typlen;
5128  bool typbyval;
5129  char typalign;
5131  bool printed = false;
5132  char *p;
5133  bits8 *bitmap;
5134  int bitmask;
5135  int i;
5136  ArrayMetaState *my_extra;
5137 
5138  ndims = ARR_NDIM(v);
5139  dims = ARR_DIMS(v);
5140  nitems = ArrayGetNItems(ndims, dims);
5141 
5142  /* if there are no elements, return an empty string */
5143  if (nitems == 0)
5144  return cstring_to_text_with_len("", 0);
5145 
5146  element_type = ARR_ELEMTYPE(v);
5147  initStringInfo(&buf);
5148 
5149  /*
5150  * We arrange to look up info about element type, including its output
5151  * conversion proc, only once per series of calls, assuming the element
5152  * type doesn't change underneath us.
5153  */
5154  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5155  if (my_extra == NULL)
5156  {
5157  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5158  sizeof(ArrayMetaState));
5159  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5160  my_extra->element_type = ~element_type;
5161  }
5162 
5163  if (my_extra->element_type != element_type)
5164  {
5165  /*
5166  * Get info about element type, including its output conversion proc
5167  */
5168  get_type_io_data(element_type, IOFunc_output,
5169  &my_extra->typlen, &my_extra->typbyval,
5170  &my_extra->typalign, &my_extra->typdelim,
5171  &my_extra->typioparam, &my_extra->typiofunc);
5172  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
5173  fcinfo->flinfo->fn_mcxt);
5174  my_extra->element_type = element_type;
5175  }
5176  typlen = my_extra->typlen;
5177  typbyval = my_extra->typbyval;
5178  typalign = my_extra->typalign;
5179 
5180  p = ARR_DATA_PTR(v);
5181  bitmap = ARR_NULLBITMAP(v);
5182  bitmask = 1;
5183 
5184  for (i = 0; i < nitems; i++)
5185  {
5186  Datum itemvalue;
5187  char *value;
5188 
5189  /* Get source element, checking for NULL */
5190  if (bitmap && (*bitmap & bitmask) == 0)
5191  {
5192  /* if null_string is NULL, we just ignore null elements */
5193  if (null_string != NULL)
5194  {
5195  if (printed)
5196  appendStringInfo(&buf, "%s%s", fldsep, null_string);
5197  else
5198  appendStringInfoString(&buf, null_string);
5199  printed = true;
5200  }
5201  }
5202  else
5203  {
5204  itemvalue = fetch_att(p, typbyval, typlen);
5205 
5206  value = OutputFunctionCall(&my_extra->proc, itemvalue);
5207 
5208  if (printed)
5209  appendStringInfo(&buf, "%s%s", fldsep, value);
5210  else
5211  appendStringInfoString(&buf, value);
5212  printed = true;
5213 
5214  p = att_addlength_pointer(p, typlen, p);
5215  p = (char *) att_align_nominal(p, typalign);
5216  }
5217 
5218  /* advance bitmap pointer if any */
5219  if (bitmap)
5220  {
5221  bitmask <<= 1;
5222  if (bitmask == 0x100)
5223  {
5224  bitmap++;
5225  bitmask = 1;
5226  }
5227  }
5228  }
5229 
5230  result = cstring_to_text_with_len(buf.data, buf.len);
5231  pfree(buf.data);
5232 
5233  return result;
5234 }
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:76
unsigned int Oid
Definition: postgres_ext.h:31
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1573
bool typbyval
Definition: array.h:233
void pfree(void *pointer)
Definition: mcxt.c:1169
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:448
uintptr_t Datum
Definition: postgres.h:411
FmgrInfo * flinfo
Definition: fmgr.h:87
static struct @143 value
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:863
int i
Oid element_type
Definition: array.h:231
Definition: c.h:621
#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:2272

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

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

5094 {
5095  ArrayType *v;
5096  char *fldsep;
5097  char *null_string;
5098 
5099  /* returns NULL when first or second parameter is NULL */
5100  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
5101  PG_RETURN_NULL();
5102 
5103  v = PG_GETARG_ARRAYTYPE_P(0);
5104  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5105 
5106  /* NULL null string is passed through as a null pointer */
5107  if (!PG_ARGISNULL(2))
5108  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
5109  else
5110  null_string = NULL;
5111 
5112  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
5113 }
#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:5119
#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 2219 of file varlena.c.

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

Referenced by varstr_sortsupport().

2220 {
2221  BpChar *arg1 = DatumGetBpCharPP(x);
2222  BpChar *arg2 = DatumGetBpCharPP(y);
2223  char *a1p,
2224  *a2p;
2225  int len1,
2226  len2,
2227  result;
2228 
2229  a1p = VARDATA_ANY(arg1);
2230  a2p = VARDATA_ANY(arg2);
2231 
2232  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2233  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2234 
2235  result = memcmp(a1p, a2p, Min(len1, len2));
2236  if ((result == 0) && (len1 != len2))
2237  result = (len1 < len2) ? -1 : 1;
2238 
2239  /* We can't afford to leak memory here. */
2240  if (PointerGetDatum(arg1) != x)
2241  pfree(arg1);
2242  if (PointerGetDatum(arg2) != y)
2243  pfree(arg2);
2244 
2245  return result;
2246 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PointerGetDatum(X)
Definition: postgres.h:600
#define Min(x, y)
Definition: c.h:986
void pfree(void *pointer)
Definition: mcxt.c:1169
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:354
Definition: c.h:621

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

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

2990 {
2991  Name arg1 = PG_GETARG_NAME(0);
2992  text *arg2 = PG_GETARG_TEXT_PP(1);
2993  int32 result;
2994 
2995  result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2996  VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2997  PG_GET_COLLATION());
2998 
2999  PG_FREE_IF_COPY(arg2, 1);
3000 
3001  PG_RETURN_INT32(result);
3002 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:429
#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:1535
Definition: c.h:675
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 3172 of file varlena.c.

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

3173 {
3174  text *arg1 = PG_GETARG_TEXT_PP(0);
3175  text *arg2 = PG_GETARG_TEXT_PP(1);
3176  int result;
3177 
3178  result = internal_text_pattern_compare(arg1, arg2);
3179 
3180  PG_FREE_IF_COPY(arg1, 0);
3181  PG_FREE_IF_COPY(arg2, 1);
3182 
3183  PG_RETURN_INT32(result);
3184 }
#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:3086
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:621

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3188 of file varlena.c.

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

3189 {
3191  MemoryContext oldcontext;
3192 
3193  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3194 
3195  /* Use generic string SortSupport, forcing "C" collation */
3196  varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
3197 
3198  MemoryContextSwitchTo(oldcontext);
3199 
3200  PG_RETURN_VOID();
3201 }
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:2020
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

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

1979 {
1980  text *arg1 = PG_GETARG_TEXT_PP(0);
1981  text *arg2 = PG_GETARG_TEXT_PP(1);
1982  int32 result;
1983 
1984  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1985 
1986  PG_FREE_IF_COPY(arg1, 0);
1987  PG_FREE_IF_COPY(arg2, 1);
1988 
1989  PG_RETURN_INT32(result);
1990 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1751
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:621

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

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

3006 {
3007  text *arg1 = PG_GETARG_TEXT_PP(0);
3008  Name arg2 = PG_GETARG_NAME(1);
3009  int32 result;
3010 
3011  result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
3012  NameStr(*arg2), strlen(NameStr(*arg2)),
3013  PG_GET_COLLATION());
3014 
3015  PG_FREE_IF_COPY(arg1, 0);
3016 
3017  PG_RETURN_INT32(result);
3018 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:429
#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:1535
Definition: c.h:675
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1993 of file varlena.c.

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

1994 {
1996  Oid collid = ssup->ssup_collation;
1997  MemoryContext oldcontext;
1998 
1999  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2000 
2001  /* Use generic string SortSupport */
2002  varstr_sortsupport(ssup, TEXTOID, collid);
2003 
2004  MemoryContextSwitchTo(oldcontext);
2005 
2006  PG_RETURN_VOID();
2007 }
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:2020
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2844 of file varlena.c.

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

2845 {
2846  /* Oid opcintype = PG_GETARG_OID(0); */
2847  Oid collid = PG_GET_COLLATION();
2848 
2849  check_collation_set(collid);
2850 
2851  if (lc_collate_is_c(collid) ||
2852  collid == DEFAULT_COLLATION_OID ||
2854  PG_RETURN_BOOL(true);
2855  else
2856  PG_RETURN_BOOL(false);
2857 }
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:1079
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:1321
static void check_collation_set(Oid collid)
Definition: varlena.c:1506
#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 5470 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().

5471 {
5472  FmgrInfo *foutcache;
5473  int i;
5474 
5475  /* We keep the info in fn_mcxt so it survives across calls */
5476  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5477  PG_NARGS() * sizeof(FmgrInfo));
5478 
5479  for (i = argidx; i < PG_NARGS(); i++)
5480  {
5481  Oid valtype;
5482  Oid typOutput;
5483  bool typIsVarlena;
5484 
5485  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5486  if (!OidIsValid(valtype))
5487  elog(ERROR, "could not determine data type of concat() input");
5488 
5489  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5490  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5491  }
5492 
5493  fcinfo->flinfo->fn_extra = foutcache;
5494 
5495  return foutcache;
5496 }
Definition: fmgr.h:56
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
MemoryContext fn_mcxt
Definition: fmgr.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
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:863
#define elog(elevel,...)
Definition: elog.h:232
int i

◆ bytea_bit_count()

Datum bytea_bit_count ( PG_FUNCTION_ARGS  )

Definition at line 3440 of file varlena.c.

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

3441 {
3442  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3443 
3445 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:296
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ bytea_catenate()

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

Definition at line 3242 of file varlena.c.

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

Referenced by bytea_overlay(), and byteacat().

3243 {
3244  bytea *result;
3245  int len1,
3246  len2,
3247  len;
3248  char *ptr;
3249 
3250  len1 = VARSIZE_ANY_EXHDR(t1);
3251  len2 = VARSIZE_ANY_EXHDR(t2);
3252 
3253  /* paranoia ... probably should throw error instead? */
3254  if (len1 < 0)
3255  len1 = 0;
3256  if (len2 < 0)
3257  len2 = 0;
3258 
3259  len = len1 + len2 + VARHDRSZ;
3260  result = (bytea *) palloc(len);
3261 
3262  /* Set size of result string... */
3263  SET_VARSIZE(result, len);
3264 
3265  /* Fill data field of result string... */
3266  ptr = VARDATA(result);
3267  if (len1 > 0)
3268  memcpy(ptr, VARDATA_ANY(t1), len1);
3269  if (len2 > 0)
3270  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
3271 
3272  return result;
3273 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ bytea_overlay()

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

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

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

◆ bytea_sortsupport()

Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 4249 of file varlena.c.

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

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

◆ bytea_string_agg_finalfn()

Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

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

534 {
536 
537  /* cannot be called directly because of internal-type argument */
538  Assert(AggCheckCallContext(fcinfo, NULL));
539 
540  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
541 
542  if (state != NULL)
543  {
544  bytea *result;
545 
546  result = (bytea *) palloc(state->len + VARHDRSZ);
547  SET_VARSIZE(result, state->len + VARHDRSZ);
548  memcpy(VARDATA(result), state->data, state->len);
549  PG_RETURN_BYTEA_P(result);
550  }
551  else
552  PG_RETURN_NULL();
553 }
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
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:80
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:804
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4495
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ bytea_string_agg_transfn()

Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

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

502 {
504 
505  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
506 
507  /* Append the value unless null. */
508  if (!PG_ARGISNULL(1))
509  {
511 
512  /* On the first time through, we ignore the delimiter. */
513  if (state == NULL)
514  state = makeStringAggState(fcinfo);
515  else if (!PG_ARGISNULL(2))
516  {
517  bytea *delim = PG_GETARG_BYTEA_PP(2);
518 
520  }
521 
523  }
524 
525  /*
526  * The transition type for string_agg() is declared to be "internal",
527  * which is a pass-by-value type the same size as a pointer.
528  */
529  PG_RETURN_POINTER(state);
530 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
ts_parserstate state
Definition: tsquery.c:80
static struct @143 value
#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:354
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5398
Definition: c.h:621
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ bytea_substr()

Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 3294 of file varlena.c.

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

3295 {
3297  PG_GETARG_INT32(1),
3298  PG_GETARG_INT32(2),
3299  false));
3300 }
#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:3317

◆ bytea_substr_no_len()

Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3308 of file varlena.c.

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

3309 {
3311  PG_GETARG_INT32(1),
3312  -1,
3313  true));
3314 }
#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:3317

◆ bytea_substring()

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

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

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

◆ byteacat()

Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 3227 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

3228 {
3229  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3230  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3231 
3233 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:3242
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:621

◆ byteacmp()

Datum byteacmp ( PG_FUNCTION_ARGS  )

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

4228 {
4229  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4230  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4231  int len1,
4232  len2;
4233  int cmp;
4234 
4235  len1 = VARSIZE_ANY_EXHDR(arg1);
4236  len2 = VARSIZE_ANY_EXHDR(arg2);
4237 
4238  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4239  if ((cmp == 0) && (len1 != len2))
4240  cmp = (len1 < len2) ? -1 : 1;
4241 
4242  PG_FREE_IF_COPY(arg1, 0);
4243  PG_FREE_IF_COPY(arg2, 1);
4244 
4245  PG_RETURN_INT32(cmp);
4246 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#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:354
Definition: c.h:621
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ byteaeq()

Datum byteaeq ( PG_FUNCTION_ARGS  )

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

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

◆ byteage()

Datum byteage ( PG_FUNCTION_ARGS  )

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

4208 {
4209  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4210  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4211  int len1,
4212  len2;
4213  int cmp;
4214 
4215  len1 = VARSIZE_ANY_EXHDR(arg1);
4216  len2 = VARSIZE_ANY_EXHDR(arg2);
4217 
4218  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4219 
4220  PG_FREE_IF_COPY(arg1, 0);
4221  PG_FREE_IF_COPY(arg2, 1);
4222 
4223  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
4224 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#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:354
Definition: c.h:621
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ byteaGetBit()

Datum byteaGetBit ( PG_FUNCTION_ARGS  )

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

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

◆ byteaGetByte()

Datum byteaGetByte ( PG_FUNCTION_ARGS  )

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

3499 {
3500  bytea *v = PG_GETARG_BYTEA_PP(0);
3501  int32 n = PG_GETARG_INT32(1);
3502  int len;
3503  int byte;
3504 
3505  len = VARSIZE_ANY_EXHDR(v);
3506 
3507  if (n < 0 || n >= len)
3508  ereport(ERROR,
3509  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3510  errmsg("index %d out of valid range, 0..%d",
3511  n, len - 1)));
3512 
3513  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3514 
3515  PG_RETURN_INT32(byte);
3516 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define byte(x, n)
Definition: rijndael.c:68
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621

◆ byteagt()

Datum byteagt ( PG_FUNCTION_ARGS  )

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

4188 {
4189  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4190  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4191  int len1,
4192  len2;
4193  int cmp;
4194 
4195  len1 = VARSIZE_ANY_EXHDR(arg1);
4196  len2 = VARSIZE_ANY_EXHDR(arg2);
4197 
4198  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4199 
4200  PG_FREE_IF_COPY(arg1, 0);
4201  PG_FREE_IF_COPY(arg2, 1);
4202 
4203  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
4204 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#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:354
Definition: c.h:621
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ byteain()

Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 295 of file varlena.c.

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

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

◆ byteale()

Datum byteale ( PG_FUNCTION_ARGS  )

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

4168 {
4169  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4170  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4171  int len1,
4172  len2;
4173  int cmp;
4174 
4175  len1 = VARSIZE_ANY_EXHDR(arg1);
4176  len2 = VARSIZE_ANY_EXHDR(arg2);
4177 
4178  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4179 
4180  PG_FREE_IF_COPY(arg1, 0);
4181  PG_FREE_IF_COPY(arg2, 1);
4182 
4183  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
4184 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#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:354
Definition: c.h:621
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ bytealt()

Datum bytealt ( PG_FUNCTION_ARGS  )

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

4148 {
4149  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4150  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4151  int len1,
4152  len2;
4153  int cmp;
4154 
4155  len1 = VARSIZE_ANY_EXHDR(arg1);
4156  len2 = VARSIZE_ANY_EXHDR(arg2);
4157 
4158  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4159 
4160  PG_FREE_IF_COPY(arg1, 0);
4161  PG_FREE_IF_COPY(arg2, 1);
4162 
4163  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
4164 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#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:354
Definition: c.h:621
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ byteane()

Datum byteane ( PG_FUNCTION_ARGS  )

Definition at line 4115 of file varlena.c.

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

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

◆ byteaoctetlen()

Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 3211 of file varlena.c.

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

3212 {
3213  Datum str = PG_GETARG_DATUM(0);
3214 
3215  /* We need not detoast the input at all */
3217 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define VARHDRSZ
Definition: c.h:627
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:545
uintptr_t Datum
Definition: postgres.h:411

◆ byteaout()

Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 391 of file varlena.c.

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

Referenced by pg_mcv_list_out().

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

◆ byteaoverlay()

Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 3384 of file varlena.c.

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

3385 {
3386  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3387  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3388  int sp = PG_GETARG_INT32(2); /* substring start position */
3389  int sl = PG_GETARG_INT32(3); /* substring length */
3390 
3391  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3392 }
#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:3407
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:621

◆ byteaoverlay_no_len()

Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 3395 of file varlena.c.

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

3396 {
3397  bytea *t1 = PG_GETARG_BYTEA_PP(0);
3398  bytea *t2 = PG_GETARG_BYTEA_PP(1);
3399  int sp = PG_GETARG_INT32(2); /* substring start position */
3400  int sl;
3401 
3402  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3403  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3404 }
#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:3407
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ byteapos()

Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 3454 of file varlena.c.

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

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

◆ bytearecv()

Datum bytearecv ( PG_FUNCTION_ARGS  )

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

475 {
477  bytea *result;
478  int nbytes;
479 
480  nbytes = buf->len - buf->cursor;
481  result = (bytea *) palloc(nbytes + VARHDRSZ);
482  SET_VARSIZE(result, nbytes + VARHDRSZ);
483  pq_copymsgbytes(buf, VARDATA(result), nbytes);
484  PG_RETURN_BYTEA_P(result);
485 }
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
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:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ byteasend()

Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 493 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

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

494 {
495  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
496 
497  PG_RETURN_BYTEA_P(vlena);
498 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
Definition: c.h:621

◆ byteaSetBit()

Datum byteaSetBit ( PG_FUNCTION_ARGS  )

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

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

◆ byteaSetByte()

Datum byteaSetByte ( PG_FUNCTION_ARGS  )

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

3566 {
3567  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3568  int32 n = PG_GETARG_INT32(1);
3569  int32 newByte = PG_GETARG_INT32(2);
3570  int len;
3571 
3572  len = VARSIZE(res) - VARHDRSZ;
3573 
3574  if (n < 0 || n >= len)
3575  ereport(ERROR,
3576  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3577  errmsg("index %d out of valid range, 0..%d",
3578  n, len - 1)));
3579 
3580  /*
3581  * Now set the byte.
3582  */
3583  ((unsigned char *) VARDATA(res))[n] = newByte;
3584 
3585  PG_RETURN_BYTEA_P(res);
3586 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARSIZE(PTR)
Definition: postgres.h:316
#define VARHDRSZ
Definition: c.h:627
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621

◆ charlen_to_bytelen()

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

Definition at line 786 of file varlena.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

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

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

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

1507 {
1508  if (!OidIsValid(collid))
1509  {
1510  /*
1511  * This typically means that the parser could not resolve a conflict
1512  * of implicit collations, so report it that way.
1513  */
1514  ereport(ERROR,
1515  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1516  errmsg("could not determine which collation to use for string comparison"),
1517  errhint("Use the COLLATE clause to set the collation explicitly.")));
1518  }
1519 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ check_replace_text_has_escape()

static int check_replace_text_has_escape ( const text replace_text)
static

Definition at line 4362 of file varlena.c.

References VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by replace_text_regexp().

4363 {
4364  int result = 0;
4365  const char *p = VARDATA_ANY(replace_text);
4366  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4367 
4368  while (p < p_end)
4369  {
4370  /* Find next escape char, if any. */
4371  p = memchr(p, '\\', p_end - p);
4372  if (p == NULL)
4373  break;
4374  p++;
4375  /* Note: a backslash at the end doesn't require extra processing. */
4376  if (p < p_end)
4377  {
4378  if (*p >= '1' && *p <= '9')
4379  return 2; /* Found a submatch specifier, so done */
4380  result = 1; /* Found some other sequence, keep looking */
4381  p++;
4382  }
4383  }
4384  return result;
4385 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354

◆ concat_internal()

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

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

5510 {
5511  text *result;
5513  FmgrInfo *foutcache;
5514  bool first_arg = true;
5515  int i;
5516 
5517  /*
5518  * concat(VARIADIC some-array) is essentially equivalent to
5519  * array_to_text(), ie concat the array elements with the given separator.
5520  * So we just pass the case off to that code.
5521  */
5522  if (get_fn_expr_variadic(fcinfo->flinfo))
5523  {
5524  ArrayType *arr;
5525 
5526  /* Should have just the one argument */
5527  Assert(argidx == PG_NARGS() - 1);
5528 
5529  /* concat(VARIADIC NULL) is defined as NULL */
5530  if (PG_ARGISNULL(argidx))
5531  return NULL;
5532 
5533  /*
5534  * Non-null argument had better be an array. We assume that any call
5535  * context that could let get_fn_expr_variadic return true will have
5536  * checked that a VARIADIC-labeled parameter actually is an array. So
5537  * it should be okay to just Assert that it's an array rather than
5538  * doing a full-fledged error check.
5539  */
5541 
5542  /* OK, safe to fetch the array value */
5543  arr = PG_GETARG_ARRAYTYPE_P(argidx);
5544 
5545  /*
5546  * And serialize the array. We tell array_to_text to ignore null
5547  * elements, which matches the behavior of the loop below.
5548  */
5549  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5550  }
5551 
5552  /* Normal case without explicit VARIADIC marker */
5553  initStringInfo(&str);
5554 
5555  /* Get output function info, building it if first time through */
5556  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5557  if (foutcache == NULL)
5558  foutcache = build_concat_foutcache(fcinfo, argidx);
5559 
5560  for (i = argidx; i < PG_NARGS(); i++)
5561  {
5562  if (!PG_ARGISNULL(i))
5563  {
5565 
5566  /* add separator if appropriate */
5567  if (first_arg)
5568  first_arg = false;
5569  else
5570  appendStringInfoString(&str, sepstr);
5571 
5572  /* call the appropriate type output function, append the result */
5574  OutputFunctionCall(&foutcache[i], value));
5575  }
5576  }
5577 
5578  result = cstring_to_text_with_len(str.data, str.len);
5579  pfree(str.data);
5580 
5581  return result;
5582 }
Definition: fmgr.h:56
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1934
#define OidIsValid(objectId)
Definition: c.h:710
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1573
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
void pfree(void *pointer)
Definition: mcxt.c:1169
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:5119
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:411
FmgrInfo * flinfo
Definition: fmgr.h:87
static struct @143 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:804
#define PG_NARGS()
Definition: fmgr.h:203
void * fn_extra
Definition: fmgr.h:64
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2779
int i
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:5470
Definition: c.h:621

◆ 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_minmax_multi_summary_out(), brin_page_type(), build_test_info_result(), build_test_match_result(), cash_words(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), executeDateTimeMethod(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_scalar(), hash_page_type(), hstore_to_json(), hstore_to_json_loose(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_get_element(), jsonb_typeof(), JsonbValueAsText(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), pg_collation_actual_version(), pg_collation_for(), pg_column_compression(), pg_crypt(), pg_current_logfile(), pg_describe_object(), pg_export_snapshot(), pg_get_function_sqlbody(), pg_get_statisticsobjdef_expressions(), pg_get_wal_replay_pause_state(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_stat_get_slru(), pg_stats_ext_mcvlist_items(), pg_tablespace_location(), pg_walfile_name(), pg_xact_status(), pgsql_version(), pgxml_result_to_text(), quote_ident(), quote_ident_cstr(), quote_nullable(), sepgsql_getcon(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), set_config_by_name(), show_all_file_settings(), show_config_by_name(), show_config_by_name_missing_ok(), ShowAllGUCConfig(), soundex(), 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(), unistr(), 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:315
#define VARHDRSZ
Definition: c.h:627
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ hexval()

static unsigned int hexval ( unsigned char  c)
static

Definition at line 6417 of file varlena.c.

References elog, and ERROR.

Referenced by hexval_n().

6418 {
6419  if (c >= '0' && c <= '9')
6420  return c - '0';
6421  if (c >= 'a' && c <= 'f')
6422  return c - 'a' + 0xA;
6423  if (c >= 'A' && c <= 'F')
6424  return c - 'A' + 0xA;
6425  elog(ERROR, "invalid hexadecimal digit");
6426  return 0; /* not reached */
6427 }
#define ERROR
Definition: elog.h:46
char * c
#define elog(elevel,...)
Definition: elog.h:232

◆ hexval_n()

static unsigned int hexval_n ( const char *  instr,
size_t  n 
)
static

Definition at line 6433 of file varlena.c.

References hexval(), and i.

Referenced by unistr().

6434 {
6435  unsigned int result = 0;
6436 
6437  for (size_t i = 0; i < n; i++)
6438  result += hexval(instr[i]) << (4 * (n - i - 1));
6439 
6440  return result;
6441 }
static unsigned int hexval(unsigned char c)
Definition: varlena.c:6417
int i

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

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

3087 {
3088  int result;
3089  int len1,
3090  len2;
3091 
3092  len1 = VARSIZE_ANY_EXHDR(arg1);
3093  len2 = VARSIZE_ANY_EXHDR(arg2);
3094 
3095  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3096  if (result != 0)
3097  return result;
3098  else if (len1 < len2)
3099  return -1;
3100  else if (len1 > len2)
3101  return 1;
3102  else
3103  return 0;
3104 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define Min(x, y)
Definition: c.h:986
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354

◆ isxdigits_n()

static bool isxdigits_n ( const char *  instr,
size_t  n 
)
static

Definition at line 6407 of file varlena.c.

References i.

Referenced by unistr().

6408 {
6409  for (size_t i = 0; i < n; i++)
6410  if (!isxdigit((unsigned char) instr[i]))
6411  return false;
6412 
6413  return true;
6414 }
int i

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 5398 of file varlena.c.

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

5399 {
5400  StringInfo state;
5401  MemoryContext aggcontext;
5402  MemoryContext oldcontext;
5403 
5404  if (!AggCheckCallContext(fcinfo, &aggcontext))
5405  {
5406  /* cannot be called directly because of internal-type argument */
5407  elog(ERROR, "string_agg_transfn called in non-aggregate context");
5408  }
5409 
5410  /*
5411  * Create state in aggregate context. It'll stay there across subsequent
5412  * calls.
5413  */
5414  oldcontext = MemoryContextSwitchTo(aggcontext);
5415  state = makeStringInfo();
5416  MemoryContextSwitchTo(oldcontext);
5417 
5418  return state;
5419 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:46
ts_parserstate state
Definition: tsquery.c:80
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4495
#define elog(elevel,...)
Definition: elog.h:232

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3671 of file varlena.c.

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

Referenced by nameiclike(), and nameicnlike().

3672 {
3673  Name s = PG_GETARG_NAME(0);
3674 
3676 }
Definition: c.h:675
#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:681
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

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

2890 {
2891  Name arg1 = PG_GETARG_NAME(0);
2892  text *arg2 = PG_GETARG_TEXT_PP(1);
2893  size_t len1 = strlen(NameStr(*arg1));
2894  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2895  Oid collid = PG_GET_COLLATION();
2896  bool result;
2897 
2898  check_collation_set(collid);
2899 
2900  if (collid == C_COLLATION_OID)
2901  result = (len1 == len2 &&
2902  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2903  else
2904  result = (varstr_cmp(NameStr(*arg1), len1,
2905  VARDATA_ANY(arg2), len2,
2906  collid) == 0);
2907 
2908  PG_FREE_IF_COPY(arg2, 1);
2909 
2910  PG_RETURN_BOOL(result);
2911 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
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:1535
static void check_collation_set(Oid collid)
Definition: varlena.c:1506
Definition: c.h:675
#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:354
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#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 2252 of file varlena.c.

References DatumGetName, NAMEDATALEN, and NameStr.

Referenced by varstr_sortsupport().

2253 {
2254  Name arg1 = DatumGetName(x);
2255  Name arg2 = DatumGetName(y);
2256 
2257  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2258 }
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:629
Definition: c.h:675
#define NameStr(name)
Definition: c.h:681

◆ namefastcmp_locale()

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

Definition at line 2295 of file varlena.c.

References DatumGetName, NameStr, and varstrfastcmp_locale().

Referenced by varstr_sortsupport().

2296 {
2297  Name arg1 = DatumGetName(x);
2298  Name arg2 = DatumGetName(y);
2299 
2300  return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2301  NameStr(*arg2), strlen(NameStr(*arg2)),
2302  ssup);
2303 }
#define DatumGetName(X)
Definition: postgres.h:629
Definition: c.h:675
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition: varlena.c:2309
#define NameStr(name)
Definition: c.h:681

◆ namegetext()

Datum namegetext ( 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:3020
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2989

◆ namegttext()

Datum namegttext ( 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:3020
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2989

◆ nameletext()

Datum nameletext ( 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:3020
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2989

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 3027 of file varlena.c.

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

3028 {
3030 }
#define CmpCall(cmpfunc)
Definition: varlena.c:3020
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition: varlena.c:2989

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

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

2940 {
2941  Name arg1 = PG_GETARG_NAME(0);
2942  text *arg2 = PG_GETARG_TEXT_PP(1);
2943  size_t len1 = strlen(NameStr(*arg1));
2944  size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2945  Oid collid = PG_GET_COLLATION();
2946  bool result;
2947 
2948  check_collation_set(collid);
2949 
2950  if (collid == C_COLLATION_OID)
2951  result = !(len1 == len2 &&
2952  memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2953  else
2954  result = !(varstr_cmp(NameStr(*arg1), len1,
2955  VARDATA_ANY(arg2), len2,
2956  collid) == 0);
2957 
2958  PG_FREE_IF_COPY(arg2, 1);
2959 
2960  PG_RETURN_BOOL(result);
2961 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
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:1535
static void check_collation_set(Oid collid)
Definition: varlena.c:1506
Definition: c.h:675
#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:354
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_column_compression()

Datum pg_column_compression ( PG_FUNCTION_ARGS  )

Definition at line 5338 of file varlena.c.

References cstring_to_text(), DatumGetPointer, elog, ERROR, get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_TEXT_P, toast_get_compression_id(), TOAST_INVALID_COMPRESSION_ID, TOAST_LZ4_COMPRESSION_ID, and TOAST_PGLZ_COMPRESSION_ID.

5339 {
5340  int typlen;
5341  char *result;
5342  ToastCompressionId cmid;
5343 
5344  /* On first call, get the input type's typlen, and save at *fn_extra */
5345  if (fcinfo->flinfo->fn_extra == NULL)
5346  {
5347  /* Lookup the datatype of the supplied argument */
5348  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5349 
5350  typlen = get_typlen(argtypeid);
5351  if (typlen == 0) /* should not happen */
5352  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5353 
5354  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5355  sizeof(int));
5356  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5357  }
5358  else
5359  typlen = *((int *) fcinfo->flinfo->fn_extra);
5360 
5361  if (typlen != -1)
5362  PG_RETURN_NULL();
5363 
5364  /* get the compression method id stored in the compressed varlena */
5365  cmid = toast_get_compression_id((struct varlena *)
5367  if (cmid == TOAST_INVALID_COMPRESSION_ID)
5368  PG_RETURN_NULL();
5369 
5370  /* convert compression method id to compression method name */
5371  switch (cmid)
5372  {
5374  result = "pglz";
5375  break;
5377  result = "lz4";
5378  break;
5379  default:
5380  elog(ERROR, "invalid compression method id %d", cmid);
5381  }
5382 
5384 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
ToastCompressionId
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
#define DatumGetPointer(X)
Definition: postgres.h:593
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2144
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621
ToastCompressionId toast_get_compression_id(struct varlena *attr)
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

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

5292 {
5294  int32 result;
5295  int typlen;
5296 
5297  /* On first call, get the input type's typlen, and save at *fn_extra */
5298  if (fcinfo->flinfo->fn_extra == NULL)
5299  {
5300  /* Lookup the datatype of the supplied argument */
5301  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5302 
5303  typlen = get_typlen(argtypeid);
5304  if (typlen == 0) /* should not happen */
5305  elog(ERROR, "cache lookup failed for type %u", argtypeid);
5306 
5307  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5308  sizeof(int));
5309  *((int *) fcinfo->flinfo->fn_extra) = typlen;
5310  }
5311  else
5312  typlen = *((int *) fcinfo->flinfo->fn_extra);
5313 
5314  if (typlen == -1)
5315  {
5316  /* varlena type, possibly toasted */
5317  result = toast_datum_size(value);
5318  }
5319  else if (typlen == -2)
5320  {
5321  /* cstring */
5322  result = strlen(DatumGetCString(value)) + 1;
5323  }
5324  else
5325  {
5326  /* ordinary fixed-width type */
5327  result = typlen;
5328  }
5329 
5330  PG_RETURN_INT32(result);
5331 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
Size toast_datum_size(Datum value)
Definition: detoast.c:601
#define ERROR
Definition: elog.h:46
#define DatumGetCString(X)
Definition: postgres.h:610
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
uintptr_t Datum
Definition: postgres.h:411
static struct @143 value
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2144
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

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

4286 {
4287  text *src_text = PG_GETARG_TEXT_PP(0);
4288  text *from_sub_text = PG_GETARG_TEXT_PP(1);
4289  text *to_sub_text = PG_GETARG_TEXT_PP(2);
4290  int src_text_len;
4291  int from_sub_text_len;
4293  text *ret_text;
4294  int chunk_len;
4295  char *curr_ptr;
4296  char *start_ptr;
4298  bool found;
4299 
4300  src_text_len = VARSIZE_ANY_EXHDR(src_text);
4301  from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4302 
4303  /* Return unmodified source string if empty source or pattern */
4304  if (src_text_len < 1 || from_sub_text_len < 1)
4305  {
4306  PG_RETURN_TEXT_P(src_text);
4307  }
4308 
4309  text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4310 
4311  found = text_position_next(&state);
4312 
4313  /* When the from_sub_text is not found, there is nothing to do. */
4314  if (!found)
4315  {
4316  text_position_cleanup(&state);
4317  PG_RETURN_TEXT_P(src_text);
4318  }
4319  curr_ptr = text_position_get_match_ptr(&state);
4320  start_ptr = VARDATA_ANY(src_text);
4321 
4322  initStringInfo(&str);
4323 
4324  do
4325  {
4327 
4328  /* copy the data skipped over by last text_position_next() */
4329  chunk_len = curr_ptr - start_ptr;
4330  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4331 
4332  appendStringInfoText(&str, to_sub_text);
4333 
4334  start_ptr = curr_ptr + from_sub_text_len;
4335 
4336  found = text_position_next(&state);
4337  if (found)
4338  curr_ptr = text_position_get_match_ptr(&state);
4339  }
4340  while (found);
4341 
4342  /* copy trailing data */
4343  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4344  appendBinaryStringInfo(&str, start_ptr, chunk_len);
4345 
4346  text_position_cleanup(&state);
4347 
4348  ret_text = cstring_to_text_with_len(str.data, str.len);
4349  pfree(str.data);
4350 
4351  PG_RETURN_TEXT_P(ret_text);
4352 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1324
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1456
#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:1169
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4271
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:80
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1499
#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:1195
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ replace_text_regexp()

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

Definition at line 4495 of file varlena.c.

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

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

4499 {
4500  text *ret_text;
4501  regex_t *re;
4502  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4503  int nmatches = 0;
4505  regmatch_t pmatch[10]; /* main match, plus \1 to \9 */
4506  int nmatch = lengthof(pmatch);
4507  pg_wchar *data;
4508  size_t data_len;
4509  int data_pos;
4510  char *start_ptr;
4511  int escape_status;
4512 
4513  initStringInfo(&buf);
4514 
4515  /* Convert data string to wide characters. */
4516  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4517  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4518 
4519  /* Check whether replace_text has escapes, especially regexp submatches. */
4520  escape_status = check_replace_text_has_escape(replace_text);
4521 
4522  /* If no regexp submatches, we can use REG_NOSUB. */
4523  if (escape_status < 2)
4524  {
4525  cflags |= REG_NOSUB;
4526  /* Also tell pg_regexec we only want the whole-match location. */
4527  nmatch = 1;
4528  }
4529 
4530  /* Prepare the regexp. */
4531  re = RE_compile_and_cache(pattern_text, cflags, collation);
4532 
4533  /* start_ptr points to the data_pos'th character of src_text */
4534  start_ptr = (char *) VARDATA_ANY(src_text);
4535  data_pos = 0;
4536 
4537  while (search_start <= data_len)
4538  {
4539  int regexec_result;
4540 
4542 
4543  regexec_result = pg_regexec(re,
4544  data,
4545  data_len,
4546  search_start,
4547  NULL, /* no details */
4548  nmatch,
4549  pmatch,
4550  0);
4551 
4552  if (regexec_result == REG_NOMATCH)
4553  break;
4554 
4555  if (regexec_result != REG_OKAY)
4556  {
4557  char errMsg[100];
4558 
4560  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4561  ereport(ERROR,
4562  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4563  errmsg("regular expression failed: %s", errMsg)));
4564  }
4565 
4566  /*
4567  * Count matches, and decide whether to replace this match.
4568  */
4569  nmatches++;
4570  if (n > 0 && nmatches != n)
4571  {
4572  /*
4573  * No, so advance search_start, but not start_ptr/data_pos. (Thus,
4574  * we treat the matched text as if it weren't matched, and copy it
4575  * to the output later.)
4576  */
4577  search_start = pmatch[0].rm_eo;
4578  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4579  search_start++;
4580  continue;
4581  }
4582 
4583  /*
4584  * Copy the text to the left of the match position. Note we are given
4585  * character not byte indexes.
4586  */
4587  if (pmatch[0].rm_so - data_pos > 0)
4588  {
4589  int chunk_len;
4590 
4591  chunk_len = charlen_to_bytelen(start_ptr,
4592  pmatch[0].rm_so - data_pos);
4593  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4594 
4595  /*
4596  * Advance start_ptr over that text, to avoid multiple rescans of
4597  * it if the replace_text contains multiple back-references.
4598  */
4599  start_ptr += chunk_len;
4600  data_pos = pmatch[0].rm_so;
4601  }
4602 
4603  /*
4604  * Copy the replace_text, processing escapes if any are present.
4605  */
4606  if (escape_status > 0)
4607  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4608  start_ptr, data_pos);
4609  else
4610  appendStringInfoText(&buf, replace_text);
4611 
4612  /* Advance start_ptr and data_pos over the matched text. */
4613  start_ptr += charlen_to_bytelen(start_ptr,
4614  pmatch[0].rm_eo - data_pos);
4615  data_pos = pmatch[0].rm_eo;
4616 
4617  /*
4618  * If we only want to replace one occurrence, we're done.
4619  */
4620  if (n > 0)
4621  break;
4622 
4623  /*
4624  * Advance search position. Normally we start the next search at the
4625  * end of the previous match; but if the match was of zero length, we
4626  * have to advance by one character, or we'd just find the same match
4627  * again.
4628  */
4629  search_start = data_pos;
4630  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4631  search_start++;
4632  }
4633 
4634  /*
4635  * Copy the text to the right of the last match.
4636  */
4637  if (data_pos < data_len)
4638  {
4639  int chunk_len;
4640 
4641  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4642  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4643  }
4644 
4645  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4646  pfree(buf.data);
4647  pfree(data);
4648 
4649  return ret_text;
4650 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
regoff_t rm_so
Definition: regex.h:87
static int check_replace_text_has_escape(const text *replace_text)
Definition: varlena.c:4362
int errcode(int sqlerrcode)
Definition: elog.c:698
#define lengthof(array)
Definition: c.h:734
regoff_t rm_eo
Definition: regex.h:88
void pfree(void *pointer)
Definition: mcxt.c:1169
#define REG_OKAY
Definition: regex.h:139
#define ERROR
Definition: elog.h:46
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:786
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:4271
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 VARSIZE_ANY(PTR)
Definition: postgres.h:348
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:929
#define ereport(elevel,...)
Definition: elog.h:157
#define REG_NOSUB
Definition: regex.h:109
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:4395
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition: regexec.c:176
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define REG_NOMATCH
Definition: regex.h:140
Definition: c.h:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
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 6238 of file varlena.c.

Referenced by varstr_levenshtein().

6239 {
6240  while (len > 0)
6241  {
6242  len--;
6243  if (s1[len] != s2[len])
6244  return false;
6245  }
6246  return true;
6247 }
char * s1
char * s2

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

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

4659 {
4660  text *inputstring = PG_GETARG_TEXT_PP(0);
4661  text *fldsep = PG_GETARG_TEXT_PP(1);
4662  int fldnum = PG_GETARG_INT32(2);
4663  int inputstring_len;
4664  int fldsep_len;
4666  char *start_ptr;
4667  char *end_ptr;
4668  text *result_text;
4669  bool found;
4670 
4671  /* field number is 1 based */
4672  if (fldnum == 0)
4673  ereport(ERROR,
4674  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4675  errmsg("field position must not be zero")));
4676 
4677  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4678  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4679 
4680  /* return empty string for empty input string */
4681  if (inputstring_len < 1)
4683 
4684  /* handle empty field separator */
4685  if (fldsep_len < 1)
4686  {
4687  /* if first or last field, return input string, else empty string */
4688  if (fldnum == 1 || fldnum == -1)
4689  PG_RETURN_TEXT_P(inputstring);
4690  else
4692  }
4693 
4694  /* find the first field separator */
4695  text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4696 
4697  found = text_position_next(&state);
4698 
4699  /* special case if fldsep not found at all */
4700  if (!found)
4701  {
4702  text_position_cleanup(&state);
4703  /* if first or last field, return input string, else empty string */
4704  if (fldnum == 1 || fldnum == -1)
4705  PG_RETURN_TEXT_P(inputstring);
4706  else
4708  }
4709 
4710  /*
4711  * take care of a negative field number (i.e. count from the right) by
4712  * converting to a positive field number; we need total number of fields
4713  */
4714  if (fldnum < 0)
4715  {
4716  /* we found a fldsep, so there are at least two fields */
4717  int numfields = 2;
4718 
4719  while (text_position_next(&state))
4720  numfields++;
4721 
4722  /* special case of last field does not require an extra pass */
4723  if (fldnum == -1)
4724  {
4725  start_ptr = text_position_get_match_ptr(&state) + fldsep_len;
4726  end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
4727  text_position_cleanup(&state);
4729  end_ptr - start_ptr));
4730  }
4731 
4732  /* else, convert fldnum to positive notation */
4733  fldnum += numfields + 1;
4734 
4735  /* if nonexistent field, return empty string */
4736  if (fldnum <= 0)
4737  {
4738  text_position_cleanup(&state);
4740  }
4741 
4742  /* reset to pointing at first match, but now with positive fldnum */
4743  text_position_reset(&state);
4744  found = text_position_next(&state);
4745  Assert(found);
4746  }
4747 
4748  /* identify bounds of first field */
4749  start_ptr = VARDATA_ANY(inputstring);
4750  end_ptr = text_position_get_match_ptr(&state);
4751 
4752  while (found && --fldnum > 0)
4753  {
4754  /* identify bounds of next field */
4755  start_ptr = end_ptr + fldsep_len;
4756  found = text_position_next(&state);
4757  if (found)
4758  end_ptr = text_position_get_match_ptr(&state);
4759  }
4760 
4761  text_position_cleanup(&state);
4762 
4763  if (fldnum > 0)
4764  {
4765  /* N'th field separator not found */
4766  /* if last field requested, return it, else empty string */
4767  if (fldnum == 1)
4768  {
4769  int last_len = start_ptr - VARDATA_ANY(inputstring);
4770 
4771  result_text = cstring_to_text_with_len(start_ptr,
4772  inputstring_len - last_len);
4773  }
4774  else
4775  result_text = cstring_to_text("");
4776  }
4777  else
4778  {
4779  /* non-last field requested */
4780  result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4781  }
4782 
4783  PG_RETURN_TEXT_P(result_text);
4784 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
static void text_position_reset(TextPositionState *state)
Definition: varlena.c:1491
int errcode(int sqlerrcode)
Definition: elog.c:698
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1324
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1456
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
ts_parserstate state
Definition: tsquery.c:80
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1499
#define ereport(elevel,...)
Definition: elog.h:157
#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:804
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1195
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

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

4903 {
4904  text *inputstring;
4905  text *fldsep;
4906  text *null_string;
4907  Oid collation = PG_GET_COLLATION();
4908  int inputstring_len;
4909  int fldsep_len;
4910  char *start_ptr;
4911  text *result_text;
4912 
4913  /* when input string is NULL, then result is NULL too */
4914  if (PG_ARGISNULL(0))
4915  return false;
4916 
4917  inputstring = PG_GETARG_TEXT_PP(0);
4918 
4919  /* fldsep can be NULL */
4920  if (!PG_ARGISNULL(1))
4921  fldsep = PG_GETARG_TEXT_PP(1);
4922  else
4923  fldsep = NULL;
4924 
4925  /* null_string can be NULL or omitted */
4926  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4927  null_string = PG_GETARG_TEXT_PP(2);
4928  else
4929  null_string = NULL;
4930 
4931  if (fldsep != NULL)
4932  {
4933  /*
4934  * Normal case with non-null fldsep. Use the text_position machinery
4935  * to search for occurrences of fldsep.
4936  */
4938 
4939  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4940  fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4941 
4942  /* return empty set for empty input string */
4943  if (inputstring_len < 1)
4944  return true;
4945 
4946  /* empty field separator: return input string as a one-element set */
4947  if (fldsep_len < 1)
4948  {
4949  split_text_accum_result(tstate, inputstring,
4950  null_string, collation);
4951  return true;
4952  }
4953 
4954  text_position_setup(inputstring, fldsep, collation, &state);
4955 
4956  start_ptr = VARDATA_ANY(inputstring);
4957 
4958  for (;;)
4959  {
4960  bool found;
4961  char *end_ptr;
4962  int chunk_len;
4963 
4965 
4966  found = text_position_next(&state);
4967  if (!found)
4968  {
4969  /* fetch last field */
4970  chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4971  end_ptr = NULL; /* not used, but some compilers complain */
4972  }
4973  else
4974  {
4975  /* fetch non-last field */
4976  end_ptr = text_position_get_match_ptr(&state);
4977  chunk_len = end_ptr - start_ptr;
4978  }
4979 
4980  /* build a temp text datum to pass to split_text_accum_result */
4981  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4982 
4983  /* stash away this field */
4984  split_text_accum_result(tstate, result_text,
4985  null_string, collation);
4986 
4987  pfree(result_text);
4988 
4989  if (!found)
4990  break;
4991 
4992  start_ptr = end_ptr + fldsep_len;
4993  }
4994 
4995  text_position_cleanup(&state);
4996  }
4997  else
4998  {
4999  /*
5000  * When fldsep is NULL, each character in the input string becomes a
5001  * separate element in the result set. The separator is effectively
5002  * the space between characters.
5003  */
5004  inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
5005 
5006  start_ptr = VARDATA_ANY(inputstring);
5007 
5008  while (inputstring_len > 0)
5009  {
5010  int chunk_len = pg_mblen(start_ptr);
5011 
5013 
5014  /* build a temp text datum to pass to split_text_accum_result */
5015  result_text = cstring_to_text_with_len(start_ptr, chunk_len);
5016 
5017  /* stash away this field */
5018  split_text_accum_result(tstate, result_text,
5019  null_string, collation);
5020 
5021  pfree(result_text);
5022 
5023  start_ptr += chunk_len;
5024  inputstring_len -= chunk_len;
5025  }
5026  }
5027 
5028  return true;
5029 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
static bool text_position_next(TextPositionState *state)
Definition: varlena.c:1324
static char * text_position_get_match_ptr(TextPositionState *state)
Definition: varlena.c:1456
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:1169
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
ts_parserstate state
Definition: tsquery.c:80
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1499
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition: varlena.c:5038
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_NARGS()
Definition: fmgr.h:203
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition: varlena.c:1195
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120

◆ split_text_accum_result()

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

Definition at line 5038 of file varlena.c.

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

Referenced by split_text().

5042 {
5043  bool is_null = false;
5044 
5045  if (null_string && text_isequal(field_value, null_string, collation))
5046  is_null = true;
5047 
5048  if (tstate->tupstore)
5049  {
5050  Datum values[1];
5051  bool nulls[1];
5052 
5053  values[0] = PointerGetDatum(field_value);
5054  nulls[0] = is_null;
5055 
5057  tstate->tupdesc,
5058  values,
5059  nulls);
5060  }
5061  else
5062  {
5063  tstate->astate = accumArrayResult(tstate->astate,
5064  PointerGetDatum(field_value),
5065  is_null,
5066  TEXTOID,
5068  }
5069 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define PointerGetDatum(X)
Definition: postgres.h:600
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:4790
ArrayBuildState * astate
Definition: varlena.c:103
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
uintptr_t Datum
Definition: postgres.h:411
static Datum values[MAXATTR]
Definition: bootstrap.c:156
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5122

◆ SplitDirectoriesString()

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

Definition at line 3873 of file varlena.c.

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

Referenced by load_libraries(), and PostmasterMain().

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

◆ SplitGUCList()

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

Definition at line 3994 of file varlena.c.

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

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

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

◆ SplitIdentifierString()

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

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

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

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

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

5449 {
5450  StringInfo state;
5451 
5452  /* cannot be called directly because of internal-type argument */
5453  Assert(AggCheckCallContext(fcinfo, NULL));
5454 
5455  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5456 
5457  if (state != NULL)
5459  else
5460  PG_RETURN_NULL();
5461 }
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:80
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:804
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4495
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 5422 of file varlena.c.

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

5423 {
5424  StringInfo state;
5425 
5426  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5427 
5428  /* Append the value unless null. */
5429  if (!PG_ARGISNULL(1))
5430  {
5431  /* On the first time through, we ignore the delimiter. */
5432  if (state == NULL)
5433  state = makeStringAggState(fcinfo);
5434  else if (!PG_ARGISNULL(2))
5435  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
5436 
5437  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
5438  }
5439 
5440  /*
5441  * The transition type for string_agg() is declared to be "internal",
5442  * which is a pass-by-value type the same size as a pointer.
5443  */
5444  PG_RETURN_POINTER(state);
5445 }
#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:4271
ts_parserstate state
Definition: tsquery.c:80
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:5398

◆ text_catenate()

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

Definition at line 745 of file varlena.c.

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

Referenced by text_overlay(), and textcat().

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

◆ text_cmp()

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

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

1752 {
1753  char *a1p,
1754  *a2p;
1755  int len1,
1756  len2;
1757 
1758  a1p = VARDATA_ANY(arg1);
1759  a2p = VARDATA_ANY(arg2);
1760 
1761  len1 = VARSIZE_ANY_EXHDR(arg1);
1762  len2 = VARSIZE_ANY_EXHDR(arg2);
1763 
1764  return varstr_cmp(a1p, len1, a2p, len2, collid);
1765 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1535
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 5588 of file varlena.c.

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

5589 {
5590  text *result;
5591 
5592  result = concat_internal("", 0, fcinfo);
5593  if (result == NULL)
5594  PG_RETURN_NULL();
5595  PG_RETURN_TEXT_P(result);
5596 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:5508
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 5603 of file varlena.c.

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

5604 {
5605  char *sep;
5606  text *result;
5607 
5608  /* return NULL when separator is NULL */
5609  if (PG_ARGISNULL(0))
5610  PG_RETURN_NULL();
5612 
5613  result = concat_internal(sep, 1, fcinfo);
5614  if (result == NULL)
5615  PG_RETURN_NULL();
5616  PG_RETURN_TEXT_P(result);
5617 }
#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:5508
char * text_to_cstring(const text *t)
Definition: varlena.c:222
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

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

5725 {
5726  text *fmt;
5728  const char *cp;
5729  const char *start_ptr;
5730  const char *end_ptr;
5731  text *result;
5732  int arg;
5733  bool funcvariadic;
5734  int nargs;
5735  Datum *elements = NULL;
5736  bool *nulls = NULL;
5737  Oid element_type = InvalidOid;
5738  Oid prev_type = InvalidOid;
5739  Oid prev_width_type = InvalidOid;
5740  FmgrInfo typoutputfinfo;
5741  FmgrInfo typoutputinfo_width;
5742 
5743  /* When format string is null, immediately return null */
5744  if (PG_ARGISNULL(0))
5745  PG_RETURN_NULL();
5746 
5747  /* If argument is marked VARIADIC, expand array into elements */
5748  if (get_fn_expr_variadic(fcinfo->flinfo))
5749  {
5750  ArrayType *arr;
5751  int16 elmlen;
5752  bool elmbyval;
5753  char elmalign;
5754  int nitems;
5755 
5756  /* Should have just the one argument */
5757  Assert(PG_NARGS() == 2);
5758 
5759  /* If argument is NULL, we treat it as zero-length array */
5760  if (PG_ARGISNULL(1))
5761  nitems = 0;
5762  else
5763  {
5764  /*
5765  * Non-null argument had better be an array. We assume that any
5766  * call context that could let get_fn_expr_variadic return true
5767  * will have checked that a VARIADIC-labeled parameter actually is
5768  * an array. So it should be okay to just Assert that it's an
5769  * array rather than doing a full-fledged error check.
5770  */
5772 
5773  /* OK, safe to fetch the array value */
5774  arr = PG_GETARG_ARRAYTYPE_P(1);
5775 
5776  /* Get info about array element type */
5777  element_type = ARR_ELEMTYPE(arr);
5778  get_typlenbyvalalign(element_type,
5779  &elmlen, &elmbyval, &elmalign);
5780 
5781  /* Extract all array elements */
5782  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5783  &elements, &nulls, &nitems);
5784  }
5785 
5786  nargs = nitems + 1;
5787  funcvariadic = true;
5788  }
5789  else
5790  {
5791  /* Non-variadic case, we'll process the arguments individually */
5792  nargs = PG_NARGS();
5793  funcvariadic = false;
5794  }
5795 
5796  /* Setup for main loop. */
5797  fmt = PG_GETARG_TEXT_PP(0);
5798  start_ptr = VARDATA_ANY(fmt);
5799  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5800  initStringInfo(&str);
5801  arg = 1; /* next argument position to print */
5802 
5803  /* Scan format string, looking for conversion specifiers. */
5804  for (cp = start_ptr; cp < end_ptr; cp++)
5805  {
5806  int argpos;
5807  int widthpos;
5808  int flags;
5809  int width;
5810  Datum value;
5811  bool isNull;
5812  Oid typid;
5813 
5814  /*
5815  * If it's not the start of a conversion specifier, just copy it to
5816  * the output buffer.
5817  */
5818  if (*cp != '%')
5819  {
5820  appendStringInfoCharMacro(&str, *cp);
5821  continue;
5822  }
5823 
5824  ADVANCE_PARSE_POINTER(cp, end_ptr);
5825 
5826  /* Easy case: %% outputs a single % */
5827  if (*cp == '%')
5828  {
5829  appendStringInfoCharMacro(&str, *cp);
5830  continue;
5831  }
5832 
5833  /* Parse the optional portions of the format specifier */
5834  cp = text_format_parse_format(cp, end_ptr,
5835  &argpos, &widthpos,
5836  &flags, &width);
5837 
5838  /*
5839  * Next we should see the main conversion specifier. Whether or not
5840  * an argument position was present, it's known that at least one
5841  * character remains in the string at this point. Experience suggests
5842  * that it's worth checking that that character is one of the expected
5843  * ones before we try to fetch arguments, so as to produce the least
5844  * confusing response to a mis-formatted specifier.
5845  */
5846  if (strchr("sIL", *cp) == NULL)
5847  ereport(ERROR,
5848  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5849  errmsg("unrecognized format() type specifier \"%.*s\"",
5850  pg_mblen(cp), cp),
5851  errhint("For a single \"%%\" use \"%%%%\".")));
5852 
5853  /* If indirect width was specified, get its value */
5854  if (widthpos >= 0)
5855  {
5856  /* Collect the specified or next argument position */
5857  if (widthpos > 0)
5858  arg = widthpos;
5859  if (arg >= nargs)
5860  ereport(ERROR,
5861  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5862  errmsg("too few arguments for format()")));
5863 
5864  /* Get the value and type of the selected argument */
5865  if (!funcvariadic)
5866  {
5867  value = PG_GETARG_DATUM(arg);
5868  isNull = PG_ARGISNULL(arg);
5869  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5870  }
5871  else
5872  {
5873  value = elements[arg - 1];
5874  isNull = nulls[arg - 1];
5875  typid = element_type;
5876  }
5877  if (!OidIsValid(typid))
5878  elog(ERROR, "could not determine data type of format() input");
5879 
5880  arg++;
5881 
5882  /* We can treat NULL width the same as zero */
5883  if (isNull)
5884  width = 0;
5885  else if (typid == INT4OID)
5886  width = DatumGetInt32(value);
5887  else if (typid == INT2OID)
5888  width = DatumGetInt16(value);
5889  else
5890  {
5891  /* For less-usual datatypes, convert to text then to int */
5892  char *str;
5893 
5894  if (typid != prev_width_type)
5895  {
5896  Oid typoutputfunc;
5897  bool typIsVarlena;
5898 
5899  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5900  fmgr_info(typoutputfunc, &typoutputinfo_width);
5901  prev_width_type = typid;
5902  }
5903 
5904  str = OutputFunctionCall(&typoutputinfo_width, value);
5905 
5906  /* pg_strtoint32 will complain about bad data or overflow */
5907  width = pg_strtoint32(str);
5908 
5909  pfree(str);
5910  }
5911  }
5912 
5913  /* Collect the specified or next argument position */
5914  if (argpos > 0)
5915  arg = argpos;
5916  if (arg >= nargs)
5917  ereport(ERROR,
5918  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5919  errmsg("too few arguments for format()")));
5920 
5921  /* Get the value and type of the selected argument */
5922  if (!funcvariadic)
5923  {
5924  value = PG_GETARG_DATUM(arg);
5925  isNull = PG_ARGISNULL(arg);
5926  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5927  }
5928  else
5929  {
5930  value = elements[arg - 1];
5931  isNull = nulls[arg - 1];
5932  typid = element_type;
5933  }
5934  if (!OidIsValid(typid))
5935  elog(ERROR, "could not determine data type of format() input");
5936 
5937  arg++;
5938 
5939  /*
5940  * Get the appropriate typOutput function, reusing previous one if
5941  * same type as previous argument. That's particularly useful in the
5942  * variadic-array case, but often saves work even for ordinary calls.
5943  */
5944  if (typid != prev_type)
5945  {
5946  Oid typoutputfunc;
5947  bool typIsVarlena;
5948 
5949  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5950  fmgr_info(typoutputfunc, &typoutputfinfo);
5951  prev_type = typid;
5952  }
5953 
5954  /*
5955  * And now we can format the value.
5956  */
5957  switch (*cp)
5958  {
5959  case 's':
5960  case 'I':
5961  case 'L':
5962  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5963  value, isNull,
5964  flags, width);
5965  break;
5966  default:
5967  /* should not get here, because of previous check */
5968  ereport(ERROR,
5969  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5970  errmsg("unrecognized format() type specifier \"%.*s\"",
5971  pg_mblen(cp), cp),
5972  errhint("For a single \"%%\" use \"%%%%\".")));
5973  break;
5974  }
5975  }
5976 
5977  /* Don't need deconstruct_array results anymore. */
5978  if (elements != NULL)
5979  pfree(elements);
5980  if (nulls != NULL)
5981  pfree(nulls);
5982 
5983  /* Generate results. */
5984  result = cstring_to_text_with_len(str.data, str.len);
5985  pfree(str.data);
5986 
5987  PG_RETURN_TEXT_P(result);
5988 }
signed short int16
Definition: c.h:428
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5711
Definition: fmgr.h:56
int errhint(const char *fmt,...)
Definition: elog.c:1156
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define DatumGetInt32(X)
Definition: postgres.h:516
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:6127
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2218
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1934
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1573
#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:1169
#define ERROR
Definition: elog.h:46
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
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:488
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:804
#define PG_NARGS()
Definition: fmgr.h:203
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
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:3491
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2779
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
void * arg
Definition: c.h:621
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:6050
#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 6176 of file varlena.c.

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

Referenced by text_format_string_conversion().

6178 {
6179  bool align_to_left = false;
6180  int len;
6181 
6182  /* fast path for typical easy case */
6183  if (width == 0)
6184  {
6186  return;
6187  }
6188 
6189  if (width < 0)
6190  {
6191  /* Negative width: implicit '-' flag, then take absolute value */
6192  align_to_left = true;
6193  /* -INT_MIN is undefined */
6194  if (width <= INT_MIN)
6195  ereport(ERROR,
6196  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6197  errmsg("number is out of range")));
6198  width = -width;
6199  }
6200  else if (flags & TEXT_FORMAT_FLAG_MINUS)
6201  align_to_left = true;
6202 
6203  len = pg_mbstrlen(str);
6204  if (align_to_left)
6205  {
6206  /* left justify */
6208  if (len < width)
6209  appendStringInfoSpaces(buf, width - len);
6210  }
6211  else
6212  {
6213  /* right justify */
6214  if (len < width)
6215  appendStringInfoSpaces(buf, width - len);
6217  }
6218 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
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:980
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5709

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 6228 of file varlena.c.

References text_format().

6229 {
6230  return text_format(fcinfo);
6231 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:5724

◆ text_format_parse_digits()

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

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

6002 {
6003  bool found = false;
6004  const char *cp = *ptr;
6005  int val = 0;
6006 
6007  while (*cp >= '0' && *cp <= '9')
6008  {
6009  int8 digit = (*cp - '0');
6010 
6011  if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
6012  unlikely(pg_add_s32_overflow(val, digit, &val)))
6013  ereport(ERROR,
6014  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6015  errmsg("number is out of range")));
6016  ADVANCE_PARSE_POINTER(cp, end_ptr);
6017  found = true;
6018  }
6019 
6020  *ptr = cp;
6021  *value = val;
6022 
6023  return found;
6024 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5711
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
signed char int8
Definition: c.h:427
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:157
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define unlikely(x)
Definition: c.h:273
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 6050 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().

6053 {
6054  const char *cp = start_ptr;
6055  int n;
6056 
6057  /* set defaults for output parameters */
6058  *argpos = -1;
6059  *widthpos = -1;
6060  *flags = 0;
6061  *width = 0;
6062 
6063  /* try to identify first number */
6064  if (text_format_parse_digits(&cp, end_ptr, &n))
6065  {
6066  if (*cp != '$')
6067  {
6068  /* Must be just a width and a type, so we're done */
6069  *width = n;
6070  return cp;
6071  }
6072  /* The number was argument position */
6073  *argpos = n;
6074  /* Explicit 0 for argument index is immediately refused */
6075  if (n == 0)
6076  ereport(ERROR,
6077  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6078  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6079  ADVANCE_PARSE_POINTER(cp, end_ptr);
6080  }
6081 
6082  /* Handle flags (only minus is supported now) */
6083  while (*cp == '-')
6084  {
6085  *flags |= TEXT_FORMAT_FLAG_MINUS;
6086  ADVANCE_PARSE_POINTER(cp, end_ptr);
6087  }
6088 
6089  if (*cp == '*')
6090  {
6091  /* Handle indirect width */
6092  ADVANCE_PARSE_POINTER(cp, end_ptr);
6093  if (text_format_parse_digits(&cp, end_ptr, &n))
6094  {
6095  /* number in this position must be closed by $ */
6096  if (*cp != '$')
6097  ereport(ERROR,
6098  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6099  errmsg("width argument position must be ended by \"$\"")));
6100  /* The number was width argument position */
6101  *widthpos = n;
6102  /* Explicit 0 for argument index is immediately refused */
6103  if (n == 0)
6104  ereport(ERROR,
6105  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6106  errmsg("format specifies argument 0, but arguments are numbered from 1")));
6107  ADVANCE_PARSE_POINTER(cp, end_ptr);
6108  }
6109  else
6110  *widthpos = 0; /* width's argument position is unspecified */
6111  }
6112  else
6113  {
6114  /* Check for direct width specification */
6115  if (text_format_parse_digits(&cp, end_ptr, &n))
6116  *width = n;
6117  }
6118 
6119  /* cp should now be pointing at type character */
6120  return cp;
6121 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:5711
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:6001
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:5709

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

6131 {
6132  char *str;
6133 
6134  /* Handle NULL arguments before trying to stringify the value. */
6135  if (isNull)
6136  {
6137  if (conversion == 's')
6138  text_format_append_string(buf, "", flags, width);
6139  else if (conversion == 'L')
6140  text_format_append_string(buf, "NULL", flags, width);
6141  else if (conversion == 'I')
6142  ereport(ERROR,
6143  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6144  errmsg("null values cannot be formatted as an SQL identifier")));
6145  return;
6146  }
6147 
6148  /* Stringify. */
6149  str = OutputFunctionCall(typOutputInfo, value);
6150 
6151  /* Escape. */
6152  if (conversion == 'I')
6153  {
6154  /* quote_identifier may or may not allocate a new string. */
6155  text_format_append_string(buf, quote_identifier(str), flags, width);
6156  }
6157  else if (conversion == 'L')
6158  {
6159  char *qstr = quote_literal_cstr(str);
6160 
6161  text_format_append_string(buf, qstr, flags, width);
6162  /* quote_literal_cstr() always allocates a new string */
6163  pfree(qstr);
6164  }
6165  else
6166  text_format_append_string(buf, str, flags, width);
6167 
6168  /* Cleanup. */
6169  pfree(str);
6170 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
int errcode(int sqlerrcode)
Definition: elog.c:698
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:6176
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1573
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

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

1924 {
1925  text *arg1 = PG_GETARG_TEXT_PP(0);
1926  text *arg2 = PG_GETARG_TEXT_PP(1);
1927  bool result;
1928 
1929  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1930 
1931  PG_FREE_IF_COPY(arg1, 0);
1932  PG_FREE_IF_COPY(arg2, 1);
1933 
1934  PG_RETURN_BOOL(result);
1935 }
#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:1751
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:621

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

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

1909 {
1910  text *arg1 = PG_GETARG_TEXT_PP(0);
1911  text *arg2 = PG_GETARG_TEXT_PP(1);
1912  bool result;
1913 
1914  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1915 
1916  PG_FREE_IF_COPY(arg1, 0);
1917  PG_FREE_IF_COPY(arg2, 1);
1918 
1919  PG_RETURN_BOOL(result);
1920 }
#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:1751
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:621

◆ text_isequal()

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

Definition at line 4790 of file varlena.c.

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

Referenced by split_text_accum_result().

4791 {
4793  collid,
4794  PointerGetDatum(txt1),
4795  PointerGetDatum(txt2)));
4796 }
#define PointerGetDatum(X)
Definition: postgres.h:600
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1776
#define DatumGetBool(X)
Definition: postgres.h:437
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:811

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2860 of file varlena.c.

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

2861 {
2862  text *arg1 = PG_GETARG_TEXT_PP(0);
2863  text *arg2 = PG_GETARG_TEXT_PP(1);
2864  text *result;
2865 
2866  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2867 
2868  PG_RETURN_TEXT_P(result);
2869 }
#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:1751
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:621

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

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

1894 {
1895  text *arg1 = PG_GETARG_TEXT_PP(0);
1896  text *arg2 = PG_GETARG_TEXT_PP(1);
1897  bool result;
1898 
1899  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1900 
1901  PG_FREE_IF_COPY(arg1, 0);
1902  PG_FREE_IF_COPY(arg2, 1);
1903 
1904  PG_RETURN_BOOL(result);
1905 }
#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:1751
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:621

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

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

5625 {
5626  int n = PG_GETARG_INT32(1);
5627 
5628  if (n < 0)
5629  {
5630  text *str = PG_GETARG_TEXT_PP(0);
5631  const char *p = VARDATA_ANY(str);
5632  int len = VARSIZE_ANY_EXHDR(str);
5633  int rlen;
5634 
5635  n = pg_mbstrlen_with_len(p, len) + n;
5636  rlen = pg_mbcharcliplen(p, len, n);
5638  }
5639  else
5641 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1068
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#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:1000
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:865
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:354
Definition: c.h:621

◆ text_length()

static int32 text_length ( Datum  str