PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
varchar.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/tuptoaster.h"
#include "catalog/pg_collation.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
#include "mb/pg_wchar.h"
Include dependency graph for varchar.c:

Go to the source code of this file.

Functions

static int32 anychar_typmodin (ArrayType *ta, const char *typename)
 
static char * anychar_typmodout (int32 typmod)
 
static BpCharbpchar_input (const char *s, size_t len, int32 atttypmod)
 
Datum bpcharin (PG_FUNCTION_ARGS)
 
Datum bpcharout (PG_FUNCTION_ARGS)
 
Datum bpcharrecv (PG_FUNCTION_ARGS)
 
Datum bpcharsend (PG_FUNCTION_ARGS)
 
Datum bpchar (PG_FUNCTION_ARGS)
 
Datum char_bpchar (PG_FUNCTION_ARGS)
 
Datum bpchar_name (PG_FUNCTION_ARGS)
 
Datum name_bpchar (PG_FUNCTION_ARGS)
 
Datum bpchartypmodin (PG_FUNCTION_ARGS)
 
Datum bpchartypmodout (PG_FUNCTION_ARGS)
 
static VarCharvarchar_input (const char *s, size_t len, int32 atttypmod)
 
Datum varcharin (PG_FUNCTION_ARGS)
 
Datum varcharout (PG_FUNCTION_ARGS)
 
Datum varcharrecv (PG_FUNCTION_ARGS)
 
Datum varcharsend (PG_FUNCTION_ARGS)
 
Datum varchar_transform (PG_FUNCTION_ARGS)
 
Datum varchar (PG_FUNCTION_ARGS)
 
Datum varchartypmodin (PG_FUNCTION_ARGS)
 
Datum varchartypmodout (PG_FUNCTION_ARGS)
 
static int bcTruelen (BpChar *arg)
 
int bpchartruelen (char *s, int len)
 
Datum bpcharlen (PG_FUNCTION_ARGS)
 
Datum bpcharoctetlen (PG_FUNCTION_ARGS)
 
Datum bpchareq (PG_FUNCTION_ARGS)
 
Datum bpcharne (PG_FUNCTION_ARGS)
 
Datum bpcharlt (PG_FUNCTION_ARGS)
 
Datum bpcharle (PG_FUNCTION_ARGS)
 
Datum bpchargt (PG_FUNCTION_ARGS)
 
Datum bpcharge (PG_FUNCTION_ARGS)
 
Datum bpcharcmp (PG_FUNCTION_ARGS)
 
Datum bpchar_sortsupport (PG_FUNCTION_ARGS)
 
Datum bpchar_larger (PG_FUNCTION_ARGS)
 
Datum bpchar_smaller (PG_FUNCTION_ARGS)
 
Datum hashbpchar (PG_FUNCTION_ARGS)
 
static int internal_bpchar_pattern_compare (BpChar *arg1, BpChar *arg2)
 
Datum bpchar_pattern_lt (PG_FUNCTION_ARGS)
 
Datum bpchar_pattern_le (PG_FUNCTION_ARGS)
 
Datum bpchar_pattern_ge (PG_FUNCTION_ARGS)
 
Datum bpchar_pattern_gt (PG_FUNCTION_ARGS)
 
Datum btbpchar_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum btbpchar_pattern_sortsupport (PG_FUNCTION_ARGS)
 

Function Documentation

static int32 anychar_typmodin ( ArrayType ta,
const char *  typename 
)
static

Definition at line 31 of file varchar.c.

References ArrayGetIntegerTypmods(), ereport, errcode(), errmsg(), ERROR, MaxAttrSize, and VARHDRSZ.

Referenced by bpchartypmodin(), and varchartypmodin().

32 {
33  int32 typmod;
34  int32 *tl;
35  int n;
36 
37  tl = ArrayGetIntegerTypmods(ta, &n);
38 
39  /*
40  * we're not too tense about good error message here because grammar
41  * shouldn't allow wrong number of modifiers for CHAR
42  */
43  if (n != 1)
44  ereport(ERROR,
45  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
46  errmsg("invalid type modifier")));
47 
48  if (*tl < 1)
49  ereport(ERROR,
50  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
51  errmsg("length for type %s must be at least 1", typename)));
52  if (*tl > MaxAttrSize)
53  ereport(ERROR,
54  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55  errmsg("length for type %s cannot exceed %d",
56  typename, MaxAttrSize)));
57 
58  /*
59  * For largely historical reasons, the typmod is VARHDRSZ plus the number
60  * of characters; there is enough client-side code that knows about that
61  * that we'd better not change it.
62  */
63  typmod = VARHDRSZ + *tl;
64 
65  return typmod;
66 }
#define MaxAttrSize
Definition: htup_details.h:586
#define VARHDRSZ
Definition: c.h:445
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char* anychar_typmodout ( int32  typmod)
static

Definition at line 70 of file varchar.c.

References palloc(), snprintf(), and VARHDRSZ.

Referenced by bpchartypmodout(), and varchartypmodout().

71 {
72  char *res = (char *) palloc(64);
73 
74  if (typmod > VARHDRSZ)
75  snprintf(res, 64, "(%d)", (int) (typmod - VARHDRSZ));
76  else
77  *res = '\0';
78 
79  return res;
80 }
#define VARHDRSZ
Definition: c.h:445
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void * palloc(Size size)
Definition: mcxt.c:849
static int bcTruelen ( BpChar arg)
inlinestatic

Definition at line 654 of file varchar.c.

References bpchartruelen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by bpchar_larger(), bpchar_smaller(), bpcharcmp(), bpchareq(), bpcharge(), bpchargt(), bpcharle(), bpcharlen(), bpcharlt(), bpcharne(), hashbpchar(), and internal_bpchar_pattern_compare().

655 {
656  return bpchartruelen(VARDATA_ANY(arg), VARSIZE_ANY_EXHDR(arg));
657 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int bpchartruelen(char *s, int len)
Definition: varchar.c:660
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Datum bpchar ( PG_FUNCTION_ARGS  )

Definition at line 267 of file varchar.c.

References Assert, ereport, errcode(), errmsg(), ERROR, i, palloc(), PG_GETARG_BOOL, PG_GETARG_BPCHAR_PP, PG_GETARG_INT32, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_BPCHAR_P, result, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by varstr_sortsupport().

268 {
269  BpChar *source = PG_GETARG_BPCHAR_PP(0);
270  int32 maxlen = PG_GETARG_INT32(1);
271  bool isExplicit = PG_GETARG_BOOL(2);
272  BpChar *result;
273  int32 len;
274  char *r;
275  char *s;
276  int i;
277  int charlen; /* number of characters in the input string +
278  * VARHDRSZ */
279 
280  /* No work if typmod is invalid */
281  if (maxlen < (int32) VARHDRSZ)
282  PG_RETURN_BPCHAR_P(source);
283 
284  maxlen -= VARHDRSZ;
285 
286  len = VARSIZE_ANY_EXHDR(source);
287  s = VARDATA_ANY(source);
288 
289  charlen = pg_mbstrlen_with_len(s, len);
290 
291  /* No work if supplied data matches typmod already */
292  if (charlen == maxlen)
293  PG_RETURN_BPCHAR_P(source);
294 
295  if (charlen > maxlen)
296  {
297  /* Verify that extra characters are spaces, and clip them off */
298  size_t maxmblen;
299 
300  maxmblen = pg_mbcharcliplen(s, len, maxlen);
301 
302  if (!isExplicit)
303  {
304  for (i = maxmblen; i < len; i++)
305  if (s[i] != ' ')
306  ereport(ERROR,
307  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
308  errmsg("value too long for type character(%d)",
309  maxlen)));
310  }
311 
312  len = maxmblen;
313 
314  /*
315  * At this point, maxlen is the necessary byte length, not the number
316  * of CHARACTERS!
317  */
318  maxlen = len;
319  }
320  else
321  {
322  /*
323  * At this point, maxlen is the necessary byte length, not the number
324  * of CHARACTERS!
325  */
326  maxlen = len + (maxlen - charlen);
327  }
328 
329  Assert(maxlen >= len);
330 
331  result = palloc(maxlen + VARHDRSZ);
332  SET_VARSIZE(result, maxlen + VARHDRSZ);
333  r = VARDATA(result);
334 
335  memcpy(r, s, len);
336 
337  /* blank pad the string if necessary */
338  if (maxlen > len)
339  memset(r + len, ' ', maxlen - len);
340 
341  PG_RETURN_BPCHAR_P(result);
342 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
signed int int32
Definition: c.h:256
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define ERROR
Definition: elog.h:43
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
#define ereport(elevel, rest)
Definition: elog.h:122
#define Assert(condition)
Definition: c.h:675
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static BpChar* bpchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
)
static

Definition at line 124 of file varchar.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), pg_mbcharcliplen(), pg_mbstrlen_with_len(), result, SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by bpcharin(), and bpcharrecv().

125 {
126  BpChar *result;
127  char *r;
128  size_t maxlen;
129 
130  /* If typmod is -1 (or invalid), use the actual string length */
131  if (atttypmod < (int32) VARHDRSZ)
132  maxlen = len;
133  else
134  {
135  size_t charlen; /* number of CHARACTERS in the input */
136 
137  maxlen = atttypmod - VARHDRSZ;
138  charlen = pg_mbstrlen_with_len(s, len);
139  if (charlen > maxlen)
140  {
141  /* Verify that extra characters are spaces, and clip them off */
142  size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
143  size_t j;
144 
145  /*
146  * at this point, len is the actual BYTE length of the input
147  * string, maxlen is the max number of CHARACTERS allowed for this
148  * bpchar type, mbmaxlen is the length in BYTES of those chars.
149  */
150  for (j = mbmaxlen; j < len; j++)
151  {
152  if (s[j] != ' ')
153  ereport(ERROR,
154  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
155  errmsg("value too long for type character(%d)",
156  (int) maxlen)));
157  }
158 
159  /*
160  * Now we set maxlen to the necessary byte length, not the number
161  * of CHARACTERS!
162  */
163  maxlen = len = mbmaxlen;
164  }
165  else
166  {
167  /*
168  * Now we set maxlen to the necessary byte length, not the number
169  * of CHARACTERS!
170  */
171  maxlen = len + (maxlen - charlen);
172  }
173  }
174 
175  result = (BpChar *) palloc(maxlen + VARHDRSZ);
176  SET_VARSIZE(result, maxlen + VARHDRSZ);
177  r = VARDATA(result);
178  memcpy(r, s, len);
179 
180  /* blank pad the string if necessary */
181  if (maxlen > len)
182  memset(r + len, ' ', maxlen - len);
183 
184  return result;
185 }
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
signed int int32
Definition: c.h:256
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum bpchar_larger ( PG_FUNCTION_ARGS  )

Definition at line 887 of file varchar.c.

References bcTruelen(), cmp(), PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BPCHAR_P, VARDATA_ANY, and varstr_cmp().

888 {
889  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
890  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
891  int len1,
892  len2;
893  int cmp;
894 
895  len1 = bcTruelen(arg1);
896  len2 = bcTruelen(arg2);
897 
898  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
899  PG_GET_COLLATION());
900 
901  PG_RETURN_BPCHAR_P((cmp >= 0) ? arg1 : arg2);
902 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpchar_name ( PG_FUNCTION_ARGS  )

Definition at line 367 of file varchar.c.

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

368 {
369  BpChar *s = PG_GETARG_BPCHAR_PP(0);
370  char *s_data;
371  Name result;
372  int len;
373 
374  len = VARSIZE_ANY_EXHDR(s);
375  s_data = VARDATA_ANY(s);
376 
377  /* Truncate oversize input */
378  if (len >= NAMEDATALEN)
379  len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);
380 
381  /* Remove trailing blanks */
382  while (len > 0)
383  {
384  if (s_data[len - 1] != ' ')
385  break;
386  len--;
387  }
388 
389  /* We use palloc0 here to ensure result is zero-padded */
390  result = (Name) palloc0(NAMEDATALEN);
391  memcpy(NameStr(*result), s_data, len);
392 
393  PG_RETURN_NAME(result);
394 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
return result
Definition: formatting.c:1633
#define NAMEDATALEN
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
Definition: c.h:493
void * palloc0(Size size)
Definition: mcxt.c:878
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
NameData * Name
Definition: c.h:497
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
Datum bpchar_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 1014 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and result.

1015 {
1016  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1017  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1018  int result;
1019 
1020  result = internal_bpchar_pattern_compare(arg1, arg2);
1021 
1022  PG_FREE_IF_COPY(arg1, 0);
1023  PG_FREE_IF_COPY(arg2, 1);
1024 
1025  PG_RETURN_BOOL(result >= 0);
1026 }
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:960
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpchar_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 1030 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and result.

1031 {
1032  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1033  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1034  int result;
1035 
1036  result = internal_bpchar_pattern_compare(arg1, arg2);
1037 
1038  PG_FREE_IF_COPY(arg1, 0);
1039  PG_FREE_IF_COPY(arg2, 1);
1040 
1041  PG_RETURN_BOOL(result > 0);
1042 }
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:960
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpchar_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 998 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and result.

999 {
1000  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1001  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1002  int result;
1003 
1004  result = internal_bpchar_pattern_compare(arg1, arg2);
1005 
1006  PG_FREE_IF_COPY(arg1, 0);
1007  PG_FREE_IF_COPY(arg2, 1);
1008 
1009  PG_RETURN_BOOL(result <= 0);
1010 }
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:960
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpchar_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 982 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and result.

983 {
984  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
985  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
986  int result;
987 
988  result = internal_bpchar_pattern_compare(arg1, arg2);
989 
990  PG_FREE_IF_COPY(arg1, 0);
991  PG_FREE_IF_COPY(arg2, 1);
992 
993  PG_RETURN_BOOL(result < 0);
994 }
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:960
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpchar_smaller ( PG_FUNCTION_ARGS  )

Definition at line 905 of file varchar.c.

References bcTruelen(), cmp(), PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BPCHAR_P, VARDATA_ANY, and varstr_cmp().

906 {
907  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
908  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
909  int len1,
910  len2;
911  int cmp;
912 
913  len1 = bcTruelen(arg1);
914  len2 = bcTruelen(arg2);
915 
916  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
917  PG_GET_COLLATION());
918 
919  PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2);
920 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpchar_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 870 of file varchar.c.

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

871 {
873  Oid collid = ssup->ssup_collation;
874  MemoryContext oldcontext;
875 
876  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
877 
878  /* Use generic string SortSupport */
879  varstr_sortsupport(ssup, collid, true);
880 
881  MemoryContextSwitchTo(oldcontext);
882 
883  PG_RETURN_VOID();
884 }
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
Datum bpcharcmp ( PG_FUNCTION_ARGS  )

Definition at line 849 of file varchar.c.

References bcTruelen(), cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_INT32, VARDATA_ANY, and varstr_cmp().

850 {
851  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
852  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
853  int len1,
854  len2;
855  int cmp;
856 
857  len1 = bcTruelen(arg1);
858  len2 = bcTruelen(arg2);
859 
860  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
861  PG_GET_COLLATION());
862 
863  PG_FREE_IF_COPY(arg1, 0);
864  PG_FREE_IF_COPY(arg2, 1);
865 
866  PG_RETURN_INT32(cmp);
867 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpchareq ( PG_FUNCTION_ARGS  )

Definition at line 711 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, result, and VARDATA_ANY.

712 {
713  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
714  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
715  int len1,
716  len2;
717  bool result;
718 
719  len1 = bcTruelen(arg1);
720  len2 = bcTruelen(arg2);
721 
722  /*
723  * Since we only care about equality or not-equality, we can avoid all the
724  * expense of strcoll() here, and just do bitwise comparison.
725  */
726  if (len1 != len2)
727  result = false;
728  else
729  result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) == 0);
730 
731  PG_FREE_IF_COPY(arg1, 0);
732  PG_FREE_IF_COPY(arg2, 1);
733 
734  PG_RETURN_BOOL(result);
735 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpcharge ( PG_FUNCTION_ARGS  )

Definition at line 828 of file varchar.c.

References bcTruelen(), cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

829 {
830  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
831  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
832  int len1,
833  len2;
834  int cmp;
835 
836  len1 = bcTruelen(arg1);
837  len2 = bcTruelen(arg2);
838 
839  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
840  PG_GET_COLLATION());
841 
842  PG_FREE_IF_COPY(arg1, 0);
843  PG_FREE_IF_COPY(arg2, 1);
844 
845  PG_RETURN_BOOL(cmp >= 0);
846 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpchargt ( PG_FUNCTION_ARGS  )

Definition at line 807 of file varchar.c.

References bcTruelen(), cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

808 {
809  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
810  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
811  int len1,
812  len2;
813  int cmp;
814 
815  len1 = bcTruelen(arg1);
816  len2 = bcTruelen(arg2);
817 
818  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
819  PG_GET_COLLATION());
820 
821  PG_FREE_IF_COPY(arg1, 0);
822  PG_FREE_IF_COPY(arg2, 1);
823 
824  PG_RETURN_BOOL(cmp > 0);
825 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpcharin ( PG_FUNCTION_ARGS  )

Definition at line 192 of file varchar.c.

References bpchar_input(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_BPCHAR_P, and result.

193 {
194  char *s = PG_GETARG_CSTRING(0);
195 
196 #ifdef NOT_USED
197  Oid typelem = PG_GETARG_OID(1);
198 #endif
199  int32 atttypmod = PG_GETARG_INT32(2);
200  BpChar *result;
201 
202  result = bpchar_input(s, strlen(s), atttypmod);
203  PG_RETURN_BPCHAR_P(result);
204 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static BpChar * bpchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:124
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Definition: c.h:439
Datum bpcharle ( PG_FUNCTION_ARGS  )

Definition at line 786 of file varchar.c.

References bcTruelen(), cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

787 {
788  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
789  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
790  int len1,
791  len2;
792  int cmp;
793 
794  len1 = bcTruelen(arg1);
795  len2 = bcTruelen(arg2);
796 
797  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
798  PG_GET_COLLATION());
799 
800  PG_FREE_IF_COPY(arg1, 0);
801  PG_FREE_IF_COPY(arg2, 1);
802 
803  PG_RETURN_BOOL(cmp <= 0);
804 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpcharlen ( PG_FUNCTION_ARGS  )

Definition at line 677 of file varchar.c.

References arg, bcTruelen(), pg_database_encoding_max_length(), PG_GETARG_BPCHAR_PP, pg_mbstrlen_with_len(), PG_RETURN_INT32, and VARDATA_ANY.

678 {
680  int len;
681 
682  /* get number of bytes, ignoring trailing spaces */
683  len = bcTruelen(arg);
684 
685  /* in multibyte encoding, convert to number of characters */
687  len = pg_mbstrlen_with_len(VARDATA_ANY(arg), len);
688 
689  PG_RETURN_INT32(len);
690 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
void * arg
Definition: c.h:439
Datum bpcharlt ( PG_FUNCTION_ARGS  )

Definition at line 765 of file varchar.c.

References bcTruelen(), cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

766 {
767  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
768  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
769  int len1,
770  len2;
771  int cmp;
772 
773  len1 = bcTruelen(arg1);
774  len2 = bcTruelen(arg2);
775 
776  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
777  PG_GET_COLLATION());
778 
779  PG_FREE_IF_COPY(arg1, 0);
780  PG_FREE_IF_COPY(arg2, 1);
781 
782  PG_RETURN_BOOL(cmp < 0);
783 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Datum bpcharne ( PG_FUNCTION_ARGS  )

Definition at line 738 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, result, and VARDATA_ANY.

739 {
740  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
741  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
742  int len1,
743  len2;
744  bool result;
745 
746  len1 = bcTruelen(arg1);
747  len2 = bcTruelen(arg2);
748 
749  /*
750  * Since we only care about equality or not-equality, we can avoid all the
751  * expense of strcoll() here, and just do bitwise comparison.
752  */
753  if (len1 != len2)
754  result = true;
755  else
756  result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) != 0);
757 
758  PG_FREE_IF_COPY(arg1, 0);
759  PG_FREE_IF_COPY(arg2, 1);
760 
761  PG_RETURN_BOOL(result);
762 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum bpcharoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 693 of file varchar.c.

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

694 {
696 
697  /* We need not detoast the input at all */
699 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define VARHDRSZ
Definition: c.h:445
#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
void * arg
Datum bpcharout ( PG_FUNCTION_ARGS  )

Definition at line 214 of file varchar.c.

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

215 {
216  Datum txt = PG_GETARG_DATUM(0);
217 
219 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Datum bpcharrecv ( PG_FUNCTION_ARGS  )

Definition at line 225 of file varchar.c.

References bpchar_input(), buf, StringInfoData::cursor, StringInfoData::len, pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_BPCHAR_P, pq_getmsgtext(), and result.

226 {
228 
229 #ifdef NOT_USED
230  Oid typelem = PG_GETARG_OID(1);
231 #endif
232  int32 atttypmod = PG_GETARG_INT32(2);
233  BpChar *result;
234  char *str;
235  int nbytes;
236 
237  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
238  result = bpchar_input(str, nbytes, atttypmod);
239  pfree(str);
240  PG_RETURN_BPCHAR_P(result);
241 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
StringInfoData * StringInfo
Definition: stringinfo.h:43
static BpChar * bpchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:124
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
void pfree(void *pointer)
Definition: mcxt.c:950
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
static char * buf
Definition: pg_test_fsync.c:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:588
Definition: c.h:439
Datum bpcharsend ( PG_FUNCTION_ARGS  )

Definition at line 247 of file varchar.c.

References textsend().

248 {
249  /* Exactly the same as textsend, so share code */
250  return textsend(fcinfo);
251 }
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:548
int bpchartruelen ( char *  s,
int  len 
)

Definition at line 660 of file varchar.c.

References i.

Referenced by bcTruelen(), bpcharfastcmp_c(), varstr_abbrev_convert(), and varstrfastcmp_locale().

661 {
662  int i;
663 
664  /*
665  * Note that we rely on the assumption that ' ' is a singleton unit on
666  * every supported multibyte server encoding.
667  */
668  for (i = len - 1; i >= 0; i--)
669  {
670  if (s[i] != ' ')
671  break;
672  }
673  return i + 1;
674 }
int i
Datum bpchartypmodin ( PG_FUNCTION_ARGS  )

Definition at line 413 of file varchar.c.

References anychar_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

414 {
416 
417  PG_RETURN_INT32(anychar_typmodin(ta, "char"));
418 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
static int32 anychar_typmodin(ArrayType *ta, const char *typename)
Definition: varchar.c:31
Datum bpchartypmodout ( PG_FUNCTION_ARGS  )

Definition at line 421 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

422 {
423  int32 typmod = PG_GETARG_INT32(0);
424 
426 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:256
static char * anychar_typmodout(int32 typmod)
Definition: varchar.c:70
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Datum btbpchar_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1046 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_INT32, and result.

1047 {
1048  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1049  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1050  int result;
1051 
1052  result = internal_bpchar_pattern_compare(arg1, arg2);
1053 
1054  PG_FREE_IF_COPY(arg1, 0);
1055  PG_FREE_IF_COPY(arg2, 1);
1056 
1057  PG_RETURN_INT32(result);
1058 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:960
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
Datum btbpchar_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1062 of file varchar.c.

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

1063 {
1065  MemoryContext oldcontext;
1066 
1067  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1068 
1069  /* Use generic string SortSupport, forcing "C" collation */
1070  varstr_sortsupport(ssup, C_COLLATION_OID, true);
1071 
1072  MemoryContextSwitchTo(oldcontext);
1073 
1074  PG_RETURN_VOID();
1075 }
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 char_bpchar ( PG_FUNCTION_ARGS  )

Definition at line 349 of file varchar.c.

References palloc(), PG_GETARG_CHAR, PG_RETURN_BPCHAR_P, result, SET_VARSIZE, VARDATA, and VARHDRSZ.

350 {
351  char c = PG_GETARG_CHAR(0);
352  BpChar *result;
353 
354  result = (BpChar *) palloc(VARHDRSZ + 1);
355 
356  SET_VARSIZE(result, VARHDRSZ + 1);
357  *(VARDATA(result)) = c;
358 
359  PG_RETURN_BPCHAR_P(result);
360 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
return result
Definition: formatting.c:1633
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
char * c
void * palloc(Size size)
Definition: mcxt.c:849
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238
Datum hashbpchar ( PG_FUNCTION_ARGS  )

Definition at line 932 of file varchar.c.

References bcTruelen(), hash_any(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, result, and VARDATA_ANY.

933 {
934  BpChar *key = PG_GETARG_BPCHAR_PP(0);
935  char *keydata;
936  int keylen;
937  Datum result;
938 
939  keydata = VARDATA_ANY(key);
940  keylen = bcTruelen(key);
941 
942  result = hash_any((unsigned char *) keydata, keylen);
943 
944  /* Avoid leaking memory for toasted inputs */
945  PG_FREE_IF_COPY(key, 0);
946 
947  return result;
948 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
return result
Definition: formatting.c:1633
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:274
uintptr_t Datum
Definition: postgres.h:372
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:439
static int internal_bpchar_pattern_compare ( BpChar arg1,
BpChar arg2 
)
static

Definition at line 960 of file varchar.c.

References bcTruelen(), Min, result, and VARDATA_ANY.

Referenced by bpchar_pattern_ge(), bpchar_pattern_gt(), bpchar_pattern_le(), bpchar_pattern_lt(), and btbpchar_pattern_cmp().

961 {
962  int result;
963  int len1,
964  len2;
965 
966  len1 = bcTruelen(arg1);
967  len2 = bcTruelen(arg2);
968 
969  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
970  if (result != 0)
971  return result;
972  else if (len1 < len2)
973  return -1;
974  else if (len1 > len2)
975  return 1;
976  else
977  return 0;
978 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define Min(x, y)
Definition: c.h:806
return result
Definition: formatting.c:1633
static int bcTruelen(BpChar *arg)
Definition: varchar.c:654
Datum name_bpchar ( PG_FUNCTION_ARGS  )

Definition at line 403 of file varchar.c.

References cstring_to_text(), NameStr, PG_GETARG_NAME, PG_RETURN_BPCHAR_P, and result.

404 {
405  Name s = PG_GETARG_NAME(0);
406  BpChar *result;
407 
408  result = (BpChar *) cstring_to_text(NameStr(*s));
409  PG_RETURN_BPCHAR_P(result);
410 }
return result
Definition: formatting.c:1633
Definition: c.h:493
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum varchar ( PG_FUNCTION_ARGS  )

Definition at line 593 of file varchar.c.

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, i, PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARCHAR_PP, pg_mbcharcliplen(), PG_RETURN_VARCHAR_P, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by main().

594 {
595  VarChar *source = PG_GETARG_VARCHAR_PP(0);
596  int32 typmod = PG_GETARG_INT32(1);
597  bool isExplicit = PG_GETARG_BOOL(2);
598  int32 len,
599  maxlen;
600  size_t maxmblen;
601  int i;
602  char *s_data;
603 
604  len = VARSIZE_ANY_EXHDR(source);
605  s_data = VARDATA_ANY(source);
606  maxlen = typmod - VARHDRSZ;
607 
608  /* No work if typmod is invalid or supplied data fits it already */
609  if (maxlen < 0 || len <= maxlen)
610  PG_RETURN_VARCHAR_P(source);
611 
612  /* only reach here if string is too long... */
613 
614  /* truncate multibyte string preserving multibyte boundary */
615  maxmblen = pg_mbcharcliplen(s_data, len, maxlen);
616 
617  if (!isExplicit)
618  {
619  for (i = maxmblen; i < len; i++)
620  if (s_data[i] != ' ')
621  ereport(ERROR,
622  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
623  errmsg("value too long for type character varying(%d)",
624  maxlen)));
625  }
626 
628  maxmblen));
629 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:332
#define PG_GETARG_VARCHAR_PP(n)
Definition: fmgr.h:275
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: c.h:439
static VarChar* varchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
)
static

Definition at line 452 of file varchar.c.

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, pg_mbcharcliplen(), result, and VARHDRSZ.

Referenced by varcharin(), and varcharrecv().

453 {
454  VarChar *result;
455  size_t maxlen;
456 
457  maxlen = atttypmod - VARHDRSZ;
458 
459  if (atttypmod >= (int32) VARHDRSZ && len > maxlen)
460  {
461  /* Verify that extra characters are spaces, and clip them off */
462  size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
463  size_t j;
464 
465  for (j = mbmaxlen; j < len; j++)
466  {
467  if (s[j] != ' ')
468  ereport(ERROR,
469  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
470  errmsg("value too long for type character varying(%d)",
471  (int) maxlen)));
472  }
473 
474  len = mbmaxlen;
475  }
476 
477  result = (VarChar *) cstring_to_text_with_len(s, len);
478  return result;
479 }
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:873
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
Datum varchar_transform ( PG_FUNCTION_ARGS  )

Definition at line 555 of file varchar.c.

References FuncExpr::args, Assert, castNode, DatumGetInt32, exprTypmod(), IsA, linitial, list_length(), lsecond, NULL, PG_GETARG_POINTER, PG_RETURN_POINTER, relabel_to_typmod(), and VARHDRSZ.

556 {
558  Node *ret = NULL;
559  Node *typmod;
560 
561  Assert(list_length(expr->args) >= 2);
562 
563  typmod = (Node *) lsecond(expr->args);
564 
565  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
566  {
567  Node *source = (Node *) linitial(expr->args);
568  int32 old_typmod = exprTypmod(source);
569  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
570  int32 old_max = old_typmod - VARHDRSZ;
571  int32 new_max = new_typmod - VARHDRSZ;
572 
573  if (new_typmod < 0 || (old_typmod >= 0 && old_max <= new_max))
574  ret = relabel_to_typmod(source, new_typmod);
575  }
576 
577  PG_RETURN_POINTER(ret);
578 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * args
Definition: primnodes.h:457
#define DatumGetInt32(X)
Definition: postgres.h:478
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define VARHDRSZ
Definition: c.h:445
Definition: nodes.h:509
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define lsecond(l)
Definition: pg_list.h:116
signed int int32
Definition: c.h:256
#define linitial(l)
Definition: pg_list.h:111
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588
Datum varcharin ( PG_FUNCTION_ARGS  )

Definition at line 486 of file varchar.c.

References PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_VARCHAR_P, result, and varchar_input().

487 {
488  char *s = PG_GETARG_CSTRING(0);
489 
490 #ifdef NOT_USED
491  Oid typelem = PG_GETARG_OID(1);
492 #endif
493  int32 atttypmod = PG_GETARG_INT32(2);
494  VarChar *result;
495 
496  result = varchar_input(s, strlen(s), atttypmod);
497  PG_RETURN_VARCHAR_P(result);
498 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:452
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:332
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Definition: c.h:439
Datum varcharout ( PG_FUNCTION_ARGS  )

Definition at line 508 of file varchar.c.

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

509 {
510  Datum txt = PG_GETARG_DATUM(0);
511 
513 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Datum varcharrecv ( PG_FUNCTION_ARGS  )

Definition at line 519 of file varchar.c.

References buf, StringInfoData::cursor, StringInfoData::len, pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARCHAR_P, pq_getmsgtext(), result, and varchar_input().

520 {
522 
523 #ifdef NOT_USED
524  Oid typelem = PG_GETARG_OID(1);
525 #endif
526  int32 atttypmod = PG_GETARG_INT32(2);
527  VarChar *result;
528  char *str;
529  int nbytes;
530 
531  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
532  result = varchar_input(str, nbytes, atttypmod);
533  pfree(str);
534  PG_RETURN_VARCHAR_P(result);
535 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:452
StringInfoData * StringInfo
Definition: stringinfo.h:43
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
void pfree(void *pointer)
Definition: mcxt.c:950
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:332
static char * buf
Definition: pg_test_fsync.c:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:588
Definition: c.h:439
Datum varcharsend ( PG_FUNCTION_ARGS  )

Definition at line 541 of file varchar.c.

References textsend().

542 {
543  /* Exactly the same as textsend, so share code */
544  return textsend(fcinfo);
545 }
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:548
Datum varchartypmodin ( PG_FUNCTION_ARGS  )

Definition at line 632 of file varchar.c.

References anychar_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

633 {
635 
636  PG_RETURN_INT32(anychar_typmodin(ta, "varchar"));
637 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
static int32 anychar_typmodin(ArrayType *ta, const char *typename)
Definition: varchar.c:31
Datum varchartypmodout ( PG_FUNCTION_ARGS  )

Definition at line 640 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

641 {
642  int32 typmod = PG_GETARG_INT32(0);
643 
645 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:256
static char * anychar_typmodout(int32 typmod)
Definition: varchar.c:70
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322