Skip to content

Commit f246499

Browse files
committed
Add citext_pattern_ops for citext contrib module
This is similar to text_pattern_ops. Alexey Chernyshov, reviewed by Jacob Champion.
1 parent f8e5f15 commit f246499

File tree

5 files changed

+1013
-0
lines changed

5 files changed

+1013
-0
lines changed

contrib/citext/citext--1.4--1.5.sql

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,77 @@ ALTER OPERATOR >= (citext, citext) SET (
1212
RESTRICT = scalargesel,
1313
JOIN = scalargejoinsel
1414
);
15+
16+
CREATE FUNCTION citext_pattern_lt( citext, citext )
17+
RETURNS bool
18+
AS 'MODULE_PATHNAME'
19+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
20+
21+
CREATE FUNCTION citext_pattern_le( citext, citext )
22+
RETURNS bool
23+
AS 'MODULE_PATHNAME'
24+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
25+
26+
CREATE FUNCTION citext_pattern_gt( citext, citext )
27+
RETURNS bool
28+
AS 'MODULE_PATHNAME'
29+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
30+
31+
CREATE FUNCTION citext_pattern_ge( citext, citext )
32+
RETURNS bool
33+
AS 'MODULE_PATHNAME'
34+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
35+
36+
CREATE OPERATOR ~<~ (
37+
LEFTARG = CITEXT,
38+
RIGHTARG = CITEXT,
39+
NEGATOR = ~>=~,
40+
COMMUTATOR = ~>~,
41+
PROCEDURE = citext_pattern_lt,
42+
RESTRICT = scalarltsel,
43+
JOIN = scalarltjoinsel
44+
);
45+
46+
CREATE OPERATOR ~<=~ (
47+
LEFTARG = CITEXT,
48+
RIGHTARG = CITEXT,
49+
NEGATOR = ~>~,
50+
COMMUTATOR = ~>=~,
51+
PROCEDURE = citext_pattern_le,
52+
RESTRICT = scalarltsel,
53+
JOIN = scalarltjoinsel
54+
);
55+
56+
CREATE OPERATOR ~>=~ (
57+
LEFTARG = CITEXT,
58+
RIGHTARG = CITEXT,
59+
NEGATOR = ~<~,
60+
COMMUTATOR = ~<=~,
61+
PROCEDURE = citext_pattern_ge,
62+
RESTRICT = scalargtsel,
63+
JOIN = scalargtjoinsel
64+
);
65+
66+
CREATE OPERATOR ~>~ (
67+
LEFTARG = CITEXT,
68+
RIGHTARG = CITEXT,
69+
NEGATOR = ~<=~,
70+
COMMUTATOR = ~<~,
71+
PROCEDURE = citext_pattern_gt,
72+
RESTRICT = scalargtsel,
73+
JOIN = scalargtjoinsel
74+
);
75+
76+
CREATE FUNCTION citext_pattern_cmp(citext, citext)
77+
RETURNS int4
78+
AS 'MODULE_PATHNAME'
79+
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
80+
81+
CREATE OPERATOR CLASS citext_pattern_ops
82+
FOR TYPE CITEXT USING btree AS
83+
OPERATOR 1 ~<~ (citext, citext),
84+
OPERATOR 2 ~<=~ (citext, citext),
85+
OPERATOR 3 = (citext, citext),
86+
OPERATOR 4 ~>=~ (citext, citext),
87+
OPERATOR 5 ~>~ (citext, citext),
88+
FUNCTION 1 citext_pattern_cmp(citext, citext);

contrib/citext/citext.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PG_MODULE_MAGIC;
1818
*/
1919

2020
static int32 citextcmp(text *left, text *right, Oid collid);
21+
static int32 internal_citext_pattern_cmp(text *left, text *right, Oid collid);
2122

2223
/*
2324
* =================
@@ -58,6 +59,41 @@ citextcmp(text *left, text *right, Oid collid)
5859
return result;
5960
}
6061

62+
/*
63+
* citext_pattern_cmp()
64+
* Internal character-by-character comparison function for citext strings.
65+
* Returns int32 negative, zero, or positive.
66+
*/
67+
static int32
68+
internal_citext_pattern_cmp(text *left, text *right, Oid collid)
69+
{
70+
char *lcstr,
71+
*rcstr;
72+
int llen,
73+
rlen;
74+
int32 result;
75+
76+
lcstr = str_tolower(VARDATA_ANY(left), VARSIZE_ANY_EXHDR(left), DEFAULT_COLLATION_OID);
77+
rcstr = str_tolower(VARDATA_ANY(right), VARSIZE_ANY_EXHDR(right), DEFAULT_COLLATION_OID);
78+
79+
llen = strlen(lcstr);
80+
rlen = strlen(rcstr);
81+
82+
result = memcmp((void *) lcstr, (void *) rcstr, Min(llen, rlen));
83+
if (result == 0)
84+
{
85+
if (llen < rlen)
86+
result = -1;
87+
else if (llen > rlen)
88+
result = 1;
89+
}
90+
91+
pfree(lcstr);
92+
pfree(rcstr);
93+
94+
return result;
95+
}
96+
6197
/*
6298
* ==================
6399
* INDEXING FUNCTIONS
@@ -81,6 +117,23 @@ citext_cmp(PG_FUNCTION_ARGS)
81117
PG_RETURN_INT32(result);
82118
}
83119

120+
PG_FUNCTION_INFO_V1(citext_pattern_cmp);
121+
122+
Datum
123+
citext_pattern_cmp(PG_FUNCTION_ARGS)
124+
{
125+
text *left = PG_GETARG_TEXT_PP(0);
126+
text *right = PG_GETARG_TEXT_PP(1);
127+
int32 result;
128+
129+
result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION());
130+
131+
PG_FREE_IF_COPY(left, 0);
132+
PG_FREE_IF_COPY(right, 1);
133+
134+
PG_RETURN_INT32(result);
135+
}
136+
84137
PG_FUNCTION_INFO_V1(citext_hash);
85138

86139
Datum
@@ -234,6 +287,74 @@ citext_ge(PG_FUNCTION_ARGS)
234287
PG_RETURN_BOOL(result);
235288
}
236289

290+
PG_FUNCTION_INFO_V1(citext_pattern_lt);
291+
292+
Datum
293+
citext_pattern_lt(PG_FUNCTION_ARGS)
294+
{
295+
text *left = PG_GETARG_TEXT_PP(0);
296+
text *right = PG_GETARG_TEXT_PP(1);
297+
bool result;
298+
299+
result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) < 0;
300+
301+
PG_FREE_IF_COPY(left, 0);
302+
PG_FREE_IF_COPY(right, 1);
303+
304+
PG_RETURN_BOOL(result);
305+
}
306+
307+
PG_FUNCTION_INFO_V1(citext_pattern_le);
308+
309+
Datum
310+
citext_pattern_le(PG_FUNCTION_ARGS)
311+
{
312+
text *left = PG_GETARG_TEXT_PP(0);
313+
text *right = PG_GETARG_TEXT_PP(1);
314+
bool result;
315+
316+
result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) <= 0;
317+
318+
PG_FREE_IF_COPY(left, 0);
319+
PG_FREE_IF_COPY(right, 1);
320+
321+
PG_RETURN_BOOL(result);
322+
}
323+
324+
PG_FUNCTION_INFO_V1(citext_pattern_gt);
325+
326+
Datum
327+
citext_pattern_gt(PG_FUNCTION_ARGS)
328+
{
329+
text *left = PG_GETARG_TEXT_PP(0);
330+
text *right = PG_GETARG_TEXT_PP(1);
331+
bool result;
332+
333+
result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) > 0;
334+
335+
PG_FREE_IF_COPY(left, 0);
336+
PG_FREE_IF_COPY(right, 1);
337+
338+
PG_RETURN_BOOL(result);
339+
}
340+
341+
PG_FUNCTION_INFO_V1(citext_pattern_ge);
342+
343+
Datum
344+
citext_pattern_ge(PG_FUNCTION_ARGS)
345+
{
346+
text *left = PG_GETARG_TEXT_PP(0);
347+
text *right = PG_GETARG_TEXT_PP(1);
348+
bool result;
349+
350+
result = internal_citext_pattern_cmp(left, right, PG_GET_COLLATION()) >= 0;
351+
352+
PG_FREE_IF_COPY(left, 0);
353+
PG_FREE_IF_COPY(right, 1);
354+
355+
PG_RETURN_BOOL(result);
356+
}
357+
237358
/*
238359
* ===================
239360
* AGGREGATE FUNCTIONS

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy