PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
citext.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "catalog/pg_collation.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/varlena.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)
 
 PG_FUNCTION_INFO_V1 (citext_cmp)
 
Datum citext_cmp (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (citext_hash)
 
Datum citext_hash (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_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

Datum citext_cmp ( PG_FUNCTION_ARGS  )

Definition at line 70 of file citext.c.

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

71 {
72  text *left = PG_GETARG_TEXT_PP(0);
73  text *right = PG_GETARG_TEXT_PP(1);
74  int32 result;
75 
76  result = citextcmp(left, right, PG_GET_COLLATION());
77 
78  PG_FREE_IF_COPY(left, 0);
79  PG_FREE_IF_COPY(right, 1);
80 
81  PG_RETURN_INT32(result);
82 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
signed int int32
Definition: c.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum citext_eq ( PG_FUNCTION_ARGS  )

Definition at line 112 of file citext.c.

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

113 {
114  text *left = PG_GETARG_TEXT_PP(0);
115  text *right = PG_GETARG_TEXT_PP(1);
116  char *lcstr,
117  *rcstr;
118  bool result;
119 
120  /* We can't compare lengths in advance of downcasing ... */
121 
124 
125  /*
126  * Since we only care about equality or not-equality, we can avoid all the
127  * expense of strcoll() here, and just do bitwise comparison.
128  */
129  result = (strcmp(lcstr, rcstr) == 0);
130 
131  pfree(lcstr);
132  pfree(rcstr);
133  PG_FREE_IF_COPY(left, 0);
134  PG_FREE_IF_COPY(right, 1);
135 
136  PG_RETURN_BOOL(result);
137 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1519
return result
Definition: formatting.c:1633
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:949
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum citext_ge ( PG_FUNCTION_ARGS  )

Definition at line 223 of file citext.c.

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

224 {
225  text *left = PG_GETARG_TEXT_PP(0);
226  text *right = PG_GETARG_TEXT_PP(1);
227  bool result;
228 
229  result = citextcmp(left, right, PG_GET_COLLATION()) >= 0;
230 
231  PG_FREE_IF_COPY(left, 0);
232  PG_FREE_IF_COPY(right, 1);
233 
234  PG_RETURN_BOOL(result);
235 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum citext_gt ( PG_FUNCTION_ARGS  )

Definition at line 206 of file citext.c.

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

207 {
208  text *left = PG_GETARG_TEXT_PP(0);
209  text *right = PG_GETARG_TEXT_PP(1);
210  bool result;
211 
212  result = citextcmp(left, right, PG_GET_COLLATION()) > 0;
213 
214  PG_FREE_IF_COPY(left, 0);
215  PG_FREE_IF_COPY(right, 1);
216 
217  PG_RETURN_BOOL(result);
218 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum citext_hash ( PG_FUNCTION_ARGS  )

Definition at line 87 of file citext.c.

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

88 {
89  text *txt = PG_GETARG_TEXT_PP(0);
90  char *str;
91  Datum result;
92 
94  result = hash_any((unsigned char *) str, strlen(str));
95  pfree(str);
96 
97  /* Avoid leaking memory for toasted inputs */
98  PG_FREE_IF_COPY(txt, 0);
99 
100  PG_RETURN_DATUM(result);
101 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1519
return result
Definition: formatting.c:1633
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:949
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum citext_larger ( PG_FUNCTION_ARGS  )

Definition at line 259 of file citext.c.

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

260 {
261  text *left = PG_GETARG_TEXT_PP(0);
262  text *right = PG_GETARG_TEXT_PP(1);
263  text *result;
264 
265  result = citextcmp(left, right, PG_GET_COLLATION()) > 0 ? left : right;
266  PG_RETURN_TEXT_P(result);
267 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
Datum citext_le ( PG_FUNCTION_ARGS  )

Definition at line 189 of file citext.c.

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

190 {
191  text *left = PG_GETARG_TEXT_PP(0);
192  text *right = PG_GETARG_TEXT_PP(1);
193  bool result;
194 
195  result = citextcmp(left, right, PG_GET_COLLATION()) <= 0;
196 
197  PG_FREE_IF_COPY(left, 0);
198  PG_FREE_IF_COPY(right, 1);
199 
200  PG_RETURN_BOOL(result);
201 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum citext_lt ( PG_FUNCTION_ARGS  )

Definition at line 172 of file citext.c.

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

173 {
174  text *left = PG_GETARG_TEXT_PP(0);
175  text *right = PG_GETARG_TEXT_PP(1);
176  bool result;
177 
178  result = citextcmp(left, right, PG_GET_COLLATION()) < 0;
179 
180  PG_FREE_IF_COPY(left, 0);
181  PG_FREE_IF_COPY(right, 1);
182 
183  PG_RETURN_BOOL(result);
184 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Definition: c.h:433
Datum citext_ne ( PG_FUNCTION_ARGS  )

Definition at line 142 of file citext.c.

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

143 {
144  text *left = PG_GETARG_TEXT_PP(0);
145  text *right = PG_GETARG_TEXT_PP(1);
146  char *lcstr,
147  *rcstr;
148  bool result;
149 
150  /* We can't compare lengths in advance of downcasing ... */
151 
154 
155  /*
156  * Since we only care about equality or not-equality, we can avoid all the
157  * expense of strcoll() here, and just do bitwise comparison.
158  */
159  result = (strcmp(lcstr, rcstr) != 0);
160 
161  pfree(lcstr);
162  pfree(rcstr);
163  PG_FREE_IF_COPY(left, 0);
164  PG_FREE_IF_COPY(right, 1);
165 
166  PG_RETURN_BOOL(result);
167 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1519
return result
Definition: formatting.c:1633
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:949
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum citext_smaller ( PG_FUNCTION_ARGS  )

Definition at line 246 of file citext.c.

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

247 {
248  text *left = PG_GETARG_TEXT_PP(0);
249  text *right = PG_GETARG_TEXT_PP(1);
250  text *result;
251 
252  result = citextcmp(left, right, PG_GET_COLLATION()) < 0 ? left : right;
253  PG_RETURN_TEXT_P(result);
254 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:34
return result
Definition: formatting.c:1633
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: c.h:433
static int32 citextcmp ( text left,
text right,
Oid  collid 
)
static

Definition at line 34 of file citext.c.

References DEFAULT_COLLATION_OID, pfree(), result, 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().

35 {
36  char *lcstr,
37  *rcstr;
38  int32 result;
39 
40  /*
41  * We must do our str_tolower calls with DEFAULT_COLLATION_OID, not the
42  * input collation as you might expect. This is so that the behavior of
43  * citext's equality and hashing functions is not collation-dependent. We
44  * should change this once the core infrastructure is able to cope with
45  * collation-dependent equality and hashing functions.
46  */
47 
50 
51  result = varstr_cmp(lcstr, strlen(lcstr),
52  rcstr, strlen(rcstr),
53  collid);
54 
55  pfree(lcstr);
56  pfree(rcstr);
57 
58  return result;
59 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1519
return result
Definition: formatting.c:1633
signed int int32
Definition: c.h:246
void pfree(void *pointer)
Definition: mcxt.c:949
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
int varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid)
Definition: varlena.c:1382
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
PG_FUNCTION_INFO_V1 ( citext_cmp  )
PG_FUNCTION_INFO_V1 ( citext_hash  )
PG_FUNCTION_INFO_V1 ( citext_eq  )
PG_FUNCTION_INFO_V1 ( citext_ne  )
PG_FUNCTION_INFO_V1 ( citext_lt  )
PG_FUNCTION_INFO_V1 ( citext_le  )
PG_FUNCTION_INFO_V1 ( citext_gt  )
PG_FUNCTION_INFO_V1 ( citext_ge  )
PG_FUNCTION_INFO_V1 ( citext_smaller  )
PG_FUNCTION_INFO_V1 ( citext_larger  )

Variable Documentation

PG_MODULE_MAGIC

Definition at line 12 of file citext.c.