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_)   DatumGetByteaPP(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 FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 

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

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

Definition at line 86 of file varlena.c.

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

Definition at line 91 of file varlena.c.

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

Definition at line 92 of file varlena.c.

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

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

Definition at line 241 of file varlena.c.

Referenced by byteaout().

#define HEXBASE   16

Definition at line 4519 of file varlena.c.

Referenced by to_hex32(), and to_hex64().

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 5524 of file varlena.c.

#define MD5_HASH_LEN   32

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

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

Definition at line 88 of file varlena.c.

#define PG_RETURN_UNKNOWN_P (   x)    PG_RETURN_POINTER(x)

Definition at line 89 of file varlena.c.

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

Definition at line 2813 of file varlena.c.

Referenced by bytea_substring().

#define REGEXP_REPLACE_BACKREF_CNT   10

Definition at line 3901 of file varlena.c.

Referenced by replace_text_regexp().

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

Definition at line 4982 of file varlena.c.

Referenced by text_format_append_string(), and text_format_parse_format().

#define TEXTBUFLEN   1024

Definition at line 83 of file varlena.c.

Referenced by varstr_cmp(), and varstr_sortsupport().

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

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

3810 {
3811  const char *p = VARDATA_ANY(replace_text);
3812  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3813  int eml = pg_database_encoding_max_length();
3814 
3815  for (;;)
3816  {
3817  const char *chunk_start = p;
3818  int so;
3819  int eo;
3820 
3821  /* Find next escape char. */
3822  if (eml == 1)
3823  {
3824  for (; p < p_end && *p != '\\'; p++)
3825  /* nothing */ ;
3826  }
3827  else
3828  {
3829  for (; p < p_end && *p != '\\'; p += pg_mblen(p))
3830  /* nothing */ ;
3831  }
3832 
3833  /* Copy the text we just scanned over, if any. */
3834  if (p > chunk_start)
3835  appendBinaryStringInfo(str, chunk_start, p - chunk_start);
3836 
3837  /* Done if at end of string, else advance over escape char. */
3838  if (p >= p_end)
3839  break;
3840  p++;
3841 
3842  if (p >= p_end)
3843  {
3844  /* Escape at very end of input. Treat same as unexpected char */
3845  appendStringInfoChar(str, '\\');
3846  break;
3847  }
3848 
3849  if (*p >= '1' && *p <= '9')
3850  {
3851  /* Use the back reference of regexp. */
3852  int idx = *p - '0';
3853 
3854  so = pmatch[idx].rm_so;
3855  eo = pmatch[idx].rm_eo;
3856  p++;
3857  }
3858  else if (*p == '&')
3859  {
3860  /* Use the entire matched string. */
3861  so = pmatch[0].rm_so;
3862  eo = pmatch[0].rm_eo;
3863  p++;
3864  }
3865  else if (*p == '\\')
3866  {
3867  /* \\ means transfer one \ to output. */
3868  appendStringInfoChar(str, '\\');
3869  p++;
3870  continue;
3871  }
3872  else
3873  {
3874  /*
3875  * If escape char is not followed by any expected char, just treat
3876  * it as ordinary data to copy. (XXX would it be better to throw
3877  * an error?)
3878  */
3879  appendStringInfoChar(str, '\\');
3880  continue;
3881  }
3882 
3883  if (so != -1 && eo != -1)
3884  {
3885  /*
3886  * Copy the text that is back reference of regexp. Note so and eo
3887  * are counted in characters not bytes.
3888  */
3889  char *chunk_start;
3890  int chunk_len;
3891 
3892  Assert(so >= data_pos);
3893  chunk_start = start_ptr;
3894  chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
3895  chunk_len = charlen_to_bytelen(chunk_start, eo - so);
3896  appendBinaryStringInfo(str, chunk_start, chunk_len);
3897  }
3898  }
3899 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
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:735
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
#define Assert(condition)
Definition: c.h:681
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
static void appendStringInfoText ( StringInfo  str,
const text t 
)
static

Definition at line 3677 of file varlena.c.

References appendBinaryStringInfo(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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

3678 {
3680 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 4360 of file varlena.c.

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

4361 {
4363  char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4364 
4365  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4366 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4402
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
char * text_to_cstring(const text *t)
Definition: varlena.c:182
static text * array_to_text_internal ( FunctionCallInfo  fcinfo,
ArrayType v,
const char *  fldsep,
const char *  null_string 
)
static

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

4404 {
4405  text *result;
4406  int nitems,
4407  *dims,
4408  ndims;
4409  Oid element_type;
4410  int typlen;
4411  bool typbyval;
4412  char typalign;
4414  bool printed = false;
4415  char *p;
4416  bits8 *bitmap;
4417  int bitmask;
4418  int i;
4419  ArrayMetaState *my_extra;
4420 
4421  ndims = ARR_NDIM(v);
4422  dims = ARR_DIMS(v);
4423  nitems = ArrayGetNItems(ndims, dims);
4424 
4425  /* if there are no elements, return an empty string */
4426  if (nitems == 0)
4427  return cstring_to_text_with_len("", 0);
4428 
4429  element_type = ARR_ELEMTYPE(v);
4430  initStringInfo(&buf);
4431 
4432  /*
4433  * We arrange to look up info about element type, including its output
4434  * conversion proc, only once per series of calls, assuming the element
4435  * type doesn't change underneath us.
4436  */
4437  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4438  if (my_extra == NULL)
4439  {
4440  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4441  sizeof(ArrayMetaState));
4442  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4443  my_extra->element_type = ~element_type;
4444  }
4445 
4446  if (my_extra->element_type != element_type)
4447  {
4448  /*
4449  * Get info about element type, including its output conversion proc
4450  */
4451  get_type_io_data(element_type, IOFunc_output,
4452  &my_extra->typlen, &my_extra->typbyval,
4453  &my_extra->typalign, &my_extra->typdelim,
4454  &my_extra->typioparam, &my_extra->typiofunc);
4455  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4456  fcinfo->flinfo->fn_mcxt);
4457  my_extra->element_type = element_type;
4458  }
4459  typlen = my_extra->typlen;
4460  typbyval = my_extra->typbyval;
4461  typalign = my_extra->typalign;
4462 
4463  p = ARR_DATA_PTR(v);
4464  bitmap = ARR_NULLBITMAP(v);
4465  bitmask = 1;
4466 
4467  for (i = 0; i < nitems; i++)
4468  {
4469  Datum itemvalue;
4470  char *value;
4471 
4472  /* Get source element, checking for NULL */
4473  if (bitmap && (*bitmap & bitmask) == 0)
4474  {
4475  /* if null_string is NULL, we just ignore null elements */
4476  if (null_string != NULL)
4477  {
4478  if (printed)
4479  appendStringInfo(&buf, "%s%s", fldsep, null_string);
4480  else
4481  appendStringInfoString(&buf, null_string);
4482  printed = true;
4483  }
4484  }
4485  else
4486  {
4487  itemvalue = fetch_att(p, typbyval, typlen);
4488 
4489  value = OutputFunctionCall(&my_extra->proc, itemvalue);
4490 
4491  if (printed)
4492  appendStringInfo(&buf, "%s%s", fldsep, value);
4493  else
4494  appendStringInfoString(&buf, value);
4495  printed = true;
4496 
4497  p = att_addlength_pointer(p, typlen, p);
4498  p = (char *) att_align_nominal(p, typalign);
4499  }
4500 
4501  /* advance bitmap pointer if any */
4502  if (bitmap)
4503  {
4504  bitmask <<= 1;
4505  if (bitmask == 0x100)
4506  {
4507  bitmap++;
4508  bitmask = 1;
4509  }
4510  }
4511  }
4512 
4513  result = cstring_to_text_with_len(buf.data, buf.len);
4514  pfree(buf.data);
4515 
4516  return result;
4517 }
MemoryContext fn_mcxt
Definition: fmgr.h:65
#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:1662
bool typbyval
Definition: array.h:225
FmgrInfo * flinfo
Definition: fmgr.h:79
void pfree(void *pointer)
Definition: mcxt.c:949
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ARR_DIMS(a)
Definition: array.h:279
static struct @121 value
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define ARR_DATA_PTR(a)
Definition: array.h:307
int16 typlen
Definition: array.h:224
static char * buf
Definition: pg_test_fsync.c:67
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
char typdelim
Definition: array.h:227
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uint8 bits8
Definition: c.h:265
uintptr_t Datum
Definition: postgres.h:372
Oid typioparam
Definition: array.h:228
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:275
Oid typiofunc
Definition: array.h:229
char typalign
Definition: array.h:226
#define fetch_att(T, attbyval, attlen)
Definition: tupmacs.h:71
FmgrInfo proc
Definition: array.h:230
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
int i
Oid element_type
Definition: array.h:223
Definition: c.h:433
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define ARR_NULLBITMAP(a)
Definition: array.h:285
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:2075
Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 4376 of file varlena.c.

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

4377 {
4378  ArrayType *v;
4379  char *fldsep;
4380  char *null_string;
4381 
4382  /* returns NULL when first or second parameter is NULL */
4383  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4384  PG_RETURN_NULL();
4385 
4386  v = PG_GETARG_ARRAYTYPE_P(0);
4387  fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4388 
4389  /* NULL null string is passed through as a null pointer */
4390  if (!PG_ARGISNULL(2))
4391  null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4392  else
4393  null_string = NULL;
4394 
4395  PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4396 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4402
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static int bpcharfastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 2001 of file varlena.c.

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

Referenced by varstr_sortsupport().

2002 {
2003  BpChar *arg1 = DatumGetBpCharPP(x);
2004  BpChar *arg2 = DatumGetBpCharPP(y);
2005  char *a1p,
2006  *a2p;
2007  int len1,
2008  len2,
2009  result;
2010 
2011  a1p = VARDATA_ANY(arg1);
2012  a2p = VARDATA_ANY(arg2);
2013 
2014  len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2015  len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2016 
2017  result = memcmp(a1p, a2p, Min(len1, len2));
2018  if ((result == 0) && (len1 != len2))
2019  result = (len1 < len2) ? -1 : 1;
2020 
2021  /* We can't afford to leak memory here. */
2022  if (PointerGetDatum(arg1) != x)
2023  pfree(arg1);
2024  if (PointerGetDatum(arg2) != y)
2025  pfree(arg2);
2026 
2027  return result;
2028 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PointerGetDatum(X)
Definition: postgres.h:562
#define Min(x, y)
Definition: c.h:812
void pfree(void *pointer)
Definition: mcxt.c:949
int bpchartruelen(char *s, int len)
Definition: varchar.c:660
#define DatumGetBpCharPP(X)
Definition: fmgr.h:257
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2710 of file varlena.c.

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

2711 {
2712  text *arg1 = PG_GETARG_TEXT_PP(0);
2713  text *arg2 = PG_GETARG_TEXT_PP(1);
2714  int result;
2715 
2716  result = internal_text_pattern_compare(arg1, arg2);
2717 
2718  PG_FREE_IF_COPY(arg1, 0);
2719  PG_FREE_IF_COPY(arg2, 1);
2720 
2721  PG_RETURN_INT32(result);
2722 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2624
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2726 of file varlena.c.

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

2727 {
2729  MemoryContext oldcontext;
2730 
2731  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2732 
2733  /* Use generic string SortSupport, forcing "C" collation */
2734  varstr_sortsupport(ssup, C_COLLATION_OID, false);
2735 
2736  MemoryContextSwitchTo(oldcontext);
2737 
2738  PG_RETURN_VOID();
2739 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1808
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define C_COLLATION_OID
Definition: pg_collation.h:78
Datum bttextcmp ( PG_FUNCTION_ARGS  )

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

1767 {
1768  text *arg1 = PG_GETARG_TEXT_PP(0);
1769  text *arg2 = PG_GETARG_TEXT_PP(1);
1770  int32 result;
1771 
1772  result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1773 
1774  PG_FREE_IF_COPY(arg1, 0);
1775  PG_FREE_IF_COPY(arg2, 1);
1776 
1777  PG_RETURN_INT32(result);
1778 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GET_COLLATION()
Definition: fmgr.h:163
signed int int32
Definition: c.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1781 of file varlena.c.

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

1782 {
1784  Oid collid = ssup->ssup_collation;
1785  MemoryContext oldcontext;
1786 
1787  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1788 
1789  /* Use generic string SortSupport */
1790  varstr_sortsupport(ssup, collid, false);
1791 
1792  MemoryContextSwitchTo(oldcontext);
1793 
1794  PG_RETURN_VOID();
1795 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
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:1808
#define PG_RETURN_VOID()
Definition: fmgr.h:309
static FmgrInfo* build_concat_foutcache ( FunctionCallInfo  fcinfo,
int  argidx 
)
static

Definition at line 4747 of file varlena.c.

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

Referenced by concat_internal().

4748 {
4749  FmgrInfo *foutcache;
4750  int i;
4751 
4752  /* We keep the info in fn_mcxt so it survives across calls */
4753  foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4754  PG_NARGS() * sizeof(FmgrInfo));
4755 
4756  for (i = argidx; i < PG_NARGS(); i++)
4757  {
4758  Oid valtype;
4759  Oid typOutput;
4760  bool typIsVarlena;
4761 
4762  valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
4763  if (!OidIsValid(valtype))
4764  elog(ERROR, "could not determine data type of concat() input");
4765 
4766  getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
4767  fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
4768  }
4769 
4770  fcinfo->flinfo->fn_extra = foutcache;
4771 
4772  return foutcache;
4773 }
Definition: fmgr.h:56
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
MemoryContext fn_mcxt
Definition: fmgr.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
FmgrInfo * flinfo
Definition: fmgr.h:79
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
struct FmgrInfo FmgrInfo
#define PG_NARGS()
Definition: fmgr.h:168
void * fn_extra
Definition: fmgr.h:64
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
int i
#define elog
Definition: elog.h:219
static bytea * bytea_catenate ( bytea t1,
bytea t2 
)
static

Definition at line 2780 of file varlena.c.

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

Referenced by bytea_overlay(), and byteacat().

2781 {
2782  bytea *result;
2783  int len1,
2784  len2,
2785  len;
2786  char *ptr;
2787 
2788  len1 = VARSIZE_ANY_EXHDR(t1);
2789  len2 = VARSIZE_ANY_EXHDR(t2);
2790 
2791  /* paranoia ... probably should throw error instead? */
2792  if (len1 < 0)
2793  len1 = 0;
2794  if (len2 < 0)
2795  len2 = 0;
2796 
2797  len = len1 + len2 + VARHDRSZ;
2798  result = (bytea *) palloc(len);
2799 
2800  /* Set size of result string... */
2801  SET_VARSIZE(result, len);
2802 
2803  /* Fill data field of result string... */
2804  ptr = VARDATA(result);
2805  if (len1 > 0)
2806  memcpy(ptr, VARDATA_ANY(t1), len1);
2807  if (len2 > 0)
2808  memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2809 
2810  return result;
2811 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static bytea * bytea_overlay ( bytea t1,
bytea t2,
int  sp,
int  sl 
)
static

Definition at line 2937 of file varlena.c.

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

Referenced by byteaoverlay(), and byteaoverlay_no_len().

2938 {
2939  bytea *result;
2940  bytea *s1;
2941  bytea *s2;
2942  int sp_pl_sl;
2943 
2944  /*
2945  * Check for possible integer-overflow cases. For negative sp, throw a
2946  * "substring length" error because that's what should be expected
2947  * according to the spec's definition of OVERLAY().
2948  */
2949  if (sp <= 0)
2950  ereport(ERROR,
2951  (errcode(ERRCODE_SUBSTRING_ERROR),
2952  errmsg("negative substring length not allowed")));
2953  sp_pl_sl = sp + sl;
2954  if (sp_pl_sl <= sl)
2955  ereport(ERROR,
2956  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2957  errmsg("integer out of range")));
2958 
2959  s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
2960  s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
2961  result = bytea_catenate(s1, t2);
2962  result = bytea_catenate(result, s2);
2963 
2964  return result;
2965 }
#define PointerGetDatum(X)
Definition: postgres.h:562
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2780
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:2855
#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:433
Datum bytea_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 3655 of file varlena.c.

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

3656 {
3658  MemoryContext oldcontext;
3659 
3660  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3661 
3662  /* Use generic string SortSupport, forcing "C" collation */
3663  varstr_sortsupport(ssup, C_COLLATION_OID, false);
3664 
3665  MemoryContextSwitchTo(oldcontext);
3666 
3667  PG_RETURN_VOID();
3668 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
Definition: varlena.c:1808
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define C_COLLATION_OID
Definition: pg_collation.h:78
Datum bytea_string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 482 of file varlena.c.

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

483 {
485 
486  /* cannot be called directly because of internal-type argument */
487  Assert(AggCheckCallContext(fcinfo, NULL));
488 
489  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
490 
491  if (state != NULL)
492  {
493  bytea *result;
494 
495  result = (bytea *) palloc(state->len + VARHDRSZ);
496  SET_VARSIZE(result, state->len + VARHDRSZ);
497  memcpy(VARDATA(result), state->data, state->len);
498  PG_RETURN_BYTEA_P(result);
499  }
500  else
501  PG_RETURN_NULL();
502 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:681
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4116
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum bytea_string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 450 of file varlena.c.

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

451 {
453 
454  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
455 
456  /* Append the value unless null. */
457  if (!PG_ARGISNULL(1))
458  {
460 
461  /* On the first time through, we ignore the delimiter. */
462  if (state == NULL)
463  state = makeStringAggState(fcinfo);
464  else if (!PG_ARGISNULL(2))
465  {
466  bytea *delim = PG_GETARG_BYTEA_PP(2);
467 
469  }
470 
472  }
473 
474  /*
475  * The transition type for string_agg() is declared to be "internal",
476  * which is a pass-by-value type the same size as a pointer.
477  */
478  PG_RETURN_POINTER(state);
479 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static struct @121 value
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4675
Definition: c.h:433
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
Datum bytea_substr ( PG_FUNCTION_ARGS  )

Definition at line 2832 of file varlena.c.

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

2833 {
2835  PG_GETARG_INT32(1),
2836  PG_GETARG_INT32(2),
2837  false));
2838 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2855
Datum bytea_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2846 of file varlena.c.

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

2847 {
2849  PG_GETARG_INT32(1),
2850  -1,
2851  true));
2852 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static bytea * bytea_substring(Datum str, int S, int L, bool length_not_specified)
Definition: varlena.c:2855
static bytea * bytea_substring ( Datum  str,
int  S,
int  L,
bool  length_not_specified 
)
static

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

2859 {
2860  int S1; /* adjusted start position */
2861  int L1; /* adjusted substring length */
2862 
2863  S1 = Max(S, 1);
2864 
2865  if (length_not_specified)
2866  {
2867  /*
2868  * Not passed a length - DatumGetByteaPSlice() grabs everything to the
2869  * end of the string if we pass it a negative value for length.
2870  */
2871  L1 = -1;
2872  }
2873  else
2874  {
2875  /* end position */
2876  int E = S + L;
2877 
2878  /*
2879  * A negative value for L is the only way for the end position to be
2880  * before the start. SQL99 says to throw an error.
2881  */
2882  if (E < S)
2883  ereport(ERROR,
2884  (errcode(ERRCODE_SUBSTRING_ERROR),
2885  errmsg("negative substring length not allowed")));
2886 
2887  /*
2888  * A zero or negative value for the end position can happen if the
2889  * start was negative or one. SQL99 says to return a zero-length
2890  * string.
2891  */
2892  if (E < 1)
2893  return PG_STR_GET_BYTEA("");
2894 
2895  L1 = E - S1;
2896  }
2897 
2898  /*
2899  * If the start position is past the end of the string, SQL99 says to
2900  * return a zero-length string -- DatumGetByteaPSlice() will do that for
2901  * us. Convert to zero-based starting position
2902  */
2903  return DatumGetByteaPSlice(str, S1 - 1, L1);
2904 }
#define DatumGetByteaPSlice(X, m, n)
Definition: fmgr.h:267
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_STR_GET_BYTEA(str_)
Definition: varlena.c:2813
#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:806
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum byteacat ( PG_FUNCTION_ARGS  )

Definition at line 2765 of file varlena.c.

References bytea_catenate(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

2766 {
2767  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2768  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2769 
2771 }
static bytea * bytea_catenate(bytea *t1, bytea *t2)
Definition: varlena.c:2780
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
Definition: c.h:433
Datum byteacmp ( PG_FUNCTION_ARGS  )

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

3634 {
3635  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3636  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3637  int len1,
3638  len2;
3639  int cmp;
3640 
3641  len1 = VARSIZE_ANY_EXHDR(arg1);
3642  len2 = VARSIZE_ANY_EXHDR(arg2);
3643 
3644  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3645  if ((cmp == 0) && (len1 != len2))
3646  cmp = (len1 < len2) ? -1 : 1;
3647 
3648  PG_FREE_IF_COPY(arg1, 0);
3649  PG_FREE_IF_COPY(arg2, 1);
3650 
3651  PG_RETURN_INT32(cmp);
3652 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
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 3489 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().

3490 {
3491  Datum arg1 = PG_GETARG_DATUM(0);
3492  Datum arg2 = PG_GETARG_DATUM(1);
3493  bool result;
3494  Size len1,
3495  len2;
3496 
3497  /*
3498  * We can use a fast path for unequal lengths, which might save us from
3499  * having to detoast one or both values.
3500  */
3501  len1 = toast_raw_datum_size(arg1);
3502  len2 = toast_raw_datum_size(arg2);
3503  if (len1 != len2)
3504  result = false;
3505  else
3506  {
3507  bytea *barg1 = DatumGetByteaPP(arg1);
3508  bytea *barg2 = DatumGetByteaPP(arg2);
3509 
3510  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3511  len1 - VARHDRSZ) == 0);
3512 
3513  PG_FREE_IF_COPY(barg1, 0);
3514  PG_FREE_IF_COPY(barg2, 1);
3515  }
3516 
3517  PG_RETURN_BOOL(result);
3518 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define VARHDRSZ
Definition: c.h:439
#define DatumGetByteaPP(X)
Definition: fmgr.h:255
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
size_t Size
Definition: c.h:350
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum byteage ( PG_FUNCTION_ARGS  )

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

3614 {
3615  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3616  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3617  int len1,
3618  len2;
3619  int cmp;
3620 
3621  len1 = VARSIZE_ANY_EXHDR(arg1);
3622  len2 = VARSIZE_ANY_EXHDR(arg2);
3623 
3624  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3625 
3626  PG_FREE_IF_COPY(arg1, 0);
3627  PG_FREE_IF_COPY(arg2, 1);
3628 
3629  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
3630 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
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 3047 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.

3048 {
3049  bytea *v = PG_GETARG_BYTEA_PP(0);
3050  int32 n = PG_GETARG_INT32(1);
3051  int byteNo,
3052  bitNo;
3053  int len;
3054  int byte;
3055 
3056  len = VARSIZE_ANY_EXHDR(v);
3057 
3058  if (n < 0 || n >= len * 8)
3059  ereport(ERROR,
3060  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3061  errmsg("index %d out of valid range, 0..%d",
3062  n, len * 8 - 1)));
3063 
3064  byteNo = n / 8;
3065  bitNo = n % 8;
3066 
3067  byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3068 
3069  if (byte & (1 << bitNo))
3070  PG_RETURN_INT32(1);
3071  else
3072  PG_RETURN_INT32(0);
3073 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:246
#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:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum byteaGetByte ( PG_FUNCTION_ARGS  )

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

3019 {
3020  bytea *v = PG_GETARG_BYTEA_PP(0);
3021  int32 n = PG_GETARG_INT32(1);
3022  int len;
3023  int byte;
3024 
3025  len = VARSIZE_ANY_EXHDR(v);
3026 
3027  if (n < 0 || n >= len)
3028  ereport(ERROR,
3029  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3030  errmsg("index %d out of valid range, 0..%d",
3031  n, len - 1)));
3032 
3033  byte = ((unsigned char *) VARDATA_ANY(v))[n];
3034 
3035  PG_RETURN_INT32(byte);
3036 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:246
#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:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum byteagt ( PG_FUNCTION_ARGS  )

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

3594 {
3595  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3596  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3597  int len1,
3598  len2;
3599  int cmp;
3600 
3601  len1 = VARSIZE_ANY_EXHDR(arg1);
3602  len2 = VARSIZE_ANY_EXHDR(arg2);
3603 
3604  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3605 
3606  PG_FREE_IF_COPY(arg1, 0);
3607  PG_FREE_IF_COPY(arg2, 1);
3608 
3609  PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
3610 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
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 255 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().

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

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

3574 {
3575  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3576  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3577  int len1,
3578  len2;
3579  int cmp;
3580 
3581  len1 = VARSIZE_ANY_EXHDR(arg1);
3582  len2 = VARSIZE_ANY_EXHDR(arg2);
3583 
3584  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3585 
3586  PG_FREE_IF_COPY(arg1, 0);
3587  PG_FREE_IF_COPY(arg2, 1);
3588 
3589  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
3590 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
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 3553 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().

3554 {
3555  bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3556  bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3557  int len1,
3558  len2;
3559  int cmp;
3560 
3561  len1 = VARSIZE_ANY_EXHDR(arg1);
3562  len2 = VARSIZE_ANY_EXHDR(arg2);
3563 
3564  cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3565 
3566  PG_FREE_IF_COPY(arg1, 0);
3567  PG_FREE_IF_COPY(arg2, 1);
3568 
3569  PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3570 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
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 3521 of file varlena.c.

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

3522 {
3523  Datum arg1 = PG_GETARG_DATUM(0);
3524  Datum arg2 = PG_GETARG_DATUM(1);
3525  bool result;
3526  Size len1,
3527  len2;
3528 
3529  /*
3530  * We can use a fast path for unequal lengths, which might save us from
3531  * having to detoast one or both values.
3532  */
3533  len1 = toast_raw_datum_size(arg1);
3534  len2 = toast_raw_datum_size(arg2);
3535  if (len1 != len2)
3536  result = true;
3537  else
3538  {
3539  bytea *barg1 = DatumGetByteaPP(arg1);
3540  bytea *barg2 = DatumGetByteaPP(arg2);
3541 
3542  result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3543  len1 - VARHDRSZ) != 0);
3544 
3545  PG_FREE_IF_COPY(barg1, 0);
3546  PG_FREE_IF_COPY(barg2, 1);
3547  }
3548 
3549  PG_RETURN_BOOL(result);
3550 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define VARHDRSZ
Definition: c.h:439
#define DatumGetByteaPP(X)
Definition: fmgr.h:255
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
size_t Size
Definition: c.h:350
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum byteaoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 2749 of file varlena.c.

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

2750 {
2751  Datum str = PG_GETARG_DATUM(0);
2752 
2753  /* We need not detoast the input at all */
2755 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define VARHDRSZ
Definition: c.h:439
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
Size toast_raw_datum_size(Datum value)
Definition: tuptoaster.c:353
uintptr_t Datum
Definition: postgres.h:372
Datum byteaout ( PG_FUNCTION_ARGS  )

Definition at line 351 of file varlena.c.

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

Referenced by patternsel(), and prefix_quals().

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

Definition at line 2914 of file varlena.c.

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

2915 {
2916  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2917  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2918  int sp = PG_GETARG_INT32(2); /* substring start position */
2919  int sl = PG_GETARG_INT32(3); /* substring length */
2920 
2921  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2922 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2937
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
Definition: c.h:433
Datum byteaoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 2925 of file varlena.c.

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

2926 {
2927  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2928  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2929  int sp = PG_GETARG_INT32(2); /* substring start position */
2930  int sl;
2931 
2932  sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
2933  PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2934 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static bytea * bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
Definition: varlena.c:2937
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum byteapos ( PG_FUNCTION_ARGS  )

Definition at line 2974 of file varlena.c.

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

2975 {
2976  bytea *t1 = PG_GETARG_BYTEA_PP(0);
2977  bytea *t2 = PG_GETARG_BYTEA_PP(1);
2978  int pos;
2979  int px,
2980  p;
2981  int len1,
2982  len2;
2983  char *p1,
2984  *p2;
2985 
2986  len1 = VARSIZE_ANY_EXHDR(t1);
2987  len2 = VARSIZE_ANY_EXHDR(t2);
2988 
2989  if (len2 <= 0)
2990  PG_RETURN_INT32(1); /* result for empty pattern */
2991 
2992  p1 = VARDATA_ANY(t1);
2993  p2 = VARDATA_ANY(t2);
2994 
2995  pos = 0;
2996  px = (len1 - len2);
2997  for (p = 0; p <= px; p++)
2998  {
2999  if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3000  {
3001  pos = p + 1;
3002  break;
3003  };
3004  p1++;
3005  };
3006 
3007  PG_RETURN_INT32(pos);
3008 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum bytearecv ( PG_FUNCTION_ARGS  )

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

424 {
426  bytea *result;
427  int nbytes;
428 
429  nbytes = buf->len - buf->cursor;
430  result = (bytea *) palloc(nbytes + VARHDRSZ);
431  SET_VARSIZE(result, nbytes + VARHDRSZ);
432  pq_copymsgbytes(buf, VARDATA(result), nbytes);
433  PG_RETURN_BYTEA_P(result);
434 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static char * buf
Definition: pg_test_fsync.c:67
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum byteasend ( PG_FUNCTION_ARGS  )

Definition at line 442 of file varlena.c.

References PG_GETARG_BYTEA_P_COPY, and PG_RETURN_BYTEA_P.

Referenced by pg_dependencies_send(), and pg_ndistinct_send().

443 {
444  bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
445 
446  PG_RETURN_BYTEA_P(vlena);
447 }
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:278
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
Definition: c.h:433
Datum byteaSetBit ( PG_FUNCTION_ARGS  )

Definition at line 3116 of file varlena.c.

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

3117 {
3118  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3119  int32 n = PG_GETARG_INT32(1);
3120  int32 newBit = PG_GETARG_INT32(2);
3121  int len;
3122  int oldByte,
3123  newByte;
3124  int byteNo,
3125  bitNo;
3126 
3127  len = VARSIZE(res) - VARHDRSZ;
3128 
3129  if (n < 0 || n >= len * 8)
3130  ereport(ERROR,
3131  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3132  errmsg("index %d out of valid range, 0..%d",
3133  n, len * 8 - 1)));
3134 
3135  byteNo = n / 8;
3136  bitNo = n % 8;
3137 
3138  /*
3139  * sanity check!
3140  */
3141  if (newBit != 0 && newBit != 1)
3142  ereport(ERROR,
3143  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3144  errmsg("new bit must be 0 or 1")));
3145 
3146  /*
3147  * Update the byte.
3148  */
3149  oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3150 
3151  if (newBit == 0)
3152  newByte = oldByte & (~(1 << bitNo));
3153  else
3154  newByte = oldByte | (1 << bitNo);
3155 
3156  ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3157 
3158  PG_RETURN_BYTEA_P(res);
3159 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:278
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum byteaSetByte ( PG_FUNCTION_ARGS  )

Definition at line 3084 of file varlena.c.

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

3085 {
3086  bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3087  int32 n = PG_GETARG_INT32(1);
3088  int32 newByte = PG_GETARG_INT32(2);
3089  int len;
3090 
3091  len = VARSIZE(res) - VARHDRSZ;
3092 
3093  if (n < 0 || n >= len)
3094  ereport(ERROR,
3095  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3096  errmsg("index %d out of valid range, 0..%d",
3097  n, len - 1)));
3098 
3099  /*
3100  * Now set the byte.
3101  */
3102  ((unsigned char *) VARDATA(res))[n] = newByte;
3103 
3104  PG_RETURN_BYTEA_P(res);
3105 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:439
#define PG_GETARG_BYTEA_P_COPY(n)
Definition: fmgr.h:278
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
static int charlen_to_bytelen ( const char *  p,
int  n 
)
static

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

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

Definition at line 3774 of file varlena.c.

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

Referenced by replace_text_regexp().

3775 {
3776  const char *p = VARDATA_ANY(replace_text);
3777  const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3778 
3780  {
3781  for (; p < p_end; p++)
3782  {
3783  if (*p == '\\')
3784  return true;
3785  }
3786  }
3787  else
3788  {
3789  for (; p < p_end; p += pg_mblen(p))
3790  {
3791  if (*p == '\\')
3792  return true;
3793  }
3794  }
3795 
3796  return false;
3797 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static text* concat_internal ( const char *  sepstr,
int  argidx,
FunctionCallInfo  fcinfo 
)
static

Definition at line 4785 of file varlena.c.

References appendStringInfoString(), array_to_text_internal(), Assert, build_concat_foutcache(), cstring_to_text_with_len(), StringInfoData::data, FunctionCallInfoData::flinfo, FmgrInfo::fn_extra, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, initStringInfo(), StringInfoData::len, OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_NARGS, and value.

Referenced by text_concat(), and text_concat_ws().

4787 {
4788  text *result;
4789  StringInfoData str;
4790  FmgrInfo *foutcache;
4791  bool first_arg = true;
4792  int i;
4793 
4794  /*
4795  * concat(VARIADIC some-array) is essentially equivalent to
4796  * array_to_text(), ie concat the array elements with the given separator.
4797  * So we just pass the case off to that code.
4798  */
4799  if (get_fn_expr_variadic(fcinfo->flinfo))
4800  {
4801  ArrayType *arr;
4802 
4803  /* Should have just the one argument */
4804  Assert(argidx == PG_NARGS() - 1);
4805 
4806  /* concat(VARIADIC NULL) is defined as NULL */
4807  if (PG_ARGISNULL(argidx))
4808  return NULL;
4809 
4810  /*
4811  * Non-null argument had better be an array. We assume that any call
4812  * context that could let get_fn_expr_variadic return true will have
4813  * checked that a VARIADIC-labeled parameter actually is an array. So
4814  * it should be okay to just Assert that it's an array rather than
4815  * doing a full-fledged error check.
4816  */
4818 
4819  /* OK, safe to fetch the array value */
4820  arr = PG_GETARG_ARRAYTYPE_P(argidx);
4821 
4822  /*
4823  * And serialize the array. We tell array_to_text to ignore null
4824  * elements, which matches the behavior of the loop below.
4825  */
4826  return array_to_text_internal(fcinfo, arr, sepstr, NULL);
4827  }
4828 
4829  /* Normal case without explicit VARIADIC marker */
4830  initStringInfo(&str);
4831 
4832  /* Get output function info, building it if first time through */
4833  foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
4834  if (foutcache == NULL)
4835  foutcache = build_concat_foutcache(fcinfo, argidx);
4836 
4837  for (i = argidx; i < PG_NARGS(); i++)
4838  {
4839  if (!PG_ARGISNULL(i))
4840  {
4842 
4843  /* add separator if appropriate */
4844  if (first_arg)
4845  first_arg = false;
4846  else
4847  appendStringInfoString(&str, sepstr);
4848 
4849  /* call the appropriate type output function, append the result */
4851  OutputFunctionCall(&foutcache[i], value));
4852  }
4853  }
4854 
4855  result = cstring_to_text_with_len(str.data, str.len);
4856  pfree(str.data);
4857 
4858  return result;
4859 }
Definition: fmgr.h:56
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2038
#define OidIsValid(objectId)
Definition: c.h:532
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1662
FmgrInfo * flinfo
Definition: fmgr.h:79
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
void pfree(void *pointer)
Definition: mcxt.c:949
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
static struct @121 value
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition: varlena.c:4402
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:681
#define PG_NARGS()
Definition: fmgr.h:168
void * fn_extra
Definition: fmgr.h:64
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2557
int i
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition: varlena.c:4747
Definition: c.h:433
text* cstring_to_text ( const char *  s)

Definition at line 149 of file varlena.c.

References cstring_to_text_with_len().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_page_type(), cash_words(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), 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_actual_version(), pg_collation_for(), pg_crypt(), pg_current_logfile(), pg_describe_object(), pg_export_snapshot(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_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(), txid_status(), unaccent_dict(), upper(), X509_NAME_to_text(), xml_encode_special_chars(), xml_in(), and xml_recv().

150 {
151  return cstring_to_text_with_len(s, strlen(s));
152 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
text* cstring_to_text_with_len ( const char *  s,
int  len 
)

Definition at line 161 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(), ExecEvalXmlExpr(), 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(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), varchar(), varchar_input(), and xslt_process().

162 {
163  text *result = (text *) palloc(len + VARHDRSZ);
164 
165  SET_VARSIZE(result, len + VARHDRSZ);
166  memcpy(VARDATA(result), s, len);
167 
168  return result;
169 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

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

2625 {
2626  int result;
2627  int len1,
2628  len2;
2629 
2630  len1 = VARSIZE_ANY_EXHDR(arg1);
2631  len2 = VARSIZE_ANY_EXHDR(arg2);
2632 
2633  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2634  if (result != 0)
2635  return result;
2636  else if (len1 < len2)
2637  return -1;
2638  else if (len1 > len2)
2639  return 1;
2640  else
2641  return 0;
2642 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:812
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 4675 of file varlena.c.

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

Referenced by bytea_string_agg_transfn(), and string_agg_transfn().

4676 {
4677  StringInfo state;
4678  MemoryContext aggcontext;
4679  MemoryContext oldcontext;
4680 
4681  if (!AggCheckCallContext(fcinfo, &aggcontext))
4682  {
4683  /* cannot be called directly because of internal-type argument */
4684  elog(ERROR, "string_agg_transfn called in non-aggregate context");
4685  }
4686 
4687  /*
4688  * Create state in aggregate context. It'll stay there across subsequent
4689  * calls.
4690  */
4691  oldcontext = MemoryContextSwitchTo(aggcontext);
4692  state = makeStringInfo();
4693  MemoryContextSwitchTo(oldcontext);
4694 
4695  return state;
4696 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:43
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4116
#define elog
Definition: elog.h:219
Datum md5_bytea ( PG_FUNCTION_ARGS  )

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

4601 {
4602  bytea *in = PG_GETARG_BYTEA_PP(0);
4603  size_t len;
4604  char hexsum[MD5_HASH_LEN + 1];
4605 
4606  len = VARSIZE_ANY_EXHDR(in);
4607  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
4608  ereport(ERROR,
4609  (errcode(ERRCODE_OUT_OF_MEMORY),
4610  errmsg("out of memory")));
4611 
4613 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define MD5_HASH_LEN
Definition: varlena.c:4573
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:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
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:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum md5_text ( PG_FUNCTION_ARGS  )

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

4577 {
4578  text *in_text = PG_GETARG_TEXT_PP(0);
4579  size_t len;
4580  char hexsum[MD5_HASH_LEN + 1];
4581 
4582  /* Calculate the length of the buffer using varlena metadata */
4583  len = VARSIZE_ANY_EXHDR(in_text);
4584 
4585  /* get the hash result */
4586  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
4587  ereport(ERROR,
4588  (errcode(ERRCODE_OUT_OF_MEMORY),
4589  errmsg("out of memory")));
4590 
4591  /* convert to text and return it */
4593 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define MD5_HASH_LEN
Definition: varlena.c:4573
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 3189 of file varlena.c.

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

Referenced by nameiclike(), and nameicnlike().

3190 {
3191  Name s = PG_GETARG_NAME(0);
3192 
3194 }
Definition: c.h:487
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 4621 of file varlena.c.

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

4622 {
4624  int32 result;
4625  int typlen;
4626 
4627  /* On first call, get the input type's typlen, and save at *fn_extra */
4628  if (fcinfo->flinfo->fn_extra == NULL)
4629  {
4630  /* Lookup the datatype of the supplied argument */
4631  Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4632 
4633  typlen = get_typlen(argtypeid);
4634  if (typlen == 0) /* should not happen */
4635  elog(ERROR, "cache lookup failed for type %u", argtypeid);
4636 
4637  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4638  sizeof(int));
4639  *((int *) fcinfo->flinfo->fn_extra) = typlen;
4640  }
4641  else
4642  typlen = *((int *) fcinfo->flinfo->fn_extra);
4643 
4644  if (typlen == -1)
4645  {
4646  /* varlena type, possibly toasted */
4647  result = toast_datum_size(value);
4648  }
4649  else if (typlen == -2)
4650  {
4651  /* cstring */
4652  result = strlen(DatumGetCString(value)) + 1;
4653  }
4654  else
4655  {
4656  /* ordinary fixed-width type */
4657  result = typlen;
4658  }
4659 
4660  PG_RETURN_INT32(result);
4661 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:572
Size toast_datum_size(Datum value)
Definition: tuptoaster.c:409
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
static struct @121 value
uintptr_t Datum
Definition: postgres.h:372
int16 get_typlen(Oid typid)
Definition: lsyscache.c:1947
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define elog
Definition: elog.h:219
Datum replace_text ( PG_FUNCTION_ARGS  )

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

3692 {
3693  text *src_text = PG_GETARG_TEXT_PP(0);
3694  text *from_sub_text = PG_GETARG_TEXT_PP(1);
3695  text *to_sub_text = PG_GETARG_TEXT_PP(2);
3696  int src_text_len;
3697  int from_sub_text_len;
3699  text *ret_text;
3700  int start_posn;
3701  int curr_posn;
3702  int chunk_len;
3703  char *start_ptr;
3704  StringInfoData str;
3705 
3706  text_position_setup(src_text, from_sub_text, &state);
3707 
3708  /*
3709  * Note: we check the converted string length, not the original, because
3710  * they could be different if the input contained invalid encoding.
3711  */
3712  src_text_len = state.len1;
3713  from_sub_text_len = state.len2;
3714 
3715  /* Return unmodified source string if empty source or pattern */
3716  if (src_text_len < 1 || from_sub_text_len < 1)
3717  {
3718  text_position_cleanup(&state);
3719  PG_RETURN_TEXT_P(src_text);
3720  }
3721 
3722  start_posn = 1;
3723  curr_posn = text_position_next(1, &state);
3724 
3725  /* When the from_sub_text is not found, there is nothing to do. */
3726  if (curr_posn == 0)
3727  {
3728  text_position_cleanup(&state);
3729  PG_RETURN_TEXT_P(src_text);
3730  }
3731 
3732  /* start_ptr points to the start_posn'th character of src_text */
3733  start_ptr = VARDATA_ANY(src_text);
3734 
3735  initStringInfo(&str);
3736 
3737  do
3738  {
3740 
3741  /* copy the data skipped over by last text_position_next() */
3742  chunk_len = charlen_to_bytelen(start_ptr, curr_posn - start_posn);
3743  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3744 
3745  appendStringInfoText(&str, to_sub_text);
3746 
3747  start_posn = curr_posn;
3748  start_ptr += chunk_len;
3749  start_posn += from_sub_text_len;
3750  start_ptr += charlen_to_bytelen(start_ptr, from_sub_text_len);
3751 
3752  curr_posn = text_position_next(start_posn, &state);
3753  }
3754  while (curr_posn > 0);
3755 
3756  /* copy trailing data */
3757  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
3758  appendBinaryStringInfo(&str, start_ptr, chunk_len);
3759 
3760  text_position_cleanup(&state);
3761 
3762  ret_text = cstring_to_text_with_len(str.data, str.len);
3763  pfree(str.data);
3764 
3765  PG_RETURN_TEXT_P(ret_text);
3766 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1119
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:949
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1231
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:735
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3677
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1365
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
text* replace_text_regexp ( text src_text,
void *  regexp,
text replace_text,
bool  glob 
)

Definition at line 3912 of file varlena.c.

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

Referenced by textregexreplace(), and textregexreplace_noopt().

3914 {
3915  text *ret_text;
3916  regex_t *re = (regex_t *) regexp;
3917  int src_text_len = VARSIZE_ANY_EXHDR(src_text);
3920  pg_wchar *data;
3921  size_t data_len;
3922  int search_start;
3923  int data_pos;
3924  char *start_ptr;
3925  bool have_escape;
3926 
3927  initStringInfo(&buf);
3928 
3929  /* Convert data string to wide characters. */
3930  data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
3931  data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
3932 
3933  /* Check whether replace_text has escape char. */
3934  have_escape = check_replace_text_has_escape_char(replace_text);
3935 
3936  /* start_ptr points to the data_pos'th character of src_text */
3937  start_ptr = (char *) VARDATA_ANY(src_text);
3938  data_pos = 0;
3939 
3940  search_start = 0;
3941  while (search_start <= data_len)
3942  {
3943  int regexec_result;
3944 
3946 
3947  regexec_result = pg_regexec(re,
3948  data,
3949  data_len,
3950  search_start,
3951  NULL, /* no details */
3953  pmatch,
3954  0);
3955 
3956  if (regexec_result == REG_NOMATCH)
3957  break;
3958 
3959  if (regexec_result != REG_OKAY)
3960  {
3961  char errMsg[100];
3962 
3964  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
3965  ereport(ERROR,
3966  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
3967  errmsg("regular expression failed: %s", errMsg)));
3968  }
3969 
3970  /*
3971  * Copy the text to the left of the match position. Note we are given
3972  * character not byte indexes.
3973  */
3974  if (pmatch[0].rm_so - data_pos > 0)
3975  {
3976  int chunk_len;
3977 
3978  chunk_len = charlen_to_bytelen(start_ptr,
3979  pmatch[0].rm_so - data_pos);
3980  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
3981 
3982  /*
3983  * Advance start_ptr over that text, to avoid multiple rescans of
3984  * it if the replace_text contains multiple back-references.
3985  */
3986  start_ptr += chunk_len;
3987  data_pos = pmatch[0].rm_so;
3988  }
3989 
3990  /*
3991  * Copy the replace_text. Process back references when the
3992  * replace_text has escape characters.
3993  */
3994  if (have_escape)
3995  appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
3996  start_ptr, data_pos);
3997  else
3998  appendStringInfoText(&buf, replace_text);
3999 
4000  /* Advance start_ptr and data_pos over the matched text. */
4001  start_ptr += charlen_to_bytelen(start_ptr,
4002  pmatch[0].rm_eo - data_pos);
4003  data_pos = pmatch[0].rm_eo;
4004 
4005  /*
4006  * When global option is off, replace the first instance only.
4007  */
4008  if (!glob)
4009  break;
4010 
4011  /*
4012  * Advance search position. Normally we start the next search at the
4013  * end of the previous match; but if the match was of zero length, we
4014  * have to advance by one character, or we'd just find the same match
4015  * again.
4016  */
4017  search_start = data_pos;
4018  if (pmatch[0].rm_so == pmatch[0].rm_eo)
4019  search_start++;
4020  }
4021 
4022  /*
4023  * Copy the text to the right of the last match.
4024  */
4025  if (data_pos < data_len)
4026  {
4027  int chunk_len;
4028 
4029  chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4030  appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4031  }
4032 
4033  ret_text = cstring_to_text_with_len(buf.data, buf.len);
4034  pfree(buf.data);
4035  pfree(data);
4036 
4037  return ret_text;
4038 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
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:949
#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:3774
static int charlen_to_bytelen(const char *p, int n)
Definition: varlena.c:735
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3677
static char * buf
Definition: pg_test_fsync.c:67
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
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:46
#define REGEXP_REPLACE_BACKREF_CNT
Definition: varlena.c:3901
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:723
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition: varlena.c:3807
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:340
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define REG_NOMATCH
Definition: regex.h:138
Definition: c.h:433
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
Definition: regex.h:55
static bool rest_of_char_same ( const char *  s1,
const char *  s2,
int  len 
)
inlinestatic

Definition at line 5511 of file varlena.c.

Referenced by varstr_levenshtein().

5512 {
5513  while (len > 0)
5514  {
5515  len--;
5516  if (s1[len] != s2[len])
5517  return false;
5518  }
5519  return true;
5520 }
char * s1
char * s2
Datum split_text ( PG_FUNCTION_ARGS  )

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

4048 {
4049  text *inputstring = PG_GETARG_TEXT_PP(0);
4050  text *fldsep = PG_GETARG_TEXT_PP(1);
4051  int fldnum = PG_GETARG_INT32(2);
4052  int inputstring_len;
4053  int fldsep_len;
4055  int start_posn;
4056  int end_posn;
4057  text *result_text;
4058 
4059  /* field number is 1 based */
4060  if (fldnum < 1)
4061  ereport(ERROR,
4062  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4063  errmsg("field position must be greater than zero")));
4064 
4065  text_position_setup(inputstring, fldsep, &state);
4066 
4067  /*
4068  * Note: we check the converted string length, not the original, because
4069  * they could be different if the input contained invalid encoding.
4070  */
4071  inputstring_len = state.len1;
4072  fldsep_len = state.len2;
4073 
4074  /* return empty string for empty input string */
4075  if (inputstring_len < 1)
4076  {
4077  text_position_cleanup(&state);
4079  }
4080 
4081  /* empty field separator */
4082  if (fldsep_len < 1)
4083  {
4084  text_position_cleanup(&state);
4085  /* if first field, return input string, else empty string */
4086  if (fldnum == 1)
4087  PG_RETURN_TEXT_P(inputstring);
4088  else
4090  }
4091 
4092  /* identify bounds of first field */
4093  start_posn = 1;
4094  end_posn = text_position_next(1, &state);
4095 
4096  /* special case if fldsep not found at all */
4097  if (end_posn == 0)
4098  {
4099  text_position_cleanup(&state);
4100  /* if field 1 requested, return input string, else empty string */
4101  if (fldnum == 1)
4102  PG_RETURN_TEXT_P(inputstring);
4103  else
4105  }
4106 
4107  while (end_posn > 0 && --fldnum > 0)
4108  {
4109  /* identify bounds of next field */
4110  start_posn = end_posn + fldsep_len;
4111  end_posn = text_position_next(start_posn, &state);
4112  }
4113 
4114  text_position_cleanup(&state);
4115 
4116  if (fldnum > 0)
4117  {
4118  /* N'th field separator not found */
4119  /* if last field requested, return it, else empty string */
4120  if (fldnum == 1)
4121  result_text = text_substring(PointerGetDatum(inputstring),
4122  start_posn,
4123  -1,
4124  true);
4125  else
4126  result_text = cstring_to_text("");
4127  }
4128  else
4129  {
4130  /* non-last field requested */
4131  result_text = text_substring(PointerGetDatum(inputstring),
4132  start_posn,
4133  end_posn - start_posn,
4134  false);
4135  }
4136 
4137  PG_RETURN_TEXT_P(result_text);
4138 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PointerGetDatum(X)
Definition: postgres.h:562
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1119
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1231
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define ereport(elevel, rest)
Definition: elog.h:122
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1365
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
bool SplitDirectoriesString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3391 of file varlena.c.

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

Referenced by load_libraries(), and PostmasterMain().

3393 {
3394  char *nextp = rawstring;
3395  bool done = false;
3396 
3397  *namelist = NIL;
3398 
3399  while (scanner_isspace(*nextp))
3400  nextp++; /* skip leading whitespace */
3401 
3402  if (*nextp == '\0')
3403  return true; /* allow empty string */
3404 
3405  /* At the top of the loop, we are at start of a new directory. */
3406  do
3407  {
3408  char *curname;
3409  char *endp;
3410 
3411  if (*nextp == '"')
3412  {
3413  /* Quoted name --- collapse quote-quote pairs */
3414  curname = nextp + 1;
3415  for (;;)
3416  {
3417  endp = strchr(nextp + 1, '"');
3418  if (endp == NULL)
3419  return false; /* mismatched quotes */
3420  if (endp[1] != '"')
3421  break; /* found end of quoted name */
3422  /* Collapse adjacent quotes into one quote, and look again */
3423  memmove(endp, endp + 1, strlen(endp));
3424  nextp = endp;
3425  }
3426  /* endp now points at the terminating quote */
3427  nextp = endp + 1;
3428  }
3429  else
3430  {
3431  /* Unquoted name --- extends to separator or end of string */
3432  curname = endp = nextp;
3433  while (*nextp && *nextp != separator)
3434  {
3435  /* trailing whitespace should not be included in name */
3436  if (!scanner_isspace(*nextp))
3437  endp = nextp + 1;
3438  nextp++;
3439  }
3440  if (curname == endp)
3441  return false; /* empty unquoted name not allowed */
3442  }
3443 
3444  while (scanner_isspace(*nextp))
3445  nextp++; /* skip trailing whitespace */
3446 
3447  if (*nextp == separator)
3448  {
3449  nextp++;
3450  while (scanner_isspace(*nextp))
3451  nextp++; /* skip leading whitespace for next */
3452  /* we expect another name, so done remains false */
3453  }
3454  else if (*nextp == '\0')
3455  done = true;
3456  else
3457  return false; /* invalid syntax */
3458 
3459  /* Now safe to overwrite separator with a null */
3460  *endp = '\0';
3461 
3462  /* Truncate path if it's overlength */
3463  if (strlen(curname) >= MAXPGPATH)
3464  curname[MAXPGPATH - 1] = '\0';
3465 
3466  /*
3467  * Finished isolating current name --- add it to list
3468  */
3469  curname = pstrdup(curname);
3470  canonicalize_path(curname);
3471  *namelist = lappend(*namelist, curname);
3472 
3473  /* Loop back if we didn't reach end of string */
3474  } while (!done);
3475 
3476  return true;
3477 }
#define NIL
Definition: pg_list.h:69
char * pstrdup(const char *in)
Definition: mcxt.c:1076
void canonicalize_path(char *path)
Definition: path.c:254
#define MAXPGPATH
#define memmove(d, s, c)
Definition: c.h:1064
List * lappend(List *list, void *datum)
Definition: list.c:128
bool scanner_isspace(char ch)
Definition: scansup.c:221
bool SplitIdentifierString ( char *  rawstring,
char  separator,
List **  namelist 
)

Definition at line 3264 of file varlena.c.

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

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

3266 {
3267  char *nextp = rawstring;
3268  bool done = false;
3269 
3270  *namelist = NIL;
3271 
3272  while (scanner_isspace(*nextp))
3273  nextp++; /* skip leading whitespace */
3274 
3275  if (*nextp == '\0')
3276  return true; /* allow empty string */
3277 
3278  /* At the top of the loop, we are at start of a new identifier. */
3279  do
3280  {
3281  char *curname;
3282  char *endp;
3283 
3284  if (*nextp == '"')
3285  {
3286  /* Quoted name --- collapse quote-quote pairs, no downcasing */
3287  curname = nextp + 1;
3288  for (;;)
3289  {
3290  endp = strchr(nextp + 1, '"');
3291  if (endp == NULL)
3292  return false; /* mismatched quotes */
3293  if (endp[1] != '"')
3294  break; /* found end of quoted name */
3295  /* Collapse adjacent quotes into one quote, and look again */
3296  memmove(endp, endp + 1, strlen(endp));
3297  nextp = endp;
3298  }
3299  /* endp now points at the terminating quote */
3300  nextp = endp + 1;
3301  }
3302  else
3303  {
3304  /* Unquoted name --- extends to separator or whitespace */
3305  char *downname;
3306  int len;
3307 
3308  curname = nextp;
3309  while (*nextp && *nextp != separator &&
3310  !scanner_isspace(*nextp))
3311  nextp++;
3312  endp = nextp;
3313  if (curname == nextp)
3314  return false; /* empty unquoted name not allowed */
3315 
3316  /*
3317  * Downcase the identifier, using same code as main lexer does.
3318  *
3319  * XXX because we want to overwrite the input in-place, we cannot
3320  * support a downcasing transformation that increases the string
3321  * length. This is not a problem given the current implementation
3322  * of downcase_truncate_identifier, but we'll probably have to do
3323  * something about this someday.
3324  */
3325  len = endp - curname;
3326  downname = downcase_truncate_identifier(curname, len, false);
3327  Assert(strlen(downname) <= len);
3328  strncpy(curname, downname, len); /* strncpy is required here */
3329  pfree(downname);
3330  }
3331 
3332  while (scanner_isspace(*nextp))
3333  nextp++; /* skip trailing whitespace */
3334 
3335  if (*nextp == separator)
3336  {
3337  nextp++;
3338  while (scanner_isspace(*nextp))
3339  nextp++; /* skip leading whitespace for next */
3340  /* we expect another name, so done remains false */
3341  }
3342  else if (*nextp == '\0')
3343  done = true;
3344  else
3345  return false; /* invalid syntax */
3346 
3347  /* Now safe to overwrite separator with a null */
3348  *endp = '\0';
3349 
3350  /* Truncate name if it's overlength */
3351  truncate_identifier(curname, strlen(curname), false);
3352 
3353  /*
3354  * Finished isolating current name --- add it to list
3355  */
3356  *namelist = lappend(*namelist, curname);
3357 
3358  /* Loop back if we didn't reach end of string */
3359  } while (!done);
3360 
3361  return true;
3362 }
#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:949
#define memmove(d, s, c)
Definition: c.h:1064
List * lappend(List *list, void *datum)
Definition: list.c:128
bool scanner_isspace(char ch)
Definition: scansup.c:221
#define Assert(condition)
Definition: c.h:681
Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 4725 of file varlena.c.

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

4726 {
4727  StringInfo state;
4728 
4729  /* cannot be called directly because of internal-type argument */
4730  Assert(AggCheckCallContext(fcinfo, NULL));
4731 
4732  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4733 
4734  if (state != NULL)
4736  else
4737  PG_RETURN_NULL();
4738 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:681
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4116
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 4699 of file varlena.c.

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

4700 {
4701  StringInfo state;
4702 
4703  state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4704 
4705  /* Append the value unless null. */
4706  if (!PG_ARGISNULL(1))
4707  {
4708  /* On the first time through, we ignore the delimiter. */
4709  if (state == NULL)
4710  state = makeStringAggState(fcinfo);
4711  else if (!PG_ARGISNULL(2))
4712  appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
4713 
4714  appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
4715  }
4716 
4717  /*
4718  * The transition type for string_agg() is declared to be "internal",
4719  * which is a pass-by-value type the same size as a pointer.
4720  */
4721  PG_RETURN_POINTER(state);
4722 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static void appendStringInfoText(StringInfo str, const text *t)
Definition: varlena.c:3677
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition: varlena.c:4675
static text * text_catenate ( text t1,
text t2 
)
static

Definition at line 694 of file varlena.c.

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

Referenced by text_overlay(), and textcat().

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

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

1618 {
1619  char *a1p,
1620  *a2p;
1621  int len1,
1622  len2;
1623 
1624  a1p = VARDATA_ANY(arg1);
1625  a2p = VARDATA_ANY(arg2);
1626 
1627  len1 = VARSIZE_ANY_EXHDR(arg1);
1628  len2 = VARSIZE_ANY_EXHDR(arg2);
1629 
1630  return varstr_cmp(a1p, len1, a2p, len2, collid);
1631 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 4865 of file varlena.c.

References concat_internal(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

4866 {
4867  text *result;
4868 
4869  result = concat_internal("", 0, fcinfo);
4870  if (result == NULL)
4871  PG_RETURN_NULL();
4872  PG_RETURN_TEXT_P(result);
4873 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4785
Definition: c.h:433
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 4880 of file varlena.c.

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

4881 {
4882  char *sep;
4883  text *result;
4884 
4885  /* return NULL when separator is NULL */
4886  if (PG_ARGISNULL(0))
4887  PG_RETURN_NULL();
4889 
4890  result = concat_internal(sep, 1, fcinfo);
4891  if (result == NULL)
4892  PG_RETURN_NULL();
4893  PG_RETURN_TEXT_P(result);
4894 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition: varlena.c:4785
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:433
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum text_format ( PG_FUNCTION_ARGS  )

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

4998 {
4999  text *fmt;
5000  StringInfoData str;
5001  const char *cp;
5002  const char *start_ptr;
5003  const char *end_ptr;
5004  text *result;
5005  int arg;
5006  bool funcvariadic;
5007  int nargs;
5008  Datum *elements = NULL;
5009  bool *nulls = NULL;
5010  Oid element_type = InvalidOid;
5011  Oid prev_type = InvalidOid;
5012  Oid prev_width_type = InvalidOid;
5013  FmgrInfo typoutputfinfo;
5014  FmgrInfo typoutputinfo_width;
5015 
5016  /* When format string is null, immediately return null */
5017  if (PG_ARGISNULL(0))
5018  PG_RETURN_NULL();
5019 
5020  /* If argument is marked VARIADIC, expand array into elements */
5021  if (get_fn_expr_variadic(fcinfo->flinfo))
5022  {
5023  ArrayType *arr;
5024  int16 elmlen;
5025  bool elmbyval;
5026  char elmalign;
5027  int nitems;
5028 
5029  /* Should have just the one argument */
5030  Assert(PG_NARGS() == 2);
5031 
5032  /* If argument is NULL, we treat it as zero-length array */
5033  if (PG_ARGISNULL(1))
5034  nitems = 0;
5035  else
5036  {
5037  /*
5038  * Non-null argument had better be an array. We assume that any
5039  * call context that could let get_fn_expr_variadic return true
5040  * will have checked that a VARIADIC-labeled parameter actually is
5041  * an array. So it should be okay to just Assert that it's an
5042  * array rather than doing a full-fledged error check.
5043  */
5045 
5046  /* OK, safe to fetch the array value */
5047  arr = PG_GETARG_ARRAYTYPE_P(1);
5048 
5049  /* Get info about array element type */
5050  element_type = ARR_ELEMTYPE(arr);
5051  get_typlenbyvalalign(element_type,
5052  &elmlen, &elmbyval, &elmalign);
5053 
5054  /* Extract all array elements */
5055  deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5056  &elements, &nulls, &nitems);
5057  }
5058 
5059  nargs = nitems + 1;
5060  funcvariadic = true;
5061  }
5062  else
5063  {
5064  /* Non-variadic case, we'll process the arguments individually */
5065  nargs = PG_NARGS();
5066  funcvariadic = false;
5067  }
5068 
5069  /* Setup for main loop. */
5070  fmt = PG_GETARG_TEXT_PP(0);
5071  start_ptr = VARDATA_ANY(fmt);
5072  end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5073  initStringInfo(&str);
5074  arg = 1; /* next argument position to print */
5075 
5076  /* Scan format string, looking for conversion specifiers. */
5077  for (cp = start_ptr; cp < end_ptr; cp++)
5078  {
5079  int argpos;
5080  int widthpos;
5081  int flags;
5082  int width;
5083  Datum value;
5084  bool isNull;
5085  Oid typid;
5086 
5087  /*
5088  * If it's not the start of a conversion specifier, just copy it to
5089  * the output buffer.
5090  */
5091  if (*cp != '%')
5092  {
5093  appendStringInfoCharMacro(&str, *cp);
5094  continue;
5095  }
5096 
5097  ADVANCE_PARSE_POINTER(cp, end_ptr);
5098 
5099  /* Easy case: %% outputs a single % */
5100  if (*cp == '%')
5101  {
5102  appendStringInfoCharMacro(&str, *cp);
5103  continue;
5104  }
5105 
5106  /* Parse the optional portions of the format specifier */
5107  cp = text_format_parse_format(cp, end_ptr,
5108  &argpos, &widthpos,
5109  &flags, &width);
5110 
5111  /*
5112  * Next we should see the main conversion specifier. Whether or not
5113  * an argument position was present, it's known that at least one
5114  * character remains in the string at this point. Experience suggests
5115  * that it's worth checking that that character is one of the expected
5116  * ones before we try to fetch arguments, so as to produce the least
5117  * confusing response to a mis-formatted specifier.
5118  */
5119  if (strchr("sIL", *cp) == NULL)
5120  ereport(ERROR,
5121  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5122  errmsg("unrecognized format() type specifier \"%c\"",
5123  *cp),
5124  errhint("For a single \"%%\" use \"%%%%\".")));
5125 
5126  /* If indirect width was specified, get its value */
5127  if (widthpos >= 0)
5128  {
5129  /* Collect the specified or next argument position */
5130  if (widthpos > 0)
5131  arg = widthpos;
5132  if (arg >= nargs)
5133  ereport(ERROR,
5134  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5135  errmsg("too few arguments for format()")));
5136 
5137  /* Get the value and type of the selected argument */
5138  if (!funcvariadic)
5139  {
5140  value = PG_GETARG_DATUM(arg);
5141  isNull = PG_ARGISNULL(arg);
5142  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5143  }
5144  else
5145  {
5146  value = elements[arg - 1];
5147  isNull = nulls[arg - 1];
5148  typid = element_type;
5149  }
5150  if (!OidIsValid(typid))
5151  elog(ERROR, "could not determine data type of format() input");
5152 
5153  arg++;
5154 
5155  /* We can treat NULL width the same as zero */
5156  if (isNull)
5157  width = 0;
5158  else if (typid == INT4OID)
5159  width = DatumGetInt32(value);
5160  else if (typid == INT2OID)
5161  width = DatumGetInt16(value);
5162  else
5163  {
5164  /* For less-usual datatypes, convert to text then to int */
5165  char *str;
5166 
5167  if (typid != prev_width_type)
5168  {
5169  Oid typoutputfunc;
5170  bool typIsVarlena;
5171 
5172  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5173  fmgr_info(typoutputfunc, &typoutputinfo_width);
5174  prev_width_type = typid;
5175  }
5176 
5177  str = OutputFunctionCall(&typoutputinfo_width, value);
5178 
5179  /* pg_atoi will complain about bad data or overflow */
5180  width = pg_atoi(str, sizeof(int), '\0');
5181 
5182  pfree(str);
5183  }
5184  }
5185 
5186  /* Collect the specified or next argument position */
5187  if (argpos > 0)
5188  arg = argpos;
5189  if (arg >= nargs)
5190  ereport(ERROR,
5191  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5192  errmsg("too few arguments for format()")));
5193 
5194  /* Get the value and type of the selected argument */
5195  if (!funcvariadic)
5196  {
5197  value = PG_GETARG_DATUM(arg);
5198  isNull = PG_ARGISNULL(arg);
5199  typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5200  }
5201  else
5202  {
5203  value = elements[arg - 1];
5204  isNull = nulls[arg - 1];
5205  typid = element_type;
5206  }
5207  if (!OidIsValid(typid))
5208  elog(ERROR, "could not determine data type of format() input");
5209 
5210  arg++;
5211 
5212  /*
5213  * Get the appropriate typOutput function, reusing previous one if
5214  * same type as previous argument. That's particularly useful in the
5215  * variadic-array case, but often saves work even for ordinary calls.
5216  */
5217  if (typid != prev_type)
5218  {
5219  Oid typoutputfunc;
5220  bool typIsVarlena;
5221 
5222  getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5223  fmgr_info(typoutputfunc, &typoutputfinfo);
5224  prev_type = typid;
5225  }
5226 
5227  /*
5228  * And now we can format the value.
5229  */
5230  switch (*cp)
5231  {
5232  case 's':
5233  case 'I':
5234  case 'L':
5235  text_format_string_conversion(&str, *cp, &typoutputfinfo,
5236  value, isNull,
5237  flags, width);
5238  break;
5239  default:
5240  /* should not get here, because of previous check */
5241  ereport(ERROR,
5242  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5243  errmsg("unrecognized format() type specifier \"%c\"",
5244  *cp),
5245  errhint("For a single \"%%\" use \"%%%%\".")));
5246  break;
5247  }
5248  }
5249 
5250  /* Don't need deconstruct_array results anymore. */
5251  if (elements != NULL)
5252  pfree(elements);
5253  if (nulls != NULL)
5254  pfree(nulls);
5255 
5256  /* Generate results. */
5257  result = cstring_to_text_with_len(str.data, str.len);
5258  pfree(str.data);
5259 
5260  PG_RETURN_TEXT_P(result);
5261 }
signed short int16
Definition: c.h:245
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4984
Definition: fmgr.h:56
int errhint(const char *fmt,...)
Definition: elog.c:987
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define DatumGetInt32(X)
Definition: postgres.h:478
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition: varlena.c:5400
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define INT4OID
Definition: pg_type.h:316
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2038
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1662
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:127
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:122
static struct @121 value
#define INT2OID
Definition: pg_type.h:308
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define DatumGetInt16(X)
Definition: postgres.h:450
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:681
#define PG_NARGS()
Definition: fmgr.h:168
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2557
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Definition: c.h:433
#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:5323
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:305
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 5449 of file varlena.c.

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

Referenced by text_format_string_conversion().

5451 {
5452  bool align_to_left = false;
5453  int len;
5454 
5455  /* fast path for typical easy case */
5456  if (width == 0)
5457  {
5458  appendStringInfoString(buf, str);
5459  return;
5460  }
5461 
5462  if (width < 0)
5463  {
5464  /* Negative width: implicit '-' flag, then take absolute value */
5465  align_to_left = true;
5466  /* -INT_MIN is undefined */
5467  if (width <= INT_MIN)
5468  ereport(ERROR,
5469  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5470  errmsg("number is out of range")));
5471  width = -width;
5472  }
5473  else if (flags & TEXT_FORMAT_FLAG_MINUS)
5474  align_to_left = true;
5475 
5476  len = pg_mbstrlen(str);
5477  if (align_to_left)
5478  {
5479  /* left justify */
5480  appendStringInfoString(buf, str);
5481  if (len < width)
5482  appendStringInfoSpaces(buf, width - len);
5483  }
5484  else
5485  {
5486  /* right justify */
5487  if (len < width)
5488  appendStringInfoSpaces(buf, width - len);
5489  appendStringInfoString(buf, str);
5490  }
5491 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:187
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:774
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4982
Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 5501 of file varlena.c.

References text_format().

5502 {
5503  return text_format(fcinfo);
5504 }
Datum text_format(PG_FUNCTION_ARGS)
Definition: varlena.c:4997
static bool text_format_parse_digits ( const char **  ptr,
const char *  end_ptr,
int *  value 
)
static

Definition at line 5274 of file varlena.c.

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

Referenced by text_format_parse_format().

5275 {
5276  bool found = false;
5277  const char *cp = *ptr;
5278  int val = 0;
5279 
5280  while (*cp >= '0' && *cp <= '9')
5281  {
5282  int newval = val * 10 + (*cp - '0');
5283 
5284  if (newval / 10 != val) /* overflow? */
5285  ereport(ERROR,
5286  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5287  errmsg("number is out of range")));
5288  val = newval;
5289  ADVANCE_PARSE_POINTER(cp, end_ptr);
5290  found = true;
5291  }
5292 
5293  *ptr = cp;
5294  *value = val;
5295 
5296  return found;
5297 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4984
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
static struct @121 value
#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 5323 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().

5326 {
5327  const char *cp = start_ptr;
5328  int n;
5329 
5330  /* set defaults for output parameters */
5331  *argpos = -1;
5332  *widthpos = -1;
5333  *flags = 0;
5334  *width = 0;
5335 
5336  /* try to identify first number */
5337  if (text_format_parse_digits(&cp, end_ptr, &n))
5338  {
5339  if (*cp != '$')
5340  {
5341  /* Must be just a width and a type, so we're done */
5342  *width = n;
5343  return cp;
5344  }
5345  /* The number was argument position */
5346  *argpos = n;
5347  /* Explicit 0 for argument index is immediately refused */
5348  if (n == 0)
5349  ereport(ERROR,
5350  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5351  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5352  ADVANCE_PARSE_POINTER(cp, end_ptr);
5353  }
5354 
5355  /* Handle flags (only minus is supported now) */
5356  while (*cp == '-')
5357  {
5358  *flags |= TEXT_FORMAT_FLAG_MINUS;
5359  ADVANCE_PARSE_POINTER(cp, end_ptr);
5360  }
5361 
5362  if (*cp == '*')
5363  {
5364  /* Handle indirect width */
5365  ADVANCE_PARSE_POINTER(cp, end_ptr);
5366  if (text_format_parse_digits(&cp, end_ptr, &n))
5367  {
5368  /* number in this position must be closed by $ */
5369  if (*cp != '$')
5370  ereport(ERROR,
5371  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5372  errmsg("width argument position must be ended by \"$\"")));
5373  /* The number was width argument position */
5374  *widthpos = n;
5375  /* Explicit 0 for argument index is immediately refused */
5376  if (n == 0)
5377  ereport(ERROR,
5378  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5379  errmsg("format specifies argument 0, but arguments are numbered from 1")));
5380  ADVANCE_PARSE_POINTER(cp, end_ptr);
5381  }
5382  else
5383  *widthpos = 0; /* width's argument position is unspecified */
5384  }
5385  else
5386  {
5387  /* Check for direct width specification */
5388  if (text_format_parse_digits(&cp, end_ptr, &n))
5389  *width = n;
5390  }
5391 
5392  /* cp should now be pointing at type character */
5393  return cp;
5394 }
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition: varlena.c:4984
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:5274
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TEXT_FORMAT_FLAG_MINUS
Definition: varlena.c:4982
static void text_format_string_conversion ( StringInfo  buf,
char  conversion,
FmgrInfo typOutputInfo,
Datum  value,
bool  isNull,
int  flags,
int  width 
)
static

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

5404 {
5405  char *str;
5406 
5407  /* Handle NULL arguments before trying to stringify the value. */
5408  if (isNull)
5409  {
5410  if (conversion == 's')
5411  text_format_append_string(buf, "", flags, width);
5412  else if (conversion == 'L')
5413  text_format_append_string(buf, "NULL", flags, width);
5414  else if (conversion == 'I')
5415  ereport(ERROR,
5416  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5417  errmsg("null values cannot be formatted as an SQL identifier")));
5418  return;
5419  }
5420 
5421  /* Stringify. */
5422  str = OutputFunctionCall(typOutputInfo, value);
5423 
5424  /* Escape. */
5425  if (conversion == 'I')
5426  {
5427  /* quote_identifier may or may not allocate a new string. */
5428  text_format_append_string(buf, quote_identifier(str), flags, width);
5429  }
5430  else if (conversion == 'L')
5431  {
5432  char *qstr = quote_literal_cstr(str);
5433 
5434  text_format_append_string(buf, qstr, flags, width);
5435  /* quote_literal_cstr() always allocates a new string */
5436  pfree(qstr);
5437  }
5438  else
5439  text_format_append_string(buf, str, flags, width);
5440 
5441  /* Cleanup. */
5442  pfree(str);
5443 }
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10390
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:5449
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1662
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
static struct @121 value
#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 1751 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().

1752 {
1753  text *arg1 = PG_GETARG_TEXT_PP(0);
1754  text *arg2 = PG_GETARG_TEXT_PP(1);
1755  bool result;
1756 
1757  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1758 
1759  PG_FREE_IF_COPY(arg1, 0);
1760  PG_FREE_IF_COPY(arg2, 1);
1761 
1762  PG_RETURN_BOOL(result);
1763 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_gt ( PG_FUNCTION_ARGS  )

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

1737 {
1738  text *arg1 = PG_GETARG_TEXT_PP(0);
1739  text *arg2 = PG_GETARG_TEXT_PP(1);
1740  bool result;
1741 
1742  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1743 
1744  PG_FREE_IF_COPY(arg1, 0);
1745  PG_FREE_IF_COPY(arg2, 1);
1746 
1747  PG_RETURN_BOOL(result);
1748 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
static bool text_isequal ( text txt1,
text txt2 
)
static

Definition at line 4144 of file varlena.c.

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

Referenced by text_to_array_internal().

4145 {
4147  PointerGetDatum(txt1),
4148  PointerGetDatum(txt2)));
4149 }
#define PointerGetDatum(X)
Definition: postgres.h:562
Datum texteq(PG_FUNCTION_ARGS)
Definition: varlena.c:1642
#define DatumGetBool(X)
Definition: postgres.h:399
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2591 of file varlena.c.

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

2592 {
2593  text *arg1 = PG_GETARG_TEXT_PP(0);
2594  text *arg2 = PG_GETARG_TEXT_PP(1);
2595  text *result;
2596 
2597  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2598 
2599  PG_RETURN_TEXT_P(result);
2600 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
Datum text_le ( PG_FUNCTION_ARGS  )

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

1722 {
1723  text *arg1 = PG_GETARG_TEXT_PP(0);
1724  text *arg2 = PG_GETARG_TEXT_PP(1);
1725  bool result;
1726 
1727  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1728 
1729  PG_FREE_IF_COPY(arg1, 0);
1730  PG_FREE_IF_COPY(arg2, 1);
1731 
1732  PG_RETURN_BOOL(result);
1733 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_left ( PG_FUNCTION_ARGS  )

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

4902 {
4903  text *str = PG_GETARG_TEXT_PP(0);
4904  const char *p = VARDATA_ANY(str);
4905  int len = VARSIZE_ANY_EXHDR(str);
4906  int n = PG_GETARG_INT32(1);
4907  int rlen;
4908 
4909  if (n < 0)
4910  n = pg_mbstrlen_with_len(p, len) + n;
4911  rlen = pg_mbcharcliplen(p, len, n);
4912 
4914 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
static int32 text_length ( Datum  str)
static

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

641 {
642  /* fastpath when max encoding length is one */
645  else
646  {
647  text *t = DatumGetTextPP(str);
648 
650  VARSIZE_ANY_EXHDR(t)));
651  }
652 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARHDRSZ
Definition: c.h:439
#define DatumGetTextPP(X)
Definition: fmgr.h:256
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
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:340
Definition: c.h:433
Datum text_lt ( PG_FUNCTION_ARGS  )

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

1707 {
1708  text *arg1 = PG_GETARG_TEXT_PP(0);
1709  text *arg2 = PG_GETARG_TEXT_PP(1);
1710  bool result;
1711 
1712  result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1713 
1714  PG_FREE_IF_COPY(arg1, 0);
1715  PG_FREE_IF_COPY(arg2, 1);
1716 
1717  PG_RETURN_BOOL(result);
1718 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 3166 of file varlena.c.

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

3167 {
3168  text *s = PG_GETARG_TEXT_PP(0);
3169  Name result;
3170  int len;
3171 
3172  len = VARSIZE_ANY_EXHDR(s);
3173 
3174  /* Truncate oversize input */
3175  if (len >= NAMEDATALEN)
3176  len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3177 
3178  /* We use palloc0 here to ensure result is zero-padded */
3179  result = (Name) palloc0(NAMEDATALEN);
3180  memcpy(NameStr(*result), VARDATA_ANY(s), len);
3181 
3182  PG_RETURN_NAME(result);
3183 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:820
Definition: c.h:487
void * palloc0(Size size)
Definition: mcxt.c:877
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
#define NameStr(name)
Definition: c.h:493
Definition: c.h:433
NameData * Name
Definition: c.h:491
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
static text * text_overlay ( text t1,
text t2,
int  sp,
int  sl 
)
static

Definition at line 1034 of file varlena.c.

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

Referenced by textoverlay(), and textoverlay_no_len().

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

Definition at line 2678 of file varlena.c.

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

2679 {
2680  text *arg1 = PG_GETARG_TEXT_PP(0);
2681  text *arg2 = PG_GETARG_TEXT_PP(1);
2682  int result;
2683 
2684  result = internal_text_pattern_compare(arg1, arg2);
2685 
2686  PG_FREE_IF_COPY(arg1, 0);
2687  PG_FREE_IF_COPY(arg2, 1);
2688 
2689  PG_RETURN_BOOL(result >= 0);
2690 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2624
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 2694 of file varlena.c.

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

2695 {
2696  text *arg1 = PG_GETARG_TEXT_PP(0);
2697  text *arg2 = PG_GETARG_TEXT_PP(1);
2698  int result;
2699 
2700  result = internal_text_pattern_compare(arg1, arg2);
2701 
2702  PG_FREE_IF_COPY(arg1, 0);
2703  PG_FREE_IF_COPY(arg2, 1);
2704 
2705  PG_RETURN_BOOL(result > 0);
2706 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2624
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 2662 of file varlena.c.

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

2663 {
2664  text *arg1 = PG_GETARG_TEXT_PP(0);
2665  text *arg2 = PG_GETARG_TEXT_PP(1);
2666  int result;
2667 
2668  result = internal_text_pattern_compare(arg1, arg2);
2669 
2670  PG_FREE_IF_COPY(arg1, 0);
2671  PG_FREE_IF_COPY(arg2, 1);
2672 
2673  PG_RETURN_BOOL(result <= 0);
2674 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2624
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 2646 of file varlena.c.

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

2647 {
2648  text *arg1 = PG_GETARG_TEXT_PP(0);
2649  text *arg2 = PG_GETARG_TEXT_PP(1);
2650  int result;
2651 
2652  result = internal_text_pattern_compare(arg1, arg2);
2653 
2654  PG_FREE_IF_COPY(arg1, 0);
2655  PG_FREE_IF_COPY(arg2, 1);
2656 
2657  PG_RETURN_BOOL(result < 0);
2658 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition: varlena.c:2624
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
static int text_position ( text t1,
text t2 
)
static

Definition at line 1095 of file varlena.c.

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

Referenced by textpos().

1096 {
1098  int result;
1099 
1100  text_position_setup(t1, t2, &state);
1101  result = text_position_next(1, &state);
1102  text_position_cleanup(&state);
1103  return result;
1104 }
static void text_position_setup(text *t1, text *t2, TextPositionState *state)
Definition: varlena.c:1119
static int text_position_next(int start_pos, TextPositionState *state)
Definition: varlena.c:1231
static void text_position_cleanup(TextPositionState *state)
Definition: varlena.c:1365
static void text_position_cleanup ( TextPositionState state)
static

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

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

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

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

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

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

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

4943 {
4944  text *str = PG_GETARG_TEXT_PP(0);
4945  const char *p = VARDATA_ANY(str);
4946  int len = VARSIZE_ANY_EXHDR(str);
4947  const char *endp = p + len;
4948  text *result;
4949  char *dst;
4950 
4951  result = palloc(len + VARHDRSZ);
4952  dst = (char *) VARDATA(result) + len;
4953  SET_VARSIZE(result, len + VARHDRSZ);
4954 
4956  {
4957  /* multibyte version */
4958  while (p < endp)
4959  {
4960  int sz;
4961 
4962  sz = pg_mblen(p);
4963  dst -= sz;
4964  memcpy(dst, p, sz);
4965  p += sz;
4966  }
4967  }
4968  else
4969  {
4970  /* single byte version */
4971  while (p < endp)
4972  *(--dst) = *p++;
4973  }
4974 
4975  PG_RETURN_TEXT_P(result);
4976 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum text_right ( PG_FUNCTION_ARGS  )

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

4922 {
4923  text *str = PG_GETARG_TEXT_PP(0);
4924  const char *p = VARDATA_ANY(str);
4925  int len = VARSIZE_ANY_EXHDR(str);
4926  int n = PG_GETARG_INT32(1);
4927  int off;
4928 
4929  if (n < 0)
4930  n = -n;
4931  else
4932  n = pg_mbstrlen_with_len(p, len) - n;
4933  off = pg_mbcharcliplen(p, len, n);
4934 
4935  PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
4936 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2603 of file varlena.c.

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

2604 {
2605  text *arg1 = PG_GETARG_TEXT_PP(0);
2606  text *arg2 = PG_GETARG_TEXT_PP(1);
2607  text *result;
2608 
2609  result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2610 
2611  PG_RETURN_TEXT_P(result);
2612 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition: varlena.c:1617
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
Datum text_substr ( PG_FUNCTION_ARGS  )

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

782 {
784  PG_GETARG_INT32(1),
785  PG_GETARG_INT32(2),
786  false));
787 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 795 of file varlena.c.

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

Referenced by build_regexp_split_result().

796 {
798  PG_GETARG_INT32(1),
799  -1, true));
800 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition: varlena.c:814
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
static text * text_substring ( Datum  str,
int32  start,
int32  length,
bool  length_not_specified 
)
static

Definition at line 814 of file varlena.c.

References cstring_to_text(), DatumGetPointer, DatumGetTextPSlice, elog, ereport, errcode(), errmsg(), ERROR, i, length(), Max, Min, 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().

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

Definition at line 4157 of file varlena.c.

References text_to_array_internal().

4158 {
4159  return text_to_array_internal(fcinfo);
4160 }
static Datum text_to_array_internal(PG_FUNCTION_ARGS)
Definition: varlena.c:4182
static Datum text_to_array_internal ( PG_FUNCTION_ARGS  )
static

Definition at line 4182 of file varlena.c.

References accumArrayResult(), charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, construct_empty_array(), construct_md_array(), cstring_to_text_with_len(), CurrentMemoryContext, TextPositionState::len1, TextPositionState::len2, makeArrayResult(), 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().

4183 {
4184  text *inputstring;
4185  text *fldsep;
4186  text *null_string;
4187  int inputstring_len;
4188  int fldsep_len;
4189  char *start_ptr;
4190  text *result_text;
4191  bool is_null;
4192  ArrayBuildState *astate = NULL;
4193 
4194  /* when input string is NULL, then result is NULL too */
4195  if (PG_ARGISNULL(0))
4196  PG_RETURN_NULL();
4197 
4198  inputstring = PG_GETARG_TEXT_PP(0);
4199 
4200  /* fldsep can be NULL */
4201  if (!PG_ARGISNULL(1))
4202  fldsep = PG_GETARG_TEXT_PP(1);
4203  else
4204  fldsep = NULL;
4205 
4206  /* null_string can be NULL or omitted */
4207  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4208  null_string = PG_GETARG_TEXT_PP(2);
4209  else
4210  null_string = NULL;
4211 
4212  if (fldsep != NULL)
4213  {
4214  /*
4215  * Normal case with non-null fldsep. Use the text_position machinery
4216  * to search for occurrences of fldsep.
4217  */
4219  int fldnum;
4220  int start_posn;
4221  int end_posn;
4222  int chunk_len;
4223 
4224  text_position_setup(inputstring, fldsep, &state);
4225 
4226  /*
4227  * Note: we check the converted string length, not the original,
4228  * because they could be different if the input contained invalid
4229  * encoding.
4230  */
4231  inputstring_len = state.len1;
4232  fldsep_len = state.len2;
4233 
4234  /* return empty array for empty input string */