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:558
uint32_t uint32
Definition c.h:624
long val
Definition informix.c:689
const void size_t len
const void * data
static char * filename
Definition pg_dumpall.c:133
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:1437
void NISortDictionary(IspellDict *Conf)
Definition spell.c:1733
void NISortAffixes(IspellDict *Conf)
Definition spell.c:1988
struct spell_struct SPELL
void NIImportDictionary(IspellDict *Conf, const char *filename)
Definition spell.c:520
TSLexeme * NINormalizeWord(IspellDict *Conf, const char *word)
Definition spell.c:2587
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::@148 flag
FlagMode flagMode
Definition spell.h:178
const char * s
Definition spell.h:173
Definition regis.h:33
Definition spell.h:51
union aff_struct::@147 reg
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
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
struct spell_struct::@145::@146 d
int len
Definition spell.h:76
const char * flag
Definition spell.h:69
char word[FLEXIBLE_ARRAY_MEMBER]
Definition spell.h:79
union spell_struct::@145 p
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 1437 of file spell.c.

1438{
1439 char *pstr = NULL;
1440 char flag[BUFSIZ];
1441 char mask[BUFSIZ];
1442 char find[BUFSIZ];
1443 char repl[BUFSIZ];
1444 char *s;
1445 bool suffixes = false;
1446 bool prefixes = false;
1447 char flagflags = 0;
1449 bool oldformat = false;
1450 char *recoded = NULL;
1451
1453 ereport(ERROR,
1455 errmsg("could not open affix file \"%s\": %m",
1456 filename)));
1457
1458 Conf->usecompound = false;
1459 Conf->useFlagAliases = false;
1460 Conf->flagMode = FM_CHAR;
1461
1462 while ((recoded = tsearch_readline(&trst)) != NULL)
1463 {
1465
1466 /* Skip comments and empty lines */
1467 if (*pstr == '#' || *pstr == '\n')
1468 goto nextline;
1469
1470 if (STRNCMP(pstr, "compoundwords") == 0)
1471 {
1472 /* Find case-insensitive L flag in non-lowercased string */
1473 s = findchar2(recoded, 'l', 'L');
1474 if (s)
1475 {
1476 while (*s && !isspace((unsigned char) *s))
1477 s += pg_mblen_cstr(s);
1478 while (*s && isspace((unsigned char) *s))
1479 s += pg_mblen_cstr(s);
1480
1481 if (*s && pg_mblen_cstr(s) == 1)
1482 {
1484 Conf->usecompound = true;
1485 }
1486 oldformat = true;
1487 goto nextline;
1488 }
1489 }
1490 if (STRNCMP(pstr, "suffixes") == 0)
1491 {
1492 suffixes = true;
1493 prefixes = false;
1494 oldformat = true;
1495 goto nextline;
1496 }
1497 if (STRNCMP(pstr, "prefixes") == 0)
1498 {
1499 suffixes = false;
1500 prefixes = true;
1501 oldformat = true;
1502 goto nextline;
1503 }
1504 if (STRNCMP(pstr, "flag") == 0)
1505 {
1506 s = recoded + 4; /* we need non-lowercased string */
1507 flagflags = 0;
1508
1509 while (*s && isspace((unsigned char) *s))
1510 s += pg_mblen_cstr(s);
1511
1512 if (*s == '*')
1513 {
1514 flagflags |= FF_CROSSPRODUCT;
1515 s++;
1516 }
1517 else if (*s == '~')
1518 {
1519 flagflags |= FF_COMPOUNDONLY;
1520 s++;
1521 }
1522
1523 if (*s == '\\')
1524 s++;
1525
1526 /*
1527 * An old-format flag is a single ASCII character; we expect it to
1528 * be followed by EOL, whitespace, or ':'. Otherwise this is a
1529 * new-format flag command.
1530 */
1531 if (*s && pg_mblen_cstr(s) == 1)
1532 {
1533 flag[0] = *s++;
1534 flag[1] = '\0';
1535
1536 if (*s == '\0' || *s == '#' || *s == '\n' || *s == ':' ||
1537 isspace((unsigned char) *s))
1538 {
1539 oldformat = true;
1540 goto nextline;
1541 }
1542 }
1543 goto isnewformat;
1544 }
1545 if (STRNCMP(recoded, "COMPOUNDFLAG") == 0 ||
1546 STRNCMP(recoded, "COMPOUNDMIN") == 0 ||
1547 STRNCMP(recoded, "PFX") == 0 ||
1548 STRNCMP(recoded, "SFX") == 0)
1549 goto isnewformat;
1550
1551 if ((!suffixes) && (!prefixes))
1552 goto nextline;
1553
1554 if (!parse_affentry(pstr, mask, find, repl))
1555 goto nextline;
1556
1557 NIAddAffix(Conf, flag, flagflags, mask, find, repl, suffixes ? FF_SUFFIX : FF_PREFIX);
1558
1559nextline:
1560 pfree(recoded);
1561 pfree(pstr);
1562 }
1564 return;
1565
1567 if (oldformat)
1568 ereport(ERROR,
1570 errmsg("affix file contains both old-style and new-style commands")));
1572
1574}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
int pg_mblen_cstr(const char *mbstr)
Definition mbutils.c:1045
void pfree(void *pointer)
Definition mcxt.c:1616
static char * errmsg
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 addCompoundAffixFlagValue(IspellDict *Conf, const char *s, uint32 val)
Definition spell.c:1076
static void NIImportOOAffixes(IspellDict *Conf, const char *filename)
Definition spell.c:1208
static void NIAddAffix(IspellDict *Conf, const char *flag, char flagflags, const char *mask, const char *find, const char *repl, int type)
Definition spell.c:680
#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(const char *str, char *mask, char *find, char *repl)
Definition spell.c:917
#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:85
char * tsearch_readline(tsearch_readline_state *stp)
Definition ts_locale.c:108
void tsearch_readline_end(tsearch_readline_state *stp)
Definition ts_locale.c:153

References addCompoundAffixFlagValue(), 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_cstr(), 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 520 of file spell.c.

521{
523 char *line;
524
528 errmsg("could not open dictionary file \"%s\": %m",
529 filename)));
530
531 while ((line = tsearch_readline(&trst)) != NULL)
532 {
533 char *s,
534 *pstr;
535
536 /* Set of affix flags */
537 const char *flag;
538
539 /* Extract flag from the line */
540 flag = NULL;
541 if ((s = findchar(line, '/')))
542 {
543 *s++ = '\0';
544 flag = s;
545 while (*s)
546 {
547 /* we allow only single encoded flags for faster works */
548 if (pg_mblen_cstr(s) == 1 && isprint((unsigned char) *s) && !isspace((unsigned char) *s))
549 s++;
550 else
551 {
552 *s = '\0';
553 break;
554 }
555 }
556 }
557 else
558 flag = "";
559
560 /* Remove trailing spaces */
561 s = line;
562 while (*s)
563 {
564 if (isspace((unsigned char) *s))
565 {
566 *s = '\0';
567 break;
568 }
569 s += pg_mblen_cstr(s);
570 }
571 pstr = lowerstr_ctx(Conf, line);
572
574 pfree(pstr);
575
576 pfree(line);
577 }
579}
static void NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
Definition spell.c:489
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_cstr(), 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 2587 of file spell.c.

2588{
2589 char **res;
2590 TSLexeme *lcur = NULL,
2591 *lres = NULL;
2592 uint16 NVariant = 1;
2593
2594 res = NormalizeSubWord(Conf, word, 0);
2595
2596 if (res)
2597 {
2598 char **ptr = res;
2599
2600 while (*ptr && (lcur - lres) < MAX_NORM)
2601 {
2602 addNorm(&lres, &lcur, *ptr, 0, NVariant++);
2603 ptr++;
2604 }
2605 pfree(res);
2606 }
2607
2608 if (Conf->usecompound)
2609 {
2610 int wordlen = strlen(word);
2611 SplitVar *ptr,
2612 *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1);
2613 int i;
2614
2615 while (var)
2616 {
2617 if (var->nstem > 1)
2618 {
2619 char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDLAST);
2620
2621 if (subres)
2622 {
2623 char **subptr = subres;
2624
2625 while (*subptr)
2626 {
2627 for (i = 0; i < var->nstem - 1; i++)
2628 {
2629 addNorm(&lres, &lcur, (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]), 0, NVariant);
2630 }
2631
2632 addNorm(&lres, &lcur, *subptr, 0, NVariant);
2633 subptr++;
2634 NVariant++;
2635 }
2636
2637 pfree(subres);
2638 var->stem[0] = NULL;
2639 pfree(var->stem[var->nstem - 1]);
2640 }
2641 }
2642
2643 for (i = 0; i < var->nstem && var->stem[i]; i++)
2644 pfree(var->stem[i]);
2645 ptr = var->next;
2646 pfree(var->stem);
2647 pfree(var);
2648 var = ptr;
2649 }
2650 }
2651
2652 return lres;
2653}
uint16_t uint16
Definition c.h:623
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:2571
static char ** NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
Definition spell.c:2223
static SplitVar * SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
Definition spell.c:2421
int nstem
Definition spell.c:2334
struct SplitVar * next
Definition spell.c:2337
char ** stem
Definition spell.c:2336

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 1988 of file spell.c.

1989{
1990 AFFIX *Affix;
1991 size_t i;
1992 CMPDAffix *ptr;
1993 int firstsuffix = Conf->naffixes;
1994
1995 if (Conf->naffixes == 0)
1996 return;
1997
1998 /* Store compound affixes in the Conf->CompoundAffix array */
1999 if (Conf->naffixes > 1)
2000 qsort(Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix);
2001 Conf->CompoundAffix = ptr = palloc_array(CMPDAffix, Conf->naffixes);
2002 ptr->affix = NULL;
2003
2004 for (i = 0; i < Conf->naffixes; i++)
2005 {
2006 Affix = &(((AFFIX *) Conf->Affix)[i]);
2007 if (Affix->type == FF_SUFFIX && i < firstsuffix)
2008 firstsuffix = i;
2009
2010 if ((Affix->flagflags & FF_COMPOUNDFLAG) && Affix->replen > 0 &&
2011 isAffixInUse(Conf, Affix->flag))
2012 {
2013 bool issuffix = (Affix->type == FF_SUFFIX);
2014
2015 if (ptr == Conf->CompoundAffix ||
2016 issuffix != (ptr - 1)->issuffix ||
2017 strbncmp((const unsigned char *) (ptr - 1)->affix,
2018 (const unsigned char *) Affix->repl,
2019 (ptr - 1)->len))
2020 {
2021 /* leave only unique and minimal suffixes */
2022 ptr->affix = Affix->repl;
2023 ptr->len = Affix->replen;
2024 ptr->issuffix = issuffix;
2025 ptr++;
2026 }
2027 }
2028 }
2029 ptr->affix = NULL;
2030 Conf->CompoundAffix = (CMPDAffix *) repalloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1));
2031
2032 /* Start build a prefix tree */
2033 Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX);
2034 Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX);
2036 mkVoidAffix(Conf, false, firstsuffix);
2037}
#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:1842
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:1919
static bool isAffixInUse(IspellDict *Conf, const char *affixflag)
Definition spell.c:1973
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 1733 of file spell.c.

1734{
1735 int i;
1736 int naffix;
1737 int curaffix;
1738
1739 /* compress affixes */
1740
1741 /*
1742 * If we use flag aliases then we need to use Conf->AffixData filled in
1743 * the NIImportOOAffixes().
1744 */
1745 if (Conf->useFlagAliases)
1746 {
1747 for (i = 0; i < Conf->nspell; i++)
1748 {
1749 char *end;
1750
1751 if (*Conf->Spell[i]->p.flag != '\0')
1752 {
1753 errno = 0;
1754 curaffix = strtol(Conf->Spell[i]->p.flag, &end, 10);
1755 if (Conf->Spell[i]->p.flag == end || errno == ERANGE)
1756 ereport(ERROR,
1758 errmsg("invalid affix alias \"%s\"",
1759 Conf->Spell[i]->p.flag)));
1760 if (curaffix < 0 || curaffix >= Conf->nAffixData)
1761 ereport(ERROR,
1763 errmsg("invalid affix alias \"%s\"",
1764 Conf->Spell[i]->p.flag)));
1765 if (*end != '\0' && !isdigit((unsigned char) *end) && !isspace((unsigned char) *end))
1766 ereport(ERROR,
1768 errmsg("invalid affix alias \"%s\"",
1769 Conf->Spell[i]->p.flag)));
1770 }
1771 else
1772 {
1773 /*
1774 * If Conf->Spell[i]->p.flag is empty, then get empty value of
1775 * Conf->AffixData (0 index).
1776 */
1777 curaffix = 0;
1778 }
1779
1780 Conf->Spell[i]->p.d.affix = curaffix;
1781 Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1782 }
1783 }
1784 /* Otherwise fill Conf->AffixData here */
1785 else
1786 {
1787 /* Count the number of different flags used in the dictionary */
1788 qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *),
1790
1791 naffix = 0;
1792 for (i = 0; i < Conf->nspell; i++)
1793 {
1794 if (i == 0 ||
1795 strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag) != 0)
1796 naffix++;
1797 }
1798
1799 /*
1800 * Fill in Conf->AffixData with the affixes that were used in the
1801 * dictionary. Replace textual flag-field of Conf->Spell entries with
1802 * indexes into Conf->AffixData array.
1803 */
1804 Conf->AffixData = palloc0_array(const char *, naffix);
1805
1806 curaffix = -1;
1807 for (i = 0; i < Conf->nspell; i++)
1808 {
1809 if (i == 0 ||
1810 strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]) != 0)
1811 {
1812 curaffix++;
1814 Conf->AffixData[curaffix] = cpstrdup(Conf,
1815 Conf->Spell[i]->p.flag);
1816 }
1817
1818 Conf->Spell[i]->p.d.affix = curaffix;
1819 Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1820 }
1821
1822 Conf->lenAffixData = Conf->nAffixData = naffix;
1823 }
1824
1825 /* Start build a prefix tree */
1826 qsort(Conf->Spell, Conf->nspell, sizeof(SPELL *), cmpspell);
1827 Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0);
1828}
#define Assert(condition)
Definition c.h:943
#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:1651
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().