PostgreSQL Source Code  git master
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, 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 140 of file spell.h.

Referenced by mkANode(), and mkVoidAffix().

◆ FF_COMPOUNDBEGIN

#define FF_COMPOUNDBEGIN   0x02

Definition at line 43 of file spell.h.

Referenced by CheckAffix(), NIImportOOAffixes(), and SplitToVariants().

◆ FF_COMPOUNDFLAG

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

Definition at line 46 of file spell.h.

Referenced by mkSPNode(), NIAddAffix(), NIImportAffixes(), NIImportOOAffixes(), and NISortAffixes().

◆ FF_COMPOUNDFLAGMASK

#define FF_COMPOUNDFLAGMASK   0x0f

Definition at line 48 of file spell.h.

Referenced by FindWord(), and makeCompoundFlags().

◆ FF_COMPOUNDFORBIDFLAG

#define FF_COMPOUNDFORBIDFLAG   0x20

Definition at line 109 of file spell.h.

Referenced by CheckAffix(), and NIImportOOAffixes().

◆ FF_COMPOUNDLAST

#define FF_COMPOUNDLAST   0x08

Definition at line 45 of file spell.h.

Referenced by CheckAffix(), NIImportOOAffixes(), NINormalizeWord(), and SplitToVariants().

◆ FF_COMPOUNDMIDDLE

#define FF_COMPOUNDMIDDLE   0x04

Definition at line 44 of file spell.h.

Referenced by CheckAffix(), NIImportOOAffixes(), and SplitToVariants().

◆ FF_COMPOUNDONLY

#define FF_COMPOUNDONLY   0x01

Definition at line 42 of file spell.h.

Referenced by CheckAffix(), FindWord(), mkSPNode(), NIAddAffix(), NIImportAffixes(), and NIImportOOAffixes().

◆ FF_COMPOUNDPERMITFLAG

#define FF_COMPOUNDPERMITFLAG   0x10

Definition at line 108 of file spell.h.

Referenced by NIAddAffix(), and NIImportOOAffixes().

◆ FF_CROSSPRODUCT

#define FF_CROSSPRODUCT   0x40

Definition at line 110 of file spell.h.

Referenced by NIImportAffixes(), NIImportOOAffixes(), and NormalizeSubWord().

◆ FF_PREFIX

#define FF_PREFIX   0

◆ FF_SUFFIX

#define FF_SUFFIX   1

◆ FLAGNUM_MAXSIZE

#define FLAGNUM_MAXSIZE   (1 << 16)

Definition at line 177 of file spell.h.

Referenced by getNextFlagFromString(), and setCompoundAffixFlagValue().

◆ SPELLHDRSZ

#define SPELLHDRSZ   (offsetof(SPELL, word))

Definition at line 82 of file spell.h.

Referenced by NIAddSpell().

◆ SPNHDRSZ

#define SPNHDRSZ   (offsetof(SPNode,data))

Definition at line 56 of file spell.h.

Referenced by mkSPNode().

Typedef Documentation

◆ AFFIX

typedef struct aff_struct AFFIX

◆ AffixNode

typedef struct AffixNode AffixNode

◆ CompoundAffixFlag

◆ SPELL

typedef struct spell_struct SPELL

◆ SPNode

typedef struct SPNode SPNode

Enumeration Type Documentation

◆ FlagMode

enum FlagMode
Enumerator
FM_CHAR 
FM_LONG 
FM_NUM 

Definition at line 152 of file spell.h.

153 {
154  FM_CHAR, /* one character (like ispell) */
155  FM_LONG, /* two characters */
156  FM_NUM /* number, >= 0 and < 65536 */
157 } FlagMode;
Definition: spell.h:154
Definition: spell.h:156
Definition: spell.h:155
FlagMode
Definition: spell.h:152

Function Documentation

◆ NIFinishBuild()

void NIFinishBuild ( IspellDict Conf)

Definition at line 102 of file spell.c.

References IspellDict::buildCxt, IspellDict::CompoundAffixFlags, IspellDict::firstfree, MemoryContextDelete(), and IspellDict::Spell.

Referenced by dispell_init().

103 {
104  /* Release no-longer-needed temp memory */
106  /* Just for cleanliness, zero the now-dangling pointers */
107  Conf->buildCxt = NULL;
108  Conf->Spell = NULL;
109  Conf->firstfree = NULL;
110  Conf->CompoundAffixFlags = NULL;
111 }
MemoryContext buildCxt
Definition: spell.h:215
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
CompoundAffixFlag * CompoundAffixFlags
Definition: spell.h:205
char * firstfree
Definition: spell.h:223
SPELL ** Spell
Definition: spell.h:218

◆ NIImportAffixes()

void NIImportAffixes ( IspellDict Conf,
const char *  filename 
)

Definition at line 1418 of file spell.c.

References addCompoundAffixFlagValue(), COPYCHAR, ereport, errcode(), errmsg(), ERROR, FF_COMPOUNDFLAG, FF_COMPOUNDONLY, FF_CROSSPRODUCT, FF_PREFIX, FF_SUFFIX, find(), findchar2(), flag(), IspellDict::flagMode, FM_CHAR, lowerstr(), NIAddAffix(), NIImportOOAffixes(), parse_affentry(), pfree(), pg_mblen(), prefixes(), STRNCMP, t_isspace(), tsearch_readline(), tsearch_readline_begin(), tsearch_readline_end(), IspellDict::usecompound, and IspellDict::useFlagAliases.

Referenced by dispell_init().

1419 {
1420  char *pstr = NULL;
1421  char flag[BUFSIZ];
1422  char mask[BUFSIZ];
1423  char find[BUFSIZ];
1424  char repl[BUFSIZ];
1425  char *s;
1426  bool suffixes = false;
1427  bool prefixes = false;
1428  char flagflags = 0;
1430  bool oldformat = false;
1431  char *recoded = NULL;
1432 
1433  if (!tsearch_readline_begin(&trst, filename))
1434  ereport(ERROR,
1435  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1436  errmsg("could not open affix file \"%s\": %m",
1437  filename)));
1438 
1439  Conf->usecompound = false;
1440  Conf->useFlagAliases = false;
1441  Conf->flagMode = FM_CHAR;
1442 
1443  while ((recoded = tsearch_readline(&trst)) != NULL)
1444  {
1445  pstr = lowerstr(recoded);
1446 
1447  /* Skip comments and empty lines */
1448  if (*pstr == '#' || *pstr == '\n')
1449  goto nextline;
1450 
1451  if (STRNCMP(pstr, "compoundwords") == 0)
1452  {
1453  /* Find case-insensitive L flag in non-lowercased string */
1454  s = findchar2(recoded, 'l', 'L');
1455  if (s)
1456  {
1457  while (*s && !t_isspace(s))
1458  s += pg_mblen(s);
1459  while (*s && t_isspace(s))
1460  s += pg_mblen(s);
1461 
1462  if (*s && pg_mblen(s) == 1)
1463  {
1465  Conf->usecompound = true;
1466  }
1467  oldformat = true;
1468  goto nextline;
1469  }
1470  }
1471  if (STRNCMP(pstr, "suffixes") == 0)
1472  {
1473  suffixes = true;
1474  prefixes = false;
1475  oldformat = true;
1476  goto nextline;
1477  }
1478  if (STRNCMP(pstr, "prefixes") == 0)
1479  {
1480  suffixes = false;
1481  prefixes = true;
1482  oldformat = true;
1483  goto nextline;
1484  }
1485  if (STRNCMP(pstr, "flag") == 0)
1486  {
1487  s = recoded + 4; /* we need non-lowercased string */
1488  flagflags = 0;
1489 
1490  while (*s && t_isspace(s))
1491  s += pg_mblen(s);
1492 
1493  if (*s == '*')
1494  {
1495  flagflags |= FF_CROSSPRODUCT;
1496  s++;
1497  }
1498  else if (*s == '~')
1499  {
1500  flagflags |= FF_COMPOUNDONLY;
1501  s++;
1502  }
1503 
1504  if (*s == '\\')
1505  s++;
1506 
1507  /*
1508  * An old-format flag is a single ASCII character; we expect it to
1509  * be followed by EOL, whitespace, or ':'. Otherwise this is a
1510  * new-format flag command.
1511  */
1512  if (*s && pg_mblen(s) == 1)
1513  {
1514  COPYCHAR(flag, s);
1515  flag[1] = '\0';
1516 
1517  s++;
1518  if (*s == '\0' || *s == '#' || *s == '\n' || *s == ':' ||
1519  t_isspace(s))
1520  {
1521  oldformat = true;
1522  goto nextline;
1523  }
1524  }
1525  goto isnewformat;
1526  }
1527  if (STRNCMP(recoded, "COMPOUNDFLAG") == 0 ||
1528  STRNCMP(recoded, "COMPOUNDMIN") == 0 ||
1529  STRNCMP(recoded, "PFX") == 0 ||
1530  STRNCMP(recoded, "SFX") == 0)
1531  goto isnewformat;
1532 
1533  if ((!suffixes) && (!prefixes))
1534  goto nextline;
1535 
1536  if (!parse_affentry(pstr, mask, find, repl))
1537  goto nextline;
1538 
1539  NIAddAffix(Conf, flag, flagflags, mask, find, repl, suffixes ? FF_SUFFIX : FF_PREFIX);
1540 
1541 nextline:
1542  pfree(recoded);
1543  pfree(pstr);
1544  }
1545  tsearch_readline_end(&trst);
1546  return;
1547 
1548 isnewformat:
1549  if (oldformat)
1550  ereport(ERROR,
1551  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1552  errmsg("affix file contains both old-style and new-style commands")));
1553  tsearch_readline_end(&trst);
1554 
1555  NIImportOOAffixes(Conf, filename);
1556 }
#define COPYCHAR(d, s)
Definition: ts_locale.h:45
#define FF_COMPOUNDONLY
Definition: spell.h:42
static int find(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:375
bool useFlagAliases
Definition: spell.h:193
bool usecompound
Definition: spell.h:197
Definition: spell.h:154
#define FF_CROSSPRODUCT
Definition: spell.h:110
int errcode(int sqlerrcode)
Definition: elog.c:610
static void NIImportOOAffixes(IspellDict *Conf, const char *filename)
Definition: spell.c:1189
char * lowerstr(const char *str)
Definition: ts_locale.c:239
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static bool parse_affentry(char *str, char *mask, char *find, char *repl)
Definition: spell.c:906
#define FF_SUFFIX
Definition: spell.h:116
int t_isspace(const char *ptr)
Definition: ts_locale.c:52
static void prefixes(struct vars *v)
Definition: regc_lex.c:99
static void NIAddAffix(IspellDict *Conf, const char *flag, char flagflags, const char *mask, const char *find, const char *repl, int type)
Definition: spell.c:676
char * flag(int b)
Definition: test-ctype.c:33
#define ereport(elevel,...)
Definition: elog.h:144
#define STRNCMP(s, p)
Definition: spell.c:189
static char * findchar2(char *str, int c1, int c2)
Definition: spell.c:241
#define FF_PREFIX
Definition: spell.h:117
static void addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
Definition: spell.c:1059
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:161
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:146
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:124
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define FF_COMPOUNDFLAG
Definition: spell.h:46
FlagMode flagMode
Definition: spell.h:198

◆ NIImportDictionary()

void NIImportDictionary ( IspellDict Conf,
const char *  filename 
)

Definition at line 516 of file spell.c.

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

Referenced by dispell_init().

517 {
519  char *line;
520 
521  if (!tsearch_readline_begin(&trst, filename))
522  ereport(ERROR,
523  (errcode(ERRCODE_CONFIG_FILE_ERROR),
524  errmsg("could not open dictionary file \"%s\": %m",
525  filename)));
526 
527  while ((line = tsearch_readline(&trst)) != NULL)
528  {
529  char *s,
530  *pstr;
531 
532  /* Set of affix flags */
533  const char *flag;
534 
535  /* Extract flag from the line */
536  flag = NULL;
537  if ((s = findchar(line, '/')))
538  {
539  *s++ = '\0';
540  flag = s;
541  while (*s)
542  {
543  /* we allow only single encoded flags for faster works */
544  if (pg_mblen(s) == 1 && t_isprint(s) && !t_isspace(s))
545  s++;
546  else
547  {
548  *s = '\0';
549  break;
550  }
551  }
552  }
553  else
554  flag = "";
555 
556  /* Remove trailing spaces */
557  s = line;
558  while (*s)
559  {
560  if (t_isspace(s))
561  {
562  *s = '\0';
563  break;
564  }
565  s += pg_mblen(s);
566  }
567  pstr = lowerstr_ctx(Conf, line);
568 
569  NIAddSpell(Conf, pstr, flag);
570  pfree(pstr);
571 
572  pfree(line);
573  }
574  tsearch_readline_end(&trst);
575 }
int t_isprint(const char *ptr)
Definition: ts_locale.c:84
static char * findchar(char *str, int c)
Definition: spell.c:228
int errcode(int sqlerrcode)
Definition: elog.c:610
static void NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
Definition: spell.c:485
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
int t_isspace(const char *ptr)
Definition: ts_locale.c:52
char * flag(int b)
Definition: test-ctype.c:33
static char * lowerstr_ctx(IspellDict *Conf, const char *src)
Definition: spell.c:174
#define ereport(elevel,...)
Definition: elog.h:144
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:161
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:146
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:124
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ NINormalizeWord()

TSLexeme* NINormalizeWord ( IspellDict Conf,
char *  word 
)

Definition at line 2525 of file spell.c.

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

Referenced by dispell_lexize().

2526 {
2527  char **res;
2528  TSLexeme *lcur = NULL,
2529  *lres = NULL;
2530  uint16 NVariant = 1;
2531 
2532  res = NormalizeSubWord(Conf, word, 0);
2533 
2534  if (res)
2535  {
2536  char **ptr = res;
2537 
2538  while (*ptr && (lcur - lres) < MAX_NORM)
2539  {
2540  addNorm(&lres, &lcur, *ptr, 0, NVariant++);
2541  ptr++;
2542  }
2543  pfree(res);
2544  }
2545 
2546  if (Conf->usecompound)
2547  {
2548  int wordlen = strlen(word);
2549  SplitVar *ptr,
2550  *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1);
2551  int i;
2552 
2553  while (var)
2554  {
2555  if (var->nstem > 1)
2556  {
2557  char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDLAST);
2558 
2559  if (subres)
2560  {
2561  char **subptr = subres;
2562 
2563  while (*subptr)
2564  {
2565  for (i = 0; i < var->nstem - 1; i++)
2566  {
2567  addNorm(&lres, &lcur, (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]), 0, NVariant);
2568  }
2569 
2570  addNorm(&lres, &lcur, *subptr, 0, NVariant);
2571  subptr++;
2572  NVariant++;
2573  }
2574 
2575  pfree(subres);
2576  var->stem[0] = NULL;
2577  pfree(var->stem[var->nstem - 1]);
2578  }
2579  }
2580 
2581  for (i = 0; i < var->nstem && var->stem[i]; i++)
2582  pfree(var->stem[i]);
2583  ptr = var->next;
2584  pfree(var->stem);
2585  pfree(var);
2586  var = ptr;
2587  }
2588  }
2589 
2590  return lres;
2591 }
static char ** NormalizeSubWord(IspellDict *Conf, char *word, int flag)
Definition: spell.c:2164
bool usecompound
Definition: spell.h:197
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define FF_COMPOUNDLAST
Definition: spell.h:45
static void addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant)
Definition: spell.c:2509
#define MAX_NORM
Definition: spell.c:186
unsigned short uint16
Definition: c.h:366
void pfree(void *pointer)
Definition: mcxt.c:1056
int nstem
Definition: spell.c:2275
static SplitVar * SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int wordlen, int startpos, int minpos)
Definition: spell.c:2362
char ** stem
Definition: spell.c:2277
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246
int i
struct SplitVar * next
Definition: spell.c:2278

◆ NISortAffixes()

void NISortAffixes ( IspellDict Conf)

Definition at line 1964 of file spell.c.

References CMPDAffix::affix, IspellDict::Affix, cmpaffix(), IspellDict::CompoundAffix, FF_COMPOUNDFLAG, FF_PREFIX, FF_SUFFIX, aff_struct::flag, aff_struct::flagflags, i, isAffixInUse(), CMPDAffix::issuffix, CMPDAffix::len, mkANode(), mkVoidAffix(), IspellDict::naffixes, palloc(), IspellDict::Prefix, qsort, repalloc(), aff_struct::repl, aff_struct::replen, strbncmp(), IspellDict::Suffix, and aff_struct::type.

Referenced by dispell_init().

1965 {
1966  AFFIX *Affix;
1967  size_t i;
1968  CMPDAffix *ptr;
1969  int firstsuffix = Conf->naffixes;
1970 
1971  if (Conf->naffixes == 0)
1972  return;
1973 
1974  /* Store compound affixes in the Conf->CompoundAffix array */
1975  if (Conf->naffixes > 1)
1976  qsort((void *) Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix);
1977  Conf->CompoundAffix = ptr = (CMPDAffix *) palloc(sizeof(CMPDAffix) * Conf->naffixes);
1978  ptr->affix = NULL;
1979 
1980  for (i = 0; i < Conf->naffixes; i++)
1981  {
1982  Affix = &(((AFFIX *) Conf->Affix)[i]);
1983  if (Affix->type == FF_SUFFIX && i < firstsuffix)
1984  firstsuffix = i;
1985 
1986  if ((Affix->flagflags & FF_COMPOUNDFLAG) && Affix->replen > 0 &&
1987  isAffixInUse(Conf, Affix->flag))
1988  {
1989  bool issuffix = (Affix->type == FF_SUFFIX);
1990 
1991  if (ptr == Conf->CompoundAffix ||
1992  issuffix != (ptr - 1)->issuffix ||
1993  strbncmp((const unsigned char *) (ptr - 1)->affix,
1994  (const unsigned char *) Affix->repl,
1995  (ptr - 1)->len))
1996  {
1997  /* leave only unique and minimals suffixes */
1998  ptr->affix = Affix->repl;
1999  ptr->len = Affix->replen;
2000  ptr->issuffix = issuffix;
2001  ptr++;
2002  }
2003  }
2004  }
2005  ptr->affix = NULL;
2006  Conf->CompoundAffix = (CMPDAffix *) repalloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1));
2007 
2008  /* Start build a prefix tree */
2009  Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX);
2010  Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX);
2011  mkVoidAffix(Conf, true, firstsuffix);
2012  mkVoidAffix(Conf, false, firstsuffix);
2013 }
char * flag
Definition: spell.h:89
int naffixes
Definition: spell.h:182
uint32 flagflags
Definition: spell.h:91
AffixNode * Prefix
Definition: spell.h:186
AffixNode * Suffix
Definition: spell.h:185
uint32 type
Definition: spell.h:91
static int strbncmp(const unsigned char *s1, const unsigned char *s2, size_t count)
Definition: spell.c:279
static bool isAffixInUse(IspellDict *Conf, char *affixflag)
Definition: spell.c:1949
int len
Definition: spell.h:145
#define FF_SUFFIX
Definition: spell.h:116
static void mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
Definition: spell.c:1895
AFFIX * Affix
Definition: spell.h:183
static AffixNode * mkANode(IspellDict *Conf, int low, int high, int level, int type)
Definition: spell.c:1818
char * affix
Definition: spell.h:144
bool issuffix
Definition: spell.h:146
#define FF_PREFIX
Definition: spell.h:117
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
CMPDAffix * CompoundAffix
Definition: spell.h:195
void * palloc(Size size)
Definition: mcxt.c:949
int i
char * repl
Definition: spell.h:97
#define FF_COMPOUNDFLAG
Definition: spell.h:46
#define qsort(a, b, c, d)
Definition: port.h:479
uint32 replen
Definition: spell.h:91
static int cmpaffix(const void *s1, const void *s2)
Definition: spell.c:310

◆ NISortDictionary()

void NISortDictionary ( IspellDict Conf)

Definition at line 1710 of file spell.c.

References IspellDict::AffixData, Assert, cmpspell(), cmpspellaffix(), cpstrdup(), spell_struct::d, IspellDict::Dictionary, ereport, errcode(), errmsg(), ERROR, spell_struct::flag, i, IspellDict::lenAffixData, mkSPNode(), IspellDict::nAffixData, IspellDict::nspell, spell_struct::p, palloc0(), qsort, IspellDict::Spell, t_isdigit(), t_isspace(), IspellDict::useFlagAliases, and spell_struct::word.

Referenced by dispell_init().

1711 {
1712  int i;
1713  int naffix = 0;
1714  int curaffix;
1715 
1716  /* compress affixes */
1717 
1718  /*
1719  * If we use flag aliases then we need to use Conf->AffixData filled in
1720  * the NIImportOOAffixes().
1721  */
1722  if (Conf->useFlagAliases)
1723  {
1724  for (i = 0; i < Conf->nspell; i++)
1725  {
1726  char *end;
1727 
1728  if (*Conf->Spell[i]->p.flag != '\0')
1729  {
1730  curaffix = strtol(Conf->Spell[i]->p.flag, &end, 10);
1731  if (Conf->Spell[i]->p.flag == end || errno == ERANGE)
1732  ereport(ERROR,
1733  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1734  errmsg("invalid affix alias \"%s\"",
1735  Conf->Spell[i]->p.flag)));
1736  if (curaffix < 0 || curaffix >= Conf->nAffixData)
1737  ereport(ERROR,
1738  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1739  errmsg("invalid affix alias \"%s\"",
1740  Conf->Spell[i]->p.flag)));
1741  if (*end != '\0' && !t_isdigit(end) && !t_isspace(end))
1742  ereport(ERROR,
1743  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1744  errmsg("invalid affix alias \"%s\"",
1745  Conf->Spell[i]->p.flag)));
1746  }
1747  else
1748  {
1749  /*
1750  * If Conf->Spell[i]->p.flag is empty, then get empty value of
1751  * Conf->AffixData (0 index).
1752  */
1753  curaffix = 0;
1754  }
1755 
1756  Conf->Spell[i]->p.d.affix = curaffix;
1757  Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1758  }
1759  }
1760  /* Otherwise fill Conf->AffixData here */
1761  else
1762  {
1763  /* Count the number of different flags used in the dictionary */
1764  qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL *),
1765  cmpspellaffix);
1766 
1767  naffix = 0;
1768  for (i = 0; i < Conf->nspell; i++)
1769  {
1770  if (i == 0 ||
1771  strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag) != 0)
1772  naffix++;
1773  }
1774 
1775  /*
1776  * Fill in Conf->AffixData with the affixes that were used in the
1777  * dictionary. Replace textual flag-field of Conf->Spell entries with
1778  * indexes into Conf->AffixData array.
1779  */
1780  Conf->AffixData = (char **) palloc0(naffix * sizeof(char *));
1781 
1782  curaffix = -1;
1783  for (i = 0; i < Conf->nspell; i++)
1784  {
1785  if (i == 0 ||
1786  strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]) != 0)
1787  {
1788  curaffix++;
1789  Assert(curaffix < naffix);
1790  Conf->AffixData[curaffix] = cpstrdup(Conf,
1791  Conf->Spell[i]->p.flag);
1792  }
1793 
1794  Conf->Spell[i]->p.d.affix = curaffix;
1795  Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1796  }
1797 
1798  Conf->lenAffixData = Conf->nAffixData = naffix;
1799  }
1800 
1801  /* Start build a prefix tree */
1802  qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL *), cmpspell);
1803  Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0);
1804 }
int nAffixData
Definition: spell.h:192
static int cmpspell(const void *s1, const void *s2)
Definition: spell.c:196
int lenAffixData
Definition: spell.h:191
bool useFlagAliases
Definition: spell.h:193
struct spell_struct::@117::@118 d
static SPNode * mkSPNode(IspellDict *Conf, int low, int high, int level)
Definition: spell.c:1628
int errcode(int sqlerrcode)
Definition: elog.c:610
SPNode * Dictionary
Definition: spell.h:188
int t_isdigit(const char *ptr)
Definition: ts_locale.c:36
int nspell
Definition: spell.h:219
#define ERROR
Definition: elog.h:43
char ** AffixData
Definition: spell.h:190
int t_isspace(const char *ptr)
Definition: ts_locale.c:52
union spell_struct::@117 p
char word[FLEXIBLE_ARRAY_MEMBER]
Definition: spell.h:79
void * palloc0(Size size)
Definition: mcxt.c:980
char * flag
Definition: spell.h:69
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
SPELL ** Spell
Definition: spell.h:218
int errmsg(const char *fmt,...)
Definition: elog.c:824
static char * cpstrdup(IspellDict *Conf, const char *str)
Definition: spell.c:161
static int cmpspellaffix(const void *s1, const void *s2)
Definition: spell.c:202
int i
#define qsort(a, b, c, d)
Definition: port.h:479

◆ NIStartBuild()

void NIStartBuild ( IspellDict Conf)

Definition at line 87 of file spell.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, IspellDict::buildCxt, and CurTransactionContext.

Referenced by dispell_init().

88 {
89  /*
90  * The temp context is a child of CurTransactionContext, so that it will
91  * go away automatically on error.
92  */
94  "Ispell dictionary init context",
96 }
MemoryContext buildCxt
Definition: spell.h:215
#define AllocSetContextCreate
Definition: memutils.h:170
MemoryContext CurTransactionContext
Definition: mcxt.c:50
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192