Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * spell.c
4 : : * Normalizing word with ISpell
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : *
8 : : * Ispell dictionary
9 : : * -----------------
10 : : *
11 : : * Rules of dictionaries are defined in two files with .affix and .dict
12 : : * extensions. They are used by spell checker programs Ispell and Hunspell.
13 : : *
14 : : * An .affix file declares morphological rules to get a basic form of words.
15 : : * The format of an .affix file has different structure for Ispell and Hunspell
16 : : * dictionaries. The Hunspell format is more complicated. But when an .affix
17 : : * file is imported and compiled, it is stored in the same structure AffixNode.
18 : : *
19 : : * A .dict file stores a list of basic forms of words with references to
20 : : * affix rules. The format of a .dict file has the same structure for Ispell
21 : : * and Hunspell dictionaries.
22 : : *
23 : : * Compilation of a dictionary
24 : : * ---------------------------
25 : : *
26 : : * A compiled dictionary is stored in the IspellDict structure. Compilation of
27 : : * a dictionary is divided into the several steps:
28 : : * - NIImportDictionary() - stores each word of a .dict file in the
29 : : * temporary Spell field.
30 : : * - NIImportAffixes() - stores affix rules of an .affix file in the
31 : : * Affix field (not temporary) if an .affix file has the Ispell format.
32 : : * -> NIImportOOAffixes() - stores affix rules if an .affix file has the
33 : : * Hunspell format. The AffixData field is initialized if AF parameter
34 : : * is defined.
35 : : * - NISortDictionary() - builds a prefix tree (Trie) from the words list
36 : : * and stores it in the Dictionary field. The words list is got from the
37 : : * Spell field. The AffixData field is initialized if AF parameter is not
38 : : * defined.
39 : : * - NISortAffixes():
40 : : * - builds a list of compound affixes from the affix list and stores it
41 : : * in the CompoundAffix.
42 : : * - builds prefix trees (Trie) from the affix list for prefixes and suffixes
43 : : * and stores them in Suffix and Prefix fields.
44 : : * The affix list is got from the Affix field.
45 : : *
46 : : * Memory management
47 : : * -----------------
48 : : *
49 : : * The IspellDict structure has the Spell field which is used only in compile
50 : : * time. The Spell field stores a words list. It can take a lot of memory.
51 : : * Therefore when a dictionary is compiled this field is cleared by
52 : : * NIFinishBuild().
53 : : *
54 : : * All resources which should cleared by NIFinishBuild() is initialized using
55 : : * tmpalloc() and tmpalloc0().
56 : : *
57 : : * IDENTIFICATION
58 : : * src/backend/tsearch/spell.c
59 : : *
60 : : *-------------------------------------------------------------------------
61 : : */
62 : :
63 : : #include "postgres.h"
64 : :
65 : : #include "catalog/pg_collation.h"
66 : : #include "miscadmin.h"
67 : : #include "tsearch/dicts/spell.h"
68 : : #include "tsearch/ts_locale.h"
69 : : #include "utils/formatting.h"
70 : : #include "utils/memutils.h"
71 : :
72 : :
73 : : /*
74 : : * Initialization requires a lot of memory that's not needed
75 : : * after the initialization is done. During initialization,
76 : : * CurrentMemoryContext is the long-lived memory context associated
77 : : * with the dictionary cache entry. We keep the short-lived stuff
78 : : * in the Conf->buildCxt context.
79 : : */
80 : : #define tmpalloc(sz) MemoryContextAlloc(Conf->buildCxt, (sz))
81 : : #define tmpalloc0(sz) MemoryContextAllocZero(Conf->buildCxt, (sz))
82 : :
83 : : /*
84 : : * Prepare for constructing an ISpell dictionary.
85 : : *
86 : : * The IspellDict struct is assumed to be zeroed when allocated.
87 : : */
88 : : void
5449 tgl@sss.pgh.pa.us 89 :CBC 67 : NIStartBuild(IspellDict *Conf)
90 : : {
91 : : /*
92 : : * The temp context is a child of CurTransactionContext, so that it will
93 : : * go away automatically on error.
94 : : */
95 : 67 : Conf->buildCxt = AllocSetContextCreate(CurTransactionContext,
96 : : "Ispell dictionary init context",
97 : : ALLOCSET_DEFAULT_SIZES);
6591 98 : 67 : }
99 : :
100 : : /*
101 : : * Clean up when dictionary construction is complete.
102 : : */
103 : : void
5449 104 : 55 : NIFinishBuild(IspellDict *Conf)
105 : : {
106 : : /* Release no-longer-needed temp memory */
107 : 55 : MemoryContextDelete(Conf->buildCxt);
108 : : /* Just for cleanliness, zero the now-dangling pointers */
109 : 55 : Conf->buildCxt = NULL;
110 : 55 : Conf->Spell = NULL;
111 : 55 : Conf->firstfree = NULL;
3460 teodor@sigaev.ru 112 : 55 : Conf->CompoundAffixFlags = NULL;
5449 tgl@sss.pgh.pa.us 113 : 55 : }
114 : :
115 : :
116 : : /*
117 : : * "Compact" palloc: allocate without extra palloc overhead.
118 : : *
119 : : * Since we have no need to free the ispell data items individually, there's
120 : : * not much value in the per-chunk overhead normally consumed by palloc.
121 : : * Getting rid of it is helpful since ispell can allocate a lot of small nodes.
122 : : *
123 : : * We currently pre-zero all data allocated this way, even though some of it
124 : : * doesn't need that. The cpalloc and cpalloc0 macros are just documentation
125 : : * to indicate which allocations actually require zeroing.
126 : : */
127 : : #define COMPACT_ALLOC_CHUNK 8192 /* amount to get from palloc at once */
128 : : #define COMPACT_MAX_REQ 1024 /* must be < COMPACT_ALLOC_CHUNK */
129 : :
130 : : static void *
131 : 6202 : compact_palloc0(IspellDict *Conf, size_t size)
132 : : {
133 : : void *result;
134 : :
135 : : /* Should only be called during init */
136 [ - + ]: 6202 : Assert(Conf->buildCxt != NULL);
137 : :
138 : : /* No point in this for large chunks */
139 [ - + ]: 6202 : if (size > COMPACT_MAX_REQ)
5449 tgl@sss.pgh.pa.us 140 :UBC 0 : return palloc0(size);
141 : :
142 : : /* Keep everything maxaligned */
5449 tgl@sss.pgh.pa.us 143 :CBC 6202 : size = MAXALIGN(size);
144 : :
145 : : /* Need more space? */
146 [ + + ]: 6202 : if (size > Conf->avail)
147 : : {
148 : 64 : Conf->firstfree = palloc0(COMPACT_ALLOC_CHUNK);
149 : 64 : Conf->avail = COMPACT_ALLOC_CHUNK;
150 : : }
151 : :
282 peter@eisentraut.org 152 : 6202 : result = Conf->firstfree;
5449 tgl@sss.pgh.pa.us 153 : 6202 : Conf->firstfree += size;
154 : 6202 : Conf->avail -= size;
155 : :
156 : 6202 : return result;
157 : : }
158 : :
159 : : #define cpalloc(size) compact_palloc0(Conf, size)
160 : : #define cpalloc0(size) compact_palloc0(Conf, size)
161 : :
162 : : static char *
163 : 3312 : cpstrdup(IspellDict *Conf, const char *str)
164 : : {
165 : 3312 : char *res = cpalloc(strlen(str) + 1);
166 : :
167 : 3312 : strcpy(res, str);
168 : 3312 : return res;
169 : : }
170 : :
171 : :
172 : : /*
173 : : * Apply str_tolower(), producing a temporary result (in the buildCxt).
174 : : */
175 : : static char *
176 : 2873 : lowerstr_ctx(IspellDict *Conf, const char *src)
177 : : {
178 : : MemoryContext saveCtx;
179 : : char *dst;
180 : :
181 : 2873 : saveCtx = MemoryContextSwitchTo(Conf->buildCxt);
263 peter@eisentraut.org 182 : 2873 : dst = str_tolower(src, strlen(src), DEFAULT_COLLATION_OID);
6591 tgl@sss.pgh.pa.us 183 : 2873 : MemoryContextSwitchTo(saveCtx);
184 : :
185 : 2873 : return dst;
186 : : }
187 : :
188 : : #define MAX_NORM 1024
189 : : #define MAXNORMLEN 256
190 : :
191 : : #define STRNCMP(s,p) strncmp( (s), (p), strlen(p) )
192 : : #define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
193 : : #define GETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T )
194 : :
195 : : static const char *VoidString = "";
196 : :
197 : : static int
198 : 1446 : cmpspell(const void *s1, const void *s2)
199 : : {
2942 peter_e@gmx.net 200 : 1446 : return strcmp((*(SPELL *const *) s1)->word, (*(SPELL *const *) s2)->word);
201 : : }
202 : :
203 : : static int
6591 tgl@sss.pgh.pa.us 204 : 1128 : cmpspellaffix(const void *s1, const void *s2)
205 : : {
2942 peter_e@gmx.net 206 : 2256 : return strcmp((*(SPELL *const *) s1)->p.flag,
2838 rhaas@postgresql.org 207 : 1128 : (*(SPELL *const *) s2)->p.flag);
208 : : }
209 : :
210 : : static int
3460 teodor@sigaev.ru 211 : 1962 : cmpcmdflag(const void *f1, const void *f2)
212 : : {
3376 rhaas@postgresql.org 213 : 1962 : CompoundAffixFlag *fv1 = (CompoundAffixFlag *) f1,
214 : 1962 : *fv2 = (CompoundAffixFlag *) f2;
215 : :
3460 teodor@sigaev.ru 216 [ - + ]: 1962 : Assert(fv1->flagMode == fv2->flagMode);
217 : :
218 [ + + ]: 1962 : if (fv1->flagMode == FM_NUM)
219 : : {
220 [ + + ]: 380 : if (fv1->flag.i == fv2->flag.i)
221 : 57 : return 0;
222 : :
223 [ + + ]: 323 : return (fv1->flag.i > fv2->flag.i) ? 1 : -1;
224 : : }
225 : :
226 : 1582 : return strcmp(fv1->flag.s, fv2->flag.s);
227 : : }
228 : :
229 : : static char *
6591 tgl@sss.pgh.pa.us 230 : 583 : findchar(char *str, int c)
231 : : {
232 [ + + ]: 4295 : while (*str)
233 : : {
234 [ + + ]: 4231 : if (t_iseq(str, c))
235 : 519 : return str;
236 : 3712 : str += pg_mblen(str);
237 : : }
238 : :
239 : 64 : return NULL;
240 : : }
241 : :
242 : : static char *
3471 243 : 21 : findchar2(char *str, int c1, int c2)
244 : : {
245 [ + - ]: 441 : while (*str)
246 : : {
247 [ + + - + ]: 441 : if (t_iseq(str, c1) || t_iseq(str, c2))
248 : 21 : return str;
249 : 420 : str += pg_mblen(str);
250 : : }
251 : :
3471 tgl@sss.pgh.pa.us 252 :UBC 0 : return NULL;
253 : : }
254 : :
255 : :
256 : : /* backward string compare for suffix tree operations */
257 : : static int
6591 tgl@sss.pgh.pa.us 258 :CBC 577 : strbcmp(const unsigned char *s1, const unsigned char *s2)
259 : : {
260 : 577 : int l1 = strlen((const char *) s1) - 1,
261 : 577 : l2 = strlen((const char *) s2) - 1;
262 : :
263 [ + + + + ]: 772 : while (l1 >= 0 && l2 >= 0)
264 : : {
265 [ + + ]: 604 : if (s1[l1] < s2[l2])
266 : 131 : return -1;
267 [ + + ]: 473 : if (s1[l1] > s2[l2])
268 : 278 : return 1;
269 : 195 : l1--;
270 : 195 : l2--;
271 : : }
272 [ + + ]: 168 : if (l1 < l2)
273 : 45 : return -1;
274 [ + + ]: 123 : if (l1 > l2)
275 : 103 : return 1;
276 : :
277 : 20 : return 0;
278 : : }
279 : :
280 : : static int
281 : 20 : strbncmp(const unsigned char *s1, const unsigned char *s2, size_t count)
282 : : {
283 : 20 : int l1 = strlen((const char *) s1) - 1,
284 : 20 : l2 = strlen((const char *) s2) - 1,
285 : 20 : l = count;
286 : :
287 [ + + + - : 30 : while (l1 >= 0 && l2 >= 0 && l > 0)
+ - ]
288 : : {
289 [ + + ]: 20 : if (s1[l1] < s2[l2])
290 : 10 : return -1;
291 [ - + ]: 10 : if (s1[l1] > s2[l2])
6591 tgl@sss.pgh.pa.us 292 :UBC 0 : return 1;
6591 tgl@sss.pgh.pa.us 293 :CBC 10 : l1--;
294 : 10 : l2--;
295 : 10 : l--;
296 : : }
297 [ + - ]: 10 : if (l == 0)
298 : 10 : return 0;
6591 tgl@sss.pgh.pa.us 299 [ # # ]:UBC 0 : if (l1 < l2)
300 : 0 : return -1;
301 [ # # ]: 0 : if (l1 > l2)
302 : 0 : return 1;
303 : 0 : return 0;
304 : : }
305 : :
306 : : /*
307 : : * Compares affixes.
308 : : * First compares the type of an affix. Prefixes should go before affixes.
309 : : * If types are equal then compares replaceable string.
310 : : */
311 : : static int
6591 tgl@sss.pgh.pa.us 312 :CBC 976 : cmpaffix(const void *s1, const void *s2)
313 : : {
314 : 976 : const AFFIX *a1 = (const AFFIX *) s1;
315 : 976 : const AFFIX *a2 = (const AFFIX *) s2;
316 : :
317 [ + + ]: 976 : if (a1->type < a2->type)
318 : 223 : return -1;
319 [ + + ]: 753 : if (a1->type > a2->type)
320 : 66 : return 1;
321 [ + + ]: 687 : if (a1->type == FF_PREFIX)
322 : 110 : return strcmp(a1->repl, a2->repl);
323 : : else
324 : 577 : return strbcmp((const unsigned char *) a1->repl,
325 : 577 : (const unsigned char *) a2->repl);
326 : : }
327 : :
328 : : /*
329 : : * Gets an affix flag from the set of affix flags (sflagset).
330 : : *
331 : : * Several flags can be stored in a single string. Flags can be represented by:
332 : : * - 1 character (FM_CHAR). A character may be Unicode.
333 : : * - 2 characters (FM_LONG). A character may be Unicode.
334 : : * - numbers from 1 to 65000 (FM_NUM).
335 : : *
336 : : * Depending on the flagMode an affix string can have the following format:
337 : : * - FM_CHAR: ABCD
338 : : * Here we have 4 flags: A, B, C and D
339 : : * - FM_LONG: ABCDE*
340 : : * Here we have 3 flags: AB, CD and E*
341 : : * - FM_NUM: 200,205,50
342 : : * Here we have 3 flags: 200, 205 and 50
343 : : *
344 : : * Conf: current dictionary.
345 : : * sflagset: the set of affix flags. Returns a reference to the start of a next
346 : : * affix flag.
347 : : * sflag: returns an affix flag from sflagset.
348 : : */
349 : : static void
396 heikki.linnakangas@i 350 : 3010 : getNextFlagFromString(IspellDict *Conf, const char **sflagset, char *sflag)
351 : : {
352 : : int32 s;
353 : : char *next;
354 : 3010 : const char *sbuf = *sflagset;
355 : : int maxstep;
3460 teodor@sigaev.ru 356 : 3010 : bool stop = false;
357 : 3010 : bool met_comma = false;
358 : :
359 [ + + ]: 3010 : maxstep = (Conf->flagMode == FM_LONG) ? 2 : 1;
360 : :
3376 rhaas@postgresql.org 361 [ + - ]: 3943 : while (**sflagset)
362 : : {
3460 teodor@sigaev.ru 363 [ + + - ]: 3943 : switch (Conf->flagMode)
364 : : {
365 : 3374 : case FM_LONG:
366 : : case FM_CHAR:
367 : 3374 : COPYCHAR(sflag, *sflagset);
368 : 3374 : sflag += pg_mblen(*sflagset);
369 : :
370 : : /* Go to start of the next flag */
371 : 3374 : *sflagset += pg_mblen(*sflagset);
372 : :
373 : : /* Check if we get all characters of flag */
374 : 3374 : maxstep--;
375 : 3374 : stop = (maxstep == 0);
376 : 3374 : break;
377 : 569 : case FM_NUM:
182 tgl@sss.pgh.pa.us 378 : 569 : errno = 0;
3460 teodor@sigaev.ru 379 : 569 : s = strtol(*sflagset, &next, 10);
380 [ + + - + ]: 569 : if (*sflagset == next || errno == ERANGE)
381 [ + - ]: 3 : ereport(ERROR,
382 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
383 : : errmsg("invalid affix flag \"%s\"", *sflagset)));
384 [ + - - + ]: 566 : if (s < 0 || s > FLAGNUM_MAXSIZE)
3460 teodor@sigaev.ru 385 [ # # ]:UBC 0 : ereport(ERROR,
386 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
387 : : errmsg("affix flag \"%s\" is out of range",
388 : : *sflagset)));
3460 teodor@sigaev.ru 389 :CBC 566 : sflag += sprintf(sflag, "%0d", s);
390 : :
391 : : /* Go to start of the next flag */
392 : 566 : *sflagset = next;
393 [ + + ]: 868 : while (**sflagset)
394 : : {
263 peter@eisentraut.org 395 [ + + ]: 604 : if (isdigit((unsigned char) **sflagset))
396 : : {
3460 teodor@sigaev.ru 397 [ - + ]: 302 : if (!met_comma)
3473 teodor@sigaev.ru 398 [ # # ]:UBC 0 : ereport(ERROR,
399 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
400 : : errmsg("invalid affix flag \"%s\"",
401 : : *sflagset)));
3460 teodor@sigaev.ru 402 :CBC 302 : break;
403 : : }
404 [ + - ]: 302 : else if (t_iseq(*sflagset, ','))
405 : : {
406 [ - + ]: 302 : if (met_comma)
3473 teodor@sigaev.ru 407 [ # # ]:UBC 0 : ereport(ERROR,
408 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
409 : : errmsg("invalid affix flag \"%s\"",
410 : : *sflagset)));
3460 teodor@sigaev.ru 411 :CBC 302 : met_comma = true;
412 : : }
263 peter@eisentraut.org 413 [ # # ]:UBC 0 : else if (!isspace((unsigned char) **sflagset))
414 : : {
3460 teodor@sigaev.ru 415 [ # # ]: 0 : ereport(ERROR,
416 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
417 : : errmsg("invalid character in affix flag \"%s\"",
418 : : *sflagset)));
419 : : }
420 : :
3460 teodor@sigaev.ru 421 :CBC 302 : *sflagset += pg_mblen(*sflagset);
422 : : }
423 : 566 : stop = true;
424 : 566 : break;
3460 teodor@sigaev.ru 425 :UBC 0 : default:
426 [ # # ]: 0 : elog(ERROR, "unrecognized type of Conf->flagMode: %d",
427 : : Conf->flagMode);
428 : : }
429 : :
3460 teodor@sigaev.ru 430 [ + + ]:CBC 3940 : if (stop)
3473 431 : 3007 : break;
432 : : }
433 : :
3460 434 [ + + - + ]: 3007 : if (Conf->flagMode == FM_LONG && maxstep > 0)
3460 teodor@sigaev.ru 435 [ # # ]:UBC 0 : ereport(ERROR,
436 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
437 : : errmsg("invalid affix flag \"%s\" with \"long\" flag value",
438 : : sbuf)));
439 : :
3460 teodor@sigaev.ru 440 :CBC 3007 : *sflag = '\0';
3473 441 : 3007 : }
442 : :
443 : : /*
444 : : * Checks if the affix set Conf->AffixData[affix] contains affixflag.
445 : : * Conf->AffixData[affix] does not contain affixflag if this flag is not used
446 : : * actually by the .dict file.
447 : : *
448 : : * Conf: current dictionary.
449 : : * affix: index of the Conf->AffixData array.
450 : : * affixflag: the affix flag.
451 : : *
452 : : * Returns true if the string Conf->AffixData[affix] contains affixflag,
453 : : * otherwise returns false.
454 : : */
455 : : static bool
2867 peter_e@gmx.net 456 : 1112 : IsAffixFlagInUse(IspellDict *Conf, int affix, const char *affixflag)
457 : : {
458 : : const char *flagcur;
459 : : char flag[BUFSIZ];
460 : :
3460 teodor@sigaev.ru 461 [ + + ]: 1112 : if (*affixflag == 0)
3473 462 : 318 : return true;
463 : :
2135 tgl@sss.pgh.pa.us 464 [ - + ]: 794 : Assert(affix < Conf->nAffixData);
465 : :
3473 teodor@sigaev.ru 466 : 794 : flagcur = Conf->AffixData[affix];
467 : :
468 [ + + ]: 2295 : while (*flagcur)
469 : : {
3460 470 : 1750 : getNextFlagFromString(Conf, &flagcur, flag);
471 : : /* Compare first affix flag in flagcur with affixflag */
472 [ + + ]: 1750 : if (strcmp(flag, affixflag) == 0)
3473 473 : 249 : return true;
474 : : }
475 : :
476 : : /* Could not find affixflag */
477 : 545 : return false;
478 : : }
479 : :
480 : : /*
481 : : * Adds the new word into the temporary array Spell.
482 : : *
483 : : * Conf: current dictionary.
484 : : * word: new word.
485 : : * flag: set of affix flags. Single flag can be get by getNextFlagFromString().
486 : : */
487 : : static void
6505 bruce@momjian.us 488 : 583 : NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
489 : : {
6591 tgl@sss.pgh.pa.us 490 [ + + ]: 583 : if (Conf->nspell >= Conf->mspell)
491 : : {
492 [ - + ]: 64 : if (Conf->mspell)
493 : : {
5449 tgl@sss.pgh.pa.us 494 :UBC 0 : Conf->mspell *= 2;
6591 495 : 0 : Conf->Spell = (SPELL **) repalloc(Conf->Spell, Conf->mspell * sizeof(SPELL *));
496 : : }
497 : : else
498 : : {
6591 tgl@sss.pgh.pa.us 499 :CBC 64 : Conf->mspell = 1024 * 20;
500 : 64 : Conf->Spell = (SPELL **) tmpalloc(Conf->mspell * sizeof(SPELL *));
501 : : }
502 : : }
503 : 583 : Conf->Spell[Conf->nspell] = (SPELL *) tmpalloc(SPELLHDRSZ + strlen(word) + 1);
504 : 583 : strcpy(Conf->Spell[Conf->nspell]->word, word);
3473 teodor@sigaev.ru 505 : 1166 : Conf->Spell[Conf->nspell]->p.flag = (*flag != '\0')
506 [ + + ]: 583 : ? cpstrdup(Conf, flag) : VoidString;
6591 tgl@sss.pgh.pa.us 507 : 583 : Conf->nspell++;
508 : 583 : }
509 : :
510 : : /*
511 : : * Imports dictionary into the temporary array Spell.
512 : : *
513 : : * Note caller must already have applied get_tsearch_config_filename.
514 : : *
515 : : * Conf: current dictionary.
516 : : * filename: path to the .dict file.
517 : : */
518 : : void
6505 bruce@momjian.us 519 : 64 : NIImportDictionary(IspellDict *Conf, const char *filename)
520 : : {
521 : : tsearch_readline_state trst;
522 : : char *line;
523 : :
6289 tgl@sss.pgh.pa.us 524 [ - + ]: 64 : if (!tsearch_readline_begin(&trst, filename))
6591 tgl@sss.pgh.pa.us 525 [ # # ]:UBC 0 : ereport(ERROR,
526 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
527 : : errmsg("could not open dictionary file \"%s\": %m",
528 : : filename)));
529 : :
6289 tgl@sss.pgh.pa.us 530 [ + + ]:CBC 647 : while ((line = tsearch_readline(&trst)) != NULL)
531 : : {
532 : : char *s,
533 : : *pstr;
534 : :
535 : : /* Set of affix flags */
536 : : const char *flag;
537 : :
538 : : /* Extract flag from the line */
6591 539 : 583 : flag = NULL;
6587 540 [ + + ]: 583 : if ((s = findchar(line, '/')))
541 : : {
6591 542 : 519 : *s++ = '\0';
543 : 519 : flag = s;
544 [ + - ]: 2075 : while (*s)
545 : : {
546 : : /* we allow only single encoded flags for faster works */
263 peter@eisentraut.org 547 [ + - + + : 2075 : if (pg_mblen(s) == 1 && isprint((unsigned char) *s) && !isspace((unsigned char) *s))
+ - ]
6591 tgl@sss.pgh.pa.us 548 : 1556 : s++;
549 : : else
550 : : {
551 : 519 : *s = '\0';
552 : 519 : break;
553 : : }
554 : : }
555 : : }
556 : : else
557 : 64 : flag = "";
558 : :
559 : : /* Remove trailing spaces */
6587 560 : 583 : s = line;
6591 561 [ + + ]: 4231 : while (*s)
562 : : {
263 peter@eisentraut.org 563 [ + + ]: 3712 : if (isspace((unsigned char) *s))
564 : : {
6591 tgl@sss.pgh.pa.us 565 : 64 : *s = '\0';
566 : 64 : break;
567 : : }
568 : 3648 : s += pg_mblen(s);
569 : : }
5449 570 : 583 : pstr = lowerstr_ctx(Conf, line);
571 : :
6591 572 : 583 : NIAddSpell(Conf, pstr, flag);
573 : 583 : pfree(pstr);
574 : :
6587 575 : 583 : pfree(line);
576 : : }
6289 577 : 64 : tsearch_readline_end(&trst);
6591 578 : 64 : }
579 : :
580 : : /*
581 : : * Searches a basic form of word in the prefix tree. This word was generated
582 : : * using an affix rule. This rule may not be presented in an affix set of
583 : : * a basic form of word.
584 : : *
585 : : * For example, we have the entry in the .dict file:
586 : : * meter/GMD
587 : : *
588 : : * The affix rule with the flag S:
589 : : * SFX S y ies [^aeiou]y
590 : : * is not presented here.
591 : : *
592 : : * The affix rule with the flag M:
593 : : * SFX M 0 's .
594 : : * is presented here.
595 : : *
596 : : * Conf: current dictionary.
597 : : * word: basic form of word.
598 : : * affixflag: affix flag, by which a basic form of word was generated.
599 : : * flag: compound flag used to compare with StopMiddle->compoundflag.
600 : : *
601 : : * Returns 1 if the word was found in the prefix tree, else returns 0.
602 : : */
603 : : static int
2867 peter_e@gmx.net 604 : 1497 : FindWord(IspellDict *Conf, const char *word, const char *affixflag, int flag)
605 : : {
6591 tgl@sss.pgh.pa.us 606 : 1497 : SPNode *node = Conf->Dictionary;
607 : : SPNodeData *StopLow,
608 : : *StopHigh,
609 : : *StopMiddle;
5109 peter_e@gmx.net 610 : 1497 : const uint8 *ptr = (const uint8 *) word;
611 : :
3460 teodor@sigaev.ru 612 : 1497 : flag &= FF_COMPOUNDFLAGMASK;
613 : :
6591 tgl@sss.pgh.pa.us 614 [ + + + + ]: 6972 : while (node && *ptr)
615 : : {
616 : 6612 : StopLow = node->data;
617 : 6612 : StopHigh = node->data + node->length;
618 [ + + ]: 9459 : while (StopLow < StopHigh)
619 : : {
620 : 8826 : StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
621 [ + + ]: 8826 : if (StopMiddle->val == *ptr)
622 : : {
623 [ + + + + ]: 5979 : if (*(ptr + 1) == '\0' && StopMiddle->isword)
624 : : {
625 [ + + ]: 573 : if (flag == 0)
626 : : {
627 : : /*
628 : : * The word can be formed only with another word. And
629 : : * in the flag parameter there is not a sign that we
630 : : * search compound words.
631 : : */
632 [ - + ]: 363 : if (StopMiddle->compoundflag & FF_COMPOUNDONLY)
6591 tgl@sss.pgh.pa.us 633 :UBC 0 : return 0;
634 : : }
6591 tgl@sss.pgh.pa.us 635 [ - + ]:CBC 210 : else if ((flag & StopMiddle->compoundflag) == 0)
6591 tgl@sss.pgh.pa.us 636 :UBC 0 : return 0;
637 : :
638 : : /*
639 : : * Check if this affix rule is presented in the affix set
640 : : * with index StopMiddle->affix.
641 : : */
3473 teodor@sigaev.ru 642 [ + + ]:CBC 573 : if (IsAffixFlagInUse(Conf, StopMiddle->affix, affixflag))
6591 tgl@sss.pgh.pa.us 643 : 504 : return 1;
644 : : }
645 : 5475 : node = StopMiddle->node;
646 : 5475 : ptr++;
647 : 5475 : break;
648 : : }
649 [ + + ]: 2847 : else if (StopMiddle->val < *ptr)
650 : 966 : StopLow = StopMiddle + 1;
651 : : else
652 : 1881 : StopHigh = StopMiddle;
653 : : }
654 [ + + ]: 6108 : if (StopLow >= StopHigh)
655 : 633 : break;
656 : : }
657 : 993 : return 0;
658 : : }
659 : :
660 : : /*
661 : : * Adds a new affix rule to the Affix field.
662 : : *
663 : : * Conf: current dictionary.
664 : : * flag: affix flag ('\' in the below example).
665 : : * flagflags: set of flags from the flagval field for this affix rule. This set
666 : : * is listed after '/' character in the added string (repl).
667 : : *
668 : : * For example L flag in the hunspell_sample.affix:
669 : : * SFX \ 0 Y/L [^Y]
670 : : *
671 : : * mask: condition for search ('[^Y]' in the above example).
672 : : * find: stripping characters from beginning (at prefix) or end (at suffix)
673 : : * of the word ('0' in the above example, 0 means that there is not
674 : : * stripping character).
675 : : * repl: adding string after stripping ('Y' in the above example).
676 : : * type: FF_SUFFIX or FF_PREFIX.
677 : : */
678 : : static void
3376 rhaas@postgresql.org 679 : 530 : NIAddAffix(IspellDict *Conf, const char *flag, char flagflags, const char *mask,
680 : : const char *find, const char *repl, int type)
681 : : {
682 : : AFFIX *Affix;
683 : :
6591 tgl@sss.pgh.pa.us 684 [ + + ]: 530 : if (Conf->naffixes >= Conf->maffixes)
685 : : {
686 [ - + ]: 64 : if (Conf->maffixes)
687 : : {
5449 tgl@sss.pgh.pa.us 688 :UBC 0 : Conf->maffixes *= 2;
942 peter@eisentraut.org 689 : 0 : Conf->Affix = (AFFIX *) repalloc(Conf->Affix, Conf->maffixes * sizeof(AFFIX));
690 : : }
691 : : else
692 : : {
6591 tgl@sss.pgh.pa.us 693 :CBC 64 : Conf->maffixes = 16;
694 : 64 : Conf->Affix = (AFFIX *) palloc(Conf->maffixes * sizeof(AFFIX));
695 : : }
696 : : }
697 : :
698 : 530 : Affix = Conf->Affix + Conf->naffixes;
699 : :
700 : : /* This affix rule can be applied for words with any ending */
3473 teodor@sigaev.ru 701 [ + + - + ]: 530 : if (strcmp(mask, ".") == 0 || *mask == '\0')
702 : : {
6591 tgl@sss.pgh.pa.us 703 : 128 : Affix->issimple = 1;
704 : 128 : Affix->isregis = 0;
705 : : }
706 : : /* This affix rule will use regis to search word ending */
707 [ + + ]: 402 : else if (RS_isRegis(mask))
708 : : {
709 : 336 : Affix->issimple = 0;
710 : 336 : Affix->isregis = 1;
3473 teodor@sigaev.ru 711 : 336 : RS_compile(&(Affix->reg.regis), (type == FF_SUFFIX),
4205 sfrost@snowman.net 712 [ + - ]: 336 : *mask ? mask : VoidString);
713 : : }
714 : : /* This affix rule will use regex_t to search word ending */
715 : : else
716 : : {
717 : : int masklen;
718 : : int wmasklen;
719 : : int err;
720 : : pg_wchar *wmask;
721 : : char *tmask;
722 : :
6591 tgl@sss.pgh.pa.us 723 : 66 : Affix->issimple = 0;
724 : 66 : Affix->isregis = 0;
725 : 66 : tmask = (char *) tmpalloc(strlen(mask) + 3);
726 [ + - ]: 66 : if (type == FF_SUFFIX)
727 : 66 : sprintf(tmask, "%s$", mask);
728 : : else
6591 tgl@sss.pgh.pa.us 729 :UBC 0 : sprintf(tmask, "^%s", mask);
730 : :
6591 tgl@sss.pgh.pa.us 731 :CBC 66 : masklen = strlen(tmask);
732 : 66 : wmask = (pg_wchar *) tmpalloc((masklen + 1) * sizeof(pg_wchar));
733 : 66 : wmasklen = pg_mb2wchar_with_len(tmask, wmask, masklen);
734 : :
735 : : /*
736 : : * The regex and all internal state created by pg_regcomp are
737 : : * allocated in the dictionary's memory context, and will be freed
738 : : * automatically when it is destroyed.
739 : : */
882 tmunro@postgresql.or 740 : 66 : Affix->reg.pregex = palloc(sizeof(regex_t));
741 : 66 : err = pg_regcomp(Affix->reg.pregex, wmask, wmasklen,
742 : : REG_ADVANCED | REG_NOSUB,
743 : : DEFAULT_COLLATION_OID);
6591 tgl@sss.pgh.pa.us 744 [ - + ]: 66 : if (err)
745 : : {
746 : : char errstr[100];
747 : :
882 tmunro@postgresql.or 748 :UBC 0 : pg_regerror(err, Affix->reg.pregex, errstr, sizeof(errstr));
6591 tgl@sss.pgh.pa.us 749 [ # # ]: 0 : ereport(ERROR,
750 : : (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
751 : : errmsg("invalid regular expression: %s", errstr)));
752 : : }
753 : : }
754 : :
6591 tgl@sss.pgh.pa.us 755 :CBC 530 : Affix->flagflags = flagflags;
756 [ + + - + ]: 530 : if ((Affix->flagflags & FF_COMPOUNDONLY) || (Affix->flagflags & FF_COMPOUNDPERMITFLAG))
757 : : {
758 [ + - ]: 96 : if ((Affix->flagflags & FF_COMPOUNDFLAG) == 0)
759 : 96 : Affix->flagflags |= FF_COMPOUNDFLAG;
760 : : }
3460 teodor@sigaev.ru 761 : 530 : Affix->flag = cpstrdup(Conf, flag);
6591 tgl@sss.pgh.pa.us 762 : 530 : Affix->type = type;
763 : :
5449 764 [ + - + + ]: 530 : Affix->find = (find && *find) ? cpstrdup(Conf, find) : VoidString;
6591 765 [ + + ]: 530 : if ((Affix->replen = strlen(repl)) > 0)
5449 766 : 513 : Affix->repl = cpstrdup(Conf, repl);
767 : : else
6591 768 : 17 : Affix->repl = VoidString;
769 : 530 : Conf->naffixes++;
770 : 530 : }
771 : :
772 : : /* Parsing states for parse_affentry() and friends */
773 : : #define PAE_WAIT_MASK 0
774 : : #define PAE_INMASK 1
775 : : #define PAE_WAIT_FIND 2
776 : : #define PAE_INFIND 3
777 : : #define PAE_WAIT_REPL 4
778 : : #define PAE_INREPL 5
779 : : #define PAE_WAIT_TYPE 6
780 : : #define PAE_WAIT_FLAG 7
781 : :
782 : : /*
783 : : * Parse next space-separated field of an .affix file line.
784 : : *
785 : : * *str is the input pointer (will be advanced past field)
786 : : * next is where to copy the field value to, with null termination
787 : : *
788 : : * The buffer at "next" must be of size BUFSIZ; we truncate the input to fit.
789 : : *
790 : : * Returns true if we found a field, false if not.
791 : : */
792 : : static bool
3496 793 : 4955 : get_nextfield(char **str, char *next)
794 : : {
795 : 4955 : int state = PAE_WAIT_MASK;
796 : 4955 : int avail = BUFSIZ;
797 : :
798 [ + + ]: 21192 : while (**str)
799 : : {
800 [ + + ]: 20610 : if (state == PAE_WAIT_MASK)
801 : : {
802 [ + + ]: 9140 : if (t_iseq(*str, '#'))
803 : 176 : return false;
263 peter@eisentraut.org 804 [ + + ]: 8964 : else if (!isspace((unsigned char) **str))
805 : : {
3496 tgl@sss.pgh.pa.us 806 : 4197 : int clen = pg_mblen(*str);
807 : :
808 [ + - ]: 4197 : if (clen < avail)
809 : : {
810 : 4197 : COPYCHAR(next, *str);
811 : 4197 : next += clen;
812 : 4197 : avail -= clen;
813 : : }
814 : 4197 : state = PAE_INMASK;
815 : : }
816 : : }
817 : : else /* state == PAE_INMASK */
818 : : {
263 peter@eisentraut.org 819 [ + + ]: 11470 : if (isspace((unsigned char) **str))
820 : : {
3496 tgl@sss.pgh.pa.us 821 : 4197 : *next = '\0';
822 : 4197 : return true;
823 : : }
824 : : else
825 : : {
826 : 7273 : int clen = pg_mblen(*str);
827 : :
828 [ + - ]: 7273 : if (clen < avail)
829 : : {
830 : 7273 : COPYCHAR(next, *str);
831 : 7273 : next += clen;
832 : 7273 : avail -= clen;
833 : : }
834 : : }
835 : : }
836 : 16237 : *str += pg_mblen(*str);
837 : : }
838 : :
839 : 582 : *next = '\0';
840 : :
2999 841 : 582 : return (state == PAE_INMASK); /* OK if we got a nonempty field */
842 : : }
843 : :
844 : : /*
845 : : * Parses entry of an .affix file of MySpell or Hunspell format.
846 : : *
847 : : * An .affix file entry has the following format:
848 : : * - header
849 : : * <type> <flag> <cross_flag> <flag_count>
850 : : * - fields after header:
851 : : * <type> <flag> <find> <replace> <mask>
852 : : *
853 : : * str is the input line
854 : : * field values are returned to type etc, which must be buffers of size BUFSIZ.
855 : : *
856 : : * Returns number of fields found; any omitted fields are set to empty strings.
857 : : */
858 : : static int
3496 859 : 1141 : parse_ooaffentry(char *str, char *type, char *flag, char *find,
860 : : char *repl, char *mask)
861 : : {
862 : 1141 : int state = PAE_WAIT_TYPE;
863 : 1141 : int fields_read = 0;
864 : 1141 : bool valid = false;
865 : :
866 : 1141 : *type = *flag = *find = *repl = *mask = '\0';
867 : :
868 [ + - ]: 4955 : while (*str)
869 : : {
870 [ + + + + : 4955 : switch (state)
+ - ]
871 : : {
872 : 1141 : case PAE_WAIT_TYPE:
873 : 1141 : valid = get_nextfield(&str, type);
874 : 1141 : state = PAE_WAIT_FLAG;
875 : 1141 : break;
876 : 1141 : case PAE_WAIT_FLAG:
877 : 1141 : valid = get_nextfield(&str, flag);
878 : 1141 : state = PAE_WAIT_FIND;
879 : 1141 : break;
880 : 1141 : case PAE_WAIT_FIND:
881 : 1141 : valid = get_nextfield(&str, find);
882 : 1141 : state = PAE_WAIT_REPL;
883 : 1141 : break;
884 : 766 : case PAE_WAIT_REPL:
885 : 766 : valid = get_nextfield(&str, repl);
886 : 766 : state = PAE_WAIT_MASK;
887 : 766 : break;
888 : 766 : case PAE_WAIT_MASK:
889 : 766 : valid = get_nextfield(&str, mask);
890 : 766 : state = -1; /* force loop exit */
891 : 766 : break;
3496 tgl@sss.pgh.pa.us 892 :UBC 0 : default:
893 [ # # ]: 0 : elog(ERROR, "unrecognized state in parse_ooaffentry: %d",
894 : : state);
895 : : break;
896 : : }
3496 tgl@sss.pgh.pa.us 897 [ + + ]:CBC 4955 : if (valid)
898 : 4197 : fields_read++;
899 : : else
900 : 758 : break; /* early EOL */
901 [ + + ]: 4197 : if (state < 0)
902 : 383 : break; /* got all fields */
903 : : }
904 : :
905 : 1141 : return fields_read;
906 : : }
907 : :
908 : : /*
909 : : * Parses entry of an .affix file of Ispell format
910 : : *
911 : : * An .affix file entry has the following format:
912 : : * <mask> > [-<find>,]<replace>
913 : : */
914 : : static bool
6289 915 : 147 : parse_affentry(char *str, char *mask, char *find, char *repl)
916 : : {
6591 917 : 147 : int state = PAE_WAIT_MASK;
918 : 147 : char *pmask = mask,
919 : 147 : *pfind = find,
920 : 147 : *prepl = repl;
921 : :
922 : 147 : *mask = *find = *repl = '\0';
923 : :
924 [ + - ]: 3864 : while (*str)
925 : : {
926 [ + + ]: 3864 : if (state == PAE_WAIT_MASK)
927 : : {
928 [ - + ]: 357 : if (t_iseq(str, '#'))
6591 tgl@sss.pgh.pa.us 929 :UBC 0 : return false;
263 peter@eisentraut.org 930 [ + + ]:CBC 357 : else if (!isspace((unsigned char) *str))
931 : : {
6591 tgl@sss.pgh.pa.us 932 : 147 : COPYCHAR(pmask, str);
933 : 147 : pmask += pg_mblen(str);
934 : 147 : state = PAE_INMASK;
935 : : }
936 : : }
937 [ + + ]: 3507 : else if (state == PAE_INMASK)
938 : : {
939 [ + + ]: 1428 : if (t_iseq(str, '>'))
940 : : {
941 : 147 : *pmask = '\0';
942 : 147 : state = PAE_WAIT_FIND;
943 : : }
263 peter@eisentraut.org 944 [ + + ]: 1281 : else if (!isspace((unsigned char) *str))
945 : : {
6591 tgl@sss.pgh.pa.us 946 : 504 : COPYCHAR(pmask, str);
947 : 504 : pmask += pg_mblen(str);
948 : : }
949 : : }
950 [ + + ]: 2079 : else if (state == PAE_WAIT_FIND)
951 : : {
952 [ + + ]: 588 : if (t_iseq(str, '-'))
953 : : {
954 : 21 : state = PAE_INFIND;
955 : : }
956 [ + + - + ]: 567 : else if (t_isalpha(str) || t_iseq(str, '\'') /* english 's */ )
957 : : {
958 : 126 : COPYCHAR(prepl, str);
959 : 126 : prepl += pg_mblen(str);
960 : 126 : state = PAE_INREPL;
961 : : }
263 peter@eisentraut.org 962 [ - + ]: 441 : else if (!isspace((unsigned char) *str))
6591 tgl@sss.pgh.pa.us 963 [ # # ]:UBC 0 : ereport(ERROR,
964 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
965 : : errmsg("syntax error")));
966 : : }
6591 tgl@sss.pgh.pa.us 967 [ + + ]:CBC 1491 : else if (state == PAE_INFIND)
968 : : {
969 [ + + ]: 42 : if (t_iseq(str, ','))
970 : : {
971 : 21 : *pfind = '\0';
972 : 21 : state = PAE_WAIT_REPL;
973 : : }
974 [ + - ]: 21 : else if (t_isalpha(str))
975 : : {
976 : 21 : COPYCHAR(pfind, str);
977 : 21 : pfind += pg_mblen(str);
978 : : }
263 peter@eisentraut.org 979 [ # # ]:UBC 0 : else if (!isspace((unsigned char) *str))
6591 tgl@sss.pgh.pa.us 980 [ # # ]: 0 : ereport(ERROR,
981 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
982 : : errmsg("syntax error")));
983 : : }
6591 tgl@sss.pgh.pa.us 984 [ + + ]:CBC 1449 : else if (state == PAE_WAIT_REPL)
985 : : {
986 [ - + ]: 21 : if (t_iseq(str, '-'))
987 : : {
6591 tgl@sss.pgh.pa.us 988 :UBC 0 : break; /* void repl */
989 : : }
6591 tgl@sss.pgh.pa.us 990 [ + - ]:CBC 21 : else if (t_isalpha(str))
991 : : {
992 : 21 : COPYCHAR(prepl, str);
993 : 21 : prepl += pg_mblen(str);
994 : 21 : state = PAE_INREPL;
995 : : }
263 peter@eisentraut.org 996 [ # # ]:UBC 0 : else if (!isspace((unsigned char) *str))
6591 tgl@sss.pgh.pa.us 997 [ # # ]: 0 : ereport(ERROR,
998 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
999 : : errmsg("syntax error")));
1000 : : }
6591 tgl@sss.pgh.pa.us 1001 [ + - ]:CBC 1428 : else if (state == PAE_INREPL)
1002 : : {
1003 [ + + ]: 1428 : if (t_iseq(str, '#'))
1004 : : {
1005 : 147 : *prepl = '\0';
1006 : 147 : break;
1007 : : }
1008 [ + + ]: 1281 : else if (t_isalpha(str))
1009 : : {
1010 : 189 : COPYCHAR(prepl, str);
1011 : 189 : prepl += pg_mblen(str);
1012 : : }
263 peter@eisentraut.org 1013 [ - + ]: 1092 : else if (!isspace((unsigned char) *str))
6591 tgl@sss.pgh.pa.us 1014 [ # # ]:UBC 0 : ereport(ERROR,
1015 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1016 : : errmsg("syntax error")));
1017 : : }
1018 : : else
6492 1019 [ # # ]: 0 : elog(ERROR, "unrecognized state in parse_affentry: %d", state);
1020 : :
6591 tgl@sss.pgh.pa.us 1021 :CBC 3717 : str += pg_mblen(str);
1022 : : }
1023 : :
1024 : 147 : *pmask = *pfind = *prepl = '\0';
1025 : :
3473 teodor@sigaev.ru 1026 [ + - + + : 147 : return (*mask && (*find || *repl));
+ - ]
1027 : : }
1028 : :
1029 : : /*
1030 : : * Sets a Hunspell options depending on flag type.
1031 : : */
1032 : : static void
3460 1033 : 1428 : setCompoundAffixFlagValue(IspellDict *Conf, CompoundAffixFlag *entry,
1034 : : char *s, uint32 val)
1035 : : {
1036 [ + + ]: 1428 : if (Conf->flagMode == FM_NUM)
1037 : : {
1038 : : char *next;
1039 : : int i;
1040 : :
182 tgl@sss.pgh.pa.us 1041 : 309 : errno = 0;
3460 teodor@sigaev.ru 1042 : 309 : i = strtol(s, &next, 10);
1043 [ + - - + ]: 309 : if (s == next || errno == ERANGE)
3460 teodor@sigaev.ru 1044 [ # # ]:UBC 0 : ereport(ERROR,
1045 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1046 : : errmsg("invalid affix flag \"%s\"", s)));
3460 teodor@sigaev.ru 1047 [ + - - + ]:CBC 309 : if (i < 0 || i > FLAGNUM_MAXSIZE)
3460 teodor@sigaev.ru 1048 [ # # ]:UBC 0 : ereport(ERROR,
1049 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1050 : : errmsg("affix flag \"%s\" is out of range", s)));
1051 : :
3460 teodor@sigaev.ru 1052 :CBC 309 : entry->flag.i = i;
1053 : : }
1054 : : else
1055 : 1119 : entry->flag.s = cpstrdup(Conf, s);
1056 : :
1057 : 1428 : entry->flagMode = Conf->flagMode;
1058 : 1428 : entry->value = val;
1059 : 1428 : }
1060 : :
1061 : : /*
1062 : : * Sets up a correspondence for the affix parameter with the affix flag.
1063 : : *
1064 : : * Conf: current dictionary.
1065 : : * s: affix flag in string.
1066 : : * val: affix parameter.
1067 : : */
1068 : : static void
1069 : 171 : addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
1070 : : {
1071 : : CompoundAffixFlag *newValue;
1072 : : char sbuf[BUFSIZ];
1073 : : char *sflag;
1074 : : int clen;
1075 : :
263 peter@eisentraut.org 1076 [ + - + + ]: 321 : while (*s && isspace((unsigned char) *s))
6288 tgl@sss.pgh.pa.us 1077 : 150 : s += pg_mblen(s);
1078 : :
6591 1079 [ - + ]: 171 : if (!*s)
6591 tgl@sss.pgh.pa.us 1080 [ # # ]:UBC 0 : ereport(ERROR,
1081 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1082 : : errmsg("syntax error")));
1083 : :
1084 : : /* Get flag without \n */
3460 teodor@sigaev.ru 1085 :CBC 171 : sflag = sbuf;
263 peter@eisentraut.org 1086 [ + - + + : 506 : while (*s && !isspace((unsigned char) *s) && *s != '\n')
+ - ]
1087 : : {
3460 teodor@sigaev.ru 1088 : 335 : clen = pg_mblen(s);
1089 : 335 : COPYCHAR(sflag, s);
1090 : 335 : sflag += clen;
1091 : 335 : s += clen;
1092 : : }
1093 : 171 : *sflag = '\0';
1094 : :
1095 : : /* Resize array or allocate memory for array CompoundAffixFlag */
1096 [ + + ]: 171 : if (Conf->nCompoundAffixFlag >= Conf->mCompoundAffixFlag)
1097 : : {
1098 [ - + ]: 64 : if (Conf->mCompoundAffixFlag)
1099 : : {
3460 teodor@sigaev.ru 1100 :UBC 0 : Conf->mCompoundAffixFlag *= 2;
1101 : 0 : Conf->CompoundAffixFlags = (CompoundAffixFlag *)
942 peter@eisentraut.org 1102 : 0 : repalloc(Conf->CompoundAffixFlags,
2999 tgl@sss.pgh.pa.us 1103 : 0 : Conf->mCompoundAffixFlag * sizeof(CompoundAffixFlag));
1104 : : }
1105 : : else
1106 : : {
3460 teodor@sigaev.ru 1107 :CBC 64 : Conf->mCompoundAffixFlag = 10;
1108 : 64 : Conf->CompoundAffixFlags = (CompoundAffixFlag *)
1109 : 64 : tmpalloc(Conf->mCompoundAffixFlag * sizeof(CompoundAffixFlag));
1110 : : }
1111 : : }
1112 : :
1113 : 171 : newValue = Conf->CompoundAffixFlags + Conf->nCompoundAffixFlag;
1114 : :
1115 : 171 : setCompoundAffixFlagValue(Conf, newValue, sbuf, val);
1116 : :
6591 tgl@sss.pgh.pa.us 1117 : 171 : Conf->usecompound = true;
3460 teodor@sigaev.ru 1118 : 171 : Conf->nCompoundAffixFlag++;
6591 tgl@sss.pgh.pa.us 1119 : 171 : }
1120 : :
1121 : : /*
1122 : : * Returns a set of affix parameters which correspondence to the set of affix
1123 : : * flags s.
1124 : : */
1125 : : static int
396 heikki.linnakangas@i 1126 : 618 : getCompoundAffixFlagValue(IspellDict *Conf, const char *s)
1127 : : {
3376 rhaas@postgresql.org 1128 : 618 : uint32 flag = 0;
1129 : : CompoundAffixFlag *found,
1130 : : key;
1131 : : char sflag[BUFSIZ];
1132 : : const char *flagcur;
1133 : :
3460 teodor@sigaev.ru 1134 [ - + ]: 618 : if (Conf->nCompoundAffixFlag == 0)
3460 teodor@sigaev.ru 1135 :UBC 0 : return 0;
1136 : :
3473 teodor@sigaev.ru 1137 :CBC 618 : flagcur = s;
1138 [ + + ]: 1875 : while (*flagcur)
1139 : : {
3460 1140 : 1260 : getNextFlagFromString(Conf, &flagcur, sflag);
1141 : 1257 : setCompoundAffixFlagValue(Conf, &key, sflag, 0);
1142 : :
1143 : : found = (CompoundAffixFlag *)
942 peter@eisentraut.org 1144 : 1257 : bsearch(&key, Conf->CompoundAffixFlags,
3460 teodor@sigaev.ru 1145 : 1257 : Conf->nCompoundAffixFlag, sizeof(CompoundAffixFlag),
1146 : : cmpcmdflag);
1147 [ + + ]: 1257 : if (found != NULL)
1148 : 287 : flag |= found->value;
1149 : : }
1150 : :
3473 1151 : 615 : return flag;
1152 : : }
1153 : :
1154 : : /*
1155 : : * Returns a flag set using the s parameter.
1156 : : *
1157 : : * If Conf->useFlagAliases is true then the s parameter is index of the
1158 : : * Conf->AffixData array and function returns its entry.
1159 : : * Else function returns the s parameter.
1160 : : */
1161 : : static const char *
3460 1162 : 75 : getAffixFlagSet(IspellDict *Conf, char *s)
1163 : : {
1164 [ + + + - ]: 75 : if (Conf->useFlagAliases && *s != '\0')
1165 : : {
1166 : : int curaffix;
1167 : : char *end;
1168 : :
182 tgl@sss.pgh.pa.us 1169 : 48 : errno = 0;
3460 teodor@sigaev.ru 1170 : 48 : curaffix = strtol(s, &end, 10);
1171 [ + - - + ]: 48 : if (s == end || errno == ERANGE)
3460 teodor@sigaev.ru 1172 [ # # ]:UBC 0 : ereport(ERROR,
1173 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1174 : : errmsg("invalid affix alias \"%s\"", s)));
1175 : :
2135 tgl@sss.pgh.pa.us 1176 [ + - + - ]:CBC 48 : if (curaffix > 0 && curaffix < Conf->nAffixData)
1177 : :
1178 : : /*
1179 : : * Do not subtract 1 from curaffix because empty string was added
1180 : : * in NIImportOOAffixes
1181 : : */
3473 teodor@sigaev.ru 1182 : 48 : return Conf->AffixData[curaffix];
2135 tgl@sss.pgh.pa.us 1183 [ # # ]:UBC 0 : else if (curaffix > Conf->nAffixData)
1184 [ # # ]: 0 : ereport(ERROR,
1185 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1186 : : errmsg("invalid affix alias \"%s\"", s)));
2134 1187 : 0 : return VoidString;
1188 : : }
1189 : : else
3473 teodor@sigaev.ru 1190 :CBC 27 : return s;
1191 : : }
1192 : :
1193 : : /*
1194 : : * Import an affix file that follows MySpell or Hunspell format.
1195 : : *
1196 : : * Conf: current dictionary.
1197 : : * filename: path to the .affix file.
1198 : : */
1199 : : static void
6505 bruce@momjian.us 1200 : 43 : NIImportOOAffixes(IspellDict *Conf, const char *filename)
1201 : : {
1202 : : char type[BUFSIZ],
6591 tgl@sss.pgh.pa.us 1203 : 43 : *ptype = NULL;
1204 : : char sflag[BUFSIZ];
1205 : : char mask[BUFSIZ],
1206 : : *pmask;
1207 : : char find[BUFSIZ],
1208 : : *pfind;
1209 : : char repl[BUFSIZ],
1210 : : *prepl;
1211 : 43 : bool isSuffix = false;
3473 teodor@sigaev.ru 1212 : 43 : int naffix = 0,
1213 : 43 : curaffix = 0;
3460 1214 : 43 : int sflaglen = 0;
6591 tgl@sss.pgh.pa.us 1215 : 43 : char flagflags = 0;
1216 : : tsearch_readline_state trst;
1217 : : char *recoded;
1218 : :
1219 : : /* read file to find any flag */
1220 : 43 : Conf->usecompound = false;
3473 teodor@sigaev.ru 1221 : 43 : Conf->useFlagAliases = false;
1222 : 43 : Conf->flagMode = FM_CHAR;
1223 : :
6289 tgl@sss.pgh.pa.us 1224 [ - + ]: 43 : if (!tsearch_readline_begin(&trst, filename))
6591 tgl@sss.pgh.pa.us 1225 [ # # ]:UBC 0 : ereport(ERROR,
1226 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1227 : : errmsg("could not open affix file \"%s\": %m",
1228 : : filename)));
1229 : :
6289 tgl@sss.pgh.pa.us 1230 [ + + ]:CBC 1682 : while ((recoded = tsearch_readline(&trst)) != NULL)
1231 : : {
263 peter@eisentraut.org 1232 [ + - + + : 1639 : if (*recoded == '\0' || isspace((unsigned char) *recoded) || t_iseq(recoded, '#'))
+ + ]
1233 : : {
6587 tgl@sss.pgh.pa.us 1234 : 498 : pfree(recoded);
6591 1235 : 498 : continue;
1236 : : }
1237 : :
1238 [ + + ]: 1141 : if (STRNCMP(recoded, "COMPOUNDFLAG") == 0)
3460 teodor@sigaev.ru 1239 : 43 : addCompoundAffixFlagValue(Conf, recoded + strlen("COMPOUNDFLAG"),
1240 : : FF_COMPOUNDFLAG);
6591 tgl@sss.pgh.pa.us 1241 [ + + ]: 1098 : else if (STRNCMP(recoded, "COMPOUNDBEGIN") == 0)
3460 teodor@sigaev.ru 1242 : 16 : addCompoundAffixFlagValue(Conf, recoded + strlen("COMPOUNDBEGIN"),
1243 : : FF_COMPOUNDBEGIN);
6591 tgl@sss.pgh.pa.us 1244 [ - + ]: 1082 : else if (STRNCMP(recoded, "COMPOUNDLAST") == 0)
3460 teodor@sigaev.ru 1245 :UBC 0 : addCompoundAffixFlagValue(Conf, recoded + strlen("COMPOUNDLAST"),
1246 : : FF_COMPOUNDLAST);
1247 : : /* COMPOUNDLAST and COMPOUNDEND are synonyms */
6591 tgl@sss.pgh.pa.us 1248 [ + + ]:CBC 1082 : else if (STRNCMP(recoded, "COMPOUNDEND") == 0)
3460 teodor@sigaev.ru 1249 : 16 : addCompoundAffixFlagValue(Conf, recoded + strlen("COMPOUNDEND"),
1250 : : FF_COMPOUNDLAST);
6591 tgl@sss.pgh.pa.us 1251 [ + + ]: 1066 : else if (STRNCMP(recoded, "COMPOUNDMIDDLE") == 0)
3460 teodor@sigaev.ru 1252 : 16 : addCompoundAffixFlagValue(Conf, recoded + strlen("COMPOUNDMIDDLE"),
1253 : : FF_COMPOUNDMIDDLE);
6591 tgl@sss.pgh.pa.us 1254 [ + + ]: 1050 : else if (STRNCMP(recoded, "ONLYINCOMPOUND") == 0)
3460 teodor@sigaev.ru 1255 : 43 : addCompoundAffixFlagValue(Conf, recoded + strlen("ONLYINCOMPOUND"),
1256 : : FF_COMPOUNDONLY);
6591 tgl@sss.pgh.pa.us 1257 [ + + ]: 1007 : else if (STRNCMP(recoded, "COMPOUNDPERMITFLAG") == 0)
3460 teodor@sigaev.ru 1258 : 16 : addCompoundAffixFlagValue(Conf,
1259 : : recoded + strlen("COMPOUNDPERMITFLAG"),
1260 : : FF_COMPOUNDPERMITFLAG);
6591 tgl@sss.pgh.pa.us 1261 [ - + ]: 991 : else if (STRNCMP(recoded, "COMPOUNDFORBIDFLAG") == 0)
3460 teodor@sigaev.ru 1262 :UBC 0 : addCompoundAffixFlagValue(Conf,
1263 : : recoded + strlen("COMPOUNDFORBIDFLAG"),
1264 : : FF_COMPOUNDFORBIDFLAG);
6591 tgl@sss.pgh.pa.us 1265 [ + + ]:CBC 991 : else if (STRNCMP(recoded, "FLAG") == 0)
1266 : : {
1267 : 33 : char *s = recoded + strlen("FLAG");
1268 : :
263 peter@eisentraut.org 1269 [ + - + + ]: 66 : while (*s && isspace((unsigned char) *s))
6288 tgl@sss.pgh.pa.us 1270 : 33 : s += pg_mblen(s);
1271 : :
3473 teodor@sigaev.ru 1272 [ + - ]: 33 : if (*s)
1273 : : {
1274 [ + + ]: 33 : if (STRNCMP(s, "long") == 0)
1275 : 16 : Conf->flagMode = FM_LONG;
1276 [ + - ]: 17 : else if (STRNCMP(s, "num") == 0)
1277 : 17 : Conf->flagMode = FM_NUM;
3473 teodor@sigaev.ru 1278 [ # # ]:UBC 0 : else if (STRNCMP(s, "default") != 0)
1279 [ # # ]: 0 : ereport(ERROR,
1280 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1281 : : errmsg("Ispell dictionary supports only "
1282 : : "\"default\", \"long\", "
1283 : : "and \"num\" flag values")));
1284 : : }
1285 : : }
1286 : :
6587 tgl@sss.pgh.pa.us 1287 :CBC 1141 : pfree(recoded);
1288 : : }
6289 1289 : 43 : tsearch_readline_end(&trst);
1290 : :
3460 teodor@sigaev.ru 1291 [ + - ]: 43 : if (Conf->nCompoundAffixFlag > 1)
942 peter@eisentraut.org 1292 : 43 : qsort(Conf->CompoundAffixFlags, Conf->nCompoundAffixFlag,
1293 : : sizeof(CompoundAffixFlag), cmpcmdflag);
1294 : :
6289 tgl@sss.pgh.pa.us 1295 [ - + ]: 43 : if (!tsearch_readline_begin(&trst, filename))
6591 tgl@sss.pgh.pa.us 1296 [ # # ]:UBC 0 : ereport(ERROR,
1297 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1298 : : errmsg("could not open affix file \"%s\": %m",
1299 : : filename)));
1300 : :
6289 tgl@sss.pgh.pa.us 1301 [ + + ]:CBC 1682 : while ((recoded = tsearch_readline(&trst)) != NULL)
1302 : : {
1303 : : int fields_read;
1304 : :
263 peter@eisentraut.org 1305 [ + - + + : 1639 : if (*recoded == '\0' || isspace((unsigned char) *recoded) || t_iseq(recoded, '#'))
+ + ]
6587 tgl@sss.pgh.pa.us 1306 : 498 : goto nextline;
1307 : :
3496 1308 : 1141 : fields_read = parse_ooaffentry(recoded, type, sflag, find, repl, mask);
1309 : :
6591 1310 [ + + ]: 1141 : if (ptype)
1311 : 1098 : pfree(ptype);
5449 1312 : 1141 : ptype = lowerstr_ctx(Conf, type);
1313 : :
1314 : : /* First try to parse AF parameter (alias compression) */
3473 teodor@sigaev.ru 1315 [ + + ]: 1141 : if (STRNCMP(ptype, "af") == 0)
1316 : : {
1317 : : /* First line is the number of aliases */
1318 [ + + ]: 192 : if (!Conf->useFlagAliases)
1319 : : {
1320 : 16 : Conf->useFlagAliases = true;
1321 : 16 : naffix = atoi(sflag);
2703 tgl@sss.pgh.pa.us 1322 [ - + ]: 16 : if (naffix <= 0)
3473 teodor@sigaev.ru 1323 [ # # ]:UBC 0 : ereport(ERROR,
1324 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1325 : : errmsg("invalid number of flag vector aliases")));
1326 : :
1327 : : /* Also reserve place for empty flag set */
3473 teodor@sigaev.ru 1328 :CBC 16 : naffix++;
1329 : :
396 heikki.linnakangas@i 1330 : 16 : Conf->AffixData = (const char **) palloc0(naffix * sizeof(char *));
3473 teodor@sigaev.ru 1331 : 16 : Conf->lenAffixData = Conf->nAffixData = naffix;
1332 : :
1333 : : /* Add empty flag set into AffixData */
1334 : 16 : Conf->AffixData[curaffix] = VoidString;
1335 : 16 : curaffix++;
1336 : : }
1337 : : /* Other lines are aliases */
1338 : : else
1339 : : {
1340 [ + - ]: 176 : if (curaffix < naffix)
1341 : : {
1342 : 176 : Conf->AffixData[curaffix] = cpstrdup(Conf, sflag);
1343 : 176 : curaffix++;
1344 : : }
1345 : : else
2703 tgl@sss.pgh.pa.us 1346 [ # # ]:UBC 0 : ereport(ERROR,
1347 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1348 : : errmsg("number of aliases exceeds specified number %d",
1349 : : naffix - 1)));
1350 : : }
3473 teodor@sigaev.ru 1351 :CBC 192 : goto nextline;
1352 : : }
1353 : : /* Else try to parse prefixes and suffixes */
3496 tgl@sss.pgh.pa.us 1354 [ + + ]: 949 : if (fields_read < 4 ||
1355 [ + + - + ]: 766 : (STRNCMP(ptype, "sfx") != 0 && STRNCMP(ptype, "pfx") != 0))
6587 1356 : 183 : goto nextline;
1357 : :
3473 teodor@sigaev.ru 1358 : 766 : sflaglen = strlen(sflag);
1359 [ + - ]: 766 : if (sflaglen == 0
1360 [ + + + - ]: 766 : || (sflaglen > 1 && Conf->flagMode == FM_CHAR)
1361 [ + + - + ]: 766 : || (sflaglen > 2 && Conf->flagMode == FM_LONG))
3473 teodor@sigaev.ru 1362 :UBC 0 : goto nextline;
1363 : :
1364 : : /*--------
1365 : : * Affix header. For example:
1366 : : * SFX \ N 1
1367 : : *--------
1368 : : */
3496 tgl@sss.pgh.pa.us 1369 [ + + ]:CBC 766 : if (fields_read == 4)
1370 : : {
3473 teodor@sigaev.ru 1371 : 383 : isSuffix = (STRNCMP(ptype, "sfx") == 0);
6571 1372 [ + - + + ]: 383 : if (t_iseq(find, 'y') || t_iseq(find, 'Y'))
6591 tgl@sss.pgh.pa.us 1373 : 265 : flagflags = FF_CROSSPRODUCT;
1374 : : else
1375 : 118 : flagflags = 0;
1376 : : }
1377 : : /*--------
1378 : : * Affix fields. For example:
1379 : : * SFX \ 0 Y/L [^Y]
1380 : : *--------
1381 : : */
1382 : : else
1383 : : {
1384 : : char *ptr;
1385 : 383 : int aflg = 0;
1386 : :
1387 : : /* Get flags after '/' (flags are case sensitive) */
3471 1388 [ + + ]: 383 : if ((ptr = strchr(repl, '/')) != NULL)
3460 teodor@sigaev.ru 1389 : 75 : aflg |= getCompoundAffixFlagValue(Conf,
1390 : : getAffixFlagSet(Conf,
1391 : : ptr + 1));
1392 : : /* Get lowercased version of string before '/' */
5449 tgl@sss.pgh.pa.us 1393 : 383 : prepl = lowerstr_ctx(Conf, repl);
6591 1394 [ + + ]: 383 : if ((ptr = strchr(prepl, '/')) != NULL)
1395 : 75 : *ptr = '\0';
5449 1396 : 383 : pfind = lowerstr_ctx(Conf, find);
1397 : 383 : pmask = lowerstr_ctx(Conf, mask);
6591 1398 [ + + ]: 383 : if (t_iseq(find, '0'))
1399 : 323 : *pfind = '\0';
1400 [ + + ]: 383 : if (t_iseq(repl, '0'))
1401 : 17 : *prepl = '\0';
1402 : :
3460 teodor@sigaev.ru 1403 : 383 : NIAddAffix(Conf, sflag, flagflags | aflg, pmask, pfind, prepl,
1404 : : isSuffix ? FF_SUFFIX : FF_PREFIX);
6591 tgl@sss.pgh.pa.us 1405 : 383 : pfree(prepl);
1406 : 383 : pfree(pfind);
1407 : 383 : pfree(pmask);
1408 : : }
1409 : :
6505 bruce@momjian.us 1410 : 1639 : nextline:
6587 tgl@sss.pgh.pa.us 1411 : 1639 : pfree(recoded);
1412 : : }
1413 : :
6289 1414 : 43 : tsearch_readline_end(&trst);
6591 1415 [ + - ]: 43 : if (ptype)
1416 : 43 : pfree(ptype);
1417 : 43 : }
1418 : :
1419 : : /*
1420 : : * import affixes
1421 : : *
1422 : : * Note caller must already have applied get_tsearch_config_filename
1423 : : *
1424 : : * This function is responsible for parsing ispell ("old format") affix files.
1425 : : * If we realize that the file contains new-format commands, we pass off the
1426 : : * work to NIImportOOAffixes(), which will re-read the whole file.
1427 : : */
1428 : : void
6505 bruce@momjian.us 1429 : 64 : NIImportAffixes(IspellDict *Conf, const char *filename)
1430 : : {
6571 teodor@sigaev.ru 1431 : 64 : char *pstr = NULL;
1432 : : char flag[BUFSIZ];
1433 : : char mask[BUFSIZ];
1434 : : char find[BUFSIZ];
1435 : : char repl[BUFSIZ];
1436 : : char *s;
6587 tgl@sss.pgh.pa.us 1437 : 64 : bool suffixes = false;
1438 : 64 : bool prefixes = false;
6591 1439 : 64 : char flagflags = 0;
1440 : : tsearch_readline_state trst;
6587 1441 : 64 : bool oldformat = false;
1442 : 64 : char *recoded = NULL;
1443 : :
6289 1444 [ - + ]: 64 : if (!tsearch_readline_begin(&trst, filename))
6591 tgl@sss.pgh.pa.us 1445 [ # # ]:UBC 0 : ereport(ERROR,
1446 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1447 : : errmsg("could not open affix file \"%s\": %m",
1448 : : filename)));
1449 : :
6591 tgl@sss.pgh.pa.us 1450 :CBC 64 : Conf->usecompound = false;
3473 teodor@sigaev.ru 1451 : 64 : Conf->useFlagAliases = false;
1452 : 64 : Conf->flagMode = FM_CHAR;
1453 : :
6289 tgl@sss.pgh.pa.us 1454 [ + + ]: 610 : while ((recoded = tsearch_readline(&trst)) != NULL)
1455 : : {
263 peter@eisentraut.org 1456 : 589 : pstr = str_tolower(recoded, strlen(recoded), DEFAULT_COLLATION_OID);
1457 : :
1458 : : /* Skip comments and empty lines */
6591 tgl@sss.pgh.pa.us 1459 [ + - + + ]: 589 : if (*pstr == '#' || *pstr == '\n')
6587 1460 : 189 : goto nextline;
1461 : :
6591 1462 [ + + ]: 400 : if (STRNCMP(pstr, "compoundwords") == 0)
1463 : : {
1464 : : /* Find case-insensitive L flag in non-lowercased string */
3471 1465 : 21 : s = findchar2(recoded, 'l', 'L');
6591 1466 [ + - ]: 21 : if (s)
1467 : : {
263 peter@eisentraut.org 1468 [ + - + + ]: 105 : while (*s && !isspace((unsigned char) *s))
6288 tgl@sss.pgh.pa.us 1469 : 84 : s += pg_mblen(s);
263 peter@eisentraut.org 1470 [ + - + + ]: 42 : while (*s && isspace((unsigned char) *s))
6288 tgl@sss.pgh.pa.us 1471 : 21 : s += pg_mblen(s);
1472 : :
6591 1473 [ + - + - ]: 21 : if (*s && pg_mblen(s) == 1)
1474 : : {
3460 teodor@sigaev.ru 1475 : 21 : addCompoundAffixFlagValue(Conf, s, FF_COMPOUNDFLAG);
6591 tgl@sss.pgh.pa.us 1476 : 21 : Conf->usecompound = true;
1477 : : }
6587 1478 : 21 : oldformat = true;
1479 : 21 : goto nextline;
1480 : : }
1481 : : }
6591 1482 [ + + ]: 379 : if (STRNCMP(pstr, "suffixes") == 0)
1483 : : {
6587 1484 : 21 : suffixes = true;
1485 : 21 : prefixes = false;
1486 : 21 : oldformat = true;
1487 : 21 : goto nextline;
1488 : : }
6591 1489 [ + + ]: 358 : if (STRNCMP(pstr, "prefixes") == 0)
1490 : : {
6587 1491 : 21 : suffixes = false;
1492 : 21 : prefixes = true;
1493 : 21 : oldformat = true;
1494 : 21 : goto nextline;
1495 : : }
6591 1496 [ + + ]: 337 : if (STRNCMP(pstr, "flag") == 0)
1497 : : {
6505 bruce@momjian.us 1498 : 180 : s = recoded + 4; /* we need non-lowercased string */
6591 tgl@sss.pgh.pa.us 1499 : 180 : flagflags = 0;
1500 : :
263 peter@eisentraut.org 1501 [ + - + + ]: 360 : while (*s && isspace((unsigned char) *s))
6288 tgl@sss.pgh.pa.us 1502 : 180 : s += pg_mblen(s);
1503 : :
6591 1504 [ + + ]: 180 : if (*s == '*')
1505 : : {
1506 : 105 : flagflags |= FF_CROSSPRODUCT;
1507 : 105 : s++;
1508 : : }
1509 [ + + ]: 75 : else if (*s == '~')
1510 : : {
1511 : 21 : flagflags |= FF_COMPOUNDONLY;
1512 : 21 : s++;
1513 : : }
1514 : :
1515 [ + + ]: 180 : if (*s == '\\')
1516 : 21 : s++;
1517 : :
1518 : : /*
1519 : : * An old-format flag is a single ASCII character; we expect it to
1520 : : * be followed by EOL, whitespace, or ':'. Otherwise this is a
1521 : : * new-format flag command.
1522 : : */
3971 1523 [ + - + - ]: 180 : if (*s && pg_mblen(s) == 1)
1524 : : {
3460 teodor@sigaev.ru 1525 : 180 : COPYCHAR(flag, s);
1526 : 180 : flag[1] = '\0';
1527 : :
3971 tgl@sss.pgh.pa.us 1528 : 180 : s++;
1529 [ + - + - : 180 : if (*s == '\0' || *s == '#' || *s == '\n' || *s == ':' ||
+ - + + ]
263 peter@eisentraut.org 1530 [ - + ]: 33 : isspace((unsigned char) *s))
1531 : : {
3971 tgl@sss.pgh.pa.us 1532 : 147 : oldformat = true;
1533 : 147 : goto nextline;
1534 : : }
1535 : : }
1536 : 33 : goto isnewformat;
1537 : : }
1538 [ + + ]: 157 : if (STRNCMP(recoded, "COMPOUNDFLAG") == 0 ||
1539 [ + - ]: 147 : STRNCMP(recoded, "COMPOUNDMIN") == 0 ||
1540 [ + - ]: 147 : STRNCMP(recoded, "PFX") == 0 ||
1541 [ - + ]: 147 : STRNCMP(recoded, "SFX") == 0)
1542 : 10 : goto isnewformat;
1543 : :
6591 1544 [ + + - + ]: 147 : if ((!suffixes) && (!prefixes))
6587 tgl@sss.pgh.pa.us 1545 :UBC 0 : goto nextline;
1546 : :
6289 tgl@sss.pgh.pa.us 1547 [ - + ]:CBC 147 : if (!parse_affentry(pstr, mask, find, repl))
6587 tgl@sss.pgh.pa.us 1548 :UBC 0 : goto nextline;
1549 : :
6591 tgl@sss.pgh.pa.us 1550 :CBC 147 : NIAddAffix(Conf, flag, flagflags, mask, find, repl, suffixes ? FF_SUFFIX : FF_PREFIX);
1551 : :
6505 bruce@momjian.us 1552 : 546 : nextline:
6571 teodor@sigaev.ru 1553 : 546 : pfree(recoded);
6591 tgl@sss.pgh.pa.us 1554 : 546 : pfree(pstr);
1555 : : }
6289 1556 : 21 : tsearch_readline_end(&trst);
3971 1557 : 21 : return;
1558 : :
1559 : 43 : isnewformat:
1560 [ - + ]: 43 : if (oldformat)
3971 tgl@sss.pgh.pa.us 1561 [ # # ]:UBC 0 : ereport(ERROR,
1562 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1563 : : errmsg("affix file contains both old-style and new-style commands")));
3971 tgl@sss.pgh.pa.us 1564 :CBC 43 : tsearch_readline_end(&trst);
1565 : :
1566 : 43 : NIImportOOAffixes(Conf, filename);
1567 : : }
1568 : :
1569 : : /*
1570 : : * Merges two affix flag sets and stores a new affix flag set into
1571 : : * Conf->AffixData.
1572 : : *
1573 : : * Returns index of a new affix flag set.
1574 : : */
1575 : : static int
6505 bruce@momjian.us 1576 : 32 : MergeAffix(IspellDict *Conf, int a1, int a2)
1577 : : {
1578 : : const char **ptr;
1579 : :
2135 tgl@sss.pgh.pa.us 1580 [ + - - + ]: 32 : Assert(a1 < Conf->nAffixData && a2 < Conf->nAffixData);
1581 : :
1582 : : /* Do not merge affix flags if one of affix flags is empty */
3466 teodor@sigaev.ru 1583 [ - + ]: 32 : if (*Conf->AffixData[a1] == '\0')
3466 teodor@sigaev.ru 1584 :UBC 0 : return a2;
3466 teodor@sigaev.ru 1585 [ - + ]:CBC 32 : else if (*Conf->AffixData[a2] == '\0')
3466 teodor@sigaev.ru 1586 :UBC 0 : return a1;
1587 : :
1588 : : /* Double the size of AffixData if there's not enough space */
1528 drowley@postgresql.o 1589 [ + - ]:CBC 32 : if (Conf->nAffixData + 1 >= Conf->lenAffixData)
1590 : : {
6591 tgl@sss.pgh.pa.us 1591 : 32 : Conf->lenAffixData *= 2;
396 heikki.linnakangas@i 1592 : 32 : Conf->AffixData = (const char **) repalloc(Conf->AffixData,
1593 : 32 : sizeof(char *) * Conf->lenAffixData);
1594 : : }
1595 : :
6591 tgl@sss.pgh.pa.us 1596 : 32 : ptr = Conf->AffixData + Conf->nAffixData;
3466 teodor@sigaev.ru 1597 [ + + ]: 32 : if (Conf->flagMode == FM_NUM)
1598 : : {
396 heikki.linnakangas@i 1599 : 14 : char *p = cpalloc(strlen(Conf->AffixData[a1]) +
1600 : : strlen(Conf->AffixData[a2]) +
1601 : : 1 /* comma */ + 1 /* \0 */ );
1602 : :
1603 : 14 : sprintf(p, "%s,%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1604 : 14 : *ptr = p;
1605 : : }
1606 : : else
1607 : : {
1608 : 18 : char *p = cpalloc(strlen(Conf->AffixData[a1]) +
1609 : : strlen(Conf->AffixData[a2]) +
1610 : : 1 /* \0 */ );
1611 : :
1612 : 18 : sprintf(p, "%s%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1613 : 18 : *ptr = p;
1614 : : }
6591 tgl@sss.pgh.pa.us 1615 : 32 : ptr++;
1616 : 32 : *ptr = NULL;
1617 : 32 : Conf->nAffixData++;
1618 : :
1619 : 32 : return Conf->nAffixData - 1;
1620 : : }
1621 : :
1622 : : /*
1623 : : * Returns a set of affix parameters which correspondence to the set of affix
1624 : : * flags with the given index.
1625 : : */
1626 : : static uint32
6505 bruce@momjian.us 1627 : 543 : makeCompoundFlags(IspellDict *Conf, int affix)
1628 : : {
2135 tgl@sss.pgh.pa.us 1629 [ - + ]: 543 : Assert(affix < Conf->nAffixData);
1630 : :
1631 : 543 : return (getCompoundAffixFlagValue(Conf, Conf->AffixData[affix]) &
1632 : : FF_COMPOUNDFLAGMASK);
1633 : : }
1634 : :
1635 : : /*
1636 : : * Makes a prefix tree for the given level.
1637 : : *
1638 : : * Conf: current dictionary.
1639 : : * low: lower index of the Conf->Spell array.
1640 : : * high: upper index of the Conf->Spell array.
1641 : : * level: current prefix tree level.
1642 : : */
1643 : : static SPNode *
6505 bruce@momjian.us 1644 : 2172 : mkSPNode(IspellDict *Conf, int low, int high, int level)
1645 : : {
1646 : : int i;
6591 tgl@sss.pgh.pa.us 1647 : 2172 : int nchar = 0;
1648 : 2172 : char lastchar = '\0';
1649 : : SPNode *rs;
1650 : : SPNodeData *data;
1651 : 2172 : int lownew = low;
1652 : :
1653 [ + + ]: 7138 : for (i = low; i < high; i++)
1654 [ + + + + ]: 4966 : if (Conf->Spell[i]->p.d.len > level && lastchar != Conf->Spell[i]->word[level])
1655 : : {
1656 : 2129 : nchar++;
1657 : 2129 : lastchar = Conf->Spell[i]->word[level];
1658 : : }
1659 : :
1660 [ + + ]: 2172 : if (!nchar)
1661 : 311 : return NULL;
1662 : :
5449 1663 : 1861 : rs = (SPNode *) cpalloc0(SPNHDRSZ + nchar * sizeof(SPNodeData));
6591 1664 : 1861 : rs->length = nchar;
1665 : 1861 : data = rs->data;
1666 : :
1667 : 1861 : lastchar = '\0';
1668 [ + + ]: 6295 : for (i = low; i < high; i++)
1669 [ + + ]: 4443 : if (Conf->Spell[i]->p.d.len > level)
1670 : : {
1671 [ + + ]: 3192 : if (lastchar != Conf->Spell[i]->word[level])
1672 : : {
1673 [ + + ]: 2123 : if (lastchar)
1674 : : {
1675 : : /* Next level of the prefix tree */
1676 : 262 : data->node = mkSPNode(Conf, lownew, i, level + 1);
1677 : 256 : lownew = i;
1678 : 256 : data++;
1679 : : }
1680 : 2117 : lastchar = Conf->Spell[i]->word[level];
1681 : : }
1682 : 3186 : data->val = ((uint8 *) (Conf->Spell[i]->word))[level];
1683 [ + + ]: 3186 : if (Conf->Spell[i]->p.d.len == level + 1)
1684 : : {
1685 : 511 : bool clearCompoundOnly = false;
1686 : :
1687 [ + + + - ]: 511 : if (data->isword && data->affix != Conf->Spell[i]->p.d.affix)
1688 : : {
1689 : : /*
1690 : : * MergeAffix called a few times. If one of word is
1691 : : * allowed to be in compound word and another isn't, then
1692 : : * clear FF_COMPOUNDONLY flag.
1693 : : */
1694 : :
1695 : 64 : clearCompoundOnly = (FF_COMPOUNDONLY & data->compoundflag
2999 1696 : 32 : & makeCompoundFlags(Conf, Conf->Spell[i]->p.d.affix))
1697 : : ? false : true;
6591 1698 : 32 : data->affix = MergeAffix(Conf, data->affix, Conf->Spell[i]->p.d.affix);
1699 : : }
1700 : : else
1701 : 479 : data->affix = Conf->Spell[i]->p.d.affix;
1702 : 511 : data->isword = 1;
1703 : :
1704 : 511 : data->compoundflag = makeCompoundFlags(Conf, data->affix);
1705 : :
1706 [ - + ]: 508 : if ((data->compoundflag & FF_COMPOUNDONLY) &&
6591 tgl@sss.pgh.pa.us 1707 [ # # ]:UBC 0 : (data->compoundflag & FF_COMPOUNDFLAG) == 0)
1708 : 0 : data->compoundflag |= FF_COMPOUNDFLAG;
1709 : :
6591 tgl@sss.pgh.pa.us 1710 [ + + ]:CBC 508 : if (clearCompoundOnly)
1711 : 32 : data->compoundflag &= ~FF_COMPOUNDONLY;
1712 : : }
1713 : : }
1714 : :
1715 : : /* Next level of the prefix tree */
1716 : 1852 : data->node = mkSPNode(Conf, lownew, high, level + 1);
1717 : :
1718 : 1849 : return rs;
1719 : : }
1720 : :
1721 : : /*
1722 : : * Builds the Conf->Dictionary tree and AffixData from the imported dictionary
1723 : : * and affixes.
1724 : : */
1725 : : void
6505 bruce@momjian.us 1726 : 64 : NISortDictionary(IspellDict *Conf)
1727 : : {
1728 : : int i;
1729 : : int naffix;
1730 : : int curaffix;
1731 : :
1732 : : /* compress affixes */
1733 : :
1734 : : /*
1735 : : * If we use flag aliases then we need to use Conf->AffixData filled in
1736 : : * the NIImportOOAffixes().
1737 : : */
3473 teodor@sigaev.ru 1738 [ + + ]: 64 : if (Conf->useFlagAliases)
1739 : : {
1740 [ + + ]: 126 : for (i = 0; i < Conf->nspell; i++)
1741 : : {
1742 : : char *end;
1743 : :
3460 1744 [ + + ]: 116 : if (*Conf->Spell[i]->p.flag != '\0')
1745 : : {
182 tgl@sss.pgh.pa.us 1746 : 106 : errno = 0;
3460 teodor@sigaev.ru 1747 : 106 : curaffix = strtol(Conf->Spell[i]->p.flag, &end, 10);
1748 [ + + - + ]: 106 : if (Conf->Spell[i]->p.flag == end || errno == ERANGE)
1749 [ + - ]: 3 : ereport(ERROR,
1750 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1751 : : errmsg("invalid affix alias \"%s\"",
1752 : : Conf->Spell[i]->p.flag)));
2135 tgl@sss.pgh.pa.us 1753 [ + - + + ]: 103 : if (curaffix < 0 || curaffix >= Conf->nAffixData)
1754 [ + - ]: 3 : ereport(ERROR,
1755 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1756 : : errmsg("invalid affix alias \"%s\"",
1757 : : Conf->Spell[i]->p.flag)));
263 peter@eisentraut.org 1758 [ - + - - : 100 : if (*end != '\0' && !isdigit((unsigned char) *end) && !isspace((unsigned char) *end))
- - ]
2135 tgl@sss.pgh.pa.us 1759 [ # # ]:UBC 0 : ereport(ERROR,
1760 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1761 : : errmsg("invalid affix alias \"%s\"",
1762 : : Conf->Spell[i]->p.flag)));
1763 : : }
1764 : : else
1765 : : {
1766 : : /*
1767 : : * If Conf->Spell[i]->p.flag is empty, then get empty value of
1768 : : * Conf->AffixData (0 index).
1769 : : */
3460 teodor@sigaev.ru 1770 :CBC 10 : curaffix = 0;
1771 : : }
1772 : :
1773 : 110 : Conf->Spell[i]->p.d.affix = curaffix;
3473 1774 : 110 : Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1775 : : }
1776 : : }
1777 : : /* Otherwise fill Conf->AffixData here */
1778 : : else
1779 : : {
1780 : : /* Count the number of different flags used in the dictionary */
942 peter@eisentraut.org 1781 : 48 : qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *),
1782 : : cmpspellaffix);
1783 : :
3473 teodor@sigaev.ru 1784 : 48 : naffix = 0;
1785 [ + + ]: 470 : for (i = 0; i < Conf->nspell; i++)
1786 : : {
2339 michael@paquier.xyz 1787 [ + + ]: 422 : if (i == 0 ||
1788 [ + + ]: 374 : strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag) != 0)
3473 teodor@sigaev.ru 1789 : 374 : naffix++;
1790 : : }
1791 : :
1792 : : /*
1793 : : * Fill in Conf->AffixData with the affixes that were used in the
1794 : : * dictionary. Replace textual flag-field of Conf->Spell entries with
1795 : : * indexes into Conf->AffixData array.
1796 : : */
396 heikki.linnakangas@i 1797 : 48 : Conf->AffixData = (const char **) palloc0(naffix * sizeof(const char *));
1798 : :
3473 teodor@sigaev.ru 1799 : 48 : curaffix = -1;
1800 [ + + ]: 470 : for (i = 0; i < Conf->nspell; i++)
1801 : : {
2339 michael@paquier.xyz 1802 [ + + ]: 422 : if (i == 0 ||
1803 [ + + ]: 374 : strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]) != 0)
1804 : : {
3473 teodor@sigaev.ru 1805 : 374 : curaffix++;
1806 [ - + ]: 374 : Assert(curaffix < naffix);
1807 : 374 : Conf->AffixData[curaffix] = cpstrdup(Conf,
3376 rhaas@postgresql.org 1808 : 374 : Conf->Spell[i]->p.flag);
1809 : : }
1810 : :
3473 teodor@sigaev.ru 1811 : 422 : Conf->Spell[i]->p.d.affix = curaffix;
1812 : 422 : Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1813 : : }
1814 : :
1815 : 48 : Conf->lenAffixData = Conf->nAffixData = naffix;
1816 : : }
1817 : :
1818 : : /* Start build a prefix tree */
942 peter@eisentraut.org 1819 : 58 : qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *), cmpspell);
6591 tgl@sss.pgh.pa.us 1820 : 58 : Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0);
1821 : 55 : }
1822 : :
1823 : : /*
1824 : : * Makes a prefix tree for the given level using the repl string of an affix
1825 : : * rule. Affixes with empty replace string do not include in the prefix tree.
1826 : : * This affixes are included by mkVoidAffix().
1827 : : *
1828 : : * Conf: current dictionary.
1829 : : * low: lower index of the Conf->Affix array.
1830 : : * high: upper index of the Conf->Affix array.
1831 : : * level: current prefix tree level.
1832 : : * type: FF_SUFFIX or FF_PREFIX.
1833 : : */
1834 : : static AffixNode *
6505 bruce@momjian.us 1835 : 928 : mkANode(IspellDict *Conf, int low, int high, int level, int type)
1836 : : {
1837 : : int i;
6591 tgl@sss.pgh.pa.us 1838 : 928 : int nchar = 0;
1839 : 928 : uint8 lastchar = '\0';
1840 : : AffixNode *rs;
1841 : : AffixNodeData *data;
1842 : 928 : int lownew = low;
1843 : : int naff;
1844 : : AFFIX **aff;
1845 : :
1846 [ + + ]: 2497 : for (i = low; i < high; i++)
1847 [ + + + + : 1569 : if (Conf->Affix[i].replen > level && lastchar != GETCHAR(Conf->Affix + i, level, type))
+ + ]
1848 : : {
1849 : 818 : nchar++;
1850 [ + + ]: 818 : lastchar = GETCHAR(Conf->Affix + i, level, type);
1851 : : }
1852 : :
1853 [ + + ]: 928 : if (!nchar)
1854 : 354 : return NULL;
1855 : :
1856 : 574 : aff = (AFFIX **) tmpalloc(sizeof(AFFIX *) * (high - low + 1));
1857 : 574 : naff = 0;
1858 : :
5449 1859 : 574 : rs = (AffixNode *) cpalloc0(ANHRDSZ + nchar * sizeof(AffixNodeData));
6591 1860 : 574 : rs->length = nchar;
1861 : 574 : data = rs->data;
1862 : :
1863 : 574 : lastchar = '\0';
1864 [ + + ]: 1700 : for (i = low; i < high; i++)
1865 [ + + ]: 1126 : if (Conf->Affix[i].replen > level)
1866 : : {
1867 [ + + + + ]: 948 : if (lastchar != GETCHAR(Conf->Affix + i, level, type))
1868 : : {
1869 [ + + ]: 818 : if (lastchar)
1870 : : {
1871 : : /* Next level of the prefix tree */
1872 : 244 : data->node = mkANode(Conf, lownew, i, level + 1, type);
1873 [ + + ]: 244 : if (naff)
1874 : : {
1875 : 55 : data->naff = naff;
5449 1876 : 55 : data->aff = (AFFIX **) cpalloc(sizeof(AFFIX *) * naff);
6591 1877 : 55 : memcpy(data->aff, aff, sizeof(AFFIX *) * naff);
1878 : 55 : naff = 0;
1879 : : }
1880 : 244 : data++;
1881 : 244 : lownew = i;
1882 : : }
1883 [ + + ]: 818 : lastchar = GETCHAR(Conf->Affix + i, level, type);
1884 : : }
1885 [ + + ]: 948 : data->val = GETCHAR(Conf->Affix + i, level, type);
1886 [ + + ]: 948 : if (Conf->Affix[i].replen == level + 1)
1887 : : { /* affix stopped */
1888 : 429 : aff[naff++] = Conf->Affix + i;
1889 : : }
1890 : : }
1891 : :
1892 : : /* Next level of the prefix tree */
1893 : 574 : data->node = mkANode(Conf, lownew, high, level + 1, type);
1894 [ + + ]: 574 : if (naff)
1895 : : {
1896 : 354 : data->naff = naff;
5449 1897 : 354 : data->aff = (AFFIX **) cpalloc(sizeof(AFFIX *) * naff);
6591 1898 : 354 : memcpy(data->aff, aff, sizeof(AFFIX *) * naff);
1899 : 354 : naff = 0;
1900 : : }
1901 : :
1902 : 574 : pfree(aff);
1903 : :
1904 : 574 : return rs;
1905 : : }
1906 : :
1907 : : /*
1908 : : * Makes the root void node in the prefix tree. The root void node is created
1909 : : * for affixes which have empty replace string ("repl" field).
1910 : : */
1911 : : static void
6505 bruce@momjian.us 1912 : 110 : mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
1913 : : {
1914 : : int i,
6591 tgl@sss.pgh.pa.us 1915 : 110 : cnt = 0;
1916 [ + + ]: 110 : int start = (issuffix) ? startsuffix : 0;
1917 [ + + ]: 110 : int end = (issuffix) ? Conf->naffixes : startsuffix;
1918 : 110 : AffixNode *Affix = (AffixNode *) palloc0(ANHRDSZ + sizeof(AffixNodeData));
1919 : :
1920 : 110 : Affix->length = 1;
1921 : 110 : Affix->isvoid = 1;
1922 : :
1923 [ + + ]: 110 : if (issuffix)
1924 : : {
1925 : 55 : Affix->data->node = Conf->Suffix;
1926 : 55 : Conf->Suffix = Affix;
1927 : : }
1928 : : else
1929 : : {
1930 : 55 : Affix->data->node = Conf->Prefix;
1931 : 55 : Conf->Prefix = Affix;
1932 : : }
1933 : :
1934 : : /* Count affixes with empty replace string */
1935 [ + + ]: 553 : for (i = start; i < end; i++)
1936 [ + + ]: 443 : if (Conf->Affix[i].replen == 0)
1937 : 14 : cnt++;
1938 : :
1939 : : /* There is not affixes with empty replace string */
1940 [ + + ]: 110 : if (cnt == 0)
1941 : 96 : return;
1942 : :
5449 1943 : 14 : Affix->data->aff = (AFFIX **) cpalloc(sizeof(AFFIX *) * cnt);
6591 1944 : 14 : Affix->data->naff = (uint32) cnt;
1945 : :
1946 : 14 : cnt = 0;
1947 [ + + ]: 112 : for (i = start; i < end; i++)
1948 [ + + ]: 98 : if (Conf->Affix[i].replen == 0)
1949 : : {
1950 : 14 : Affix->data->aff[cnt] = Conf->Affix + i;
1951 : 14 : cnt++;
1952 : : }
1953 : : }
1954 : :
1955 : : /*
1956 : : * Checks if the affixflag is used by dictionary. Conf->AffixData does not
1957 : : * contain affixflag if this flag is not used actually by the .dict file.
1958 : : *
1959 : : * Conf: current dictionary.
1960 : : * affixflag: affix flag.
1961 : : *
1962 : : * Returns true if the Conf->AffixData array contains affixflag, otherwise
1963 : : * returns false.
1964 : : */
1965 : : static bool
396 heikki.linnakangas@i 1966 : 75 : isAffixInUse(IspellDict *Conf, const char *affixflag)
1967 : : {
1968 : : int i;
1969 : :
6591 tgl@sss.pgh.pa.us 1970 [ + + ]: 551 : for (i = 0; i < Conf->nAffixData; i++)
3473 teodor@sigaev.ru 1971 [ + + ]: 539 : if (IsAffixFlagInUse(Conf, i, affixflag))
6591 tgl@sss.pgh.pa.us 1972 : 63 : return true;
1973 : :
1974 : 12 : return false;
1975 : : }
1976 : :
1977 : : /*
1978 : : * Builds Conf->Prefix and Conf->Suffix trees from the imported affixes.
1979 : : */
1980 : : void
6505 bruce@momjian.us 1981 : 55 : NISortAffixes(IspellDict *Conf)
1982 : : {
1983 : : AFFIX *Affix;
1984 : : size_t i;
1985 : : CMPDAffix *ptr;
6587 tgl@sss.pgh.pa.us 1986 : 55 : int firstsuffix = Conf->naffixes;
1987 : :
6591 1988 [ - + ]: 55 : if (Conf->naffixes == 0)
6591 tgl@sss.pgh.pa.us 1989 :UBC 0 : return;
1990 : :
1991 : : /* Store compound affixes in the Conf->CompoundAffix array */
6591 tgl@sss.pgh.pa.us 1992 [ + - ]:CBC 55 : if (Conf->naffixes > 1)
942 peter@eisentraut.org 1993 : 55 : qsort(Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix);
6591 tgl@sss.pgh.pa.us 1994 : 55 : Conf->CompoundAffix = ptr = (CMPDAffix *) palloc(sizeof(CMPDAffix) * Conf->naffixes);
1995 : 55 : ptr->affix = NULL;
1996 : :
1997 [ + + ]: 498 : for (i = 0; i < Conf->naffixes; i++)
1998 : : {
1999 : 443 : Affix = &(((AFFIX *) Conf->Affix)[i]);
6587 2000 [ + + + + ]: 443 : if (Affix->type == FF_SUFFIX && i < firstsuffix)
6591 2001 : 55 : firstsuffix = i;
2002 : :
2003 [ + + + - : 518 : if ((Affix->flagflags & FF_COMPOUNDFLAG) && Affix->replen > 0 &&
+ + ]
3473 teodor@sigaev.ru 2004 : 75 : isAffixInUse(Conf, Affix->flag))
2005 : : {
2704 tgl@sss.pgh.pa.us 2006 : 63 : bool issuffix = (Affix->type == FF_SUFFIX);
2007 : :
6591 2008 [ + + ]: 63 : if (ptr == Conf->CompoundAffix ||
2704 2009 [ + - + + ]: 40 : issuffix != (ptr - 1)->issuffix ||
6591 2010 : 20 : strbncmp((const unsigned char *) (ptr - 1)->affix,
2011 : 20 : (const unsigned char *) Affix->repl,
2012 : 20 : (ptr - 1)->len))
2013 : : {
2014 : : /* leave only unique and minimal suffixes */
2015 : 53 : ptr->affix = Affix->repl;
2016 : 53 : ptr->len = Affix->replen;
2704 2017 : 53 : ptr->issuffix = issuffix;
6591 2018 : 53 : ptr++;
2019 : : }
2020 : : }
2021 : : }
2022 : 55 : ptr->affix = NULL;
2023 : 55 : Conf->CompoundAffix = (CMPDAffix *) repalloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1));
2024 : :
2025 : : /* Start build a prefix tree */
2026 : 55 : Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX);
2027 : 55 : Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX);
6587 2028 : 55 : mkVoidAffix(Conf, true, firstsuffix);
2029 : 55 : mkVoidAffix(Conf, false, firstsuffix);
2030 : : }
2031 : :
2032 : : static AffixNodeData *
6505 bruce@momjian.us 2033 : 2310 : FindAffixes(AffixNode *node, const char *word, int wrdlen, int *level, int type)
2034 : : {
2035 : : AffixNodeData *StopLow,
2036 : : *StopHigh,
2037 : : *StopMiddle;
2038 : : uint8 symbol;
2039 : :
6591 tgl@sss.pgh.pa.us 2040 [ + + ]: 2310 : if (node->isvoid)
2041 : : { /* search void affixes */
2042 [ + + ]: 2010 : if (node->data->naff)
2043 : 171 : return node->data;
2044 : 1839 : node = node->data->node;
2045 : : }
2046 : :
2047 [ + - + + ]: 2691 : while (node && *level < wrdlen)
2048 : : {
2049 : 2679 : StopLow = node->data;
2050 : 2679 : StopHigh = node->data + node->length;
2051 [ + + ]: 5913 : while (StopLow < StopHigh)
2052 : : {
2053 : 4437 : StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
2054 [ + + ]: 4437 : symbol = GETWCHAR(word, wrdlen, *level, type);
2055 : :
2056 [ + + ]: 4437 : if (StopMiddle->val == symbol)
2057 : : {
2058 : 1203 : (*level)++;
2059 [ + + ]: 1203 : if (StopMiddle->naff)
2060 : 651 : return StopMiddle;
2061 : 552 : node = StopMiddle->node;
2062 : 552 : break;
2063 : : }
2064 [ + + ]: 3234 : else if (StopMiddle->val < symbol)
2065 : 804 : StopLow = StopMiddle + 1;
2066 : : else
2067 : 2430 : StopHigh = StopMiddle;
2068 : : }
2069 [ + + ]: 2028 : if (StopLow >= StopHigh)
2070 : 1476 : break;
2071 : : }
2072 : 1488 : return NULL;
2073 : : }
2074 : :
2075 : : static char *
6505 bruce@momjian.us 2076 : 918 : CheckAffix(const char *word, size_t len, AFFIX *Affix, int flagflags, char *newword, int *baselen)
2077 : : {
2078 : : /*
2079 : : * Check compound allow flags
2080 : : */
2081 : :
6591 tgl@sss.pgh.pa.us 2082 [ + + ]: 918 : if (flagflags == 0)
2083 : : {
2084 [ + + ]: 633 : if (Affix->flagflags & FF_COMPOUNDONLY)
2085 : 66 : return NULL;
2086 : : }
2087 [ - + ]: 285 : else if (flagflags & FF_COMPOUNDBEGIN)
2088 : : {
6591 tgl@sss.pgh.pa.us 2089 [ # # ]:UBC 0 : if (Affix->flagflags & FF_COMPOUNDFORBIDFLAG)
2090 : 0 : return NULL;
2091 [ # # ]: 0 : if ((Affix->flagflags & FF_COMPOUNDBEGIN) == 0)
2092 [ # # ]: 0 : if (Affix->type == FF_SUFFIX)
2093 : 0 : return NULL;
2094 : : }
6591 tgl@sss.pgh.pa.us 2095 [ + + ]:CBC 285 : else if (flagflags & FF_COMPOUNDMIDDLE)
2096 : : {
2097 [ + + ]: 204 : if ((Affix->flagflags & FF_COMPOUNDMIDDLE) == 0 ||
2098 [ - + ]: 114 : (Affix->flagflags & FF_COMPOUNDFORBIDFLAG))
2099 : 90 : return NULL;
2100 : : }
2101 [ + - ]: 81 : else if (flagflags & FF_COMPOUNDLAST)
2102 : : {
2103 [ - + ]: 81 : if (Affix->flagflags & FF_COMPOUNDFORBIDFLAG)
6591 tgl@sss.pgh.pa.us 2104 :UBC 0 : return NULL;
6591 tgl@sss.pgh.pa.us 2105 [ + + ]:CBC 81 : if ((Affix->flagflags & FF_COMPOUNDLAST) == 0)
2106 [ - + ]: 75 : if (Affix->type == FF_PREFIX)
6591 tgl@sss.pgh.pa.us 2107 :UBC 0 : return NULL;
2108 : : }
2109 : :
2110 : : /*
2111 : : * make replace pattern of affix
2112 : : */
6591 tgl@sss.pgh.pa.us 2113 [ + + ]:CBC 762 : if (Affix->type == FF_SUFFIX)
2114 : : {
2115 : 522 : strcpy(newword, word);
2116 : 522 : strcpy(newword + len - Affix->replen, Affix->find);
2117 [ + - ]: 522 : if (baselen) /* store length of non-changed part of word */
2118 : 522 : *baselen = len - Affix->replen;
2119 : : }
2120 : : else
2121 : : {
2122 : : /*
2123 : : * if prefix is an all non-changed part's length then all word
2124 : : * contains only prefix and suffix, so out
2125 : : */
2126 [ + + - + ]: 240 : if (baselen && *baselen + strlen(Affix->find) <= Affix->replen)
6591 tgl@sss.pgh.pa.us 2127 :UBC 0 : return NULL;
6591 tgl@sss.pgh.pa.us 2128 :CBC 240 : strcpy(newword, Affix->find);
2129 : 240 : strcat(newword, word + Affix->replen);
2130 : : }
2131 : :
2132 : : /*
2133 : : * check resulting word
2134 : : */
2135 [ + + ]: 762 : if (Affix->issimple)
2136 : 240 : return newword;
2137 [ + + ]: 522 : else if (Affix->isregis)
2138 : : {
2139 [ + + ]: 354 : if (RS_execute(&(Affix->reg.regis), newword))
2140 : 336 : return newword;
2141 : : }
2142 : : else
2143 : : {
2144 : : pg_wchar *data;
2145 : : size_t data_len;
2146 : : int newword_len;
2147 : :
2148 : : /* Convert data string to wide characters */
2149 : 168 : newword_len = strlen(newword);
2150 : 168 : data = (pg_wchar *) palloc((newword_len + 1) * sizeof(pg_wchar));
2151 : 168 : data_len = pg_mb2wchar_with_len(newword, data, newword_len);
2152 : :
882 tmunro@postgresql.or 2153 [ + - ]: 168 : if (pg_regexec(Affix->reg.pregex, data, data_len,
2154 : : 0, NULL, 0, NULL, 0) == REG_OKAY)
2155 : : {
6591 tgl@sss.pgh.pa.us 2156 : 168 : pfree(data);
2157 : 168 : return newword;
2158 : : }
6591 tgl@sss.pgh.pa.us 2159 :UBC 0 : pfree(data);
2160 : : }
2161 : :
6591 tgl@sss.pgh.pa.us 2162 :CBC 18 : return NULL;
2163 : : }
2164 : :
2165 : : static int
2166 : 270 : addToResult(char **forms, char **cur, char *word)
2167 : : {
2168 [ - + ]: 270 : if (cur - forms >= MAX_NORM - 1)
6591 tgl@sss.pgh.pa.us 2169 :UBC 0 : return 0;
6591 tgl@sss.pgh.pa.us 2170 [ + + + - ]:CBC 270 : if (forms == cur || strcmp(word, *(cur - 1)) != 0)
2171 : : {
2172 : 270 : *cur = pstrdup(word);
5931 bruce@momjian.us 2173 : 270 : *(cur + 1) = NULL;
6591 tgl@sss.pgh.pa.us 2174 : 270 : return 1;
2175 : : }
2176 : :
6591 tgl@sss.pgh.pa.us 2177 :UBC 0 : return 0;
2178 : : }
2179 : :
2180 : : static char **
396 heikki.linnakangas@i 2181 :CBC 753 : NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
2182 : : {
6591 tgl@sss.pgh.pa.us 2183 : 753 : AffixNodeData *suffix = NULL,
2184 : 753 : *prefix = NULL;
2185 : 753 : int slevel = 0,
2186 : 753 : plevel = 0;
2187 : 753 : int wrdlen = strlen(word),
2188 : : swrdlen;
2189 : : char **forms;
2190 : : char **cur;
2191 : 753 : char newword[2 * MAXNORMLEN] = "";
2192 : 753 : char pnewword[2 * MAXNORMLEN] = "";
2193 : 753 : AffixNode *snode = Conf->Suffix,
2194 : : *pnode;
2195 : : int i,
2196 : : j;
2197 : :
2198 [ - + ]: 753 : if (wrdlen > MAXNORMLEN)
6591 tgl@sss.pgh.pa.us 2199 :UBC 0 : return NULL;
6591 tgl@sss.pgh.pa.us 2200 :CBC 753 : cur = forms = (char **) palloc(MAX_NORM * sizeof(char *));
2201 : 753 : *cur = NULL;
2202 : :
2203 : :
2204 : : /* Check that the word itself is normal form */
3460 teodor@sigaev.ru 2205 [ + + ]: 753 : if (FindWord(Conf, word, VoidString, flag))
2206 : : {
6591 tgl@sss.pgh.pa.us 2207 : 234 : *cur = pstrdup(word);
2208 : 234 : cur++;
2209 : 234 : *cur = NULL;
2210 : : }
2211 : :
2212 : : /* Find all other NORMAL forms of the 'word' (check only prefix) */
2213 : 753 : pnode = Conf->Prefix;
2214 : 753 : plevel = 0;
2215 [ + + ]: 861 : while (pnode)
2216 : : {
6587 2217 : 753 : prefix = FindAffixes(pnode, word, wrdlen, &plevel, FF_PREFIX);
6591 2218 [ + + ]: 753 : if (!prefix)
2219 : 645 : break;
2220 [ + + ]: 216 : for (j = 0; j < prefix->naff; j++)
2221 : : {
2222 [ + + ]: 108 : if (CheckAffix(word, wrdlen, prefix->aff[j], flag, newword, NULL))
2223 : : {
2224 : : /* prefix success */
2225 [ + + ]: 96 : if (FindWord(Conf, newword, prefix->aff[j]->flag, flag))
2226 : 24 : cur += addToResult(forms, cur, newword);
2227 : : }
2228 : : }
2229 : 108 : pnode = prefix->node;
2230 : : }
2231 : :
2232 : : /*
2233 : : * Find all other NORMAL forms of the 'word' (check suffix and then
2234 : : * prefix)
2235 : : */
2236 [ + + ]: 1299 : while (snode)
2237 : : {
2238 : 1053 : int baselen = 0;
2239 : :
2240 : : /* find possible suffix */
6587 2241 : 1053 : suffix = FindAffixes(snode, word, wrdlen, &slevel, FF_SUFFIX);
6591 2242 [ + + ]: 1053 : if (!suffix)
2243 : 507 : break;
2244 : : /* foreach suffix check affix */
2245 [ + + ]: 1188 : for (i = 0; i < suffix->naff; i++)
2246 : : {
2247 [ + + ]: 642 : if (CheckAffix(word, wrdlen, suffix->aff[i], flag, newword, &baselen))
2248 : : {
2249 : : /* suffix success */
2250 [ + + ]: 504 : if (FindWord(Conf, newword, suffix->aff[i]->flag, flag))
2251 : 138 : cur += addToResult(forms, cur, newword);
2252 : :
2253 : : /* now we will look changed word with prefixes */
2254 : 504 : pnode = Conf->Prefix;
2255 : 504 : plevel = 0;
2256 : 504 : swrdlen = strlen(newword);
2257 [ + + ]: 672 : while (pnode)
2258 : : {
6587 2259 : 504 : prefix = FindAffixes(pnode, newword, swrdlen, &plevel, FF_PREFIX);
6591 2260 [ + + ]: 504 : if (!prefix)
2261 : 336 : break;
2262 [ + + ]: 336 : for (j = 0; j < prefix->naff; j++)
2263 : : {
2264 [ + + ]: 168 : if (CheckAffix(newword, swrdlen, prefix->aff[j], flag, pnewword, &baselen))
2265 : : {
2266 : : /* prefix success */
396 heikki.linnakangas@i 2267 : 288 : const char *ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ?
841 tgl@sss.pgh.pa.us 2268 [ + + ]: 144 : VoidString : prefix->aff[j]->flag;
2269 : :
6591 2270 [ + + ]: 144 : if (FindWord(Conf, pnewword, ff, flag))
2271 : 108 : cur += addToResult(forms, cur, pnewword);
2272 : : }
2273 : : }
2274 : 168 : pnode = prefix->node;
2275 : : }
2276 : : }
2277 : : }
2278 : :
2279 : 546 : snode = suffix->node;
2280 : : }
2281 : :
2282 [ + + ]: 753 : if (cur == forms)
2283 : : {
2284 : 333 : pfree(forms);
2942 peter_e@gmx.net 2285 : 333 : return NULL;
2286 : : }
2287 : 420 : return forms;
2288 : : }
2289 : :
2290 : : typedef struct SplitVar
2291 : : {
2292 : : int nstem;
2293 : : int lenstem;
2294 : : char **stem;
2295 : : struct SplitVar *next;
2296 : : } SplitVar;
2297 : :
2298 : : static int
396 heikki.linnakangas@i 2299 : 3030 : CheckCompoundAffixes(CMPDAffix **ptr, const char *word, int len, bool CheckInPlace)
2300 : : {
2301 : : bool issuffix;
2302 : :
2303 : : /* in case CompoundAffix is null: */
3971 tgl@sss.pgh.pa.us 2304 [ - + ]: 3030 : if (*ptr == NULL)
3971 tgl@sss.pgh.pa.us 2305 :UBC 0 : return -1;
2306 : :
6591 tgl@sss.pgh.pa.us 2307 [ + + ]:CBC 3030 : if (CheckInPlace)
2308 : : {
2309 [ + + ]: 5784 : while ((*ptr)->affix)
2310 : : {
2311 [ + + + + ]: 3222 : if (len > (*ptr)->len && strncmp((*ptr)->affix, word, (*ptr)->len) == 0)
2312 : : {
2313 : 30 : len = (*ptr)->len;
2314 : 30 : issuffix = (*ptr)->issuffix;
2315 : 30 : (*ptr)++;
2316 [ + - ]: 30 : return (issuffix) ? len : 0;
2317 : : }
2318 : 3192 : (*ptr)++;
2319 : : }
2320 : : }
2321 : : else
2322 : : {
2323 : : char *affbegin;
2324 : :
2325 [ + + ]: 846 : while ((*ptr)->affix)
2326 : : {
2327 [ + + + + ]: 471 : if (len > (*ptr)->len && (affbegin = strstr(word, (*ptr)->affix)) != NULL)
2328 : : {
2329 : 63 : len = (*ptr)->len + (affbegin - word);
2330 : 63 : issuffix = (*ptr)->issuffix;
2331 : 63 : (*ptr)++;
2332 [ + - ]: 63 : return (issuffix) ? len : 0;
2333 : : }
2334 : 408 : (*ptr)++;
2335 : : }
2336 : : }
2337 : 2937 : return -1;
2338 : : }
2339 : :
2340 : : static SplitVar *
6505 bruce@momjian.us 2341 : 705 : CopyVar(SplitVar *s, int makedup)
2342 : : {
6591 tgl@sss.pgh.pa.us 2343 : 705 : SplitVar *v = (SplitVar *) palloc(sizeof(SplitVar));
2344 : :
2345 : 705 : v->next = NULL;
2346 [ + + ]: 705 : if (s)
2347 : : {
2348 : : int i;
2349 : :
6443 teodor@sigaev.ru 2350 : 330 : v->lenstem = s->lenstem;
2351 : 330 : v->stem = (char **) palloc(sizeof(char *) * v->lenstem);
6591 tgl@sss.pgh.pa.us 2352 : 330 : v->nstem = s->nstem;
2353 [ + + ]: 501 : for (i = 0; i < s->nstem; i++)
2354 [ + + ]: 171 : v->stem[i] = (makedup) ? pstrdup(s->stem[i]) : s->stem[i];
2355 : : }
2356 : : else
2357 : : {
6443 teodor@sigaev.ru 2358 : 375 : v->lenstem = 16;
2359 : 375 : v->stem = (char **) palloc(sizeof(char *) * v->lenstem);
6591 tgl@sss.pgh.pa.us 2360 : 375 : v->nstem = 0;
2361 : : }
2362 : 705 : return v;
2363 : : }
2364 : :
2365 : : static void
6443 teodor@sigaev.ru 2366 : 945 : AddStem(SplitVar *v, char *word)
2367 : : {
5931 bruce@momjian.us 2368 [ - + ]: 945 : if (v->nstem >= v->lenstem)
2369 : : {
6443 teodor@sigaev.ru 2370 :UBC 0 : v->lenstem *= 2;
2371 : 0 : v->stem = (char **) repalloc(v->stem, sizeof(char *) * v->lenstem);
2372 : : }
2373 : :
6443 teodor@sigaev.ru 2374 :CBC 945 : v->stem[v->nstem] = word;
2375 : 945 : v->nstem++;
2376 : 945 : }
2377 : :
2378 : : static SplitVar *
396 heikki.linnakangas@i 2379 : 660 : SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
2380 : : {
6591 tgl@sss.pgh.pa.us 2381 : 660 : SplitVar *var = NULL;
2382 : : SPNodeData *StopLow,
2383 : : *StopHigh,
2384 : 660 : *StopMiddle = NULL;
2385 [ + + ]: 660 : SPNode *node = (snode) ? snode : Conf->Dictionary;
2386 [ + + ]: 660 : int level = (snode) ? minpos : startpos; /* recursive
2387 : : * minpos==level */
2388 : : int lenaff;
2389 : : CMPDAffix *caff;
2390 : : char *notprobed;
2391 : 660 : int compoundflag = 0;
2392 : :
2393 : : /* since this function recurses, it could be driven to stack overflow */
1109 2394 : 660 : check_stack_depth();
2395 : :
6591 2396 : 660 : notprobed = (char *) palloc(wordlen);
2397 : 660 : memset(notprobed, 1, wordlen);
2398 : 660 : var = CopyVar(orig, 1);
2399 : :
2400 [ + + ]: 3726 : while (level < wordlen)
2401 : : {
2402 : : /* find word with epenthetic or/and compound affix */
2403 : 3597 : caff = Conf->CompoundAffix;
2404 [ + + + + ]: 3690 : while (level > startpos && (lenaff = CheckCompoundAffixes(&caff, word + level, wordlen - level, (node) ? true : false)) >= 0)
2405 : : {
2406 : : /*
2407 : : * there is one of compound affixes, so check word for existings
2408 : : */
2409 : : char buf[MAXNORMLEN];
2410 : : char **subres;
2411 : :
2412 : 93 : lenaff = level - startpos + lenaff;
2413 : :
2414 [ - + ]: 93 : if (!notprobed[startpos + lenaff - 1])
6591 tgl@sss.pgh.pa.us 2415 :UBC 0 : continue;
2416 : :
6591 tgl@sss.pgh.pa.us 2417 [ - + ]:CBC 93 : if (level + lenaff - 1 <= minpos)
6591 tgl@sss.pgh.pa.us 2418 :UBC 0 : continue;
2419 : :
5931 bruce@momjian.us 2420 [ - + ]:CBC 93 : if (lenaff >= MAXNORMLEN)
5931 bruce@momjian.us 2421 :UBC 0 : continue; /* skip too big value */
6591 tgl@sss.pgh.pa.us 2422 [ + - ]:CBC 93 : if (lenaff > 0)
2423 : 93 : memcpy(buf, word + startpos, lenaff);
2424 : 93 : buf[lenaff] = '\0';
2425 : :
6443 teodor@sigaev.ru 2426 [ - + ]: 93 : if (level == 0)
6591 tgl@sss.pgh.pa.us 2427 :UBC 0 : compoundflag = FF_COMPOUNDBEGIN;
6591 tgl@sss.pgh.pa.us 2428 [ - + ]:CBC 93 : else if (level == wordlen - 1)
6591 tgl@sss.pgh.pa.us 2429 :UBC 0 : compoundflag = FF_COMPOUNDLAST;
2430 : : else
6591 tgl@sss.pgh.pa.us 2431 :CBC 93 : compoundflag = FF_COMPOUNDMIDDLE;
2432 : 93 : subres = NormalizeSubWord(Conf, buf, compoundflag);
2433 [ + + ]: 93 : if (subres)
2434 : : {
2435 : : /* Yes, it was a word from dictionary */
2436 : 45 : SplitVar *new = CopyVar(var, 0);
2437 : 45 : SplitVar *ptr = var;
2438 : 45 : char **sptr = subres;
2439 : :
2440 : 45 : notprobed[startpos + lenaff - 1] = 0;
2441 : :
2442 [ + + ]: 90 : while (*sptr)
2443 : : {
5931 bruce@momjian.us 2444 : 45 : AddStem(new, *sptr);
6591 tgl@sss.pgh.pa.us 2445 : 45 : sptr++;
2446 : : }
2447 : 45 : pfree(subres);
2448 : :
2449 [ - + ]: 45 : while (ptr->next)
6591 tgl@sss.pgh.pa.us 2450 :UBC 0 : ptr = ptr->next;
6591 tgl@sss.pgh.pa.us 2451 :CBC 45 : ptr->next = SplitToVariants(Conf, NULL, new, word, wordlen, startpos + lenaff, startpos + lenaff);
2452 : :
2453 : 45 : pfree(new->stem);
2454 : 45 : pfree(new);
2455 : : }
2456 : : }
2457 : :
2458 [ + + ]: 3597 : if (!node)
2459 : 375 : break;
2460 : :
2461 : 3222 : StopLow = node->data;
2462 : 3222 : StopHigh = node->data + node->length;
2463 [ + + ]: 4347 : while (StopLow < StopHigh)
2464 : : {
2465 : 4032 : StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
2466 [ + + ]: 4032 : if (StopMiddle->val == ((uint8 *) (word))[level])
2467 : 2907 : break;
2468 [ + + ]: 1125 : else if (StopMiddle->val < ((uint8 *) (word))[level])
2469 : 489 : StopLow = StopMiddle + 1;
2470 : : else
2471 : 636 : StopHigh = StopMiddle;
2472 : : }
2473 : :
2474 [ + + ]: 3222 : if (StopLow < StopHigh)
2475 : : {
3473 teodor@sigaev.ru 2476 [ + + ]: 2907 : if (startpos == 0)
6591 tgl@sss.pgh.pa.us 2477 : 1635 : compoundflag = FF_COMPOUNDBEGIN;
2478 [ + + ]: 1272 : else if (level == wordlen - 1)
2479 : 144 : compoundflag = FF_COMPOUNDLAST;
2480 : : else
2481 : 1128 : compoundflag = FF_COMPOUNDMIDDLE;
2482 : :
2483 : : /* find infinitive */
2484 [ + + ]: 2907 : if (StopMiddle->isword &&
2485 [ + + ]: 768 : (StopMiddle->compoundflag & compoundflag) &&
2486 [ + - ]: 636 : notprobed[level])
2487 : : {
2488 : : /* ok, we found full compoundallowed word */
2489 [ + + ]: 636 : if (level > minpos)
2490 : : {
2491 : : /* and its length more than minimal */
2492 [ + + ]: 396 : if (wordlen == level + 1)
2493 : : {
2494 : : /* well, it was last word */
5931 bruce@momjian.us 2495 : 156 : AddStem(var, pnstrdup(word + startpos, wordlen - startpos));
6591 tgl@sss.pgh.pa.us 2496 : 156 : pfree(notprobed);
2497 : 156 : return var;
2498 : : }
2499 : : else
2500 : 240 : {
2501 : : /* then we will search more big word at the same point */
2502 : 240 : SplitVar *ptr = var;
2503 : :
2504 [ + + ]: 372 : while (ptr->next)
2505 : 132 : ptr = ptr->next;
2506 : 240 : ptr->next = SplitToVariants(Conf, node, var, word, wordlen, startpos, level);
2507 : : /* we can find next word */
2508 : 240 : level++;
5931 bruce@momjian.us 2509 : 240 : AddStem(var, pnstrdup(word + startpos, level - startpos));
6591 tgl@sss.pgh.pa.us 2510 : 240 : node = Conf->Dictionary;
2511 : 240 : startpos = level;
2512 : 240 : continue;
2513 : : }
2514 : : }
2515 : : }
2516 : 2511 : node = StopMiddle->node;
2517 : : }
2518 : : else
2519 : 315 : node = NULL;
2520 : 2826 : level++;
2521 : : }
2522 : :
5931 bruce@momjian.us 2523 : 504 : AddStem(var, pnstrdup(word + startpos, wordlen - startpos));
6591 tgl@sss.pgh.pa.us 2524 : 504 : pfree(notprobed);
2525 : 504 : return var;
2526 : : }
2527 : :
2528 : : static void
5931 bruce@momjian.us 2529 : 657 : addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant)
2530 : : {
2531 [ + + ]: 657 : if (*lres == NULL)
6443 teodor@sigaev.ru 2532 : 303 : *lcur = *lres = (TSLexeme *) palloc(MAX_NORM * sizeof(TSLexeme));
2533 : :
5931 bruce@momjian.us 2534 [ + - ]: 657 : if (*lcur - *lres < MAX_NORM - 1)
2535 : : {
6443 teodor@sigaev.ru 2536 : 657 : (*lcur)->lexeme = word;
2537 : 657 : (*lcur)->flags = flags;
2538 : 657 : (*lcur)->nvariant = NVariant;
2539 : 657 : (*lcur)++;
2540 : 657 : (*lcur)->lexeme = NULL;
2541 : : }
2542 : 657 : }
2543 : :
2544 : : TSLexeme *
396 heikki.linnakangas@i 2545 : 375 : NINormalizeWord(IspellDict *Conf, const char *word)
2546 : : {
2547 : : char **res;
6591 tgl@sss.pgh.pa.us 2548 : 375 : TSLexeme *lcur = NULL,
2549 : 375 : *lres = NULL;
2550 : 375 : uint16 NVariant = 1;
2551 : :
2552 : 375 : res = NormalizeSubWord(Conf, word, 0);
2553 : :
2554 [ + + ]: 375 : if (res)
2555 : : {
2556 : 243 : char **ptr = res;
2557 : :
5931 bruce@momjian.us 2558 [ + + + - ]: 570 : while (*ptr && (lcur - lres) < MAX_NORM)
2559 : : {
2560 : 327 : addNorm(&lres, &lcur, *ptr, 0, NVariant++);
6591 tgl@sss.pgh.pa.us 2561 : 327 : ptr++;
2562 : : }
2563 : 243 : pfree(res);
2564 : : }
2565 : :
2566 [ + - ]: 375 : if (Conf->usecompound)
2567 : : {
2568 : 375 : int wordlen = strlen(word);
2569 : : SplitVar *ptr,
2570 : 375 : *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1);
2571 : : int i;
2572 : :
2573 [ + + ]: 1035 : while (var)
2574 : : {
2575 [ + + ]: 660 : if (var->nstem > 1)
2576 : : {
2577 : 285 : char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDLAST);
2578 : :
2579 [ + + ]: 285 : if (subres)
2580 : : {
2581 : 132 : char **subptr = subres;
2582 : :
2583 [ + + ]: 264 : while (*subptr)
2584 : : {
2585 [ + + ]: 330 : for (i = 0; i < var->nstem - 1; i++)
2586 : : {
5931 bruce@momjian.us 2587 [ + - ]: 198 : addNorm(&lres, &lcur, (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]), 0, NVariant);
2588 : : }
2589 : :
2590 : 132 : addNorm(&lres, &lcur, *subptr, 0, NVariant);
6591 tgl@sss.pgh.pa.us 2591 : 132 : subptr++;
2592 : 132 : NVariant++;
2593 : : }
2594 : :
2595 : 132 : pfree(subres);
2596 : 132 : var->stem[0] = NULL;
2597 : 132 : pfree(var->stem[var->nstem - 1]);
2598 : : }
2599 : : }
2600 : :
2601 [ + + + + ]: 1371 : for (i = 0; i < var->nstem && var->stem[i]; i++)
2602 : 711 : pfree(var->stem[i]);
2603 : 660 : ptr = var->next;
2604 : 660 : pfree(var->stem);
2605 : 660 : pfree(var);
2606 : 660 : var = ptr;
2607 : : }
2608 : : }
2609 : :
2610 : 375 : return lres;
2611 : : }
|