PostgreSQL Source Code git master
Loading...
Searching...
No Matches
spell.h File Reference
#include "regex/regex.h"
#include "tsearch/dicts/regis.h"
#include "tsearch/ts_public.h"
Include dependency graph for spell.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SPNodeData
 
struct  SPNode
 
struct  spell_struct
 
struct  aff_struct
 
struct  AffixNodeData
 
struct  AffixNode
 
struct  CMPDAffix
 
struct  CompoundAffixFlag
 
struct  IspellDict
 

Macros

#define FF_COMPOUNDONLY   0x01
 
#define FF_COMPOUNDBEGIN   0x02
 
#define FF_COMPOUNDMIDDLE   0x04
 
#define FF_COMPOUNDLAST   0x08
 
#define FF_COMPOUNDFLAG
 
#define FF_COMPOUNDFLAGMASK   0x0f
 
#define SPNHDRSZ   (offsetof(SPNode,data))
 
#define SPELLHDRSZ   (offsetof(SPELL, word))
 
#define FF_COMPOUNDPERMITFLAG   0x10
 
#define FF_COMPOUNDFORBIDFLAG   0x20
 
#define FF_CROSSPRODUCT   0x40
 
#define FF_SUFFIX   1
 
#define FF_PREFIX   0
 
#define ANHRDSZ   (offsetof(AffixNode, data))
 
#define FLAGNUM_MAXSIZE   (1 << 16)
 

Typedefs

typedef struct SPNode SPNode
 
typedef struct spell_struct SPELL
 
typedef struct aff_struct AFFIX
 
typedef struct AffixNode AffixNode
 
typedef struct CompoundAffixFlag CompoundAffixFlag
 

Enumerations

enum  FlagMode { FM_CHAR , FM_LONG , FM_NUM }
 

Functions

TSLexemeNINormalizeWord (IspellDict *Conf, const char *word)
 
void NIStartBuild (IspellDict *Conf)
 
void NIImportAffixes (IspellDict *Conf, const char *filename)
 
void NIImportDictionary (IspellDict *Conf, const char *filename)
 
void NISortDictionary (IspellDict *Conf)
 
void NISortAffixes (IspellDict *Conf)
 
void NIFinishBuild (IspellDict *Conf)
 

Macro Definition Documentation

◆ ANHRDSZ

#define ANHRDSZ   (offsetof(AffixNode, data))

Definition at line 145 of file spell.h.

◆ FF_COMPOUNDBEGIN

#define FF_COMPOUNDBEGIN   0x02

Definition at line 43 of file spell.h.

◆ FF_COMPOUNDFLAG

#define FF_COMPOUNDFLAG
Value:
#define FF_COMPOUNDBEGIN
Definition spell.h:43
#define FF_COMPOUNDMIDDLE
Definition spell.h:44
#define FF_COMPOUNDLAST
Definition spell.h:45

Definition at line 46 of file spell.h.

51{
52 uint32 length;
54} SPNode;
55
56#define SPNHDRSZ (offsetof(SPNode,data))
57
58/*
59 * Represents an entry in a words list.
60 */
61typedef struct spell_struct
62{
63 union
64 {
65 /*
66 * flag is filled in by NIImportDictionary(). After
67 * NISortDictionary(), d is used instead of flag.
68 */
69 const char *flag;
70 /* d is used in mkSPNode() */
71 struct
72 {
73 /* Reference to an entry of the AffixData field */
74 int affix;
75 /* Length of the word */
76 int len;
77 } d;
78 } p;
80} SPELL;
81
82#define SPELLHDRSZ (offsetof(SPELL, word))
83
84/*
85 * Represents an entry in an affix list.
86 */
87typedef struct aff_struct
88{
89 const char *flag;
90 /* FF_SUFFIX or FF_PREFIX */
91 uint32 type:1,
92 flagflags:7,
93 issimple:1,
94 isregis:1,
95 replen:14;
96 const char *find;
97 const char *repl;
98 union
99 {
100 /*
101 * Arrays of AFFIX are moved and sorted. We'll use a pointer to
102 * regex_t to keep this struct small, and avoid assuming that regex_t
103 * is movable.
104 */
106 Regis regis;
107 } reg;
108} AFFIX;
109
110/*
111 * affixes use dictionary flags too
112 */
113#define FF_COMPOUNDPERMITFLAG 0x10
114#define FF_COMPOUNDFORBIDFLAG 0x20
115#define FF_CROSSPRODUCT 0x40
116
117/*
118 * Don't change the order of these. Initialization sorts by these,
119 * and expects prefixes to come first after sorting.
120 */
121#define FF_SUFFIX 1
122#define FF_PREFIX 0
123
124/*
125 * AffixNode and AffixNodeData are used to represent prefix tree (Trie) to store
126 * an affix list.
127 */
128struct AffixNode;
129
130typedef struct
131{
132 uint32 val:8,
133 naff:24;
134 AFFIX **aff;
135 struct AffixNode *node;
137
138typedef struct AffixNode
139{
140 uint32 isvoid:1,
141 length:31;
143} AffixNode;
144
145#define ANHRDSZ (offsetof(AffixNode, data))
146
147typedef struct
148{
149 const char *affix;
150 int len;
151 bool issuffix;
152} CMPDAffix;
153
154/*
155 * Type of encoding affix flags in Hunspell dictionaries
156 */
157typedef enum
158{
159 FM_CHAR, /* one character (like ispell) */
160 FM_LONG, /* two characters */
161 FM_NUM, /* number, >= 0 and < 65536 */
162} FlagMode;
163
164/*
165 * Structure to store Hunspell options. Flag representation depends on flag
166 * type. These flags are about support of compound words.
167 */
168typedef struct CompoundAffixFlag
169{
170 union
171 {
172 /* Flag name if flagMode is FM_CHAR or FM_LONG */
173 const char *s;
174 /* Flag name if flagMode is FM_NUM */
175 uint32 i;
176 } flag;
177 /* we don't have a bsearch_arg version, so, copy FlagMode */
181
182#define FLAGNUM_MAXSIZE (1 << 16)
183
184typedef struct
185{
186 int maffixes;
187 int naffixes;
188 AFFIX *Affix;
189
190 AffixNode *Suffix;
191 AffixNode *Prefix;
192
193 SPNode *Dictionary;
194 /* Array of sets of affixes */
195 const char **AffixData;
196 int lenAffixData;
197 int nAffixData;
198 bool useFlagAliases;
199
200 CMPDAffix *CompoundAffix;
201
202 bool usecompound;
203 FlagMode flagMode;
204
205 /*
206 * All follow fields are actually needed only for initialization
207 */
208
209 /* Array of Hunspell options in affix file */
210 CompoundAffixFlag *CompoundAffixFlags;
211 /* number of entries in CompoundAffixFlags array */
212 int nCompoundAffixFlag;
213 /* allocated length of CompoundAffixFlags array */
214 int mCompoundAffixFlag;
215
216 /*
217 * Remaining fields are only used during dictionary construction; they are
218 * set up by NIStartBuild and cleared by NIFinishBuild.
219 */
220 MemoryContext buildCxt; /* temp context for construction */
221
222 /* Temporary array of all words in the dict file */
223 SPELL **Spell;
224 int nspell; /* number of valid entries in Spell array */
225 int mspell; /* allocated length of Spell array */
226
227 /* These are used to allocate "compact" data without palloc overhead */
228 char *firstfree; /* first free address (always maxaligned) */
229 size_t avail; /* free space remaining at firstfree */
230} IspellDict;
231
232extern TSLexeme *NINormalizeWord(IspellDict *Conf, const char *word);
233
234extern void NIStartBuild(IspellDict *Conf);
235extern void NIImportAffixes(IspellDict *Conf, const char *filename);
236extern void NIImportDictionary(IspellDict *Conf, const char *filename);
237extern void NISortDictionary(IspellDict *Conf);
238extern void NISortAffixes(IspellDict *Conf);
239extern void NIFinishBuild(IspellDict *Conf);
240
241#endif
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:480
uint32_t uint32
Definition c.h:546
long val
Definition informix.c:689
const void size_t len
const void * data
static char * filename
Definition pg_dumpall.c:120
static int fb(int x)
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition regcomp.c:1476
#define regex_t
Definition regex.h:245
void NIStartBuild(IspellDict *Conf)
Definition spell.c:89
void NIFinishBuild(IspellDict *Conf)
Definition spell.c:104
void NIImportAffixes(IspellDict *Conf, const char *filename)
Definition spell.c:1429
void NISortDictionary(IspellDict *Conf)
Definition spell.c:1726
void NISortAffixes(IspellDict *Conf)
Definition spell.c:1981
struct spell_struct SPELL
void NIImportDictionary(IspellDict *Conf, const char *filename)
Definition spell.c:519
TSLexeme * NINormalizeWord(IspellDict *Conf, const char *word)
Definition spell.c:2545
FlagMode
Definition spell.h:158
@ FM_LONG
Definition spell.h:160
@ FM_CHAR
Definition spell.h:159
@ FM_NUM
Definition spell.h:161
struct aff_struct AFFIX
uint32 isvoid
Definition spell.h:140
AffixNodeData data[FLEXIBLE_ARRAY_MEMBER]
Definition spell.h:142
uint32 length
Definition spell.h:141
union CompoundAffixFlag::@143 flag
FlagMode flagMode
Definition spell.h:178
const char * s
Definition spell.h:173
Definition regis.h:33
Definition spell.h:51
const char * find
Definition spell.h:96
uint32 isregis
Definition spell.h:94
uint32 type
Definition spell.h:91
Regis regis
Definition spell.h:106
const char * flag
Definition spell.h:89
uint32 replen
Definition spell.h:95
union aff_struct::@142 reg
regex_t * pregex
Definition spell.h:105
uint32 flagflags
Definition spell.h:92
const char * repl
Definition spell.h:97
uint32 issimple
Definition spell.h:93
union spell_struct::@140 p
int len
Definition spell.h:76
const char * flag
Definition spell.h:69
struct spell_struct::@140::@141 d
char word[FLEXIBLE_ARRAY_MEMBER]
Definition spell.h:79
int affix
Definition spell.h:74

◆ FF_COMPOUNDFLAGMASK

#define FF_COMPOUNDFLAGMASK   0x0f

Definition at line 48 of file spell.h.

◆ FF_COMPOUNDFORBIDFLAG

#define FF_COMPOUNDFORBIDFLAG   0x20

Definition at line 114 of file spell.h.

◆ FF_COMPOUNDLAST

#define FF_COMPOUNDLAST   0x08

Definition at line 45 of file spell.h.

◆ FF_COMPOUNDMIDDLE

#define FF_COMPOUNDMIDDLE   0x04

Definition at line 44 of file spell.h.

◆ FF_COMPOUNDONLY

#define FF_COMPOUNDONLY   0x01

Definition at line 42 of file spell.h.

◆ FF_COMPOUNDPERMITFLAG

#define FF_COMPOUNDPERMITFLAG   0x10

Definition at line 113 of file spell.h.

◆ FF_CROSSPRODUCT

#define FF_CROSSPRODUCT   0x40

Definition at line 115 of file spell.h.

◆ FF_PREFIX

#define FF_PREFIX   0

Definition at line 122 of file spell.h.

◆ FF_SUFFIX

#define FF_SUFFIX   1

Definition at line 121 of file spell.h.

◆ FLAGNUM_MAXSIZE

#define FLAGNUM_MAXSIZE   (1 << 16)

Definition at line 182 of file spell.h.

◆ SPELLHDRSZ

#define SPELLHDRSZ   (offsetof(SPELL, word))

Definition at line 82 of file spell.h.

◆ SPNHDRSZ

#define SPNHDRSZ   (offsetof(SPNode,data))

Definition at line 56 of file spell.h.

Typedef Documentation

◆ AFFIX

◆ AffixNode

◆ CompoundAffixFlag

◆ SPELL

◆ SPNode

Enumeration Type Documentation

◆ FlagMode

Enumerator
FM_CHAR 
FM_LONG 
FM_NUM 

Definition at line 157 of file spell.h.

158{
159 FM_CHAR, /* one character (like ispell) */
160 FM_LONG, /* two characters */
161 FM_NUM, /* number, >= 0 and < 65536 */
162} FlagMode;

Function Documentation

◆ NIFinishBuild()

void NIFinishBuild ( IspellDict Conf)
extern

Definition at line 104 of file spell.c.

105{
106 /* Release no-longer-needed temp memory */
107 MemoryContextDelete(Conf->buildCxt);
108 /* Just for cleanliness, zero the now-dangling pointers */
109 Conf->buildCxt = NULL;
110 Conf->Spell = NULL;
111 Conf->firstfree = NULL;
112 Conf->CompoundAffixFlags = NULL;
113}
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472

References fb(), and MemoryContextDelete().

Referenced by dispell_init().

◆ NIImportAffixes()

void NIImportAffixes ( IspellDict Conf,
const char filename 
)
extern

Definition at line 1429 of file spell.c.

1430{
1431 char *pstr = NULL;
1432 char flag[BUFSIZ];
1433 char mask[BUFSIZ];
1434 char find[BUFSIZ];
1435 char repl[BUFSIZ];
1436 char *s;
1437 bool suffixes = false;
1438 bool prefixes = false;
1439 char flagflags = 0;
1441 bool oldformat = false;
1442 char *recoded = NULL;
1443
1445 ereport(ERROR,
1447 errmsg("could not open affix file \"%s\": %m",
1448 filename)));
1449
1450 Conf->usecompound = false;
1451 Conf->useFlagAliases = false;
1452 Conf->flagMode = FM_CHAR;
1453
1454 while ((recoded = tsearch_readline(&trst)) != NULL)
1455 {
1457
1458 /* Skip comments and empty lines */
1459 if (*pstr == '#' || *pstr == '\n')
1460 goto nextline;
1461
1462 if (STRNCMP(pstr, "compoundwords") == 0)
1463 {
1464 /* Find case-insensitive L flag in non-lowercased string */
1465 s = findchar2(recoded, 'l', 'L');
1466 if (s)
1467 {
1468 while (*s && !isspace((unsigned char) *s))
1469 s += pg_mblen(s);
1470 while (*s && isspace((unsigned char) *s))
1471 s += pg_mblen(s);
1472
1473 if (*s && pg_mblen(s) == 1)
1474 {
1476 Conf->usecompound = true;
1477 }
1478 oldformat = true;
1479 goto nextline;
1480 }
1481 }
1482 if (STRNCMP(pstr, "suffixes") == 0)
1483 {
1484 suffixes = true;
1485 prefixes = false;
1486 oldformat = true;
1487 goto nextline;
1488 }
1489 if (STRNCMP(pstr, "prefixes") == 0)
1490 {
1491 suffixes = false;
1492 prefixes = true;
1493 oldformat = true;
1494 goto nextline;
1495 }
1496 if (STRNCMP(pstr, "flag") == 0)
1497 {
1498 s = recoded + 4; /* we need non-lowercased string */
1499 flagflags = 0;
1500
1501 while (*s && isspace((unsigned char) *s))
1502 s += pg_mblen(s);
1503
1504 if (*s == '*')
1505 {
1506 flagflags |= FF_CROSSPRODUCT;
1507 s++;
1508 }
1509 else if (*s == '~')
1510 {
1511 flagflags |= FF_COMPOUNDONLY;
1512 s++;
1513 }
1514
1515 if (*s == '\\')
1516 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 */
1523 if (*s && pg_mblen(s) == 1)
1524 {
1525 COPYCHAR(flag, s);
1526 flag[1] = '\0';
1527
1528 s++;
1529 if (*s == '\0' || *s == '#' || *s == '\n' || *s == ':' ||
1530 isspace((unsigned char) *s))
1531 {
1532 oldformat = true;
1533 goto nextline;
1534 }
1535 }
1536 goto isnewformat;
1537 }
1538 if (STRNCMP(recoded, "COMPOUNDFLAG") == 0 ||
1539 STRNCMP(recoded, "COMPOUNDMIN") == 0 ||
1540 STRNCMP(recoded, "PFX") == 0 ||
1541 STRNCMP(recoded, "SFX") == 0)
1542 goto isnewformat;
1543
1544 if ((!suffixes) && (!prefixes))
1545 goto nextline;
1546
1547 if (!parse_affentry(pstr, mask, find, repl))
1548 goto nextline;
1549
1550 NIAddAffix(Conf, flag, flagflags, mask, find, repl, suffixes ? FF_SUFFIX : FF_PREFIX);
1551
1552nextline:
1553 pfree(recoded);
1554 pfree(pstr);
1555 }
1557 return;
1558
1560 if (oldformat)
1561 ereport(ERROR,
1563 errmsg("affix file contains both old-style and new-style commands")));
1565
1567}
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
int pg_mblen(const char *mbstr)
Definition mbutils.c:1026
void pfree(void *pointer)
Definition mcxt.c:1616
static void prefixes(struct vars *v)
Definition regc_lex.c:99
static int find(struct vars *v, struct cnfa *cnfa, struct colormap *cm)
Definition regexec.c:419
static void NIImportOOAffixes(IspellDict *Conf, const char *filename)
Definition spell.c:1200
static void NIAddAffix(IspellDict *Conf, const char *flag, char flagflags, const char *mask, const char *find, const char *repl, int type)
Definition spell.c:679
#define STRNCMP(s, p)
Definition spell.c:191
static char * findchar2(char *str, int c1, int c2)
Definition spell.c:243
static bool parse_affentry(char *str, char *mask, char *find, char *repl)
Definition spell.c:915
static void addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
Definition spell.c:1069
#define FF_SUFFIX
Definition spell.h:121
#define FF_COMPOUNDFLAG
Definition spell.h:46
#define FF_PREFIX
Definition spell.h:122
#define FF_CROSSPRODUCT
Definition spell.h:115
#define FF_COMPOUNDONLY
Definition spell.h:42
char * flag(int b)
Definition test-ctype.c:33
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition ts_locale.c:77
char * tsearch_readline(tsearch_readline_state *stp)
Definition ts_locale.c:100
void tsearch_readline_end(tsearch_readline_state *stp)
Definition ts_locale.c:145
#define COPYCHAR(d, s)
Definition ts_locale.h:40

References addCompoundAffixFlagValue(), COPYCHAR, ereport, errcode(), errmsg(), ERROR, fb(), FF_COMPOUNDFLAG, FF_COMPOUNDONLY, FF_CROSSPRODUCT, FF_PREFIX, FF_SUFFIX, filename, find(), findchar2(), flag(), FM_CHAR, NIAddAffix(), NIImportOOAffixes(), parse_affentry(), pfree(), pg_mblen(), prefixes(), str_tolower(), STRNCMP, tsearch_readline(), tsearch_readline_begin(), and tsearch_readline_end().

Referenced by dispell_init().

◆ NIImportDictionary()

void NIImportDictionary ( IspellDict Conf,
const char filename 
)
extern

Definition at line 519 of file spell.c.

520{
522 char *line;
523
527 errmsg("could not open dictionary file \"%s\": %m",
528 filename)));
529
530 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 */
539 flag = NULL;
540 if ((s = findchar(line, '/')))
541 {
542 *s++ = '\0';
543 flag = s;
544 while (*s)
545 {
546 /* we allow only single encoded flags for faster works */
547 if (pg_mblen(s) == 1 && isprint((unsigned char) *s) && !isspace((unsigned char) *s))
548 s++;
549 else
550 {
551 *s = '\0';
552 break;
553 }
554 }
555 }
556 else
557 flag = "";
558
559 /* Remove trailing spaces */
560 s = line;
561 while (*s)
562 {
563 if (isspace((unsigned char) *s))
564 {
565 *s = '\0';
566 break;
567 }
568 s += pg_mblen(s);
569 }
570 pstr = lowerstr_ctx(Conf, line);
571
573 pfree(pstr);
574
575 pfree(line);
576 }
578}
static void NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
Definition spell.c:488
static char * findchar(char *str, int c)
Definition spell.c:230
static char * lowerstr_ctx(IspellDict *Conf, const char *src)
Definition spell.c:176

References ereport, errcode(), errmsg(), ERROR, fb(), filename, findchar(), flag(), lowerstr_ctx(), NIAddSpell(), pfree(), pg_mblen(), tsearch_readline(), tsearch_readline_begin(), and tsearch_readline_end().

Referenced by dispell_init().

◆ NINormalizeWord()

TSLexeme * NINormalizeWord ( IspellDict Conf,
const char word 
)
extern

Definition at line 2545 of file spell.c.

2546{
2547 char **res;
2548 TSLexeme *lcur = NULL,
2549 *lres = NULL;
2550 uint16 NVariant = 1;
2551
2552 res = NormalizeSubWord(Conf, word, 0);
2553
2554 if (res)
2555 {
2556 char **ptr = res;
2557
2558 while (*ptr && (lcur - lres) < MAX_NORM)
2559 {
2560 addNorm(&lres, &lcur, *ptr, 0, NVariant++);
2561 ptr++;
2562 }
2563 pfree(res);
2564 }
2565
2566 if (Conf->usecompound)
2567 {
2568 int wordlen = strlen(word);
2569 SplitVar *ptr,
2570 *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1);
2571 int i;
2572
2573 while (var)
2574 {
2575 if (var->nstem > 1)
2576 {
2577 char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDLAST);
2578
2579 if (subres)
2580 {
2581 char **subptr = subres;
2582
2583 while (*subptr)
2584 {
2585 for (i = 0; i < var->nstem - 1; i++)
2586 {
2587 addNorm(&lres, &lcur, (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]), 0, NVariant);
2588 }
2589
2590 addNorm(&lres, &lcur, *subptr, 0, NVariant);
2591 subptr++;
2592 NVariant++;
2593 }
2594
2595 pfree(subres);
2596 var->stem[0] = NULL;
2597 pfree(var->stem[var->nstem - 1]);
2598 }
2599 }
2600
2601 for (i = 0; i < var->nstem && var->stem[i]; i++)
2602 pfree(var->stem[i]);
2603 ptr = var->next;
2604 pfree(var->stem);
2605 pfree(var);
2606 var = ptr;
2607 }
2608 }
2609
2610 return lres;
2611}
uint16_t uint16
Definition c.h:545
int i
Definition isn.c:77
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define MAX_NORM
Definition spell.c:188
static void addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant)
Definition spell.c:2529
static char ** NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
Definition spell.c:2181
static SplitVar * SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
Definition spell.c:2379
int nstem
Definition spell.c:2292
struct SplitVar * next
Definition spell.c:2295
char ** stem
Definition spell.c:2294

References addNorm(), fb(), FF_COMPOUNDLAST, i, MAX_NORM, SplitVar::next, NormalizeSubWord(), SplitVar::nstem, pfree(), pstrdup(), SplitToVariants(), SplitVar::stem, and word().

Referenced by dispell_lexize().

◆ NISortAffixes()

void NISortAffixes ( IspellDict Conf)
extern

Definition at line 1981 of file spell.c.

1982{
1983 AFFIX *Affix;
1984 size_t i;
1985 CMPDAffix *ptr;
1986 int firstsuffix = Conf->naffixes;
1987
1988 if (Conf->naffixes == 0)
1989 return;
1990
1991 /* Store compound affixes in the Conf->CompoundAffix array */
1992 if (Conf->naffixes > 1)
1993 qsort(Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix);
1994 Conf->CompoundAffix = ptr = palloc_array(CMPDAffix, Conf->naffixes);
1995 ptr->affix = NULL;
1996
1997 for (i = 0; i < Conf->naffixes; i++)
1998 {
1999 Affix = &(((AFFIX *) Conf->Affix)[i]);
2000 if (Affix->type == FF_SUFFIX && i < firstsuffix)
2001 firstsuffix = i;
2002
2003 if ((Affix->flagflags & FF_COMPOUNDFLAG) && Affix->replen > 0 &&
2004 isAffixInUse(Conf, Affix->flag))
2005 {
2006 bool issuffix = (Affix->type == FF_SUFFIX);
2007
2008 if (ptr == Conf->CompoundAffix ||
2009 issuffix != (ptr - 1)->issuffix ||
2010 strbncmp((const unsigned char *) (ptr - 1)->affix,
2011 (const unsigned char *) Affix->repl,
2012 (ptr - 1)->len))
2013 {
2014 /* leave only unique and minimal suffixes */
2015 ptr->affix = Affix->repl;
2016 ptr->len = Affix->replen;
2017 ptr->issuffix = issuffix;
2018 ptr++;
2019 }
2020 }
2021 }
2022 ptr->affix = NULL;
2023 Conf->CompoundAffix = (CMPDAffix *) repalloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1));
2024
2025 /* Start build a prefix tree */
2026 Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX);
2027 Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX);
2029 mkVoidAffix(Conf, false, firstsuffix);
2030}
#define palloc_array(type, count)
Definition fe_memutils.h:76
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
#define qsort(a, b, c, d)
Definition port.h:495
static AffixNode * mkANode(IspellDict *Conf, int low, int high, int level, int type)
Definition spell.c:1835
static int strbncmp(const unsigned char *s1, const unsigned char *s2, size_t count)
Definition spell.c:281
static void mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
Definition spell.c:1912
static bool isAffixInUse(IspellDict *Conf, const char *affixflag)
Definition spell.c:1966
static int cmpaffix(const void *s1, const void *s2)
Definition spell.c:312
int len
Definition spell.h:150
bool issuffix
Definition spell.h:151
const char * affix
Definition spell.h:149

References CMPDAffix::affix, cmpaffix(), fb(), FF_COMPOUNDFLAG, FF_PREFIX, FF_SUFFIX, aff_struct::flag, aff_struct::flagflags, i, isAffixInUse(), CMPDAffix::issuffix, CMPDAffix::len, mkANode(), mkVoidAffix(), palloc_array, qsort, repalloc(), aff_struct::repl, aff_struct::replen, strbncmp(), and aff_struct::type.

Referenced by dispell_init().

◆ NISortDictionary()

void NISortDictionary ( IspellDict Conf)
extern

Definition at line 1726 of file spell.c.

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 */
1738 if (Conf->useFlagAliases)
1739 {
1740 for (i = 0; i < Conf->nspell; i++)
1741 {
1742 char *end;
1743
1744 if (*Conf->Spell[i]->p.flag != '\0')
1745 {
1746 errno = 0;
1747 curaffix = strtol(Conf->Spell[i]->p.flag, &end, 10);
1748 if (Conf->Spell[i]->p.flag == end || errno == ERANGE)
1749 ereport(ERROR,
1751 errmsg("invalid affix alias \"%s\"",
1752 Conf->Spell[i]->p.flag)));
1753 if (curaffix < 0 || curaffix >= Conf->nAffixData)
1754 ereport(ERROR,
1756 errmsg("invalid affix alias \"%s\"",
1757 Conf->Spell[i]->p.flag)));
1758 if (*end != '\0' && !isdigit((unsigned char) *end) && !isspace((unsigned char) *end))
1759 ereport(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 */
1770 curaffix = 0;
1771 }
1772
1773 Conf->Spell[i]->p.d.affix = curaffix;
1774 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 */
1781 qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *),
1783
1784 naffix = 0;
1785 for (i = 0; i < Conf->nspell; i++)
1786 {
1787 if (i == 0 ||
1788 strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag) != 0)
1789 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 */
1797 Conf->AffixData = palloc0_array(const char *, naffix);
1798
1799 curaffix = -1;
1800 for (i = 0; i < Conf->nspell; i++)
1801 {
1802 if (i == 0 ||
1803 strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]) != 0)
1804 {
1805 curaffix++;
1807 Conf->AffixData[curaffix] = cpstrdup(Conf,
1808 Conf->Spell[i]->p.flag);
1809 }
1810
1811 Conf->Spell[i]->p.d.affix = curaffix;
1812 Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1813 }
1814
1815 Conf->lenAffixData = Conf->nAffixData = naffix;
1816 }
1817
1818 /* Start build a prefix tree */
1819 qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *), cmpspell);
1820 Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0);
1821}
#define Assert(condition)
Definition c.h:873
#define palloc0_array(type, count)
Definition fe_memutils.h:77
static char * cpstrdup(IspellDict *Conf, const char *str)
Definition spell.c:163
static SPNode * mkSPNode(IspellDict *Conf, int low, int high, int level)
Definition spell.c:1644
static int cmpspell(const void *s1, const void *s2)
Definition spell.c:198
static int cmpspellaffix(const void *s1, const void *s2)
Definition spell.c:204

References Assert, cmpspell(), cmpspellaffix(), cpstrdup(), ereport, errcode(), errmsg(), ERROR, fb(), i, mkSPNode(), palloc0_array, and qsort.

Referenced by dispell_init().

◆ NIStartBuild()

void NIStartBuild ( IspellDict Conf)
extern

Definition at line 89 of file spell.c.

90{
91 /*
92 * The temp context is a child of CurTransactionContext, so that it will
93 * go away automatically on error.
94 */
96 "Ispell dictionary init context",
98}
MemoryContext CurTransactionContext
Definition mcxt.c:172
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurTransactionContext, and fb().

Referenced by dispell_init().