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 72 of file citext.c.

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

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

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

115 {
116  text *left = PG_GETARG_TEXT_PP(0);
117  text *right = PG_GETARG_TEXT_PP(1);
118  char *lcstr,
119  *rcstr;
120  bool result;
121 
122  /* We can't compare lengths in advance of downcasing ... */
123 
126 
127  /*
128  * Since we only care about equality or not-equality, we can avoid all the
129  * expense of strcoll() here, and just do bitwise comparison.
130  */
131  result = (strcmp(lcstr, rcstr) == 0);
132 
133  pfree(lcstr);
134  pfree(rcstr);
135  PG_FREE_IF_COPY(left, 0);
136  PG_FREE_IF_COPY(right, 1);
137 
138  PG_RETURN_BOOL(result);
139 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1509
return result
Definition: formatting.c:1618
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:950
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#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:439
Datum citext_ge ( PG_FUNCTION_ARGS  )

Definition at line 225 of file citext.c.

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

226 {
227  text *left = PG_GETARG_TEXT_PP(0);
228  text *right = PG_GETARG_TEXT_PP(1);
229  bool result;
230 
231  result = citextcmp(left, right, PG_GET_COLLATION()) >= 0;
232 
233  PG_FREE_IF_COPY(left, 0);
234  PG_FREE_IF_COPY(right, 1);
235 
236  PG_RETURN_BOOL(result);
237 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:439
Datum citext_gt ( PG_FUNCTION_ARGS  )

Definition at line 208 of file citext.c.

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

209 {
210  text *left = PG_GETARG_TEXT_PP(0);
211  text *right = PG_GETARG_TEXT_PP(1);
212  bool result;
213 
214  result = citextcmp(left, right, PG_GET_COLLATION()) > 0;
215 
216  PG_FREE_IF_COPY(left, 0);
217  PG_FREE_IF_COPY(right, 1);
218 
219  PG_RETURN_BOOL(result);
220 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:439
Datum citext_hash ( PG_FUNCTION_ARGS  )

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

90 {
91  text *txt = PG_GETARG_TEXT_PP(0);
92  char *str;
93  Datum result;
94 
96  result = hash_any((unsigned char *) str, strlen(str));
97  pfree(str);
98 
99  /* Avoid leaking memory for toasted inputs */
100  PG_FREE_IF_COPY(txt, 0);
101 
102  PG_RETURN_DATUM(result);
103 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1509
return result
Definition: formatting.c:1618
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:950
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
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:307
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum citext_larger ( PG_FUNCTION_ARGS  )

Definition at line 261 of file citext.c.

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

262 {
263  text *left = PG_GETARG_TEXT_PP(0);
264  text *right = PG_GETARG_TEXT_PP(1);
265  text *result;
266 
267  result = citextcmp(left, right, PG_GET_COLLATION()) > 0 ? left : right;
268  PG_RETURN_TEXT_P(result);
269 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:330
Definition: c.h:439
Datum citext_le ( PG_FUNCTION_ARGS  )

Definition at line 191 of file citext.c.

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

192 {
193  text *left = PG_GETARG_TEXT_PP(0);
194  text *right = PG_GETARG_TEXT_PP(1);
195  bool result;
196 
197  result = citextcmp(left, right, PG_GET_COLLATION()) <= 0;
198 
199  PG_FREE_IF_COPY(left, 0);
200  PG_FREE_IF_COPY(right, 1);
201 
202  PG_RETURN_BOOL(result);
203 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:439
Datum citext_lt ( PG_FUNCTION_ARGS  )

Definition at line 174 of file citext.c.

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

175 {
176  text *left = PG_GETARG_TEXT_PP(0);
177  text *right = PG_GETARG_TEXT_PP(1);
178  bool result;
179 
180  result = citextcmp(left, right, PG_GET_COLLATION()) < 0;
181 
182  PG_FREE_IF_COPY(left, 0);
183  PG_FREE_IF_COPY(right, 1);
184 
185  PG_RETURN_BOOL(result);
186 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:439
Datum citext_ne ( PG_FUNCTION_ARGS  )

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

145 {
146  text *left = PG_GETARG_TEXT_PP(0);
147  text *right = PG_GETARG_TEXT_PP(1);
148  char *lcstr,
149  *rcstr;
150  bool result;
151 
152  /* We can't compare lengths in advance of downcasing ... */
153 
156 
157  /*
158  * Since we only care about equality or not-equality, we can avoid all the
159  * expense of strcoll() here, and just do bitwise comparison.
160  */
161  result = (strcmp(lcstr, rcstr) != 0);
162 
163  pfree(lcstr);
164  pfree(rcstr);
165  PG_FREE_IF_COPY(left, 0);
166  PG_FREE_IF_COPY(right, 1);
167 
168  PG_RETURN_BOOL(result);
169 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1509
return result
Definition: formatting.c:1618
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:950
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
#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:439
Datum citext_smaller ( PG_FUNCTION_ARGS  )

Definition at line 248 of file citext.c.

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

249 {
250  text *left = PG_GETARG_TEXT_PP(0);
251  text *right = PG_GETARG_TEXT_PP(1);
252  text *result;
253 
254  result = citextcmp(left, right, PG_GET_COLLATION()) < 0 ? left : right;
255  PG_RETURN_TEXT_P(result);
256 }
static int32 citextcmp(text *left, text *right, Oid collid)
Definition: citext.c:36
return result
Definition: formatting.c:1618
#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:330
Definition: c.h:439
static int32 citextcmp ( text left,
text right,
Oid  collid 
)
static

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

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 
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 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1509
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
void pfree(void *pointer)
Definition: mcxt.c:950
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
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 13 of file citext.c.