diff options
Diffstat (limited to 'contrib/tsearch2')
-rw-r--r-- | contrib/tsearch2/common.c | 77 | ||||
-rw-r--r-- | contrib/tsearch2/common.h | 15 | ||||
-rw-r--r-- | contrib/tsearch2/dict.c | 30 | ||||
-rw-r--r-- | contrib/tsearch2/gistidx.c | 201 | ||||
-rw-r--r-- | contrib/tsearch2/ispell/regis.c | 194 | ||||
-rw-r--r-- | contrib/tsearch2/ispell/regis.h | 48 | ||||
-rw-r--r-- | contrib/tsearch2/ispell/spell.c | 1010 | ||||
-rw-r--r-- | contrib/tsearch2/ispell/spell.h | 130 | ||||
-rw-r--r-- | contrib/tsearch2/query.c | 17 | ||||
-rw-r--r-- | contrib/tsearch2/snmap.c | 6 | ||||
-rw-r--r-- | contrib/tsearch2/ts_cfg.c | 59 | ||||
-rw-r--r-- | contrib/tsearch2/ts_stat.c | 76 | ||||
-rw-r--r-- | contrib/tsearch2/tsvector.c | 124 | ||||
-rw-r--r-- | contrib/tsearch2/wordparser/parser.h | 4 | ||||
-rw-r--r-- | contrib/tsearch2/wparser.c | 56 | ||||
-rw-r--r-- | contrib/tsearch2/wparser_def.c | 75 |
16 files changed, 1210 insertions, 912 deletions
diff --git a/contrib/tsearch2/common.c b/contrib/tsearch2/common.c index b51c3e05833..4984c3d256b 100644 --- a/contrib/tsearch2/common.c +++ b/contrib/tsearch2/common.c @@ -21,7 +21,7 @@ #include "dict.h" -Oid TSNSP_FunctionOid = InvalidOid; +Oid TSNSP_FunctionOid = InvalidOid; text * @@ -121,44 +121,45 @@ text_cmp(text *a, text *b) } -char* -get_namespace(Oid funcoid) { - HeapTuple tuple; - Form_pg_proc proc; - Form_pg_namespace nsp; - Oid nspoid; - char *txt; - - tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for proc oid %u", funcoid); - proc=(Form_pg_proc) GETSTRUCT(tuple); - nspoid = proc->pronamespace; - ReleaseSysCache(tuple); - - tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for namespace oid %u", nspoid); - nsp = (Form_pg_namespace) GETSTRUCT(tuple); - txt = pstrdup( NameStr((nsp->nspname)) ); - ReleaseSysCache(tuple); - - return txt; +char * +get_namespace(Oid funcoid) +{ + HeapTuple tuple; + Form_pg_proc proc; + Form_pg_namespace nsp; + Oid nspoid; + char *txt; + + tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for proc oid %u", funcoid); + proc = (Form_pg_proc) GETSTRUCT(tuple); + nspoid = proc->pronamespace; + ReleaseSysCache(tuple); + + tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for namespace oid %u", nspoid); + nsp = (Form_pg_namespace) GETSTRUCT(tuple); + txt = pstrdup(NameStr((nsp->nspname))); + ReleaseSysCache(tuple); + + return txt; } Oid -get_oidnamespace(Oid funcoid) { - HeapTuple tuple; - Form_pg_proc proc; - Oid nspoid; - - tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for proc oid %u", funcoid); - proc=(Form_pg_proc) GETSTRUCT(tuple); - nspoid = proc->pronamespace; - ReleaseSysCache(tuple); - - return nspoid; +get_oidnamespace(Oid funcoid) +{ + HeapTuple tuple; + Form_pg_proc proc; + Oid nspoid; + + tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for proc oid %u", funcoid); + proc = (Form_pg_proc) GETSTRUCT(tuple); + nspoid = proc->pronamespace; + ReleaseSysCache(tuple); + + return nspoid; } - diff --git a/contrib/tsearch2/common.h b/contrib/tsearch2/common.h index 6720598f817..c84e841e15e 100644 --- a/contrib/tsearch2/common.h +++ b/contrib/tsearch2/common.h @@ -21,13 +21,14 @@ int text_cmp(text *a, text *b); void ts_error(int state, const char *format,...); -extern Oid TSNSP_FunctionOid; /* oid of called function, needed only for determ namespace, no more */ -char* get_namespace(Oid funcoid); -Oid get_oidnamespace(Oid funcoid); - -#define SET_FUNCOID() do { \ - if ( fcinfo->flinfo && fcinfo->flinfo->fn_oid != InvalidOid ) \ - TSNSP_FunctionOid = fcinfo->flinfo->fn_oid; \ +extern Oid TSNSP_FunctionOid; /* oid of called function, needed only for + * determ namespace, no more */ +char *get_namespace(Oid funcoid); +Oid get_oidnamespace(Oid funcoid); + +#define SET_FUNCOID() do { \ + if ( fcinfo->flinfo && fcinfo->flinfo->fn_oid != InvalidOid ) \ + TSNSP_FunctionOid = fcinfo->flinfo->fn_oid; \ } while(0) #endif diff --git a/contrib/tsearch2/dict.c b/contrib/tsearch2/dict.c index 7be406da9ed..357097681e5 100644 --- a/contrib/tsearch2/dict.c +++ b/contrib/tsearch2/dict.c @@ -26,18 +26,18 @@ init_dict(Oid id, DictInfo * dict) bool isnull; Datum pars[1]; int stat; - void *plan; - char buf[1024]; - char *nsp = get_namespace(TSNSP_FunctionOid); + void *plan; + char buf[1024]; + char *nsp = get_namespace(TSNSP_FunctionOid); arg[0] = OIDOID; pars[0] = ObjectIdGetDatum(id); memset(dict, 0, sizeof(DictInfo)); SPI_connect(); - sprintf(buf,"select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp); + sprintf(buf, "select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp); pfree(nsp); - plan= SPI_prepare(buf, 1, arg); + plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -142,8 +142,9 @@ name2id_dict(text *name) Datum pars[1]; int stat; Oid id = findSNMap_t(&(DList.name2id_map), name); - void *plan; - char buf[1024], *nsp; + void *plan; + char buf[1024], + *nsp; arg[0] = TEXTOID; pars[0] = PointerGetDatum(name); @@ -153,9 +154,9 @@ name2id_dict(text *name) nsp = get_namespace(TSNSP_FunctionOid); SPI_connect(); - sprintf(buf,"select oid from %s.pg_ts_dict where dict_name = $1", nsp); + sprintf(buf, "select oid from %s.pg_ts_dict where dict_name = $1", nsp); pfree(nsp); - plan= SPI_prepare(buf, 1, arg); + plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -245,7 +246,8 @@ lexize_byname(PG_FUNCTION_ARGS) { text *dictname = PG_GETARG_TEXT_P(0); Datum res; - SET_FUNCOID(); + + SET_FUNCOID(); res = DirectFunctionCall3( lexize, @@ -267,7 +269,7 @@ Datum set_curdict(PG_FUNCTION_ARGS); Datum set_curdict(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); finddict(PG_GETARG_OID(0)); currect_dictionary_id = PG_GETARG_OID(0); PG_RETURN_VOID(); @@ -279,7 +281,8 @@ Datum set_curdict_byname(PG_FUNCTION_ARGS) { text *dictname = PG_GETARG_TEXT_P(0); - SET_FUNCOID(); + + SET_FUNCOID(); DirectFunctionCall1( set_curdict, ObjectIdGetDatum(name2id_dict(dictname)) @@ -294,7 +297,8 @@ Datum lexize_bycurrent(PG_FUNCTION_ARGS) { Datum res; - SET_FUNCOID(); + + SET_FUNCOID(); if (currect_dictionary_id == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), diff --git a/contrib/tsearch2/gistidx.c b/contrib/tsearch2/gistidx.c index 6f9475a0c79..f0a9d8828d9 100644 --- a/contrib/tsearch2/gistidx.c +++ b/contrib/tsearch2/gistidx.c @@ -123,8 +123,8 @@ gtsvector_compress(PG_FUNCTION_ARGS) if (entry->leafkey) { /* tsvector */ GISTTYPE *res; - tsvector *toastedval = (tsvector *) DatumGetPointer(entry->key); - tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(entry->key)); + tsvector *toastedval = (tsvector *) DatumGetPointer(entry->key); + tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(entry->key)); int4 len; int4 *arr; WordEntry *ptr = ARRPTR(val); @@ -277,10 +277,10 @@ gtsvector_consistent(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); PG_RETURN_BOOL(TS_execute( - GETQUERY(query), - (void *) GETSIGN(key), false, - checkcondition_bit - )); + GETQUERY(query), + (void *) GETSIGN(key), false, + checkcondition_bit + )); } else { /* only leaf pages */ @@ -289,10 +289,10 @@ gtsvector_consistent(PG_FUNCTION_ARGS) chkval.arrb = GETARR(key); chkval.arre = chkval.arrb + ARRNELEM(key); PG_RETURN_BOOL(TS_execute( - GETQUERY(query), - (void *) &chkval, true, - checkcondition_arr - )); + GETQUERY(query), + (void *) &chkval, true, + checkcondition_arr + )); } } @@ -326,10 +326,11 @@ unionkey(BITVECP sbase, GISTTYPE * add) Datum gtsvector_union(PG_FUNCTION_ARGS) { - GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); + GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); int *size = (int *) PG_GETARG_POINTER(1); BITVEC base; - int4 i,len; + int4 i, + len; int4 flag = 0; GISTTYPE *result; @@ -418,34 +419,39 @@ sizebitvec(BITVECP sign) i; LOOPBYTE( - size += SUMBIT(*(char *) sign); - sign = (BITVECP) (((char *) sign) + 1); + size += SUMBIT(*(char *) sign); + sign = (BITVECP) (((char *) sign) + 1); ); return size; } static int -hemdistsign(BITVECP a, BITVECP b) { - int i,dist=0; +hemdistsign(BITVECP a, BITVECP b) +{ + int i, + dist = 0; LOOPBIT( - if ( GETBIT(a,i) != GETBIT(b,i) ) + if (GETBIT(a, i) != GETBIT(b, i)) dist++; ); return dist; } static int -hemdist(GISTTYPE *a, GISTTYPE *b) { - if ( ISALLTRUE(a) ) { +hemdist(GISTTYPE * a, GISTTYPE * b) +{ + if (ISALLTRUE(a)) + { if (ISALLTRUE(b)) return 0; else - return SIGLENBIT-sizebitvec(GETSIGN(b)); - } else if (ISALLTRUE(b)) - return SIGLENBIT-sizebitvec(GETSIGN(a)); + return SIGLENBIT - sizebitvec(GETSIGN(b)); + } + else if (ISALLTRUE(b)) + return SIGLENBIT - sizebitvec(GETSIGN(a)); - return hemdistsign( GETSIGN(a), GETSIGN(b) ); + return hemdistsign(GETSIGN(a), GETSIGN(b)); } Datum @@ -460,17 +466,19 @@ gtsvector_penalty(PG_FUNCTION_ARGS) *penalty = 0.0; - if (ISARRKEY(newval)) { - BITVEC sign; + if (ISARRKEY(newval)) + { + BITVEC sign; + makesign(sign, newval); - if ( ISALLTRUE(origval) ) - *penalty=((float)(SIGLENBIT-sizebitvec(sign)))/(float)(SIGLENBIT+1); - else - *penalty=hemdistsign(sign,orig); - } else { - *penalty=hemdist(origval,newval); + if (ISALLTRUE(origval)) + *penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1); + else + *penalty = hemdistsign(sign, orig); } + else + *penalty = hemdist(origval, newval); PG_RETURN_POINTER(penalty); } @@ -510,22 +518,25 @@ comparecost(const void *a, const void *b) static int -hemdistcache(CACHESIGN *a, CACHESIGN *b) { - if ( a->allistrue ) { +hemdistcache(CACHESIGN * a, CACHESIGN * b) +{ + if (a->allistrue) + { if (b->allistrue) return 0; else - return SIGLENBIT-sizebitvec(b->sign); - } else if (b->allistrue) - return SIGLENBIT-sizebitvec(a->sign); + return SIGLENBIT - sizebitvec(b->sign); + } + else if (b->allistrue) + return SIGLENBIT - sizebitvec(a->sign); - return hemdistsign( a->sign, b->sign ); + return hemdistsign(a->sign, b->sign); } Datum gtsvector_picksplit(PG_FUNCTION_ARGS) { - GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); + GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0); GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1); OffsetNumber k, j; @@ -556,13 +567,16 @@ gtsvector_picksplit(PG_FUNCTION_ARGS) cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2)); fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber)); - for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) { - for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) { + for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) + { + for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) + { if (k == FirstOffsetNumber) fillcache(&cache[j], GETENTRY(entryvec, j)); - size_waste=hemdistcache(&(cache[j]),&(cache[k])); - if (size_waste > waste) { + size_waste = hemdistcache(&(cache[j]), &(cache[k])); + if (size_waste > waste) + { waste = size_waste; seed_1 = k; seed_2 = j; @@ -575,101 +589,124 @@ gtsvector_picksplit(PG_FUNCTION_ARGS) right = v->spl_right; v->spl_nright = 0; - if (seed_1 == 0 || seed_2 == 0) { + if (seed_1 == 0 || seed_2 == 0) + { seed_1 = 1; seed_2 = 2; } /* form initial .. */ - if (cache[seed_1].allistrue) { + if (cache[seed_1].allistrue) + { datum_l = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0)); datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0); datum_l->flag = SIGNKEY | ALLISTRUE; - } else { + } + else + { datum_l = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY, 0)); datum_l->len = CALCGTSIZE(SIGNKEY, 0); datum_l->flag = SIGNKEY; memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC)); } - if (cache[seed_2].allistrue) { + if (cache[seed_2].allistrue) + { datum_r = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0)); datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0); datum_r->flag = SIGNKEY | ALLISTRUE; - } else { + } + else + { datum_r = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY, 0)); datum_r->len = CALCGTSIZE(SIGNKEY, 0); datum_r->flag = SIGNKEY; memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC)); } - union_l=GETSIGN(datum_l); - union_r=GETSIGN(datum_r); + union_l = GETSIGN(datum_l); + union_r = GETSIGN(datum_r); maxoff = OffsetNumberNext(maxoff); fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff)); /* sort before ... */ costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff); - for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) { + for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) + { costvector[j - 1].pos = j; size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j])); - size_beta = hemdistcache(&(cache[seed_2]), &(cache[j])); + size_beta = hemdistcache(&(cache[seed_2]), &(cache[j])); costvector[j - 1].cost = abs(size_alpha - size_beta); } qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost); - for (k = 0; k < maxoff; k++) { + for (k = 0; k < maxoff; k++) + { j = costvector[k].pos; - if (j == seed_1) { + if (j == seed_1) + { *left++ = j; v->spl_nleft++; continue; - } else if (j == seed_2) { + } + else if (j == seed_2) + { *right++ = j; v->spl_nright++; continue; } - if (ISALLTRUE(datum_l) || cache[j].allistrue) { - if ( ISALLTRUE(datum_l) && cache[j].allistrue ) - size_alpha=0; + if (ISALLTRUE(datum_l) || cache[j].allistrue) + { + if (ISALLTRUE(datum_l) && cache[j].allistrue) + size_alpha = 0; else - size_alpha = SIGLENBIT-sizebitvec( - ( cache[j].allistrue ) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign) - ); - } else { - size_alpha=hemdistsign(cache[j].sign,GETSIGN(datum_l)); + size_alpha = SIGLENBIT - sizebitvec( + (cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign) + ); } + else + size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l)); - if (ISALLTRUE(datum_r) || cache[j].allistrue) { - if ( ISALLTRUE(datum_r) && cache[j].allistrue ) - size_beta=0; + if (ISALLTRUE(datum_r) || cache[j].allistrue) + { + if (ISALLTRUE(datum_r) && cache[j].allistrue) + size_beta = 0; else - size_beta = SIGLENBIT-sizebitvec( - ( cache[j].allistrue ) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign) - ); - } else { - size_beta=hemdistsign(cache[j].sign,GETSIGN(datum_r)); + size_beta = SIGLENBIT - sizebitvec( + (cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign) + ); } + else + size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r)); - if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1)) { - if (ISALLTRUE(datum_l) || cache[j].allistrue) { - if (! ISALLTRUE(datum_l) ) + if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1)) + { + if (ISALLTRUE(datum_l) || cache[j].allistrue) + { + if (!ISALLTRUE(datum_l)) MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC)); - } else { - ptr=cache[j].sign; + } + else + { + ptr = cache[j].sign; LOOPBYTE( - union_l[i] |= ptr[i]; + union_l[i] |= ptr[i]; ); } *left++ = j; v->spl_nleft++; - } else { - if (ISALLTRUE(datum_r) || cache[j].allistrue) { - if (! ISALLTRUE(datum_r) ) + } + else + { + if (ISALLTRUE(datum_r) || cache[j].allistrue) + { + if (!ISALLTRUE(datum_r)) MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC)); - } else { - ptr=cache[j].sign; + } + else + { + ptr = cache[j].sign; LOOPBYTE( - union_r[i] |= ptr[i]; + union_r[i] |= ptr[i]; ); } *right++ = j; diff --git a/contrib/tsearch2/ispell/regis.c b/contrib/tsearch2/ispell/regis.c index 052413788b0..996417b18a9 100644 --- a/contrib/tsearch2/ispell/regis.c +++ b/contrib/tsearch2/ispell/regis.c @@ -2,105 +2,132 @@ #include <stdlib.h> #include <string.h> #include <ctype.h> - + #include "regis.h" #include "common.h" int -RS_isRegis(const char *str) { - unsigned char *ptr=(unsigned char *)str; +RS_isRegis(const char *str) +{ + unsigned char *ptr = (unsigned char *) str; - while(ptr && *ptr) - if ( isalpha(*ptr) || *ptr=='[' || *ptr==']' || *ptr=='^') + while (ptr && *ptr) + if (isalpha(*ptr) || *ptr == '[' || *ptr == ']' || *ptr == '^') ptr++; else return 0; - return 1; + return 1; } -#define RS_IN_ONEOF 1 +#define RS_IN_ONEOF 1 #define RS_IN_ONEOF_IN 2 #define RS_IN_NONEOF 3 #define RS_IN_WAIT 4 -static RegisNode* -newRegisNode(RegisNode *prev, int len) { - RegisNode *ptr; - ptr = (RegisNode*)malloc(RNHDRSZ+len+1); +static RegisNode * +newRegisNode(RegisNode * prev, int len) +{ + RegisNode *ptr; + + ptr = (RegisNode *) malloc(RNHDRSZ + len + 1); if (!ptr) - ts_error(ERROR, "No memory"); - memset(ptr,0,RNHDRSZ+len+1); + ts_error(ERROR, "No memory"); + memset(ptr, 0, RNHDRSZ + len + 1); if (prev) - prev->next=ptr; + prev->next = ptr; return ptr; } int -RS_compile(Regis *r, int issuffix, const char *str) { - int i,len = strlen(str); - int state = RS_IN_WAIT; - RegisNode *ptr=NULL; - - memset(r,0,sizeof(Regis)); +RS_compile(Regis * r, int issuffix, const char *str) +{ + int i, + len = strlen(str); + int state = RS_IN_WAIT; + RegisNode *ptr = NULL; + + memset(r, 0, sizeof(Regis)); r->issuffix = (issuffix) ? 1 : 0; - for(i=0;i<len;i++) { - unsigned char c = *( ( (unsigned char*)str ) + i ); - if ( state == RS_IN_WAIT ) { - if ( isalpha(c) ) { - if ( ptr ) - ptr = newRegisNode(ptr,len); + for (i = 0; i < len; i++) + { + unsigned char c = *(((unsigned char *) str) + i); + + if (state == RS_IN_WAIT) + { + if (isalpha(c)) + { + if (ptr) + ptr = newRegisNode(ptr, len); else - ptr = r->node = newRegisNode(NULL,len); - ptr->data[ 0 ] = c; + ptr = r->node = newRegisNode(NULL, len); + ptr->data[0] = c; ptr->type = RSF_ONEOF; - ptr->len=1; - } else if ( c=='[' ) { - if ( ptr ) - ptr = newRegisNode(ptr,len); + ptr->len = 1; + } + else if (c == '[') + { + if (ptr) + ptr = newRegisNode(ptr, len); else - ptr = r->node = newRegisNode(NULL,len); + ptr = r->node = newRegisNode(NULL, len); ptr->type = RSF_ONEOF; - state=RS_IN_ONEOF; - } else - ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1); - } else if ( state == RS_IN_ONEOF ) { - if ( c=='^' ) { + state = RS_IN_ONEOF; + } + else + ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); + } + else if (state == RS_IN_ONEOF) + { + if (c == '^') + { ptr->type = RSF_NONEOF; - state=RS_IN_NONEOF; - } else if ( isalpha(c) ) { - ptr->data[ 0 ] = c; - ptr->len=1; - state=RS_IN_ONEOF_IN; - } else - ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1); - } else if ( state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF ) { - if ( isalpha(c) ) { - ptr->data[ ptr->len ] = c; + state = RS_IN_NONEOF; + } + else if (isalpha(c)) + { + ptr->data[0] = c; + ptr->len = 1; + state = RS_IN_ONEOF_IN; + } + else + ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); + } + else if (state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF) + { + if (isalpha(c)) + { + ptr->data[ptr->len] = c; ptr->len++; - } else if ( c==']' ) { - state=RS_IN_WAIT; - } else - ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1); - } else - ts_error(ERROR,"Internal error in RS_compile: %d\n", state); + } + else if (c == ']') + state = RS_IN_WAIT; + else + ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); + } + else + ts_error(ERROR, "Internal error in RS_compile: %d\n", state); } ptr = r->node; - while(ptr) { + while (ptr) + { r->nchar++; - ptr=ptr->next; + ptr = ptr->next; } return 0; } -void -RS_free(Regis *r) { - RegisNode *ptr=r->node,*tmp; +void +RS_free(Regis * r) +{ + RegisNode *ptr = r->node, + *tmp; - while(ptr) { - tmp=ptr->next; + while (ptr) + { + tmp = ptr->next; free(ptr); ptr = tmp; } @@ -108,42 +135,49 @@ RS_free(Regis *r) { r->node = NULL; } -int -RS_execute(Regis *r, const char *str, int len) { - RegisNode *ptr=r->node; +int +RS_execute(Regis * r, const char *str, int len) +{ + RegisNode *ptr = r->node; unsigned char *c; - if (len<0) - len=strlen(str); + if (len < 0) + len = strlen(str); - if (len<r->nchar) + if (len < r->nchar) return 0; - if ( r->issuffix ) - c = ((unsigned char*)str) + len - r->nchar; + if (r->issuffix) + c = ((unsigned char *) str) + len - r->nchar; else - c = (unsigned char*)str; + c = (unsigned char *) str; - while(ptr) { - switch(ptr->type) { + while (ptr) + { + switch (ptr->type) + { case RSF_ONEOF: - if ( ptr->len==0 ) { - if ( *c != *(ptr->data) ) + if (ptr->len == 0) + { + if (*c != *(ptr->data)) return 0; - } else if ( strchr((char*)ptr->data, *c) == NULL ) + } + else if (strchr((char *) ptr->data, *c) == NULL) return 0; break; case RSF_NONEOF: - if ( ptr->len==0 ) { - if ( *c == *(ptr->data) ) + if (ptr->len == 0) + { + if (*c == *(ptr->data)) return 0; - } else if ( strchr((char*)ptr->data, *c) != NULL ) + } + else if (strchr((char *) ptr->data, *c) != NULL) return 0; break; default: - ts_error(ERROR,"RS_execute: Unknown type node: %d\n", ptr->type); + ts_error(ERROR, "RS_execute: Unknown type node: %d\n", ptr->type); } - ptr=ptr->next; + ptr = ptr->next; c++; } diff --git a/contrib/tsearch2/ispell/regis.h b/contrib/tsearch2/ispell/regis.h index 64a7e9d996c..fd03de45e98 100644 --- a/contrib/tsearch2/ispell/regis.h +++ b/contrib/tsearch2/ispell/regis.h @@ -1,34 +1,38 @@ #ifndef __REGIS_H__ #define __REGIS_H__ -#include "postgres.h" +#include "postgres.h" -typedef struct RegisNode { - uint32 - type:2, - len:16, - unused:14; +typedef struct RegisNode +{ + uint32 + type:2, + len:16, + unused:14; struct RegisNode *next; - unsigned char data[1]; -} RegisNode; + unsigned char data[1]; +} RegisNode; -#define RNHDRSZ (sizeof(uint32)+sizeof(void*)) +#define RNHDRSZ (sizeof(uint32)+sizeof(void*)) -#define RSF_ONEOF 1 -#define RSF_NONEOF 2 +#define RSF_ONEOF 1 +#define RSF_NONEOF 2 -typedef struct Regis { - RegisNode *node; - uint32 - issuffix:1, - nchar:16, - unused:15; -} Regis; +typedef struct Regis +{ + RegisNode *node; + uint32 + issuffix:1, + nchar:16, + unused:15; +} Regis; -int RS_isRegis(const char *str); +int RS_isRegis(const char *str); + +int RS_compile(Regis * r, int issuffix, const char *str); +void RS_free(Regis * r); -int RS_compile(Regis *r, int issuffix, const char *str); -void RS_free(Regis *r); /*возвращает 1 если матчится */ -int RS_execute(Regis *r, const char *str, int len); +int RS_execute(Regis * r, const char *str, int len); + #endif diff --git a/contrib/tsearch2/ispell/spell.c b/contrib/tsearch2/ispell/spell.c index 3a3f19b1e8c..c5783236b63 100644 --- a/contrib/tsearch2/ispell/spell.c +++ b/contrib/tsearch2/ispell/spell.c @@ -40,13 +40,16 @@ strlower(char *str) } } -static char* -strnduplicate(char *s, int len) { - char *d=(char*)palloc( len + 1 ); - memcpy(d, s, len ); - d[len]='\0'; +static char * +strnduplicate(char *s, int len) +{ + char *d = (char *) palloc(len + 1); + + memcpy(d, s, len); + d[len] = '\0'; return d; } + /* backward string compaire for suffix tree operations */ static int strbcmp(const unsigned char *s1, const unsigned char *s2) @@ -188,33 +191,39 @@ NIImportDictionary(IspellDict * Conf, const char *filename) static int FindWord(IspellDict * Conf, const char *word, int affixflag, char compoundonly) { - SPNode *node = Conf->Dictionary; - SPNodeData *StopLow, *StopHigh, *StopMiddle; - uint8 *ptr =(uint8*)word; + SPNode *node = Conf->Dictionary; + SPNodeData *StopLow, + *StopHigh, + *StopMiddle; + uint8 *ptr = (uint8 *) word; - while( node && *ptr) { + while (node && *ptr) + { StopLow = node->data; - StopHigh = node->data+node->length; - while (StopLow < StopHigh) { + StopHigh = node->data + node->length; + while (StopLow < StopHigh) + { StopMiddle = StopLow + ((StopHigh - StopLow) >> 1); - if ( StopMiddle->val == *ptr ) { - if ( *(ptr+1)=='\0' && StopMiddle->isword ) { - if ( compoundonly && !StopMiddle->compoundallow ) + if (StopMiddle->val == *ptr) + { + if (*(ptr + 1) == '\0' && StopMiddle->isword) + { + if (compoundonly && !StopMiddle->compoundallow) return 0; - if ( (affixflag == 0) || (strchr(Conf->AffixData[StopMiddle->affix], affixflag) != NULL)) + if ((affixflag == 0) || (strchr(Conf->AffixData[StopMiddle->affix], affixflag) != NULL)) return 1; } - node=StopMiddle->node; + node = StopMiddle->node; ptr++; break; - } else if ( StopMiddle->val < *ptr ) { + } + else if (StopMiddle->val < *ptr) StopLow = StopMiddle + 1; - } else { + else StopHigh = StopMiddle; - } } - if ( StopLow >= StopHigh ) - break; + if (StopLow >= StopHigh) + break; } return 0; } @@ -237,31 +246,36 @@ NIAddAffix(IspellDict * Conf, int flag, char flagflags, const char *mask, const MEMOUT(Conf->Affix); } - if ( strcmp(mask,".")==0 ) { - Conf->Affix[Conf->naffixes].issimple=1; - Conf->Affix[Conf->naffixes].isregis=0; - *( Conf->Affix[Conf->naffixes].mask )='\0'; - } else if ( RS_isRegis(mask) ) { - Conf->Affix[Conf->naffixes].issimple=0; - Conf->Affix[Conf->naffixes].isregis=1; - strcpy(Conf->Affix[Conf->naffixes].mask, mask); - } else { - Conf->Affix[Conf->naffixes].issimple=0; - Conf->Affix[Conf->naffixes].isregis=0; - if (type == FF_SUFFIX) - sprintf(Conf->Affix[Conf->naffixes].mask, "%s$", mask); - else - sprintf(Conf->Affix[Conf->naffixes].mask, "^%s", mask); - } - Conf->Affix[Conf->naffixes].compile = 1; - Conf->Affix[Conf->naffixes].flagflags = flagflags; - Conf->Affix[Conf->naffixes].flag = flag; - Conf->Affix[Conf->naffixes].type = type; - - strcpy(Conf->Affix[Conf->naffixes].find, find); - strcpy(Conf->Affix[Conf->naffixes].repl, repl); - Conf->Affix[Conf->naffixes].replen = strlen(repl); - Conf->naffixes++; + if (strcmp(mask, ".") == 0) + { + Conf->Affix[Conf->naffixes].issimple = 1; + Conf->Affix[Conf->naffixes].isregis = 0; + *(Conf->Affix[Conf->naffixes].mask) = '\0'; + } + else if (RS_isRegis(mask)) + { + Conf->Affix[Conf->naffixes].issimple = 0; + Conf->Affix[Conf->naffixes].isregis = 1; + strcpy(Conf->Affix[Conf->naffixes].mask, mask); + } + else + { + Conf->Affix[Conf->naffixes].issimple = 0; + Conf->Affix[Conf->naffixes].isregis = 0; + if (type == FF_SUFFIX) + sprintf(Conf->Affix[Conf->naffixes].mask, "%s$", mask); + else + sprintf(Conf->Affix[Conf->naffixes].mask, "^%s", mask); + } + Conf->Affix[Conf->naffixes].compile = 1; + Conf->Affix[Conf->naffixes].flagflags = flagflags; + Conf->Affix[Conf->naffixes].flag = flag; + Conf->Affix[Conf->naffixes].type = type; + + strcpy(Conf->Affix[Conf->naffixes].find, find); + strcpy(Conf->Affix[Conf->naffixes].repl, repl); + Conf->Affix[Conf->naffixes].replen = strlen(repl); + Conf->naffixes++; return (0); } @@ -304,46 +318,55 @@ NIImportAffixes(IspellDict * Conf, const char *filename) if (!(affix = fopen(filename, "r"))) return (1); - Conf->compoundcontrol='\t'; + Conf->compoundcontrol = '\t'; while (fgets(str, sizeof(str), affix)) { - if (STRNCASECMP(str, "compoundwords")==0) { - s=strchr(str, 'l'); - if ( s ) { - while( *s!=' ' ) s++; - while( *s==' ' ) s++; + if (STRNCASECMP(str, "compoundwords") == 0) + { + s = strchr(str, 'l'); + if (s) + { + while (*s != ' ') + s++; + while (*s == ' ') + s++; Conf->compoundcontrol = *s; - continue; + continue; } } - if (STRNCASECMP(str, "suffixes")==0) + if (STRNCASECMP(str, "suffixes") == 0) { suffixes = 1; prefixes = 0; continue; } - if (STRNCASECMP(str, "prefixes")==0) + if (STRNCASECMP(str, "prefixes") == 0) { suffixes = 0; prefixes = 1; continue; } - if (STRNCASECMP(str, "flag ")==0) + if (STRNCASECMP(str, "flag ") == 0) { s = str + 5; - flagflags=0; - while( *s==' ' ) s++; - if ( *s=='*' ) { - flagflags|=FF_CROSSPRODUCT; + flagflags = 0; + while (*s == ' ') + s++; + if (*s == '*') + { + flagflags |= FF_CROSSPRODUCT; s++; - } else if ( *s=='~' ) { - flagflags|=FF_COMPOUNDONLYAFX; + } + else if (*s == '~') + { + flagflags |= FF_COMPOUNDONLYAFX; s++; } - if ( *s=='\\' ) s++; - + if (*s == '\\') + s++; + flag = *s; continue; } @@ -387,84 +410,93 @@ NIImportAffixes(IspellDict * Conf, const char *filename) return (0); } -static int -MergeAffix(IspellDict *Conf, int a1, int a2) { - int naffix=0; - char **ptr=Conf->AffixData; +static int +MergeAffix(IspellDict * Conf, int a1, int a2) +{ + int naffix = 0; + char **ptr = Conf->AffixData; - while(*ptr) { + while (*ptr) + { naffix++; ptr++; } - - Conf->AffixData=(char**)realloc( Conf->AffixData, (naffix+2)*sizeof(char*) ); + + Conf->AffixData = (char **) realloc(Conf->AffixData, (naffix + 2) * sizeof(char *)); MEMOUT(Conf->AffixData); ptr = Conf->AffixData + naffix; - *ptr=malloc( strlen(Conf->AffixData[a1]) + strlen(Conf->AffixData[a2]) + 1 /* space */ + 1 /* \0 */ ); + *ptr = malloc(strlen(Conf->AffixData[a1]) + strlen(Conf->AffixData[a2]) + 1 /* space */ + 1 /* \0 */ ); MEMOUT(ptr); sprintf(*ptr, "%s %s", Conf->AffixData[a1], Conf->AffixData[a2]); ptr++; - *ptr='\0'; - return naffix; + *ptr = '\0'; + return naffix; } -static SPNode* -mkSPNode(IspellDict *Conf, int low, int high, int level) { - int i; - int nchar=0; - char lastchar='\0'; - SPNode *rs; +static SPNode * +mkSPNode(IspellDict * Conf, int low, int high, int level) +{ + int i; + int nchar = 0; + char lastchar = '\0'; + SPNode *rs; SPNodeData *data; - int lownew=low; + int lownew = low; - for(i=low; i<high; i++) - if ( Conf->Spell[i].p.d.len>level && lastchar!=Conf->Spell[i].word[level] ) { + for (i = low; i < high; i++) + if (Conf->Spell[i].p.d.len > level && lastchar != Conf->Spell[i].word[level]) + { nchar++; - lastchar=Conf->Spell[i].word[level]; + lastchar = Conf->Spell[i].word[level]; } if (!nchar) return NULL; - rs=(SPNode*)malloc(SPNHRDSZ+nchar*sizeof(SPNodeData)); + rs = (SPNode *) malloc(SPNHRDSZ + nchar * sizeof(SPNodeData)); MEMOUT(rs); - memset(rs,0,SPNHRDSZ+nchar*sizeof(SPNodeData)); + memset(rs, 0, SPNHRDSZ + nchar * sizeof(SPNodeData)); rs->length = nchar; - data=rs->data; - - lastchar='\0'; - for(i=low; i<high; i++) - if ( Conf->Spell[i].p.d.len>level ) { - if ( lastchar!=Conf->Spell[i].word[level] ) { - if ( lastchar ) { - data->node = mkSPNode(Conf, lownew, i, level+1); - lownew=i; + data = rs->data; + + lastchar = '\0'; + for (i = low; i < high; i++) + if (Conf->Spell[i].p.d.len > level) + { + if (lastchar != Conf->Spell[i].word[level]) + { + if (lastchar) + { + data->node = mkSPNode(Conf, lownew, i, level + 1); + lownew = i; data++; } - lastchar=Conf->Spell[i].word[level]; + lastchar = Conf->Spell[i].word[level]; } - data->val=((uint8*)(Conf->Spell[i].word))[level]; - if ( Conf->Spell[i].p.d.len == level+1 ) { - if ( data->isword && data->affix!=Conf->Spell[i].p.d.affix) { - /* - fprintf(stderr,"Word already exists: %s (affixes: '%s' and '%s')\n", - Conf->Spell[i].word, - Conf->AffixData[data->affix], - Conf->AffixData[Conf->Spell[i].p.d.affix] - ); - */ + data->val = ((uint8 *) (Conf->Spell[i].word))[level]; + if (Conf->Spell[i].p.d.len == level + 1) + { + if (data->isword && data->affix != Conf->Spell[i].p.d.affix) + { + /* + * fprintf(stderr,"Word already exists: %s (affixes: + * '%s' and '%s')\n", Conf->Spell[i].word, + * Conf->AffixData[data->affix], + * Conf->AffixData[Conf->Spell[i].p.d.affix] ); + */ /* MergeAffix called a few times */ data->affix = MergeAffix(Conf, data->affix, Conf->Spell[i].p.d.affix); - } else + } + else data->affix = Conf->Spell[i].p.d.affix; - data->isword=1; - if ( strchr( Conf->AffixData[ data->affix ], Conf->compoundcontrol ) ) - data->compoundallow=1; + data->isword = 1; + if (strchr(Conf->AffixData[data->affix], Conf->compoundcontrol)) + data->compoundallow = 1; } } - - data->node = mkSPNode(Conf, lownew, high, level+1); + + data->node = mkSPNode(Conf, lownew, high, level + 1); return rs; } @@ -475,132 +507,147 @@ void NISortDictionary(IspellDict * Conf) { size_t i; - int naffix=3; - + int naffix = 3; + /* compress affixes */ qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL), cmpspellaffix); for (i = 1; i < Conf->nspell; i++) - if ( strcmp(Conf->Spell[i].p.flag,Conf->Spell[i-1].p.flag) ) + if (strcmp(Conf->Spell[i].p.flag, Conf->Spell[i - 1].p.flag)) naffix++; - Conf->AffixData=(char**)malloc( naffix*sizeof(char*) ); + Conf->AffixData = (char **) malloc(naffix * sizeof(char *)); MEMOUT(Conf->AffixData); - memset(Conf->AffixData, 0, naffix*sizeof(char*)); - naffix=1; - Conf->AffixData[0]=strdup(""); + memset(Conf->AffixData, 0, naffix * sizeof(char *)); + naffix = 1; + Conf->AffixData[0] = strdup(""); MEMOUT(Conf->AffixData[0]); - Conf->AffixData[1]=strdup( Conf->Spell[0].p.flag ); + Conf->AffixData[1] = strdup(Conf->Spell[0].p.flag); MEMOUT(Conf->AffixData[1]); Conf->Spell[0].p.d.affix = 1; Conf->Spell[0].p.d.len = strlen(Conf->Spell[0].word); - for (i = 1; i < Conf->nspell; i++) { - if ( strcmp(Conf->Spell[i].p.flag, Conf->AffixData[naffix]) ) { + for (i = 1; i < Conf->nspell; i++) + { + if (strcmp(Conf->Spell[i].p.flag, Conf->AffixData[naffix])) + { naffix++; - Conf->AffixData[naffix] = strdup( Conf->Spell[i].p.flag ); + Conf->AffixData[naffix] = strdup(Conf->Spell[i].p.flag); MEMOUT(Conf->AffixData[naffix]); } Conf->Spell[i].p.d.affix = naffix; Conf->Spell[i].p.d.len = strlen(Conf->Spell[i].word); } - + qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL), cmpspell); Conf->Dictionary = mkSPNode(Conf, 0, Conf->nspell, 0); - - for (i = 0; i < Conf->nspell; i++) - free( Conf->Spell[i].word ); - free( Conf->Spell ); - Conf->Spell=NULL; + + for (i = 0; i < Conf->nspell; i++) + free(Conf->Spell[i].word); + free(Conf->Spell); + Conf->Spell = NULL; } -static AffixNode* -mkANode(IspellDict *Conf, int low, int high, int level, int type) { - int i; - int nchar=0; - uint8 lastchar='\0'; - AffixNode *rs; +static AffixNode * +mkANode(IspellDict * Conf, int low, int high, int level, int type) +{ + int i; + int nchar = 0; + uint8 lastchar = '\0'; + AffixNode *rs; AffixNodeData *data; - int lownew=low; + int lownew = low; - for(i=low; i<high; i++) - if ( Conf->Affix[i].replen>level && lastchar!=GETCHAR( Conf->Affix + i, level, type ) ) { + for (i = low; i < high; i++) + if (Conf->Affix[i].replen > level && lastchar != GETCHAR(Conf->Affix + i, level, type)) + { nchar++; - lastchar=GETCHAR( Conf->Affix + i, level, type ); + lastchar = GETCHAR(Conf->Affix + i, level, type); } if (!nchar) return NULL; - rs=(AffixNode*)malloc(ANHRDSZ+nchar*sizeof(AffixNodeData)); + rs = (AffixNode *) malloc(ANHRDSZ + nchar * sizeof(AffixNodeData)); MEMOUT(rs); - memset(rs,0,ANHRDSZ+nchar*sizeof(AffixNodeData)); + memset(rs, 0, ANHRDSZ + nchar * sizeof(AffixNodeData)); rs->length = nchar; - data=rs->data; - - lastchar='\0'; - for(i=low; i<high; i++) - if ( Conf->Affix[i].replen>level ) { - if ( lastchar!=GETCHAR( Conf->Affix + i, level, type ) ) { - if ( lastchar ) { - data->node = mkANode(Conf, lownew, i, level+1, type); - lownew=i; + data = rs->data; + + lastchar = '\0'; + for (i = low; i < high; i++) + if (Conf->Affix[i].replen > level) + { + if (lastchar != GETCHAR(Conf->Affix + i, level, type)) + { + if (lastchar) + { + data->node = mkANode(Conf, lownew, i, level + 1, type); + lownew = i; data++; } - lastchar=GETCHAR( Conf->Affix + i, level, type ); + lastchar = GETCHAR(Conf->Affix + i, level, type); } - data->val=GETCHAR( Conf->Affix + i, level, type ); - if ( Conf->Affix[i].replen == level+1 ) { /* affix stopped */ - if ( !data->naff ) { - data->aff=(AFFIX**)malloc(sizeof(AFFIX*)*(high-i+1)); + data->val = GETCHAR(Conf->Affix + i, level, type); + if (Conf->Affix[i].replen == level + 1) + { /* affix stopped */ + if (!data->naff) + { + data->aff = (AFFIX **) malloc(sizeof(AFFIX *) * (high - i + 1)); MEMOUT(data->aff); } - data->aff[ data->naff ] = Conf->Affix + i; + data->aff[data->naff] = Conf->Affix + i; data->naff++; } } - - data->node = mkANode(Conf, lownew, high, level+1, type); + + data->node = mkANode(Conf, lownew, high, level + 1, type); return rs; } static void -mkVoidAffix(IspellDict * Conf, int issuffix, int startsuffix) { - int i,cnt=0; - int start = (issuffix) ? startsuffix : 0; - int end = (issuffix) ? Conf->naffixes : startsuffix; - AffixNode *Affix = (AffixNode*)malloc( ANHRDSZ + sizeof(AffixNodeData)); +mkVoidAffix(IspellDict * Conf, int issuffix, int startsuffix) +{ + int i, + cnt = 0; + int start = (issuffix) ? startsuffix : 0; + int end = (issuffix) ? Conf->naffixes : startsuffix; + AffixNode *Affix = (AffixNode *) malloc(ANHRDSZ + sizeof(AffixNodeData)); MEMOUT(Affix); - memset(Affix, 0, ANHRDSZ + sizeof(AffixNodeData) ); - Affix->length=1; - Affix->isvoid=1; + memset(Affix, 0, ANHRDSZ + sizeof(AffixNodeData)); + Affix->length = 1; + Affix->isvoid = 1; - if (issuffix) { - Affix->data->node=Conf->Suffix; - Conf->Suffix = Affix; - } else { - Affix->data->node=Conf->Prefix; - Conf->Prefix = Affix; - } + if (issuffix) + { + Affix->data->node = Conf->Suffix; + Conf->Suffix = Affix; + } + else + { + Affix->data->node = Conf->Prefix; + Conf->Prefix = Affix; + } - for(i=start;i<end;i++) - if (Conf->Affix[i].replen==0) - cnt++; + for (i = start; i < end; i++) + if (Conf->Affix[i].replen == 0) + cnt++; - if ( cnt==0 ) - return; + if (cnt == 0) + return; - Affix->data->aff = (AFFIX**)malloc( sizeof(AFFIX*) * cnt ); + Affix->data->aff = (AFFIX **) malloc(sizeof(AFFIX *) * cnt); MEMOUT(Affix->data->aff); - Affix->data->naff = (uint32)cnt; - - cnt=0; - for(i=start;i<end;i++) - if (Conf->Affix[i].replen==0) { - Affix->data->aff[cnt] = Conf->Affix + i; - cnt++; - } + Affix->data->naff = (uint32) cnt; + + cnt = 0; + for (i = start; i < end; i++) + if (Conf->Affix[i].replen == 0) + { + Affix->data->aff[cnt] = Conf->Affix + i; + cnt++; + } } void @@ -608,120 +655,149 @@ NISortAffixes(IspellDict * Conf) { AFFIX *Affix; size_t i; - CMPDAffix* ptr; - int firstsuffix=-1; + CMPDAffix *ptr; + int firstsuffix = -1; if (Conf->naffixes > 1) qsort((void *) Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix); - Conf->CompoundAffix = ptr = (CMPDAffix*)malloc( sizeof(CMPDAffix) * Conf->naffixes ); + Conf->CompoundAffix = ptr = (CMPDAffix *) malloc(sizeof(CMPDAffix) * Conf->naffixes); MEMOUT(Conf->CompoundAffix); - ptr->affix=NULL; + ptr->affix = NULL; - for (i = 0; i < Conf->naffixes; i++) { + for (i = 0; i < Conf->naffixes; i++) + { Affix = &(((AFFIX *) Conf->Affix)[i]); - if ( Affix->type == FF_SUFFIX ) { - if ( firstsuffix<0 ) firstsuffix=i; - if ( Affix->flagflags & FF_COMPOUNDONLYAFX ) { - if ( !ptr->affix || strbncmp((ptr-1)->affix, Affix->repl, (ptr-1)->len) ) { + if (Affix->type == FF_SUFFIX) + { + if (firstsuffix < 0) + firstsuffix = i; + if (Affix->flagflags & FF_COMPOUNDONLYAFX) + { + if (!ptr->affix || strbncmp((ptr - 1)->affix, Affix->repl, (ptr - 1)->len)) + { /* leave only unique and minimals suffixes */ - ptr->affix=Affix->repl; - ptr->len=Affix->replen; + ptr->affix = Affix->repl; + ptr->len = Affix->replen; ptr++; } } } } ptr->affix = NULL; - Conf->CompoundAffix = (CMPDAffix*)realloc( Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr-Conf->CompoundAffix+1) ); + Conf->CompoundAffix = (CMPDAffix *) realloc(Conf->CompoundAffix, sizeof(CMPDAffix) * (ptr - Conf->CompoundAffix + 1)); - Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX); + Conf->Prefix = mkANode(Conf, 0, firstsuffix, 0, FF_PREFIX); Conf->Suffix = mkANode(Conf, firstsuffix, Conf->naffixes, 0, FF_SUFFIX); - mkVoidAffix(Conf, 1, firstsuffix); - mkVoidAffix(Conf, 0, firstsuffix); + mkVoidAffix(Conf, 1, firstsuffix); + mkVoidAffix(Conf, 0, firstsuffix); } -static AffixNodeData* -FinfAffixes(AffixNode *node, const char *word, int wrdlen, int *level, int type) { - AffixNodeData *StopLow, *StopHigh, *StopMiddle; - uint8 symbol; - - if ( node->isvoid ) { /* search void affixes */ - if (node->data->naff) - return node->data; - node = node->data->node; - } +static AffixNodeData * +FinfAffixes(AffixNode * node, const char *word, int wrdlen, int *level, int type) +{ + AffixNodeData *StopLow, + *StopHigh, + *StopMiddle; + uint8 symbol; + + if (node->isvoid) + { /* search void affixes */ + if (node->data->naff) + return node->data; + node = node->data->node; + } - while( node && *level<wrdlen) { + while (node && *level < wrdlen) + { StopLow = node->data; - StopHigh = node->data+node->length; - while (StopLow < StopHigh) { + StopHigh = node->data + node->length; + while (StopLow < StopHigh) + { StopMiddle = StopLow + ((StopHigh - StopLow) >> 1); - symbol = GETWCHAR(word,wrdlen,*level,type); - if ( StopMiddle->val == symbol ) { + symbol = GETWCHAR(word, wrdlen, *level, type); + if (StopMiddle->val == symbol) + { (*level)++; - if ( StopMiddle->naff ) + if (StopMiddle->naff) return StopMiddle; - node=StopMiddle->node; + node = StopMiddle->node; break; - } else if ( StopMiddle->val < symbol ) { + } + else if (StopMiddle->val < symbol) StopLow = StopMiddle + 1; - } else { + else StopHigh = StopMiddle; - } } - if ( StopLow >= StopHigh ) - break; + if (StopLow >= StopHigh) + break; } return NULL; } static char * -CheckAffix(const char *word, size_t len, AFFIX * Affix, char flagflags, char *newword) { +CheckAffix(const char *word, size_t len, AFFIX * Affix, char flagflags, char *newword) +{ - if ( flagflags & FF_COMPOUNDONLYAFX ) { - if ( (Affix->flagflags & FF_COMPOUNDONLYAFX) == 0 ) + if (flagflags & FF_COMPOUNDONLYAFX) + { + if ((Affix->flagflags & FF_COMPOUNDONLYAFX) == 0) return NULL; - } else { - if ( Affix->flagflags & FF_COMPOUNDONLYAFX ) + } + else + { + if (Affix->flagflags & FF_COMPOUNDONLYAFX) return NULL; - } + } - if ( Affix->type==FF_SUFFIX ) { + if (Affix->type == FF_SUFFIX) + { strcpy(newword, word); strcpy(newword + len - Affix->replen, Affix->find); - } else { + } + else + { strcpy(newword, Affix->find); strcat(newword, word + Affix->replen); } - if ( Affix->issimple ) { - return newword; - } else if ( Affix->isregis ) { - if (Affix->compile) { - RS_compile(&(Affix->reg.regis), (Affix->type==FF_SUFFIX) ? 1 : 0, Affix->mask); - Affix->compile = 0; - } - if ( RS_execute(&(Affix->reg.regis), newword, -1) ) - return newword; - } else { - regmatch_t subs[2]; /* workaround for apache&linux */ + if (Affix->issimple) + return newword; + else if (Affix->isregis) + { + if (Affix->compile) + { + RS_compile(&(Affix->reg.regis), (Affix->type == FF_SUFFIX) ? 1 : 0, Affix->mask); + Affix->compile = 0; + } + if (RS_execute(&(Affix->reg.regis), newword, -1)) + return newword; + } + else + { + regmatch_t subs[2]; /* workaround for apache&linux */ int err; pg_wchar *data; size_t data_len; - int dat_len; + int dat_len; + if (Affix->compile) { - int wmasklen,masklen = strlen(Affix->mask); - pg_wchar *mask; + int wmasklen, + masklen = strlen(Affix->mask); + pg_wchar *mask; + mask = (pg_wchar *) palloc((masklen + 1) * sizeof(pg_wchar)); - wmasklen = pg_mb2wchar_with_len( Affix->mask, mask, masklen); - + wmasklen = pg_mb2wchar_with_len(Affix->mask, mask, masklen); + err = pg_regcomp(&(Affix->reg.regex), mask, wmasklen, REG_EXTENDED | REG_ICASE | REG_NOSUB); pfree(mask); if (err) { - /* regerror(err, &(Affix->reg.regex), regerrstr, ERRSTRSIZE); */ + /* + * regerror(err, &(Affix->reg.regex), regerrstr, + * ERRSTRSIZE); + */ pg_regfree(&(Affix->reg.regex)); return (NULL); } @@ -733,9 +809,10 @@ CheckAffix(const char *word, size_t len, AFFIX * Affix, char flagflags, char *ne data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar)); data_len = pg_mb2wchar_with_len(newword, data, dat_len); - if (!(err = pg_regexec(&(Affix->reg.regex), data,dat_len,NULL, 1, subs, 0))) { - pfree(data); - return newword; + if (!(err = pg_regexec(&(Affix->reg.regex), data, dat_len, NULL, 1, subs, 0))) + { + pfree(data); + return newword; } pfree(data); } @@ -744,111 +821,143 @@ CheckAffix(const char *word, size_t len, AFFIX * Affix, char flagflags, char *ne } -static char ** -NormalizeSubWord(IspellDict * Conf, char *word, char flag) { - AffixNodeData *suffix=NULL, *prefix=NULL; - int slevel=0, plevel=0; - int wrdlen = strlen(word), swrdlen; +static char ** +NormalizeSubWord(IspellDict * Conf, char *word, char flag) +{ + AffixNodeData *suffix = NULL, + *prefix = NULL; + int slevel = 0, + plevel = 0; + int wrdlen = strlen(word), + swrdlen; char **forms; char **cur; char newword[2 * MAXNORMLEN] = ""; char pnewword[2 * MAXNORMLEN] = ""; - AffixNode *snode = Conf->Suffix, *pnode; - int i,j; + AffixNode *snode = Conf->Suffix, + *pnode; + int i, + j; - if (wrdlen > MAXNORMLEN) return NULL; - strlower(word); + if (wrdlen > MAXNORMLEN) + return NULL; + strlower(word); cur = forms = (char **) palloc(MAX_NORM * sizeof(char *)); *cur = NULL; /* Check that the word itself is normal form */ - if (FindWord(Conf, word, 0, flag & FF_COMPOUNDWORD)) { + if (FindWord(Conf, word, 0, flag & FF_COMPOUNDWORD)) + { *cur = pstrdup(word); cur++; *cur = NULL; } - /* Find all other NORMAL forms of the 'word' (check only prefix)*/ - pnode=Conf->Prefix; - plevel=0; - while(pnode) { - prefix=FinfAffixes(pnode, word, wrdlen, &plevel,FF_PREFIX); - if (!prefix) break; - for(j=0;j<prefix->naff;j++) { - if ( CheckAffix(word,wrdlen,prefix->aff[j], flag, newword) ) { + /* Find all other NORMAL forms of the 'word' (check only prefix) */ + pnode = Conf->Prefix; + plevel = 0; + while (pnode) + { + prefix = FinfAffixes(pnode, word, wrdlen, &plevel, FF_PREFIX); + if (!prefix) + break; + for (j = 0; j < prefix->naff; j++) + { + if (CheckAffix(word, wrdlen, prefix->aff[j], flag, newword)) + { /* prefix success */ - if ( FindWord(Conf, newword, prefix->aff[j]->flag, flag&FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM-1) ) { + if (FindWord(Conf, newword, prefix->aff[j]->flag, flag & FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM - 1)) + { /* word search success */ *cur = pstrdup(newword); cur++; - *cur=NULL; + *cur = NULL; } } } pnode = prefix->node; } - - /* Find all other NORMAL forms of the 'word' (check suffix and then prefix)*/ - while( snode ) { + + /* + * Find all other NORMAL forms of the 'word' (check suffix and then + * prefix) + */ + while (snode) + { /* find possible suffix */ suffix = FinfAffixes(snode, word, wrdlen, &slevel, FF_SUFFIX); - if (!suffix) break; + if (!suffix) + break; /* foreach suffix check affix */ - for(i=0;i<suffix->naff;i++) { - if ( CheckAffix(word, wrdlen, suffix->aff[i], flag, newword) ) { + for (i = 0; i < suffix->naff; i++) + { + if (CheckAffix(word, wrdlen, suffix->aff[i], flag, newword)) + { /* suffix success */ - if ( FindWord(Conf, newword, suffix->aff[i]->flag, flag&FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM-1) ) { + if (FindWord(Conf, newword, suffix->aff[i]->flag, flag & FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM - 1)) + { /* word search success */ *cur = pstrdup(newword); cur++; - *cur=NULL; + *cur = NULL; } /* now we will look changed word with prefixes */ - pnode=Conf->Prefix; - plevel=0; - swrdlen=strlen(newword); - while(pnode) { - prefix=FinfAffixes(pnode, newword, swrdlen, &plevel,FF_PREFIX); - if (!prefix) break; - for(j=0;j<prefix->naff;j++) { - if ( CheckAffix(newword,swrdlen,prefix->aff[j], flag, pnewword) ) { + pnode = Conf->Prefix; + plevel = 0; + swrdlen = strlen(newword); + while (pnode) + { + prefix = FinfAffixes(pnode, newword, swrdlen, &plevel, FF_PREFIX); + if (!prefix) + break; + for (j = 0; j < prefix->naff; j++) + { + if (CheckAffix(newword, swrdlen, prefix->aff[j], flag, pnewword)) + { /* prefix success */ - int ff=( prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT ) ? - 0 : prefix->aff[j]->flag; - if ( FindWord(Conf, pnewword, ff, flag&FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM-1) ) { + int ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ? + 0 : prefix->aff[j]->flag; + + if (FindWord(Conf, pnewword, ff, flag & FF_COMPOUNDWORD) && (cur - forms) < (MAX_NORM - 1)) + { /* word search success */ *cur = pstrdup(pnewword); cur++; - *cur=NULL; + *cur = NULL; } } } pnode = prefix->node; - } + } } } - snode=suffix->node; + snode = suffix->node; } - if (cur == forms) { + if (cur == forms) + { pfree(forms); return (NULL); } return (forms); } -typedef struct SplitVar { - int nstem; - char **stem; - struct SplitVar *next; -} SplitVar; +typedef struct SplitVar +{ + int nstem; + char **stem; + struct SplitVar *next; +} SplitVar; -static int -CheckCompoundAffixes(CMPDAffix **ptr, char *word, int len) { - while( (*ptr)->affix ) { - if ( len > (*ptr)->len && strncmp((*ptr)->affix, word, (*ptr)->len)==0 ) { +static int +CheckCompoundAffixes(CMPDAffix ** ptr, char *word, int len) +{ + while ((*ptr)->affix) + { + if (len > (*ptr)->len && strncmp((*ptr)->affix, word, (*ptr)->len) == 0) + { len = (*ptr)->len; (*ptr)++; return len; @@ -858,245 +967,290 @@ CheckCompoundAffixes(CMPDAffix **ptr, char *word, int len) { return 0; } -static SplitVar* -CopyVar(SplitVar *s, int makedup) { - SplitVar *v = (SplitVar*)palloc(sizeof(SplitVar)); +static SplitVar * +CopyVar(SplitVar * s, int makedup) +{ + SplitVar *v = (SplitVar *) palloc(sizeof(SplitVar)); + + v->stem = (char **) palloc(sizeof(char *) * (MAX_NORM)); + v->next = NULL; + if (s) + { + int i; - v->stem=(char**)palloc( sizeof(char*) * (MAX_NORM) ); - v->next=NULL; - if ( s ) { - int i; v->nstem = s->nstem; - for(i=0;i<s->nstem;i++) - v->stem[i] = (makedup) ? pstrdup( s->stem[i] ) : s->stem[i]; - } else { - v->nstem=0; + for (i = 0; i < s->nstem; i++) + v->stem[i] = (makedup) ? pstrdup(s->stem[i]) : s->stem[i]; } + else + v->nstem = 0; return v; } -static SplitVar* -SplitToVariants( IspellDict * Conf, SPNode *snode, SplitVar * orig, char *word, int wordlen, int startpos, int minpos ) { - SplitVar *var=NULL; - SPNodeData *StopLow, *StopHigh, *StopMiddle = NULL; - SPNode *node = (snode) ? snode : Conf->Dictionary; - int level=(snode) ? minpos : startpos; /* recursive minpos==level*/ - int lenaff; - CMPDAffix *caff; - char *notprobed; +static SplitVar * +SplitToVariants(IspellDict * Conf, SPNode * snode, SplitVar * orig, char *word, int wordlen, int startpos, int minpos) +{ + SplitVar *var = NULL; + SPNodeData *StopLow, + *StopHigh, + *StopMiddle = NULL; + SPNode *node = (snode) ? snode : Conf->Dictionary; + int level = (snode) ? minpos : startpos; /* recursive + * minpos==level */ + int lenaff; + CMPDAffix *caff; + char *notprobed; notprobed = (char *) palloc(wordlen); - memset(notprobed,1,wordlen); - var = CopyVar(orig,1); + memset(notprobed, 1, wordlen); + var = CopyVar(orig, 1); - while( node && level<wordlen) { + while (node && level < wordlen) + { StopLow = node->data; - StopHigh = node->data+node->length; - while (StopLow < StopHigh) { + StopHigh = node->data + node->length; + while (StopLow < StopHigh) + { StopMiddle = StopLow + ((StopHigh - StopLow) >> 1); - if ( StopMiddle->val == ((uint8*)(word))[level] ) { + if (StopMiddle->val == ((uint8 *) (word))[level]) break; - } else if ( StopMiddle->val < ((uint8*)(word))[level] ) { + else if (StopMiddle->val < ((uint8 *) (word))[level]) StopLow = StopMiddle + 1; - } else { + else StopHigh = StopMiddle; - } } - if ( StopLow >= StopHigh ) + if (StopLow >= StopHigh) break; /* find word with epenthetic */ caff = Conf->CompoundAffix; - while ( level>startpos && (lenaff=CheckCompoundAffixes( &caff, word + level, wordlen - level ))>0 ) { - /* there is one of compound suffixes, so check word for existings */ - char buf[MAXNORMLEN]; - char **subres; - - lenaff=level-startpos+lenaff; - - if ( !notprobed[startpos+lenaff-1] ) + while (level > startpos && (lenaff = CheckCompoundAffixes(&caff, word + level, wordlen - level)) > 0) + { + /* + * there is one of compound suffixes, so check word for + * existings + */ + char buf[MAXNORMLEN]; + char **subres; + + lenaff = level - startpos + lenaff; + + if (!notprobed[startpos + lenaff - 1]) continue; - - if ( level+lenaff-1 <= minpos ) + + if (level + lenaff - 1 <= minpos) continue; - memcpy(buf, word+startpos, lenaff); - buf[lenaff]='\0'; + memcpy(buf, word + startpos, lenaff); + buf[lenaff] = '\0'; subres = NormalizeSubWord(Conf, buf, FF_COMPOUNDWORD | FF_COMPOUNDONLYAFX); - if ( subres ) { + if (subres) + { /* Yes, it was a word from dictionary */ - SplitVar *new=CopyVar(var,0); - SplitVar *ptr=var; - char **sptr=subres; - - notprobed[startpos+lenaff-1]=0; - - while(*sptr) { - new->stem[ new->nstem ] = *sptr; + SplitVar *new = CopyVar(var, 0); + SplitVar *ptr = var; + char **sptr = subres; + + notprobed[startpos + lenaff - 1] = 0; + + while (*sptr) + { + new->stem[new->nstem] = *sptr; new->nstem++; sptr++; } pfree(subres); - while( ptr->next ) + while (ptr->next) ptr = ptr->next; - ptr->next = SplitToVariants(Conf, NULL, new, word, wordlen, startpos+lenaff, startpos+lenaff); - + ptr->next = SplitToVariants(Conf, NULL, new, word, wordlen, startpos + lenaff, startpos + lenaff); + pfree(new->stem); pfree(new); } } /* find infinitive */ - if ( StopMiddle->isword && StopMiddle->compoundallow && notprobed[level] ) { - /* ok, we found full compoundallowed word*/ - if ( level>minpos ) { + if (StopMiddle->isword && StopMiddle->compoundallow && notprobed[level]) + { + /* ok, we found full compoundallowed word */ + if (level > minpos) + { /* and its length more than minimal */ - if ( wordlen==level+1 ) { + if (wordlen == level + 1) + { /* well, it was last word */ - var->stem[ var->nstem ] = strnduplicate(word + startpos, wordlen - startpos); + var->stem[var->nstem] = strnduplicate(word + startpos, wordlen - startpos); var->nstem++; pfree(notprobed); return var; - } else { + } + else + { /* then we will search more big word at the same point */ - SplitVar *ptr=var; - while( ptr->next ) + SplitVar *ptr = var; + + while (ptr->next) ptr = ptr->next; - ptr->next=SplitToVariants(Conf, node, var, word, wordlen, startpos, level); + ptr->next = SplitToVariants(Conf, node, var, word, wordlen, startpos, level); /* we can find next word */ level++; - var->stem[ var->nstem ] = strnduplicate(word + startpos, level - startpos); + var->stem[var->nstem] = strnduplicate(word + startpos, level - startpos); var->nstem++; node = Conf->Dictionary; - startpos=level; + startpos = level; continue; } } } level++; - node=StopMiddle->node; + node = StopMiddle->node; } - var->stem[ var->nstem ] = strnduplicate(word + startpos, wordlen - startpos); + var->stem[var->nstem] = strnduplicate(word + startpos, wordlen - startpos); var->nstem++; pfree(notprobed); return var; -} - -char ** -NINormalizeWord(IspellDict * Conf, char *word) { - char **res= NormalizeSubWord(Conf, word, 0); - - if ( Conf->compoundcontrol != '\t' ) { - int wordlen=strlen(word); - SplitVar *ptr, *var = SplitToVariants(Conf,NULL,NULL, word, wordlen, 0, -1); - char **cur=res; - int i; - - while(var) { - if ( var->nstem > 1 ) { - char **subres = NormalizeSubWord(Conf, var->stem[ var->nstem-1 ], FF_COMPOUNDWORD); - if ( subres ) { - char **ptr=subres; - - if ( cur ) { - while(*cur) +} + +char ** +NINormalizeWord(IspellDict * Conf, char *word) +{ + char **res = NormalizeSubWord(Conf, word, 0); + + if (Conf->compoundcontrol != '\t') + { + int wordlen = strlen(word); + SplitVar *ptr, + *var = SplitToVariants(Conf, NULL, NULL, word, wordlen, 0, -1); + char **cur = res; + int i; + + while (var) + { + if (var->nstem > 1) + { + char **subres = NormalizeSubWord(Conf, var->stem[var->nstem - 1], FF_COMPOUNDWORD); + + if (subres) + { + char **ptr = subres; + + if (cur) + { + while (*cur) cur++; - } else { - res=cur=(char **) palloc(MAX_NORM * sizeof(char *)); } - - for(i=0;i<var->nstem-1;i++) { - *cur=var->stem[ i ]; + else + res = cur = (char **) palloc(MAX_NORM * sizeof(char *)); + + for (i = 0; i < var->nstem - 1; i++) + { + *cur = var->stem[i]; cur++; } - while(*ptr) { - *cur=*ptr; - cur++; ptr++; + while (*ptr) + { + *cur = *ptr; + cur++; + ptr++; } - *cur=NULL; + *cur = NULL; pfree(subres); - var->stem[ 0 ] = NULL; + var->stem[0] = NULL; } } - - for(i=0;i<var->nstem && var->stem[ i ];i++) - pfree( var->stem[i] ); + + for (i = 0; i < var->nstem && var->stem[i]; i++) + pfree(var->stem[i]); ptr = var->next; pfree(var->stem); - pfree(var); - var=ptr; + pfree(var); + var = ptr; } } return res; } -static void freeSPNode(SPNode *node) { +static void +freeSPNode(SPNode * node) +{ SPNodeData *data; - if (!node) return; - data=node->data; - while( node->length ) { + if (!node) + return; + data = node->data; + while (node->length) + { freeSPNode(data->node); data++; node->length--; } free(node); } - -static void freeANode(AffixNode *node) { + +static void +freeANode(AffixNode * node) +{ AffixNodeData *data; - if (!node) return; - data=node->data; - while( node->length ) { + if (!node) + return; + data = node->data; + while (node->length) + { freeANode(data->node); if (data->naff) - free(data->aff); + free(data->aff); data++; node->length--; } free(node); } - + void NIFree(IspellDict * Conf) { int i; AFFIX *Affix = (AFFIX *) Conf->Affix; - char** aff = Conf->AffixData; + char **aff = Conf->AffixData; - if ( aff ) { - while(*aff) { + if (aff) + { + while (*aff) + { free(*aff); aff++; } free(Conf->AffixData); } - + for (i = 0; i < Conf->naffixes; i++) { - if (Affix[i].compile == 0) { - if ( Affix[i].isregis ) - RS_free(&(Affix[i].reg.regis)); - else + if (Affix[i].compile == 0) + { + if (Affix[i].isregis) + RS_free(&(Affix[i].reg.regis)); + else pg_regfree(&(Affix[i].reg.regex)); } } - if (Conf->Spell) { + if (Conf->Spell) + { for (i = 0; i < Conf->nspell; i++) free(Conf->Spell[i].word); free(Conf->Spell); } - if (Conf->Affix) free(Conf->Affix); - if ( Conf->CompoundAffix ) free(Conf->CompoundAffix); + if (Conf->Affix) + free(Conf->Affix); + if (Conf->CompoundAffix) + free(Conf->CompoundAffix); freeSPNode(Conf->Dictionary); freeANode(Conf->Suffix); freeANode(Conf->Prefix); diff --git a/contrib/tsearch2/ispell/spell.h b/contrib/tsearch2/ispell/spell.h index 1e22a0d1bcd..44f1e7be08f 100644 --- a/contrib/tsearch2/ispell/spell.h +++ b/contrib/tsearch2/ispell/spell.h @@ -10,19 +10,21 @@ struct SPNode; -typedef struct { - uint32 - val:8, - isword:1, - compoundallow:1, - affix:22; - struct SPNode *node; -} SPNodeData; - -typedef struct SPNode { - uint32 length; - SPNodeData data[1]; -} SPNode; +typedef struct +{ + uint32 + val:8, + isword:1, + compoundallow:1, + affix:22; + struct SPNode *node; +} SPNodeData; + +typedef struct SPNode +{ + uint32 length; + SPNodeData data[1]; +} SPNode; #define SPNHRDSZ (sizeof(uint32)) @@ -30,81 +32,87 @@ typedef struct SPNode { typedef struct spell_struct { char *word; - union { + union + { char flag[16]; - struct { - int affix; - int len; - } d; - } p; + struct + { + int affix; + int len; + } d; + } p; } SPELL; typedef struct aff_struct { - uint32 - flag:8, - type:2, - compile:1, - flagflags:3, - issimple:1, - isregis:1, - unused:1, - replen:16; - char mask[32]; - char find[16]; - char repl[16]; - union { - regex_t regex; - Regis regis; - } reg; + uint32 + flag:8, + type:2, + compile:1, + flagflags:3, + issimple:1, + isregis:1, + unused:1, + replen:16; + char mask[32]; + char find[16]; + char repl[16]; + union + { + regex_t regex; + Regis regis; + } reg; } AFFIX; -#define FF_CROSSPRODUCT 0x01 -#define FF_COMPOUNDWORD 0x02 -#define FF_COMPOUNDONLYAFX 0x04 -#define FF_SUFFIX 2 -#define FF_PREFIX 1 +#define FF_CROSSPRODUCT 0x01 +#define FF_COMPOUNDWORD 0x02 +#define FF_COMPOUNDONLYAFX 0x04 +#define FF_SUFFIX 2 +#define FF_PREFIX 1 struct AffixNode; -typedef struct { +typedef struct +{ uint32 - val:8, - naff:24; - AFFIX **aff; + val:8, + naff:24; + AFFIX **aff; struct AffixNode *node; -} AffixNodeData; +} AffixNodeData; -typedef struct AffixNode { - uint32 isvoid:1, - length:31; - AffixNodeData data[1]; -} AffixNode; +typedef struct AffixNode +{ + uint32 isvoid:1, + length:31; + AffixNodeData data[1]; +} AffixNode; -#define ANHRDSZ (sizeof(uint32)) +#define ANHRDSZ (sizeof(uint32)) -typedef struct { - char *affix; - int len; -} CMPDAffix; +typedef struct +{ + char *affix; + int len; +} CMPDAffix; typedef struct { int maffixes; int naffixes; AFFIX *Affix; - char compoundcontrol; + char compoundcontrol; int nspell; int mspell; SPELL *Spell; - AffixNode *Suffix; - AffixNode *Prefix; + AffixNode *Suffix; + AffixNode *Prefix; - SPNode *Dictionary; - char **AffixData; - CMPDAffix *CompoundAffix; + SPNode *Dictionary; + char **AffixData; + CMPDAffix *CompoundAffix; } IspellDict; diff --git a/contrib/tsearch2/query.c b/contrib/tsearch2/query.c index 81343b0c462..eb931030ca7 100644 --- a/contrib/tsearch2/query.c +++ b/contrib/tsearch2/query.c @@ -469,7 +469,7 @@ TS_execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void * Datum rexectsq(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); return DirectFunctionCall2( exectsq, PG_GETARG_DATUM(1), @@ -484,7 +484,8 @@ exectsq(PG_FUNCTION_ARGS) QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1))); CHKVAL chkval; bool result; - SET_FUNCOID(); + + SET_FUNCOID(); if (!val->size || !query->size) { PG_FREE_IF_COPY(val, 0); @@ -639,7 +640,7 @@ static QUERYTYPE * Datum tsquery_in(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); PG_RETURN_POINTER(queryin((char *) PG_GETARG_POINTER(0), pushval_asis, 0)); } @@ -865,7 +866,8 @@ to_tsquery(PG_FUNCTION_ARGS) QUERYTYPE *query; ITEM *res; int4 len; - SET_FUNCOID(); + + SET_FUNCOID(); str = text2char(in); PG_FREE_IF_COPY(in, 1); @@ -888,10 +890,11 @@ to_tsquery_name(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_P(0); Datum res; - SET_FUNCOID(); + + SET_FUNCOID(); res = DirectFunctionCall2(to_tsquery, - Int32GetDatum(name2id_cfg(name)), - PG_GETARG_DATUM(1)); + Int32GetDatum(name2id_cfg(name)), + PG_GETARG_DATUM(1)); PG_FREE_IF_COPY(name, 0); PG_RETURN_DATUM(res); diff --git a/contrib/tsearch2/snmap.c b/contrib/tsearch2/snmap.c index d41fb45b0dc..c152b1ac238 100644 --- a/contrib/tsearch2/snmap.c +++ b/contrib/tsearch2/snmap.c @@ -13,11 +13,11 @@ static int compareSNMapEntry(const void *a, const void *b) { - if ( ((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp ) + if (((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp) return -1; - else if ( ((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp ) + else if (((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp) return 1; - else + else return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key); } diff --git a/contrib/tsearch2/ts_cfg.c b/contrib/tsearch2/ts_cfg.c index 4e0a0bb9043..afebb113199 100644 --- a/contrib/tsearch2/ts_cfg.c +++ b/contrib/tsearch2/ts_cfg.c @@ -38,10 +38,10 @@ init_cfg(Oid id, TSCfgInfo * cfg) j; text *ptr; text *prsname = NULL; - char *nsp=get_namespace(TSNSP_FunctionOid); - char buf[1024]; + char *nsp = get_namespace(TSNSP_FunctionOid); + char buf[1024]; MemoryContext oldcontext; - void *plan; + void *plan; arg[0] = OIDOID; arg[1] = OIDOID; @@ -52,7 +52,7 @@ init_cfg(Oid id, TSCfgInfo * cfg) SPI_connect(); sprintf(buf, "select prs_name from %s.pg_ts_cfg where oid = $1", nsp); - plan= SPI_prepare(buf, 1, arg); + plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -77,7 +77,7 @@ init_cfg(Oid id, TSCfgInfo * cfg) arg[0] = TEXTOID; sprintf(buf, "select lt.tokid, map.dict_name from %s.pg_ts_cfgmap as map, %s.pg_ts_cfg as cfg, %s.token_type( $1 ) as lt where lt.alias = map.tok_alias and map.ts_name = cfg.ts_name and cfg.oid= $2 order by lt.tokid desc;", nsp, nsp, nsp); - plan= SPI_prepare(buf, 2, arg); + plan = SPI_prepare(buf, 2, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -118,7 +118,7 @@ init_cfg(Oid id, TSCfgInfo * cfg) cfg->map[lexid].len = ARRNELEMS(a); cfg->map[lexid].dict_id = (Datum *) malloc(sizeof(Datum) * cfg->map[lexid].len); if (!cfg->map[lexid].dict_id) - ts_error(ERROR, "No memory"); + ts_error(ERROR, "No memory"); memset(cfg->map[lexid].dict_id, 0, sizeof(Datum) * cfg->map[lexid].len); ptr = (text *) ARR_DATA_PTR(a); @@ -235,9 +235,9 @@ name2id_cfg(text *name) Datum pars[1]; int stat; Oid id = findSNMap_t(&(CList.name2id_map), name); - void *plan; - char *nsp; - char buf[1024]; + void *plan; + char *nsp; + char buf[1024]; arg[0] = TEXTOID; pars[0] = PointerGetDatum(name); @@ -245,10 +245,10 @@ name2id_cfg(text *name) if (id) return id; - nsp=get_namespace(TSNSP_FunctionOid); + nsp = get_namespace(TSNSP_FunctionOid); SPI_connect(); - sprintf(buf, "select oid from %s.pg_ts_cfg where ts_name = $1", nsp); - plan= SPI_prepare(buf, 1, arg); + sprintf(buf, "select oid from %s.pg_ts_cfg where ts_name = $1", nsp); + plan = SPI_prepare(buf, 1, arg); if (!plan) /* internal error */ elog(ERROR, "SPI_prepare() failed"); @@ -301,13 +301,14 @@ parsetext_v2(TSCfgInfo * cfg, PRSTEXT * prs, char *buf, int4 buflen) PointerGetDatum(&lenlemm)))) != 0) { - if (lenlemm >= MAXSTRLEN) { + if (lenlemm >= MAXSTRLEN) + { #ifdef IGNORE_LONGLEXEME ereport(NOTICE, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); continue; -#else +#else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); @@ -435,13 +436,14 @@ hlparsetext(TSCfgInfo * cfg, HLPRSTEXT * prs, QUERYTYPE * query, char *buf, int4 PointerGetDatum(&lenlemm)))) != 0) { - if (lenlemm >= MAXSTRLEN) { + if (lenlemm >= MAXSTRLEN) + { #ifdef IGNORE_LONGLEXEME ereport(NOTICE, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); continue; -#else +#else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); @@ -532,9 +534,8 @@ genhl(HLPRSTEXT * prs) ptr += prs->stopsellen; } } - } else - - if (!wrd->repeated) + } + else if (!wrd->repeated) pfree(wrd->word); wrd++; @@ -552,16 +553,16 @@ get_currcfg(void) Datum pars[1]; bool isnull; int stat; - char buf[1024]; - char *nsp; - void *plan; + char buf[1024]; + char *nsp; + void *plan; if (current_cfg_id > 0) return current_cfg_id; - nsp=get_namespace(TSNSP_FunctionOid); + nsp = get_namespace(TSNSP_FunctionOid); SPI_connect(); - sprintf(buf, "select oid from %s.pg_ts_cfg where locale = $1 ", nsp); + sprintf(buf, "select oid from %s.pg_ts_cfg where locale = $1 ", nsp); pfree(nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) @@ -593,7 +594,7 @@ Datum set_curcfg(PG_FUNCTION_ARGS); Datum set_curcfg(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); findcfg(PG_GETARG_OID(0)); current_cfg_id = PG_GETARG_OID(0); PG_RETURN_VOID(); @@ -605,7 +606,8 @@ Datum set_curcfg_byname(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_P(0); - SET_FUNCOID(); + + SET_FUNCOID(); DirectFunctionCall1( set_curcfg, ObjectIdGetDatum(name2id_cfg(name)) @@ -619,7 +621,7 @@ Datum show_curcfg(PG_FUNCTION_ARGS); Datum show_curcfg(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); PG_RETURN_OID(get_currcfg()); } @@ -628,8 +630,7 @@ Datum reset_tsearch(PG_FUNCTION_ARGS); Datum reset_tsearch(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); ts_error(NOTICE, "TSearch cache cleaned"); PG_RETURN_VOID(); } - diff --git a/contrib/tsearch2/ts_stat.c b/contrib/tsearch2/ts_stat.c index 7995406f43f..2e6a98197e3 100644 --- a/contrib/tsearch2/ts_stat.c +++ b/contrib/tsearch2/ts_stat.c @@ -15,7 +15,7 @@ Datum tsstat_in(PG_FUNCTION_ARGS) { tsstat *stat = palloc(STATHDRSIZE); - + stat->len = STATHDRSIZE; stat->size = 0; stat->weight = 0; @@ -34,12 +34,14 @@ tsstat_out(PG_FUNCTION_ARGS) } static int -check_weight(tsvector *txt, WordEntry *wptr, int8 weight) { - int len = POSDATALEN(txt, wptr); - int num=0; - WordEntryPos *ptr = POSDATAPTR(txt, wptr); +check_weight(tsvector * txt, WordEntry * wptr, int8 weight) +{ + int len = POSDATALEN(txt, wptr); + int num = 0; + WordEntryPos *ptr = POSDATAPTR(txt, wptr); - while (len--) { + while (len--) + { if (weight & (1 << ptr->weight)) num++; ptr++; @@ -123,9 +125,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len) } nptr = STATPTR(newstat) + (StopLow - STATPTR(stat)); memcpy(STATPTR(newstat), STATPTR(stat), sizeof(StatEntry) * (StopLow - STATPTR(stat))); - if ( (*ptr)->haspos ) { - nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); - } else + if ((*ptr)->haspos) + nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); + else nptr->nentry = 1; nptr->ndoc = 1; nptr->len = (*ptr)->len; @@ -144,9 +146,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len) } else { - if ( (*ptr)->haspos ) { - nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); - } else + if ((*ptr)->haspos) + nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); + else nptr->nentry = 1; nptr->ndoc = 1; nptr->len = (*ptr)->len; @@ -162,9 +164,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len) while (ptr - entry < len) { - if ( (*ptr)->haspos ) { - nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); - } else + if ((*ptr)->haspos) + nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr); + else nptr->nentry = 1; nptr->ndoc = 1; nptr->len = (*ptr)->len; @@ -192,7 +194,7 @@ ts_accum(PG_FUNCTION_ARGS) cur = 0; StatEntry *sptr; WordEntry *wptr; - int n=0; + int n = 0; if (stat == NULL || PG_ARGISNULL(0)) { /* Init in first */ @@ -222,10 +224,13 @@ ts_accum(PG_FUNCTION_ARGS) sptr++; else if (cmp == 0) { - if ( stat->weight == 0 ) { + if (stat->weight == 0) + { sptr->ndoc++; sptr->nentry += (wptr->haspos) ? POSDATALEN(txt, wptr) : 1; - } else if ( wptr->haspos && (n=check_weight(txt, wptr, stat->weight))!=0 ) { + } + else if (wptr->haspos && (n = check_weight(txt, wptr, stat->weight)) != 0) + { sptr->ndoc++; sptr->nentry += n; } @@ -234,7 +239,8 @@ ts_accum(PG_FUNCTION_ARGS) } else { - if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) { + if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0) + { if (cur == len) newentry = SEI_realloc(newentry, &len); newentry[cur] = wptr; @@ -246,7 +252,8 @@ ts_accum(PG_FUNCTION_ARGS) while (wptr - ARRPTR(txt) < txt->size) { - if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) { + if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0) + { if (cur == len) newentry = SEI_realloc(newentry, &len); newentry[cur] = wptr; @@ -269,10 +276,13 @@ ts_accum(PG_FUNCTION_ARGS) cmp = compareStatWord(sptr, wptr, stat, txt); if (cmp == 0) { - if ( stat->weight == 0 ) { + if (stat->weight == 0) + { sptr->ndoc++; sptr->nentry += (wptr->haspos) ? POSDATALEN(txt, wptr) : 1; - } else if ( wptr->haspos && (n=check_weight(txt, wptr, stat->weight))!=0 ) { + } + else if (wptr->haspos && (n = check_weight(txt, wptr, stat->weight)) != 0) + { sptr->ndoc++; sptr->nentry += n; } @@ -286,7 +296,8 @@ ts_accum(PG_FUNCTION_ARGS) if (StopLow >= StopHigh) { /* not found */ - if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) { + if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0) + { if (cur == len) newentry = SEI_realloc(newentry, &len); newentry[cur] = wptr; @@ -454,11 +465,15 @@ ts_stat_sql(text *txt, text *ws) stat->size = 0; stat->weight = 0; - if ( ws ) { - char *buf; + if (ws) + { + char *buf; + buf = VARDATA(ws); - while( buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ ) { - switch (tolower(*buf)) { + while (buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ) + { + switch (tolower(*buf)) + { case 'a': stat->weight |= 1 << 3; break; @@ -521,13 +536,14 @@ ts_stat(PG_FUNCTION_ARGS) { tsstat *stat; text *txt = PG_GETARG_TEXT_P(0); - text *ws = (PG_NARGS() > 1) ? PG_GETARG_TEXT_P(1) : NULL; + text *ws = (PG_NARGS() > 1) ? PG_GETARG_TEXT_P(1) : NULL; funcctx = SRF_FIRSTCALL_INIT(); SPI_connect(); - stat = ts_stat_sql(txt,ws); + stat = ts_stat_sql(txt, ws); PG_FREE_IF_COPY(txt, 0); - if (PG_NARGS() > 1 ) PG_FREE_IF_COPY(ws, 1); + if (PG_NARGS() > 1) + PG_FREE_IF_COPY(ws, 1); ts_setup_firstcall(funcctx, stat); SPI_finish(); } diff --git a/contrib/tsearch2/tsvector.c b/contrib/tsearch2/tsvector.c index a39a40fb366..9f26dec670f 100644 --- a/contrib/tsearch2/tsvector.c +++ b/contrib/tsearch2/tsvector.c @@ -404,7 +404,8 @@ tsvector_in(PG_FUNCTION_ARGS) *cur; int4 i, buflen = 256; - SET_FUNCOID(); + + SET_FUNCOID(); state.prsbuf = buf; state.len = 32; state.word = (char *) palloc(state.len); @@ -453,7 +454,7 @@ tsvector_in(PG_FUNCTION_ARGS) if (len > 0) len = uniqueentry(arr, len, tmpbuf, &buflen); else - buflen=0; + buflen = 0; totallen = CALCDATASIZE(len, buflen); in = (tsvector *) palloc(totallen); memset(in, 0, totallen); @@ -638,7 +639,8 @@ uniqueWORD(TSWORD * a, int4 l) res->alen *= 2; res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen); } - if ( res->pos.apos[0]==0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos) ) { + if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos)) + { res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos); res->pos.apos[0]++; } @@ -725,7 +727,7 @@ to_tsvector(PG_FUNCTION_ARGS) tsvector *out = NULL; TSCfgInfo *cfg; - SET_FUNCOID(); + SET_FUNCOID(); cfg = findcfg(PG_GETARG_INT32(0)); prs.lenwords = 32; @@ -753,13 +755,14 @@ to_tsvector_name(PG_FUNCTION_ARGS) { text *cfg = PG_GETARG_TEXT_P(0); Datum res; - SET_FUNCOID(); + + SET_FUNCOID(); res = DirectFunctionCall3( - to_tsvector, - Int32GetDatum(name2id_cfg(cfg)), - PG_GETARG_DATUM(1), - (Datum) 0 - ); + to_tsvector, + Int32GetDatum(name2id_cfg(cfg)), + PG_GETARG_DATUM(1), + (Datum) 0 + ); PG_FREE_IF_COPY(cfg, 0); PG_RETURN_DATUM(res); @@ -769,13 +772,14 @@ Datum to_tsvector_current(PG_FUNCTION_ARGS) { Datum res; - SET_FUNCOID(); + + SET_FUNCOID(); res = DirectFunctionCall3( - to_tsvector, - Int32GetDatum(get_currcfg()), - PG_GETARG_DATUM(0), - (Datum) 0 - ); + to_tsvector, + Int32GetDatum(get_currcfg()), + PG_GETARG_DATUM(0), + (Datum) 0 + ); PG_RETURN_DATUM(res); } @@ -823,7 +827,7 @@ tsearch2(PG_FUNCTION_ARGS) Oid funcoid = InvalidOid; TSCfgInfo *cfg; - SET_FUNCOID(); + SET_FUNCOID(); cfg = findcfg(get_currcfg()); if (!CALLED_AS_TRIGGER(fcinfo)) @@ -947,26 +951,30 @@ tsearch2(PG_FUNCTION_ARGS) } static int -silly_cmp_tsvector(const tsvector *a, const tsvector *b) { - if ( a->len < b->len ) +silly_cmp_tsvector(const tsvector * a, const tsvector * b) +{ + if (a->len < b->len) return -1; - else if ( a->len > b->len ) + else if (a->len > b->len) return 1; - else if ( a->size < b->size ) + else if (a->size < b->size) return -1; - else if ( a->size > b->size ) + else if (a->size > b->size) return 1; - else { - unsigned char *aptr=(unsigned char *)(a->data) + DATAHDRSIZE; - unsigned char *bptr=(unsigned char *)(b->data) + DATAHDRSIZE; - - while( aptr - ( (unsigned char *)(a->data) ) < a->len ) { - if ( *aptr != *bptr ) - return ( *aptr < *bptr ) ? -1 : 1; - aptr++; bptr++; - } + else + { + unsigned char *aptr = (unsigned char *) (a->data) + DATAHDRSIZE; + unsigned char *bptr = (unsigned char *) (b->data) + DATAHDRSIZE; + + while (aptr - ((unsigned char *) (a->data)) < a->len) + { + if (*aptr != *bptr) + return (*aptr < *bptr) ? -1 : 1; + aptr++; + bptr++; + } } - return 0; + return 0; } PG_FUNCTION_INFO_V1(tsvector_cmp); @@ -976,60 +984,66 @@ PG_FUNCTION_INFO_V1(tsvector_eq); PG_FUNCTION_INFO_V1(tsvector_ne); PG_FUNCTION_INFO_V1(tsvector_ge); PG_FUNCTION_INFO_V1(tsvector_gt); -Datum tsvector_cmp(PG_FUNCTION_ARGS); -Datum tsvector_lt(PG_FUNCTION_ARGS); -Datum tsvector_le(PG_FUNCTION_ARGS); -Datum tsvector_eq(PG_FUNCTION_ARGS); -Datum tsvector_ne(PG_FUNCTION_ARGS); -Datum tsvector_ge(PG_FUNCTION_ARGS); -Datum tsvector_gt(PG_FUNCTION_ARGS); - -#define RUNCMP \ -tsvector *a = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));\ -tsvector *b = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));\ +Datum tsvector_cmp(PG_FUNCTION_ARGS); +Datum tsvector_lt(PG_FUNCTION_ARGS); +Datum tsvector_le(PG_FUNCTION_ARGS); +Datum tsvector_eq(PG_FUNCTION_ARGS); +Datum tsvector_ne(PG_FUNCTION_ARGS); +Datum tsvector_ge(PG_FUNCTION_ARGS); +Datum tsvector_gt(PG_FUNCTION_ARGS); + +#define RUNCMP \ +tsvector *a = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));\ +tsvector *b = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));\ int res = silly_cmp_tsvector(a,b); \ PG_FREE_IF_COPY(a,0); \ PG_FREE_IF_COPY(b,1); \ Datum -tsvector_cmp(PG_FUNCTION_ARGS) { +tsvector_cmp(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_INT32(res); } Datum -tsvector_lt(PG_FUNCTION_ARGS) { +tsvector_lt(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_BOOL((res < 0) ? true : false); } Datum -tsvector_le(PG_FUNCTION_ARGS) { +tsvector_le(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_BOOL((res <= 0) ? true : false); } Datum -tsvector_eq(PG_FUNCTION_ARGS) { +tsvector_eq(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_BOOL((res == 0) ? true : false); } Datum -tsvector_ge(PG_FUNCTION_ARGS) { +tsvector_ge(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_BOOL((res >= 0) ? true : false); } - + Datum -tsvector_gt(PG_FUNCTION_ARGS) { +tsvector_gt(PG_FUNCTION_ARGS) +{ RUNCMP PG_RETURN_BOOL((res > 0) ? true : false); -} - +} + Datum -tsvector_ne(PG_FUNCTION_ARGS) { - RUNCMP +tsvector_ne(PG_FUNCTION_ARGS) +{ + RUNCMP PG_RETURN_BOOL((res != 0) ? true : false); } - diff --git a/contrib/tsearch2/wordparser/parser.h b/contrib/tsearch2/wordparser/parser.h index 7fb2d0a2868..3f0e0cd6359 100644 --- a/contrib/tsearch2/wordparser/parser.h +++ b/contrib/tsearch2/wordparser/parser.h @@ -1,8 +1,8 @@ #ifndef __PARSER_H__ #define __PARSER_H__ -extern char *token; -extern int tokenlen; +extern char *token; +extern int tokenlen; int tsearch2_yylex(void); void tsearch2_start_parse_str(char *, int); void tsearch2_end_parse(void); diff --git a/contrib/tsearch2/wparser.c b/contrib/tsearch2/wparser.c index eba2cd6eb58..554c2684b9b 100644 --- a/contrib/tsearch2/wparser.c +++ b/contrib/tsearch2/wparser.c @@ -30,18 +30,19 @@ init_prs(Oid id, WParserInfo * prs) bool isnull; Datum pars[1]; int stat; - void *plan; - char buf[1024], *nsp; + void *plan; + char buf[1024], + *nsp; arg[0] = OIDOID; pars[0] = ObjectIdGetDatum(id); memset(prs, 0, sizeof(WParserInfo)); SPI_connect(); - nsp=get_namespace(TSNSP_FunctionOid); + nsp = get_namespace(TSNSP_FunctionOid); sprintf(buf, "select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from %s.pg_ts_parser where oid = $1", nsp); pfree(nsp); - plan= SPI_prepare(buf, 1, arg); + plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -140,8 +141,9 @@ name2id_prs(text *name) Datum pars[1]; int stat; Oid id = findSNMap_t(&(PList.name2id_map), name); - char buf[1024], *nsp; - void *plan; + char buf[1024], + *nsp; + void *plan; arg[0] = TEXTOID; pars[0] = PointerGetDatum(name); @@ -153,7 +155,7 @@ name2id_prs(text *name) nsp = get_namespace(TSNSP_FunctionOid); sprintf(buf, "select oid from %s.pg_ts_parser where prs_name = $1", nsp); pfree(nsp); - plan= SPI_prepare(buf, 1, arg); + plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); @@ -242,7 +244,8 @@ token_type(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { funcctx = SRF_FIRSTCALL_INIT(); @@ -263,7 +266,8 @@ token_type_byname(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { text *name = PG_GETARG_TEXT_P(0); @@ -287,7 +291,8 @@ token_type_current(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { funcctx = SRF_FIRSTCALL_INIT(); @@ -309,7 +314,7 @@ Datum set_curprs(PG_FUNCTION_ARGS); Datum set_curprs(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); findprs(PG_GETARG_OID(0)); current_parser_id = PG_GETARG_OID(0); PG_RETURN_VOID(); @@ -321,7 +326,8 @@ Datum set_curprs_byname(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_P(0); - SET_FUNCOID(); + + SET_FUNCOID(); DirectFunctionCall1( set_curprs, ObjectIdGetDatum(name2id_prs(name)) @@ -444,7 +450,8 @@ parse(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { text *txt = PG_GETARG_TEXT_P(1); @@ -468,7 +475,8 @@ parse_byname(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { text *name = PG_GETARG_TEXT_P(0); @@ -495,7 +503,8 @@ parse_current(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; - SET_FUNCOID(); + + SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { text *txt = PG_GETARG_TEXT_P(0); @@ -527,7 +536,7 @@ headline(PG_FUNCTION_ARGS) TSCfgInfo *cfg; WParserInfo *prsobj; - SET_FUNCOID(); + SET_FUNCOID(); cfg = findcfg(PG_GETARG_OID(0)); prsobj = findprs(cfg->prs_id); @@ -566,14 +575,15 @@ headline_byname(PG_FUNCTION_ARGS) text *cfg = PG_GETARG_TEXT_P(0); Datum out; - SET_FUNCOID(); + + SET_FUNCOID(); out = DirectFunctionCall4( - headline, - ObjectIdGetDatum(name2id_cfg(cfg)), - PG_GETARG_DATUM(1), - PG_GETARG_DATUM(2), + headline, + ObjectIdGetDatum(name2id_cfg(cfg)), + PG_GETARG_DATUM(1), + PG_GETARG_DATUM(2), (PG_NARGS() > 3) ? PG_GETARG_DATUM(3) : PointerGetDatum(NULL) - ); + ); PG_FREE_IF_COPY(cfg, 0); PG_RETURN_DATUM(out); @@ -584,7 +594,7 @@ Datum headline_current(PG_FUNCTION_ARGS); Datum headline_current(PG_FUNCTION_ARGS) { - SET_FUNCOID(); + SET_FUNCOID(); PG_RETURN_DATUM(DirectFunctionCall4( headline, ObjectIdGetDatum(get_currcfg()), diff --git a/contrib/tsearch2/wparser_def.c b/contrib/tsearch2/wparser_def.c index 035e5f2495d..21b41eef8f6 100644 --- a/contrib/tsearch2/wparser_def.c +++ b/contrib/tsearch2/wparser_def.c @@ -192,12 +192,13 @@ prsd_headline(PG_FUNCTION_ARGS) int bestb = -1, beste = -1; int bestlen = -1; - int pose = 0, posb, + int pose = 0, + posb, poslen, curlen; int i; - int highlight=0; + int highlight = 0; /* config */ prs->startsel = NULL; @@ -224,13 +225,13 @@ prsd_headline(PG_FUNCTION_ARGS) prs->stopsel = pstrdup(mptr->value); else if (pg_strcasecmp(mptr->key, "HighlightAll") == 0) highlight = ( - pg_strcasecmp(mptr->value, "1")==0 || - pg_strcasecmp(mptr->value, "on")==0 || - pg_strcasecmp(mptr->value, "true")==0 || - pg_strcasecmp(mptr->value, "t")==0 || - pg_strcasecmp(mptr->value, "y")==0 || - pg_strcasecmp(mptr->value, "yes")==0 ) ? - 1 : 0; + pg_strcasecmp(mptr->value, "1") == 0 || + pg_strcasecmp(mptr->value, "on") == 0 || + pg_strcasecmp(mptr->value, "true") == 0 || + pg_strcasecmp(mptr->value, "t") == 0 || + pg_strcasecmp(mptr->value, "y") == 0 || + pg_strcasecmp(mptr->value, "yes") == 0) ? + 1 : 0; pfree(mptr->key); pfree(mptr->value); @@ -239,23 +240,25 @@ prsd_headline(PG_FUNCTION_ARGS) } pfree(map); - if (highlight==0) { + if (highlight == 0) + { if (min_words >= max_words) ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("MinWords should be less than MaxWords"))); + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("MinWords should be less than MaxWords"))); if (min_words <= 0) ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("MinWords should be positive"))); + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("MinWords should be positive"))); if (shortword < 0) ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("ShortWord should be >= 0"))); + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("ShortWord should be >= 0"))); } } - if (highlight==0) { + if (highlight == 0) + { while (hlCover(prs, query, &p, &q)) { /* find cover len in words */ @@ -269,17 +272,17 @@ prsd_headline(PG_FUNCTION_ARGS) poslen++; pose = i; } - + if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)) { /* best already finded, so try one more cover */ p++; continue; } - - posb=p; + + posb = p; if (curlen < max_words) - { /* find good end */ + { /* find good end */ for (i = i - 1; i < prs->curwords && curlen < max_words; i++) { if (i != q) @@ -295,8 +298,11 @@ prsd_headline(PG_FUNCTION_ARGS) if (curlen >= min_words) break; } - if ( curlen < min_words && i>=prs->curwords ) { /* got end of text and our cover is shoter than min_words */ - for(i=p; i>= 0; i--) { + if (curlen < min_words && i >= prs->curwords) + { /* got end of text and our cover is shoter + * than min_words */ + for (i = p; i >= 0; i--) + { if (!NONWORDTOKEN(prs->words[i].type)) curlen++; if (prs->words[i].item && !prs->words[i].repeated) @@ -306,11 +312,11 @@ prsd_headline(PG_FUNCTION_ARGS) if (curlen >= min_words) break; } - posb=(i>=0) ? i : 0; + posb = (i >= 0) ? i : 0; } } else - { /* shorter cover :((( */ + { /* shorter cover :((( */ for (; curlen > min_words; i--) { if (!NONWORDTOKEN(prs->words[i].type)) @@ -323,7 +329,7 @@ prsd_headline(PG_FUNCTION_ARGS) break; } } - + if (bestlen < 0 || (poslen > bestlen && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword)) || (bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) && (NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword))) @@ -332,7 +338,7 @@ prsd_headline(PG_FUNCTION_ARGS) beste = pose; bestlen = poslen; } - + p++; } @@ -348,19 +354,24 @@ prsd_headline(PG_FUNCTION_ARGS) bestb = 0; beste = pose; } - } else { - bestb=0; - beste=prs->curwords-1; + } + else + { + bestb = 0; + beste = prs->curwords - 1; } for (i = bestb; i <= beste; i++) { if (prs->words[i].item) prs->words[i].selected = 1; - if ( highlight==0 ) { + if (highlight == 0) + { if (HLIDIGNORE(prs->words[i].type)) prs->words[i].replace = 1; - } else { + } + else + { if (HTMLHLIDIGNORE(prs->words[i].type)) prs->words[i].replace = 1; } |