Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * dict_int.c
4 : * Text search dictionary for integers
5 : *
6 : * Copyright (c) 2007-2026, PostgreSQL Global Development Group
7 : *
8 : * IDENTIFICATION
9 : * contrib/dict_int/dict_int.c
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 : #include "postgres.h"
14 :
15 : #include "commands/defrem.h"
16 : #include "tsearch/ts_public.h"
17 :
18 1 : PG_MODULE_MAGIC_EXT(
19 : .name = "dict_int",
20 : .version = PG_VERSION
21 : );
22 :
23 : typedef struct
24 : {
25 : int maxlen;
26 : bool rejectlong;
27 : bool absval;
28 : } DictInt;
29 :
30 :
31 2 : PG_FUNCTION_INFO_V1(dintdict_init);
32 2 : PG_FUNCTION_INFO_V1(dintdict_lexize);
33 :
34 : Datum
35 9 : dintdict_init(PG_FUNCTION_ARGS)
36 : {
37 9 : List *dictoptions = (List *) PG_GETARG_POINTER(0);
38 : DictInt *d;
39 : ListCell *l;
40 :
41 9 : d = palloc0_object(DictInt);
42 9 : d->maxlen = 6;
43 9 : d->rejectlong = false;
44 9 : d->absval = false;
45 :
46 21 : foreach(l, dictoptions)
47 : {
48 13 : DefElem *defel = (DefElem *) lfirst(l);
49 :
50 13 : if (strcmp(defel->defname, "maxlen") == 0)
51 : {
52 7 : d->maxlen = atoi(defGetString(defel));
53 :
54 7 : if (d->maxlen < 1)
55 1 : ereport(ERROR,
56 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57 : errmsg("maxlen value has to be >= 1")));
58 : }
59 6 : else if (strcmp(defel->defname, "rejectlong") == 0)
60 : {
61 2 : d->rejectlong = defGetBoolean(defel);
62 : }
63 4 : else if (strcmp(defel->defname, "absval") == 0)
64 : {
65 4 : d->absval = defGetBoolean(defel);
66 : }
67 : else
68 : {
69 0 : ereport(ERROR,
70 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71 : errmsg("unrecognized intdict parameter: \"%s\"",
72 : defel->defname)));
73 : }
74 : }
75 :
76 8 : PG_RETURN_POINTER(d);
77 : }
78 :
79 : Datum
80 57 : dintdict_lexize(PG_FUNCTION_ARGS)
81 : {
82 57 : DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
83 57 : char *in = (char *) PG_GETARG_POINTER(1);
84 57 : int len = PG_GETARG_INT32(2);
85 : char *txt;
86 57 : TSLexeme *res = palloc0_array(TSLexeme, 2);
87 :
88 57 : res[1].lexeme = NULL;
89 :
90 57 : if (d->absval && (in[0] == '+' || in[0] == '-'))
91 : {
92 5 : len--;
93 5 : txt = pnstrdup(in + 1, len);
94 : }
95 : else
96 52 : txt = pnstrdup(in, len);
97 :
98 57 : if (len > d->maxlen)
99 : {
100 38 : if (d->rejectlong)
101 : {
102 : /* reject by returning void array */
103 2 : pfree(txt);
104 2 : res[0].lexeme = NULL;
105 : }
106 : else
107 : {
108 : /* trim integer */
109 36 : txt[d->maxlen] = '\0';
110 36 : res[0].lexeme = txt;
111 : }
112 : }
113 : else
114 : {
115 19 : res[0].lexeme = txt;
116 : }
117 :
118 57 : PG_RETURN_POINTER(res);
119 : }
|