PostgreSQL Source Code  git master
varchar.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/hashutils.h"
#include "utils/lsyscache.h"
#include "utils/pg_locale.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_support (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)
 
static void check_collation_set (Oid collid)
 
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)
 
Datum hashbpcharextended (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

◆ anychar_typmodin()

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

Definition at line 34 of file varchar.c.

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

Referenced by bpchartypmodin(), and varchartypmodin().

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

◆ anychar_typmodout()

static char* anychar_typmodout ( int32  typmod)
static

Definition at line 73 of file varchar.c.

References palloc(), snprintf, and VARHDRSZ.

Referenced by bpchartypmodout(), and varchartypmodout().

74 {
75  char *res = (char *) palloc(64);
76 
77  if (typmod > VARHDRSZ)
78  snprintf(res, 64, "(%d)", (int) (typmod - VARHDRSZ));
79  else
80  *res = '\0';
81 
82  return res;
83 }
#define VARHDRSZ
Definition: c.h:555
void * palloc(Size size)
Definition: mcxt.c:949
#define snprintf
Definition: port.h:192

◆ bcTruelen()

static int bcTruelen ( BpChar arg)
inlinestatic

Definition at line 666 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(), hashbpcharextended(), and internal_bpchar_pattern_compare().

667 {
668  return bpchartruelen(VARDATA_ANY(arg), VARSIZE_ANY_EXHDR(arg));
669 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int bpchartruelen(char *s, int len)
Definition: varchar.c:672
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ bpchar()

Datum bpchar ( PG_FUNCTION_ARGS  )

Definition at line 270 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, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by leftmostvalue_text().

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

◆ bpchar_input()

static BpChar* bpchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
)
static

Definition at line 127 of file varchar.c.

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

Referenced by bpcharin(), and bpcharrecv().

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

◆ bpchar_larger()

Datum bpchar_larger ( PG_FUNCTION_ARGS  )

Definition at line 939 of file varchar.c.

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

940 {
941  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
942  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
943  int len1,
944  len2;
945  int cmp;
946 
947  len1 = bcTruelen(arg1);
948  len2 = bcTruelen(arg2);
949 
950  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
951  PG_GET_COLLATION());
952 
953  PG_RETURN_BPCHAR_P((cmp >= 0) ? arg1 : arg2);
954 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpchar_name()

Datum bpchar_name ( PG_FUNCTION_ARGS  )

Definition at line 370 of file varchar.c.

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

371 {
372  BpChar *s = PG_GETARG_BPCHAR_PP(0);
373  char *s_data;
374  Name result;
375  int len;
376 
377  len = VARSIZE_ANY_EXHDR(s);
378  s_data = VARDATA_ANY(s);
379 
380  /* Truncate oversize input */
381  if (len >= NAMEDATALEN)
382  len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);
383 
384  /* Remove trailing blanks */
385  while (len > 0)
386  {
387  if (s_data[len - 1] != ' ')
388  break;
389  len--;
390  }
391 
392  /* We use palloc0 here to ensure result is zero-padded */
393  result = (Name) palloc0(NAMEDATALEN);
394  memcpy(NameStr(*result), s_data, len);
395 
396  PG_RETURN_NAME(result);
397 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define NAMEDATALEN
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
Definition: c.h:603
void * palloc0(Size size)
Definition: mcxt.c:980
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
NameData * Name
Definition: c.h:607
#define PG_RETURN_NAME(x)
Definition: fmgr.h:353

◆ bpchar_pattern_ge()

Datum bpchar_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 1162 of file varchar.c.

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

1163 {
1164  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1165  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1166  int result;
1167 
1168  result = internal_bpchar_pattern_compare(arg1, arg2);
1169 
1170  PG_FREE_IF_COPY(arg1, 0);
1171  PG_FREE_IF_COPY(arg2, 1);
1172 
1173  PG_RETURN_BOOL(result >= 0);
1174 }
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1108
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpchar_pattern_gt()

Datum bpchar_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 1178 of file varchar.c.

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

1179 {
1180  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1181  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1182  int result;
1183 
1184  result = internal_bpchar_pattern_compare(arg1, arg2);
1185 
1186  PG_FREE_IF_COPY(arg1, 0);
1187  PG_FREE_IF_COPY(arg2, 1);
1188 
1189  PG_RETURN_BOOL(result > 0);
1190 }
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1108
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpchar_pattern_le()

Datum bpchar_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 1146 of file varchar.c.

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

1147 {
1148  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1149  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1150  int result;
1151 
1152  result = internal_bpchar_pattern_compare(arg1, arg2);
1153 
1154  PG_FREE_IF_COPY(arg1, 0);
1155  PG_FREE_IF_COPY(arg2, 1);
1156 
1157  PG_RETURN_BOOL(result <= 0);
1158 }
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1108
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpchar_pattern_lt()

Datum bpchar_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 1130 of file varchar.c.

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

1131 {
1132  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1133  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1134  int result;
1135 
1136  result = internal_bpchar_pattern_compare(arg1, arg2);
1137 
1138  PG_FREE_IF_COPY(arg1, 0);
1139  PG_FREE_IF_COPY(arg2, 1);
1140 
1141  PG_RETURN_BOOL(result < 0);
1142 }
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1108
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpchar_smaller()

Datum bpchar_smaller ( PG_FUNCTION_ARGS  )

Definition at line 957 of file varchar.c.

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

958 {
959  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
960  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
961  int len1,
962  len2;
963  int cmp;
964 
965  len1 = bcTruelen(arg1);
966  len2 = bcTruelen(arg2);
967 
968  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
969  PG_GET_COLLATION());
970 
971  PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2);
972 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpchar_sortsupport()

Datum bpchar_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 922 of file varchar.c.

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

923 {
925  Oid collid = ssup->ssup_collation;
926  MemoryContext oldcontext;
927 
928  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
929 
930  /* Use generic string SortSupport */
931  varstr_sortsupport(ssup, BPCHAROID, collid);
932 
933  MemoryContextSwitchTo(oldcontext);
934 
935  PG_RETURN_VOID();
936 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1959
#define PG_RETURN_VOID()
Definition: fmgr.h:339

◆ bpcharcmp()

Datum bpcharcmp ( PG_FUNCTION_ARGS  )

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

Referenced by leftmostvalue_text().

902 {
903  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
904  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
905  int len1,
906  len2;
907  int cmp;
908 
909  len1 = bcTruelen(arg1);
910  len2 = bcTruelen(arg2);
911 
912  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
913  PG_GET_COLLATION());
914 
915  PG_FREE_IF_COPY(arg1, 0);
916  PG_FREE_IF_COPY(arg2, 1);
917 
918  PG_RETURN_INT32(cmp);
919 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpchareq()

Datum bpchareq ( PG_FUNCTION_ARGS  )

Definition at line 739 of file varchar.c.

References bcTruelen(), check_collation_set(), lc_collate_is_c(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, pg_newlocale_from_collation(), PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

740 {
741  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
742  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
743  int len1,
744  len2;
745  bool result;
746  Oid collid = PG_GET_COLLATION();
747 
748  check_collation_set(collid);
749 
750  len1 = bcTruelen(arg1);
751  len2 = bcTruelen(arg2);
752 
753  if (lc_collate_is_c(collid) ||
754  collid == DEFAULT_COLLATION_OID ||
755  pg_newlocale_from_collation(collid)->deterministic)
756  {
757  /*
758  * Since we only care about equality or not-equality, we can avoid all
759  * the expense of strcoll() here, and just do bitwise comparison.
760  */
761  if (len1 != len2)
762  result = false;
763  else
764  result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) == 0);
765  }
766  else
767  {
768  result = (varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
769  collid) == 0);
770  }
771 
772  PG_FREE_IF_COPY(arg1, 0);
773  PG_FREE_IF_COPY(arg2, 1);
774 
775  PG_RETURN_BOOL(result);
776 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:193
static void check_collation_set(Oid collid)
Definition: varchar.c:723
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1176
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpcharge()

Datum bpcharge ( PG_FUNCTION_ARGS  )

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

881 {
882  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
883  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
884  int len1,
885  len2;
886  int cmp;
887 
888  len1 = bcTruelen(arg1);
889  len2 = bcTruelen(arg2);
890 
891  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
892  PG_GET_COLLATION());
893 
894  PG_FREE_IF_COPY(arg1, 0);
895  PG_FREE_IF_COPY(arg2, 1);
896 
897  PG_RETURN_BOOL(cmp >= 0);
898 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpchargt()

Datum bpchargt ( PG_FUNCTION_ARGS  )

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

860 {
861  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
862  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
863  int len1,
864  len2;
865  int cmp;
866 
867  len1 = bcTruelen(arg1);
868  len2 = bcTruelen(arg2);
869 
870  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
871  PG_GET_COLLATION());
872 
873  PG_FREE_IF_COPY(arg1, 0);
874  PG_FREE_IF_COPY(arg2, 1);
875 
876  PG_RETURN_BOOL(cmp > 0);
877 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpcharin()

Datum bpcharin ( PG_FUNCTION_ARGS  )

Definition at line 195 of file varchar.c.

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

196 {
197  char *s = PG_GETARG_CSTRING(0);
198 
199 #ifdef NOT_USED
200  Oid typelem = PG_GETARG_OID(1);
201 #endif
202  int32 atttypmod = PG_GETARG_INT32(2);
203  BpChar *result;
204 
205  result = bpchar_input(s, strlen(s), atttypmod);
206  PG_RETURN_BPCHAR_P(result);
207 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static BpChar * bpchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:127
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
Definition: c.h:549

◆ bpcharle()

Datum bpcharle ( PG_FUNCTION_ARGS  )

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

839 {
840  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
841  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
842  int len1,
843  len2;
844  int cmp;
845 
846  len1 = bcTruelen(arg1);
847  len2 = bcTruelen(arg2);
848 
849  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
850  PG_GET_COLLATION());
851 
852  PG_FREE_IF_COPY(arg1, 0);
853  PG_FREE_IF_COPY(arg2, 1);
854 
855  PG_RETURN_BOOL(cmp <= 0);
856 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpcharlen()

Datum bpcharlen ( PG_FUNCTION_ARGS  )

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

690 {
692  int len;
693 
694  /* get number of bytes, ignoring trailing spaces */
695  len = bcTruelen(arg);
696 
697  /* in multibyte encoding, convert to number of characters */
699  len = pg_mbstrlen_with_len(VARDATA_ANY(arg), len);
700 
701  PG_RETURN_INT32(len);
702 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:836
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
void * arg
Definition: c.h:549

◆ bpcharlt()

Datum bpcharlt ( PG_FUNCTION_ARGS  )

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

818 {
819  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
820  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
821  int len1,
822  len2;
823  int cmp;
824 
825  len1 = bcTruelen(arg1);
826  len2 = bcTruelen(arg2);
827 
828  cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
829  PG_GET_COLLATION());
830 
831  PG_FREE_IF_COPY(arg1, 0);
832  PG_FREE_IF_COPY(arg2, 1);
833 
834  PG_RETURN_BOOL(cmp < 0);
835 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ bpcharne()

Datum bpcharne ( PG_FUNCTION_ARGS  )

Definition at line 779 of file varchar.c.

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

780 {
781  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
782  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
783  int len1,
784  len2;
785  bool result;
786  Oid collid = PG_GET_COLLATION();
787 
788  len1 = bcTruelen(arg1);
789  len2 = bcTruelen(arg2);
790 
791  if (lc_collate_is_c(collid) ||
792  collid == DEFAULT_COLLATION_OID ||
793  pg_newlocale_from_collation(collid)->deterministic)
794  {
795  /*
796  * Since we only care about equality or not-equality, we can avoid all
797  * the expense of strcoll() here, and just do bitwise comparison.
798  */
799  if (len1 != len2)
800  result = true;
801  else
802  result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) != 0);
803  }
804  else
805  {
806  result = (varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
807  collid) != 0);
808  }
809 
810  PG_FREE_IF_COPY(arg1, 0);
811  PG_FREE_IF_COPY(arg2, 1);
812 
813  PG_RETURN_BOOL(result);
814 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1176
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ bpcharoctetlen()

Datum bpcharoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 705 of file varchar.c.

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

706 {
708 
709  /* We need not detoast the input at all */
711 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define VARHDRSZ
Definition: c.h:555
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:806
uintptr_t Datum
Definition: postgres.h:367
void * arg

◆ bpcharout()

Datum bpcharout ( PG_FUNCTION_ARGS  )

Definition at line 217 of file varchar.c.

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

218 {
219  Datum txt = PG_GETARG_DATUM(0);
220 
222 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ bpcharrecv()

Datum bpcharrecv ( PG_FUNCTION_ARGS  )

Definition at line 228 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 generate_unaccent_rules::str.

229 {
231 
232 #ifdef NOT_USED
233  Oid typelem = PG_GETARG_OID(1);
234 #endif
235  int32 atttypmod = PG_GETARG_INT32(2);
236  BpChar *result;
237  char *str;
238  int nbytes;
239 
240  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
241  result = bpchar_input(str, nbytes, atttypmod);
242  pfree(str);
243  PG_RETURN_BPCHAR_P(result);
244 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
StringInfoData * StringInfo
Definition: stringinfo.h:43
static BpChar * bpchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:127
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
void pfree(void *pointer)
Definition: mcxt.c:1056
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
Definition: c.h:549

◆ bpcharsend()

Datum bpcharsend ( PG_FUNCTION_ARGS  )

Definition at line 250 of file varchar.c.

References textsend().

251 {
252  /* Exactly the same as textsend, so share code */
253  return textsend(fcinfo);
254 }
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:570

◆ bpchartruelen()

int bpchartruelen ( char *  s,
int  len 
)

Definition at line 672 of file varchar.c.

References i.

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

673 {
674  int i;
675 
676  /*
677  * Note that we rely on the assumption that ' ' is a singleton unit on
678  * every supported multibyte server encoding.
679  */
680  for (i = len - 1; i >= 0; i--)
681  {
682  if (s[i] != ' ')
683  break;
684  }
685  return i + 1;
686 }
int i

◆ bpchartypmodin()

Datum bpchartypmodin ( PG_FUNCTION_ARGS  )

Definition at line 416 of file varchar.c.

References anychar_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

417 {
419 
420  PG_RETURN_INT32(anychar_typmodin(ta, "char"));
421 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
static int32 anychar_typmodin(ArrayType *ta, const char *typename)
Definition: varchar.c:34

◆ bpchartypmodout()

Datum bpchartypmodout ( PG_FUNCTION_ARGS  )

Definition at line 424 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

425 {
426  int32 typmod = PG_GETARG_INT32(0);
427 
429 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
signed int int32
Definition: c.h:346
static char * anychar_typmodout(int32 typmod)
Definition: varchar.c:73
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ btbpchar_pattern_cmp()

Datum btbpchar_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1194 of file varchar.c.

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

1195 {
1196  BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1197  BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1198  int result;
1199 
1200  result = internal_bpchar_pattern_compare(arg1, arg2);
1201 
1202  PG_FREE_IF_COPY(arg1, 0);
1203  PG_FREE_IF_COPY(arg2, 1);
1204 
1205  PG_RETURN_INT32(result);
1206 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1108
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:549

◆ btbpchar_pattern_sortsupport()

Datum btbpchar_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1210 of file varchar.c.

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

1211 {
1213  MemoryContext oldcontext;
1214 
1215  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1216 
1217  /* Use generic string SortSupport, forcing "C" collation */
1218  varstr_sortsupport(ssup, BPCHAROID, C_COLLATION_OID);
1219 
1220  MemoryContextSwitchTo(oldcontext);
1221 
1222  PG_RETURN_VOID();
1223 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1959
#define PG_RETURN_VOID()
Definition: fmgr.h:339

◆ char_bpchar()

Datum char_bpchar ( PG_FUNCTION_ARGS  )

Definition at line 352 of file varchar.c.

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

353 {
354  char c = PG_GETARG_CHAR(0);
355  BpChar *result;
356 
357  result = (BpChar *) palloc(VARHDRSZ + 1);
358 
359  SET_VARSIZE(result, VARHDRSZ + 1);
360  *(VARDATA(result)) = c;
361 
362  PG_RETURN_BPCHAR_P(result);
363 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
char * c
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:268

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

Definition at line 723 of file varchar.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, and OidIsValid.

Referenced by bpchareq().

724 {
725  if (!OidIsValid(collid))
726  {
727  /*
728  * This typically means that the parser could not resolve a conflict
729  * of implicit collations, so report it that way.
730  */
731  ereport(ERROR,
732  (errcode(ERRCODE_INDETERMINATE_COLLATION),
733  errmsg("could not determine which collation to use for string comparison"),
734  errhint("Use the COLLATE clause to set the collation explicitly.")));
735  }
736 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ hashbpchar()

Datum hashbpchar ( PG_FUNCTION_ARGS  )

Definition at line 980 of file varchar.c.

References bcTruelen(), buf, pg_locale_struct::deterministic, elog, ereport, errcode(), errhint(), errmsg(), ERROR, hash_any(), pg_locale_struct::info, sort-test::key, lc_collate_is_c(), palloc(), pfree(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, pg_newlocale_from_collation(), pg_locale_struct::provider, and VARDATA_ANY.

981 {
983  Oid collid = PG_GET_COLLATION();
984  char *keydata;
985  int keylen;
986  pg_locale_t mylocale = 0;
987  Datum result;
988 
989  if (!collid)
990  ereport(ERROR,
991  (errcode(ERRCODE_INDETERMINATE_COLLATION),
992  errmsg("could not determine which collation to use for string hashing"),
993  errhint("Use the COLLATE clause to set the collation explicitly.")));
994 
995  keydata = VARDATA_ANY(key);
996  keylen = bcTruelen(key);
997 
998  if (!lc_collate_is_c(collid) && collid != DEFAULT_COLLATION_OID)
999  mylocale = pg_newlocale_from_collation(collid);
1000 
1001  if (!mylocale || mylocale->deterministic)
1002  {
1003  result = hash_any((unsigned char *) keydata, keylen);
1004  }
1005  else
1006  {
1007 #ifdef USE_ICU
1008  if (mylocale->provider == COLLPROVIDER_ICU)
1009  {
1010  int32_t ulen = -1;
1011  UChar *uchar = NULL;
1012  Size bsize;
1013  uint8_t *buf;
1014 
1015  ulen = icu_to_uchar(&uchar, keydata, keylen);
1016 
1017  bsize = ucol_getSortKey(mylocale->info.icu.ucol,
1018  uchar, ulen, NULL, 0);
1019  buf = palloc(bsize);
1020  ucol_getSortKey(mylocale->info.icu.ucol,
1021  uchar, ulen, buf, bsize);
1022 
1023  result = hash_any(buf, bsize);
1024 
1025  pfree(buf);
1026  }
1027  else
1028 #endif
1029  /* shouldn't happen */
1030  elog(ERROR, "unsupported collprovider: %c", mylocale->provider);
1031  }
1032 
1033  /* Avoid leaking memory for toasted inputs */
1034  PG_FREE_IF_COPY(key, 0);
1035 
1036  return result;
1037 }
union pg_locale_struct::@144 info
int errhint(const char *fmt,...)
Definition: elog.c:974
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:193
void pfree(void *pointer)
Definition: mcxt.c:1056
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define ERROR
Definition: elog.h:43
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1176
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel, rest)
Definition: elog.h:141
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
uintptr_t Datum
Definition: postgres.h:367
bool deterministic
Definition: pg_locale.h:85
size_t Size
Definition: c.h:466
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
Definition: c.h:549

◆ hashbpcharextended()

Datum hashbpcharextended ( PG_FUNCTION_ARGS  )

Definition at line 1040 of file varchar.c.

References bcTruelen(), buf, pg_locale_struct::deterministic, elog, ereport, errcode(), errhint(), errmsg(), ERROR, hash_any_extended(), pg_locale_struct::info, sort-test::key, lc_collate_is_c(), palloc(), pfree(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_GETARG_INT64, pg_newlocale_from_collation(), pg_locale_struct::provider, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

1041 {
1043  Oid collid = PG_GET_COLLATION();
1044  char *keydata;
1045  int keylen;
1046  pg_locale_t mylocale = 0;
1047  Datum result;
1048 
1049  if (!collid)
1050  ereport(ERROR,
1051  (errcode(ERRCODE_INDETERMINATE_COLLATION),
1052  errmsg("could not determine which collation to use for string hashing"),
1053  errhint("Use the COLLATE clause to set the collation explicitly.")));
1054 
1055  keydata = VARDATA_ANY(key);
1056  keylen = bcTruelen(key);
1057 
1058  if (!lc_collate_is_c(collid) && collid != DEFAULT_COLLATION_OID)
1059  mylocale = pg_newlocale_from_collation(collid);
1060 
1061  if (!mylocale || mylocale->deterministic)
1062  {
1063  result = hash_any_extended((unsigned char *) keydata, keylen,
1064  PG_GETARG_INT64(1));
1065  }
1066  else
1067  {
1068 #ifdef USE_ICU
1069  if (mylocale->provider == COLLPROVIDER_ICU)
1070  {
1071  int32_t ulen = -1;
1072  UChar *uchar = NULL;
1073  Size bsize;
1074  uint8_t *buf;
1075 
1076  ulen = icu_to_uchar(&uchar, VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
1077 
1078  bsize = ucol_getSortKey(mylocale->info.icu.ucol,
1079  uchar, ulen, NULL, 0);
1080  buf = palloc(bsize);
1081  ucol_getSortKey(mylocale->info.icu.ucol,
1082  uchar, ulen, buf, bsize);
1083 
1084  result = hash_any_extended(buf, bsize, PG_GETARG_INT64(1));
1085 
1086  pfree(buf);
1087  }
1088  else
1089 #endif
1090  /* shouldn't happen */
1091  elog(ERROR, "unsupported collprovider: %c", mylocale->provider);
1092  }
1093 
1094  PG_FREE_IF_COPY(key, 0);
1095 
1096  return result;
1097 }
union pg_locale_struct::@144 info
int errhint(const char *fmt,...)
Definition: elog.c:974
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:570
Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.c:374
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GET_COLLATION()
Definition: fmgr.h:193
void pfree(void *pointer)
Definition: mcxt.c:1056
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:304
#define ERROR
Definition: elog.h:43
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1176
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel, rest)
Definition: elog.h:141
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1323
uintptr_t Datum
Definition: postgres.h:367
bool deterministic
Definition: pg_locale.h:85
size_t Size
Definition: c.h:466
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
Definition: c.h:549
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277

◆ internal_bpchar_pattern_compare()

static int internal_bpchar_pattern_compare ( BpChar arg1,
BpChar arg2 
)
static

Definition at line 1108 of file varchar.c.

References bcTruelen(), Min, and VARDATA_ANY.

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

1109 {
1110  int result;
1111  int len1,
1112  len2;
1113 
1114  len1 = bcTruelen(arg1);
1115  len2 = bcTruelen(arg2);
1116 
1117  result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
1118  if (result != 0)
1119  return result;
1120  else if (len1 < len2)
1121  return -1;
1122  else if (len1 > len2)
1123  return 1;
1124  else
1125  return 0;
1126 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define Min(x, y)
Definition: c.h:904
static int bcTruelen(BpChar *arg)
Definition: varchar.c:666

◆ name_bpchar()

Datum name_bpchar ( PG_FUNCTION_ARGS  )

Definition at line 406 of file varchar.c.

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

407 {
408  Name s = PG_GETARG_NAME(0);
409  BpChar *result;
410 
411  result = (BpChar *) cstring_to_text(NameStr(*s));
412  PG_RETURN_BPCHAR_P(result);
413 }
Definition: c.h:603
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:362
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ varchar()

Datum varchar ( PG_FUNCTION_ARGS  )

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

606 {
607  VarChar *source = PG_GETARG_VARCHAR_PP(0);
608  int32 typmod = PG_GETARG_INT32(1);
609  bool isExplicit = PG_GETARG_BOOL(2);
610  int32 len,
611  maxlen;
612  size_t maxmblen;
613  int i;
614  char *s_data;
615 
616  len = VARSIZE_ANY_EXHDR(source);
617  s_data = VARDATA_ANY(source);
618  maxlen = typmod - VARHDRSZ;
619 
620  /* No work if typmod is invalid or supplied data fits it already */
621  if (maxlen < 0 || len <= maxlen)
622  PG_RETURN_VARCHAR_P(source);
623 
624  /* only reach here if string is too long... */
625 
626  /* truncate multibyte string preserving multibyte boundary */
627  maxmblen = pg_mbcharcliplen(s_data, len, maxlen);
628 
629  if (!isExplicit)
630  {
631  for (i = maxmblen; i < len; i++)
632  if (s_data[i] != ' ')
633  ereport(ERROR,
634  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
635  errmsg("value too long for type character varying(%d)",
636  maxlen)));
637  }
638 
640  maxmblen));
641 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:904
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARHDRSZ
Definition: c.h:555
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:363
#define PG_GETARG_VARCHAR_PP(n)
Definition: fmgr.h:305
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
Definition: c.h:549

◆ varchar_input()

static VarChar* varchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
)
static

Definition at line 455 of file varchar.c.

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

Referenced by varcharin(), and varcharrecv().

456 {
457  VarChar *result;
458  size_t maxlen;
459 
460  maxlen = atttypmod - VARHDRSZ;
461 
462  if (atttypmod >= (int32) VARHDRSZ && len > maxlen)
463  {
464  /* Verify that extra characters are spaces, and clip them off */
465  size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
466  size_t j;
467 
468  for (j = mbmaxlen; j < len; j++)
469  {
470  if (s[j] != ' ')
471  ereport(ERROR,
472  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
473  errmsg("value too long for type character varying(%d)",
474  (int) maxlen)));
475  }
476 
477  len = mbmaxlen;
478  }
479 
480  result = (VarChar *) cstring_to_text_with_len(s, len);
481  return result;
482 }
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:904
#define VARHDRSZ
Definition: c.h:555
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ varchar_support()

Datum varchar_support ( PG_FUNCTION_ARGS  )

Definition at line 561 of file varchar.c.

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

562 {
563  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
564  Node *ret = NULL;
565 
566  if (IsA(rawreq, SupportRequestSimplify))
567  {
569  FuncExpr *expr = req->fcall;
570  Node *typmod;
571 
572  Assert(list_length(expr->args) >= 2);
573 
574  typmod = (Node *) lsecond(expr->args);
575 
576  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
577  {
578  Node *source = (Node *) linitial(expr->args);
579  int32 old_typmod = exprTypmod(source);
580  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
581  int32 old_max = old_typmod - VARHDRSZ;
582  int32 new_max = new_typmod - VARHDRSZ;
583 
584  if (new_typmod < 0 || (old_typmod >= 0 && old_max <= new_max))
585  ret = relabel_to_typmod(source, new_typmod);
586  }
587  }
588 
589  PG_RETURN_POINTER(ret);
590 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * args
Definition: primnodes.h:463
#define DatumGetInt32(X)
Definition: postgres.h:472
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define VARHDRSZ
Definition: c.h:555
Definition: nodes.h:525
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:346
#define linitial(l)
Definition: pg_list.h:195
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588

◆ varcharin()

Datum varcharin ( PG_FUNCTION_ARGS  )

Definition at line 489 of file varchar.c.

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

490 {
491  char *s = PG_GETARG_CSTRING(0);
492 
493 #ifdef NOT_USED
494  Oid typelem = PG_GETARG_OID(1);
495 #endif
496  int32 atttypmod = PG_GETARG_INT32(2);
497  VarChar *result;
498 
499  result = varchar_input(s, strlen(s), atttypmod);
500  PG_RETURN_VARCHAR_P(result);
501 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:455
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:363
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
Definition: c.h:549

◆ varcharout()

Datum varcharout ( PG_FUNCTION_ARGS  )

Definition at line 511 of file varchar.c.

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

512 {
513  Datum txt = PG_GETARG_DATUM(0);
514 
516 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ varcharrecv()

Datum varcharrecv ( PG_FUNCTION_ARGS  )

Definition at line 522 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(), generate_unaccent_rules::str, and varchar_input().

523 {
525 
526 #ifdef NOT_USED
527  Oid typelem = PG_GETARG_OID(1);
528 #endif
529  int32 atttypmod = PG_GETARG_INT32(2);
530  VarChar *result;
531  char *str;
532  int nbytes;
533 
534  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
535  result = varchar_input(str, nbytes, atttypmod);
536  pfree(str);
537  PG_RETURN_VARCHAR_P(result);
538 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod)
Definition: varchar.c:455
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
void pfree(void *pointer)
Definition: mcxt.c:1056
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:363
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
Definition: c.h:549

◆ varcharsend()

Datum varcharsend ( PG_FUNCTION_ARGS  )

Definition at line 544 of file varchar.c.

References textsend().

545 {
546  /* Exactly the same as textsend, so share code */
547  return textsend(fcinfo);
548 }
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:570

◆ varchartypmodin()

Datum varchartypmodin ( PG_FUNCTION_ARGS  )

Definition at line 644 of file varchar.c.

References anychar_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

645 {
647 
648  PG_RETURN_INT32(anychar_typmodin(ta, "varchar"));
649 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
static int32 anychar_typmodin(ArrayType *ta, const char *typename)
Definition: varchar.c:34

◆ varchartypmodout()

Datum varchartypmodout ( PG_FUNCTION_ARGS  )

Definition at line 652 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

653 {
654  int32 typmod = PG_GETARG_INT32(0);
655 
657 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
signed int int32
Definition: c.h:346
static char * anychar_typmodout(int32 typmod)
Definition: varchar.c:73
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352