Skip to content

Commit 806eb92

Browse files
committed
Add an "absval" parameter to allow contrib/dict_int to ignore signs.
Jeff Janes Discussion: https://postgr.es/m/CAMkU=1xRcs_BUPzR0+V3WndaCAv0E_m3h6aUEJ8NF-sY1nnHsw@mail.gmail.com
1 parent 38ce06c commit 806eb92

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

contrib/dict_int/dict_int.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef struct
2121
{
2222
int maxlen;
2323
bool rejectlong;
24+
bool absval;
2425
} DictInt;
2526

2627

@@ -37,6 +38,7 @@ dintdict_init(PG_FUNCTION_ARGS)
3738
d = (DictInt *) palloc0(sizeof(DictInt));
3839
d->maxlen = 6;
3940
d->rejectlong = false;
41+
d->absval = false;
4042

4143
foreach(l, dictoptions)
4244
{
@@ -55,6 +57,10 @@ dintdict_init(PG_FUNCTION_ARGS)
5557
{
5658
d->rejectlong = defGetBoolean(defel);
5759
}
60+
else if (strcmp(defel->defname, "absval") == 0)
61+
{
62+
d->absval = defGetBoolean(defel);
63+
}
5864
else
5965
{
6066
ereport(ERROR,
@@ -72,11 +78,21 @@ dintdict_lexize(PG_FUNCTION_ARGS)
7278
{
7379
DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
7480
char *in = (char *) PG_GETARG_POINTER(1);
75-
char *txt = pnstrdup(in, PG_GETARG_INT32(2));
81+
int len = PG_GETARG_INT32(2);
82+
char *txt;
7683
TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
7784

7885
res[1].lexeme = NULL;
79-
if (PG_GETARG_INT32(2) > d->maxlen)
86+
87+
if (d->absval && (in[0] == '+' || in[0] == '-'))
88+
{
89+
len--;
90+
txt = pnstrdup(in + 1, len);
91+
}
92+
else
93+
txt = pnstrdup(in, len);
94+
95+
if (len > d->maxlen)
8096
{
8197
if (d->rejectlong)
8298
{

contrib/dict_int/expected/dict_int.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,28 @@ select ts_lexize('intdict', '314532610153');
302302

303303
ALTER TEXT SEARCH DICTIONARY intdict (MAXLEN = -214783648);
304304
ERROR: maxlen value has to be >= 1
305+
select ts_lexize('intdict', '-40865854');
306+
ts_lexize
307+
-----------
308+
{-40865}
309+
(1 row)
310+
311+
select ts_lexize('intdict', '+40865854');
312+
ts_lexize
313+
-----------
314+
{+40865}
315+
(1 row)
316+
317+
ALTER TEXT SEARCH DICTIONARY intdict (ABSVAL = true);
318+
select ts_lexize('intdict', '-40865854');
319+
ts_lexize
320+
-----------
321+
{408658}
322+
(1 row)
323+
324+
select ts_lexize('intdict', '+40865854');
325+
ts_lexize
326+
-----------
327+
{408658}
328+
(1 row)
329+

contrib/dict_int/sql/dict_int.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ select ts_lexize('intdict', '641439323669');
5353
select ts_lexize('intdict', '314532610153');
5454

5555
ALTER TEXT SEARCH DICTIONARY intdict (MAXLEN = -214783648);
56+
57+
select ts_lexize('intdict', '-40865854');
58+
select ts_lexize('intdict', '+40865854');
59+
ALTER TEXT SEARCH DICTIONARY intdict (ABSVAL = true);
60+
select ts_lexize('intdict', '-40865854');
61+
select ts_lexize('intdict', '+40865854');

doc/src/sgml/dict-int.sgml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<title>Configuration</title>
2626

2727
<para>
28-
The dictionary accepts two options:
28+
The dictionary accepts three options:
2929
</para>
3030

3131
<itemizedlist>
@@ -46,6 +46,15 @@
4646
such an integer cannot be searched for.
4747
</para>
4848
</listitem>
49+
<listitem>
50+
<para>
51+
The <literal>absval</literal> parameter specifies whether leading
52+
<quote><literal>+</literal></quote> or <quote><literal>-</literal></quote>
53+
signs should be removed from integer words. The default
54+
is <literal>false</literal>. When <literal>true</literal>, the sign is
55+
removed before <literal>maxlen</literal> is applied.
56+
</para>
57+
</listitem>
4958
</itemizedlist>
5059
</sect2>
5160

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