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_regex_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_regex_struct aff_regex_struct
 
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 151 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 120 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 119 of file spell.h.

Referenced by NIAddAffix(), and NIImportOOAffixes().

◆ FF_CROSSPRODUCT

#define FF_CROSSPRODUCT   0x40

Definition at line 121 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 188 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

◆ aff_regex_struct

◆ 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 163 of file spell.h.

164 {
165  FM_CHAR, /* one character (like ispell) */
166  FM_LONG, /* two characters */
167  FM_NUM /* number, >= 0 and < 65536 */
168 } FlagMode;
Definition: spell.h:165
Definition: spell.h:167
Definition: spell.h:166
FlagMode
Definition: spell.h:163

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:226
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
CompoundAffixFlag * CompoundAffixFlags
Definition: spell.h:216
char * firstfree
Definition: spell.h:234
SPELL ** Spell
Definition: spell.h:229

◆ NIImportAffixes()

void NIImportAffixes ( IspellDict Conf,
const char *  filename 
)

Definition at line 1444 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().

1445 {
1446  char *pstr = NULL;
1447  char flag[BUFSIZ];
1448  char mask[BUFSIZ];
1449  char find[BUFSIZ];
1450  char repl[BUFSIZ];
1451  char *s;
1452  bool suffixes = false;
1453  bool prefixes = false;
1454  char flagflags = 0;
1456  bool oldformat = false;
1457  char *recoded = NULL;
1458 
1459  if (!tsearch_readline_begin(&trst, filename))
1460  ereport(ERROR,
1461  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1462  errmsg("could not open affix file \"%s\": %m",
1463  filename)));
1464 
1465  Conf->usecompound = false;
1466  Conf->useFlagAliases = false;
1467  Conf->flagMode = FM_CHAR;
1468 
1469  while ((recoded = tsearch_readline(&trst)) != NULL)
1470  {
1471  pstr = lowerstr(recoded);
1472 
1473  /* Skip comments and empty lines */
1474  if (*pstr == '#' || *pstr == '\n')
1475  goto nextline;
1476 
1477  if (STRNCMP(pstr, "compoundwords") == 0)
1478  {
1479  /* Find case-insensitive L flag in non-lowercased string */
1480  s = findchar2(recoded, 'l', 'L');
1481  if (s)
1482  {
1483  while (*s && !t_isspace(s))
1484  s += pg_mblen(s);
1485  while (*s && t_isspace(s))
1486  s += pg_mblen(s);
1487 
1488  if (*s && pg_mblen(s) == 1)
1489  {
1491  Conf->usecompound = true;
1492  }
1493  oldformat = true;
1494  goto nextline;
1495  }
1496  }
1497  if (STRNCMP(pstr, "suffixes") == 0)
1498  {
1499  suffixes = true;
1500  prefixes = false;
1501  oldformat = true;
1502  goto nextline;
1503  }
1504  if (STRNCMP(pstr, "prefixes") == 0)
1505  {
1506  suffixes = false;
1507  prefixes = true;
1508  oldformat = true;
1509  goto nextline;
1510  }
1511  if (STRNCMP(pstr, "flag") == 0)
1512  {
1513  s = recoded + 4; /* we need non-lowercased string */
1514  flagflags = 0;
1515 
1516  while (*s && t_isspace(s))
1517  s += pg_mblen(s);
1518 
1519  if (*s == '*')
1520  {
1521  flagflags |= FF_CROSSPRODUCT;
1522  s++;
1523  }
1524  else if (*s == '~')
1525  {
1526  flagflags |= FF_COMPOUNDONLY;
1527  s++;
1528  }
1529 
1530  if (*s == '\\')
1531  s++;
1532 
1533  /*
1534  * An old-format flag is a single ASCII character; we expect it to
1535  * be followed by EOL, whitespace, or ':'. Otherwise this is a
1536  * new-format flag command.
1537  */
1538  if (*s && pg_mblen(s) == 1)
1539  {
1540  COPYCHAR(flag, s);
1541  flag[1] = '\0';
1542 
1543  s++;
1544  if (*s == '\0' || *s == '#' || *s == '\n' || *s == ':' ||
1545  t_isspace(s))
1546  {
1547  oldformat = true;
1548  goto nextline;
1549  }
1550  }
1551  goto isnewformat;
1552  }
1553  if (STRNCMP(recoded, "COMPOUNDFLAG") == 0 ||
1554  STRNCMP(recoded, "COMPOUNDMIN") == 0 ||
1555  STRNCMP(recoded, "PFX") == 0 ||
1556  STRNCMP(recoded, "SFX") == 0)
1557  goto isnewformat;
1558 
1559  if ((!suffixes) && (!prefixes))
1560  goto nextline;
1561 
1562  if (!parse_affentry(pstr, mask, find, repl))
1563  goto nextline;
1564 
1565  NIAddAffix(Conf, flag, flagflags, mask, find, repl, suffixes ? FF_SUFFIX : FF_PREFIX);
1566 
1567 nextline:
1568  pfree(recoded);
1569  pfree(pstr);
1570  }
1571  tsearch_readline_end(&trst);
1572  return;
1573 
1574 isnewformat:
1575  if (oldformat)
1576  ereport(ERROR,
1577  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1578  errmsg("affix file contains both old-style and new-style commands")));
1579  tsearch_readline_end(&trst);
1580 
1581  NIImportOOAffixes(Conf, filename);
1582 }
#define COPYCHAR(d, s)
Definition: ts_locale.h:48
#define FF_COMPOUNDONLY
Definition: spell.h:42
static int find(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:393
bool useFlagAliases
Definition: spell.h:204
bool usecompound
Definition: spell.h:208
Definition: spell.h:165
#define FF_CROSSPRODUCT
Definition: spell.h:121
int errcode(int sqlerrcode)
Definition: elog.c:698
static void NIImportOOAffixes(IspellDict *Conf, const char *filename)
Definition: spell.c:1215
char * lowerstr(const char *str)
Definition: ts_locale.c:244
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
static bool parse_affentry(char *str, char *mask, char *find, char *repl)
Definition: spell.c:932
#define FF_SUFFIX
Definition: spell.h:127
int t_isspace(const char *ptr)
Definition: ts_locale.c:53
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:687
char * flag(int b)
Definition: test-ctype.c:33
#define ereport(elevel,...)
Definition: elog.h:157
#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:128
static void addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
Definition: spell.c:1085
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:193
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:148
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:125
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define FF_COMPOUNDFLAG
Definition: spell.h:46
FlagMode flagMode
Definition: spell.h:209

◆ 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:85
static char * findchar(char *str, int c)
Definition: spell.c:228
int errcode(int sqlerrcode)
Definition: elog.c:698
static void NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
Definition: spell.c:485
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
int t_isspace(const char *ptr)
Definition: ts_locale.c:53
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:157
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:193
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:148
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:125
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ NINormalizeWord()

TSLexeme* NINormalizeWord ( IspellDict Conf,
char *  word 
)

Definition at line 2551 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().

2552 {
2553  char **res;
2554  TSLexeme *lcur = NULL,
2555  *lres = NULL;
2556  uint16 NVariant = 1;
2557 
2558  res = NormalizeSubWord(Conf, word, 0);
2559 
2560  if (res)
2561  {
2562  char **ptr = res;
2563 
2564  while (*ptr && (lcur - lres) < MAX_NORM)
2565  {
2566  addNorm(&lres, &lcur, *ptr, 0, NVariant++);
2567  ptr++;
2568  }
2569  pfree(res);
2570  }
2571 
2572  if (Conf->usecompound)
2573  {
2574  int wordlen = strlen(word);
2575  SplitVar *ptr,
2576  *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1);
2577  int i;
2578 
2579  while (var)
2580  {
2581  if (var->nstem > 1)
2582  {
2583  char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDLAST);
2584 
2585  if (subres)
2586  {
2587  char **subptr = subres;
2588 
2589  while (*subptr)
2590  {
2591  for (i = 0; i < var->nstem - 1; i++)
2592  {
2593  addNorm(&lres, &lcur, (subptr == subres) ? var->stem[i] : pstrdup(var->stem[i]), 0, NVariant);
2594  }
2595 
2596  addNorm(&lres, &lcur, *subptr, 0, NVariant);
2597  subptr++;
2598  NVariant++;
2599  }
2600 
2601  pfree(subres);
2602  var->stem[0] = NULL;
2603  pfree(var->stem[var->nstem - 1]);
2604  }
2605  }
2606 
2607  for (i = 0; i < var->nstem && var->stem[i]; i++)
2608  pfree(var->stem[i]);
2609  ptr = var->next;
2610  pfree(var->stem);
2611  pfree(var);
2612  var = ptr;
2613  }
2614  }
2615 
2616  return lres;
2617 }
static char ** NormalizeSubWord(IspellDict *Conf, char *word, int flag)
Definition: spell.c:2190
bool usecompound
Definition: spell.h:208
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define FF_COMPOUNDLAST
Definition: spell.h:45
static void addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant)
Definition: spell.c:2535
#define MAX_NORM
Definition: spell.c:186
unsigned short uint16
Definition: c.h:440
void pfree(void *pointer)
Definition: mcxt.c:1169
int nstem
Definition: spell.c:2301
static SplitVar * SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int wordlen, int startpos, int minpos)
Definition: spell.c:2388
char ** stem
Definition: spell.c:2303
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1393
int i
struct SplitVar * next
Definition: spell.c:2304

◆ NISortAffixes()

void NISortAffixes ( IspellDict Conf)

Definition at line 1990 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().

1991 {
1992  AFFIX *Affix;
1993  size_t i;
1994  CMPDAffix *ptr;
1995  int firstsuffix = Conf->naffixes;
1996 
1997  if (Conf->naffixes == 0)
1998  return;
1999 
2000  /* Store compound affixes in the Conf->CompoundAffix array */
2001  if (Conf->naffixes > 1)
2002  qsort((void *) Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix);
2003  Conf->CompoundAffix = ptr = (CMPDAffix *) palloc(sizeof(CMPDAffix) * Conf->naffixes);
2004  ptr->affix = NULL;
2005 
2006  for (i = 0; i < Conf->naffixes; i++)
2007  {
2008  Affix = &(((AFFIX *) Conf->Affix)[i]);
2009  if (Affix->type == FF_SUFFIX && i < firstsuffix)
2010  firstsuffix = i;
2011 
2012  if ((Affix->flagflags & FF_COMPOUNDFLAG) && Affix->replen > 0 &&
2013  isAffixInUse(Conf, Affix->flag))
2014  {
2015  bool issuffix = (Affix->type == FF_SUFFIX);
2016 
2017  if (ptr == Conf->CompoundAffix ||
2018  issuffix != (ptr - 1)->issuffix ||
2019  strbncmp((const unsigned char *) (ptr - 1)->affix,
2020  (const unsigned char *) Affix->repl,
2021  (ptr - 1)->len))
2022  {
2023  /* leave only unique and minimal suffixes */
2024  ptr->affix = Affix->repl;
2025  ptr->len = Affix->replen;
2026  ptr->issuffix = issuffix;
2027  ptr++;
2028  }
2029  }
2030  }
2031  ptr->affix = NULL;
2032  Conf->CompoundAffix = (CMPDAffix *) repalloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1));
2033 
2034  /* Start build a prefix tree */
2035  Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX);
2036  Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX);
2037  mkVoidAffix(Conf, true, firstsuffix);
2038  mkVoidAffix(Conf, false, firstsuffix);
2039 }
char * flag
Definition: spell.h:100
int naffixes
Definition: spell.h:193
uint32 flagflags
Definition: spell.h:102
AffixNode * Prefix
Definition: spell.h:197
AffixNode * Suffix
Definition: spell.h:196
uint32 type
Definition: spell.h:102
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:1975
int len
Definition: spell.h:156
#define FF_SUFFIX
Definition: spell.h:127
static void mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
Definition: spell.c:1921
AFFIX * Affix
Definition: spell.h:194
static AffixNode * mkANode(IspellDict *Conf, int low, int high, int level, int type)
Definition: spell.c:1844
char * affix
Definition: spell.h:155
bool issuffix
Definition: spell.h:157
#define FF_PREFIX
Definition: spell.h:128
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
CMPDAffix * CompoundAffix
Definition: spell.h:206
void * palloc(Size size)
Definition: mcxt.c:1062
int i
char * repl
Definition: spell.h:108
#define FF_COMPOUNDFLAG
Definition: spell.h:46
#define qsort(a, b, c, d)
Definition: port.h:504
uint32 replen
Definition: spell.h:102
static int cmpaffix(const void *s1, const void *s2)
Definition: spell.c:310

◆ NISortDictionary()

void NISortDictionary ( IspellDict Conf)

Definition at line 1736 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().

1737 {
1738  int i;
1739  int naffix;
1740  int curaffix;
1741 
1742  /* compress affixes */
1743 
1744  /*
1745  * If we use flag aliases then we need to use Conf->AffixData filled in
1746  * the NIImportOOAffixes().
1747  */
1748  if (Conf->useFlagAliases)
1749  {
1750  for (i = 0; i < Conf->nspell; i++)
1751  {
1752  char *end;
1753 
1754  if (*Conf->Spell[i]->p.flag != '\0')
1755  {
1756  curaffix = strtol(Conf->Spell[i]->p.flag, &end, 10);
1757  if (Conf->Spell[i]->p.flag == end || errno == ERANGE)
1758  ereport(ERROR,
1759  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1760  errmsg("invalid affix alias \"%s\"",
1761  Conf->Spell[i]->p.flag)));
1762  if (curaffix < 0 || curaffix >= Conf->nAffixData)
1763  ereport(ERROR,
1764  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1765  errmsg("invalid affix alias \"%s\"",
1766  Conf->Spell[i]->p.flag)));
1767  if (*end != '\0' && !t_isdigit(end) && !t_isspace(end))
1768  ereport(ERROR,
1769  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1770  errmsg("invalid affix alias \"%s\"",
1771  Conf->Spell[i]->p.flag)));
1772  }
1773  else
1774  {
1775  /*
1776  * If Conf->Spell[i]->p.flag is empty, then get empty value of
1777  * Conf->AffixData (0 index).
1778  */
1779  curaffix = 0;
1780  }
1781 
1782  Conf->Spell[i]->p.d.affix = curaffix;
1783  Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1784  }
1785  }
1786  /* Otherwise fill Conf->AffixData here */
1787  else
1788  {
1789  /* Count the number of different flags used in the dictionary */
1790  qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL *),
1791  cmpspellaffix);
1792 
1793  naffix = 0;
1794  for (i = 0; i < Conf->nspell; i++)
1795  {
1796  if (i == 0 ||
1797  strcmp(Conf->Spell[i]->p.flag, Conf->Spell[i - 1]->p.flag) != 0)
1798  naffix++;
1799  }
1800 
1801  /*
1802  * Fill in Conf->AffixData with the affixes that were used in the
1803  * dictionary. Replace textual flag-field of Conf->Spell entries with
1804  * indexes into Conf->AffixData array.
1805  */
1806  Conf->AffixData = (char **) palloc0(naffix * sizeof(char *));
1807 
1808  curaffix = -1;
1809  for (i = 0; i < Conf->nspell; i++)
1810  {
1811  if (i == 0 ||
1812  strcmp(Conf->Spell[i]->p.flag, Conf->AffixData[curaffix]) != 0)
1813  {
1814  curaffix++;
1815  Assert(curaffix < naffix);
1816  Conf->AffixData[curaffix] = cpstrdup(Conf,
1817  Conf->Spell[i]->p.flag);
1818  }
1819 
1820  Conf->Spell[i]->p.d.affix = curaffix;
1821  Conf->Spell[i]->p.d.len = strlen(Conf->Spell[i]->word);
1822  }
1823 
1824  Conf->lenAffixData = Conf->nAffixData = naffix;
1825  }
1826 
1827  /* Start build a prefix tree */
1828  qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL *), cmpspell);
1829  Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0);
1830 }
int nAffixData
Definition: spell.h:203
static int cmpspell(const void *s1, const void *s2)
Definition: spell.c:196
struct spell_struct::@116::@117 d
int lenAffixData
Definition: spell.h:202
bool useFlagAliases
Definition: spell.h:204
static SPNode * mkSPNode(IspellDict *Conf, int low, int high, int level)
Definition: spell.c:1654
int errcode(int sqlerrcode)
Definition: elog.c:698
SPNode * Dictionary
Definition: spell.h:199
int t_isdigit(const char *ptr)
Definition: ts_locale.c:37
int nspell
Definition: spell.h:230
#define ERROR
Definition: elog.h:46
char ** AffixData
Definition: spell.h:201
int t_isspace(const char *ptr)
Definition: ts_locale.c:53
char word[FLEXIBLE_ARRAY_MEMBER]
Definition: spell.h:79
void * palloc0(Size size)
Definition: mcxt.c:1093
char * flag
Definition: spell.h:69
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
SPELL ** Spell
Definition: spell.h:229
union spell_struct::@116 p
int errmsg(const char *fmt,...)
Definition: elog.c:909
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:504

◆ 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:226
#define AllocSetContextCreate
Definition: memutils.h:173
MemoryContext CurTransactionContext
Definition: mcxt.c:54
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195