PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
varlena.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "access/hash.h"
#include "access/tuptoaster.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "common/md5.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.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
 

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 PG_STR_GET_BYTEA(str_)   DatumGetByteaP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
 
#define REGEXP_REPLACE_BACKREF_CNT   10
 
#define HEXBASE   16
 
#define MD5_HASH_LEN   32
 
#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 varstrfastcmp_locale (Datum x, Datum y, 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)
 
static void text_position_setup (text *t1, text *t2, TextPositionState *state)
 
static int text_position_next (int start_pos, TextPositionState *state)
 
static void text_position_cleanup (TextPositionState *state)
 
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 Datum text_to_array_internal (PG_FUNCTION_ARGS)
 
static textarray_to_text_internal (FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
 
static StringInfo makeStringAggState (FunctionCallInfo fcinfo)
 
static bool text_format_parse_digits (const char **ptr, const char *end_ptr, int *value)
 
static const char * text_format_parse_format (const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
 
static void text_format_string_conversion (StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
 
static void text_format_append_string (StringInfo buf, const char *str, int flags, int width)
 
textcstring_to_text (const char *s)
 
textcstring_to_text_with_len (const char *s, int len)
 
char * text_to_cstring (const text *t)
 
void text_to_cstring_buffer (const text *src, char *dst, size_t dst_len)
 
Datum byteain (PG_FUNCTION_ARGS)
 
Datum byteaout (PG_FUNCTION_ARGS)
 
Datum bytearecv (PG_FUNCTION_ARGS)
 
Datum byteasend (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum bytea_string_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum textin (PG_FUNCTION_ARGS)
 
Datum textout (PG_FUNCTION_ARGS)
 
Datum textrecv (PG_FUNCTION_ARGS)
 
Datum textsend (PG_FUNCTION_ARGS)
 
Datum unknownin (PG_FUNCTION_ARGS)
 
Datum unknownout (PG_FUNCTION_ARGS)
 
Datum unknownrecv (PG_FUNCTION_ARGS)
 
Datum unknownsend (PG_FUNCTION_ARGS)
 
Datum textlen (PG_FUNCTION_ARGS)
 
Datum textoctetlen (PG_FUNCTION_ARGS)
 
Datum textcat (PG_FUNCTION_ARGS)
 
static int charlen_to_bytelen (const char *p, int n)
 
Datum text_substr (PG_FUNCTION_ARGS)
 
Datum text_substr_no_len (PG_FUNCTION_ARGS)
 
Datum textoverlay (PG_FUNCTION_ARGS)
 
Datum textoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum textpos (PG_FUNCTION_ARGS)
 
int varstr_cmp (char *arg1, int len1, 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 bttextcmp (PG_FUNCTION_ARGS)
 
Datum bttextsortsupport (PG_FUNCTION_ARGS)
 
void varstr_sortsupport (SortSupport ssup, Oid collid, bool bpchar)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
static int internal_text_pattern_compare (text *arg1, text *arg2)
 
Datum text_pattern_lt (PG_FUNCTION_ARGS)
 
Datum text_pattern_le (PG_FUNCTION_ARGS)
 
Datum text_pattern_ge (PG_FUNCTION_ARGS)
 
Datum text_pattern_gt (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_sortsupport (PG_FUNCTION_ARGS)
 
Datum byteaoctetlen (PG_FUNCTION_ARGS)
 
Datum byteacat (PG_FUNCTION_ARGS)
 
Datum bytea_substr (PG_FUNCTION_ARGS)
 
Datum bytea_substr_no_len (PG_FUNCTION_ARGS)
 
Datum byteaoverlay (PG_FUNCTION_ARGS)
 
Datum byteaoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum byteapos (PG_FUNCTION_ARGS)
 
Datum byteaGetByte (PG_FUNCTION_ARGS)
 
Datum byteaGetBit (PG_FUNCTION_ARGS)
 
Datum byteaSetByte (PG_FUNCTION_ARGS)
 
Datum byteaSetBit (PG_FUNCTION_ARGS)
 
Datum text_name (PG_FUNCTION_ARGS)
 
Datum name_text (PG_FUNCTION_ARGS)
 
ListtextToQualifiedNameList (text *textval)
 
bool SplitIdentifierString (char *rawstring, char separator, List **namelist)
 
bool SplitDirectoriesString (char *rawstring, char separator, List **namelist)
 
Datum byteaeq (PG_FUNCTION_ARGS)
 
Datum byteane (PG_FUNCTION_ARGS)
 
Datum bytealt (PG_FUNCTION_ARGS)
 
Datum byteale (PG_FUNCTION_ARGS)
 
Datum byteagt (PG_FUNCTION_ARGS)
 
Datum byteage (PG_FUNCTION_ARGS)
 
Datum byteacmp (PG_FUNCTION_ARGS)
 
Datum bytea_sortsupport (PG_FUNCTION_ARGS)
 
Datum replace_text (PG_FUNCTION_ARGS)
 
static bool check_replace_text_has_escape_char (const text *replace_text)
 
static void appendStringInfoRegexpSubstr (StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
 
textreplace_text_regexp (text *src_text, void *regexp, text *replace_text, bool glob)
 
Datum split_text (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_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 md5_text (PG_FUNCTION_ARGS)
 
Datum md5_bytea (PG_FUNCTION_ARGS)
 
Datum pg_column_size (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static 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)
 

Variables

int bytea_output = BYTEA_OUTPUT_HEX
 

Macro Definition Documentation

#define ADVANCE_PARSE_POINTER (   ptr,
  end_ptr 
)
Value:
do { \
if (++(ptr) >= (end_ptr)) \
(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:987
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

Definition at line 4800 of file varlena.c.

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

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

Definition at line 87 of file varlena.c.

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

Definition at line 88 of file varlena.c.

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

Definition at line 93 of file varlena.c.

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

Definition at line 94 of file varlena.c.

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

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

Definition at line 243 of file varlena.c.

Referenced by byteaout().

#define HEXBASE   16

Definition at line 4371 of file varlena.c.

Referenced by to_hex32(), and to_hex64().

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 5340 of file varlena.c.

#define MD5_HASH_LEN   32

Definition at line 4425 of file varlena.c.

Referenced by md5_bytea(), and md5_text().

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

Definition at line 89 of file varlena.c.

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

Definition at line 90 of file varlena.c.

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 91 of file varlena.c.

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

Definition at line 2664 of file varlena.c.

Referenced by bytea_substring().

#define REGEXP_REPLACE_BACKREF_CNT   10

Definition at line 3764 of file varlena.c.

Referenced by replace_text_regexp().

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

Definition at line 4798 of file varlena.c.

Referenced by text_format_append_string(), and text_format_parse_format().

#define TEXTBUFLEN   1024

Definition at line 85 of file varlena.c.

Referenced by varstr_cmp(), and varstr_sortsupport().

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

Definition at line 242 of file varlena.c.

Referenced by byteain().

Typedef Documentation

Definition at line 43 of file varlena.c.

Definition at line 44 of file varlena.c.

Function Documentation

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

Definition at line 3670 of file varlena.c.

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

Referenced by replace_text_regexp().

3673 {
3674  const char *p = VARDATA_ANY(replace_text);
3675  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3676  int eml = pg_database_encoding_max_length();
3677 
3678  for (;;)
3679  {
3680  const char *chunk_start = p;
3681  int so;
3682  int eo;
3683 
3684  /* Find next escape char. */
3685  if (eml == 1)
3686  {
3687  for (; p < p_end && *p != '\\'; p++)
3688  /* nothing */ ;
3689  }
3690  else
3691  {
3692  for (; p < p_end && *p != '\\'; p += pg_mblen(p))
3693  /* nothing */ ;
3694  }
3695 
3696  /* Copy the text we just scanned over, if any. */
3697  if (p > chunk_start)
3698  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
3699 
3700  /* Done if at end of string, else advance over escape char. */
3701  if (p >= p_end)
3702  break;
3703  p++;
3704 
3705  if (p >= p_end)
3706  {
3707  /* Escape at very end of input. Treat same as unexpected char */
3708  appendStringInfoChar(str, '\\');
3709  break;
3710  }
3711 
3712  if (*p >= '1' && *p <= '9')
3713  {
3714  /* Use the back reference of regexp. */
3715  int idx = *p - '0';
3716 
3717  so = pmatch[idx].rm_so;
3718  eo = pmatch[idx].rm_eo;
3719  p++;
3720  }
3721  else if (*p == '&')
3722  {
3723  /* Use the entire matched string. */
3724  so = pmatch[0].rm_so;
3725  eo = pmatch[0].rm_eo;
3726  p++;
3727  }
3728  else if (*p == '\\')
3729  {
3730  /* \\ means transfer one \ to output. */
3731  appendStringInfoChar(str, '\\');
3732  p++;
3733  continue;
3734  }
3735  else
3736  {
3737  /*
3738  * If escape char is not followed by any expected char, just treat
3739  * it as ordinary data to copy. (XXX would it be better to throw
3740  * an error?)
3741  */
3742  appendStringInfoChar(str, '\\');
3743  continue;
3744  }
3745 
3746  if (so != -1 && eo != -1)
3747  {
3748  /*
3749  * Copy the text that is back reference of regexp. Note so and eo
3750  * are counted in characters not bytes.
3751  */
3752  char *chunk_start;
3753  int chunk_len;
3754 
3755  Assert(so >= data_pos);
3756  chunk_start = start_ptr;
3757  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
3758  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
3759  appendBinaryStringInfo(str, chunk_start, chunk_len);
3760  }
3761  }
3762 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
regoff_t rm_so
Definition: regex.h:85
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
regoff_t rm_eo
Definition: regex.h:86
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:737
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define Assert(condition)
Definition: c.h:671
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
static void appendStringInfoText ( StringInfo  str,
const text t 
)
static

Definition at line 3540 of file varlena.c.

References appendBinaryStringInfo(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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

3541 {
3543 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4212 of file varlena.c.

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

4213 {
4215  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4216 
4217  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4218 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4254
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
static text * array_to_text_internal ( FunctionCallInfo  fcinfo,
ArrayType v,
const char *  fldsep,
const char *  null_string 
)
static

Definition at line 4254 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, FunctionCallInfoData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_type_io_data(), i, initStringInfo(), IOFunc_output, StringInfoData::len, MemoryContextAlloc(), NULL, OutputFunctionCall(), pfree(), ArrayMetaState::proc, 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().

4256 {
4257  text *result;
4258  int nitems,
4259  *dims,
4260  ndims;
4261  Oid element_type;
4262  int typlen;
4263  bool typbyval;
4264  char typalign;
4266  bool printed = false;
4267  char *p;
4268  bits8 *bitmap;
4269  int bitmask;
4270  int i;
4271  ArrayMetaState *my_extra;
4272 
4273  ndims = ARR_NDIM(v);
4274  dims = ARR_DIMS(v);
4275  nitems = ArrayGetNItems(ndims, dims);
4276 
4277  /* if there are no elements, return an empty string */
4278  if (nitems == 0)
4279  return cstring_to_text_with_len("", 0);
4280 
4281  element_type = ARR_ELEMTYPE(v);
4282  initStringInfo(&buf);
4283 
4284  /*
4285  * We arrange to look up info about element type, including its output
4286  * conversion proc, only once per series of calls, assuming the element
4287  * type doesn't change underneath us.
4288  */
4289  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4290  if (my_extra == NULL)
4291  {
4292  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4293  sizeof(ArrayMetaState));
4294  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4295  my_extra->element_type = ~element_type;
4296  }
4297 
4298  if (my_extra->element_type != element_type)
4299  {
4300  /*
4301  * Get info about element type, including its output conversion proc
4302  */
4303  get_type_io_data(element_type, IOFunc_output,
4304  &my_extra->typlen, &my_extra->typbyval,
4305  &my_extra->typalign, &my_extra->typdelim,
4306  &my_extra->typioparam, &my_extra->typiofunc);
4307  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4308  fcinfo->flinfo->fn_mcxt);
4309  my_extra->element_type = element_type;
4310  }
4311  typlen = my_extra->typlen;
4312  typbyval = my_extra->typbyval;
4313  typalign = my_extra->typalign;
4314 
4315  p = ARR_DATA_PTR(v);
4316  bitmap = ARR_NULLBITMAP(v);
4317  bitmask = 1;
4318 
4319  for (i = 0; i < nitems; i++)
4320  {
4321  Datum itemvalue;
4322  char *value;
4323 
4324  /* Get source element, checking for NULL */
4325  if (bitmap && (*bitmap & bitmask) == 0)
4326  {
4327  /* if null_string is NULL, we just ignore null elements */
4328  if (null_string != NULL)
4329  {
4330  if (printed)
4331  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4332  else
4333  appendStringInfoString(&buf, null_string);
4334  printed = true;
4335  }
4336  }
4337  else
4338  {
4339  itemvalue = fetch_att(p, typbyval, typlen);
4340 
4341  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4342 
4343  if (printed)
4344  appendStringInfo(&buf, "%s%s", fldsep, value);
4345  else
4346  appendStringInfoString(&buf, value);
4347  printed = true;
4348 
4349  p = att_addlength_pointer(p, typlen, p);
4350  p = (char *) att_align_nominal(p, typalign);
4351  }
4352 
4353  /* advance bitmap pointer if any */
4354  if (bitmap)
4355  {
4356  bitmask <<= 1;
4357  if (bitmask == 0x100)
4358  {
4359  bitmap++;
4360  bitmask = 1;
4361  }
4362  }
4363  }
4364 
4365  result = cstring_to_text_with_len(buf.data, buf.len);
4366  pfree(buf.data);
4367 
4368  return result;
4369 }
static struct @76 value
MemoryContext fn_mcxt
Definition: fmgr.h:62
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
unsigned int Oid
Definition: postgres_ext.h:31
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1926
bool typbyval
Definition: array.h:221
FmgrInfo * flinfo
Definition: fmgr.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ARR_DIMS(a)
Definition: array.h:275
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define ARR_DATA_PTR(a)
Definition: array.h:303
int16 typlen
Definition: array.h:220
static char * buf
Definition: pg_test_fsync.c:65
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
char typdelim
Definition: array.h:223
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:169
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
uint8 bits8
Definition: c.h:272
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
Oid typioparam
Definition: array.h:224
void * fn_extra
Definition: fmgr.h:61
#define ARR_NDIM(a)
Definition: array.h:271
Oid typiofunc
Definition: array.h:225
char typalign
Definition: array.h:222
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:71
FmgrInfo proc
Definition: array.h:226
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
int i
Oid element_type
Definition: array.h:219
Definition: c.h:435
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define ARR_NULLBITMAP(a)
Definition: array.h:281
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:2043
Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4228 of file varlena.c.

References array_to_text_internal(), NULL, PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

4229 {
4230  ArrayType *v;
4231  char *fldsep;
4232  char *null_string;
4233 
4234  /* returns NULL when first or second parameter is NULL */
4235  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4236  PG_RETURN_NULL();
4237 
4238  v = PG_GETARG_ARRAYTYPE_P(0);
4239  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4240 
4241  /* NULL null string is passed through as a null pointer */
4242  if (!PG_ARGISNULL(2))
4243  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4244  else
4245  null_string = NULL;
4246 
4247  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4248 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4254
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define PG_RETURN_NULL()
Definition: fmgr.h:289
static int bpcharfastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1962 of file varlena.c.

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

Referenced by varstr_sortsupport().

1963 {
1964  BpChar *arg1 = DatumGetBpCharPP(x);
1965  BpChar *arg2 = DatumGetBpCharPP(y);
1966  char *a1p,
1967  *a2p;
1968  int len1,
1969  len2,
1970  result;
1971 
1972  a1p = VARDATA_ANY(arg1);
1973  a2p = VARDATA_ANY(arg2);
1974 
1975  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
1976  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
1977 
1978  result = memcmp(a1p, a2p, Min(len1, len2));
1979  if ((result == 0) && (len1 != len2))
1980  result = (len1 < len2) ? -1 : 1;
1981 
1982  /* We can't afford to leak memory here. */
1983  if (PointerGetDatum(arg1) != x)
1984  pfree(arg1);
1985  if (PointerGetDatum(arg2) != y)
1986  pfree(arg2);
1987 
1988  return result;
1989 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PointerGetDatum(X)
Definition: postgres.h:564
#define Min(x, y)
Definition: c.h:802
void pfree(void *pointer)
Definition: mcxt.c:992
int bpchartruelen(char *s, int len)
Definition: varchar.c:660
#define DatumGetBpCharPP(X)
Definition: fmgr.h:251
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2561 of file varlena.c.

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

2562 {
2563  text *arg1 = PG_GETARG_TEXT_PP(0);
2564  text *arg2 = PG_GETARG_TEXT_PP(1);
2565  int result;
2566 
2567  result = internal_text_pattern_compare(arg1, arg2);
2568 
2569  PG_FREE_IF_COPY(arg1, 0);
2570  PG_FREE_IF_COPY(arg2, 1);
2571 
2572  PG_RETURN_INT32(result);
2573 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2475
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2577 of file varlena.c.

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

2578 {
2580  MemoryContext oldcontext;
2581 
2582  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2583 
2584  /* Use generic string SortSupport, forcing "C" collation */
2585  varstr_sortsupport(ssup, C_COLLATION_OID, false);
2586 
2587  MemoryContextSwitchTo(oldcontext);
2588 
2589  PG_RETURN_VOID();
2590 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1766
#define PG_RETURN_VOID()
Definition: fmgr.h:293
#define C_COLLATION_OID
Definition: pg_collation.h:71
Datum bttextcmp ( PG_FUNCTION_ARGS  )

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

1725 {
1726  text *arg1 = PG_GETARG_TEXT_PP(0);
1727  text *arg2 = PG_GETARG_TEXT_PP(1);
1728  int32 result;
1729 
1730  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1731 
1732  PG_FREE_IF_COPY(arg1, 0);
1733  PG_FREE_IF_COPY(arg2, 1);
1734 
1735  PG_RETURN_INT32(result);
1736 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GET_COLLATION()
Definition: fmgr.h:155
signed int int32
Definition: c.h:253
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1739 of file varlena.c.

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

1740 {
1742  Oid collid = ssup->ssup_collation;
1743  MemoryContext oldcontext;
1744 
1745  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1746 
1747  /* Use generic string SortSupport */
1748  varstr_sortsupport(ssup, collid, false);
1749 
1750  MemoryContextSwitchTo(oldcontext);
1751 
1752  PG_RETURN_VOID();
1753 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1766
#define PG_RETURN_VOID()
Definition: fmgr.h:293
static bytea * bytea_catenate ( bytea t1,
bytea t2 
)
static

Definition at line 2631 of file varlena.c.

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

Referenced by bytea_overlay(), and byteacat().

2632 {
2633  bytea *result;
2634  int len1,
2635  len2,
2636  len;
2637  char *ptr;
2638 
2639  len1 = VARSIZE_ANY_EXHDR(t1);
2640  len2 = VARSIZE_ANY_EXHDR(t2);
2641 
2642  /* paranoia ... probably should throw error instead? */
2643  if (len1 < 0)
2644  len1 = 0;
2645  if (len2 < 0)
2646  len2 = 0;
2647 
2648  len = len1 + len2 + VARHDRSZ;
2649  result = (bytea *) palloc(len);
2650 
2651  /* Set size of result string... */
2652  SET_VARSIZE(result, len);
2653 
2654  /* Fill data field of result string... */
2655  ptr = VARDATA(result);
2656  if (len1 > 0)
2657  memcpy(ptr, VARDATA_ANY(t1), len1);
2658  if (len2 > 0)
2659  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2660 
2661  return result;
2662 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static bytea * bytea_overlay ( bytea t1,
bytea t2,
int  sp,
int  sl 
)
static

Definition at line 2788 of file varlena.c.

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

Referenced by byteaoverlay(), and byteaoverlay_no_len().

2789 {
2790  bytea *result;
2791  bytea *s1;
2792  bytea *s2;
2793  int sp_pl_sl;
2794 
2795  /*
2796  * Check for possible integer-overflow cases. For negative sp, throw a
2797  * "substring length" error because that's what should be expected
2798  * according to the spec's definition of OVERLAY().
2799  */
2800  if (sp <= 0)
2801  ereport(ERROR,
2802  (errcode(ERRCODE_SUBSTRING_ERROR),
2803  errmsg("negative substring length not allowed")));
2804  sp_pl_sl = sp + sl;
2805  if (sp_pl_sl <= sl)
2806  ereport(ERROR,
2807  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2808  errmsg("integer out of range")));
2809 
2810  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
2811  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
2812  result = bytea_catenate(s1, t2);
2813  result = bytea_catenate(result, s2);
2814 
2815  return result;
2816 }
#define PointerGetDatum(X)
Definition: postgres.h:564
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2631
int errcode(int sqlerrcode)
Definition: elog.c:575
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2706
#define ERROR
Definition: elog.h:43
char * s1
#define ereport(elevel, rest)
Definition: elog.h:122
char * s2
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3518 of file varlena.c.

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

3519 {
3521  MemoryContext oldcontext;
3522 
3523  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3524 
3525  /* Use generic string SortSupport, forcing "C" collation */
3526  varstr_sortsupport(ssup, C_COLLATION_OID, false);
3527 
3528  MemoryContextSwitchTo(oldcontext);
3529 
3530  PG_RETURN_VOID();
3531 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1766
#define PG_RETURN_VOID()
Definition: fmgr.h:293
#define C_COLLATION_OID
Definition: pg_collation.h:71
Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 484 of file varlena.c.

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

485 {
487 
488  /* cannot be called directly because of internal-type argument */
489  Assert(AggCheckCallContext(fcinfo, NULL));
490 
491  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
492 
493  if (state != NULL)
494  {
495  bytea *result;
496 
497  result = (bytea *) palloc(state->len + VARHDRSZ);
498  SET_VARSIZE(result, state->len + VARHDRSZ);
499  memcpy(VARDATA(result), state->data, state->len);
500  PG_RETURN_BYTEA_P(result);
501  }
502  else
503  PG_RETURN_NULL();
504 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3660
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 452 of file varlena.c.

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

453 {
455 
456  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
457 
458  /* Append the value unless null. */
459  if (!PG_ARGISNULL(1))
460  {
462 
463  /* On the first time through, we ignore the delimiter. */
464  if (state == NULL)
465  state = makeStringAggState(fcinfo);
466  else if (!PG_ARGISNULL(2))
467  {
468  bytea *delim = PG_GETARG_BYTEA_PP(2);
469 
471  }
472 
474  }
475 
476  /*
477  * The transition type for string_agg() is declared to be "internal",
478  * which is a pass-by-value type the same size as a pointer.
479  */
480  PG_RETURN_POINTER(state);
481 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
static struct @76 value
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4527
Definition: c.h:435
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 2683 of file varlena.c.

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

2684 {
2686  PG_GETARG_INT32(1),
2687  PG_GETARG_INT32(2),
2688  false));
2689 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2706
Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2697 of file varlena.c.

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

2698 {
2700  PG_GETARG_INT32(1),
2701  -1,
2702  true));
2703 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2706
static bytea * bytea_substring ( Datum  str,
int  S,
int  L,
bool  length_not_specified 
)
static

Definition at line 2706 of file varlena.c.

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

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

2710 {
2711  int S1; /* adjusted start position */
2712  int L1; /* adjusted substring length */
2713 
2714  S1 = Max(S, 1);
2715 
2716  if (length_not_specified)
2717  {
2718  /*
2719  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
2720  * end of the string if we pass it a negative value for length.
2721  */
2722  L1 = -1;
2723  }
2724  else
2725  {
2726  /* end position */
2727  int E = S + L;
2728 
2729  /*
2730  * A negative value for L is the only way for the end position to be
2731  * before the start. SQL99 says to throw an error.
2732  */
2733  if (E < S)
2734  ereport(ERROR,
2735  (errcode(ERRCODE_SUBSTRING_ERROR),
2736  errmsg("negative substring length not allowed")));
2737 
2738  /*
2739  * A zero or negative value for the end position can happen if the
2740  * start was negative or one. SQL99 says to return a zero-length
2741  * string.
2742  */
2743  if (E < 1)
2744  return PG_STR_GET_BYTEA("");
2745 
2746  L1 = E - S1;
2747  }
2748 
2749  /*
2750  * If the start position is past the end of the string, SQL99 says to
2751  * return a zero-length string -- DatumGetByteaPSlice() will do that for
2752  * us. Convert to zero-based starting position
2753  */
2754  return DatumGetByteaPSlice(str, S1 - 1, L1);
2755 }
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:262
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:2664
#define ERROR
Definition: elog.h:43
#define S(n, x)
Definition: sha1.c:55
#define ereport(elevel, rest)
Definition: elog.h:122
#define Max(x, y)
Definition: c.h:796
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2616 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

2617 {
2618  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2619  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2620 
2622 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2631
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
Definition: c.h:435
Datum byteacmp ( PG_FUNCTION_ARGS  )

Definition at line 3496 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(), and gbt_byteacmp().

3497 {
3498  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3499  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3500  int len1,
3501  len2;
3502  int cmp;
3503 
3504  len1 = VARSIZE_ANY_EXHDR(arg1);
3505  len2 = VARSIZE_ANY_EXHDR(arg2);
3506 
3507  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3508  if ((cmp == 0) && (len1 != len2))
3509  cmp = (len1 < len2) ? -1 : 1;
3510 
3511  PG_FREE_IF_COPY(arg1, 0);
3512  PG_FREE_IF_COPY(arg2, 1);
3513 
3514  PG_RETURN_INT32(cmp);
3515 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum byteaeq ( PG_FUNCTION_ARGS  )

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

3353 {
3354  Datum arg1 = PG_GETARG_DATUM(0);
3355  Datum arg2 = PG_GETARG_DATUM(1);
3356  bool result;
3357  Size len1,
3358  len2;
3359 
3360  /*
3361  * We can use a fast path for unequal lengths, which might save us from
3362  * having to detoast one or both values.
3363  */
3364  len1 = toast_raw_datum_size(arg1);
3365  len2 = toast_raw_datum_size(arg2);
3366  if (len1 != len2)
3367  result = false;
3368  else
3369  {
3370  bytea *barg1 = DatumGetByteaPP(arg1);
3371  bytea *barg2 = DatumGetByteaPP(arg2);
3372 
3373  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3374  len1 - VARHDRSZ) == 0);
3375 
3376  PG_FREE_IF_COPY(barg1, 0);
3377  PG_FREE_IF_COPY(barg2, 1);
3378  }
3379 
3380  PG_RETURN_BOOL(result);
3381 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
#define DatumGetByteaPP(X)
Definition: fmgr.h:247
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
size_t Size
Definition: c.h:353
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum byteage ( PG_FUNCTION_ARGS  )

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

3477 {
3478  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3479  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3480  int len1,
3481  len2;
3482  int cmp;
3483 
3484  len1 = VARSIZE_ANY_EXHDR(arg1);
3485  len2 = VARSIZE_ANY_EXHDR(arg2);
3486 
3487  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3488 
3489  PG_FREE_IF_COPY(arg1, 0);
3490  PG_FREE_IF_COPY(arg2, 1);
3491 
3492  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
3493 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum byteaGetBit ( PG_FUNCTION_ARGS  )

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

2899 {
2900  bytea *v = PG_GETARG_BYTEA_PP(0);
2901  int32 n = PG_GETARG_INT32(1);
2902  int byteNo,
2903  bitNo;
2904  int len;
2905  int byte;
2906 
2907  len = VARSIZE_ANY_EXHDR(v);
2908 
2909  if (n < 0 || n >= len * 8)
2910  ereport(ERROR,
2911  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2912  errmsg("index %d out of valid range, 0..%d",
2913  n, len * 8 - 1)));
2914 
2915  byteNo = n / 8;
2916  bitNo = n % 8;
2917 
2918  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
2919 
2920  if (byte & (1 << bitNo))
2921  PG_RETURN_INT32(1);
2922  else
2923  PG_RETURN_INT32(0);
2924 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum byteaGetByte ( PG_FUNCTION_ARGS  )

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

2870 {
2871  bytea *v = PG_GETARG_BYTEA_PP(0);
2872  int32 n = PG_GETARG_INT32(1);
2873  int len;
2874  int byte;
2875 
2876  len = VARSIZE_ANY_EXHDR(v);
2877 
2878  if (n < 0 || n >= len)
2879  ereport(ERROR,
2880  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2881  errmsg("index %d out of valid range, 0..%d",
2882  n, len - 1)));
2883 
2884  byte = ((unsigned char *) VARDATA_ANY(v))[n];
2885 
2886  PG_RETURN_INT32(byte);
2887 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum byteagt ( PG_FUNCTION_ARGS  )

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

3457 {
3458  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3459  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3460  int len1,
3461  len2;
3462  int cmp;
3463 
3464  len1 = VARSIZE_ANY_EXHDR(arg1);
3465  len2 = VARSIZE_ANY_EXHDR(arg2);
3466 
3467  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3468 
3469  PG_FREE_IF_COPY(arg1, 0);
3470  PG_FREE_IF_COPY(arg2, 1);
3471 
3472  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
3473 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum byteain ( PG_FUNCTION_ARGS  )

Definition at line 257 of file varlena.c.

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

Referenced by CreateTrigger(), and string_to_datum().

258 {
259  char *inputText = PG_GETARG_CSTRING(0);
260  char *tp;
261  char *rp;
262  int bc;
263  bytea *result;
264 
265  /* Recognize hex input */
266  if (inputText[0] == '\\' && inputText[1] == 'x')
267  {
268  size_t len = strlen(inputText);
269 
270  bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */
271  result = palloc(bc);
272  bc = hex_decode(inputText + 2, len - 2, VARDATA(result));
273  SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */
274 
275  PG_RETURN_BYTEA_P(result);
276  }
277 
278  /* Else, it's the traditional escaped style */
279  for (bc = 0, tp = inputText; *tp != '\0'; bc++)
280  {
281  if (tp[0] != '\\')
282  tp++;
283  else if ((tp[0] == '\\') &&
284  (tp[1] >= '0' && tp[1] <= '3') &&
285  (tp[2] >= '0' && tp[2] <= '7') &&
286  (tp[3] >= '0' && tp[3] <= '7'))
287  tp += 4;
288  else if ((tp[0] == '\\') &&
289  (tp[1] == '\\'))
290  tp += 2;
291  else
292  {
293  /*
294  * one backslash, not followed by another or ### valid octal
295  */
296  ereport(ERROR,
297  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
298  errmsg("invalid input syntax for type %s", "bytea")));
299  }
300  }
301 
302  bc += VARHDRSZ;
303 
304  result = (bytea *) palloc(bc);
305  SET_VARSIZE(result, bc);
306 
307  tp = inputText;
308  rp = VARDATA(result);
309  while (*tp != '\0')
310  {
311  if (tp[0] != '\\')
312  *rp++ = *tp++;
313  else if ((tp[0] == '\\') &&
314  (tp[1] >= '0' && tp[1] <= '3') &&
315  (tp[2] >= '0' && tp[2] <= '7') &&
316  (tp[3] >= '0' && tp[3] <= '7'))
317  {
318  bc = VAL(tp[1]);
319  bc <<= 3;
320  bc += VAL(tp[2]);
321  bc <<= 3;
322  *rp++ = bc + VAL(tp[3]);
323 
324  tp += 4;
325  }
326  else if ((tp[0] == '\\') &&
327  (tp[1] == '\\'))
328  {
329  *rp++ = '\\';
330  tp += 2;
331  }
332  else
333  {
334  /*
335  * We should never get here. The first pass should not allow it.
336  */
337  ereport(ERROR,
338  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
339  errmsg("invalid input syntax for type %s", "bytea")));
340  }
341  }
342 
343  PG_RETURN_BYTEA_P(result);
344 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
unsigned hex_decode(const char *src, unsigned len, char *dst)
Definition: encode.c:156
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
#define VAL(CH)
Definition: varlena.c:242
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
Datum byteale ( PG_FUNCTION_ARGS  )

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

3437 {
3438  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3439  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3440  int len1,
3441  len2;
3442  int cmp;
3443 
3444  len1 = VARSIZE_ANY_EXHDR(arg1);
3445  len2 = VARSIZE_ANY_EXHDR(arg2);
3446 
3447  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3448 
3449  PG_FREE_IF_COPY(arg1, 0);
3450  PG_FREE_IF_COPY(arg2, 1);
3451 
3452  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
3453 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bytealt ( PG_FUNCTION_ARGS  )

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

3417 {
3418  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3419  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3420  int len1,
3421  len2;
3422  int cmp;
3423 
3424  len1 = VARSIZE_ANY_EXHDR(arg1);
3425  len2 = VARSIZE_ANY_EXHDR(arg2);
3426 
3427  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3428 
3429  PG_FREE_IF_COPY(arg1, 0);
3430  PG_FREE_IF_COPY(arg2, 1);
3431 
3432  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3433 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum byteane ( PG_FUNCTION_ARGS  )

Definition at line 3384 of file varlena.c.

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

3385 {
3386  Datum arg1 = PG_GETARG_DATUM(0);
3387  Datum arg2 = PG_GETARG_DATUM(1);
3388  bool result;
3389  Size len1,
3390  len2;
3391 
3392  /*
3393  * We can use a fast path for unequal lengths, which might save us from
3394  * having to detoast one or both values.
3395  */
3396  len1 = toast_raw_datum_size(arg1);
3397  len2 = toast_raw_datum_size(arg2);
3398  if (len1 != len2)
3399  result = true;
3400  else
3401  {
3402  bytea *barg1 = DatumGetByteaPP(arg1);
3403  bytea *barg2 = DatumGetByteaPP(arg2);
3404 
3405  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3406  len1 - VARHDRSZ) != 0);
3407 
3408  PG_FREE_IF_COPY(barg1, 0);
3409  PG_FREE_IF_COPY(barg2, 1);
3410  }
3411 
3412  PG_RETURN_BOOL(result);
3413 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
#define DatumGetByteaPP(X)
Definition: fmgr.h:247
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
size_t Size
Definition: c.h:353
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 2600 of file varlena.c.

References PG_GETARG_DATUM, PG_RETURN_INT32, toast_raw_datum_size(), and VARHDRSZ.

2601 {
2602  Datum str = PG_GETARG_DATUM(0);
2603 
2604  /* We need not detoast the input at all */
2606 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define VARHDRSZ
Definition: c.h:441
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
uintptr_t Datum
Definition: postgres.h:374
Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 353 of file varlena.c.

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

Referenced by patternsel(), and prefix_quals().

354 {
355  bytea *vlena = PG_GETARG_BYTEA_PP(0);
356  char *result;
357  char *rp;
358 
360  {
361  /* Print hex format */
362  rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
363  *rp++ = '\\';
364  *rp++ = 'x';
365  rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
366  }
367  else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
368  {
369  /* Print traditional escaped format */
370  char *vp;
371  int len;
372  int i;
373 
374  len = 1; /* empty string has 1 char */
375  vp = VARDATA_ANY(vlena);
376  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
377  {
378  if (*vp == '\\')
379  len += 2;
380  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
381  len += 4;
382  else
383  len++;
384  }
385  rp = result = (char *) palloc(len);
386  vp = VARDATA_ANY(vlena);
387  for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
388  {
389  if (*vp == '\\')
390  {
391  *rp++ = '\\';
392  *rp++ = '\\';
393  }
394  else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
395  {
396  int val; /* holds unprintable chars */
397 
398  val = *vp;
399  rp[0] = '\\';
400  rp[3] = DIG(val & 07);
401  val >>= 3;
402  rp[2] = DIG(val & 07);
403  val >>= 3;
404  rp[1] = DIG(val & 03);
405  rp += 4;
406  }
407  else
408  *rp++ = *vp;
409  }
410  }
411  else
412  {
413  elog(ERROR, "unrecognized bytea_output setting: %d",
414  bytea_output);
415  rp = result = NULL; /* keep compiler quiet */
416  }
417  *rp = '\0';
418  PG_RETURN_CSTRING(result);
419 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
unsigned hex_encode(const char *src, unsigned len, char *dst)
Definition: encode.c:126
#define ERROR
Definition: elog.h:43
int bytea_output
Definition: varlena.c:41
#define NULL
Definition: c.h:226
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:306
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
int i
Definition: c.h:435
#define elog
Definition: elog.h:219
long val
Definition: informix.c:689
#define DIG(VAL)
Definition: varlena.c:243
Datum byteaoverlay ( PG_FUNCTION_ARGS  )

Definition at line 2765 of file varlena.c.

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

2766 {
2767  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2768  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2769  int sp = PG_GETARG_INT32(2); /* substring start position */
2770  int sl = PG_GETARG_INT32(3); /* substring length */
2771 
2772  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2773 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2788
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
Definition: c.h:435
Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2776 of file varlena.c.

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

2777 {
2778  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2779  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2780  int sp = PG_GETARG_INT32(2); /* substring start position */
2781  int sl;
2782 
2783  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
2784  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2785 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2788
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 2825 of file varlena.c.

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

2826 {
2827  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2828  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2829  int pos;
2830  int px,
2831  p;
2832  int len1,
2833  len2;
2834  char *p1,
2835  *p2;
2836 
2837  len1 = VARSIZE_ANY_EXHDR(t1);
2838  len2 = VARSIZE_ANY_EXHDR(t2);
2839 
2840  if (len2 <= 0)
2841  PG_RETURN_INT32(1); /* result for empty pattern */
2842 
2843  p1 = VARDATA_ANY(t1);
2844  p2 = VARDATA_ANY(t2);
2845 
2846  pos = 0;
2847  px = (len1 - len2);
2848  for (p = 0; p <= px; p++)
2849  {
2850  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
2851  {
2852  pos = p + 1;
2853  break;
2854  };
2855  p1++;
2856  };
2857 
2858  PG_RETURN_INT32(pos);
2859 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
Definition: geqo_px.c:46
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum bytearecv ( PG_FUNCTION_ARGS  )

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

426 {
428  bytea *result;
429  int nbytes;
430 
431  nbytes = buf->len - buf->cursor;
432  result = (bytea *) palloc(nbytes + VARHDRSZ);
433  SET_VARSIZE(result, nbytes + VARHDRSZ);
434  pq_copymsgbytes(buf, VARDATA(result), nbytes);
435  PG_RETURN_BYTEA_P(result);
436 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
static char * buf
Definition: pg_test_fsync.c:65
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:570
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 444 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

445 {
446  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
447 
448  PG_RETURN_BYTEA_P(vlena);
449 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:277
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
Definition: c.h:435
Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 2974 of file varlena.c.

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

2975 {
2976  bytea *v = PG_GETARG_BYTEA_P(0);
2977  int32 n = PG_GETARG_INT32(1);
2978  int32 newBit = PG_GETARG_INT32(2);
2979  bytea *res;
2980  int len;
2981  int oldByte,
2982  newByte;
2983  int byteNo,
2984  bitNo;
2985 
2986  len = VARSIZE(v) - VARHDRSZ;
2987 
2988  if (n < 0 || n >= len * 8)
2989  ereport(ERROR,
2990  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2991  errmsg("index %d out of valid range, 0..%d",
2992  n, len * 8 - 1)));
2993 
2994  byteNo = n / 8;
2995  bitNo = n % 8;
2996 
2997  /*
2998  * sanity check!
2999  */
3000  if (newBit != 0 && newBit != 1)
3001  ereport(ERROR,
3002  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3003  errmsg("new bit must be 0 or 1")));
3004 
3005  /*
3006  * Make a copy of the original varlena.
3007  */
3008  res = (bytea *) palloc(VARSIZE(v));
3009  memcpy((char *) res, (char *) v, VARSIZE(v));
3010 
3011  /*
3012  * Update the byte.
3013  */
3014  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3015 
3016  if (newBit == 0)
3017  newByte = oldByte & (~(1 << bitNo));
3018  else
3019  newByte = oldByte | (1 << bitNo);
3020 
3021  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3022 
3023  PG_RETURN_BYTEA_P(res);
3024 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
signed int int32
Definition: c.h:253
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:267
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 2935 of file varlena.c.

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

2936 {
2937  bytea *v = PG_GETARG_BYTEA_P(0);
2938  int32 n = PG_GETARG_INT32(1);
2939  int32 newByte = PG_GETARG_INT32(2);
2940  int len;
2941  bytea *res;
2942 
2943  len = VARSIZE(v) - VARHDRSZ;
2944 
2945  if (n < 0 || n >= len)
2946  ereport(ERROR,
2947  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2948  errmsg("index %d out of valid range, 0..%d",
2949  n, len - 1)));
2950 
2951  /*
2952  * Make a copy of the original varlena.
2953  */
2954  res = (bytea *) palloc(VARSIZE(v));
2955  memcpy((char *) res, (char *) v, VARSIZE(v));
2956 
2957  /*
2958  * Now set the byte.
2959  */
2960  ((unsigned char *) VARDATA(res))[n] = newByte;
2961 
2962  PG_RETURN_BYTEA_P(res);
2963 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
signed int int32
Definition: c.h:253
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:267
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
static int charlen_to_bytelen ( const char *  p,
int  n 
)
static

Definition at line 737 of file varlena.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), replace_text(), replace_text_regexp(), and text_to_array_internal().

738 {
740  {
741  /* Optimization for single-byte encodings */
742  return n;
743  }
744  else
745  {
746  const char *s;
747 
748  for (s = p; n > 0; n--)
749  s += pg_mblen(s);
750 
751  return s - p;
752  }
753 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
static bool check_replace_text_has_escape_char ( const text replace_text)
static

Definition at line 3637 of file varlena.c.

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

Referenced by replace_text_regexp().

3638 {
3639  const char *p = VARDATA_ANY(replace_text);
3640  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3641 
3643  {
3644  for (; p < p_end; p++)
3645  {
3646  if (*p == '\\')
3647  return true;
3648  }
3649  }
3650  else
3651  {
3652  for (; p < p_end; p += pg_mblen(p))
3653  {
3654  if (*p == '\\')
3655  return true;
3656  }
3657  }
3658 
3659  return false;
3660 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
static text* concat_internal ( const char *  sepstr,
int  argidx,
FunctionCallInfo  fcinfo 
)
static

Definition at line 4600 of file varlena.c.

References appendStringInfoString(), array_to_text_internal(), Assert, cstring_to_text_with_len(), StringInfoData::data, elog, ERROR, FunctionCallInfoData::flinfo, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), getTypeOutputInfo(), i, initStringInfo(), StringInfoData::len, NULL, OidIsValid, OidOutputFunctionCall(), pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_NARGS, and value.

Referenced by text_concat(), and text_concat_ws().

4602 {
4603  text *result;
4604  StringInfoData str;
4605  bool first_arg = true;
4606  int i;
4607 
4608  /*
4609  * concat(VARIADIC some-array) is essentially equivalent to
4610  * array_to_text(), ie concat the array elements with the given separator.
4611  * So we just pass the case off to that code.
4612  */
4613  if (get_fn_expr_variadic(fcinfo->flinfo))
4614  {
4615  ArrayType *arr;
4616 
4617  /* Should have just the one argument */
4618  Assert(argidx == PG_NARGS() - 1);
4619 
4620  /* concat(VARIADIC NULL) is defined as NULL */
4621  if (PG_ARGISNULL(argidx))
4622  return NULL;
4623 
4624  /*
4625  * Non-null argument had better be an array. We assume that any call
4626  * context that could let get_fn_expr_variadic return true will have
4627  * checked that a VARIADIC-labeled parameter actually is an array. So
4628  * it should be okay to just Assert that it's an array rather than
4629  * doing a full-fledged error check.
4630  */
4632 
4633  /* OK, safe to fetch the array value */
4634  arr = PG_GETARG_ARRAYTYPE_P(argidx);
4635 
4636  /*
4637  * And serialize the array. We tell array_to_text to ignore null
4638  * elements, which matches the behavior of the loop below.
4639  */
4640  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
4641  }
4642 
4643  /* Normal case without explicit VARIADIC marker */
4644  initStringInfo(&str);
4645 
4646  for (i = argidx; i < PG_NARGS(); i++)
4647  {
4648  if (!PG_ARGISNULL(i))
4649  {
4651  Oid valtype;
4652  Oid typOutput;
4653  bool typIsVarlena;
4654 
4655  /* add separator if appropriate */
4656  if (first_arg)
4657  first_arg = false;
4658  else
4659  appendStringInfoString(&str, sepstr);
4660 
4661  /* call the appropriate type output function, append the result */
4662  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
4663  if (!OidIsValid(valtype))
4664  elog(ERROR, "could not determine data type of concat() input");
4665  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
4667  OidOutputFunctionCall(typOutput, value));
4668  }
4669  }
4670 
4671  result = cstring_to_text_with_len(str.data, str.len);
4672  pfree(str.data);
4673 
4674  return result;
4675 }
static struct @76 value
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2362
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
FmgrInfo * flinfo
Definition: fmgr.h:71
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:2220
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4254
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
uintptr_t Datum
Definition: postgres.h:374
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define PG_NARGS()
Definition: fmgr.h:160
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2525
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2006
int i
Definition: c.h:435
#define elog
Definition: elog.h:219
text* cstring_to_text ( const char *  s)

Definition at line 151 of file varlena.c.

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_page_type(), cash_words(), chkpass_rout(), 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(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_jsonb_path_all(), get_scalar(), hash_page_type(), hstore_to_json(), hstore_to_json_loose(), inet_abbrev(), initcap(), json_in(), json_typeof(), jsonb_array_element_text(), jsonb_object_field_text(), jsonb_typeof(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), pg_collation_for(), pg_crypt(), pg_describe_object(), pg_export_snapshot(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_tablespace_location(), pg_walfile_name(), pgsql_version(), pgxml_result_to_text(), quote_ident(), quote_ident_cstr(), quote_nullable(), sepgsql_getcon(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), set_config_by_name(), show_all_file_settings(), show_config_by_name(), show_config_by_name_missing_ok(), ShowAllGUCConfig(), soundex(), split_text(), ssl_cipher(), ssl_version(), string_to_text(), text_substring(), textin(), timeofday(), to_hex32(), to_hex64(), tsquerytree(), unaccent_dict(), upper(), X509_NAME_to_text(), xml_encode_special_chars(), xml_in(), and xml_recv().

152 {
153  return cstring_to_text_with_len(s, strlen(s));
154 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
text* cstring_to_text_with_len ( const char *  s,
int  len 
)

Definition at line 163 of file varlena.c.

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

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), compute_tsvector_stats(), concat_internal(), cstring_to_text(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), elements_worker_jsonb(), ExecEvalXml(), fsm_page_contents(), get_array_element_end(), get_array_end(), get_jsonb_path_all(), get_object_end(), get_object_field_end(), get_scalar(), gin_extract_tsquery(), gin_extract_tsvector(), hstore_akeys(), hstore_avals(), hstore_each(), hstore_fetchval(), hstore_skeys(), hstore_slice_to_array(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array(), json_build_array_noargs(), json_build_object(), json_build_object_noargs(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_array_element_text(), jsonb_object_field_text(), jsonb_pretty(), 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(), ssl_extension_info(), string_agg_finalfn(), stringinfo_to_xmltype(), text_format(), text_left(), text_right(), text_to_array_internal(), textrecv(), to_json(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), varchar(), varchar_input(), and xslt_process().

164 {
165  text *result = (text *) palloc(len + VARHDRSZ);
166 
167  SET_VARSIZE(result, len + VARHDRSZ);
168  memcpy(VARDATA(result), s, len);
169 
170  return result;
171 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

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

2476 {
2477  int result;
2478  int len1,
2479  len2;
2480 
2481  len1 = VARSIZE_ANY_EXHDR(arg1);
2482  len2 = VARSIZE_ANY_EXHDR(arg2);
2483 
2484  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2485  if (result != 0)
2486  return result;
2487  else if (len1 < len2)
2488  return -1;
2489  else if (len1 > len2)
2490  return 1;
2491  else
2492  return 0;
2493 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define Min(x, y)
Definition: c.h:802
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 4527 of file varlena.c.

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

4528 {
4529  StringInfo state;
4530  MemoryContext aggcontext;
4531  MemoryContext oldcontext;
4532 
4533  if (!AggCheckCallContext(fcinfo, &aggcontext))
4534  {
4535  /* cannot be called directly because of internal-type argument */
4536  elog(ERROR, "string_agg_transfn called in non-aggregate context");
4537  }
4538 
4539  /*
4540  * Create state in aggregate context. It'll stay there across subsequent
4541  * calls.
4542  */
4543  oldcontext = MemoryContextSwitchTo(aggcontext);
4544  state = makeStringInfo();
4545  MemoryContextSwitchTo(oldcontext);
4546 
4547  return state;
4548 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:43
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3660
#define elog
Definition: elog.h:219
Datum md5_bytea ( PG_FUNCTION_ARGS  )

Definition at line 4452 of file varlena.c.

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, MD5_HASH_LEN, PG_GETARG_BYTEA_PP, pg_md5_hash(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4453 {
4454  bytea *in = PG_GETARG_BYTEA_PP(0);
4455  size_t len;
4456  char hexsum[MD5_HASH_LEN + 1];
4457 
4458  len = VARSIZE_ANY_EXHDR(in);
4459  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
4460  ereport(ERROR,
4461  (errcode(ERRCODE_OUT_OF_MEMORY),
4462  errmsg("out of memory")));
4463 
4465 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define MD5_HASH_LEN
Definition: varlena.c:4425
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:151
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum md5_text ( PG_FUNCTION_ARGS  )

Definition at line 4428 of file varlena.c.

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, MD5_HASH_LEN, PG_GETARG_TEXT_PP, pg_md5_hash(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4429 {
4430  text *in_text = PG_GETARG_TEXT_PP(0);
4431  size_t len;
4432  char hexsum[MD5_HASH_LEN + 1];
4433 
4434  /* Calculate the length of the buffer using varlena metadata */
4435  len = VARSIZE_ANY_EXHDR(in_text);
4436 
4437  /* get the hash result */
4438  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
4439  ereport(ERROR,
4440  (errcode(ERRCODE_OUT_OF_MEMORY),
4441  errmsg("out of memory")));
4442 
4443  /* convert to text and return it */
4445 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define MD5_HASH_LEN
Definition: varlena.c:4425
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:151
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3054 of file varlena.c.

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

Referenced by nameiclike(), and nameicnlike().

3055 {
3056  Name s = PG_GETARG_NAME(0);
3057 
3059 }
Definition: c.h:489
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:151
#define NameStr(name)
Definition: c.h:495
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 4473 of file varlena.c.

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

4474 {
4476  int32 result;
4477  int typlen;
4478 
4479  /* On first call, get the input type's typlen, and save at *fn_extra */
4480  if (fcinfo->flinfo->fn_extra == NULL)
4481  {
4482  /* Lookup the datatype of the supplied argument */
4483  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4484 
4485  typlen = get_typlen(argtypeid);
4486  if (typlen == 0) /* should not happen */
4487  elog(ERROR, "cache lookup failed for type %u", argtypeid);
4488 
4489  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4490  sizeof(int));
4491  *((int *) fcinfo->flinfo->fn_extra) = typlen;
4492  }
4493  else
4494  typlen = *((int *) fcinfo->flinfo->fn_extra);
4495 
4496  if (typlen == -1)
4497  {
4498  /* varlena type, possibly toasted */
4499  result = toast_datum_size(value);
4500  }
4501  else if (typlen == -2)
4502  {
4503  /* cstring */
4504  result = strlen(DatumGetCString(value)) + 1;
4505  }
4506  else
4507  {
4508  /* ordinary fixed-width type */
4509  result = typlen;
4510  }
4511 
4512  PG_RETURN_INT32(result);
4513 }
static struct @76 value
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:253
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:574
Size toast_datum_size(Datum value)
Definition: tuptoaster.c:409
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:2220
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
int16 get_typlen(Oid typid)
Definition: lsyscache.c:1915
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
#define elog
Definition: elog.h:219
Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3554 of file varlena.c.

References appendBinaryStringInfo(), appendStringInfoText(), charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), StringInfoData::data, initStringInfo(), StringInfoData::len, TextPositionState::len1, TextPositionState::len2, pfree(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, text_position_cleanup(), text_position_next(), text_position_setup(), VARDATA_ANY, and VARSIZE_ANY.

Referenced by execute_extension_script().

3555 {
3556  text *src_text = PG_GETARG_TEXT_PP(0);
3557  text *from_sub_text = PG_GETARG_TEXT_PP(1);
3558  text *to_sub_text = PG_GETARG_TEXT_PP(2);
3559  int src_text_len;
3560  int from_sub_text_len;
3562  text *ret_text;
3563  int start_posn;
3564  int curr_posn;
3565  int chunk_len;
3566  char *start_ptr;
3567  StringInfoData str;
3568 
3569  text_position_setup(src_text, from_sub_text, &state);
3570 
3571  /*
3572  * Note: we check the converted string length, not the original, because
3573  * they could be different if the input contained invalid encoding.
3574  */
3575  src_text_len = state.len1;
3576  from_sub_text_len = state.len2;
3577 
3578  /* Return unmodified source string if empty source or pattern */
3579  if (src_text_len < 1 || from_sub_text_len < 1)
3580  {
3581  text_position_cleanup(&state);
3582  PG_RETURN_TEXT_P(src_text);
3583  }
3584 
3585  start_posn = 1;
3586  curr_posn = text_position_next(1, &state);
3587 
3588  /* When the from_sub_text is not found, there is nothing to do. */
3589  if (curr_posn == 0)
3590  {
3591  text_position_cleanup(&state);
3592  PG_RETURN_TEXT_P(src_text);
3593  }
3594 
3595  /* start_ptr points to the start_posn'th character of src_text */
3596  start_ptr = VARDATA_ANY(src_text);
3597 
3598  initStringInfo(&str);
3599 
3600  do
3601  {
3603 
3604  /* copy the data skipped over by last text_position_next() */
3605  chunk_len = charlen_to_bytelen(start_ptr, curr_posn - start_posn);
3606  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3607 
3608  appendStringInfoText(&str, to_sub_text);
3609 
3610  start_posn = curr_posn;
3611  start_ptr += chunk_len;
3612  start_posn += from_sub_text_len;
3613  start_ptr += charlen_to_bytelen(start_ptr, from_sub_text_len);
3614 
3615  curr_posn = text_position_next(start_posn, &state);
3616  }
3617  while (curr_posn > 0);
3618 
3619  /* copy trailing data */
3620  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
3621  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3622 
3623  text_position_cleanup(&state);
3624 
3625  ret_text = cstring_to_text_with_len(str.data, str.len);
3626  pfree(str.data);
3627 
3628  PG_RETURN_TEXT_P(ret_text);
3629 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1121
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
void pfree(void *pointer)
Definition: mcxt.c:992
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1233
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:737
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3540
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1367
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Definition: c.h:435
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
text* replace_text_regexp ( text src_text,
void *  regexp,
text replace_text,
bool  glob 
)

Definition at line 3775 of file varlena.c.

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

Referenced by textregexreplace(), and textregexreplace_noopt().

3777 {
3778  text *ret_text;
3779  regex_t *re = (regex_t *) regexp;
3780  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
3783  pg_wchar *data;
3784  size_t data_len;
3785  int search_start;
3786  int data_pos;
3787  char *start_ptr;
3788  bool have_escape;
3789 
3790  initStringInfo(&buf);
3791 
3792  /* Convert data string to wide characters. */
3793  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
3794  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
3795 
3796  /* Check whether replace_text has escape char. */
3797  have_escape = check_replace_text_has_escape_char(replace_text);
3798 
3799  /* start_ptr points to the data_pos'th character of src_text */
3800  start_ptr = (char *) VARDATA_ANY(src_text);
3801  data_pos = 0;
3802 
3803  search_start = 0;
3804  while (search_start <= data_len)
3805  {
3806  int regexec_result;
3807 
3809 
3810  regexec_result = pg_regexec(re,
3811  data,
3812  data_len,
3813  search_start,
3814  NULL, /* no details */
3816  pmatch,
3817  0);
3818 
3819  if (regexec_result == REG_NOMATCH)
3820  break;
3821 
3822  if (regexec_result != REG_OKAY)
3823  {
3824  char errMsg[100];
3825 
3827  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
3828  ereport(ERROR,
3829  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
3830  errmsg("regular expression failed: %s", errMsg)));
3831  }
3832 
3833  /*
3834  * Copy the text to the left of the match position. Note we are given
3835  * character not byte indexes.
3836  */
3837  if (pmatch[0].rm_so - data_pos > 0)
3838  {
3839  int chunk_len;
3840 
3841  chunk_len = charlen_to_bytelen(start_ptr,
3842  pmatch[0].rm_so - data_pos);
3843  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
3844 
3845  /*
3846  * Advance start_ptr over that text, to avoid multiple rescans of
3847  * it if the replace_text contains multiple back-references.
3848  */
3849  start_ptr += chunk_len;
3850  data_pos = pmatch[0].rm_so;
3851  }
3852 
3853  /*
3854  * Copy the replace_text. Process back references when the
3855  * replace_text has escape characters.
3856  */
3857  if (have_escape)
3858  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
3859  start_ptr, data_pos);
3860  else
3861  appendStringInfoText(&buf, replace_text);
3862 
3863  /* Advance start_ptr and data_pos over the matched text. */
3864  start_ptr += charlen_to_bytelen(start_ptr,
3865  pmatch[0].rm_eo - data_pos);
3866  data_pos = pmatch[0].rm_eo;
3867 
3868  /*
3869  * When global option is off, replace the first instance only.
3870  */
3871  if (!glob)
3872  break;
3873 
3874  /*
3875  * Advance search position. Normally we start the next search at the
3876  * end of the previous match; but if the match was of zero length, we
3877  * have to advance by one character, or we'd just find the same match
3878  * again.
3879  */
3880  search_start = data_pos;
3881  if (pmatch[0].rm_so == pmatch[0].rm_eo)
3882  search_start++;
3883  }
3884 
3885  /*
3886  * Copy the text to the right of the last match.
3887  */
3888  if (data_pos < data_len)
3889  {
3890  int chunk_len;
3891 
3892  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
3893  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
3894  }
3895 
3896  ret_text = cstring_to_text_with_len(buf.data, buf.len);
3897  pfree(buf.data);
3898  pfree(data);
3899 
3900  return ret_text;
3901 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
regoff_t rm_so
Definition: regex.h:85
int errcode(int sqlerrcode)
Definition: elog.c:575
regoff_t rm_eo
Definition: regex.h:86
void pfree(void *pointer)
Definition: mcxt.c:992
#define REG_OKAY
Definition: regex.h:137
#define ERROR
Definition: elog.h:43
static bool check_replace_text_has_escape_char(const text *replace_text)
Definition: varlena.c:3637
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:737
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3540
static char * buf
Definition: pg_test_fsync.c:65
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned int pg_wchar
Definition: mbprint.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define REGEXP_REPLACE_BACKREF_CNT
Definition: varlena.c:3764
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
#define NULL
Definition: c.h:226
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:3670
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition: regexec.c:172
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define REG_NOMATCH
Definition: regex.h:138
Definition: c.h:435
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
Definition: regex.h:55
static bool rest_of_char_same ( const char *  s1,
const char *  s2,
int  len 
)
inlinestatic

Definition at line 5327 of file varlena.c.

Referenced by varstr_levenshtein().

5328 {
5329  while (len > 0)
5330  {
5331  len--;
5332  if (s1[len] != s2[len])
5333  return false;
5334  }
5335  return true;
5336 }
char * s1
char * s2
Datum split_text ( PG_FUNCTION_ARGS  )

Definition at line 3910 of file varlena.c.

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, TextPositionState::len1, TextPositionState::len2, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, PointerGetDatum, text_position_cleanup(), text_position_next(), text_position_setup(), and text_substring().

3911 {
3912  text *inputstring = PG_GETARG_TEXT_PP(0);
3913  text *fldsep = PG_GETARG_TEXT_PP(1);
3914  int fldnum = PG_GETARG_INT32(2);
3915  int inputstring_len;
3916  int fldsep_len;
3918  int start_posn;
3919  int end_posn;
3920  text *result_text;
3921 
3922  /* field number is 1 based */
3923  if (fldnum < 1)
3924  ereport(ERROR,
3925  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3926  errmsg("field position must be greater than zero")));
3927 
3928  text_position_setup(inputstring, fldsep, &state);
3929 
3930  /*
3931  * Note: we check the converted string length, not the original, because
3932  * they could be different if the input contained invalid encoding.
3933  */
3934  inputstring_len = state.len1;
3935  fldsep_len = state.len2;
3936 
3937  /* return empty string for empty input string */
3938  if (inputstring_len < 1)
3939  {
3940  text_position_cleanup(&state);
3942  }
3943 
3944  /* empty field separator */
3945  if (fldsep_len < 1)
3946  {
3947  text_position_cleanup(&state);
3948  /* if first field, return input string, else empty string */
3949  if (fldnum == 1)
3950  PG_RETURN_TEXT_P(inputstring);
3951  else
3953  }
3954 
3955  /* identify bounds of first field */
3956  start_posn = 1;
3957  end_posn = text_position_next(1, &state);
3958 
3959  /* special case if fldsep not found at all */
3960  if (end_posn == 0)
3961  {
3962  text_position_cleanup(&state);
3963  /* if field 1 requested, return input string, else empty string */
3964  if (fldnum == 1)
3965  PG_RETURN_TEXT_P(inputstring);
3966  else
3968  }
3969 
3970  while (end_posn > 0 && --fldnum > 0)
3971  {
3972  /* identify bounds of next field */
3973  start_posn = end_posn + fldsep_len;
3974  end_posn = text_position_next(start_posn, &state);
3975  }
3976 
3977  text_position_cleanup(&state);
3978 
3979  if (fldnum > 0)
3980  {
3981  /* N'th field separator not found */
3982  /* if last field requested, return it, else empty string */
3983  if (fldnum == 1)
3984  result_text = text_substring(PointerGetDatum(inputstring),
3985  start_posn,
3986  -1,
3987  true);
3988  else
3989  result_text = cstring_to_text("");
3990  }
3991  else
3992  {
3993  /* non-last field requested */
3994  result_text = text_substring(PointerGetDatum(inputstring),
3995  start_posn,
3996  end_posn - start_posn,
3997  false);
3998  }
3999 
4000  PG_RETURN_TEXT_P(result_text);
4001 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PointerGetDatum(X)
Definition: postgres.h:564
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1121
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define ERROR
Definition: elog.h:43
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1233
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:816
#define ereport(elevel, rest)
Definition: elog.h:122
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1367
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:151
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
bool SplitDirectoriesString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3254 of file varlena.c.

References canonicalize_path(), lappend(), MAXPGPATH, memmove, NIL, NULL, and pstrdup().

Referenced by PostmasterMain().

3256 {
3257  char *nextp = rawstring;
3258  bool done = false;
3259 
3260  *namelist = NIL;
3261 
3262  while (isspace((unsigned char) *nextp))
3263  nextp++; /* skip leading whitespace */
3264 
3265  if (*nextp == '\0')
3266  return true; /* allow empty string */
3267 
3268  /* At the top of the loop, we are at start of a new directory. */
3269  do
3270  {
3271  char *curname;
3272  char *endp;
3273 
3274  if (*nextp == '"')
3275  {
3276  /* Quoted name --- collapse quote-quote pairs */
3277  curname = nextp + 1;
3278  for (;;)
3279  {
3280  endp = strchr(nextp + 1, '"');
3281  if (endp == NULL)
3282  return false; /* mismatched quotes */
3283  if (endp[1] != '"')
3284  break; /* found end of quoted name */
3285  /* Collapse adjacent quotes into one quote, and look again */
3286  memmove(endp, endp + 1, strlen(endp));
3287  nextp = endp;
3288  }
3289  /* endp now points at the terminating quote */
3290  nextp = endp + 1;
3291  }
3292  else
3293  {
3294  /* Unquoted name --- extends to separator or end of string */
3295  curname = endp = nextp;
3296  while (*nextp && *nextp != separator)
3297  {
3298  /* trailing whitespace should not be included in name */
3299  if (!isspace((unsigned char) *nextp))
3300  endp = nextp + 1;
3301  nextp++;
3302  }
3303  if (curname == endp)
3304  return false; /* empty unquoted name not allowed */
3305  }
3306 
3307  while (isspace((unsigned char) *nextp))
3308  nextp++; /* skip trailing whitespace */
3309 
3310  if (*nextp == separator)
3311  {
3312  nextp++;
3313  while (isspace((unsigned char) *nextp))
3314  nextp++; /* skip leading whitespace for next */
3315  /* we expect another name, so done remains false */
3316  }
3317  else if (*nextp == '\0')
3318  done = true;
3319  else
3320  return false; /* invalid syntax */
3321 
3322  /* Now safe to overwrite separator with a null */
3323  *endp = '\0';
3324 
3325  /* Truncate path if it's overlength */
3326  if (strlen(curname) >= MAXPGPATH)
3327  curname[MAXPGPATH - 1] = '\0';
3328 
3329  /*
3330  * Finished isolating current name --- add it to list
3331  */
3332  curname = pstrdup(curname);
3333  canonicalize_path(curname);
3334  *namelist = lappend(*namelist, curname);
3335 
3336  /* Loop back if we didn't reach end of string */
3337  } while (!done);
3338 
3339  return true;
3340 }
#define NIL
Definition: pg_list.h:69
char * pstrdup(const char *in)
Definition: mcxt.c:1165
void canonicalize_path(char *path)
Definition: path.c:254
#define MAXPGPATH
#define memmove(d, s, c)
Definition: c.h:1058
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
bool SplitIdentifierString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3129 of file varlena.c.

References Assert, downcase_truncate_identifier(), lappend(), memmove, NIL, NULL, pfree(), and truncate_identifier().

Referenced by check_datestyle(), check_log_destination(), check_search_path(), check_temp_tablespaces(), check_wal_consistency_checking(), ExtractExtensionList(), load_libraries(), parse_extension_control_file(), parse_output_parameters(), plpgsql_extra_checks_check_hook(), PostmasterMain(), PrepareTempTablespaces(), recomputeNamespacePath(), stringToQualifiedNameList(), and textToQualifiedNameList().

3131 {
3132  char *nextp = rawstring;
3133  bool done = false;
3134 
3135  *namelist = NIL;
3136 
3137  while (isspace((unsigned char) *nextp))
3138  nextp++; /* skip leading whitespace */
3139 
3140  if (*nextp == '\0')
3141  return true; /* allow empty string */
3142 
3143  /* At the top of the loop, we are at start of a new identifier. */
3144  do
3145  {
3146  char *curname;
3147  char *endp;
3148 
3149  if (*nextp == '"')
3150  {
3151  /* Quoted name --- collapse quote-quote pairs, no downcasing */
3152  curname = nextp + 1;
3153  for (;;)
3154  {
3155  endp = strchr(nextp + 1, '"');
3156  if (endp == NULL)
3157  return false; /* mismatched quotes */
3158  if (endp[1] != '"')
3159  break; /* found end of quoted name */
3160  /* Collapse adjacent quotes into one quote, and look again */
3161  memmove(endp, endp + 1, strlen(endp));
3162  nextp = endp;
3163  }
3164  /* endp now points at the terminating quote */
3165  nextp = endp + 1;
3166  }
3167  else
3168  {
3169  /* Unquoted name --- extends to separator or whitespace */
3170  char *downname;
3171  int len;
3172 
3173  curname = nextp;
3174  while (*nextp && *nextp != separator &&
3175  !isspace((unsigned char) *nextp))
3176  nextp++;
3177  endp = nextp;
3178  if (curname == nextp)
3179  return false; /* empty unquoted name not allowed */
3180 
3181  /*
3182  * Downcase the identifier, using same code as main lexer does.
3183  *
3184  * XXX because we want to overwrite the input in-place, we cannot
3185  * support a downcasing transformation that increases the string
3186  * length. This is not a problem given the current implementation
3187  * of downcase_truncate_identifier, but we'll probably have to do
3188  * something about this someday.
3189  */
3190  len = endp - curname;
3191  downname = downcase_truncate_identifier(curname, len, false);
3192  Assert(strlen(downname) <= len);
3193  strncpy(curname, downname, len); /* strncpy is required here */
3194  pfree(downname);
3195  }
3196 
3197  while (isspace((unsigned char) *nextp))
3198  nextp++; /* skip trailing whitespace */
3199 
3200  if (*nextp == separator)
3201  {
3202  nextp++;
3203  while (isspace((unsigned char) *nextp))
3204  nextp++; /* skip leading whitespace for next */
3205  /* we expect another name, so done remains false */
3206  }
3207  else if (*nextp == '\0')
3208  done = true;
3209  else
3210  return false; /* invalid syntax */
3211 
3212  /* Now safe to overwrite separator with a null */
3213  *endp = '\0';
3214 
3215  /* Truncate name if it's overlength */
3216  truncate_identifier(curname, strlen(curname), false);
3217 
3218  /*
3219  * Finished isolating current name --- add it to list
3220  */
3221  *namelist = lappend(*namelist, curname);
3222 
3223  /* Loop back if we didn't reach end of string */
3224  } while (!done);
3225 
3226  return true;
3227 }
#define NIL
Definition: pg_list.h:69
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
void truncate_identifier(char *ident, int len, bool warn)
Definition: scansup.c:187
void pfree(void *pointer)
Definition: mcxt.c:992
#define memmove(d, s, c)
Definition: c.h:1058
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 4577 of file varlena.c.

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

4578 {
4579  StringInfo state;
4580 
4581  /* cannot be called directly because of internal-type argument */
4582  Assert(AggCheckCallContext(fcinfo, NULL));
4583 
4584  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4585 
4586  if (state != NULL)
4588  else
4589  PG_RETURN_NULL();
4590 }
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3660
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 4551 of file varlena.c.

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

4552 {
4553  StringInfo state;
4554 
4555  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4556 
4557  /* Append the value unless null. */
4558  if (!PG_ARGISNULL(1))
4559  {
4560  /* On the first time through, we ignore the delimiter. */
4561  if (state == NULL)
4562  state = makeStringAggState(fcinfo);
4563  else if (!PG_ARGISNULL(2))
4564  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
4565 
4566  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
4567  }
4568 
4569  /*
4570  * The transition type for string_agg() is declared to be "internal",
4571  * which is a pass-by-value type the same size as a pointer.
4572  */
4573  PG_RETURN_POINTER(state);
4574 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3540
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4527
static text * text_catenate ( text t1,
text t2 
)
static

Definition at line 696 of file varlena.c.

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

Referenced by text_overlay(), and textcat().

697 {
698  text *result;
699  int len1,
700  len2,
701  len;
702  char *ptr;
703 
704  len1 = VARSIZE_ANY_EXHDR(t1);
705  len2 = VARSIZE_ANY_EXHDR(t2);
706 
707  /* paranoia ... probably should throw error instead? */
708  if (len1 < 0)
709  len1 = 0;
710  if (len2 < 0)
711  len2 = 0;
712 
713  len = len1 + len2 + VARHDRSZ;
714  result = (text *) palloc(len);
715 
716  /* Set size of result string... */
717  SET_VARSIZE(result, len);
718 
719  /* Fill data field of result string... */
720  ptr = VARDATA(result);
721  if (len1 > 0)
722  memcpy(ptr, VARDATA_ANY(t1), len1);
723  if (len2 > 0)
724  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
725 
726  return result;
727 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static int text_cmp ( text arg1,
text arg2,
Oid  collid 
)
static

Definition at line 1575 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(), and text_smaller().

1576 {
1577  char *a1p,
1578  *a2p;
1579  int len1,
1580  len2;
1581 
1582  a1p = VARDATA_ANY(arg1);
1583  a2p = VARDATA_ANY(arg2);
1584 
1585  len1 = VARSIZE_ANY_EXHDR(arg1);
1586  len2 = VARSIZE_ANY_EXHDR(arg2);
1587 
1588  return varstr_cmp(a1p, len1, a2p, len2, collid);
1589 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1384
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 4681 of file varlena.c.

References concat_internal(), NULL, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

4682 {
4683  text *result;
4684 
4685  result = concat_internal("", 0, fcinfo);
4686  if (result == NULL)
4687  PG_RETURN_NULL();
4688  PG_RETURN_TEXT_P(result);
4689 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define NULL
Definition: c.h:226
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4600
Definition: c.h:435
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 4696 of file varlena.c.

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

4697 {
4698  char *sep;
4699  text *result;
4700 
4701  /* return NULL when separator is NULL */
4702  if (PG_ARGISNULL(0))
4703  PG_RETURN_NULL();
4705 
4706  result = concat_internal(sep, 1, fcinfo);
4707  if (result == NULL)
4708  PG_RETURN_NULL();
4709  PG_RETURN_TEXT_P(result);
4710 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4600
char * text_to_cstring(const text *t)
Definition: varlena.c:184
Definition: c.h:435
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 4813 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(), INT2OID, INT4OID, InvalidOid, StringInfoData::len, NULL, OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, pg_atoi(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, text_format_parse_format(), text_format_string_conversion(), value, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by text_format_nv().

4814 {
4815  text *fmt;
4816  StringInfoData str;
4817  const char *cp;
4818  const char *start_ptr;
4819  const char *end_ptr;
4820  text *result;
4821  int arg;
4822  bool funcvariadic;
4823  int nargs;
4824  Datum *elements = NULL;
4825  bool *nulls = NULL;
4826  Oid element_type = InvalidOid;
4827  Oid prev_type = InvalidOid;
4828  Oid prev_width_type = InvalidOid;
4829  FmgrInfo typoutputfinfo;
4830  FmgrInfo typoutputinfo_width;
4831 
4832  /* When format string is null, immediately return null */
4833  if (PG_ARGISNULL(0))
4834  PG_RETURN_NULL();
4835 
4836  /* If argument is marked VARIADIC, expand array into elements */
4837  if (get_fn_expr_variadic(fcinfo->flinfo))
4838  {
4839  ArrayType *arr;
4840  int16 elmlen;
4841  bool elmbyval;
4842  char elmalign;
4843  int nitems;
4844 
4845  /* Should have just the one argument */
4846  Assert(PG_NARGS() == 2);
4847 
4848  /* If argument is NULL, we treat it as zero-length array */
4849  if (PG_ARGISNULL(1))
4850  nitems = 0;
4851  else
4852  {
4853  /*
4854  * Non-null argument had better be an array. We assume that any
4855  * call context that could let get_fn_expr_variadic return true
4856  * will have checked that a VARIADIC-labeled parameter actually is
4857  * an array. So it should be okay to just Assert that it's an
4858  * array rather than doing a full-fledged error check.
4859  */
4861 
4862  /* OK, safe to fetch the array value */
4863  arr = PG_GETARG_ARRAYTYPE_P(1);
4864 
4865  /* Get info about array element type */
4866  element_type = ARR_ELEMTYPE(arr);
4867  get_typlenbyvalalign(element_type,
4868  &elmlen, &elmbyval, &elmalign);
4869 
4870  /* Extract all array elements */
4871  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
4872  &elements, &nulls, &nitems);
4873  }
4874 
4875  nargs = nitems + 1;
4876  funcvariadic = true;
4877  }
4878  else
4879  {
4880  /* Non-variadic case, we'll process the arguments individually */
4881  nargs = PG_NARGS();
4882  funcvariadic = false;
4883  }
4884 
4885  /* Setup for main loop. */
4886  fmt = PG_GETARG_TEXT_PP(0);
4887  start_ptr = VARDATA_ANY(fmt);
4888  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
4889  initStringInfo(&str);
4890  arg = 1; /* next argument position to print */
4891 
4892  /* Scan format string, looking for conversion specifiers. */
4893  for (cp = start_ptr; cp < end_ptr; cp++)
4894  {
4895  int argpos;
4896  int widthpos;
4897  int flags;
4898  int width;
4899  Datum value;
4900  bool isNull;
4901  Oid typid;
4902 
4903  /*
4904  * If it's not the start of a conversion specifier, just copy it to
4905  * the output buffer.
4906  */
4907  if (*cp != '%')
4908  {
4909  appendStringInfoCharMacro(&str, *cp);
4910  continue;
4911  }
4912 
4913  ADVANCE_PARSE_POINTER(cp, end_ptr);
4914 
4915  /* Easy case: %% outputs a single % */
4916  if (*cp == '%')
4917  {
4918  appendStringInfoCharMacro(&str, *cp);
4919  continue;
4920  }
4921 
4922  /* Parse the optional portions of the format specifier */
4923  cp = text_format_parse_format(cp, end_ptr,
4924  &argpos, &widthpos,
4925  &flags, &width);
4926 
4927  /*
4928  * Next we should see the main conversion specifier. Whether or not
4929  * an argument position was present, it's known that at least one
4930  * character remains in the string at this point. Experience suggests
4931  * that it's worth checking that that character is one of the expected
4932  * ones before we try to fetch arguments, so as to produce the least
4933  * confusing response to a mis-formatted specifier.
4934  */
4935  if (strchr("sIL", *cp) == NULL)
4936  ereport(ERROR,
4937  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4938  errmsg("unrecognized format() type specifier \"%c\"",
4939  *cp),
4940  errhint("For a single \"%%\" use \"%%%%\".")));
4941 
4942  /* If indirect width was specified, get its value */
4943  if (widthpos >= 0)
4944  {
4945  /* Collect the specified or next argument position */
4946  if (widthpos > 0)
4947  arg = widthpos;
4948  if (arg >= nargs)
4949  ereport(ERROR,
4950  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4951  errmsg("too few arguments for format()")));
4952 
4953  /* Get the value and type of the selected argument */
4954  if (!funcvariadic)
4955  {
4956  value = PG_GETARG_DATUM(arg);
4957  isNull = PG_ARGISNULL(arg);
4958  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
4959  }
4960  else
4961  {
4962  value = elements[arg - 1];
4963  isNull = nulls[arg - 1];
4964  typid = element_type;
4965  }
4966  if (!OidIsValid(typid))
4967  elog(ERROR, "could not determine data type of format() input");
4968 
4969  arg++;
4970 
4971  /* We can treat NULL width the same as zero */
4972  if (isNull)
4973  width = 0;
4974  else if (typid == INT4OID)
4975  width = DatumGetInt32(value);
4976  else if (typid == INT2OID)
4977  width = DatumGetInt16(value);
4978  else
4979  {
4980  /* For less-usual datatypes, convert to text then to int */
4981  char *str;
4982 
4983  if (typid != prev_width_type)
4984  {
4985  Oid typoutputfunc;
4986  bool typIsVarlena;
4987 
4988  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
4989  fmgr_info(typoutputfunc, &typoutputinfo_width);
4990  prev_width_type = typid;
4991  }
4992 
4993  str = OutputFunctionCall(&typoutputinfo_width, value);
4994 
4995  /* pg_atoi will complain about bad data or overflow */
4996  width = pg_atoi(str, sizeof(int), '\0');
4997 
4998  pfree(str);
4999  }
5000  }
5001 
5002  /* Collect the specified or next argument position */
5003  if (argpos > 0)
5004  arg = argpos;
5005  if (arg >= nargs)
5006  ereport(ERROR,
5007  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5008  errmsg("too few arguments for format()")));
5009 
5010  /* Get the value and type of the selected argument */
5011  if (!funcvariadic)
5012  {
5013  value = PG_GETARG_DATUM(arg);
5014  isNull = PG_ARGISNULL(arg);
5015  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5016  }
5017  else
5018  {
5019  value = elements[arg - 1];
5020  isNull = nulls[arg - 1];
5021  typid = element_type;
5022  }
5023  if (!OidIsValid(typid))
5024  elog(ERROR, "could not determine data type of format() input");
5025 
5026  arg++;
5027 
5028  /*
5029  * Get the appropriate typOutput function, reusing previous one if
5030  * same type as previous argument. That's particularly useful in the
5031  * variadic-array case, but often saves work even for ordinary calls.
5032  */
5033  if (typid != prev_type)
5034  {
5035  Oid typoutputfunc;
5036  bool typIsVarlena;
5037 
5038  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5039  fmgr_info(typoutputfunc, &typoutputfinfo);
5040  prev_type = typid;
5041  }
5042 
5043  /*
5044  * And now we can format the value.
5045  */
5046  switch (*cp)
5047  {
5048  case 's':
5049  case 'I':
5050  case 'L':
5051  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5052  value, isNull,
5053  flags, width);
5054  break;
5055  default:
5056  /* should not get here, because of previous check */
5057  ereport(ERROR,
5058  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5059  errmsg("unrecognized format() type specifier \"%c\"",
5060  *cp),
5061  errhint("For a single \"%%\" use \"%%%%\".")));
5062  break;
5063  }
5064  }
5065 
5066  /* Don't need deconstruct_array results anymore. */
5067  if (elements != NULL)
5068  pfree(elements);
5069  if (nulls != NULL)
5070  pfree(nulls);
5071 
5072  /* Generate results. */
5073  result = cstring_to_text_with_len(str.data, str.len);
5074  pfree(str.data);
5075 
5076  PG_RETURN_TEXT_P(result);
5077 }
signed short int16
Definition: c.h:252
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4800
Definition: fmgr.h:53
static struct @76 value
int errhint(const char *fmt,...)
Definition: elog.c:987
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define DatumGetInt32(X)
Definition: postgres.h:480
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:5216
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:1989
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define INT4OID
Definition: pg_type.h:316
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2362
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1926
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:135
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:2220
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define INT2OID
Definition: pg_type.h:308
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define DatumGetInt16(X)
Definition: postgres.h:452
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define PG_NARGS()
Definition: fmgr.h:160
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2525
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Definition: c.h:435
#define elog
Definition: elog.h:219
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:5139
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define PG_RETURN_NULL()
Definition: fmgr.h:289
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37
static void text_format_append_string ( StringInfo  buf,
const char *  str,
int  flags,
int  width 
)
static

Definition at line 5265 of file varlena.c.

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

Referenced by text_format_string_conversion().

5267 {
5268  bool align_to_left = false;
5269  int len;
5270 
5271  /* fast path for typical easy case */
5272  if (width == 0)
5273  {
5274  appendStringInfoString(buf, str);
5275  return;
5276  }
5277 
5278  if (width < 0)
5279  {
5280  /* Negative width: implicit '-' flag, then take absolute value */
5281  align_to_left = true;
5282  /* -INT_MIN is undefined */
5283  if (width <= INT_MIN)
5284  ereport(ERROR,
5285  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5286  errmsg("number is out of range")));
5287  width = -width;
5288  }
5289  else if (flags & TEXT_FORMAT_FLAG_MINUS)
5290  align_to_left = true;
5291 
5292  len = pg_mbstrlen(str);
5293  if (align_to_left)
5294  {
5295  /* left justify */
5296  appendStringInfoString(buf, str);
5297  if (len < width)
5298  appendStringInfoSpaces(buf, width - len);
5299  }
5300  else
5301  {
5302  /* right justify */
5303  if (len < width)
5304  appendStringInfoSpaces(buf, width - len);
5305  appendStringInfoString(buf, str);
5306  }
5307 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:219
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:785
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4798
Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 5317 of file varlena.c.

References text_format().

5318 {
5319  return text_format(fcinfo);
5320 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:4813
static bool text_format_parse_digits ( const char **  ptr,
const char *  end_ptr,
int *  value 
)
static

Definition at line 5090 of file varlena.c.

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, newval, and val.

Referenced by text_format_parse_format().

5091 {
5092  bool found = false;
5093  const char *cp = *ptr;
5094  int val = 0;
5095 
5096  while (*cp >= '0' && *cp <= '9')
5097  {
5098  int newval = val * 10 + (*cp - '0');
5099 
5100  if (newval / 10 != val) /* overflow? */
5101  ereport(ERROR,
5102  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5103  errmsg("number is out of range")));
5104  val = newval;
5105  ADVANCE_PARSE_POINTER(cp, end_ptr);
5106  found = true;
5107  }
5108 
5109  *ptr = cp;
5110  *value = val;
5111 
5112  return found;
5113 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4800
static struct @76 value
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define newval
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
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 5139 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().

5142 {
5143  const char *cp = start_ptr;
5144  int n;
5145 
5146  /* set defaults for output parameters */
5147  *argpos = -1;
5148  *widthpos = -1;
5149  *flags = 0;
5150  *width = 0;
5151 
5152  /* try to identify first number */
5153  if (text_format_parse_digits(&cp, end_ptr, &n))
5154  {
5155  if (*cp != '$')
5156  {
5157  /* Must be just a width and a type, so we're done */
5158  *width = n;
5159  return cp;
5160  }
5161  /* The number was argument position */
5162  *argpos = n;
5163  /* Explicit 0 for argument index is immediately refused */
5164  if (n == 0)
5165  ereport(ERROR,
5166  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5167  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5168  ADVANCE_PARSE_POINTER(cp, end_ptr);
5169  }
5170 
5171  /* Handle flags (only minus is supported now) */
5172  while (*cp == '-')
5173  {
5174  *flags |= TEXT_FORMAT_FLAG_MINUS;
5175  ADVANCE_PARSE_POINTER(cp, end_ptr);
5176  }
5177 
5178  if (*cp == '*')
5179  {
5180  /* Handle indirect width */
5181  ADVANCE_PARSE_POINTER(cp, end_ptr);
5182  if (text_format_parse_digits(&cp, end_ptr, &n))
5183  {
5184  /* number in this position must be closed by $ */
5185  if (*cp != '$')
5186  ereport(ERROR,
5187  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5188  errmsg("width argument position must be ended by \"$\"")));
5189  /* The number was width argument position */
5190  *widthpos = n;
5191  /* Explicit 0 for argument index is immediately refused */
5192  if (n == 0)
5193  ereport(ERROR,
5194  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5195  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5196  ADVANCE_PARSE_POINTER(cp, end_ptr);
5197  }
5198  else
5199  *widthpos = 0; /* width's argument position is unspecified */
5200  }
5201  else
5202  {
5203  /* Check for direct width specification */
5204  if (text_format_parse_digits(&cp, end_ptr, &n))
5205  *width = n;
5206  }
5207 
5208  /* cp should now be pointing at type character */
5209  return cp;
5210 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4800
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition: varlena.c:5090
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4798
static void text_format_string_conversion ( StringInfo  buf,
char  conversion,
FmgrInfo typOutputInfo,
Datum  value,
bool  isNull,
int  flags,
int  width 
)
static

Definition at line 5216 of file varlena.c.

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

Referenced by text_format().

5220 {
5221  char *str;
5222 
5223  /* Handle NULL arguments before trying to stringify the value. */
5224  if (isNull)
5225  {
5226  if (conversion == 's')
5227  text_format_append_string(buf, "", flags, width);
5228  else if (conversion == 'L')
5229  text_format_append_string(buf, "NULL", flags, width);
5230  else if (conversion == 'I')
5231  ereport(ERROR,
5232  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5233  errmsg("null values cannot be formatted as an SQL identifier")));
5234  return;
5235  }
5236 
5237  /* Stringify. */
5238  str = OutputFunctionCall(typOutputInfo, value);
5239 
5240  /* Escape. */
5241  if (conversion == 'I')
5242  {
5243  /* quote_identifier may or may not allocate a new string. */
5244  text_format_append_string(buf, quote_identifier(str), flags, width);
5245  }
5246  else if (conversion == 'L')
5247  {
5248  char *qstr = quote_literal_cstr(str);
5249 
5250  text_format_append_string(buf, qstr, flags, width);
5251  /* quote_literal_cstr() always allocates a new string */
5252  pfree(qstr);
5253  }
5254  else
5255  text_format_append_string(buf, str, flags, width);
5256 
5257  /* Cleanup. */
5258  pfree(str);
5259 }
static struct @76 value
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:9968
int errcode(int sqlerrcode)
Definition: elog.c:575
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition: varlena.c:5265
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1926
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum text_ge ( PG_FUNCTION_ARGS  )

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

1710 {
1711  text *arg1 = PG_GETARG_TEXT_PP(0);
1712  text *arg2 = PG_GETARG_TEXT_PP(1);
1713  bool result;
1714 
1715  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1716 
1717  PG_FREE_IF_COPY(arg1, 0);
1718  PG_FREE_IF_COPY(arg2, 1);
1719 
1720  PG_RETURN_BOOL(result);
1721 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_gt ( PG_FUNCTION_ARGS  )

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

1695 {
1696  text *arg1 = PG_GETARG_TEXT_PP(0);
1697  text *arg2 = PG_GETARG_TEXT_PP(1);
1698  bool result;
1699 
1700  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1701 
1702  PG_FREE_IF_COPY(arg1, 0);
1703  PG_FREE_IF_COPY(arg2, 1);
1704 
1705  PG_RETURN_BOOL(result);
1706 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
static bool text_isequal ( text txt1,
text txt2 
)
static

Definition at line 4007 of file varlena.c.

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

Referenced by text_to_array_internal().

4008 {
4010  PointerGetDatum(txt1),
4011  PointerGetDatum(txt2)));
4012 }
#define PointerGetDatum(X)
Definition: postgres.h:564
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1600
#define DatumGetBool(X)
Definition: postgres.h:401
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2442 of file varlena.c.

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

2443 {
2444  text *arg1 = PG_GETARG_TEXT_PP(0);
2445  text *arg2 = PG_GETARG_TEXT_PP(1);
2446  text *result;
2447 
2448  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2449 
2450  PG_RETURN_TEXT_P(result);
2451 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Definition: c.h:435
Datum text_le ( PG_FUNCTION_ARGS  )

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

1680 {
1681  text *arg1 = PG_GETARG_TEXT_PP(0);
1682  text *arg2 = PG_GETARG_TEXT_PP(1);
1683  bool result;
1684 
1685  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1686 
1687  PG_FREE_IF_COPY(arg1, 0);
1688  PG_FREE_IF_COPY(arg2, 1);
1689 
1690  PG_RETURN_BOOL(result);
1691 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 4717 of file varlena.c.

References cstring_to_text_with_len(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4718 {
4719  text *str = PG_GETARG_TEXT_PP(0);
4720  const char *p = VARDATA_ANY(str);
4721  int len = VARSIZE_ANY_EXHDR(str);
4722  int n = PG_GETARG_INT32(1);
4723  int rlen;
4724 
4725  if (n < 0)
4726  n = pg_mbstrlen_with_len(p, len) + n;
4727  rlen = pg_mbcharcliplen(p, len, n);
4728 
4730 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
static int32 text_length ( Datum  str)
static

Definition at line 642 of file varlena.c.

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

Referenced by textlen(), and textoverlay_no_len().

643 {
644  /* fastpath when max encoding length is one */
647  else
648  {
649  text *t = DatumGetTextPP(str);
650 
652  VARSIZE_ANY_EXHDR(t)));
653  }
654 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARHDRSZ
Definition: c.h:441
#define DatumGetTextPP(X)
Definition: fmgr.h:249
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1664 of file varlena.c.

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

Referenced by gbt_textlt().

1665 {
1666  text *arg1 = PG_GETARG_TEXT_PP(0);
1667  text *arg2 = PG_GETARG_TEXT_PP(1);
1668  bool result;
1669 
1670  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1671 
1672  PG_FREE_IF_COPY(arg1, 0);
1673  PG_FREE_IF_COPY(arg2, 1);
1674 
1675  PG_RETURN_BOOL(result);
1676 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3031 of file varlena.c.

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

3032 {
3033  text *s = PG_GETARG_TEXT_PP(0);
3034  Name result;
3035  int len;
3036 
3037  len = VARSIZE_ANY_EXHDR(s);
3038 
3039  /* Truncate oversize input */
3040  if (len >= NAMEDATALEN)
3041  len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3042 
3043  /* We use palloc0 here to ensure result is zero-padded */
3044  result = (Name) palloc0(NAMEDATALEN);
3045  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3046 
3047  PG_RETURN_NAME(result);
3048 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
Definition: c.h:489
void * palloc0(Size size)
Definition: mcxt.c:920
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
#define NameStr(name)
Definition: c.h:495
Definition: c.h:435
NameData * Name
Definition: c.h:493
#define PG_RETURN_NAME(x)
Definition: fmgr.h:307
static text * text_overlay ( text t1,
text t2,
int  sp,
int  sl 
)
static

Definition at line 1036 of file varlena.c.

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

Referenced by textoverlay(), and textoverlay_no_len().

1037 {
1038  text *result;
1039  text *s1;
1040  text *s2;
1041  int sp_pl_sl;
1042 
1043  /*
1044  * Check for possible integer-overflow cases. For negative sp, throw a
1045  * "substring length" error because that's what should be expected
1046  * according to the spec's definition of OVERLAY().
1047  */
1048  if (sp <= 0)
1049  ereport(ERROR,
1050  (errcode(ERRCODE_SUBSTRING_ERROR),
1051  errmsg("negative substring length not allowed")));
1052  sp_pl_sl = sp + sl;
1053  if (sp_pl_sl <= sl)
1054  ereport(ERROR,
1055  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1056  errmsg("integer out of range")));
1057 
1058  s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1059  s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1060  result = text_catenate(s1, t2);
1061  result = text_catenate(result, s2);
1062 
1063  return result;
1064 }
#define PointerGetDatum(X)
Definition: postgres.h:564
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
char * s1
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:816
static text * text_catenate(text *t1, text *t2)
Definition: varlena.c:696
#define ereport(elevel, rest)
Definition: elog.h:122
char * s2
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:435
Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 2529 of file varlena.c.

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

2530 {
2531  text *arg1 = PG_GETARG_TEXT_PP(0);
2532  text *arg2 = PG_GETARG_TEXT_PP(1);
2533  int result;
2534 
2535  result = internal_text_pattern_compare(arg1, arg2);
2536 
2537  PG_FREE_IF_COPY(arg1, 0);
2538  PG_FREE_IF_COPY(arg2, 1);
2539 
2540  PG_RETURN_BOOL(result >= 0);
2541 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2475
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 2545 of file varlena.c.

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

2546 {
2547  text *arg1 = PG_GETARG_TEXT_PP(0);
2548  text *arg2 = PG_GETARG_TEXT_PP(1);
2549  int result;
2550 
2551  result = internal_text_pattern_compare(arg1, arg2);
2552 
2553  PG_FREE_IF_COPY(arg1, 0);
2554  PG_FREE_IF_COPY(arg2, 1);
2555 
2556  PG_RETURN_BOOL(result > 0);
2557 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2475
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 2513 of file varlena.c.

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

2514 {
2515  text *arg1 = PG_GETARG_TEXT_PP(0);
2516  text *arg2 = PG_GETARG_TEXT_PP(1);
2517  int result;
2518 
2519  result = internal_text_pattern_compare(arg1, arg2);
2520 
2521  PG_FREE_IF_COPY(arg1, 0);
2522  PG_FREE_IF_COPY(arg2, 1);
2523 
2524  PG_RETURN_BOOL(result <= 0);
2525 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2475
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 2497 of file varlena.c.

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

2498 {
2499  text *arg1 = PG_GETARG_TEXT_PP(0);
2500  text *arg2 = PG_GETARG_TEXT_PP(1);
2501  int result;
2502 
2503  result = internal_text_pattern_compare(arg1, arg2);
2504 
2505  PG_FREE_IF_COPY(arg1, 0);
2506  PG_FREE_IF_COPY(arg2, 1);
2507 
2508  PG_RETURN_BOOL(result < 0);
2509 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2475
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
Definition: c.h:435
static int text_position ( text t1,
text t2 
)
static

Definition at line 1097 of file varlena.c.

References text_position_cleanup(), text_position_next(), and text_position_setup().

Referenced by textpos().

1098 {
1100  int result;
1101 
1102  text_position_setup(t1, t2, &state);
1103  result = text_position_next(1, &state);
1104  text_position_cleanup(&state);
1105  return result;
1106 }
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1121
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1233
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1367
static void text_position_cleanup ( TextPositionState state)
static

Definition at line 1367 of file varlena.c.

References pfree(), TextPositionState::use_wchar, TextPositionState::wstr1, and TextPositionState::wstr2.

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

1368 {
1369  if (state->use_wchar)
1370  {
1371  pfree(state->wstr1);
1372  pfree(state->wstr2);
1373  }
1374 }
pg_wchar * wstr2
Definition: varlena.c:52
void pfree(void *pointer)
Definition: mcxt.c:992
pg_wchar * wstr1
Definition: varlena.c:51
static int text_position_next ( int  start_pos,
TextPositionState state 
)
static

Definition at line 1233 of file varlena.c.

References Assert, TextPositionState::len1, TextPositionState::len2, TextPositionState::skiptable, TextPositionState::skiptablemask, TextPositionState::str1, TextPositionState::str2, TextPositionState::use_wchar, TextPositionState::wstr1, and TextPositionState::wstr2.

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

1234 {
1235  int haystack_len = state->len1;
1236  int needle_len = state->len2;
1237  int skiptablemask = state->skiptablemask;
1238 
1239  Assert(start_pos > 0); /* else caller error */
1240 
1241  if (needle_len <= 0)
1242  return start_pos; /* result for empty pattern */
1243 
1244  start_pos--; /* adjust for zero based arrays */
1245 
1246  /* Done if the needle can't possibly fit */
1247  if (haystack_len < start_pos + needle_len)
1248  return 0;
1249 
1250  if (!state->use_wchar)
1251  {
1252  /* simple case - single byte encoding */
1253  const char *haystack = state->str1;
1254  const char *needle = state->str2;
1255  const char *haystack_end = &haystack[haystack_len];
1256  const char *hptr;
1257 
1258  if (needle_len == 1)
1259  {
1260  /* No point in using B-M-H for a one-character needle */
1261  char nchar = *needle;
1262 
1263  hptr = &haystack[start_pos];
1264  while (hptr < haystack_end)
1265  {
1266  if (*hptr == nchar)
1267  return hptr - haystack + 1;
1268  hptr++;
1269  }
1270  }
1271  else
1272  {
1273  const char *needle_last = &needle[needle_len - 1];
1274 
1275  /* Start at startpos plus the length of the needle */
1276  hptr = &haystack[start_pos + needle_len - 1];
1277  while (hptr < haystack_end)
1278  {
1279  /* Match the needle scanning *backward* */
1280  const char *nptr;
1281  const char *p;
1282 
1283  nptr = needle_last;
1284  p = hptr;
1285  while (*nptr == *p)
1286  {
1287  /* Matched it all? If so, return 1-based position */
1288  if (nptr == needle)
1289  return p - haystack + 1;
1290  nptr--, p--;
1291  }
1292 
1293  /*
1294  * No match, so use the haystack char at hptr to decide how
1295  * far to advance. If the needle had any occurrence of that
1296  * character (or more precisely, one sharing the same
1297  * skiptable entry) before its last character, then we advance
1298  * far enough to align the last such needle character with
1299  * that haystack position. Otherwise we can advance by the
1300  * whole needle length.
1301  */
1302  hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1303  }
1304  }
1305  }
1306  else
1307  {
1308  /* The multibyte char version. This works exactly the same way. */
1309  const pg_wchar *haystack = state->wstr1;
1310  const pg_wchar *needle = state->wstr2;
1311  const pg_wchar *haystack_end = &haystack[haystack_len];
1312  const pg_wchar *hptr;
1313 
1314  if (needle_len == 1)
1315  {
1316  /* No point in using B-M-H for a one-character needle */
1317  pg_wchar nchar = *needle;
1318 
1319  hptr = &haystack[start_pos];
1320  while (hptr < haystack_end)
1321  {
1322  if (*hptr == nchar)
1323  return hptr - haystack + 1;
1324  hptr++;
1325  }
1326  }
1327  else
1328  {
1329  const pg_wchar *needle_last = &needle[needle_len - 1];
1330 
1331  /* Start at startpos plus the length of the needle */
1332  hptr = &haystack[start_pos + needle_len - 1];
1333  while (hptr < haystack_end)
1334  {
1335  /* Match the needle scanning *backward* */
1336  const pg_wchar *nptr;
1337  const pg_wchar *p;
1338 
1339  nptr = needle_last;
1340  p = hptr;
1341  while (*nptr == *p)
1342  {
1343  /* Matched it all? If so, return 1-based position */
1344  if (nptr == needle)
1345  return p - haystack + 1;
1346  nptr--, p--;
1347  }
1348 
1349  /*
1350  * No match, so use the haystack char at hptr to decide how
1351  * far to advance. If the needle had any occurrence of that
1352  * character (or more precisely, one sharing the same
1353  * skiptable entry) before its last character, then we advance
1354  * far enough to align the last such needle character with
1355  * that haystack position. Otherwise we can advance by the
1356  * whole needle length.
1357  */
1358  hptr += state->skiptable[*hptr & skiptablemask];
1359  }
1360  }
1361  }
1362 
1363  return 0; /* not found */
1364 }
pg_wchar * wstr2
Definition: varlena.c:52
unsigned int pg_wchar
Definition: mbprint.c:31
int skiptable[256]
Definition: varlena.c:57
#define Assert(condition)
Definition: c.h:671
pg_wchar * wstr1
Definition: varlena.c:51
static void text_position_setup ( text t1,
text t2,
TextPositionState state 
)
static

Definition at line 1121 of file varlena.c.

References i, TextPositionState::len1, TextPositionState::len2, palloc(), pg_database_encoding_max_length(), pg_mb2wchar_with_len(), TextPositionState::skiptable, TextPositionState::skiptablemask, TextPositionState::str1, TextPositionState::str2, TextPositionState::use_wchar, VARDATA_ANY, VARSIZE_ANY_EXHDR, TextPositionState::wstr1, and TextPositionState::wstr2.

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

1122 {
1123  int len1 = VARSIZE_ANY_EXHDR(t1);
1124  int len2 = VARSIZE_ANY_EXHDR(t2);
1125 
1127  {
1128  /* simple case - single byte encoding */
1129  state->use_wchar = false;
1130  state->str1 = VARDATA_ANY(t1);
1131  state->str2 = VARDATA_ANY(t2);
1132  state->len1 = len1;
1133  state->len2 = len2;
1134  }
1135  else
1136  {
1137  /* not as simple - multibyte encoding */
1138  pg_wchar *p1,
1139  *p2;
1140 
1141  p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));
1142  len1 = pg_mb2wchar_with_len(VARDATA_ANY(t1), p1, len1);
1143  p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));
1144  len2 = pg_mb2wchar_with_len(VARDATA_ANY(t2), p2, len2);
1145 
1146  state->use_wchar = true;
1147  state->wstr1 = p1;
1148  state->wstr2 = p2;
1149  state->len1 = len1;
1150  state->len2 = len2;
1151  }
1152 
1153  /*
1154  * Prepare the skip table for Boyer-Moore-Horspool searching. In these
1155  * notes we use the terminology that the "haystack" is the string to be
1156  * searched (t1) and the "needle" is the pattern being sought (t2).
1157  *
1158  * If the needle is empty or bigger than the haystack then there is no
1159  * point in wasting cycles initializing the table. We also choose not to
1160  * use B-M-H for needles of length 1, since the skip table can't possibly
1161  * save anything in that case.
1162  */
1163  if (len1 >= len2 && len2 > 1)
1164  {
1165  int searchlength = len1 - len2;
1166  int skiptablemask;
1167  int last;
1168  int i;
1169 
1170  /*
1171  * First we must determine how much of the skip table to use. The
1172  * declaration of TextPositionState allows up to 256 elements, but for
1173  * short search problems we don't really want to have to initialize so
1174  * many elements --- it would take too long in comparison to the
1175  * actual search time. So we choose a useful skip table size based on
1176  * the haystack length minus the needle length. The closer the needle
1177  * length is to the haystack length the less useful skipping becomes.
1178  *
1179  * Note: since we use bit-masking to select table elements, the skip
1180  * table size MUST be a power of 2, and so the mask must be 2^N-1.
1181  */
1182  if (searchlength < 16)
1183  skiptablemask = 3;
1184  else if (searchlength < 64)
1185  skiptablemask = 7;
1186  else if (searchlength < 128)
1187  skiptablemask = 15;
1188  else if (searchlength < 512)
1189  skiptablemask = 31;
1190  else if (searchlength < 2048)
1191  skiptablemask = 63;
1192  else if (searchlength < 4096)
1193  skiptablemask = 127;
1194  else
1195  skiptablemask = 255;
1196  state->skiptablemask = skiptablemask;
1197 
1198  /*
1199  * Initialize the skip table. We set all elements to the needle
1200  * length, since this is the correct skip distance for any character
1201  * not found in the needle.
1202  */
1203  for (i = 0; i <= skiptablemask; i++)
1204  state->skiptable[i] = len2;
1205 
1206  /*
1207  * Now examine the needle. For each character except the last one,
1208  * set the corresponding table element to the appropriate skip
1209  * distance. Note that when two characters share the same skip table
1210  * entry, the one later in the needle must determine the skip
1211  * distance.
1212  */
1213  last = len2 - 1;
1214 
1215  if (!state->use_wchar)
1216  {
1217  const char *str2 = state->str2;
1218 
1219  for (i = 0; i < last; i++)
1220  state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1221  }
1222  else
1223  {
1224  const pg_wchar *wstr2 = state->wstr2;
1225 
1226  for (i = 0; i < last; i++)
1227  state->skiptable[wstr2[i] & skiptablemask] = last - i;
1228  }
1229  }
1230 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
pg_wchar * wstr2
Definition: varlena.c:52
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
unsigned int pg_wchar
Definition: mbprint.c:31
int skiptable[256]
Definition: varlena.c:57
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
pg_wchar * wstr1
Definition: varlena.c:51
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
int i
Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 4758 of file varlena.c.

References palloc(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, pg_mblen(), PG_RETURN_TEXT_P, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

4759 {
4760  text *str = PG_GETARG_TEXT_PP(0);
4761  const char *p = VARDATA_ANY(str);
4762  int len = VARSIZE_ANY_EXHDR(str);
4763  const char *endp = p + len;
4764  text *result;
4765  char *dst;
4766 
4767  result = palloc(len + VARHDRSZ);
4768  dst = (char *) VARDATA(result) + len;
4769  SET_VARSIZE(result, len + VARHDRSZ);
4770 
4772  {
4773  /* multibyte version */
4774  while (p < endp)
4775  {
4776  int sz;
4777 
4778  sz = pg_mblen(p);
4779  dst -= sz;
4780  memcpy(dst, p, sz);
4781  p += sz;
4782  }
4783  }
4784  else
4785  {
4786  /* single byte version */
4787  while (p < endp)
4788  *(--dst) = *p++;
4789  }
4790 
4791  PG_RETURN_TEXT_P(result);
4792 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARHDRSZ
Definition: c.h:441
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 4737 of file varlena.c.

References cstring_to_text_with_len(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4738 {
4739  text *str = PG_GETARG_TEXT_PP(0);
4740  const char *p = VARDATA_ANY(str);
4741  int len = VARSIZE_ANY_EXHDR(str);
4742  int n = PG_GETARG_INT32(1);
4743  int off;
4744 
4745  if (n < 0)
4746  n = -n;
4747  else
4748  n = pg_mbstrlen_with_len(p, len) - n;
4749  off = pg_mbcharcliplen(p, len, n);
4750 
4751  PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
4752 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:163
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
Definition: c.h:435
Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2454 of file varlena.c.

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

2455 {
2456  text *arg1 = PG_GETARG_TEXT_PP(0);
2457  text *arg2 = PG_GETARG_TEXT_PP(1);
2458  text *result;
2459 
2460  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2461 
2462  PG_RETURN_TEXT_P(result);
2463 }
#define PG_GET_COLLATION()
Definition: fmgr.h:155
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1575
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Definition: c.h:435
Datum text_substr ( PG_FUNCTION_ARGS  )

Definition at line 783 of file varlena.c.

References PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and text_substring().

Referenced by build_regexp_match_result(), build_regexp_split_result(), and textregexsubstr().

784 {
786  PG_GETARG_INT32(1),
787  PG_GETARG_INT32(2),
788  false));
789 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:816
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 797 of file varlena.c.

References PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and text_substring().

Referenced by build_regexp_split_result().

798 {
800  PG_GETARG_INT32(1),
801  -1, true));
802 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:816
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
static text * text_substring ( Datum  str,
int32  start,
int32  length,
bool  length_not_specified 
)
static

Definition at line 816 of file varlena.c.

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

Referenced by split_text(), text_overlay(), text_substr(), and text_substr_no_len().

817 {
819  int32 S = start; /* start position */
820  int32 S1; /* adjusted start position */
821  int32 L1; /* adjusted substring length */
822 
823  /* life is easy if the encoding max length is 1 */
824  if (eml == 1)
825  {
826  S1 = Max(S, 1);
827 
828  if (length_not_specified) /* special case - get length to end of
829  * string */
830  L1 = -1;
831  else
832  {
833  /* end position */
834  int E = S + length;
835 
836  /*
837  * A negative value for L is the only way for the end position to
838  * be before the start. SQL99 says to throw an error.
839  */
840  if (E < S)
841  ereport(ERROR,
842  (errcode(ERRCODE_SUBSTRING_ERROR),
843  errmsg("negative substring length not allowed")));
844 
845  /*
846  * A zero or negative value for the end position can happen if the
847  * start was negative or one. SQL99 says to return a zero-length
848  * string.
849  */
850  if (E < 1)
851  return cstring_to_text("");
852 
853  L1 = E - S1;
854  }
855 
856  /*
857  * If the start position is past the end of the string, SQL99 says to
858  * return a zero-length string -- PG_GETARG_TEXT_P_SLICE() will do
859  * that for us. Convert to zero-based starting position
860  */
861  return DatumGetTextPSlice(str, S1 - 1, L1);
862  }
863  else if (eml > 1)
864  {
865  /*
866  * When encoding max length is > 1, we can't get LC without
867  * detoasting, so we'll grab a conservatively large slice now and go
868  * back later to do the right thing
869  */
870  int32 slice_start;
871  int32 slice_size;
872  int32 slice_strlen;
873  text *slice;
874  int32 E1;
875  int32 i;
876  char *p;
877  char *s;
878  text *ret;
879 
880  /*
881  * if S is past the end of the string, the tuple toaster will return a
882  * zero-length string to us
883  */
884  S1 = Max(S, 1);
885 
886  /*
887  * We need to start at position zero because there is no way to know
888  * in advance which byte offset corresponds to the supplied start
889  * position.
890  */
891  slice_start = 0;
892 
893  if (length_not_specified) /* special case - get length to end of
894  * string */
895  slice_size = L1 = -1;
896  else
897  {
898  int E = S + length;
899 
900  /*
901  * A negative value for L is the only way for the end position to
902  * be before the start. SQL99 says to throw an error.
903  */
904  if (E < S)
905  ereport(ERROR,
906  (errcode(ERRCODE_SUBSTRING_ERROR),
907  errmsg("negative substring length not allowed")));
908 
909  /*
910  * A zero or negative value for the end position can happen if the
911  * start was negative or one. SQL99 says to return a zero-length
912  * string.
913  */
914  if (E < 1)
915  return cstring_to_text("");
916 
917  /*
918  * if E is past the end of the string, the tuple toaster will
919  * truncate the length for us
920  */
921  L1 = E - S1;
922 
923  /*
924  * Total slice size in bytes can't be any longer than the start
925  * position plus substring length times the encoding max length.
926  */
927  slice_size = (S1 + L1) * eml;
928  }
929 
930  /*
931  * If we're working with an untoasted source, no need to do an extra
932  * copying step.
933  */
936  slice = DatumGetTextPSlice(str, slice_start, slice_size);
937  else
938  slice = (text *) DatumGetPointer(str);
939 
940  /* see if we got back an empty string */
941  if (VARSIZE_ANY_EXHDR(slice) == 0)
942  {
943  if (slice != (text *) DatumGetPointer(str))
944  pfree(slice);
945  return cstring_to_text("");
946  }
947 
948  /* Now we can get the actual length of the slice in MB characters */
949  slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
950  VARSIZE_ANY_EXHDR(slice));
951 
952  /*
953  * Check that the start position wasn't > slice_strlen. If so, SQL99
954  * says to return a zero-length string.
955  */
956  if (S1 > slice_strlen)
957  {
958  if (slice != (text *) DatumGetPointer(str))
959  pfree(slice);
960  return cstring_to_text("");
961  }
962 
963  /*
964  * Adjust L1 and E1 now that we know the slice string length. Again
965  * remember that S1 is one based, and slice_start is zero based.
966  */
967  if (L1 > -1)
968  E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
969  else
970  E1 = slice_start + 1 + slice_strlen;
971 
972  /*
973  * Find the start position in the slice; remember S1 is not zero based
974  */
975  p = VARDATA_ANY(slice);
976  for (i = 0; i < S1 - 1; i++)
977  p += pg_mblen(p);
978 
979  /* hang onto a pointer to our start position */
980  s = p;
981 
982  /*
983  * Count the actual bytes used by the substring of the requested
984  * length.
985  */
986  for (i = S1; i < E1; i++)
987  p += pg_mblen(p);
988 
989  ret = (text *) palloc(VARHDRSZ + (p - s));
990  SET_VARSIZE(ret, VARHDRSZ + (p - s));
991  memcpy(VARDATA(ret), s, (p - s));
992 
993  if (slice != (text *) DatumGetPointer(str))
994  pfree(slice);
995 
996  return ret;
997  }
998  else
999  elog(ERROR, "invalid backend encoding: encoding max length < 1");
1000 
1001  /* not reached: suppress compiler warning */
1002  return NULL;
1003 }
int length(const List *list)
Definition: list.c:1271
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:315
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
#define DatumGetTextPSlice(X, m, n)
Definition: fmgr.h:263
#define VARHDRSZ
Definition: c.h:441
#define Min(x, y)
Definition: c.h:802
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:316
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define S(n, x)
Definition: sha1.c:55
#define ereport(elevel, rest)
Definition: elog.h:122
#define Max(x, y)
Definition: c.h:796
text * cstring_to_text(const char *s)
Definition: varlena.c:151
#define NULL
Definition: c.h:226
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
#define DatumGetPointer(X)
Definition: postgres.h:557
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:435
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define elog
Definition: elog.h:219
Datum text_to_array ( PG_FUNCTION_ARGS  )

Definition at line 4020 of file varlena.c.

References text_to_array_internal().

4021 {
4022  return text_to_array_internal(fcinfo);
4023 }
static Datum text_to_array_internal(PG_FUNCTION_ARGS)
Definition: varlena.c:4045
static Datum text_to_array_internal ( PG_FUNCTION_ARGS  )
static

Definition at line 4045 of file varlena.c.

References accumArrayResult(), charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, construct_empty_array(), create_singleton_array(), cstring_to_text_with_len(), CurrentMemoryContext, TextPositionState::len1, TextPositionState::len2, makeArrayResult(), NULL, pfree(), PG_ARGISNULL, PG_GETARG_TEXT_PP, pg_mblen(), PG_NARGS, PG_RETURN_ARRAYTYPE_P, PG_RETURN_NULL, PointerGetDatum, text_isequal(), text_position_cleanup(), text_position_next(), text_position_setup(), TEXTOID, VARDATA_ANY, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by text_to_array(), and text_to_array_null().

4046 {
4047  text *inputstring;
4048  text *fldsep;
4049  text *null_string;
4050  int inputstring_len;
4051  int fldsep_len;
4052  char *start_ptr;
4053  text *result_text;
4054  bool is_null;
4055  ArrayBuildState *astate = NULL;
4056 
4057  /* when input string is NULL, then result is NULL too */
4058  if (PG_ARGISNULL(0))
4059  PG_RETURN_NULL();
4060 
4061  inputstring = PG_GETARG_TEXT_PP(0);
4062 
4063  /* fldsep can be NULL */
4064  if (!PG_ARGISNULL(1))
4065  fldsep = PG_GETARG_TEXT_PP(1);
4066  else
4067  fldsep = NULL;
4068 
4069  /* null_string can be NULL or omitted */
4070  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4071  null_string = PG_GETARG_TEXT_PP(2);
4072  else
4073  null_string = NULL;
4074 
4075  if (fldsep != NULL)
4076  {
4077  /*
4078  * Normal case with non-null fldsep. Use the text_position machinery
4079  * to search for occurrences of fldsep.
4080  */
4082  int fldnum;
4083  int start_posn;
4084  int end_posn;
4085  int chunk_len;
4086 
4087  text_position_setup(inputstring, fldsep, &state);
4088 
4089  /*
4090  * Note: we check the converted string length, not the original,
4091  * because they could be different if the input contained invalid
4092  * encoding.
4093  */
4094  inputstring_len = state.len1;
4095  fldsep_len = state.len2;
4096 
4097  /* return empty array for empty input string */
4098  if (inputstring_len < 1)
4099  {
4100  text_position_cleanup(&state);
4102  }
4103 
4104  /*
4105  * empty field separator: return the input string as a one-element
4106  * array
4107  */