PostgreSQL Source Code  git master
citext.c File Reference
#include "postgres.h"
#include "catalog/pg_collation.h"
#include "common/hashfn.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/varlena.h"
#include "varatt.h"
Include dependency graph for citext.c:

Go to the source code of this file.

Functions

static int32 citextcmp (text *left, text *right, Oid collid)
 
static int32 internal_citext_pattern_cmp (text *left, text *right, Oid collid)
 
 PG_FUNCTION_INFO_V1 (citext_cmp)
 
Datum citext_cmp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_pattern_cmp)
 
Datum citext_pattern_cmp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_hash)
 
Datum citext_hash (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_hash_extended)
 
Datum citext_hash_extended (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_eq)
 
Datum citext_eq (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_ne)
 
Datum citext_ne (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_lt)
 
Datum citext_lt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_le)
 
Datum citext_le (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_gt)
 
Datum citext_gt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_ge)
 
Datum citext_ge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_pattern_lt)
 
Datum citext_pattern_lt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_pattern_le)
 
Datum citext_pattern_le (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_pattern_gt)
 
Datum citext_pattern_gt (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_pattern_ge)
 
Datum citext_pattern_ge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_smaller)
 
Datum citext_smaller (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_larger)
 
Datum citext_larger (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ citext_cmp()

Datum citext_cmp ( PG_FUNCTION_ARGS  )

Definition at line 107 of file citext.c.

108 {
109  text *left = PG_GETARG_TEXT_PP(0);
110  text *right = PG_GETARG_TEXT_PP(1);
111  int32 result;
112 
113  result = citextcmp(left, right, PG_GET_COLLATION());
114 
115  PG_FREE_IF_COPY(left, 0);
116  PG_FREE_IF_COPY(right, 1);
117 
118  PG_RETURN_INT32(result);
119 }
signed int int32
Definition: c.h:494
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
Definition: c.h:687

References citextcmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, and PG_RETURN_INT32.

◆ citext_eq()

Datum citext_eq ( PG_FUNCTION_ARGS  )

Definition at line 186 of file citext.c.

187 {
188  text *left = PG_GETARG_TEXT_PP(0);
189  text *right = PG_GETARG_TEXT_PP(1);
190  char *lcstr,
191  *rcstr;
192  bool result;
193 
194  /* We can't compare lengths in advance of downcasing ... */
195 
196  lcstr = str_tolower(VARDATA_ANY(left), VARSIZE_ANY_EXHDR(left), DEFAULT_COLLATION_OID);
197  rcstr = str_tolower(VARDATA_ANY(right), VARSIZE_ANY_EXHDR(right), DEFAULT_COLLATION_OID);
198 
199  /*
200  * Since we only care about equality or not-equality, we can avoid all the
201  * expense of strcoll() here, and just do bitwise comparison.
202  */
203  result = (strcmp(lcstr, rcstr) == 0);
204 
205  pfree(lcstr);
206  pfree(rcstr);
207  PG_FREE_IF_COPY(left, 0);
208  PG_FREE_IF_COPY(right, 1);
209 
210  PG_RETURN_BOOL(result);
211 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1636
void pfree(void *pointer)
Definition: mcxt.c:1520
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str_tolower(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ citext_ge()

Datum citext_ge ( PG_FUNCTION_ARGS  )

Definition at line 297 of file citext.c.

298 {
299  text *left = PG_GETARG_TEXT_PP(0);
300  text *right = PG_GETARG_TEXT_PP(1);
301  bool result;
302 
303  result = citextcmp(left, right, PG_GET_COLLATION()) >= 0;
304 
305  PG_FREE_IF_COPY(left, 0);
306  PG_FREE_IF_COPY(right, 1);
307 
308  PG_RETURN_BOOL(result);
309 }

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

◆ citext_gt()

Datum citext_gt ( PG_FUNCTION_ARGS  )

Definition at line 280 of file citext.c.

281 {
282  text *left = PG_GETARG_TEXT_PP(0);
283  text *right = PG_GETARG_TEXT_PP(1);
284  bool result;
285 
286  result = citextcmp(left, right, PG_GET_COLLATION()) > 0;
287 
288  PG_FREE_IF_COPY(left, 0);
289  PG_FREE_IF_COPY(right, 1);
290 
291  PG_RETURN_BOOL(result);
292 }

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

◆ citext_hash()

Datum citext_hash ( PG_FUNCTION_ARGS  )

Definition at line 141 of file citext.c.

142 {
143  text *txt = PG_GETARG_TEXT_PP(0);
144  char *str;
145  Datum result;
146 
147  str = str_tolower(VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt), DEFAULT_COLLATION_OID);
148  result = hash_any((unsigned char *) str, strlen(str));
149  pfree(str);
150 
151  /* Avoid leaking memory for toasted inputs */
152  PG_FREE_IF_COPY(txt, 0);
153 
154  PG_RETURN_DATUM(result);
155 }
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
const char * str
uintptr_t Datum
Definition: postgres.h:64

References hash_any(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, str, str_tolower(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ citext_hash_extended()

Datum citext_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 160 of file citext.c.

161 {
162  text *txt = PG_GETARG_TEXT_PP(0);
163  uint64 seed = PG_GETARG_INT64(1);
164  char *str;
165  Datum result;
166 
167  str = str_tolower(VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt), DEFAULT_COLLATION_OID);
168  result = hash_any_extended((unsigned char *) str, strlen(str), seed);
169  pfree(str);
170 
171  /* Avoid leaking memory for toasted inputs */
172  PG_FREE_IF_COPY(txt, 0);
173 
174  PG_RETURN_DATUM(result);
175 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

References hash_any_extended(), pfree(), PG_FREE_IF_COPY, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, str, str_tolower(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ citext_larger()

Datum citext_larger ( PG_FUNCTION_ARGS  )

Definition at line 401 of file citext.c.

402 {
403  text *left = PG_GETARG_TEXT_PP(0);
404  text *right = PG_GETARG_TEXT_PP(1);
405  text *result;
406 
407  result = citextcmp(left, right, PG_GET_COLLATION()) > 0 ? left : right;
408  PG_RETURN_TEXT_P(result);
409 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372

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

◆ citext_le()

Datum citext_le ( PG_FUNCTION_ARGS  )

Definition at line 263 of file citext.c.

264 {
265  text *left = PG_GETARG_TEXT_PP(0);
266  text *right = PG_GETARG_TEXT_PP(1);
267  bool result;
268 
269  result = citextcmp(left, right, PG_GET_COLLATION()) <= 0;
270 
271  PG_FREE_IF_COPY(left, 0);
272  PG_FREE_IF_COPY(right, 1);
273 
274  PG_RETURN_BOOL(result);
275 }

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

◆ citext_lt()

Datum citext_lt ( PG_FUNCTION_ARGS  )

Definition at line 246 of file citext.c.

247 {
248  text *left = PG_GETARG_TEXT_PP(0);
249  text *right = PG_GETARG_TEXT_PP(1);
250  bool result;
251 
252  result = citextcmp(left, right, PG_GET_COLLATION()) < 0;
253 
254  PG_FREE_IF_COPY(left, 0);
255  PG_FREE_IF_COPY(right, 1);
256 
257  PG_RETURN_BOOL(result);
258 }

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

◆ citext_ne()

Datum citext_ne ( PG_FUNCTION_ARGS  )

Definition at line 216 of file citext.c.

217 {
218  text *left = PG_GETARG_TEXT_PP(0);
219  text *right = PG_GETARG_TEXT_PP(1);
220  char *lcstr,
221  *rcstr;
222  bool result;
223 
224  /* We can't compare lengths in advance of downcasing ... */
225 
226  lcstr = str_tolower(VARDATA_ANY(left), VARSIZE_ANY_EXHDR(left), DEFAULT_COLLATION_OID);
227  rcstr = str_tolower(VARDATA_ANY(right), VARSIZE_ANY_EXHDR(right), DEFAULT_COLLATION_OID);
228 
229  /*
230  * Since we only care about equality or not-equality, we can avoid all the
231  * expense of strcoll() here, and just do bitwise comparison.
232  */
233  result = (strcmp(lcstr, rcstr) != 0);
234 
235  pfree(lcstr);
236  pfree(rcstr);
237  PG_FREE_IF_COPY(left, 0);
238  PG_FREE_IF_COPY(right, 1);
239 
240  PG_RETURN_BOOL(result);
241 }

References pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, str_tolower(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ citext_pattern_cmp()

Datum citext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 124 of file citext.c.

125 {
126  text *left = PG_GETARG_TEXT_PP(0);
127  text *right = PG_GETARG_TEXT_PP(1);
128  int32 result;
129 
130  result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION());
131 
132  PG_FREE_IF_COPY(left, 0);
133  PG_FREE_IF_COPY(right, 1);
134 
135  PG_RETURN_INT32(result);
136 }
static int32 internal_citext_pattern_cmp(text *left, text *right, Oid collid)
Definition: citext.c:69

References internal_citext_pattern_cmp(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, and PG_RETURN_INT32.

◆ citext_pattern_ge()

Datum citext_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 365 of file citext.c.

366 {
367  text *left = PG_GETARG_TEXT_PP(0);
368  text *right = PG_GETARG_TEXT_PP(1);
369  bool result;
370 
371  result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) >= 0;
372 
373  PG_FREE_IF_COPY(left, 0);
374  PG_FREE_IF_COPY(right, 1);
375 
376  PG_RETURN_BOOL(result);
377 }

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

◆ citext_pattern_gt()

Datum citext_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 348 of file citext.c.

349 {
350  text *left = PG_GETARG_TEXT_PP(0);
351  text *right = PG_GETARG_TEXT_PP(1);
352  bool result;
353 
354  result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) > 0;
355 
356  PG_FREE_IF_COPY(left, 0);
357  PG_FREE_IF_COPY(right, 1);
358 
359  PG_RETURN_BOOL(result);
360 }

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

◆ citext_pattern_le()

Datum citext_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 331 of file citext.c.

332 {
333  text *left = PG_GETARG_TEXT_PP(0);
334  text *right = PG_GETARG_TEXT_PP(1);
335  bool result;
336 
337  result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) <= 0;
338 
339  PG_FREE_IF_COPY(left, 0);
340  PG_FREE_IF_COPY(right, 1);
341 
342  PG_RETURN_BOOL(result);
343 }

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

◆ citext_pattern_lt()

Datum citext_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 314 of file citext.c.

315 {
316  text *left = PG_GETARG_TEXT_PP(0);
317  text *right = PG_GETARG_TEXT_PP(1);
318  bool result;
319 
320  result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) < 0;
321 
322  PG_FREE_IF_COPY(left, 0);
323  PG_FREE_IF_COPY(right, 1);
324 
325  PG_RETURN_BOOL(result);
326 }

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

◆ citext_smaller()

Datum citext_smaller ( PG_FUNCTION_ARGS  )

Definition at line 388 of file citext.c.

389 {
390  text *left = PG_GETARG_TEXT_PP(0);
391  text *right = PG_GETARG_TEXT_PP(1);
392  text *result;
393 
394  result = citextcmp(left, right, PG_GET_COLLATION()) < 0 ? left : right;
395  PG_RETURN_TEXT_P(result);
396 }

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

◆ citextcmp()

static int32 citextcmp ( text left,
text right,
Oid  collid 
)
static

Definition at line 36 of file citext.c.

37 {
38  char *lcstr,
39  *rcstr;
40  int32 result;
41 
42  /*
43  * We must do our str_tolower calls with DEFAULT_COLLATION_OID, not the
44  * input collation as you might expect. This is so that the behavior of
45  * citext's equality and hashing functions is not collation-dependent. We
46  * should change this once the core infrastructure is able to cope with
47  * collation-dependent equality and hashing functions.
48  */
49 
50  lcstr = str_tolower(VARDATA_ANY(left), VARSIZE_ANY_EXHDR(left), DEFAULT_COLLATION_OID);
51  rcstr = str_tolower(VARDATA_ANY(right), VARSIZE_ANY_EXHDR(right), DEFAULT_COLLATION_OID);
52 
53  result = varstr_cmp(lcstr, strlen(lcstr),
54  rcstr, strlen(rcstr),
55  collid);
56 
57  pfree(lcstr);
58  pfree(rcstr);
59 
60  return result;
61 }
Oid collid
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1539

References collid, pfree(), str_tolower(), VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_cmp().

Referenced by citext_cmp(), citext_ge(), citext_gt(), citext_larger(), citext_le(), citext_lt(), and citext_smaller().

◆ internal_citext_pattern_cmp()

static int32 internal_citext_pattern_cmp ( text left,
text right,
Oid  collid 
)
static

Definition at line 69 of file citext.c.

70 {
71  char *lcstr,
72  *rcstr;
73  int llen,
74  rlen;
75  int32 result;
76 
77  lcstr = str_tolower(VARDATA_ANY(left), VARSIZE_ANY_EXHDR(left), DEFAULT_COLLATION_OID);
78  rcstr = str_tolower(VARDATA_ANY(right), VARSIZE_ANY_EXHDR(right), DEFAULT_COLLATION_OID);
79 
80  llen = strlen(lcstr);
81  rlen = strlen(rcstr);
82 
83  result = memcmp(lcstr, rcstr, Min(llen, rlen));
84  if (result == 0)
85  {
86  if (llen < rlen)
87  result = -1;
88  else if (llen > rlen)
89  result = 1;
90  }
91 
92  pfree(lcstr);
93  pfree(rcstr);
94 
95  return result;
96 }
#define Min(x, y)
Definition: c.h:1004

References Min, pfree(), str_tolower(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by citext_pattern_cmp(), citext_pattern_ge(), citext_pattern_gt(), citext_pattern_le(), and citext_pattern_lt().

◆ PG_FUNCTION_INFO_V1() [1/16]

PG_FUNCTION_INFO_V1 ( citext_cmp  )

◆ PG_FUNCTION_INFO_V1() [2/16]

PG_FUNCTION_INFO_V1 ( citext_eq  )

◆ PG_FUNCTION_INFO_V1() [3/16]

PG_FUNCTION_INFO_V1 ( citext_ge  )

◆ PG_FUNCTION_INFO_V1() [4/16]

PG_FUNCTION_INFO_V1 ( citext_gt  )

◆ PG_FUNCTION_INFO_V1() [5/16]

PG_FUNCTION_INFO_V1 ( citext_hash  )

◆ PG_FUNCTION_INFO_V1() [6/16]

PG_FUNCTION_INFO_V1 ( citext_hash_extended  )

◆ PG_FUNCTION_INFO_V1() [7/16]

PG_FUNCTION_INFO_V1 ( citext_larger  )

◆ PG_FUNCTION_INFO_V1() [8/16]

PG_FUNCTION_INFO_V1 ( citext_le  )

◆ PG_FUNCTION_INFO_V1() [9/16]

PG_FUNCTION_INFO_V1 ( citext_lt  )

◆ PG_FUNCTION_INFO_V1() [10/16]

PG_FUNCTION_INFO_V1 ( citext_ne  )

◆ PG_FUNCTION_INFO_V1() [11/16]

PG_FUNCTION_INFO_V1 ( citext_pattern_cmp  )

◆ PG_FUNCTION_INFO_V1() [12/16]

PG_FUNCTION_INFO_V1 ( citext_pattern_ge  )

◆ PG_FUNCTION_INFO_V1() [13/16]

PG_FUNCTION_INFO_V1 ( citext_pattern_gt  )

◆ PG_FUNCTION_INFO_V1() [14/16]

PG_FUNCTION_INFO_V1 ( citext_pattern_le  )

◆ PG_FUNCTION_INFO_V1() [15/16]

PG_FUNCTION_INFO_V1 ( citext_pattern_lt  )

◆ PG_FUNCTION_INFO_V1() [16/16]

PG_FUNCTION_INFO_V1 ( citext_smaller  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 13 of file citext.c.