80#define tmpalloc(sz) MemoryContextAlloc(Conf->buildCxt, (sz))
81#define tmpalloc0(sz) MemoryContextAllocZero(Conf->buildCxt, (sz))
96 "Ispell dictionary init context",
127#define COMPACT_ALLOC_CHUNK 8192
128#define COMPACT_MAX_REQ 1024
146 if (size > Conf->
avail)
159#define cpalloc(size) compact_palloc0(Conf, size)
160#define cpalloc0(size) compact_palloc0(Conf, size)
182 dst =
str_tolower(src, strlen(src), DEFAULT_COLLATION_OID);
189#define MAXNORMLEN 256
191#define STRNCMP(s,p) strncmp( (s), (p), strlen(p) )
192#define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
193#define GETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T )
206 return strcmp((*(
SPELL *
const *)
s1)->p.flag,
207 (*(
SPELL *
const *)
s2)->p.flag);
220 if (fv1->
flag.
i == fv2->flag.i)
223 return (fv1->
flag.
i > fv2->flag.i) ? 1 : -1;
226 return strcmp(fv1->
flag.
s, fv2->flag.s);
260 int l1 = strlen((
const char *)
s1) - 1,
261 l2 = strlen((
const char *)
s2) - 1;
263 while (l1 >= 0 && l2 >= 0)
281strbncmp(
const unsigned char *
s1,
const unsigned char *
s2,
size_t count)
283 int l1 = strlen((
const char *)
s1) - 1,
284 l2 = strlen((
const char *)
s2) - 1,
287 while (l1 >= 0 && l2 >= 0 && l > 0)
317 if (
a1->type <
a2->type)
319 if (
a1->type >
a2->type)
322 return strcmp(
a1->repl,
a2->repl);
324 return strbcmp((
const unsigned char *)
a1->repl,
325 (
const unsigned char *)
a2->repl);
354 const char *sbuf = *sflagset;
357 bool met_comma =
false;
375 stop = (maxstep == 0);
379 s = strtol(*sflagset, &
next, 10);
380 if (*sflagset ==
next || errno == ERANGE)
382 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
383 errmsg(
"invalid affix flag \"%s\"", *sflagset)));
386 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
387 errmsg(
"affix flag \"%s\" is out of range",
389 sflag +=
sprintf(sflag,
"%0d", s);
395 if (isdigit((
unsigned char) **sflagset))
399 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
400 errmsg(
"invalid affix flag \"%s\"",
404 else if (
t_iseq(*sflagset,
','))
408 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
409 errmsg(
"invalid affix flag \"%s\"",
413 else if (!isspace((
unsigned char) **sflagset))
416 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
417 errmsg(
"invalid character in affix flag \"%s\"",
426 elog(
ERROR,
"unrecognized type of Conf->flagMode: %d",
436 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
437 errmsg(
"invalid affix flag \"%s\" with \"long\" flag value",
464 Assert(affix < Conf->nAffixData);
472 if (strcmp(
flag, affixflag) == 0)
526 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
527 errmsg(
"could not open dictionary file \"%s\": %m",
547 if (
pg_mblen(s) == 1 && isprint((
unsigned char) *s) && !isspace((
unsigned char) *s))
563 if (isspace((
unsigned char) *s))
616 StopLow = node->
data;
618 while (StopLow < StopHigh)
620 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
621 if (StopMiddle->
val == *ptr)
623 if (*(ptr + 1) ==
'\0' && StopMiddle->
isword)
645 node = StopMiddle->
node;
649 else if (StopMiddle->
val < *ptr)
650 StopLow = StopMiddle + 1;
652 StopHigh = StopMiddle;
654 if (StopLow >= StopHigh)
680 const char *
find,
const char *repl,
int type)
701 if (strcmp(mask,
".") == 0 || *mask ==
'\0')
725 tmask = (
char *)
tmpalloc(strlen(mask) + 3);
731 masklen = strlen(tmask);
743 DEFAULT_COLLATION_OID);
750 (
errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
751 errmsg(
"invalid regular expression: %s", errstr)));
765 if ((Affix->
replen = strlen(repl)) > 0)
773#define PAE_WAIT_MASK 0
775#define PAE_WAIT_FIND 2
777#define PAE_WAIT_REPL 4
779#define PAE_WAIT_TYPE 6
780#define PAE_WAIT_FLAG 7
804 else if (!isspace((
unsigned char) **
str))
819 if (isspace((
unsigned char) **
str))
860 char *repl,
char *mask)
893 elog(
ERROR,
"unrecognized state in parse_ooaffentry: %d",
922 *mask = *
find = *repl =
'\0';
930 else if (!isspace((
unsigned char) *
str))
944 else if (!isspace((
unsigned char) *
str))
962 else if (!isspace((
unsigned char) *
str))
964 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
979 else if (!isspace((
unsigned char) *
str))
981 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
996 else if (!isspace((
unsigned char) *
str))
998 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1013 else if (!isspace((
unsigned char) *
str))
1015 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1016 errmsg(
"syntax error")));
1024 *pmask = *pfind = *prepl =
'\0';
1026 return (*mask && (*
find || *repl));
1042 i = strtol(s, &
next, 10);
1043 if (s ==
next || errno == ERANGE)
1045 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1046 errmsg(
"invalid affix flag \"%s\"", s)));
1049 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1050 errmsg(
"affix flag \"%s\" is out of range", s)));
1076 while (*s && isspace((
unsigned char) *s))
1081 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1082 errmsg(
"syntax error")));
1086 while (*s && !isspace((
unsigned char) *s) && *s !=
'\n')
1132 const char *flagcur;
1170 curaffix = strtol(s, &end, 10);
1171 if (s == end || errno == ERANGE)
1173 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1174 errmsg(
"invalid affix alias \"%s\"", s)));
1176 if (curaffix > 0 && curaffix < Conf->nAffixData)
1185 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1186 errmsg(
"invalid affix alias \"%s\"", s)));
1211 bool isSuffix =
false;
1226 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1227 errmsg(
"could not open affix file \"%s\": %m",
1232 if (*recoded ==
'\0' || isspace((
unsigned char) *recoded) ||
t_iseq(recoded,
'#'))
1238 if (
STRNCMP(recoded,
"COMPOUNDFLAG") == 0)
1241 else if (
STRNCMP(recoded,
"COMPOUNDBEGIN") == 0)
1244 else if (
STRNCMP(recoded,
"COMPOUNDLAST") == 0)
1248 else if (
STRNCMP(recoded,
"COMPOUNDEND") == 0)
1251 else if (
STRNCMP(recoded,
"COMPOUNDMIDDLE") == 0)
1254 else if (
STRNCMP(recoded,
"ONLYINCOMPOUND") == 0)
1257 else if (
STRNCMP(recoded,
"COMPOUNDPERMITFLAG") == 0)
1259 recoded + strlen(
"COMPOUNDPERMITFLAG"),
1261 else if (
STRNCMP(recoded,
"COMPOUNDFORBIDFLAG") == 0)
1263 recoded + strlen(
"COMPOUNDFORBIDFLAG"),
1265 else if (
STRNCMP(recoded,
"FLAG") == 0)
1267 char *s = recoded + strlen(
"FLAG");
1269 while (*s && isspace((
unsigned char) *s))
1276 else if (
STRNCMP(s,
"num") == 0)
1278 else if (
STRNCMP(s,
"default") != 0)
1280 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1281 errmsg(
"Ispell dictionary supports only "
1282 "\"default\", \"long\", "
1283 "and \"num\" flag values")));
1297 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1298 errmsg(
"could not open affix file \"%s\": %m",
1305 if (*recoded ==
'\0' || isspace((
unsigned char) *recoded) ||
t_iseq(recoded,
'#'))
1315 if (
STRNCMP(ptype,
"af") == 0)
1321 naffix = atoi(sflag);
1324 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1325 errmsg(
"invalid number of flag vector aliases")));
1340 if (curaffix < naffix)
1347 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1348 errmsg(
"number of aliases exceeds specified number %d",
1354 if (fields_read < 4 ||
1358 sflaglen = strlen(sflag);
1369 if (fields_read == 4)
1371 isSuffix = (
STRNCMP(ptype,
"sfx") == 0);
1388 if ((ptr = strchr(repl,
'/')) != NULL)
1394 if ((ptr = strchr(prepl,
'/')) != NULL)
1403 NIAddAffix(Conf, sflag, flagflags | aflg, pmask, pfind, prepl,
1437 bool suffixes =
false;
1441 bool oldformat =
false;
1442 char *recoded = NULL;
1446 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1447 errmsg(
"could not open affix file \"%s\": %m",
1456 pstr =
str_tolower(recoded, strlen(recoded), DEFAULT_COLLATION_OID);
1459 if (*pstr ==
'#' || *pstr ==
'\n')
1462 if (
STRNCMP(pstr,
"compoundwords") == 0)
1468 while (*s && !isspace((
unsigned char) *s))
1470 while (*s && isspace((
unsigned char) *s))
1482 if (
STRNCMP(pstr,
"suffixes") == 0)
1489 if (
STRNCMP(pstr,
"prefixes") == 0)
1496 if (
STRNCMP(pstr,
"flag") == 0)
1501 while (*s && isspace((
unsigned char) *s))
1529 if (*s ==
'\0' || *s ==
'#' || *s ==
'\n' || *s ==
':' ||
1530 isspace((
unsigned char) *s))
1538 if (
STRNCMP(recoded,
"COMPOUNDFLAG") == 0 ||
1539 STRNCMP(recoded,
"COMPOUNDMIN") == 0 ||
1540 STRNCMP(recoded,
"PFX") == 0 ||
1562 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1563 errmsg(
"affix file contains both old-style and new-style commands")));
1580 Assert(a1 < Conf->nAffixData && a2 < Conf->nAffixData);
1629 Assert(affix < Conf->nAffixData);
1648 char lastchar =
'\0';
1653 for (
i = low;
i < high;
i++)
1668 for (
i = low;
i < high;
i++)
1685 bool clearCompoundOnly =
false;
1710 if (clearCompoundOnly)
1711 data->compoundflag &= ~FF_COMPOUNDONLY;
1747 curaffix = strtol(Conf->
Spell[
i]->
p.
flag, &end, 10);
1748 if (Conf->
Spell[
i]->
p.
flag == end || errno == ERANGE)
1750 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1751 errmsg(
"invalid affix alias \"%s\"",
1753 if (curaffix < 0 || curaffix >= Conf->
nAffixData)
1755 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1756 errmsg(
"invalid affix alias \"%s\"",
1758 if (*end !=
'\0' && !isdigit((
unsigned char) *end) && !isspace((
unsigned char) *end))
1760 (
errcode(ERRCODE_CONFIG_FILE_ERROR),
1761 errmsg(
"invalid affix alias \"%s\"",
1806 Assert(curaffix < naffix);
1839 uint8 lastchar =
'\0';
1846 for (
i = low;
i < high;
i++)
1864 for (
i = low;
i < high;
i++)
1877 memcpy(
data->aff, aff,
sizeof(
AFFIX *) * naff);
1888 aff[naff++] = Conf->
Affix +
i;
1898 memcpy(
data->aff, aff,
sizeof(
AFFIX *) * naff);
1916 int start = (issuffix) ? startsuffix : 0;
1917 int end = (issuffix) ? Conf->
naffixes : startsuffix;
1925 Affix->data->node = Conf->
Suffix;
1944 Affix->data->naff = (
uint32) cnt;
1950 Affix->data->aff[cnt] = Conf->
Affix +
i;
2010 strbncmp((
const unsigned char *) (ptr - 1)->affix,
2011 (
const unsigned char *) Affix->
repl,
2047 while (node && *level < wrdlen)
2049 StopLow = node->
data;
2051 while (StopLow < StopHigh)
2053 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
2059 if (StopMiddle->
naff)
2061 node = StopMiddle->
node;
2065 StopLow = StopMiddle + 1;
2067 StopHigh = StopMiddle;
2069 if (StopLow >= StopHigh)
2115 strcpy(newword,
word);
2126 if (baselen && *baselen + strlen(Affix->
find) <= Affix->
replen)
2128 strcpy(newword, Affix->
find);
2149 newword_len = strlen(newword);
2170 if (forms ==
cur || strcmp(
word, *(
cur - 1)) != 0)
2187 int wrdlen = strlen(
word),
2220 for (
j = 0;
j < prefix->naff;
j++)
2229 pnode = prefix->node;
2245 for (
i = 0;
i < suffix->
naff;
i++)
2256 swrdlen = strlen(newword);
2262 for (
j = 0;
j < prefix->naff;
j++)
2264 if (
CheckAffix(newword, swrdlen, prefix->aff[
j],
flag, pnewword, &baselen))
2274 pnode = prefix->node;
2279 snode = suffix->
node;
2309 while ((*ptr)->affix)
2311 if (
len > (*ptr)->len && strncmp((*ptr)->affix,
word, (*ptr)->len) == 0)
2314 issuffix = (*ptr)->issuffix;
2316 return (issuffix) ?
len : 0;
2325 while ((*ptr)->affix)
2327 if (
len > (*ptr)->len && (affbegin = strstr(
word, (*ptr)->affix)) != NULL)
2329 len = (*ptr)->len + (affbegin -
word);
2330 issuffix = (*ptr)->issuffix;
2332 return (issuffix) ?
len : 0;
2386 int level = (snode) ? minpos :
startpos;
2391 int compoundflag = 0;
2396 notprobed = (
char *)
palloc(wordlen);
2397 memset(notprobed, 1, wordlen);
2400 while (level < wordlen)
2412 lenaff = level -
startpos + lenaff;
2414 if (!notprobed[
startpos + lenaff - 1])
2417 if (level + lenaff - 1 <= minpos)
2428 else if (level == wordlen - 1)
2438 char **sptr = subres;
2440 notprobed[
startpos + lenaff - 1] = 0;
2461 StopLow = node->
data;
2463 while (StopLow < StopHigh)
2465 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
2469 StopLow = StopMiddle + 1;
2471 StopHigh = StopMiddle;
2474 if (StopLow < StopHigh)
2478 else if (level == wordlen - 1)
2484 if (StopMiddle->
isword &&
2492 if (wordlen == level + 1)
2516 node = StopMiddle->
node;
2536 (*lcur)->lexeme =
word;
2537 (*lcur)->flags = flags;
2538 (*lcur)->nvariant = NVariant;
2540 (*lcur)->lexeme = NULL;
2558 while (*ptr && (lcur - lres) <
MAX_NORM)
2560 addNorm(&lres, &lcur, *ptr, 0, NVariant++);
2568 int wordlen = strlen(
word);
2581 char **subptr = subres;
2585 for (
i = 0;
i < var->
nstem - 1;
i++)
2590 addNorm(&lres, &lcur, *subptr, 0, NVariant);
2596 var->
stem[0] = NULL;
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
Assert(PointerIsAligned(start, uint64))
static const FormData_pg_attribute a1
static const FormData_pg_attribute a2
if(TABLE==NULL||TABLE_index==NULL)
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
int pg_mblen(const char *mbstr)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurTransactionContext
char * pnstrdup(const char *in, Size len)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static XLogRecPtr startpos
#define qsort(a, b, c, d)
static void prefixes(struct vars *v)
int pg_regcomp(regex_t *re, const chr *string, size_t len, int flags, Oid collation)
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
static int find(struct vars *v, struct cnfa *cnfa, struct colormap *cm)
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
void RS_compile(Regis *r, bool issuffix, const char *str)
bool RS_execute(Regis *r, char *str)
bool RS_isRegis(const char *str)
void NIStartBuild(IspellDict *Conf)
#define GETWCHAR(W, L, N, T)
static int strbcmp(const unsigned char *s1, const unsigned char *s2)
void NIFinishBuild(IspellDict *Conf)
void NIImportAffixes(IspellDict *Conf, const char *filename)
static char * cpstrdup(IspellDict *Conf, const char *str)
static int parse_ooaffentry(char *str, char *type, char *flag, char *find, char *repl, char *mask)
static const char * getAffixFlagSet(IspellDict *Conf, char *s)
static SPNode * mkSPNode(IspellDict *Conf, int low, int high, int level)
static int cmpspell(const void *s1, const void *s2)
static bool get_nextfield(char **str, char *next)
void NISortDictionary(IspellDict *Conf)
static int FindWord(IspellDict *Conf, const char *word, const char *affixflag, int flag)
static AffixNodeData * FindAffixes(AffixNode *node, const char *word, int wrdlen, int *level, int type)
static char * CheckAffix(const char *word, size_t len, AFFIX *Affix, int flagflags, char *newword, int *baselen)
static SplitVar * CopyVar(SplitVar *s, int makedup)
static void NIAddSpell(IspellDict *Conf, const char *word, const char *flag)
static void NIImportOOAffixes(IspellDict *Conf, const char *filename)
#define COMPACT_ALLOC_CHUNK
static void addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant)
static void NIAddAffix(IspellDict *Conf, const char *flag, char flagflags, const char *mask, const char *find, const char *repl, int type)
static char * findchar(char *str, int c)
static const char * VoidString
static char ** NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
static int CheckCompoundAffixes(CMPDAffix **ptr, const char *word, int len, bool CheckInPlace)
static void getNextFlagFromString(IspellDict *Conf, const char **sflagset, char *sflag)
void NISortAffixes(IspellDict *Conf)
static int cmpcmdflag(const void *f1, const void *f2)
static char * findchar2(char *str, int c1, int c2)
static void setCompoundAffixFlagValue(IspellDict *Conf, CompoundAffixFlag *entry, char *s, uint32 val)
void NIImportDictionary(IspellDict *Conf, const char *filename)
static AffixNode * mkANode(IspellDict *Conf, int low, int high, int level, int type)
static bool IsAffixFlagInUse(IspellDict *Conf, int affix, const char *affixflag)
static bool parse_affentry(char *str, char *mask, char *find, char *repl)
static SplitVar * SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
static uint32 makeCompoundFlags(IspellDict *Conf, int affix)
static int cmpspellaffix(const void *s1, const void *s2)
static int strbncmp(const unsigned char *s1, const unsigned char *s2, size_t count)
TSLexeme * NINormalizeWord(IspellDict *Conf, const char *word)
static int MergeAffix(IspellDict *Conf, int a1, int a2)
static void * compact_palloc0(IspellDict *Conf, size_t size)
static int getCompoundAffixFlagValue(IspellDict *Conf, const char *s)
static void AddStem(SplitVar *v, char *word)
static int addToResult(char **forms, char **cur, char *word)
static void mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
static bool isAffixInUse(IspellDict *Conf, const char *affixflag)
static void addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
static char * lowerstr_ctx(IspellDict *Conf, const char *src)
static int cmpaffix(const void *s1, const void *s2)
#define FF_COMPOUNDFLAGMASK
#define FF_COMPOUNDFORBIDFLAG
#define FF_COMPOUNDPERMITFLAG
#define FF_COMPOUNDMIDDLE
void check_stack_depth(void)
AffixNodeData data[FLEXIBLE_ARRAY_MEMBER]
union CompoundAffixFlag::@139 flag
CompoundAffixFlag * CompoundAffixFlags
CMPDAffix * CompoundAffix
SPNodeData data[FLEXIBLE_ARRAY_MEMBER]
union aff_struct::@138 reg
union spell_struct::@136 p
char word[FLEXIBLE_ARRAY_MEMBER]
struct spell_struct::@136::@137 d
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
char * tsearch_readline(tsearch_readline_state *stp)
int t_isalpha(const char *ptr)
void tsearch_readline_end(tsearch_readline_state *stp)