diff options
author | Bruce Momjian | 2003-08-04 00:43:34 +0000 |
---|---|---|
committer | Bruce Momjian | 2003-08-04 00:43:34 +0000 |
commit | 089003fb462fcce46c02bf47322b429f73c33c50 (patch) | |
tree | 77d78bc3a149df06f5603f60200a6ab363336624 /contrib/tsearch2 | |
parent | 63354a0228a1dbc4a0d5ddc8ecdd8326349d2100 (diff) |
pgindent run.
Diffstat (limited to 'contrib/tsearch2')
36 files changed, 5984 insertions, 4558 deletions
diff --git a/contrib/tsearch2/common.c b/contrib/tsearch2/common.c index 917dced87fa..30062180076 100644 --- a/contrib/tsearch2/common.c +++ b/contrib/tsearch2/common.c @@ -4,80 +4,99 @@ #include "ts_cfg.h" #include "dict.h" -text* -char2text(char* in) { +text * +char2text(char *in) +{ return charl2text(in, strlen(in)); } -text* charl2text(char* in, int len) { - text *out=(text*)palloc(len+VARHDRSZ); +text * +charl2text(char *in, int len) +{ + text *out = (text *) palloc(len + VARHDRSZ); + memcpy(VARDATA(out), in, len); - VARATT_SIZEP(out) = len+VARHDRSZ; + VARATT_SIZEP(out) = len + VARHDRSZ; return out; } -char -*text2char(text* in) { - char *out=palloc( VARSIZE(in) ); - memcpy(out, VARDATA(in), VARSIZE(in)-VARHDRSZ); - out[ VARSIZE(in)-VARHDRSZ ] ='\0'; +char + * +text2char(text *in) +{ + char *out = palloc(VARSIZE(in)); + + memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ); + out[VARSIZE(in) - VARHDRSZ] = '\0'; return out; } -char -*pnstrdup(char* in, int len) { - char *out=palloc( len+1 ); +char + * +pnstrdup(char *in, int len) +{ + char *out = palloc(len + 1); + memcpy(out, in, len); - out[len]='\0'; + out[len] = '\0'; return out; } -text -*ptextdup(text* in) { - text *out=(text*)palloc( VARSIZE(in) ); - memcpy(out,in,VARSIZE(in)); +text + * +ptextdup(text *in) +{ + text *out = (text *) palloc(VARSIZE(in)); + + memcpy(out, in, VARSIZE(in)); return out; } -text -*mtextdup(text* in) { - text *out=(text*)malloc( VARSIZE(in) ); - if ( !out ) +text + * +mtextdup(text *in) +{ + text *out = (text *) malloc(VARSIZE(in)); + + if (!out) ts_error(ERROR, "No memory"); - memcpy(out,in,VARSIZE(in)); + memcpy(out, in, VARSIZE(in)); return out; } -void -ts_error(int state, const char *format, ...) { - va_list args; - int tlen = 128, len=0; - char *buf; - +void +ts_error(int state, const char *format,...) +{ + va_list args; + int tlen = 128, + len = 0; + char *buf; + reset_cfg(); reset_dict(); reset_prs(); va_start(args, format); buf = palloc(tlen); - len = vsnprintf(buf, tlen-1, format, args); - if ( len >= tlen ) { - tlen=len+1; - buf = repalloc( buf, tlen ); - vsnprintf(buf, tlen-1, format, args); + len = vsnprintf(buf, tlen - 1, format, args); + if (len >= tlen) + { + tlen = len + 1; + buf = repalloc(buf, tlen); + vsnprintf(buf, tlen - 1, format, args); } va_end(args); - + /* ?? internal error ?? */ elog(state, "%s", buf); pfree(buf); } -int -text_cmp(text *a, text *b) { - if ( VARSIZE(a) == VARSIZE(b) ) - return strncmp( VARDATA(a), VARDATA(b), VARSIZE(a)-VARHDRSZ ); - return (int)VARSIZE(a) - (int)VARSIZE(b); +int +text_cmp(text *a, text *b) +{ + if (VARSIZE(a) == VARSIZE(b)) + return strncmp(VARDATA(a), VARDATA(b), VARSIZE(a) - VARHDRSZ); + return (int) VARSIZE(a) - (int) VARSIZE(b); } - diff --git a/contrib/tsearch2/common.h b/contrib/tsearch2/common.h index 70313fa4d24..481f00405bb 100644 --- a/contrib/tsearch2/common.h +++ b/contrib/tsearch2/common.h @@ -7,18 +7,18 @@ #define PG_NARGS() (fcinfo->nargs) #endif -text* char2text(char* in); -text* charl2text(char* in, int len); -char *text2char(text* in); -char *pnstrdup(char* in, int len); -text *ptextdup(text* in); -text *mtextdup(text* in); +text *char2text(char *in); +text *charl2text(char *in, int len); +char *text2char(text *in); +char *pnstrdup(char *in, int len); +text *ptextdup(text *in); +text *mtextdup(text *in); -int text_cmp(text *a, text *b); +int text_cmp(text *a, text *b); #define NEXTVAL(x) ( (text*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) ) #define ARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x)) -void ts_error(int state, const char *format, ...); +void ts_error(int state, const char *format,...); #endif diff --git a/contrib/tsearch2/dict.c b/contrib/tsearch2/dict.c index 8f4cad5c417..9ceb78ffb83 100644 --- a/contrib/tsearch2/dict.c +++ b/contrib/tsearch2/dict.c @@ -1,5 +1,5 @@ -/* - * interface functions to dictionary +/* + * interface functions to dictionary * Teodor Sigaev <teodor@sigaev.ru> */ #include <errno.h> @@ -19,260 +19,285 @@ /*********top interface**********/ -static void *plan_getdict=NULL; +static void *plan_getdict = NULL; void -init_dict(Oid id, DictInfo *dict) { - Oid arg[1]={ OIDOID }; - bool isnull; - Datum pars[1]={ ObjectIdGetDatum(id) }; - int stat; - - memset(dict,0,sizeof(DictInfo)); +init_dict(Oid id, DictInfo * dict) +{ + Oid arg[1] = {OIDOID}; + bool isnull; + Datum pars[1] = {ObjectIdGetDatum(id)}; + int stat; + + memset(dict, 0, sizeof(DictInfo)); SPI_connect(); - if ( !plan_getdict ) { - plan_getdict = SPI_saveplan( SPI_prepare( "select dict_init, dict_initoption, dict_lexize from pg_ts_dict where oid = $1" , 1, arg ) ); - if ( !plan_getdict ) + if (!plan_getdict) + { + plan_getdict = SPI_saveplan(SPI_prepare("select dict_init, dict_initoption, dict_lexize from pg_ts_dict where oid = $1", 1, arg)); + if (!plan_getdict) ts_error(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_getdict, pars, " ", 1); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) { - Datum opt; - Oid oid=InvalidOid; - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - if ( !(isnull || oid==InvalidOid) ) { - opt=SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull); - dict->dictionary=(void*)DatumGetPointer(OidFunctionCall1(oid, opt)); + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + { + Datum opt; + Oid oid = InvalidOid; + + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + if (!(isnull || oid == InvalidOid)) + { + opt = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull); + dict->dictionary = (void *) DatumGetPointer(OidFunctionCall1(oid, opt)); } - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull) ); - if ( isnull || oid==InvalidOid ) + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull)); + if (isnull || oid == InvalidOid) ts_error(ERROR, "Null dict_lexize for dictonary %d", id); fmgr_info_cxt(oid, &(dict->lexize_info), TopMemoryContext); - dict->dict_id=id; - } else + dict->dict_id = id; + } + else ts_error(ERROR, "No dictionary with id %d", id); SPI_finish(); } -typedef struct { - DictInfo *last_dict; - int len; - int reallen; - DictInfo *list; +typedef struct +{ + DictInfo *last_dict; + int len; + int reallen; + DictInfo *list; SNMap name2id_map; -} DictList; +} DictList; -static DictList DList = {NULL,0,0,NULL,{0,0,NULL}}; +static DictList DList = {NULL, 0, 0, NULL, {0, 0, NULL}}; void -reset_dict(void) { - freeSNMap( &(DList.name2id_map) ); +reset_dict(void) +{ + freeSNMap(&(DList.name2id_map)); /* XXX need to free DList.list[*].dictionary */ - if ( DList.list ) + if (DList.list) free(DList.list); - memset(&DList,0,sizeof(DictList)); + memset(&DList, 0, sizeof(DictList)); } static int -comparedict(const void *a, const void *b) { - return ((DictInfo*)a)->dict_id - ((DictInfo*)b)->dict_id; +comparedict(const void *a, const void *b) +{ + return ((DictInfo *) a)->dict_id - ((DictInfo *) b)->dict_id; } DictInfo * -finddict(Oid id) { +finddict(Oid id) +{ /* last used dict */ - if ( DList.last_dict && DList.last_dict->dict_id==id ) + if (DList.last_dict && DList.last_dict->dict_id == id) return DList.last_dict; /* already used dict */ - if ( DList.len != 0 ) { - DictInfo key; - key.dict_id=id; + if (DList.len != 0) + { + DictInfo key; + + key.dict_id = id; DList.last_dict = bsearch(&key, DList.list, DList.len, sizeof(DictInfo), comparedict); - if ( DList.last_dict != NULL ) + if (DList.last_dict != NULL) return DList.last_dict; } /* last chance */ - if ( DList.len==DList.reallen ) { - DictInfo *tmp; - int reallen = ( DList.reallen ) ? 2*DList.reallen : 16; - tmp=(DictInfo*)realloc(DList.list,sizeof(DictInfo)*reallen); - if ( !tmp ) - ts_error(ERROR,"No memory"); - DList.reallen=reallen; - DList.list=tmp; + if (DList.len == DList.reallen) + { + DictInfo *tmp; + int reallen = (DList.reallen) ? 2 * DList.reallen : 16; + + tmp = (DictInfo *) realloc(DList.list, sizeof(DictInfo) * reallen); + if (!tmp) + ts_error(ERROR, "No memory"); + DList.reallen = reallen; + DList.list = tmp; } - DList.last_dict=&(DList.list[DList.len]); + DList.last_dict = &(DList.list[DList.len]); init_dict(id, DList.last_dict); DList.len++; qsort(DList.list, DList.len, sizeof(DictInfo), comparedict); - return finddict(id); /* qsort changed order!! */; + return finddict(id); /* qsort changed order!! */ ; } -static void *plan_name2id=NULL; +static void *plan_name2id = NULL; Oid -name2id_dict(text *name) { - Oid arg[1]={ TEXTOID }; - bool isnull; - Datum pars[1]={ PointerGetDatum(name) }; - int stat; - Oid id=findSNMap_t( &(DList.name2id_map), name ); - - if ( id ) +name2id_dict(text *name) +{ + Oid arg[1] = {TEXTOID}; + bool isnull; + Datum pars[1] = {PointerGetDatum(name)}; + int stat; + Oid id = findSNMap_t(&(DList.name2id_map), name); + + if (id) return id; - + SPI_connect(); - if ( !plan_name2id ) { - plan_name2id = SPI_saveplan( SPI_prepare( "select oid from pg_ts_dict where dict_name = $1" , 1, arg ) ); - if ( !plan_name2id ) + if (!plan_name2id) + { + plan_name2id = SPI_saveplan(SPI_prepare("select oid from pg_ts_dict where dict_name = $1", 1, arg)); + if (!plan_name2id) ts_error(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_name2id, pars, " ", 1); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) - id=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - else + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + else ts_error(ERROR, "No dictionary with name '%s'", text2char(name)); SPI_finish(); - addSNMap_t( &(DList.name2id_map), name, id ); + addSNMap_t(&(DList.name2id_map), name, id); return id; } /******sql-level interface******/ PG_FUNCTION_INFO_V1(lexize); -Datum lexize(PG_FUNCTION_ARGS); +Datum lexize(PG_FUNCTION_ARGS); Datum -lexize(PG_FUNCTION_ARGS) { - text *in=PG_GETARG_TEXT_P(1); - DictInfo *dict = finddict( PG_GETARG_OID(0) ); - char **res, **ptr; - Datum *da; - ArrayType *a; - - - ptr = res = (char**)DatumGetPointer( - FunctionCall3(&(dict->lexize_info), - PointerGetDatum(dict->dictionary), - PointerGetDatum(VARDATA(in)), - Int32GetDatum(VARSIZE(in)-VARHDRSZ) - ) - ); +lexize(PG_FUNCTION_ARGS) +{ + text *in = PG_GETARG_TEXT_P(1); + DictInfo *dict = finddict(PG_GETARG_OID(0)); + char **res, + **ptr; + Datum *da; + ArrayType *a; + + + ptr = res = (char **) DatumGetPointer( + FunctionCall3(&(dict->lexize_info), + PointerGetDatum(dict->dictionary), + PointerGetDatum(VARDATA(in)), + Int32GetDatum(VARSIZE(in) - VARHDRSZ) + ) + ); PG_FREE_IF_COPY(in, 1); - if ( !res ) { - if (PG_NARGS() > 2) + if (!res) + { + if (PG_NARGS() > 2) PG_RETURN_POINTER(NULL); else PG_RETURN_NULL(); } - while(*ptr) ptr++; - da = (Datum*)palloc(sizeof(Datum)*(ptr-res+1)); - ptr=res; - while(*ptr) { - da[ ptr-res ] = PointerGetDatum( char2text(*ptr) ); + while (*ptr) + ptr++; + da = (Datum *) palloc(sizeof(Datum) * (ptr - res + 1)); + ptr = res; + while (*ptr) + { + da[ptr - res] = PointerGetDatum(char2text(*ptr)); ptr++; } a = construct_array( - da, - ptr-res, - TEXTOID, - -1, - false, - 'i' - ); - - ptr=res; - while(*ptr) { - pfree( DatumGetPointer(da[ ptr-res ]) ); - pfree( *ptr ); + da, + ptr - res, + TEXTOID, + -1, + false, + 'i' + ); + + ptr = res; + while (*ptr) + { + pfree(DatumGetPointer(da[ptr - res])); + pfree(*ptr); ptr++; } pfree(res); pfree(da); - - PG_RETURN_POINTER(a); + + PG_RETURN_POINTER(a); } PG_FUNCTION_INFO_V1(lexize_byname); -Datum lexize_byname(PG_FUNCTION_ARGS); -Datum -lexize_byname(PG_FUNCTION_ARGS) { - text *dictname=PG_GETARG_TEXT_P(0); - Datum res; +Datum lexize_byname(PG_FUNCTION_ARGS); +Datum +lexize_byname(PG_FUNCTION_ARGS) +{ + text *dictname = PG_GETARG_TEXT_P(0); + Datum res; strdup("simple"); - res=DirectFunctionCall3( - lexize, - ObjectIdGetDatum(name2id_dict(dictname)), - PG_GETARG_DATUM(1), - (Datum)0 - ); + res = DirectFunctionCall3( + lexize, + ObjectIdGetDatum(name2id_dict(dictname)), + PG_GETARG_DATUM(1), + (Datum) 0 + ); PG_FREE_IF_COPY(dictname, 0); - if (res) - PG_RETURN_DATUM(res); - else + if (res) + PG_RETURN_DATUM(res); + else PG_RETURN_NULL(); } -static Oid currect_dictionary_id=0; +static Oid currect_dictionary_id = 0; PG_FUNCTION_INFO_V1(set_curdict); -Datum set_curdict(PG_FUNCTION_ARGS); +Datum set_curdict(PG_FUNCTION_ARGS); Datum -set_curdict(PG_FUNCTION_ARGS) { +set_curdict(PG_FUNCTION_ARGS) +{ finddict(PG_GETARG_OID(0)); - currect_dictionary_id=PG_GETARG_OID(0); + currect_dictionary_id = PG_GETARG_OID(0); PG_RETURN_VOID(); } PG_FUNCTION_INFO_V1(set_curdict_byname); -Datum set_curdict_byname(PG_FUNCTION_ARGS); +Datum set_curdict_byname(PG_FUNCTION_ARGS); Datum -set_curdict_byname(PG_FUNCTION_ARGS) { - text *dictname=PG_GETARG_TEXT_P(0); +set_curdict_byname(PG_FUNCTION_ARGS) +{ + text *dictname = PG_GETARG_TEXT_P(0); DirectFunctionCall1( - set_curdict, - ObjectIdGetDatum( name2id_dict(dictname) ) - ); + set_curdict, + ObjectIdGetDatum(name2id_dict(dictname)) + ); PG_FREE_IF_COPY(dictname, 0); PG_RETURN_VOID(); } PG_FUNCTION_INFO_V1(lexize_bycurrent); -Datum lexize_bycurrent(PG_FUNCTION_ARGS); -Datum -lexize_bycurrent(PG_FUNCTION_ARGS) { - Datum res; - if ( currect_dictionary_id == 0 ) +Datum lexize_bycurrent(PG_FUNCTION_ARGS); +Datum +lexize_bycurrent(PG_FUNCTION_ARGS) +{ + Datum res; + + if (currect_dictionary_id == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("no currect dictionary"), errhint("Execute select set_curdict()."))); res = DirectFunctionCall3( - lexize, - ObjectIdGetDatum(currect_dictionary_id), - PG_GETARG_DATUM(0), - (Datum)0 - ); - if (res) + lexize, + ObjectIdGetDatum(currect_dictionary_id), + PG_GETARG_DATUM(0), + (Datum) 0 + ); + if (res) PG_RETURN_DATUM(res); - else + else PG_RETURN_NULL(); } - - diff --git a/contrib/tsearch2/dict.h b/contrib/tsearch2/dict.h index bbbbfc47a51..86ea42263e5 100644 --- a/contrib/tsearch2/dict.h +++ b/contrib/tsearch2/dict.h @@ -3,36 +3,39 @@ #include "postgres.h" #include "fmgr.h" -typedef struct { - int len; - char **stop; - char* (*wordop)(char*); -} StopList; - -void sortstoplist(StopList *s); -void freestoplist(StopList *s); -void readstoplist(text *in, StopList *s); -bool searchstoplist(StopList *s, char *key); -char* lowerstr(char *str); - -typedef struct { - Oid dict_id; - FmgrInfo lexize_info; - void *dictionary; -} DictInfo; - -void init_dict(Oid id, DictInfo *dict); -DictInfo* finddict(Oid id); -Oid name2id_dict(text *name); -void reset_dict(void); +typedef struct +{ + int len; + char **stop; + char *(*wordop) (char *); +} StopList; + +void sortstoplist(StopList * s); +void freestoplist(StopList * s); +void readstoplist(text *in, StopList * s); +bool searchstoplist(StopList * s, char *key); +char *lowerstr(char *str); + +typedef struct +{ + Oid dict_id; + FmgrInfo lexize_info; + void *dictionary; +} DictInfo; + +void init_dict(Oid id, DictInfo * dict); +DictInfo *finddict(Oid id); +Oid name2id_dict(text *name); +void reset_dict(void); /* simple parser of cfg string */ -typedef struct { - char *key; - char *value; -} Map; +typedef struct +{ + char *key; + char *value; +} Map; -void parse_cfgdict(text *in, Map **m); +void parse_cfgdict(text *in, Map ** m); #endif diff --git a/contrib/tsearch2/dict_ex.c b/contrib/tsearch2/dict_ex.c index ead96106871..a8fb20453ba 100644 --- a/contrib/tsearch2/dict_ex.c +++ b/contrib/tsearch2/dict_ex.c @@ -1,5 +1,5 @@ -/* - * example of dictionary +/* + * example of dictionary * Teodor Sigaev <teodor@sigaev.ru> */ #include <errno.h> @@ -11,30 +11,35 @@ #include "dict.h" #include "common.h" -typedef struct { +typedef struct +{ StopList stoplist; -} DictExample; +} DictExample; PG_FUNCTION_INFO_V1(dex_init); -Datum dex_init(PG_FUNCTION_ARGS); +Datum dex_init(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(dex_lexize); -Datum dex_lexize(PG_FUNCTION_ARGS); +Datum dex_lexize(PG_FUNCTION_ARGS); -Datum -dex_init(PG_FUNCTION_ARGS) { - DictExample *d = (DictExample*)malloc( sizeof(DictExample) ); +Datum +dex_init(PG_FUNCTION_ARGS) +{ + DictExample *d = (DictExample *) malloc(sizeof(DictExample)); - if ( !d ) + if (!d) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - memset(d,0,sizeof(DictExample)); + memset(d, 0, sizeof(DictExample)); + + d->stoplist.wordop = lowerstr; + + if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL) + { + text *in = PG_GETARG_TEXT_P(0); - d->stoplist.wordop=lowerstr; - - if ( !PG_ARGISNULL(0) && PG_GETARG_POINTER(0)!=NULL ) { - text *in = PG_GETARG_TEXT_P(0); readstoplist(in, &(d->stoplist)); sortstoplist(&(d->stoplist)); PG_FREE_IF_COPY(in, 0); @@ -44,18 +49,21 @@ dex_init(PG_FUNCTION_ARGS) { } Datum -dex_lexize(PG_FUNCTION_ARGS) { - DictExample *d = (DictExample*)PG_GETARG_POINTER(0); - char *in = (char*)PG_GETARG_POINTER(1); - char *txt = pnstrdup(in, PG_GETARG_INT32(2)); - char **res=palloc(sizeof(char*)*2); +dex_lexize(PG_FUNCTION_ARGS) +{ + DictExample *d = (DictExample *) PG_GETARG_POINTER(0); + char *in = (char *) PG_GETARG_POINTER(1); + char *txt = pnstrdup(in, PG_GETARG_INT32(2)); + char **res = palloc(sizeof(char *) * 2); - if ( *txt=='\0' || searchstoplist(&(d->stoplist),txt) ) { + if (*txt == '\0' || searchstoplist(&(d->stoplist), txt)) + { pfree(txt); - res[0]=NULL; - } else - res[0]=txt; - res[1]=NULL; + res[0] = NULL; + } + else + res[0] = txt; + res[1] = NULL; PG_RETURN_POINTER(res); } diff --git a/contrib/tsearch2/dict_ispell.c b/contrib/tsearch2/dict_ispell.c index c053adfad0b..e3a100fa013 100644 --- a/contrib/tsearch2/dict_ispell.c +++ b/contrib/tsearch2/dict_ispell.c @@ -1,4 +1,4 @@ -/* +/* * ISpell interface * Teodor Sigaev <teodor@sigaev.ru> */ @@ -12,96 +12,117 @@ #include "common.h" #include "ispell/spell.h" -typedef struct { +typedef struct +{ StopList stoplist; IspellDict obj; -} DictISpell; +} DictISpell; PG_FUNCTION_INFO_V1(spell_init); -Datum spell_init(PG_FUNCTION_ARGS); +Datum spell_init(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(spell_lexize); -Datum spell_lexize(PG_FUNCTION_ARGS); +Datum spell_lexize(PG_FUNCTION_ARGS); static void -freeDictISpell(DictISpell *d) { +freeDictISpell(DictISpell * d) +{ FreeIspell(&(d->obj)); freestoplist(&(d->stoplist)); free(d); } -Datum -spell_init(PG_FUNCTION_ARGS) { - DictISpell *d; - Map *cfg, *pcfg; - text *in; - bool affloaded=false, dictloaded=false, stoploaded=false; - - if ( PG_ARGISNULL(0) || PG_GETARG_POINTER(0)==NULL ) +Datum +spell_init(PG_FUNCTION_ARGS) +{ + DictISpell *d; + Map *cfg, + *pcfg; + text *in; + bool affloaded = false, + dictloaded = false, + stoploaded = false; + + if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL) ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("ISpell confguration error"))); - - d = (DictISpell*)malloc( sizeof(DictISpell) ); - if ( !d ) + + d = (DictISpell *) malloc(sizeof(DictISpell)); + if (!d) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - memset(d,0,sizeof(DictISpell)); - d->stoplist.wordop=lowerstr; + memset(d, 0, sizeof(DictISpell)); + d->stoplist.wordop = lowerstr; in = PG_GETARG_TEXT_P(0); - parse_cfgdict(in,&cfg); + parse_cfgdict(in, &cfg); PG_FREE_IF_COPY(in, 0); - pcfg=cfg; - while(pcfg->key) { - if ( strcasecmp("DictFile", pcfg->key) == 0 ) { - if ( dictloaded ) { + pcfg = cfg; + while (pcfg->key) + { + if (strcasecmp("DictFile", pcfg->key) == 0) + { + if (dictloaded) + { freeDictISpell(d); ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("dictionary already loaded"))); + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("dictionary already loaded"))); } - if ( ImportDictionary(&(d->obj), pcfg->value) ) { + if (ImportDictionary(&(d->obj), pcfg->value)) + { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not load dictionary file \"%s\"", pcfg->value))); } - dictloaded=true; - } else if ( strcasecmp("AffFile", pcfg->key) == 0 ) { - if ( affloaded ) { + dictloaded = true; + } + else if (strcasecmp("AffFile", pcfg->key) == 0) + { + if (affloaded) + { freeDictISpell(d); ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("affixes already loaded"))); + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("affixes already loaded"))); } - if ( ImportAffixes(&(d->obj), pcfg->value) ) { + if (ImportAffixes(&(d->obj), pcfg->value)) + { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not load affix file \"%s\"", pcfg->value))); } - affloaded=true; - } else if ( strcasecmp("StopFile", pcfg->key) == 0 ) { - text *tmp=char2text(pcfg->value); - if ( stoploaded ) { + affloaded = true; + } + else if (strcasecmp("StopFile", pcfg->key) == 0) + { + text *tmp = char2text(pcfg->value); + + if (stoploaded) + { freeDictISpell(d); ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("stop words already loaded"))); + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("stop words already loaded"))); } readstoplist(tmp, &(d->stoplist)); sortstoplist(&(d->stoplist)); pfree(tmp); - stoploaded=true; - } else { + stoploaded = true; + } + else + { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("unrecognized option: %s => %s", - pcfg->key, pcfg->value))); + pcfg->key, pcfg->value))); } pfree(pcfg->key); pfree(pcfg->value); @@ -109,15 +130,20 @@ spell_init(PG_FUNCTION_ARGS) { } pfree(cfg); - if ( affloaded && dictloaded ) { + if (affloaded && dictloaded) + { SortDictionary(&(d->obj)); SortAffixes(&(d->obj)); - } else if ( !affloaded ) { + } + else if (!affloaded) + { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("no affixes"))); - } else { + } + else + { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), @@ -128,37 +154,43 @@ spell_init(PG_FUNCTION_ARGS) { } Datum -spell_lexize(PG_FUNCTION_ARGS) { - DictISpell *d = (DictISpell*)PG_GETARG_POINTER(0); - char *in = (char*)PG_GETARG_POINTER(1); - char *txt; - char **res; - char **ptr, **cptr; - - if ( !PG_GETARG_INT32(2) ) +spell_lexize(PG_FUNCTION_ARGS) +{ + DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0); + char *in = (char *) PG_GETARG_POINTER(1); + char *txt; + char **res; + char **ptr, + **cptr; + + if (!PG_GETARG_INT32(2)) PG_RETURN_POINTER(NULL); - res=palloc(sizeof(char*)*2); + res = palloc(sizeof(char *) * 2); txt = pnstrdup(in, PG_GETARG_INT32(2)); - res=NormalizeWord(&(d->obj), txt); + res = NormalizeWord(&(d->obj), txt); pfree(txt); - if ( res==NULL ) + if (res == NULL) PG_RETURN_POINTER(NULL); - ptr=cptr=res; - while(*ptr) { - if ( searchstoplist(&(d->stoplist),*ptr) ) { + ptr = cptr = res; + while (*ptr) + { + if (searchstoplist(&(d->stoplist), *ptr)) + { pfree(*ptr); - *ptr=NULL; + *ptr = NULL; + ptr++; + } + else + { + *cptr = *ptr; + cptr++; ptr++; - } else { - *cptr=*ptr; - cptr++; ptr++; } } - *cptr=NULL; + *cptr = NULL; PG_RETURN_POINTER(res); } - diff --git a/contrib/tsearch2/dict_snowball.c b/contrib/tsearch2/dict_snowball.c index 103d87d7f99..51dba044499 100644 --- a/contrib/tsearch2/dict_snowball.c +++ b/contrib/tsearch2/dict_snowball.c @@ -1,6 +1,6 @@ -/* +/* * example of Snowball dictionary - * http://snowball.tartarus.org/ + * http://snowball.tartarus.org/ * Teodor Sigaev <teodor@sigaev.ru> */ #include <stdlib.h> @@ -14,103 +14,118 @@ #include "snowball/english_stem.h" #include "snowball/russian_stem.h" -typedef struct { +typedef struct +{ struct SN_env *z; StopList stoplist; - int (*stem)(struct SN_env * z); -} DictSnowball; + int (*stem) (struct SN_env * z); +} DictSnowball; PG_FUNCTION_INFO_V1(snb_en_init); -Datum snb_en_init(PG_FUNCTION_ARGS); +Datum snb_en_init(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(snb_ru_init); -Datum snb_ru_init(PG_FUNCTION_ARGS); +Datum snb_ru_init(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(snb_lexize); -Datum snb_lexize(PG_FUNCTION_ARGS); +Datum snb_lexize(PG_FUNCTION_ARGS); -Datum -snb_en_init(PG_FUNCTION_ARGS) { - DictSnowball *d = (DictSnowball*)malloc( sizeof(DictSnowball) ); +Datum +snb_en_init(PG_FUNCTION_ARGS) +{ + DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball)); - if ( !d ) + if (!d) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - memset(d,0,sizeof(DictSnowball)); - d->stoplist.wordop=lowerstr; - - if ( !PG_ARGISNULL(0) && PG_GETARG_POINTER(0)!=NULL ) { - text *in = PG_GETARG_TEXT_P(0); + memset(d, 0, sizeof(DictSnowball)); + d->stoplist.wordop = lowerstr; + + if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL) + { + text *in = PG_GETARG_TEXT_P(0); + readstoplist(in, &(d->stoplist)); sortstoplist(&(d->stoplist)); PG_FREE_IF_COPY(in, 0); } d->z = english_create_env(); - if (!d->z) { + if (!d->z) + { freestoplist(&(d->stoplist)); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - d->stem=english_stem; + d->stem = english_stem; PG_RETURN_POINTER(d); } -Datum -snb_ru_init(PG_FUNCTION_ARGS) { - DictSnowball *d = (DictSnowball*)malloc( sizeof(DictSnowball) ); +Datum +snb_ru_init(PG_FUNCTION_ARGS) +{ + DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball)); - if ( !d ) + if (!d) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - memset(d,0,sizeof(DictSnowball)); - d->stoplist.wordop=lowerstr; - - if ( !PG_ARGISNULL(0) && PG_GETARG_POINTER(0)!=NULL ) { - text *in = PG_GETARG_TEXT_P(0); + memset(d, 0, sizeof(DictSnowball)); + d->stoplist.wordop = lowerstr; + + if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL) + { + text *in = PG_GETARG_TEXT_P(0); + readstoplist(in, &(d->stoplist)); sortstoplist(&(d->stoplist)); PG_FREE_IF_COPY(in, 0); } d->z = russian_create_env(); - if (!d->z) { + if (!d->z) + { freestoplist(&(d->stoplist)); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - d->stem=russian_stem; + d->stem = russian_stem; PG_RETURN_POINTER(d); } Datum -snb_lexize(PG_FUNCTION_ARGS) { - DictSnowball *d = (DictSnowball*)PG_GETARG_POINTER(0); - char *in = (char*)PG_GETARG_POINTER(1); - char *txt = pnstrdup(in, PG_GETARG_INT32(2)); - char **res=palloc(sizeof(char*)*2); - - if ( *txt=='\0' || searchstoplist(&(d->stoplist),txt) ) { +snb_lexize(PG_FUNCTION_ARGS) +{ + DictSnowball *d = (DictSnowball *) PG_GETARG_POINTER(0); + char *in = (char *) PG_GETARG_POINTER(1); + char *txt = pnstrdup(in, PG_GETARG_INT32(2)); + char **res = palloc(sizeof(char *) * 2); + + if (*txt == '\0' || searchstoplist(&(d->stoplist), txt)) + { pfree(txt); - res[0]=NULL; - } else { + res[0] = NULL; + } + else + { SN_set_current(d->z, strlen(txt), txt); - (d->stem)(d->z); - if ( d->z->p && d->z->l ) { - txt=repalloc(txt, d->z->l+1); - memcpy( txt, d->z->p, d->z->l); - txt[d->z->l]='\0'; - } - res[0]=txt; + (d->stem) (d->z); + if (d->z->p && d->z->l) + { + txt = repalloc(txt, d->z->l + 1); + memcpy(txt, d->z->p, d->z->l); + txt[d->z->l] = '\0'; + } + res[0] = txt; } - res[1]=NULL; + res[1] = NULL; PG_RETURN_POINTER(res); } - diff --git a/contrib/tsearch2/dict_syn.c b/contrib/tsearch2/dict_syn.c index 34f74cf0ff7..8364223be40 100644 --- a/contrib/tsearch2/dict_syn.c +++ b/contrib/tsearch2/dict_syn.c @@ -1,4 +1,4 @@ -/* +/* * ISpell interface * Teodor Sigaev <teodor@sigaev.ru> */ @@ -13,93 +13,106 @@ #include "common.h" #define SYNBUFLEN 4096 -typedef struct { - char *in; - char *out; -} Syn; - -typedef struct { - int len; - Syn *syn; -} DictSyn; +typedef struct +{ + char *in; + char *out; +} Syn; + +typedef struct +{ + int len; + Syn *syn; +} DictSyn; PG_FUNCTION_INFO_V1(syn_init); -Datum syn_init(PG_FUNCTION_ARGS); +Datum syn_init(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(syn_lexize); -Datum syn_lexize(PG_FUNCTION_ARGS); +Datum syn_lexize(PG_FUNCTION_ARGS); static char * -findwrd(char *in, char **end) { - char *start; +findwrd(char *in, char **end) +{ + char *start; - *end=NULL; - while(*in && isspace(*in)) + *end = NULL; + while (*in && isspace(*in)) in++; - if ( !in ) + if (!in) return NULL; - start=in; + start = in; - while(*in && !isspace(*in)) + while (*in && !isspace(*in)) in++; - *end=in; + *end = in; return start; } static int -compareSyn(const void *a, const void *b) { - return strcmp( ((Syn*)a)->in, ((Syn*)b)->in ); +compareSyn(const void *a, const void *b) +{ + return strcmp(((Syn *) a)->in, ((Syn *) b)->in); } -Datum -syn_init(PG_FUNCTION_ARGS) { - text *in; - DictSyn *d; - int cur=0; - FILE *fin; - char *filename; - char buf[SYNBUFLEN]; - char *starti,*starto,*end=NULL; - int slen; - - if ( PG_ARGISNULL(0) || PG_GETARG_POINTER(0)==NULL ) +Datum +syn_init(PG_FUNCTION_ARGS) +{ + text *in; + DictSyn *d; + int cur = 0; + FILE *fin; + char *filename; + char buf[SYNBUFLEN]; + char *starti, + *starto, + *end = NULL; + int slen; + + if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("NULL config"))); in = PG_GETARG_TEXT_P(0); - if ( VARSIZE(in) - VARHDRSZ == 0 ) + if (VARSIZE(in) - VARHDRSZ == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("VOID config"))); - filename=text2char(in); + filename = text2char(in); PG_FREE_IF_COPY(in, 0); - if ( (fin=fopen(filename,"r")) == NULL ) + if ((fin = fopen(filename, "r")) == NULL) ereport(ERROR, (errcode_for_file_access(), errmsg("could not open file \"%s\": %m", - filename))); + filename))); - d = (DictSyn*)malloc( sizeof(DictSyn) ); - if ( !d ) { + d = (DictSyn *) malloc(sizeof(DictSyn)); + if (!d) + { fclose(fin); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - memset(d,0,sizeof(DictSyn)); + memset(d, 0, sizeof(DictSyn)); - while( fgets(buf,SYNBUFLEN,fin) ) { - slen = strlen(buf)-1; + while (fgets(buf, SYNBUFLEN, fin)) + { + slen = strlen(buf) - 1; buf[slen] = '\0'; - if ( *buf=='\0' ) continue; - if (cur==d->len) { - d->len = (d->len) ? 2*d->len : 16; - d->syn=(Syn*)realloc( d->syn, sizeof(Syn)*d->len ); - if ( !d->syn ) { + if (*buf == '\0') + continue; + if (cur == d->len) + { + d->len = (d->len) ? 2 * d->len : 16; + d->syn = (Syn *) realloc(d->syn, sizeof(Syn) * d->len); + if (!d->syn) + { fclose(fin); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), @@ -107,64 +120,66 @@ syn_init(PG_FUNCTION_ARGS) { } } - starti=findwrd(buf,&end); - if ( !starti ) + starti = findwrd(buf, &end); + if (!starti) continue; - *end='\0'; - if ( end >= buf+slen ) + *end = '\0'; + if (end >= buf + slen) continue; - starto= findwrd(end+1, &end); - if ( !starto ) + starto = findwrd(end + 1, &end); + if (!starto) continue; - *end='\0'; + *end = '\0'; - d->syn[cur].in=strdup(lowerstr(starti)); - d->syn[cur].out=strdup(lowerstr(starto)); - if ( !(d->syn[cur].in && d->syn[cur].out) ) { + d->syn[cur].in = strdup(lowerstr(starti)); + d->syn[cur].out = strdup(lowerstr(starto)); + if (!(d->syn[cur].in && d->syn[cur].out)) + { fclose(fin); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - cur++; + cur++; } - - fclose(fin); - - d->len=cur; - if ( cur>1 ) - qsort(d->syn, d->len, sizeof(Syn), compareSyn); + + fclose(fin); + + d->len = cur; + if (cur > 1) + qsort(d->syn, d->len, sizeof(Syn), compareSyn); pfree(filename); - PG_RETURN_POINTER(d); + PG_RETURN_POINTER(d); } Datum -syn_lexize(PG_FUNCTION_ARGS) { - DictSyn *d = (DictSyn*)PG_GETARG_POINTER(0); - char *in = (char*)PG_GETARG_POINTER(1); - Syn key,*found; - char **res=NULL; - - if ( !PG_GETARG_INT32(2) ) +syn_lexize(PG_FUNCTION_ARGS) +{ + DictSyn *d = (DictSyn *) PG_GETARG_POINTER(0); + char *in = (char *) PG_GETARG_POINTER(1); + Syn key, + *found; + char **res = NULL; + + if (!PG_GETARG_INT32(2)) PG_RETURN_POINTER(NULL); - key.out=NULL; - key.in=lowerstr(pnstrdup(in, PG_GETARG_INT32(2))); + key.out = NULL; + key.in = lowerstr(pnstrdup(in, PG_GETARG_INT32(2))); - found=(Syn*)bsearch(&key, d->syn, d->len, sizeof(Syn), compareSyn); + found = (Syn *) bsearch(&key, d->syn, d->len, sizeof(Syn), compareSyn); pfree(key.in); - if ( !found ) + if (!found) PG_RETURN_POINTER(NULL); - res=palloc(sizeof(char*)*2); + res = palloc(sizeof(char *) * 2); - res[0]=pstrdup(found->out); - res[1]=NULL; + res[0] = pstrdup(found->out); + res[1] = NULL; - PG_RETURN_POINTER(res); + PG_RETURN_POINTER(res); } - diff --git a/contrib/tsearch2/ispell/spell.c b/contrib/tsearch2/ispell/spell.c index c5ab2601496..45786cca652 100644 --- a/contrib/tsearch2/ispell/spell.c +++ b/contrib/tsearch2/ispell/spell.c @@ -9,518 +9,648 @@ #define MAXNORMLEN 56 -#define STRNCASECMP(x,y) (strncasecmp(x,y,strlen(y))) +#define STRNCASECMP(x,y) (strncasecmp(x,y,strlen(y))) -static int cmpspell(const void *s1,const void *s2){ - return(strcmp(((const SPELL*)s1)->word,((const SPELL*)s2)->word)); +static int +cmpspell(const void *s1, const void *s2) +{ + return (strcmp(((const SPELL *) s1)->word, ((const SPELL *) s2)->word)); } -static void -strlower( char * str ) { - unsigned char *ptr = (unsigned char *)str; - while ( *ptr ) { - *ptr = tolower( *ptr ); +static void +strlower(char *str) +{ + unsigned char *ptr = (unsigned char *) str; + + while (*ptr) + { + *ptr = tolower(*ptr); ptr++; } } /* backward string compaire for suffix tree operations */ -static int -strbcmp(const char *s1, const char *s2) { - int l1 = strlen(s1)-1, l2 = strlen(s2)-1; - while (l1 >= 0 && l2 >= 0) { - if (s1[l1] < s2[l2]) return -1; - if (s1[l1] > s2[l2]) return 1; - l1--; l2--; +static int +strbcmp(const char *s1, const char *s2) +{ + int l1 = strlen(s1) - 1, + l2 = strlen(s2) - 1; + + while (l1 >= 0 && l2 >= 0) + { + if (s1[l1] < s2[l2]) + return -1; + if (s1[l1] > s2[l2]) + return 1; + l1--; + l2--; } - if (l1 < l2) return -1; - if (l1 > l2) return 1; + if (l1 < l2) + return -1; + if (l1 > l2) + return 1; return 0; } -static int -strbncmp(const char *s1, const char *s2, size_t count) { - int l1 = strlen(s1) - 1, l2 = strlen(s2) - 1, l = count; - while (l1 >= 0 && l2 >= 0 && l > 0) { - if (s1[l1] < s2[l2]) return -1; - if (s1[l1] > s2[l2]) return 1; +static int +strbncmp(const char *s1, const char *s2, size_t count) +{ + int l1 = strlen(s1) - 1, + l2 = strlen(s2) - 1, + l = count; + + while (l1 >= 0 && l2 >= 0 && l > 0) + { + if (s1[l1] < s2[l2]) + return -1; + if (s1[l1] > s2[l2]) + return 1; l1--; l2--; l--; } - if (l == 0) return 0; - if (l1 < l2) return -1; - if (l1 > l2) return 1; + if (l == 0) + return 0; + if (l1 < l2) + return -1; + if (l1 > l2) + return 1; return 0; } -static int -cmpaffix(const void *s1,const void *s2){ - if (((const AFFIX*)s1)->type < ((const AFFIX*)s2)->type) return -1; - if (((const AFFIX*)s1)->type > ((const AFFIX*)s2)->type) return 1; - if (((const AFFIX*)s1)->type == 'p') - return(strcmp(((const AFFIX*)s1)->repl,((const AFFIX*)s2)->repl)); - else - return(strbcmp(((const AFFIX*)s1)->repl,((const AFFIX*)s2)->repl)); +static int +cmpaffix(const void *s1, const void *s2) +{ + if (((const AFFIX *) s1)->type < ((const AFFIX *) s2)->type) + return -1; + if (((const AFFIX *) s1)->type > ((const AFFIX *) s2)->type) + return 1; + if (((const AFFIX *) s1)->type == 'p') + return (strcmp(((const AFFIX *) s1)->repl, ((const AFFIX *) s2)->repl)); + else + return (strbcmp(((const AFFIX *) s1)->repl, ((const AFFIX *) s2)->repl)); } -int -AddSpell(IspellDict * Conf,const char * word,const char *flag){ - if(Conf->nspell>=Conf->mspell){ - if(Conf->mspell){ - Conf->mspell+=1024*20; - Conf->Spell=(SPELL *)realloc(Conf->Spell,Conf->mspell*sizeof(SPELL)); - }else{ - Conf->mspell=1024*20; - Conf->Spell=(SPELL *)malloc(Conf->mspell*sizeof(SPELL)); +int +AddSpell(IspellDict * Conf, const char *word, const char *flag) +{ + if (Conf->nspell >= Conf->mspell) + { + if (Conf->mspell) + { + Conf->mspell += 1024 * 20; + Conf->Spell = (SPELL *) realloc(Conf->Spell, Conf->mspell * sizeof(SPELL)); + } + else + { + Conf->mspell = 1024 * 20; + Conf->Spell = (SPELL *) malloc(Conf->mspell * sizeof(SPELL)); } - if ( Conf->Spell == NULL ) + if (Conf->Spell == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - Conf->Spell[Conf->nspell].word=strdup(word); - if ( !Conf->Spell[Conf->nspell].word ) + Conf->Spell[Conf->nspell].word = strdup(word); + if (!Conf->Spell[Conf->nspell].word) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - strncpy(Conf->Spell[Conf->nspell].flag,flag,10); + strncpy(Conf->Spell[Conf->nspell].flag, flag, 10); Conf->nspell++; - return(0); + return (0); } -int -ImportDictionary(IspellDict * Conf,const char *filename){ - unsigned char str[BUFSIZ]; - FILE *dict; +int +ImportDictionary(IspellDict * Conf, const char *filename) +{ + unsigned char str[BUFSIZ]; + FILE *dict; - if(!(dict=fopen(filename,"r")))return(1); - while(fgets(str,sizeof(str),dict)){ + if (!(dict = fopen(filename, "r"))) + return (1); + while (fgets(str, sizeof(str), dict)) + { unsigned char *s; const unsigned char *flag; - flag = NULL; - if((s=strchr(str,'/'))){ - *s=0; - s++;flag=s; - while(*s){ - if (((*s>='A')&&(*s<='Z'))||((*s>='a')&&(*s<='z'))) + flag = NULL; + if ((s = strchr(str, '/'))) + { + *s = 0; + s++; + flag = s; + while (*s) + { + if (((*s >= 'A') && (*s <= 'Z')) || ((*s >= 'a') && (*s <= 'z'))) s++; - else { - *s=0; + else + { + *s = 0; break; } } - }else{ - flag=""; } + else + flag = ""; strlower(str); /* Dont load words if first letter is not required */ /* It allows to optimize loading at search time */ - s=str; - while(*s){ - if(*s=='\r')*s=0; - if(*s=='\n')*s=0; + s = str; + while (*s) + { + if (*s == '\r') + *s = 0; + if (*s == '\n') + *s = 0; s++; } - AddSpell(Conf,str,flag); + AddSpell(Conf, str, flag); } fclose(dict); - return(0); + return (0); } -static SPELL * -FindWord(IspellDict * Conf, const char *word, int affixflag) { - int l,c,r,resc,resl,resr, i; +static SPELL * +FindWord(IspellDict * Conf, const char *word, int affixflag) +{ + int l, + c, + r, + resc, + resl, + resr, + i; - i = (int)(*word) & 255; + i = (int) (*word) & 255; l = Conf->SpellTree.Left[i]; r = Conf->SpellTree.Right[i]; - if (l == -1) return (NULL); - while(l<=r){ + if (l == -1) + return (NULL); + while (l <= r) + { c = (l + r) >> 1; resc = strcmp(Conf->Spell[c].word, word); - if( (resc == 0) && - ((affixflag == 0) || (strchr(Conf->Spell[c].flag, affixflag) != NULL)) ) { - return(&Conf->Spell[c]); - } + if ((resc == 0) && + ((affixflag == 0) || (strchr(Conf->Spell[c].flag, affixflag) != NULL))) + return (&Conf->Spell[c]); resl = strcmp(Conf->Spell[l].word, word); - if( (resl == 0) && - ((affixflag == 0) || (strchr(Conf->Spell[l].flag, affixflag) != NULL)) ) { - return(&Conf->Spell[l]); - } + if ((resl == 0) && + ((affixflag == 0) || (strchr(Conf->Spell[l].flag, affixflag) != NULL))) + return (&Conf->Spell[l]); resr = strcmp(Conf->Spell[r].word, word); - if( (resr == 0) && - ((affixflag == 0) || (strchr(Conf->Spell[r].flag, affixflag) != NULL)) ) { - return(&Conf->Spell[r]); - } - if(resc < 0){ + if ((resr == 0) && + ((affixflag == 0) || (strchr(Conf->Spell[r].flag, affixflag) != NULL))) + return (&Conf->Spell[r]); + if (resc < 0) + { l = c + 1; r--; - } else if(resc > 0){ + } + else if (resc > 0) + { r = c - 1; l++; - } else { + } + else + { l++; r--; } } - return(NULL); + return (NULL); } -int -AddAffix(IspellDict * Conf,int flag,const char *mask,const char *find,const char *repl,int type) { - if(Conf->naffixes>=Conf->maffixes){ - if(Conf->maffixes){ - Conf->maffixes+=16; - Conf->Affix = (AFFIX*)realloc((void*)Conf->Affix,Conf->maffixes*sizeof(AFFIX)); - }else{ - Conf->maffixes=16; - Conf->Affix = (AFFIX*)malloc(Conf->maffixes * sizeof(AFFIX)); +int +AddAffix(IspellDict * Conf, int flag, const char *mask, const char *find, const char *repl, int type) +{ + if (Conf->naffixes >= Conf->maffixes) + { + if (Conf->maffixes) + { + Conf->maffixes += 16; + Conf->Affix = (AFFIX *) realloc((void *) Conf->Affix, Conf->maffixes * sizeof(AFFIX)); + } + else + { + Conf->maffixes = 16; + Conf->Affix = (AFFIX *) malloc(Conf->maffixes * sizeof(AFFIX)); } - if ( Conf->Affix == NULL ) + if (Conf->Affix == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - if (type=='s') { - sprintf(Conf->Affix[Conf->naffixes].mask,"%s$",mask); - } else { - sprintf(Conf->Affix[Conf->naffixes].mask,"^%s",mask); - } + if (type == 's') + 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].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->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); + return (0); } -static char * -remove_spaces(char *dist,char *src){ -char *d,*s; - d=dist; - s=src; - while(*s){ - if(*s!=' '&&*s!='-'&&*s!='\t'){ - *d=*s; +static char * +remove_spaces(char *dist, char *src) +{ + char *d, + *s; + + d = dist; + s = src; + while (*s) + { + if (*s != ' ' && *s != '-' && *s != '\t') + { + *d = *s; d++; } s++; } - *d=0; - return(dist); + *d = 0; + return (dist); } -int -ImportAffixes(IspellDict * Conf,const char *filename){ +int +ImportAffixes(IspellDict * Conf, const char *filename) +{ unsigned char str[BUFSIZ]; - unsigned char flag=0; - unsigned char mask[BUFSIZ]=""; - unsigned char find[BUFSIZ]=""; - unsigned char repl[BUFSIZ]=""; + unsigned char flag = 0; + unsigned char mask[BUFSIZ] = ""; + unsigned char find[BUFSIZ] = ""; + unsigned char repl[BUFSIZ] = ""; unsigned char *s; - int i; - int suffixes=0; - int prefixes=0; - FILE *affix; - - if(!(affix=fopen(filename,"r"))) - return(1); - - while(fgets(str,sizeof(str),affix)){ - if(!STRNCASECMP(str,"suffixes")){ - suffixes=1; - prefixes=0; + int i; + int suffixes = 0; + int prefixes = 0; + FILE *affix; + + if (!(affix = fopen(filename, "r"))) + return (1); + + while (fgets(str, sizeof(str), affix)) + { + if (!STRNCASECMP(str, "suffixes")) + { + suffixes = 1; + prefixes = 0; continue; } - if(!STRNCASECMP(str,"prefixes")){ - suffixes=0; - prefixes=1; + if (!STRNCASECMP(str, "prefixes")) + { + suffixes = 0; + prefixes = 1; continue; } - if(!STRNCASECMP(str,"flag ")){ - s=str+5; - while(strchr("* ",*s)) + if (!STRNCASECMP(str, "flag ")) + { + s = str + 5; + while (strchr("* ", *s)) s++; - flag=*s; + flag = *s; continue; } - if((!suffixes)&&(!prefixes))continue; - if((s=strchr(str,'#')))*s=0; - if(!*str)continue; + if ((!suffixes) && (!prefixes)) + continue; + if ((s = strchr(str, '#'))) + *s = 0; + if (!*str) + continue; strlower(str); - strcpy(mask,""); - strcpy(find,""); - strcpy(repl,""); - i=sscanf(str,"%[^>\n]>%[^,\n],%[^\n]",mask,find,repl); - remove_spaces(str,repl);strcpy(repl,str); - remove_spaces(str,find);strcpy(find,str); - remove_spaces(str,mask);strcpy(mask,str); - switch(i){ + strcpy(mask, ""); + strcpy(find, ""); + strcpy(repl, ""); + i = sscanf(str, "%[^>\n]>%[^,\n],%[^\n]", mask, find, repl); + remove_spaces(str, repl); + strcpy(repl, str); + remove_spaces(str, find); + strcpy(find, str); + remove_spaces(str, mask); + strcpy(mask, str); + switch (i) + { case 3: break; case 2: - if(*find != '\0'){ - strcpy(repl,find); - strcpy(find,""); + if (*find != '\0') + { + strcpy(repl, find); + strcpy(find, ""); } break; default: continue; } - - AddAffix(Conf,(int)flag,mask,find,repl,suffixes?'s':'p'); - + + AddAffix(Conf, (int) flag, mask, find, repl, suffixes ? 's' : 'p'); + } fclose(affix); - - return(0); + + return (0); } -void -SortDictionary(IspellDict * Conf){ - int CurLet = -1, Let;size_t i; +void +SortDictionary(IspellDict * Conf) +{ + int CurLet = -1, + Let; + size_t i; - qsort((void*)Conf->Spell,Conf->nspell,sizeof(SPELL),cmpspell); + qsort((void *) Conf->Spell, Conf->nspell, sizeof(SPELL), cmpspell); - for(i = 0; i < 256 ; i++ ) + for (i = 0; i < 256; i++) Conf->SpellTree.Left[i] = -1; - for(i = 0; i < Conf->nspell; i++) { - Let = (int)(*(Conf->Spell[i].word)) & 255; - if (CurLet != Let) { - Conf->SpellTree.Left[Let] = i; - CurLet = Let; - } - Conf->SpellTree.Right[Let] = i; + for (i = 0; i < Conf->nspell; i++) + { + Let = (int) (*(Conf->Spell[i].word)) & 255; + if (CurLet != Let) + { + Conf->SpellTree.Left[Let] = i; + CurLet = Let; + } + Conf->SpellTree.Right[Let] = i; } } -void -SortAffixes(IspellDict * Conf) { - int CurLetP = -1, CurLetS = -1, Let; - AFFIX *Affix; size_t i; - - if (Conf->naffixes > 1) - qsort((void*)Conf->Affix,Conf->naffixes,sizeof(AFFIX),cmpaffix); - for(i = 0; i < 256; i++) { - Conf->PrefixTree.Left[i] = Conf->PrefixTree.Right[i] = -1; - Conf->SuffixTree.Left[i] = Conf->SuffixTree.Right[i] = -1; - } - - for(i = 0; i < Conf->naffixes; i++) { - Affix = &(((AFFIX*)Conf->Affix)[i]); - if(Affix->type == 'p') { - Let = (int)(*(Affix->repl)) & 255; - if (CurLetP != Let) { - Conf->PrefixTree.Left[Let] = i; - CurLetP = Let; - } - Conf->PrefixTree.Right[Let] = i; - } else { - Let = (Affix->replen) ? (int)(Affix->repl[Affix->replen-1]) & 255 : 0; - if (CurLetS != Let) { - Conf->SuffixTree.Left[Let] = i; - CurLetS = Let; - } - Conf->SuffixTree.Right[Let] = i; - } - } +void +SortAffixes(IspellDict * Conf) +{ + int CurLetP = -1, + CurLetS = -1, + Let; + AFFIX *Affix; + size_t i; + + if (Conf->naffixes > 1) + qsort((void *) Conf->Affix, Conf->naffixes, sizeof(AFFIX), cmpaffix); + for (i = 0; i < 256; i++) + { + Conf->PrefixTree.Left[i] = Conf->PrefixTree.Right[i] = -1; + Conf->SuffixTree.Left[i] = Conf->SuffixTree.Right[i] = -1; + } + + for (i = 0; i < Conf->naffixes; i++) + { + Affix = &(((AFFIX *) Conf->Affix)[i]); + if (Affix->type == 'p') + { + Let = (int) (*(Affix->repl)) & 255; + if (CurLetP != Let) + { + Conf->PrefixTree.Left[Let] = i; + CurLetP = Let; + } + Conf->PrefixTree.Right[Let] = i; + } + else + { + Let = (Affix->replen) ? (int) (Affix->repl[Affix->replen - 1]) & 255 : 0; + if (CurLetS != Let) + { + Conf->SuffixTree.Left[Let] = i; + CurLetS = Let; + } + Conf->SuffixTree.Right[Let] = i; + } + } } -static char * -CheckSuffix(const char *word, size_t len, AFFIX *Affix, int *res, IspellDict *Conf) { - regmatch_t subs[2]; /* workaround for apache&linux */ - char newword[2*MAXNORMLEN] = ""; - int err; - - *res = strbncmp(word, Affix->repl, Affix->replen); - if (*res < 0) { - return NULL; - } - if (*res > 0) { - return NULL; - } - strcpy(newword, word); - strcpy(newword+len-Affix->replen, Affix->find); - - if (Affix->compile) { - err = regcomp(&(Affix->reg),Affix->mask,REG_EXTENDED|REG_ICASE|REG_NOSUB); - if(err){ - /*regerror(err, &(Affix->reg), regerrstr, ERRSTRSIZE);*/ - regfree(&(Affix->reg)); - return(NULL); - } - Affix->compile = 0; - } - if(!(err=regexec(&(Affix->reg),newword,1,subs,0))){ - if(FindWord(Conf, newword, Affix->flag)) - return pstrdup(newword); - } - return NULL; +static char * +CheckSuffix(const char *word, size_t len, AFFIX * Affix, int *res, IspellDict * Conf) +{ + regmatch_t subs[2]; /* workaround for apache&linux */ + char newword[2 * MAXNORMLEN] = ""; + int err; + + *res = strbncmp(word, Affix->repl, Affix->replen); + if (*res < 0) + return NULL; + if (*res > 0) + return NULL; + strcpy(newword, word); + strcpy(newword + len - Affix->replen, Affix->find); + + if (Affix->compile) + { + err = regcomp(&(Affix->reg), Affix->mask, REG_EXTENDED | REG_ICASE | REG_NOSUB); + if (err) + { + /* regerror(err, &(Affix->reg), regerrstr, ERRSTRSIZE); */ + regfree(&(Affix->reg)); + return (NULL); + } + Affix->compile = 0; + } + if (!(err = regexec(&(Affix->reg), newword, 1, subs, 0))) + { + if (FindWord(Conf, newword, Affix->flag)) + return pstrdup(newword); + } + return NULL; } #define NS 1 #define MAX_NORM 512 -static int -CheckPrefix(const char *word, size_t len, AFFIX *Affix, IspellDict *Conf, int pi, - char **forms, char ***cur ) { - regmatch_t subs[NS*2]; - char newword[2*MAXNORMLEN] = ""; - int err, ls, res, lres; - size_t newlen; - AFFIX *CAffix = Conf->Affix; - - res = strncmp(word, Affix->repl, Affix->replen); - if (res != 0) { - return res; - } - strcpy(newword, Affix->find); - strcat(newword, word+Affix->replen); - - if (Affix->compile) { - err = regcomp(&(Affix->reg),Affix->mask,REG_EXTENDED|REG_ICASE|REG_NOSUB); - if(err){ - /*regerror(err, &(Affix->reg), regerrstr, ERRSTRSIZE);*/ - regfree(&(Affix->reg)); - return (0); - } - Affix->compile = 0; - } - if(!(err=regexec(&(Affix->reg),newword,1,subs,0))){ - SPELL * curspell; - - if((curspell=FindWord(Conf, newword, Affix->flag))){ - if ((*cur - forms) < (MAX_NORM-1)) { - **cur = pstrdup(newword); - (*cur)++; **cur = NULL; - } - } - newlen = strlen(newword); - ls = Conf->SuffixTree.Left[pi]; - if ( ls>=0 && ((*cur - forms) < (MAX_NORM-1)) ) { - **cur = CheckSuffix(newword, newlen, &CAffix[ls], &lres, Conf); - if (**cur) { - (*cur)++; **cur = NULL; +static int +CheckPrefix(const char *word, size_t len, AFFIX * Affix, IspellDict * Conf, int pi, + char **forms, char ***cur) +{ + regmatch_t subs[NS * 2]; + char newword[2 * MAXNORMLEN] = ""; + int err, + ls, + res, + lres; + size_t newlen; + AFFIX *CAffix = Conf->Affix; + + res = strncmp(word, Affix->repl, Affix->replen); + if (res != 0) + return res; + strcpy(newword, Affix->find); + strcat(newword, word + Affix->replen); + + if (Affix->compile) + { + err = regcomp(&(Affix->reg), Affix->mask, REG_EXTENDED | REG_ICASE | REG_NOSUB); + if (err) + { + /* regerror(err, &(Affix->reg), regerrstr, ERRSTRSIZE); */ + regfree(&(Affix->reg)); + return (0); + } + Affix->compile = 0; } - } - } - return 0; + if (!(err = regexec(&(Affix->reg), newword, 1, subs, 0))) + { + SPELL *curspell; + + if ((curspell = FindWord(Conf, newword, Affix->flag))) + { + if ((*cur - forms) < (MAX_NORM - 1)) + { + **cur = pstrdup(newword); + (*cur)++; + **cur = NULL; + } + } + newlen = strlen(newword); + ls = Conf->SuffixTree.Left[pi]; + if (ls >= 0 && ((*cur - forms) < (MAX_NORM - 1))) + { + **cur = CheckSuffix(newword, newlen, &CAffix[ls], &lres, Conf); + if (**cur) + { + (*cur)++; + **cur = NULL; + } + } + } + return 0; } -char ** -NormalizeWord(IspellDict * Conf,char *word){ +char ** +NormalizeWord(IspellDict * Conf, char *word) +{ /*regmatch_t subs[NS];*/ -size_t len; -char ** forms; -char **cur; -AFFIX * Affix; -int ri, pi, ipi, lp, rp, cp, ls, rs; -int lres, rres, cres = 0; - SPELL *spell; - - len=strlen(word); + size_t len; + char **forms; + char **cur; + AFFIX *Affix; + int ri, + pi, + ipi, + lp, + rp, + cp, + ls, + rs; + int lres, + rres, + cres = 0; + SPELL *spell; + + len = strlen(word); if (len > MAXNORMLEN) - return(NULL); + return (NULL); strlower(word); - forms=(char **) palloc(MAX_NORM*sizeof(char **)); - cur=forms;*cur=NULL; + forms = (char **) palloc(MAX_NORM * sizeof(char **)); + cur = forms; + *cur = NULL; - ri = (int)(*word) & 255; - pi = (int)(word[strlen(word)-1]) & 255; - Affix=(AFFIX*)Conf->Affix; + ri = (int) (*word) & 255; + pi = (int) (word[strlen(word) - 1]) & 255; + Affix = (AFFIX *) Conf->Affix; /* Check that the word itself is normal form */ - if((spell = FindWord(Conf, word, 0))){ - *cur=pstrdup(word); - cur++;*cur=NULL; + if ((spell = FindWord(Conf, word, 0))) + { + *cur = pstrdup(word); + cur++; + *cur = NULL; } /* Find all other NORMAL forms of the 'word' */ - for (ipi = 0; ipi <= pi; ipi += pi) { - - /* check prefix */ - lp = Conf->PrefixTree.Left[ri]; - rp = Conf->PrefixTree.Right[ri]; - while (lp >= 0 && lp <= rp) { - cp = (lp + rp) >> 1; - cres = 0; - if ((cur - forms) < (MAX_NORM-1)) { - cres = CheckPrefix(word, len, &Affix[cp], Conf, ipi, forms, &cur); - } - if ((lp < cp) && ((cur - forms) < (MAX_NORM-1)) ) { - lres = CheckPrefix(word, len, &Affix[lp], Conf, ipi, forms, &cur); - } - if ( (rp > cp) && ((cur - forms) < (MAX_NORM-1)) ) { - rres = CheckPrefix(word, len, &Affix[rp], Conf, ipi, forms, &cur); - } - if (cres < 0) { - rp = cp - 1; - lp++; - } else if (cres > 0) { - lp = cp + 1; - rp--; - } else { - lp++; - rp--; - } - } - - /* check suffix */ - ls = Conf->SuffixTree.Left[ipi]; - rs = Conf->SuffixTree.Right[ipi]; - while (ls >= 0 && ls <= rs) { - if ( ((cur - forms) < (MAX_NORM-1)) ) { - *cur = CheckSuffix(word, len, &Affix[ls], &lres, Conf); - if (*cur) { - cur++; *cur = NULL; - } - } - if ( (rs > ls) && ((cur - forms) < (MAX_NORM-1)) ) { - *cur = CheckSuffix(word, len, &Affix[rs], &rres, Conf); - if (*cur) { - cur++; *cur = NULL; + for (ipi = 0; ipi <= pi; ipi += pi) + { + + /* check prefix */ + lp = Conf->PrefixTree.Left[ri]; + rp = Conf->PrefixTree.Right[ri]; + while (lp >= 0 && lp <= rp) + { + cp = (lp + rp) >> 1; + cres = 0; + if ((cur - forms) < (MAX_NORM - 1)) + cres = CheckPrefix(word, len, &Affix[cp], Conf, ipi, forms, &cur); + if ((lp < cp) && ((cur - forms) < (MAX_NORM - 1))) + lres = CheckPrefix(word, len, &Affix[lp], Conf, ipi, forms, &cur); + if ((rp > cp) && ((cur - forms) < (MAX_NORM - 1))) + rres = CheckPrefix(word, len, &Affix[rp], Conf, ipi, forms, &cur); + if (cres < 0) + { + rp = cp - 1; + lp++; + } + else if (cres > 0) + { + lp = cp + 1; + rp--; + } + else + { + lp++; + rp--; + } } - } - ls++; - rs--; - } /* end while */ - - } /* for ipi */ - - if(cur==forms){ + + /* check suffix */ + ls = Conf->SuffixTree.Left[ipi]; + rs = Conf->SuffixTree.Right[ipi]; + while (ls >= 0 && ls <= rs) + { + if (((cur - forms) < (MAX_NORM - 1))) + { + *cur = CheckSuffix(word, len, &Affix[ls], &lres, Conf); + if (*cur) + { + cur++; + *cur = NULL; + } + } + if ((rs > ls) && ((cur - forms) < (MAX_NORM - 1))) + { + *cur = CheckSuffix(word, len, &Affix[rs], &rres, Conf); + if (*cur) + { + cur++; + *cur = NULL; + } + } + ls++; + rs--; + } /* end while */ + + } /* for ipi */ + + if (cur == forms) + { pfree(forms); - return(NULL); + return (NULL); } - return(forms); + return (forms); } -void -FreeIspell (IspellDict *Conf) { - int i; - AFFIX *Affix = (AFFIX *)Conf->Affix; - - for (i = 0; i < Conf->naffixes; i++) { - if (Affix[i].compile == 0) { - regfree(&(Affix[i].reg)); - } - } - for (i = 0; i < Conf->naffixes; i++) { - free( Conf->Spell[i].word ); - } - free(Conf->Affix); - free(Conf->Spell); - memset( (void*)Conf, 0, sizeof(IspellDict) ); - return; +void +FreeIspell(IspellDict * Conf) +{ + int i; + AFFIX *Affix = (AFFIX *) Conf->Affix; + + for (i = 0; i < Conf->naffixes; i++) + { + if (Affix[i].compile == 0) + regfree(&(Affix[i].reg)); + } + for (i = 0; i < Conf->naffixes; i++) + free(Conf->Spell[i].word); + free(Conf->Affix); + free(Conf->Spell); + memset((void *) Conf, 0, sizeof(IspellDict)); + return; } diff --git a/contrib/tsearch2/ispell/spell.h b/contrib/tsearch2/ispell/spell.h index 3034ca6709d..baf5052f026 100644 --- a/contrib/tsearch2/ispell/spell.h +++ b/contrib/tsearch2/ispell/spell.h @@ -4,48 +4,53 @@ #include <sys/types.h> #include <regex.h> -typedef struct spell_struct { - char * word; - char flag[10]; -} SPELL; - -typedef struct aff_struct { - char flag; - char type; - char mask[33]; - char find[16]; - char repl[16]; - regex_t reg; - size_t replen; - char compile; -} AFFIX; - -typedef struct Tree_struct { - int Left[256], Right[256]; -} Tree_struct; - -typedef struct { - int maffixes; - int naffixes; - AFFIX * Affix; - - int nspell; - int mspell; - SPELL *Spell; - Tree_struct SpellTree; - Tree_struct PrefixTree; - Tree_struct SuffixTree; - -} IspellDict; - -char ** NormalizeWord(IspellDict * Conf,char *word); -int ImportAffixes(IspellDict * Conf, const char *filename); -int ImportDictionary(IspellDict * Conf,const char *filename); - -int AddSpell(IspellDict * Conf,const char * word,const char *flag); -int AddAffix(IspellDict * Conf,int flag,const char *mask,const char *find,const char *repl,int type); -void SortDictionary(IspellDict * Conf); -void SortAffixes(IspellDict * Conf); -void FreeIspell (IspellDict *Conf); +typedef struct spell_struct +{ + char *word; + char flag[10]; +} SPELL; + +typedef struct aff_struct +{ + char flag; + char type; + char mask[33]; + char find[16]; + char repl[16]; + regex_t reg; + size_t replen; + char compile; +} AFFIX; + +typedef struct Tree_struct +{ + int Left[256], + Right[256]; +} Tree_struct; + +typedef struct +{ + int maffixes; + int naffixes; + AFFIX *Affix; + + int nspell; + int mspell; + SPELL *Spell; + Tree_struct SpellTree; + Tree_struct PrefixTree; + Tree_struct SuffixTree; + +} IspellDict; + +char **NormalizeWord(IspellDict * Conf, char *word); +int ImportAffixes(IspellDict * Conf, const char *filename); +int ImportDictionary(IspellDict * Conf, const char *filename); + +int AddSpell(IspellDict * Conf, const char *word, const char *flag); +int AddAffix(IspellDict * Conf, int flag, const char *mask, const char *find, const char *repl, int type); +void SortDictionary(IspellDict * Conf); +void SortAffixes(IspellDict * Conf); +void FreeIspell(IspellDict * Conf); #endif diff --git a/contrib/tsearch2/prs_dcfg.c b/contrib/tsearch2/prs_dcfg.c index 783cf43ceb7..6b553e45256 100644 --- a/contrib/tsearch2/prs_dcfg.c +++ b/contrib/tsearch2/prs_dcfg.c @@ -1,5 +1,5 @@ -/* - * Simple config parser +/* + * Simple config parser * Teodor Sigaev <teodor@sigaev.ru> */ #include <stdlib.h> @@ -16,126 +16,164 @@ #define CS_WAITEQ 2 #define CS_WAITVALUE 3 #define CS_INVALUE 4 -#define CS_IN2VALUE 5 +#define CS_IN2VALUE 5 #define CS_WAITDELIM 6 #define CS_INESC 7 #define CS_IN2ESC 8 static char * -nstrdup(char *ptr, int len) { - char *res=palloc(len+1), *cptr; - memcpy(res,ptr,len); - res[len]='\0'; +nstrdup(char *ptr, int len) +{ + char *res = palloc(len + 1), + *cptr; + + memcpy(res, ptr, len); + res[len] = '\0'; cptr = ptr = res; - while(*ptr) { - if ( *ptr == '\\' ) + while (*ptr) + { + if (*ptr == '\\') ptr++; - *cptr=*ptr; ptr++; cptr++; + *cptr = *ptr; + ptr++; + cptr++; } - *cptr='\0'; + *cptr = '\0'; return res; } void -parse_cfgdict(text *in, Map **m) { - Map *mptr; - char *ptr=VARDATA(in), *begin=NULL; - char num=0; - int state=CS_WAITKEY; +parse_cfgdict(text *in, Map ** m) +{ + Map *mptr; + char *ptr = VARDATA(in), + *begin = NULL; + char num = 0; + int state = CS_WAITKEY; - while( ptr-VARDATA(in) < VARSIZE(in) - VARHDRSZ ) { - if ( *ptr==',' ) num++; + while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ) + { + if (*ptr == ',') + num++; ptr++; } - *m=mptr=(Map*)palloc( sizeof(Map)*(num+2) ); - memset(mptr, 0, sizeof(Map)*(num+2) ); - ptr=VARDATA(in); - while( ptr-VARDATA(in) < VARSIZE(in) - VARHDRSZ ) { - if (state==CS_WAITKEY) { - if (isalpha(*ptr)) { - begin=ptr; - state=CS_INKEY; - } else if ( !isspace(*ptr) ) + *m = mptr = (Map *) palloc(sizeof(Map) * (num + 2)); + memset(mptr, 0, sizeof(Map) * (num + 2)); + ptr = VARDATA(in); + while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ) + { + if (state == CS_WAITKEY) + { + if (isalpha(*ptr)) + { + begin = ptr; + state = CS_INKEY; + } + else if (!isspace(*ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), - errdetail("Syntax error in position %d near \"%c\"", - (int) (ptr-VARDATA(in)), *ptr))); - } else if (state==CS_INKEY) { - if ( isspace(*ptr) ) { - mptr->key=nstrdup(begin, ptr-begin); - state=CS_WAITEQ; - } else if ( *ptr=='=' ) { - mptr->key=nstrdup(begin, ptr-begin); - state=CS_WAITVALUE; - } else if ( !isalpha(*ptr) ) + errdetail("Syntax error in position %d near \"%c\"", + (int) (ptr - VARDATA(in)), *ptr))); + } + else if (state == CS_INKEY) + { + if (isspace(*ptr)) + { + mptr->key = nstrdup(begin, ptr - begin); + state = CS_WAITEQ; + } + else if (*ptr == '=') + { + mptr->key = nstrdup(begin, ptr - begin); + state = CS_WAITVALUE; + } + else if (!isalpha(*ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), - errdetail("Syntax error in position %d near \"%c\"", - (int) (ptr-VARDATA(in)), *ptr))); - } else if ( state==CS_WAITEQ ) { - if ( *ptr=='=' ) - state=CS_WAITVALUE; - else if ( !isspace(*ptr) ) + errdetail("Syntax error in position %d near \"%c\"", + (int) (ptr - VARDATA(in)), *ptr))); + } + else if (state == CS_WAITEQ) + { + if (*ptr == '=') + state = CS_WAITVALUE; + else if (!isspace(*ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), - errdetail("Syntax error in position %d near \"%c\"", - (int) (ptr-VARDATA(in)), *ptr))); - } else if ( state==CS_WAITVALUE ) { - if ( *ptr=='"' ) { - begin=ptr+1; - state=CS_INVALUE; - } else if ( !isspace(*ptr) ) { - begin=ptr; - state=CS_IN2VALUE; + errdetail("Syntax error in position %d near \"%c\"", + (int) (ptr - VARDATA(in)), *ptr))); + } + else if (state == CS_WAITVALUE) + { + if (*ptr == '"') + { + begin = ptr + 1; + state = CS_INVALUE; + } + else if (!isspace(*ptr)) + { + begin = ptr; + state = CS_IN2VALUE; } - } else if ( state==CS_INVALUE ) { - if ( *ptr=='"' ) { - mptr->value = nstrdup(begin, ptr-begin); + } + else if (state == CS_INVALUE) + { + if (*ptr == '"') + { + mptr->value = nstrdup(begin, ptr - begin); mptr++; - state=CS_WAITDELIM; - } else if ( *ptr=='\\' ) - state=CS_INESC; - } else if ( state==CS_IN2VALUE ) { - if ( isspace(*ptr) || *ptr==',' ) { - mptr->value = nstrdup(begin, ptr-begin); + state = CS_WAITDELIM; + } + else if (*ptr == '\\') + state = CS_INESC; + } + else if (state == CS_IN2VALUE) + { + if (isspace(*ptr) || *ptr == ',') + { + mptr->value = nstrdup(begin, ptr - begin); mptr++; - state=( *ptr==',' ) ? CS_WAITKEY : CS_WAITDELIM; - } else if ( *ptr=='\\' ) - state=CS_INESC; - } else if ( state==CS_WAITDELIM ) { - if ( *ptr==',' ) - state=CS_WAITKEY; - else if ( !isspace(*ptr) ) + state = (*ptr == ',') ? CS_WAITKEY : CS_WAITDELIM; + } + else if (*ptr == '\\') + state = CS_INESC; + } + else if (state == CS_WAITDELIM) + { + if (*ptr == ',') + state = CS_WAITKEY; + else if (!isspace(*ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), - errdetail("Syntax error in position %d near \"%c\"", - (int) (ptr-VARDATA(in)), *ptr))); - } else if ( state == CS_INESC ) { - state=CS_INVALUE; - } else if ( state == CS_IN2ESC ) { - state=CS_IN2VALUE; - } else + errdetail("Syntax error in position %d near \"%c\"", + (int) (ptr - VARDATA(in)), *ptr))); + } + else if (state == CS_INESC) + state = CS_INVALUE; + else if (state == CS_IN2ESC) + state = CS_IN2VALUE; + else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("bad parser state"), errdetail("%d at position %d near \"%c\"", - state, (int) (ptr-VARDATA(in)), *ptr))); + state, (int) (ptr - VARDATA(in)), *ptr))); ptr++; } - if (state==CS_IN2VALUE) { - mptr->value = nstrdup(begin, ptr-begin); + if (state == CS_IN2VALUE) + { + mptr->value = nstrdup(begin, ptr - begin); mptr++; - } else if ( !(state==CS_WAITDELIM || state==CS_WAITKEY) ) + } + else if (!(state == CS_WAITDELIM || state == CS_WAITKEY)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("unexpected end of line"))); } - - diff --git a/contrib/tsearch2/query.c b/contrib/tsearch2/query.c index 14564c62895..0019b04f166 100644 --- a/contrib/tsearch2/query.c +++ b/contrib/tsearch2/query.c @@ -99,28 +99,40 @@ typedef struct TI_IN_STATE valstate; /* tscfg */ - int cfg_id; + int cfg_id; } QPRS_STATE; -static char* -get_weight(char *buf, int2 *weight) { +static char * +get_weight(char *buf, int2 *weight) +{ *weight = 0; - if ( *buf != ':' ) + if (*buf != ':') return buf; buf++; - while( *buf ) { - switch(tolower(*buf)) { - case 'a': *weight |= 1<<3; break; - case 'b': *weight |= 1<<2; break; - case 'c': *weight |= 1<<1; break; - case 'd': *weight |= 1; break; - default: return buf; + while (*buf) + { + switch (tolower(*buf)) + { + case 'a': + *weight |= 1 << 3; + break; + case 'b': + *weight |= 1 << 2; + break; + case 'c': + *weight |= 1 << 1; + break; + case 'd': + *weight |= 1; + break; + default: + return buf; } buf++; } - + return buf; } @@ -146,11 +158,15 @@ gettoken_query(QPRS_STATE * state, int4 *val, int4 *lenval, char **strval, int2 state->count++; (state->buf)++; return OPEN; - } else if ( *(state->buf) == ':' ) { + } + else if (*(state->buf) == ':') + { ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("error at start of operand"))); - } else if (*(state->buf) != ' ') { + } + else if (*(state->buf) != ' ') + { state->valstate.prsbuf = state->buf; state->state = WAITOPERATOR; if (gettoken_tsvector(&(state->valstate))) @@ -257,7 +273,7 @@ static void pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval, int2 weight) { int4 count = 0; - PRSTEXT prs; + PRSTEXT prs; prs.lenwords = 32; prs.curwords = 0; @@ -266,16 +282,17 @@ pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval, int2 we parsetext_v2(findcfg(state->cfg_id), &prs, strval, lenval); - for(count=0;count<prs.curwords;count++) { + for (count = 0; count < prs.curwords; count++) + { pushval_asis(state, VAL, prs.words[count].word, prs.words[count].len, weight); - pfree( prs.words[count].word ); + pfree(prs.words[count].word); if (count) - pushquery(state, OPR, (int4) '&', 0, 0, 0 ); - } + pushquery(state, OPR, (int4) '&', 0, 0, 0); + } pfree(prs.words); /* XXX */ - if ( prs.curwords==0 ) + if (prs.curwords == 0) pushval_asis(state, VALTRUE, 0, 0, 0); } @@ -381,15 +398,18 @@ ValCompare(CHKVAL * chkval, WordEntry * ptr, ITEM * item) * check weight info */ static bool -checkclass_str(CHKVAL * chkval, WordEntry * val, ITEM * item) { - WordEntryPos *ptr = (WordEntryPos*) (chkval->values+val->pos+SHORTALIGN(val->len)+sizeof(uint16)); - uint16 len = *( (uint16*) (chkval->values+val->pos+SHORTALIGN(val->len)) ); - while (len--) { - if ( item->weight & ( 1<<ptr->weight ) ) +checkclass_str(CHKVAL * chkval, WordEntry * val, ITEM * item) +{ + WordEntryPos *ptr = (WordEntryPos *) (chkval->values + val->pos + SHORTALIGN(val->len) + sizeof(uint16)); + uint16 len = *((uint16 *) (chkval->values + val->pos + SHORTALIGN(val->len))); + + while (len--) + { + if (item->weight & (1 << ptr->weight)) return true; ptr++; } - return false; + return false; } /* @@ -410,8 +430,8 @@ checkcondition_str(void *checkval, ITEM * val) StopMiddle = StopLow + (StopHigh - StopLow) / 2; difference = ValCompare((CHKVAL *) checkval, StopMiddle, val); if (difference == 0) - return ( val->weight && StopMiddle->haspos ) ? - checkclass_str((CHKVAL *) checkval,StopMiddle, val) : true; + return (val->weight && StopMiddle->haspos) ? + checkclass_str((CHKVAL *) checkval, StopMiddle, val) : true; else if (difference < 0) StopLow = StopMiddle + 1; else @@ -468,7 +488,7 @@ rexectsq(PG_FUNCTION_ARGS) Datum exectsq(PG_FUNCTION_ARGS) { - tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0))); + tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0))); QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1))); CHKVAL chkval; bool result; @@ -485,10 +505,10 @@ exectsq(PG_FUNCTION_ARGS) chkval.values = STRPTR(val); chkval.operand = GETOPERAND(query); result = TS_execute( - GETQUERY(query), - &chkval, - true, - checkcondition_str + GETQUERY(query), + &chkval, + true, + checkcondition_str ); PG_FREE_IF_COPY(val, 0); @@ -534,7 +554,7 @@ findoprnd(ITEM * ptr, int4 *pos) * input */ static QUERYTYPE * -queryin(char *buf, void (*pushval) (QPRS_STATE *, int, char *, int, int2), int cfg_id) + queryin(char *buf, void (*pushval) (QPRS_STATE *, int, char *, int, int2), int cfg_id) { QPRS_STATE state; int4 i; @@ -555,7 +575,7 @@ queryin(char *buf, void (*pushval) (QPRS_STATE *, int, char *, int, int2), int c state.count = 0; state.num = 0; state.str = NULL; - state.cfg_id=cfg_id; + state.cfg_id = cfg_id; /* init value parser's state */ state.valstate.oprisdelim = true; @@ -678,12 +698,30 @@ infix(INFIX * in, bool first) } *(in->cur) = '\''; in->cur++; - if ( in->curpol->weight ) { - *(in->cur) = ':'; in->cur++; - if ( in->curpol->weight & (1<<3) ) { *(in->cur) = 'A'; in->cur++; } - if ( in->curpol->weight & (1<<2) ) { *(in->cur) = 'B'; in->cur++; } - if ( in->curpol->weight & (1<<1) ) { *(in->cur) = 'C'; in->cur++; } - if ( in->curpol->weight & 1 ) { *(in->cur) = 'D'; in->cur++; } + if (in->curpol->weight) + { + *(in->cur) = ':'; + in->cur++; + if (in->curpol->weight & (1 << 3)) + { + *(in->cur) = 'A'; + in->cur++; + } + if (in->curpol->weight & (1 << 2)) + { + *(in->cur) = 'B'; + in->cur++; + } + if (in->curpol->weight & (1 << 1)) + { + *(in->cur) = 'C'; + in->cur++; + } + if (in->curpol->weight & 1) + { + *(in->cur) = 'D'; + in->cur++; + } } *(in->cur) = '\0'; in->curpol++; @@ -827,15 +865,16 @@ tsquerytree(PG_FUNCTION_ARGS) } Datum -to_tsquery(PG_FUNCTION_ARGS) { - text *in = PG_GETARG_TEXT_P(1); - char *str; +to_tsquery(PG_FUNCTION_ARGS) +{ + text *in = PG_GETARG_TEXT_P(1); + char *str; QUERYTYPE *query; ITEM *res; int4 len; - str=text2char(in); - PG_FREE_IF_COPY(in,1); + str = text2char(in); + PG_FREE_IF_COPY(in, 1); query = queryin(str, pushval_morph, PG_GETARG_INT32(0)); res = clean_fakeval_v2(GETQUERY(query), &len); @@ -851,25 +890,25 @@ to_tsquery(PG_FUNCTION_ARGS) { } Datum -to_tsquery_name(PG_FUNCTION_ARGS) { - text *name=PG_GETARG_TEXT_P(0); - Datum res= DirectFunctionCall2( - to_tsquery, - Int32GetDatum( name2id_cfg(name) ), - PG_GETARG_DATUM(1) +to_tsquery_name(PG_FUNCTION_ARGS) +{ + text *name = PG_GETARG_TEXT_P(0); + Datum res = DirectFunctionCall2( + to_tsquery, + Int32GetDatum(name2id_cfg(name)), + PG_GETARG_DATUM(1) ); - - PG_FREE_IF_COPY(name,1); + + PG_FREE_IF_COPY(name, 1); PG_RETURN_DATUM(res); } Datum -to_tsquery_current(PG_FUNCTION_ARGS) { - PG_RETURN_DATUM( DirectFunctionCall2( - to_tsquery, - Int32GetDatum( get_currcfg() ), - PG_GETARG_DATUM(0) - )); +to_tsquery_current(PG_FUNCTION_ARGS) +{ + PG_RETURN_DATUM(DirectFunctionCall2( + to_tsquery, + Int32GetDatum(get_currcfg()), + PG_GETARG_DATUM(0) + )); } - - diff --git a/contrib/tsearch2/query.h b/contrib/tsearch2/query.h index c0715a2a037..4a79efdc539 100644 --- a/contrib/tsearch2/query.h +++ b/contrib/tsearch2/query.h @@ -16,10 +16,10 @@ typedef struct ITEM int2 left; int4 val; /* user-friendly value, must correlate with WordEntry */ - uint32 - unused:1, - length:11, - distance:20; + uint32 + unused:1, + length:11, + distance:20; } ITEM; /* @@ -50,6 +50,6 @@ typedef struct #define VALFALSE 7 bool TS_execute(ITEM * curitem, void *checkval, - bool calcnot, bool (*chkcond) (void *checkval, ITEM * val)); + bool calcnot, bool (*chkcond) (void *checkval, ITEM * val)); #endif diff --git a/contrib/tsearch2/rank.c b/contrib/tsearch2/rank.c index 0840eb83470..5b62c9810bc 100644 --- a/contrib/tsearch2/rank.c +++ b/contrib/tsearch2/rank.c @@ -37,29 +37,35 @@ Datum rank_cd_def(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(get_covers); Datum get_covers(PG_FUNCTION_ARGS); -static float weights[]={0.1, 0.2, 0.4, 1.0}; +static float weights[] = {0.1, 0.2, 0.4, 1.0}; #define wpos(wep) ( w[ ((WordEntryPos*)(wep))->weight ] ) -#define DEF_NORM_METHOD 0 +#define DEF_NORM_METHOD 0 /* * Returns a weight of a word collocation */ -static float4 word_distance ( int4 w ) { - if ( w>100 ) - return 1e-30; +static float4 +word_distance(int4 w) +{ + if (w > 100) + return 1e-30; - return 1.0/(1.005+0.05*exp( ((float4)w)/1.5-2) ); + return 1.0 / (1.005 + 0.05 * exp(((float4) w) / 1.5 - 2)); } static int -cnt_length( tsvector *t ) { - WordEntry *ptr=ARRPTR(t), *end=(WordEntry*)STRPTR(t); - int len = 0, clen; - - while(ptr < end) { - if ( (clen=POSDATALEN(t, ptr)) == 0 ) +cnt_length(tsvector * t) +{ + WordEntry *ptr = ARRPTR(t), + *end = (WordEntry *) STRPTR(t); + int len = 0, + clen; + + while (ptr < end) + { + if ((clen = POSDATALEN(t, ptr)) == 0) len += 1; else len += clen; @@ -70,191 +76,225 @@ cnt_length( tsvector *t ) { } static int4 -WordECompareITEM(char *eval, char *qval, WordEntry * ptr, ITEM * item) { - if (ptr->len == item->length) - return strncmp( - eval + ptr->pos, - qval + item->distance, - item->length); - - return (ptr->len > item->length) ? 1 : -1; +WordECompareITEM(char *eval, char *qval, WordEntry * ptr, ITEM * item) +{ + if (ptr->len == item->length) + return strncmp( + eval + ptr->pos, + qval + item->distance, + item->length); + + return (ptr->len > item->length) ? 1 : -1; } -static WordEntry* -find_wordentry(tsvector *t, QUERYTYPE *q, ITEM *item) { - WordEntry *StopLow = ARRPTR(t); - WordEntry *StopHigh = (WordEntry*)STRPTR(t); - WordEntry *StopMiddle; - int difference; - - /* Loop invariant: StopLow <= item < StopHigh */ - - while (StopLow < StopHigh) - { - StopMiddle = StopLow + (StopHigh - StopLow) / 2; - difference = WordECompareITEM(STRPTR(t), GETOPERAND(q), StopMiddle, item); - if (difference == 0) - return StopMiddle; - else if (difference < 0) - StopLow = StopMiddle + 1; - else - StopHigh = StopMiddle; - } - - return NULL; +static WordEntry * +find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item) +{ + WordEntry *StopLow = ARRPTR(t); + WordEntry *StopHigh = (WordEntry *) STRPTR(t); + WordEntry *StopMiddle; + int difference; + + /* Loop invariant: StopLow <= item < StopHigh */ + + while (StopLow < StopHigh) + { + StopMiddle = StopLow + (StopHigh - StopLow) / 2; + difference = WordECompareITEM(STRPTR(t), GETOPERAND(q), StopMiddle, item); + if (difference == 0) + return StopMiddle; + else if (difference < 0) + StopLow = StopMiddle + 1; + else + StopHigh = StopMiddle; + } + + return NULL; } -static WordEntryPos POSNULL[]={ - {0,0}, - {0,MAXENTRYPOS-1} +static WordEntryPos POSNULL[] = { + {0, 0}, + {0, MAXENTRYPOS - 1} }; static float -calc_rank_and(float *w, tsvector *t, QUERYTYPE *q) { - uint16 **pos=(uint16**)palloc(sizeof(uint16*) * q->size); - int i,k,l,p; - WordEntry *entry; - WordEntryPos *post,*ct; - int4 dimt,lenct,dist; - float res=-1.0; - ITEM *item=GETQUERY(q); - - memset(pos,0,sizeof(uint16**) * q->size); - *(uint16*)POSNULL = lengthof(POSNULL)-1; - - for(i=0; i<q->size; i++) { - - if ( item[i].type != VAL ) +calc_rank_and(float *w, tsvector * t, QUERYTYPE * q) +{ + uint16 **pos = (uint16 **) palloc(sizeof(uint16 *) * q->size); + int i, + k, + l, + p; + WordEntry *entry; + WordEntryPos *post, + *ct; + int4 dimt, + lenct, + dist; + float res = -1.0; + ITEM *item = GETQUERY(q); + + memset(pos, 0, sizeof(uint16 **) * q->size); + *(uint16 *) POSNULL = lengthof(POSNULL) - 1; + + for (i = 0; i < q->size; i++) + { + + if (item[i].type != VAL) continue; - entry=find_wordentry(t,q,&(item[i])); - if ( !entry ) + entry = find_wordentry(t, q, &(item[i])); + if (!entry) continue; - if ( entry->haspos ) - pos[i] = (uint16*)_POSDATAPTR(t,entry); + if (entry->haspos) + pos[i] = (uint16 *) _POSDATAPTR(t, entry); else - pos[i] = (uint16*)POSNULL; - - - dimt = *(uint16*)(pos[i]); - post = (WordEntryPos*)(pos[i]+1); - for( k=0; k<i; k++ ) { - if ( !pos[k] ) continue; - lenct = *(uint16*)(pos[k]); - ct = (WordEntryPos*)(pos[k]+1); - for(l=0; l<dimt; l++) { - for(p=0; p<lenct; p++) { - dist = abs( post[l].pos - ct[p].pos ); - if ( dist || (dist==0 && (pos[i]==(uint16*)POSNULL || pos[k]==(uint16*)POSNULL) ) ) { - float curw; - if ( !dist ) dist=MAXENTRYPOS; - curw= sqrt( wpos(&(post[l])) * wpos( &(ct[p]) ) * word_distance(dist) ); - res = ( res < 0 ) ? curw : 1.0 - ( 1.0 - res ) * ( 1.0 - curw ); + pos[i] = (uint16 *) POSNULL; + + + dimt = *(uint16 *) (pos[i]); + post = (WordEntryPos *) (pos[i] + 1); + for (k = 0; k < i; k++) + { + if (!pos[k]) + continue; + lenct = *(uint16 *) (pos[k]); + ct = (WordEntryPos *) (pos[k] + 1); + for (l = 0; l < dimt; l++) + { + for (p = 0; p < lenct; p++) + { + dist = abs(post[l].pos - ct[p].pos); + if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL))) + { + float curw; + + if (!dist) + dist = MAXENTRYPOS; + curw = sqrt(wpos(&(post[l])) * wpos(&(ct[p])) * word_distance(dist)); + res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw); } } } } } pfree(pos); - return res; + return res; } static float -calc_rank_or(float *w, tsvector *t, QUERYTYPE *q) { - WordEntry *entry; - WordEntryPos *post; - int4 dimt,j,i; - float res=-1.0; - ITEM *item=GETQUERY(q); - - *(uint16*)POSNULL = lengthof(POSNULL)-1; - - for(i=0; i<q->size; i++) { - if ( item[i].type != VAL ) +calc_rank_or(float *w, tsvector * t, QUERYTYPE * q) +{ + WordEntry *entry; + WordEntryPos *post; + int4 dimt, + j, + i; + float res = -1.0; + ITEM *item = GETQUERY(q); + + *(uint16 *) POSNULL = lengthof(POSNULL) - 1; + + for (i = 0; i < q->size; i++) + { + if (item[i].type != VAL) continue; - entry=find_wordentry(t,q,&(item[i])); - if ( !entry ) + entry = find_wordentry(t, q, &(item[i])); + if (!entry) continue; - if ( entry->haspos ) { - dimt = POSDATALEN(t,entry); - post = POSDATAPTR(t,entry); - } else { - dimt = *(uint16*)POSNULL; - post = POSNULL+1; + if (entry->haspos) + { + dimt = POSDATALEN(t, entry); + post = POSDATAPTR(t, entry); + } + else + { + dimt = *(uint16 *) POSNULL; + post = POSNULL + 1; } - for(j=0;j<dimt;j++) { - if ( res < 0 ) - res = wpos( &(post[j]) ); + for (j = 0; j < dimt; j++) + { + if (res < 0) + res = wpos(&(post[j])); else - res = 1.0 - ( 1.0-res ) * ( 1.0-wpos( &(post[j]) ) ); + res = 1.0 - (1.0 - res) * (1.0 - wpos(&(post[j]))); } } return res; } static float -calc_rank(float *w, tsvector *t, QUERYTYPE *q, int4 method) { - ITEM *item = GETQUERY(q); - float res=0.0; +calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method) +{ + ITEM *item = GETQUERY(q); + float res = 0.0; if (!t->size || !q->size) return 0.0; - res = ( item->type != VAL && item->val == (int4) '&' ) ? - calc_rank_and(w,t,q) : calc_rank_or(w,t,q); + res = (item->type != VAL && item->val == (int4) '&') ? + calc_rank_and(w, t, q) : calc_rank_or(w, t, q); - if ( res < 0 ) + if (res < 0) res = 1e-20; - switch(method) { - case 0: break; - case 1: res /= log((float)cnt_length(t)); break; - case 2: res /= (float)cnt_length(t); break; + switch (method) + { + case 0: + break; + case 1: + res /= log((float) cnt_length(t)); + break; + case 2: + res /= (float) cnt_length(t); + break; default: - /* internal error */ - elog(ERROR,"unrecognized normalization method: %d", method); - } + /* internal error */ + elog(ERROR, "unrecognized normalization method: %d", method); + } return res; } Datum -rank(PG_FUNCTION_ARGS) { +rank(PG_FUNCTION_ARGS) +{ ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2)); - int method=DEF_NORM_METHOD; - float res=0.0; - float ws[ lengthof(weights) ]; - int i; + int method = DEF_NORM_METHOD; + float res = 0.0; + float ws[lengthof(weights)]; + int i; - if ( ARR_NDIM(win) != 1 ) + if (ARR_NDIM(win) != 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array of weight must be one-dimensional"))); - if ( ARRNELEMS(win) < lengthof(weights) ) + if (ARRNELEMS(win) < lengthof(weights)) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array of weight is too short"))); - for(i=0;i<lengthof(weights);i++) { - ws[ i ] = ( ((float4*)ARR_DATA_PTR(win))[i] >= 0 ) ? ((float4*)ARR_DATA_PTR(win))[i] : weights[i]; - if ( ws[ i ] > 1.0 ) + for (i = 0; i < lengthof(weights); i++) + { + ws[i] = (((float4 *) ARR_DATA_PTR(win))[i] >= 0) ? ((float4 *) ARR_DATA_PTR(win))[i] : weights[i]; + if (ws[i] > 1.0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("weight out of range"))); - } + } + + if (PG_NARGS() == 4) + method = PG_GETARG_INT32(3); - if ( PG_NARGS() == 4 ) - method=PG_GETARG_INT32(3); + res = calc_rank(ws, txt, query, method); - res=calc_rank(ws, txt, query, method); - PG_FREE_IF_COPY(win, 0); PG_FREE_IF_COPY(txt, 1); PG_FREE_IF_COPY(query, 2); @@ -262,108 +302,127 @@ rank(PG_FUNCTION_ARGS) { } Datum -rank_def(PG_FUNCTION_ARGS) { - tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); +rank_def(PG_FUNCTION_ARGS) +{ + tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); - float res=0.0; - int method=DEF_NORM_METHOD; + float res = 0.0; + int method = DEF_NORM_METHOD; - if ( PG_NARGS() == 3 ) - method=PG_GETARG_INT32(2); + if (PG_NARGS() == 3) + method = PG_GETARG_INT32(2); + + res = calc_rank(weights, txt, query, method); - res=calc_rank(weights, txt, query, method); - PG_FREE_IF_COPY(txt, 0); PG_FREE_IF_COPY(query, 1); PG_RETURN_FLOAT4(res); } -typedef struct { - ITEM *item; - int32 pos; -} DocRepresentation; +typedef struct +{ + ITEM *item; + int32 pos; +} DocRepresentation; static int -compareDocR(const void *a, const void *b) { - if ( ((DocRepresentation *) a)->pos == ((DocRepresentation *) b)->pos ) +compareDocR(const void *a, const void *b) +{ + if (((DocRepresentation *) a)->pos == ((DocRepresentation *) b)->pos) return 1; - return ( ((DocRepresentation *) a)->pos > ((DocRepresentation *) b)->pos ) ? 1 : -1; + return (((DocRepresentation *) a)->pos > ((DocRepresentation *) b)->pos) ? 1 : -1; } -typedef struct { +typedef struct +{ DocRepresentation *doc; - int len; + int len; } ChkDocR; static bool -checkcondition_DR(void *checkval, ITEM *val) { - DocRepresentation *ptr = ((ChkDocR*)checkval)->doc; +checkcondition_DR(void *checkval, ITEM * val) +{ + DocRepresentation *ptr = ((ChkDocR *) checkval)->doc; - while( ptr - ((ChkDocR*)checkval)->doc < ((ChkDocR*)checkval)->len ) { - if ( val == ptr->item ) + while (ptr - ((ChkDocR *) checkval)->doc < ((ChkDocR *) checkval)->len) + { + if (val == ptr->item) return true; ptr++; - } + } return false; } static bool -Cover(DocRepresentation *doc, int len, QUERYTYPE *query, int *pos, int *p, int *q) { - int i; - DocRepresentation *ptr,*f=(DocRepresentation*)0xffffffff; - ITEM *item=GETQUERY(query); - int lastpos=*pos; - int oldq=*q; - - *p=0x7fffffff; - *q=0; - - for(i=0; i<query->size; i++) { - if ( item->type != VAL ) { +Cover(DocRepresentation * doc, int len, QUERYTYPE * query, int *pos, int *p, int *q) +{ + int i; + DocRepresentation *ptr, + *f = (DocRepresentation *) 0xffffffff; + ITEM *item = GETQUERY(query); + int lastpos = *pos; + int oldq = *q; + + *p = 0x7fffffff; + *q = 0; + + for (i = 0; i < query->size; i++) + { + if (item->type != VAL) + { item++; continue; } ptr = doc + *pos; - while(ptr-doc<len) { - if ( ptr->item == item ) { - if ( ptr->pos > *q ) { + while (ptr - doc < len) + { + if (ptr->item == item) + { + if (ptr->pos > *q) + { *q = ptr->pos; - lastpos= ptr - doc; - } + lastpos = ptr - doc; + } break; - } + } ptr++; } item++; } - if (*q==0 ) + if (*q == 0) return false; - if (*q==oldq) { /* already check this pos */ + if (*q == oldq) + { /* already check this pos */ (*pos)++; - return Cover(doc, len, query, pos,p,q); - } + return Cover(doc, len, query, pos, p, q); + } - item=GETQUERY(query); - for(i=0; i<query->size; i++) { - if ( item->type != VAL ) { + item = GETQUERY(query); + for (i = 0; i < query->size; i++) + { + if (item->type != VAL) + { item++; continue; } ptr = doc + lastpos; - while(ptr>=doc+*pos) { - if ( ptr->item == item ) { - if ( ptr->pos < *p ) { + while (ptr >= doc + *pos) + { + if (ptr->item == item) + { + if (ptr->pos < *p) + { *p = ptr->pos; - f=ptr; + f = ptr; } break; } @@ -371,106 +430,135 @@ Cover(DocRepresentation *doc, int len, QUERYTYPE *query, int *pos, int *p, int * } item++; } - - if ( *p<=*q ) { - ChkDocR ch = { f, (doc + lastpos)-f+1 }; - *pos = f-doc+1; - if ( TS_execute(GETQUERY(query), &ch, false, checkcondition_DR) ) { - /*elog(NOTICE,"OP:%d NP:%d P:%d Q:%d", *pos, lastpos, *p, *q);*/ + + if (*p <= *q) + { + ChkDocR ch = {f, (doc + lastpos) - f + 1}; + + *pos = f - doc + 1; + if (TS_execute(GETQUERY(query), &ch, false, checkcondition_DR)) + { + /* + * elog(NOTICE,"OP:%d NP:%d P:%d Q:%d", *pos, lastpos, *p, + * *q); + */ return true; - } else - return Cover(doc, len, query, pos,p,q); + } + else + return Cover(doc, len, query, pos, p, q); } - + return false; } -static DocRepresentation* -get_docrep(tsvector *txt, QUERYTYPE *query, int *doclen) { - ITEM *item=GETQUERY(query); - WordEntry *entry; - WordEntryPos *post; - int4 dimt,j,i; - int len=query->size*4,cur=0; +static DocRepresentation * +get_docrep(tsvector * txt, QUERYTYPE * query, int *doclen) +{ + ITEM *item = GETQUERY(query); + WordEntry *entry; + WordEntryPos *post; + int4 dimt, + j, + i; + int len = query->size * 4, + cur = 0; DocRepresentation *doc; - *(uint16*)POSNULL = lengthof(POSNULL)-1; - doc = (DocRepresentation*)palloc(sizeof(DocRepresentation)*len); - for(i=0; i<query->size; i++) { - if ( item[i].type != VAL ) + *(uint16 *) POSNULL = lengthof(POSNULL) - 1; + doc = (DocRepresentation *) palloc(sizeof(DocRepresentation) * len); + for (i = 0; i < query->size; i++) + { + if (item[i].type != VAL) continue; - entry=find_wordentry(txt,query,&(item[i])); - if ( !entry ) + entry = find_wordentry(txt, query, &(item[i])); + if (!entry) continue; - if ( entry->haspos ) { - dimt = POSDATALEN(txt,entry); - post = POSDATAPTR(txt,entry); - } else { - dimt = *(uint16*)POSNULL; - post = POSNULL+1; + if (entry->haspos) + { + dimt = POSDATALEN(txt, entry); + post = POSDATAPTR(txt, entry); + } + else + { + dimt = *(uint16 *) POSNULL; + post = POSNULL + 1; } - while( cur+dimt >= len ) { - len*=2; - doc = (DocRepresentation*)repalloc(doc,sizeof(DocRepresentation)*len); + while (cur + dimt >= len) + { + len *= 2; + doc = (DocRepresentation *) repalloc(doc, sizeof(DocRepresentation) * len); } - for(j=0;j<dimt;j++) { - doc[cur].item=&(item[i]); - doc[cur].pos=post[j].pos; + for (j = 0; j < dimt; j++) + { + doc[cur].item = &(item[i]); + doc[cur].pos = post[j].pos; cur++; } } - *doclen=cur; - - if ( cur>0 ) { - if ( cur>1 ) + *doclen = cur; + + if (cur > 0) + { + if (cur > 1) qsort((void *) doc, cur, sizeof(DocRepresentation), compareDocR); return doc; } - + pfree(doc); return NULL; } Datum -rank_cd(PG_FUNCTION_ARGS) { - int K = PG_GETARG_INT32(0); - tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); +rank_cd(PG_FUNCTION_ARGS) +{ + int K = PG_GETARG_INT32(0); + tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2)); - int method=DEF_NORM_METHOD; - DocRepresentation *doc; - float res=0.0; - int p=0,q=0,len,cur; + int method = DEF_NORM_METHOD; + DocRepresentation *doc; + float res = 0.0; + int p = 0, + q = 0, + len, + cur; doc = get_docrep(txt, query, &len); - if ( !doc ) { + if (!doc) + { PG_FREE_IF_COPY(txt, 1); PG_FREE_IF_COPY(query, 2); PG_RETURN_FLOAT4(0.0); } - cur=0; - if (K<=0) - K=4; - while( Cover(doc, len, query, &cur, &p, &q) ) - res += ( q-p+1 > K ) ? ((float)K)/((float)(q-p+1)) : 1.0; - - if ( PG_NARGS() == 4 ) - method=PG_GETARG_INT32(3); - - switch(method) { - case 0: break; - case 1: res /= log((float)cnt_length(txt)); break; - case 2: res /= (float)cnt_length(txt); break; + cur = 0; + if (K <= 0) + K = 4; + while (Cover(doc, len, query, &cur, &p, &q)) + res += (q - p + 1 > K) ? ((float) K) / ((float) (q - p + 1)) : 1.0; + + if (PG_NARGS() == 4) + method = PG_GETARG_INT32(3); + + switch (method) + { + case 0: + break; + case 1: + res /= log((float) cnt_length(txt)); + break; + case 2: + res /= (float) cnt_length(txt); + break; default: - /* internal error */ - elog(ERROR,"unrecognized normalization method: %d", method); - } + /* internal error */ + elog(ERROR, "unrecognized normalization method: %d", method); + } pfree(doc); PG_FREE_IF_COPY(txt, 1); @@ -481,120 +569,141 @@ rank_cd(PG_FUNCTION_ARGS) { Datum -rank_cd_def(PG_FUNCTION_ARGS) { - PG_RETURN_DATUM( DirectFunctionCall4( - rank_cd, - Int32GetDatum(-1), - PG_GETARG_DATUM(0), - PG_GETARG_DATUM(1), - ( PG_NARGS() == 3 ) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD) - )); +rank_cd_def(PG_FUNCTION_ARGS) +{ + PG_RETURN_DATUM(DirectFunctionCall4( + rank_cd, + Int32GetDatum(-1), + PG_GETARG_DATUM(0), + PG_GETARG_DATUM(1), + (PG_NARGS() == 3) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD) + )); } /**************debug*************/ -typedef struct { - char *w; - int2 len; - int2 pos; - int2 start; - int2 finish; -} DocWord; +typedef struct +{ + char *w; + int2 len; + int2 pos; + int2 start; + int2 finish; +} DocWord; static int -compareDocWord(const void *a, const void *b) { - if ( ((DocWord *) a)->pos == ((DocWord *) b)->pos ) +compareDocWord(const void *a, const void *b) +{ + if (((DocWord *) a)->pos == ((DocWord *) b)->pos) return 1; - return ( ((DocWord *) a)->pos > ((DocWord *) b)->pos ) ? 1 : -1; + return (((DocWord *) a)->pos > ((DocWord *) b)->pos) ? 1 : -1; } -Datum -get_covers(PG_FUNCTION_ARGS) { - tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); +Datum +get_covers(PG_FUNCTION_ARGS) +{ + tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); - WordEntry *pptr=ARRPTR(txt); - int i,dlen=0,j,cur=0,len=0,rlen; - DocWord *dw,*dwptr; - text *out; - char *cptr; + WordEntry *pptr = ARRPTR(txt); + int i, + dlen = 0, + j, + cur = 0, + len = 0, + rlen; + DocWord *dw, + *dwptr; + text *out; + char *cptr; DocRepresentation *doc; - int pos=0,p,q,olddwpos=0; - int ncover=1; + int pos = 0, + p, + q, + olddwpos = 0; + int ncover = 1; doc = get_docrep(txt, query, &rlen); - if ( !doc ) { - out=palloc(VARHDRSZ); + if (!doc) + { + out = palloc(VARHDRSZ); VARATT_SIZEP(out) = VARHDRSZ; - PG_FREE_IF_COPY(txt,0); - PG_FREE_IF_COPY(query,1); + PG_FREE_IF_COPY(txt, 0); + PG_FREE_IF_COPY(query, 1); PG_RETURN_POINTER(out); } - for(i=0;i<txt->size;i++) { + for (i = 0; i < txt->size; i++) + { if (!pptr[i].haspos) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("no pos info"))); - dlen += POSDATALEN(txt,&(pptr[i])); + dlen += POSDATALEN(txt, &(pptr[i])); } - dwptr=dw=palloc(sizeof(DocWord)*dlen); - memset(dw,0,sizeof(DocWord)*dlen); + dwptr = dw = palloc(sizeof(DocWord) * dlen); + memset(dw, 0, sizeof(DocWord) * dlen); + + for (i = 0; i < txt->size; i++) + { + WordEntryPos *posdata = POSDATAPTR(txt, &(pptr[i])); - for(i=0;i<txt->size;i++) { - WordEntryPos *posdata = POSDATAPTR(txt,&(pptr[i])); - for(j=0;j<POSDATALEN(txt,&(pptr[i]));j++) { - dw[cur].w=STRPTR(txt)+pptr[i].pos; - dw[cur].len=pptr[i].len; - dw[cur].pos=posdata[j].pos; + for (j = 0; j < POSDATALEN(txt, &(pptr[i])); j++) + { + dw[cur].w = STRPTR(txt) + pptr[i].pos; + dw[cur].len = pptr[i].len; + dw[cur].pos = posdata[j].pos; cur++; } - len+=(pptr[i].len + 1) * (int)POSDATALEN(txt,&(pptr[i])); + len += (pptr[i].len + 1) * (int) POSDATALEN(txt, &(pptr[i])); } qsort((void *) dw, dlen, sizeof(DocWord), compareDocWord); - while( Cover(doc, rlen, query, &pos, &p, &q) ) { - dwptr=dw+olddwpos; - while(dwptr->pos < p && dwptr-dw<dlen) + while (Cover(doc, rlen, query, &pos, &p, &q)) + { + dwptr = dw + olddwpos; + while (dwptr->pos < p && dwptr - dw < dlen) dwptr++; - olddwpos=dwptr-dw; - dwptr->start=ncover; - while(dwptr->pos < q+1 && dwptr-dw<dlen) + olddwpos = dwptr - dw; + dwptr->start = ncover; + while (dwptr->pos < q + 1 && dwptr - dw < dlen) dwptr++; - (dwptr-1)->finish=ncover; - len+= 4 /* {}+two spaces */ + 2*16 /*numbers*/; - ncover++; - } - - out=palloc(VARHDRSZ+len); - cptr=((char*)out)+VARHDRSZ; - dwptr=dw; - - while( dwptr-dw < dlen) { - if ( dwptr->start ) { - sprintf(cptr,"{%d ",dwptr->start); - cptr=strchr(cptr,'\0'); + (dwptr - 1)->finish = ncover; + len += 4 /* {}+two spaces */ + 2 * 16 /* numbers */ ; + ncover++; + } + + out = palloc(VARHDRSZ + len); + cptr = ((char *) out) + VARHDRSZ; + dwptr = dw; + + while (dwptr - dw < dlen) + { + if (dwptr->start) + { + sprintf(cptr, "{%d ", dwptr->start); + cptr = strchr(cptr, '\0'); } - memcpy(cptr,dwptr->w,dwptr->len); - cptr+=dwptr->len; - *cptr=' '; + memcpy(cptr, dwptr->w, dwptr->len); + cptr += dwptr->len; + *cptr = ' '; cptr++; - if ( dwptr->finish ) { - sprintf(cptr,"}%d ",dwptr->finish); - cptr=strchr(cptr,'\0'); + if (dwptr->finish) + { + sprintf(cptr, "}%d ", dwptr->finish); + cptr = strchr(cptr, '\0'); } dwptr++; - } + } + + VARATT_SIZEP(out) = cptr - ((char *) out); - VARATT_SIZEP(out) = cptr - ((char*)out); - pfree(dw); pfree(doc); - PG_FREE_IF_COPY(txt,0); - PG_FREE_IF_COPY(query,1); + PG_FREE_IF_COPY(txt, 0); + PG_FREE_IF_COPY(query, 1); PG_RETURN_POINTER(out); } - diff --git a/contrib/tsearch2/snmap.c b/contrib/tsearch2/snmap.c index 023c9eb5f9f..2cd3f53e497 100644 --- a/contrib/tsearch2/snmap.c +++ b/contrib/tsearch2/snmap.c @@ -1,4 +1,4 @@ -/* +/* * simple but fast map from str to Oid * Teodor Sigaev <teodor@sigaev.ru> */ @@ -11,69 +11,85 @@ #include "common.h" static int -compareSNMapEntry(const void *a, const void *b) { - return strcmp( ((SNMapEntry*)a)->key, ((SNMapEntry*)b)->key ); +compareSNMapEntry(const void *a, const void *b) +{ + return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key); } -void -addSNMap( SNMap *map, char *key, Oid value ) { - if (map->len>=map->reallen) { +void +addSNMap(SNMap * map, char *key, Oid value) +{ + if (map->len >= map->reallen) + { SNMapEntry *tmp; - int len = (map->reallen) ? 2*map->reallen : 16; - tmp=(SNMapEntry*)realloc(map->list, sizeof(SNMapEntry) * len); - if ( !tmp ) + int len = (map->reallen) ? 2 * map->reallen : 16; + + tmp = (SNMapEntry *) realloc(map->list, sizeof(SNMapEntry) * len); + if (!tmp) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - map->reallen=len; - map->list=tmp; + map->reallen = len; + map->list = tmp; } - map->list[ map->len ].key = strdup(key); - if ( ! map->list[ map->len ].key ) + map->list[map->len].key = strdup(key); + if (!map->list[map->len].key) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - map->list[ map->len ].value=value; + map->list[map->len].value = value; map->len++; - if ( map->len>1 ) qsort(map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry); + if (map->len > 1) + qsort(map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry); } -void -addSNMap_t( SNMap *map, text *key, Oid value ) { - char *k=text2char( key ); +void +addSNMap_t(SNMap * map, text *key, Oid value) +{ + char *k = text2char(key); + addSNMap(map, k, value); pfree(k); } -Oid -findSNMap( SNMap *map, char *key ) { +Oid +findSNMap(SNMap * map, char *key) +{ SNMapEntry *ptr; - SNMapEntry ks = {key, 0}; - if ( map->len==0 || !map->list ) - return 0; - ptr = (SNMapEntry*) bsearch(&ks, map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry); + SNMapEntry ks = {key, 0}; + + if (map->len == 0 || !map->list) + return 0; + ptr = (SNMapEntry *) bsearch(&ks, map->list, map->len, sizeof(SNMapEntry), compareSNMapEntry); return (ptr) ? ptr->value : 0; } -Oid -findSNMap_t( SNMap *map, text *key ) { - char *k=text2char(key); - int res; - res= findSNMap(map, k); +Oid +findSNMap_t(SNMap * map, text *key) +{ + char *k = text2char(key); + int res; + + res = findSNMap(map, k); pfree(k); return res; } -void freeSNMap( SNMap *map ) { - SNMapEntry *entry=map->list; - if ( map->list ) { - while( map->len ) { - if ( entry->key ) free(entry->key); - entry++; map->len--; +void +freeSNMap(SNMap * map) +{ + SNMapEntry *entry = map->list; + + if (map->list) + { + while (map->len) + { + if (entry->key) + free(entry->key); + entry++; + map->len--; } - free( map->list ); + free(map->list); } - memset(map,0,sizeof(SNMap)); + memset(map, 0, sizeof(SNMap)); } - - diff --git a/contrib/tsearch2/snmap.h b/contrib/tsearch2/snmap.h index b4856019c9e..ae188b66a4c 100644 --- a/contrib/tsearch2/snmap.h +++ b/contrib/tsearch2/snmap.h @@ -3,21 +3,23 @@ #include "postgres.h" -typedef struct { - char *key; - Oid value; -} SNMapEntry; +typedef struct +{ + char *key; + Oid value; +} SNMapEntry; -typedef struct { - int len; - int reallen; - SNMapEntry *list; -} SNMap; +typedef struct +{ + int len; + int reallen; + SNMapEntry *list; +} SNMap; -void addSNMap( SNMap *map, char *key, Oid value ); -void addSNMap_t( SNMap *map, text *key, Oid value ); -Oid findSNMap( SNMap *map, char *key ); -Oid findSNMap_t( SNMap *map, text *key ); -void freeSNMap( SNMap *map ); +void addSNMap(SNMap * map, char *key, Oid value); +void addSNMap_t(SNMap * map, text *key, Oid value); +Oid findSNMap(SNMap * map, char *key); +Oid findSNMap_t(SNMap * map, text *key); +void freeSNMap(SNMap * map); #endif diff --git a/contrib/tsearch2/snowball/api.c b/contrib/tsearch2/snowball/api.c index 27bf31a588e..5cbf37d73bf 100644 --- a/contrib/tsearch2/snowball/api.c +++ b/contrib/tsearch2/snowball/api.c @@ -2,48 +2,64 @@ #include "header.h" -extern struct SN_env * SN_create_env(int S_size, int I_size, int B_size) -{ struct SN_env * z = (struct SN_env *) calloc(1, sizeof(struct SN_env)); - z->p = create_s(); - if (S_size) - { z->S = (symbol * *) calloc(S_size, sizeof(symbol *)); - { int i; - for (i = 0; i < S_size; i++) z->S[i] = create_s(); - } - z->S_size = S_size; - } - - if (I_size) - { z->I = (int *) calloc(I_size, sizeof(int)); - z->I_size = I_size; - } - - if (B_size) - { z->B = (symbol *) calloc(B_size, sizeof(symbol)); - z->B_size = B_size; - } - - return z; +extern struct SN_env * +SN_create_env(int S_size, int I_size, int B_size) +{ + struct SN_env *z = (struct SN_env *) calloc(1, sizeof(struct SN_env)); + + z->p = create_s(); + if (S_size) + { + z->S = (symbol * *) calloc(S_size, sizeof(symbol *)); + { + int i; + + for (i = 0; i < S_size; i++) + z->S[i] = create_s(); + } + z->S_size = S_size; + } + + if (I_size) + { + z->I = (int *) calloc(I_size, sizeof(int)); + z->I_size = I_size; + } + + if (B_size) + { + z->B = (symbol *) calloc(B_size, sizeof(symbol)); + z->B_size = B_size; + } + + return z; } -extern void SN_close_env(struct SN_env * z) +extern void +SN_close_env(struct SN_env * z) { - if (z->S_size) - { - { int i; - for (i = 0; i < z->S_size; i++) lose_s(z->S[i]); - } - free(z->S); - } - if (z->I_size) free(z->I); - if (z->B_size) free(z->B); - if (z->p) lose_s(z->p); - free(z); + if (z->S_size) + { + { + int i; + + for (i = 0; i < z->S_size; i++) + lose_s(z->S[i]); + } + free(z->S); + } + if (z->I_size) + free(z->I); + if (z->B_size) + free(z->B); + if (z->p) + lose_s(z->p); + free(z); } -extern void SN_set_current(struct SN_env * z, int size, const symbol * s) +extern void +SN_set_current(struct SN_env * z, int size, const symbol * s) { - replace_s(z, 0, z->l, size, s); - z->c = 0; + replace_s(z, 0, z->l, size, s); + z->c = 0; } - diff --git a/contrib/tsearch2/snowball/api.h b/contrib/tsearch2/snowball/api.h index 3e8b6e18517..efad537b9e8 100644 --- a/contrib/tsearch2/snowball/api.h +++ b/contrib/tsearch2/snowball/api.h @@ -11,17 +11,24 @@ typedef unsigned char symbol; */ -struct SN_env { - symbol * p; - int c; int a; int l; int lb; int bra; int ket; - int S_size; int I_size; int B_size; - symbol * * S; - int * I; - symbol * B; +struct SN_env +{ + symbol *p; + int c; + int a; + int l; + int lb; + int bra; + int ket; + int S_size; + int I_size; + int B_size; + symbol **S; + int *I; + symbol *B; }; -extern struct SN_env * SN_create_env(int S_size, int I_size, int B_size); +extern struct SN_env *SN_create_env(int S_size, int I_size, int B_size); extern void SN_close_env(struct SN_env * z); extern void SN_set_current(struct SN_env * z, int size, const symbol * s); - diff --git a/contrib/tsearch2/snowball/english_stem.c b/contrib/tsearch2/snowball/english_stem.c index f6ceb5c576f..310eddf97d8 100644 --- a/contrib/tsearch2/snowball/english_stem.c +++ b/contrib/tsearch2/snowball/english_stem.c @@ -3,891 +3,1103 @@ #include "header.h" -extern int english_stem(struct SN_env * z); -static int r_exception2(struct SN_env * z); -static int r_exception1(struct SN_env * z); -static int r_Step_5(struct SN_env * z); -static int r_Step_4(struct SN_env * z); -static int r_Step_3(struct SN_env * z); -static int r_Step_2(struct SN_env * z); -static int r_Step_1c(struct SN_env * z); -static int r_Step_1b(struct SN_env * z); -static int r_Step_1a(struct SN_env * z); -static int r_R2(struct SN_env * z); -static int r_R1(struct SN_env * z); -static int r_shortv(struct SN_env * z); -static int r_mark_regions(struct SN_env * z); -static int r_postlude(struct SN_env * z); -static int r_prelude(struct SN_env * z); - -extern struct SN_env * english_create_env(void); +extern int english_stem(struct SN_env * z); +static int r_exception2(struct SN_env * z); +static int r_exception1(struct SN_env * z); +static int r_Step_5(struct SN_env * z); +static int r_Step_4(struct SN_env * z); +static int r_Step_3(struct SN_env * z); +static int r_Step_2(struct SN_env * z); +static int r_Step_1c(struct SN_env * z); +static int r_Step_1b(struct SN_env * z); +static int r_Step_1a(struct SN_env * z); +static int r_R2(struct SN_env * z); +static int r_R1(struct SN_env * z); +static int r_shortv(struct SN_env * z); +static int r_mark_regions(struct SN_env * z); +static int r_postlude(struct SN_env * z); +static int r_prelude(struct SN_env * z); + +extern struct SN_env *english_create_env(void); extern void english_close_env(struct SN_env * z); -static symbol s_0_0[5] = { 'g', 'e', 'n', 'e', 'r' }; +static symbol s_0_0[5] = {'g', 'e', 'n', 'e', 'r'}; static struct among a_0[1] = { -/* 0 */ { 5, s_0_0, -1, -1, 0} + /* 0 */ {5, s_0_0, -1, -1, 0} }; -static symbol s_1_0[3] = { 'i', 'e', 'd' }; -static symbol s_1_1[1] = { 's' }; -static symbol s_1_2[3] = { 'i', 'e', 's' }; -static symbol s_1_3[4] = { 's', 's', 'e', 's' }; -static symbol s_1_4[2] = { 's', 's' }; -static symbol s_1_5[2] = { 'u', 's' }; +static symbol s_1_0[3] = {'i', 'e', 'd'}; +static symbol s_1_1[1] = {'s'}; +static symbol s_1_2[3] = {'i', 'e', 's'}; +static symbol s_1_3[4] = {'s', 's', 'e', 's'}; +static symbol s_1_4[2] = {'s', 's'}; +static symbol s_1_5[2] = {'u', 's'}; static struct among a_1[6] = { -/* 0 */ { 3, s_1_0, -1, 2, 0}, -/* 1 */ { 1, s_1_1, -1, 3, 0}, -/* 2 */ { 3, s_1_2, 1, 2, 0}, -/* 3 */ { 4, s_1_3, 1, 1, 0}, -/* 4 */ { 2, s_1_4, 1, -1, 0}, -/* 5 */ { 2, s_1_5, 1, -1, 0} + /* 0 */ {3, s_1_0, -1, 2, 0}, + /* 1 */ {1, s_1_1, -1, 3, 0}, + /* 2 */ {3, s_1_2, 1, 2, 0}, + /* 3 */ {4, s_1_3, 1, 1, 0}, + /* 4 */ {2, s_1_4, 1, -1, 0}, + /* 5 */ {2, s_1_5, 1, -1, 0} }; -static symbol s_2_1[2] = { 'b', 'b' }; -static symbol s_2_2[2] = { 'd', 'd' }; -static symbol s_2_3[2] = { 'f', 'f' }; -static symbol s_2_4[2] = { 'g', 'g' }; -static symbol s_2_5[2] = { 'b', 'l' }; -static symbol s_2_6[2] = { 'm', 'm' }; -static symbol s_2_7[2] = { 'n', 'n' }; -static symbol s_2_8[2] = { 'p', 'p' }; -static symbol s_2_9[2] = { 'r', 'r' }; -static symbol s_2_10[2] = { 'a', 't' }; -static symbol s_2_11[2] = { 't', 't' }; -static symbol s_2_12[2] = { 'i', 'z' }; +static symbol s_2_1[2] = {'b', 'b'}; +static symbol s_2_2[2] = {'d', 'd'}; +static symbol s_2_3[2] = {'f', 'f'}; +static symbol s_2_4[2] = {'g', 'g'}; +static symbol s_2_5[2] = {'b', 'l'}; +static symbol s_2_6[2] = {'m', 'm'}; +static symbol s_2_7[2] = {'n', 'n'}; +static symbol s_2_8[2] = {'p', 'p'}; +static symbol s_2_9[2] = {'r', 'r'}; +static symbol s_2_10[2] = {'a', 't'}; +static symbol s_2_11[2] = {'t', 't'}; +static symbol s_2_12[2] = {'i', 'z'}; static struct among a_2[13] = { -/* 0 */ { 0, 0, -1, 3, 0}, -/* 1 */ { 2, s_2_1, 0, 2, 0}, -/* 2 */ { 2, s_2_2, 0, 2, 0}, -/* 3 */ { 2, s_2_3, 0, 2, 0}, -/* 4 */ { 2, s_2_4, 0, 2, 0}, -/* 5 */ { 2, s_2_5, 0, 1, 0}, -/* 6 */ { 2, s_2_6, 0, 2, 0}, -/* 7 */ { 2, s_2_7, 0, 2, 0}, -/* 8 */ { 2, s_2_8, 0, 2, 0}, -/* 9 */ { 2, s_2_9, 0, 2, 0}, -/* 10 */ { 2, s_2_10, 0, 1, 0}, -/* 11 */ { 2, s_2_11, 0, 2, 0}, -/* 12 */ { 2, s_2_12, 0, 1, 0} + /* 0 */ {0, 0, -1, 3, 0}, + /* 1 */ {2, s_2_1, 0, 2, 0}, + /* 2 */ {2, s_2_2, 0, 2, 0}, + /* 3 */ {2, s_2_3, 0, 2, 0}, + /* 4 */ {2, s_2_4, 0, 2, 0}, + /* 5 */ {2, s_2_5, 0, 1, 0}, + /* 6 */ {2, s_2_6, 0, 2, 0}, + /* 7 */ {2, s_2_7, 0, 2, 0}, + /* 8 */ {2, s_2_8, 0, 2, 0}, + /* 9 */ {2, s_2_9, 0, 2, 0}, + /* 10 */ {2, s_2_10, 0, 1, 0}, + /* 11 */ {2, s_2_11, 0, 2, 0}, + /* 12 */ {2, s_2_12, 0, 1, 0} }; -static symbol s_3_0[2] = { 'e', 'd' }; -static symbol s_3_1[3] = { 'e', 'e', 'd' }; -static symbol s_3_2[3] = { 'i', 'n', 'g' }; -static symbol s_3_3[4] = { 'e', 'd', 'l', 'y' }; -static symbol s_3_4[5] = { 'e', 'e', 'd', 'l', 'y' }; -static symbol s_3_5[5] = { 'i', 'n', 'g', 'l', 'y' }; +static symbol s_3_0[2] = {'e', 'd'}; +static symbol s_3_1[3] = {'e', 'e', 'd'}; +static symbol s_3_2[3] = {'i', 'n', 'g'}; +static symbol s_3_3[4] = {'e', 'd', 'l', 'y'}; +static symbol s_3_4[5] = {'e', 'e', 'd', 'l', 'y'}; +static symbol s_3_5[5] = {'i', 'n', 'g', 'l', 'y'}; static struct among a_3[6] = { -/* 0 */ { 2, s_3_0, -1, 2, 0}, -/* 1 */ { 3, s_3_1, 0, 1, 0}, -/* 2 */ { 3, s_3_2, -1, 2, 0}, -/* 3 */ { 4, s_3_3, -1, 2, 0}, -/* 4 */ { 5, s_3_4, 3, 1, 0}, -/* 5 */ { 5, s_3_5, -1, 2, 0} + /* 0 */ {2, s_3_0, -1, 2, 0}, + /* 1 */ {3, s_3_1, 0, 1, 0}, + /* 2 */ {3, s_3_2, -1, 2, 0}, + /* 3 */ {4, s_3_3, -1, 2, 0}, + /* 4 */ {5, s_3_4, 3, 1, 0}, + /* 5 */ {5, s_3_5, -1, 2, 0} }; -static symbol s_4_0[4] = { 'a', 'n', 'c', 'i' }; -static symbol s_4_1[4] = { 'e', 'n', 'c', 'i' }; -static symbol s_4_2[3] = { 'o', 'g', 'i' }; -static symbol s_4_3[2] = { 'l', 'i' }; -static symbol s_4_4[3] = { 'b', 'l', 'i' }; -static symbol s_4_5[4] = { 'a', 'b', 'l', 'i' }; -static symbol s_4_6[4] = { 'a', 'l', 'l', 'i' }; -static symbol s_4_7[5] = { 'f', 'u', 'l', 'l', 'i' }; -static symbol s_4_8[6] = { 'l', 'e', 's', 's', 'l', 'i' }; -static symbol s_4_9[5] = { 'o', 'u', 's', 'l', 'i' }; -static symbol s_4_10[5] = { 'e', 'n', 't', 'l', 'i' }; -static symbol s_4_11[5] = { 'a', 'l', 'i', 't', 'i' }; -static symbol s_4_12[6] = { 'b', 'i', 'l', 'i', 't', 'i' }; -static symbol s_4_13[5] = { 'i', 'v', 'i', 't', 'i' }; -static symbol s_4_14[6] = { 't', 'i', 'o', 'n', 'a', 'l' }; -static symbol s_4_15[7] = { 'a', 't', 'i', 'o', 'n', 'a', 'l' }; -static symbol s_4_16[5] = { 'a', 'l', 'i', 's', 'm' }; -static symbol s_4_17[5] = { 'a', 't', 'i', 'o', 'n' }; -static symbol s_4_18[7] = { 'i', 'z', 'a', 't', 'i', 'o', 'n' }; -static symbol s_4_19[4] = { 'i', 'z', 'e', 'r' }; -static symbol s_4_20[4] = { 'a', 't', 'o', 'r' }; -static symbol s_4_21[7] = { 'i', 'v', 'e', 'n', 'e', 's', 's' }; -static symbol s_4_22[7] = { 'f', 'u', 'l', 'n', 'e', 's', 's' }; -static symbol s_4_23[7] = { 'o', 'u', 's', 'n', 'e', 's', 's' }; +static symbol s_4_0[4] = {'a', 'n', 'c', 'i'}; +static symbol s_4_1[4] = {'e', 'n', 'c', 'i'}; +static symbol s_4_2[3] = {'o', 'g', 'i'}; +static symbol s_4_3[2] = {'l', 'i'}; +static symbol s_4_4[3] = {'b', 'l', 'i'}; +static symbol s_4_5[4] = {'a', 'b', 'l', 'i'}; +static symbol s_4_6[4] = {'a', 'l', 'l', 'i'}; +static symbol s_4_7[5] = {'f', 'u', 'l', 'l', 'i'}; +static symbol s_4_8[6] = {'l', 'e', 's', 's', 'l', 'i'}; +static symbol s_4_9[5] = {'o', 'u', 's', 'l', 'i'}; +static symbol s_4_10[5] = {'e', 'n', 't', 'l', 'i'}; +static symbol s_4_11[5] = {'a', 'l', 'i', 't', 'i'}; +static symbol s_4_12[6] = {'b', 'i', 'l', 'i', 't', 'i'}; +static symbol s_4_13[5] = {'i', 'v', 'i', 't', 'i'}; +static symbol s_4_14[6] = {'t', 'i', 'o', 'n', 'a', 'l'}; +static symbol s_4_15[7] = {'a', 't', 'i', 'o', 'n', 'a', 'l'}; +static symbol s_4_16[5] = {'a', 'l', 'i', 's', 'm'}; +static symbol s_4_17[5] = {'a', 't', 'i', 'o', 'n'}; +static symbol s_4_18[7] = {'i', 'z', 'a', 't', 'i', 'o', 'n'}; +static symbol s_4_19[4] = {'i', 'z', 'e', 'r'}; +static symbol s_4_20[4] = {'a', 't', 'o', 'r'}; +static symbol s_4_21[7] = {'i', 'v', 'e', 'n', 'e', 's', 's'}; +static symbol s_4_22[7] = {'f', 'u', 'l', 'n', 'e', 's', 's'}; +static symbol s_4_23[7] = {'o', 'u', 's', 'n', 'e', 's', 's'}; static struct among a_4[24] = { -/* 0 */ { 4, s_4_0, -1, 3, 0}, -/* 1 */ { 4, s_4_1, -1, 2, 0}, -/* 2 */ { 3, s_4_2, -1, 13, 0}, -/* 3 */ { 2, s_4_3, -1, 16, 0}, -/* 4 */ { 3, s_4_4, 3, 12, 0}, -/* 5 */ { 4, s_4_5, 4, 4, 0}, -/* 6 */ { 4, s_4_6, 3, 8, 0}, -/* 7 */ { 5, s_4_7, 3, 14, 0}, -/* 8 */ { 6, s_4_8, 3, 15, 0}, -/* 9 */ { 5, s_4_9, 3, 10, 0}, -/* 10 */ { 5, s_4_10, 3, 5, 0}, -/* 11 */ { 5, s_4_11, -1, 8, 0}, -/* 12 */ { 6, s_4_12, -1, 12, 0}, -/* 13 */ { 5, s_4_13, -1, 11, 0}, -/* 14 */ { 6, s_4_14, -1, 1, 0}, -/* 15 */ { 7, s_4_15, 14, 7, 0}, -/* 16 */ { 5, s_4_16, -1, 8, 0}, -/* 17 */ { 5, s_4_17, -1, 7, 0}, -/* 18 */ { 7, s_4_18, 17, 6, 0}, -/* 19 */ { 4, s_4_19, -1, 6, 0}, -/* 20 */ { 4, s_4_20, -1, 7, 0}, -/* 21 */ { 7, s_4_21, -1, 11, 0}, -/* 22 */ { 7, s_4_22, -1, 9, 0}, -/* 23 */ { 7, s_4_23, -1, 10, 0} + /* 0 */ {4, s_4_0, -1, 3, 0}, + /* 1 */ {4, s_4_1, -1, 2, 0}, + /* 2 */ {3, s_4_2, -1, 13, 0}, + /* 3 */ {2, s_4_3, -1, 16, 0}, + /* 4 */ {3, s_4_4, 3, 12, 0}, + /* 5 */ {4, s_4_5, 4, 4, 0}, + /* 6 */ {4, s_4_6, 3, 8, 0}, + /* 7 */ {5, s_4_7, 3, 14, 0}, + /* 8 */ {6, s_4_8, 3, 15, 0}, + /* 9 */ {5, s_4_9, 3, 10, 0}, + /* 10 */ {5, s_4_10, 3, 5, 0}, + /* 11 */ {5, s_4_11, -1, 8, 0}, + /* 12 */ {6, s_4_12, -1, 12, 0}, + /* 13 */ {5, s_4_13, -1, 11, 0}, + /* 14 */ {6, s_4_14, -1, 1, 0}, + /* 15 */ {7, s_4_15, 14, 7, 0}, + /* 16 */ {5, s_4_16, -1, 8, 0}, + /* 17 */ {5, s_4_17, -1, 7, 0}, + /* 18 */ {7, s_4_18, 17, 6, 0}, + /* 19 */ {4, s_4_19, -1, 6, 0}, + /* 20 */ {4, s_4_20, -1, 7, 0}, + /* 21 */ {7, s_4_21, -1, 11, 0}, + /* 22 */ {7, s_4_22, -1, 9, 0}, + /* 23 */ {7, s_4_23, -1, 10, 0} }; -static symbol s_5_0[5] = { 'i', 'c', 'a', 't', 'e' }; -static symbol s_5_1[5] = { 'a', 't', 'i', 'v', 'e' }; -static symbol s_5_2[5] = { 'a', 'l', 'i', 'z', 'e' }; -static symbol s_5_3[5] = { 'i', 'c', 'i', 't', 'i' }; -static symbol s_5_4[4] = { 'i', 'c', 'a', 'l' }; -static symbol s_5_5[6] = { 't', 'i', 'o', 'n', 'a', 'l' }; -static symbol s_5_6[7] = { 'a', 't', 'i', 'o', 'n', 'a', 'l' }; -static symbol s_5_7[3] = { 'f', 'u', 'l' }; -static symbol s_5_8[4] = { 'n', 'e', 's', 's' }; +static symbol s_5_0[5] = {'i', 'c', 'a', 't', 'e'}; +static symbol s_5_1[5] = {'a', 't', 'i', 'v', 'e'}; +static symbol s_5_2[5] = {'a', 'l', 'i', 'z', 'e'}; +static symbol s_5_3[5] = {'i', 'c', 'i', 't', 'i'}; +static symbol s_5_4[4] = {'i', 'c', 'a', 'l'}; +static symbol s_5_5[6] = {'t', 'i', 'o', 'n', 'a', 'l'}; +static symbol s_5_6[7] = {'a', 't', 'i', 'o', 'n', 'a', 'l'}; +static symbol s_5_7[3] = {'f', 'u', 'l'}; +static symbol s_5_8[4] = {'n', 'e', 's', 's'}; static struct among a_5[9] = { -/* 0 */ { 5, s_5_0, -1, 4, 0}, -/* 1 */ { 5, s_5_1, -1, 6, 0}, -/* 2 */ { 5, s_5_2, -1, 3, 0}, -/* 3 */ { 5, s_5_3, -1, 4, 0}, -/* 4 */ { 4, s_5_4, -1, 4, 0}, -/* 5 */ { 6, s_5_5, -1, 1, 0}, -/* 6 */ { 7, s_5_6, 5, 2, 0}, -/* 7 */ { 3, s_5_7, -1, 5, 0}, -/* 8 */ { 4, s_5_8, -1, 5, 0} + /* 0 */ {5, s_5_0, -1, 4, 0}, + /* 1 */ {5, s_5_1, -1, 6, 0}, + /* 2 */ {5, s_5_2, -1, 3, 0}, + /* 3 */ {5, s_5_3, -1, 4, 0}, + /* 4 */ {4, s_5_4, -1, 4, 0}, + /* 5 */ {6, s_5_5, -1, 1, 0}, + /* 6 */ {7, s_5_6, 5, 2, 0}, + /* 7 */ {3, s_5_7, -1, 5, 0}, + /* 8 */ {4, s_5_8, -1, 5, 0} }; -static symbol s_6_0[2] = { 'i', 'c' }; -static symbol s_6_1[4] = { 'a', 'n', 'c', 'e' }; -static symbol s_6_2[4] = { 'e', 'n', 'c', 'e' }; -static symbol s_6_3[4] = { 'a', 'b', 'l', 'e' }; -static symbol s_6_4[4] = { 'i', 'b', 'l', 'e' }; -static symbol s_6_5[3] = { 'a', 't', 'e' }; -static symbol s_6_6[3] = { 'i', 'v', 'e' }; -static symbol s_6_7[3] = { 'i', 'z', 'e' }; -static symbol s_6_8[3] = { 'i', 't', 'i' }; -static symbol s_6_9[2] = { 'a', 'l' }; -static symbol s_6_10[3] = { 'i', 's', 'm' }; -static symbol s_6_11[3] = { 'i', 'o', 'n' }; -static symbol s_6_12[2] = { 'e', 'r' }; -static symbol s_6_13[3] = { 'o', 'u', 's' }; -static symbol s_6_14[3] = { 'a', 'n', 't' }; -static symbol s_6_15[3] = { 'e', 'n', 't' }; -static symbol s_6_16[4] = { 'm', 'e', 'n', 't' }; -static symbol s_6_17[5] = { 'e', 'm', 'e', 'n', 't' }; +static symbol s_6_0[2] = {'i', 'c'}; +static symbol s_6_1[4] = {'a', 'n', 'c', 'e'}; +static symbol s_6_2[4] = {'e', 'n', 'c', 'e'}; +static symbol s_6_3[4] = {'a', 'b', 'l', 'e'}; +static symbol s_6_4[4] = {'i', 'b', 'l', 'e'}; +static symbol s_6_5[3] = {'a', 't', 'e'}; +static symbol s_6_6[3] = {'i', 'v', 'e'}; +static symbol s_6_7[3] = {'i', 'z', 'e'}; +static symbol s_6_8[3] = {'i', 't', 'i'}; +static symbol s_6_9[2] = {'a', 'l'}; +static symbol s_6_10[3] = {'i', 's', 'm'}; +static symbol s_6_11[3] = {'i', 'o', 'n'}; +static symbol s_6_12[2] = {'e', 'r'}; +static symbol s_6_13[3] = {'o', 'u', 's'}; +static symbol s_6_14[3] = {'a', 'n', 't'}; +static symbol s_6_15[3] = {'e', 'n', 't'}; +static symbol s_6_16[4] = {'m', 'e', 'n', 't'}; +static symbol s_6_17[5] = {'e', 'm', 'e', 'n', 't'}; static struct among a_6[18] = { -/* 0 */ { 2, s_6_0, -1, 1, 0}, -/* 1 */ { 4, s_6_1, -1, 1, 0}, -/* 2 */ { 4, s_6_2, -1, 1, 0}, -/* 3 */ { 4, s_6_3, -1, 1, 0}, -/* 4 */ { 4, s_6_4, -1, 1, 0}, -/* 5 */ { 3, s_6_5, -1, 1, 0}, -/* 6 */ { 3, s_6_6, -1, 1, 0}, -/* 7 */ { 3, s_6_7, -1, 1, 0}, -/* 8 */ { 3, s_6_8, -1, 1, 0}, -/* 9 */ { 2, s_6_9, -1, 1, 0}, -/* 10 */ { 3, s_6_10, -1, 1, 0}, -/* 11 */ { 3, s_6_11, -1, 2, 0}, -/* 12 */ { 2, s_6_12, -1, 1, 0}, -/* 13 */ { 3, s_6_13, -1, 1, 0}, -/* 14 */ { 3, s_6_14, -1, 1, 0}, -/* 15 */ { 3, s_6_15, -1, 1, 0}, -/* 16 */ { 4, s_6_16, 15, 1, 0}, -/* 17 */ { 5, s_6_17, 16, 1, 0} + /* 0 */ {2, s_6_0, -1, 1, 0}, + /* 1 */ {4, s_6_1, -1, 1, 0}, + /* 2 */ {4, s_6_2, -1, 1, 0}, + /* 3 */ {4, s_6_3, -1, 1, 0}, + /* 4 */ {4, s_6_4, -1, 1, 0}, + /* 5 */ {3, s_6_5, -1, 1, 0}, + /* 6 */ {3, s_6_6, -1, 1, 0}, + /* 7 */ {3, s_6_7, -1, 1, 0}, + /* 8 */ {3, s_6_8, -1, 1, 0}, + /* 9 */ {2, s_6_9, -1, 1, 0}, + /* 10 */ {3, s_6_10, -1, 1, 0}, + /* 11 */ {3, s_6_11, -1, 2, 0}, + /* 12 */ {2, s_6_12, -1, 1, 0}, + /* 13 */ {3, s_6_13, -1, 1, 0}, + /* 14 */ {3, s_6_14, -1, 1, 0}, + /* 15 */ {3, s_6_15, -1, 1, 0}, + /* 16 */ {4, s_6_16, 15, 1, 0}, + /* 17 */ {5, s_6_17, 16, 1, 0} }; -static symbol s_7_0[1] = { 'e' }; -static symbol s_7_1[1] = { 'l' }; +static symbol s_7_0[1] = {'e'}; +static symbol s_7_1[1] = {'l'}; static struct among a_7[2] = { -/* 0 */ { 1, s_7_0, -1, 1, 0}, -/* 1 */ { 1, s_7_1, -1, 2, 0} + /* 0 */ {1, s_7_0, -1, 1, 0}, + /* 1 */ {1, s_7_1, -1, 2, 0} }; -static symbol s_8_0[7] = { 's', 'u', 'c', 'c', 'e', 'e', 'd' }; -static symbol s_8_1[7] = { 'p', 'r', 'o', 'c', 'e', 'e', 'd' }; -static symbol s_8_2[6] = { 'e', 'x', 'c', 'e', 'e', 'd' }; -static symbol s_8_3[7] = { 'c', 'a', 'n', 'n', 'i', 'n', 'g' }; -static symbol s_8_4[6] = { 'i', 'n', 'n', 'i', 'n', 'g' }; -static symbol s_8_5[7] = { 'e', 'a', 'r', 'r', 'i', 'n', 'g' }; -static symbol s_8_6[7] = { 'h', 'e', 'r', 'r', 'i', 'n', 'g' }; -static symbol s_8_7[6] = { 'o', 'u', 't', 'i', 'n', 'g' }; +static symbol s_8_0[7] = {'s', 'u', 'c', 'c', 'e', 'e', 'd'}; +static symbol s_8_1[7] = {'p', 'r', 'o', 'c', 'e', 'e', 'd'}; +static symbol s_8_2[6] = {'e', 'x', 'c', 'e', 'e', 'd'}; +static symbol s_8_3[7] = {'c', 'a', 'n', 'n', 'i', 'n', 'g'}; +static symbol s_8_4[6] = {'i', 'n', 'n', 'i', 'n', 'g'}; +static symbol s_8_5[7] = {'e', 'a', 'r', 'r', 'i', 'n', 'g'}; +static symbol s_8_6[7] = {'h', 'e', 'r', 'r', 'i', 'n', 'g'}; +static symbol s_8_7[6] = {'o', 'u', 't', 'i', 'n', 'g'}; static struct among a_8[8] = { -/* 0 */ { 7, s_8_0, -1, -1, 0}, -/* 1 */ { 7, s_8_1, -1, -1, 0}, -/* 2 */ { 6, s_8_2, -1, -1, 0}, -/* 3 */ { 7, s_8_3, -1, -1, 0}, -/* 4 */ { 6, s_8_4, -1, -1, 0}, -/* 5 */ { 7, s_8_5, -1, -1, 0}, -/* 6 */ { 7, s_8_6, -1, -1, 0}, -/* 7 */ { 6, s_8_7, -1, -1, 0} + /* 0 */ {7, s_8_0, -1, -1, 0}, + /* 1 */ {7, s_8_1, -1, -1, 0}, + /* 2 */ {6, s_8_2, -1, -1, 0}, + /* 3 */ {7, s_8_3, -1, -1, 0}, + /* 4 */ {6, s_8_4, -1, -1, 0}, + /* 5 */ {7, s_8_5, -1, -1, 0}, + /* 6 */ {7, s_8_6, -1, -1, 0}, + /* 7 */ {6, s_8_7, -1, -1, 0} }; -static symbol s_9_0[5] = { 'a', 'n', 'd', 'e', 's' }; -static symbol s_9_1[5] = { 'a', 't', 'l', 'a', 's' }; -static symbol s_9_2[4] = { 'b', 'i', 'a', 's' }; -static symbol s_9_3[6] = { 'c', 'o', 's', 'm', 'o', 's' }; -static symbol s_9_4[5] = { 'd', 'y', 'i', 'n', 'g' }; -static symbol s_9_5[5] = { 'e', 'a', 'r', 'l', 'y' }; -static symbol s_9_6[6] = { 'g', 'e', 'n', 't', 'l', 'y' }; -static symbol s_9_7[4] = { 'h', 'o', 'w', 'e' }; -static symbol s_9_8[4] = { 'i', 'd', 'l', 'y' }; -static symbol s_9_9[5] = { 'l', 'y', 'i', 'n', 'g' }; -static symbol s_9_10[4] = { 'n', 'e', 'w', 's' }; -static symbol s_9_11[4] = { 'o', 'n', 'l', 'y' }; -static symbol s_9_12[6] = { 's', 'i', 'n', 'g', 'l', 'y' }; -static symbol s_9_13[5] = { 's', 'k', 'i', 'e', 's' }; -static symbol s_9_14[4] = { 's', 'k', 'i', 's' }; -static symbol s_9_15[3] = { 's', 'k', 'y' }; -static symbol s_9_16[5] = { 't', 'y', 'i', 'n', 'g' }; -static symbol s_9_17[4] = { 'u', 'g', 'l', 'y' }; +static symbol s_9_0[5] = {'a', 'n', 'd', 'e', 's'}; +static symbol s_9_1[5] = {'a', 't', 'l', 'a', 's'}; +static symbol s_9_2[4] = {'b', 'i', 'a', 's'}; +static symbol s_9_3[6] = {'c', 'o', 's', 'm', 'o', 's'}; +static symbol s_9_4[5] = {'d', 'y', 'i', 'n', 'g'}; +static symbol s_9_5[5] = {'e', 'a', 'r', 'l', 'y'}; +static symbol s_9_6[6] = {'g', 'e', 'n', 't', 'l', 'y'}; +static symbol s_9_7[4] = {'h', 'o', 'w', 'e'}; +static symbol s_9_8[4] = {'i', 'd', 'l', 'y'}; +static symbol s_9_9[5] = {'l', 'y', 'i', 'n', 'g'}; +static symbol s_9_10[4] = {'n', 'e', 'w', 's'}; +static symbol s_9_11[4] = {'o', 'n', 'l', 'y'}; +static symbol s_9_12[6] = {'s', 'i', 'n', 'g', 'l', 'y'}; +static symbol s_9_13[5] = {'s', 'k', 'i', 'e', 's'}; +static symbol s_9_14[4] = {'s', 'k', 'i', 's'}; +static symbol s_9_15[3] = {'s', 'k', 'y'}; +static symbol s_9_16[5] = {'t', 'y', 'i', 'n', 'g'}; +static symbol s_9_17[4] = {'u', 'g', 'l', 'y'}; static struct among a_9[18] = { -/* 0 */ { 5, s_9_0, -1, -1, 0}, -/* 1 */ { 5, s_9_1, -1, -1, 0}, -/* 2 */ { 4, s_9_2, -1, -1, 0}, -/* 3 */ { 6, s_9_3, -1, -1, 0}, -/* 4 */ { 5, s_9_4, -1, 3, 0}, -/* 5 */ { 5, s_9_5, -1, 9, 0}, -/* 6 */ { 6, s_9_6, -1, 7, 0}, -/* 7 */ { 4, s_9_7, -1, -1, 0}, -/* 8 */ { 4, s_9_8, -1, 6, 0}, -/* 9 */ { 5, s_9_9, -1, 4, 0}, -/* 10 */ { 4, s_9_10, -1, -1, 0}, -/* 11 */ { 4, s_9_11, -1, 10, 0}, -/* 12 */ { 6, s_9_12, -1, 11, 0}, -/* 13 */ { 5, s_9_13, -1, 2, 0}, -/* 14 */ { 4, s_9_14, -1, 1, 0}, -/* 15 */ { 3, s_9_15, -1, -1, 0}, -/* 16 */ { 5, s_9_16, -1, 5, 0}, -/* 17 */ { 4, s_9_17, -1, 8, 0} + /* 0 */ {5, s_9_0, -1, -1, 0}, + /* 1 */ {5, s_9_1, -1, -1, 0}, + /* 2 */ {4, s_9_2, -1, -1, 0}, + /* 3 */ {6, s_9_3, -1, -1, 0}, + /* 4 */ {5, s_9_4, -1, 3, 0}, + /* 5 */ {5, s_9_5, -1, 9, 0}, + /* 6 */ {6, s_9_6, -1, 7, 0}, + /* 7 */ {4, s_9_7, -1, -1, 0}, + /* 8 */ {4, s_9_8, -1, 6, 0}, + /* 9 */ {5, s_9_9, -1, 4, 0}, + /* 10 */ {4, s_9_10, -1, -1, 0}, + /* 11 */ {4, s_9_11, -1, 10, 0}, + /* 12 */ {6, s_9_12, -1, 11, 0}, + /* 13 */ {5, s_9_13, -1, 2, 0}, + /* 14 */ {4, s_9_14, -1, 1, 0}, + /* 15 */ {3, s_9_15, -1, -1, 0}, + /* 16 */ {5, s_9_16, -1, 5, 0}, + /* 17 */ {4, s_9_17, -1, 8, 0} }; -static unsigned char g_v[] = { 17, 65, 16, 1 }; - -static unsigned char g_v_WXY[] = { 1, 17, 65, 208, 1 }; - -static unsigned char g_valid_LI[] = { 55, 141, 2 }; - -static symbol s_0[] = { 'y' }; -static symbol s_1[] = { 'Y' }; -static symbol s_2[] = { 'y' }; -static symbol s_3[] = { 'Y' }; -static symbol s_4[] = { 's', 's' }; -static symbol s_5[] = { 'i', 'e' }; -static symbol s_6[] = { 'i' }; -static symbol s_7[] = { 'e', 'e' }; -static symbol s_8[] = { 'e' }; -static symbol s_9[] = { 'e' }; -static symbol s_10[] = { 'y' }; -static symbol s_11[] = { 'Y' }; -static symbol s_12[] = { 'i' }; -static symbol s_13[] = { 't', 'i', 'o', 'n' }; -static symbol s_14[] = { 'e', 'n', 'c', 'e' }; -static symbol s_15[] = { 'a', 'n', 'c', 'e' }; -static symbol s_16[] = { 'a', 'b', 'l', 'e' }; -static symbol s_17[] = { 'e', 'n', 't' }; -static symbol s_18[] = { 'i', 'z', 'e' }; -static symbol s_19[] = { 'a', 't', 'e' }; -static symbol s_20[] = { 'a', 'l' }; -static symbol s_21[] = { 'f', 'u', 'l' }; -static symbol s_22[] = { 'o', 'u', 's' }; -static symbol s_23[] = { 'i', 'v', 'e' }; -static symbol s_24[] = { 'b', 'l', 'e' }; -static symbol s_25[] = { 'l' }; -static symbol s_26[] = { 'o', 'g' }; -static symbol s_27[] = { 'f', 'u', 'l' }; -static symbol s_28[] = { 'l', 'e', 's', 's' }; -static symbol s_29[] = { 't', 'i', 'o', 'n' }; -static symbol s_30[] = { 'a', 't', 'e' }; -static symbol s_31[] = { 'a', 'l' }; -static symbol s_32[] = { 'i', 'c' }; -static symbol s_33[] = { 's' }; -static symbol s_34[] = { 't' }; -static symbol s_35[] = { 'l' }; -static symbol s_36[] = { 's', 'k', 'i' }; -static symbol s_37[] = { 's', 'k', 'y' }; -static symbol s_38[] = { 'd', 'i', 'e' }; -static symbol s_39[] = { 'l', 'i', 'e' }; -static symbol s_40[] = { 't', 'i', 'e' }; -static symbol s_41[] = { 'i', 'd', 'l' }; -static symbol s_42[] = { 'g', 'e', 'n', 't', 'l' }; -static symbol s_43[] = { 'u', 'g', 'l', 'i' }; -static symbol s_44[] = { 'e', 'a', 'r', 'l', 'i' }; -static symbol s_45[] = { 'o', 'n', 'l', 'i' }; -static symbol s_46[] = { 's', 'i', 'n', 'g', 'l' }; -static symbol s_47[] = { 'Y' }; -static symbol s_48[] = { 'y' }; - -static int r_prelude(struct SN_env * z) { - z->B[0] = 0; /* unset Y_found, line 24 */ - { int c = z->c; /* do, line 25 */ - z->bra = z->c; /* [, line 25 */ - if (!(eq_s(z, 1, s_0))) goto lab0; - z->ket = z->c; /* ], line 25 */ - if (!(in_grouping(z, g_v, 97, 121))) goto lab0; - slice_from_s(z, 1, s_1); /* <-, line 25 */ - z->B[0] = 1; /* set Y_found, line 25 */ - lab0: - z->c = c; - } - { int c = z->c; /* do, line 26 */ - while(1) { /* repeat, line 26 */ - int c = z->c; - while(1) { /* goto, line 26 */ - int c = z->c; - if (!(in_grouping(z, g_v, 97, 121))) goto lab3; - z->bra = z->c; /* [, line 26 */ - if (!(eq_s(z, 1, s_2))) goto lab3; - z->ket = z->c; /* ], line 26 */ - z->c = c; - break; - lab3: - z->c = c; - if (z->c >= z->l) goto lab2; - z->c++; - } - slice_from_s(z, 1, s_3); /* <-, line 26 */ - z->B[0] = 1; /* set Y_found, line 26 */ - continue; - lab2: - z->c = c; - break; - } - z->c = c; - } - return 1; +static unsigned char g_v[] = {17, 65, 16, 1}; + +static unsigned char g_v_WXY[] = {1, 17, 65, 208, 1}; + +static unsigned char g_valid_LI[] = {55, 141, 2}; + +static symbol s_0[] = {'y'}; +static symbol s_1[] = {'Y'}; +static symbol s_2[] = {'y'}; +static symbol s_3[] = {'Y'}; +static symbol s_4[] = {'s', 's'}; +static symbol s_5[] = {'i', 'e'}; +static symbol s_6[] = {'i'}; +static symbol s_7[] = {'e', 'e'}; +static symbol s_8[] = {'e'}; +static symbol s_9[] = {'e'}; +static symbol s_10[] = {'y'}; +static symbol s_11[] = {'Y'}; +static symbol s_12[] = {'i'}; +static symbol s_13[] = {'t', 'i', 'o', 'n'}; +static symbol s_14[] = {'e', 'n', 'c', 'e'}; +static symbol s_15[] = {'a', 'n', 'c', 'e'}; +static symbol s_16[] = {'a', 'b', 'l', 'e'}; +static symbol s_17[] = {'e', 'n', 't'}; +static symbol s_18[] = {'i', 'z', 'e'}; +static symbol s_19[] = {'a', 't', 'e'}; +static symbol s_20[] = {'a', 'l'}; +static symbol s_21[] = {'f', 'u', 'l'}; +static symbol s_22[] = {'o', 'u', 's'}; +static symbol s_23[] = {'i', 'v', 'e'}; +static symbol s_24[] = {'b', 'l', 'e'}; +static symbol s_25[] = {'l'}; +static symbol s_26[] = {'o', 'g'}; +static symbol s_27[] = {'f', 'u', 'l'}; +static symbol s_28[] = {'l', 'e', 's', 's'}; +static symbol s_29[] = {'t', 'i', 'o', 'n'}; +static symbol s_30[] = {'a', 't', 'e'}; +static symbol s_31[] = {'a', 'l'}; +static symbol s_32[] = {'i', 'c'}; +static symbol s_33[] = {'s'}; +static symbol s_34[] = {'t'}; +static symbol s_35[] = {'l'}; +static symbol s_36[] = {'s', 'k', 'i'}; +static symbol s_37[] = {'s', 'k', 'y'}; +static symbol s_38[] = {'d', 'i', 'e'}; +static symbol s_39[] = {'l', 'i', 'e'}; +static symbol s_40[] = {'t', 'i', 'e'}; +static symbol s_41[] = {'i', 'd', 'l'}; +static symbol s_42[] = {'g', 'e', 'n', 't', 'l'}; +static symbol s_43[] = {'u', 'g', 'l', 'i'}; +static symbol s_44[] = {'e', 'a', 'r', 'l', 'i'}; +static symbol s_45[] = {'o', 'n', 'l', 'i'}; +static symbol s_46[] = {'s', 'i', 'n', 'g', 'l'}; +static symbol s_47[] = {'Y'}; +static symbol s_48[] = {'y'}; + +static int +r_prelude(struct SN_env * z) +{ + z->B[0] = 0; /* unset Y_found, line 24 */ + { + int c = z->c; /* do, line 25 */ + + z->bra = z->c; /* [, line 25 */ + if (!(eq_s(z, 1, s_0))) + goto lab0; + z->ket = z->c; /* ], line 25 */ + if (!(in_grouping(z, g_v, 97, 121))) + goto lab0; + slice_from_s(z, 1, s_1); /* <-, line 25 */ + z->B[0] = 1; /* set Y_found, line 25 */ +lab0: + z->c = c; + } + { + int c = z->c; /* do, line 26 */ + + while (1) + { /* repeat, line 26 */ + int c = z->c; + + while (1) + { /* goto, line 26 */ + int c = z->c; + + if (!(in_grouping(z, g_v, 97, 121))) + goto lab3; + z->bra = z->c; /* [, line 26 */ + if (!(eq_s(z, 1, s_2))) + goto lab3; + z->ket = z->c; /* ], line 26 */ + z->c = c; + break; + lab3: + z->c = c; + if (z->c >= z->l) + goto lab2; + z->c++; + } + slice_from_s(z, 1, s_3); /* <-, line 26 */ + z->B[0] = 1; /* set Y_found, line 26 */ + continue; + lab2: + z->c = c; + break; + } + z->c = c; + } + return 1; } -static int r_mark_regions(struct SN_env * z) { - z->I[0] = z->l; - z->I[1] = z->l; - { int c = z->c; /* do, line 32 */ - { int c = z->c; /* or, line 36 */ - if (!(find_among(z, a_0, 1))) goto lab2; /* among, line 33 */ - goto lab1; - lab2: - z->c = c; - while(1) { /* gopast, line 36 */ - if (!(in_grouping(z, g_v, 97, 121))) goto lab3; - break; - lab3: - if (z->c >= z->l) goto lab0; - z->c++; - } - while(1) { /* gopast, line 36 */ - if (!(out_grouping(z, g_v, 97, 121))) goto lab4; - break; - lab4: - if (z->c >= z->l) goto lab0; - z->c++; - } - } - lab1: - z->I[0] = z->c; /* setmark p1, line 37 */ - while(1) { /* gopast, line 38 */ - if (!(in_grouping(z, g_v, 97, 121))) goto lab5; - break; - lab5: - if (z->c >= z->l) goto lab0; - z->c++; - } - while(1) { /* gopast, line 38 */ - if (!(out_grouping(z, g_v, 97, 121))) goto lab6; - break; - lab6: - if (z->c >= z->l) goto lab0; - z->c++; - } - z->I[1] = z->c; /* setmark p2, line 38 */ - lab0: - z->c = c; - } - return 1; +static int +r_mark_regions(struct SN_env * z) +{ + z->I[0] = z->l; + z->I[1] = z->l; + { + int c = z->c; /* do, line 32 */ + + { + int c = z->c; /* or, line 36 */ + + if (!(find_among(z, a_0, 1))) + goto lab2; /* among, line 33 */ + goto lab1; + lab2: + z->c = c; + while (1) + { /* gopast, line 36 */ + if (!(in_grouping(z, g_v, 97, 121))) + goto lab3; + break; + lab3: + if (z->c >= z->l) + goto lab0; + z->c++; + } + while (1) + { /* gopast, line 36 */ + if (!(out_grouping(z, g_v, 97, 121))) + goto lab4; + break; + lab4: + if (z->c >= z->l) + goto lab0; + z->c++; + } + } +lab1: + z->I[0] = z->c; /* setmark p1, line 37 */ + while (1) + { /* gopast, line 38 */ + if (!(in_grouping(z, g_v, 97, 121))) + goto lab5; + break; + lab5: + if (z->c >= z->l) + goto lab0; + z->c++; + } + while (1) + { /* gopast, line 38 */ + if (!(out_grouping(z, g_v, 97, 121))) + goto lab6; + break; + lab6: + if (z->c >= z->l) + goto lab0; + z->c++; + } + z->I[1] = z->c; /* setmark p2, line 38 */ +lab0: + z->c = c; + } + return 1; } -static int r_shortv(struct SN_env * z) { - { int m = z->l - z->c; /* or, line 46 */ - if (!(out_grouping_b(z, g_v_WXY, 89, 121))) goto lab1; - if (!(in_grouping_b(z, g_v, 97, 121))) goto lab1; - if (!(out_grouping_b(z, g_v, 97, 121))) goto lab1; - goto lab0; - lab1: - z->c = z->l - m; - if (!(out_grouping_b(z, g_v, 97, 121))) return 0; - if (!(in_grouping_b(z, g_v, 97, 121))) return 0; - if (z->c > z->lb) return 0; /* atlimit, line 47 */ - } +static int +r_shortv(struct SN_env * z) +{ + { + int m = z->l - z->c; /* or, line 46 */ + + if (!(out_grouping_b(z, g_v_WXY, 89, 121))) + goto lab1; + if (!(in_grouping_b(z, g_v, 97, 121))) + goto lab1; + if (!(out_grouping_b(z, g_v, 97, 121))) + goto lab1; + goto lab0; +lab1: + z->c = z->l - m; + if (!(out_grouping_b(z, g_v, 97, 121))) + return 0; + if (!(in_grouping_b(z, g_v, 97, 121))) + return 0; + if (z->c > z->lb) + return 0; /* atlimit, line 47 */ + } lab0: - return 1; + return 1; } -static int r_R1(struct SN_env * z) { - if (!(z->I[0] <= z->c)) return 0; - return 1; +static int +r_R1(struct SN_env * z) +{ + if (!(z->I[0] <= z->c)) + return 0; + return 1; } -static int r_R2(struct SN_env * z) { - if (!(z->I[1] <= z->c)) return 0; - return 1; +static int +r_R2(struct SN_env * z) +{ + if (!(z->I[1] <= z->c)) + return 0; + return 1; } -static int r_Step_1a(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 54 */ - among_var = find_among_b(z, a_1, 6); /* substring, line 54 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 54 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_from_s(z, 2, s_4); /* <-, line 55 */ - break; - case 2: - { int m = z->l - z->c; /* or, line 57 */ - if (z->c <= z->lb) goto lab1; - z->c--; /* next, line 57 */ - if (z->c > z->lb) goto lab1; /* atlimit, line 57 */ - slice_from_s(z, 2, s_5); /* <-, line 57 */ - goto lab0; - lab1: - z->c = z->l - m; - slice_from_s(z, 1, s_6); /* <-, line 57 */ - } - lab0: - break; - case 3: - if (z->c <= z->lb) return 0; - z->c--; /* next, line 58 */ - while(1) { /* gopast, line 58 */ - if (!(in_grouping_b(z, g_v, 97, 121))) goto lab2; - break; - lab2: - if (z->c <= z->lb) return 0; - z->c--; - } - slice_del(z); /* delete, line 58 */ - break; - } - return 1; +static int +r_Step_1a(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 54 */ + among_var = find_among_b(z, a_1, 6); /* substring, line 54 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 54 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_from_s(z, 2, s_4); /* <-, line 55 */ + break; + case 2: + { + int m = z->l - z->c; /* or, line 57 */ + + if (z->c <= z->lb) + goto lab1; + z->c--; /* next, line 57 */ + if (z->c > z->lb) + goto lab1; /* atlimit, line 57 */ + slice_from_s(z, 2, s_5); /* <-, line 57 */ + goto lab0; + lab1: + z->c = z->l - m; + slice_from_s(z, 1, s_6); /* <-, line 57 */ + } + lab0: + break; + case 3: + if (z->c <= z->lb) + return 0; + z->c--; /* next, line 58 */ + while (1) + { /* gopast, line 58 */ + if (!(in_grouping_b(z, g_v, 97, 121))) + goto lab2; + break; + lab2: + if (z->c <= z->lb) + return 0; + z->c--; + } + slice_del(z); /* delete, line 58 */ + break; + } + return 1; } -static int r_Step_1b(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 64 */ - among_var = find_among_b(z, a_3, 6); /* substring, line 64 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 64 */ - switch(among_var) { - case 0: return 0; - case 1: - if (!r_R1(z)) return 0; /* call R1, line 66 */ - slice_from_s(z, 2, s_7); /* <-, line 66 */ - break; - case 2: - { int m_test = z->l - z->c; /* test, line 69 */ - while(1) { /* gopast, line 69 */ - if (!(in_grouping_b(z, g_v, 97, 121))) goto lab0; - break; - lab0: - if (z->c <= z->lb) return 0; - z->c--; - } - z->c = z->l - m_test; - } - slice_del(z); /* delete, line 69 */ - { int m_test = z->l - z->c; /* test, line 70 */ - among_var = find_among_b(z, a_2, 13); /* substring, line 70 */ - if (!(among_var)) return 0; - z->c = z->l - m_test; - } - switch(among_var) { - case 0: return 0; - case 1: - { int c = z->c; - insert_s(z, z->c, z->c, 1, s_8); /* <+, line 72 */ - z->c = c; - } - break; - case 2: - z->ket = z->c; /* [, line 75 */ - if (z->c <= z->lb) return 0; - z->c--; /* next, line 75 */ - z->bra = z->c; /* ], line 75 */ - slice_del(z); /* delete, line 75 */ - break; - case 3: - if (z->c != z->I[0]) return 0; /* atmark, line 76 */ - { int m_test = z->l - z->c; /* test, line 76 */ - if (!r_shortv(z)) return 0; /* call shortv, line 76 */ - z->c = z->l - m_test; - } - { int c = z->c; - insert_s(z, z->c, z->c, 1, s_9); /* <+, line 76 */ - z->c = c; - } - break; - } - break; - } - return 1; +static int +r_Step_1b(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 64 */ + among_var = find_among_b(z, a_3, 6); /* substring, line 64 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 64 */ + switch (among_var) + { + case 0: + return 0; + case 1: + if (!r_R1(z)) + return 0; /* call R1, line 66 */ + slice_from_s(z, 2, s_7); /* <-, line 66 */ + break; + case 2: + { + int m_test = z->l - z->c; /* test, line 69 */ + + while (1) + { /* gopast, line 69 */ + if (!(in_grouping_b(z, g_v, 97, 121))) + goto lab0; + break; + lab0: + if (z->c <= z->lb) + return 0; + z->c--; + } + z->c = z->l - m_test; + } + slice_del(z); /* delete, line 69 */ + { + int m_test = z->l - z->c; /* test, line 70 */ + + among_var = find_among_b(z, a_2, 13); /* substring, line 70 */ + if (!(among_var)) + return 0; + z->c = z->l - m_test; + } + switch (among_var) + { + case 0: + return 0; + case 1: + { + int c = z->c; + + insert_s(z, z->c, z->c, 1, s_8); /* <+, line 72 */ + z->c = c; + } + break; + case 2: + z->ket = z->c; /* [, line 75 */ + if (z->c <= z->lb) + return 0; + z->c--; /* next, line 75 */ + z->bra = z->c; /* ], line 75 */ + slice_del(z); /* delete, line 75 */ + break; + case 3: + if (z->c != z->I[0]) + return 0; /* atmark, line 76 */ + { + int m_test = z->l - z->c; /* test, line 76 */ + + if (!r_shortv(z)) + return 0; /* call shortv, line 76 */ + z->c = z->l - m_test; + } + { + int c = z->c; + + insert_s(z, z->c, z->c, 1, s_9); /* <+, line 76 */ + z->c = c; + } + break; + } + break; + } + return 1; } -static int r_Step_1c(struct SN_env * z) { - z->ket = z->c; /* [, line 83 */ - { int m = z->l - z->c; /* or, line 83 */ - if (!(eq_s_b(z, 1, s_10))) goto lab1; - goto lab0; - lab1: - z->c = z->l - m; - if (!(eq_s_b(z, 1, s_11))) return 0; - } +static int +r_Step_1c(struct SN_env * z) +{ + z->ket = z->c; /* [, line 83 */ + { + int m = z->l - z->c; /* or, line 83 */ + + if (!(eq_s_b(z, 1, s_10))) + goto lab1; + goto lab0; +lab1: + z->c = z->l - m; + if (!(eq_s_b(z, 1, s_11))) + return 0; + } lab0: - z->bra = z->c; /* ], line 83 */ - if (!(out_grouping_b(z, g_v, 97, 121))) return 0; - { int m = z->l - z->c; /* not, line 84 */ - if (z->c > z->lb) goto lab2; /* atlimit, line 84 */ - return 0; - lab2: - z->c = z->l - m; - } - slice_from_s(z, 1, s_12); /* <-, line 85 */ - return 1; -} + z->bra = z->c; /* ], line 83 */ + if (!(out_grouping_b(z, g_v, 97, 121))) + return 0; + { + int m = z->l - z->c; /* not, line 84 */ -static int r_Step_2(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 89 */ - among_var = find_among_b(z, a_4, 24); /* substring, line 89 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 89 */ - if (!r_R1(z)) return 0; /* call R1, line 89 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_from_s(z, 4, s_13); /* <-, line 90 */ - break; - case 2: - slice_from_s(z, 4, s_14); /* <-, line 91 */ - break; - case 3: - slice_from_s(z, 4, s_15); /* <-, line 92 */ - break; - case 4: - slice_from_s(z, 4, s_16); /* <-, line 93 */ - break; - case 5: - slice_from_s(z, 3, s_17); /* <-, line 94 */ - break; - case 6: - slice_from_s(z, 3, s_18); /* <-, line 96 */ - break; - case 7: - slice_from_s(z, 3, s_19); /* <-, line 98 */ - break; - case 8: - slice_from_s(z, 2, s_20); /* <-, line 100 */ - break; - case 9: - slice_from_s(z, 3, s_21); /* <-, line 101 */ - break; - case 10: - slice_from_s(z, 3, s_22); /* <-, line 103 */ - break; - case 11: - slice_from_s(z, 3, s_23); /* <-, line 105 */ - break; - case 12: - slice_from_s(z, 3, s_24); /* <-, line 107 */ - break; - case 13: - if (!(eq_s_b(z, 1, s_25))) return 0; - slice_from_s(z, 2, s_26); /* <-, line 108 */ - break; - case 14: - slice_from_s(z, 3, s_27); /* <-, line 109 */ - break; - case 15: - slice_from_s(z, 4, s_28); /* <-, line 110 */ - break; - case 16: - if (!(in_grouping_b(z, g_valid_LI, 99, 116))) return 0; - slice_del(z); /* delete, line 111 */ - break; - } - return 1; + if (z->c > z->lb) + goto lab2; /* atlimit, line 84 */ + return 0; +lab2: + z->c = z->l - m; + } + slice_from_s(z, 1, s_12); /* <-, line 85 */ + return 1; } -static int r_Step_3(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 116 */ - among_var = find_among_b(z, a_5, 9); /* substring, line 116 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 116 */ - if (!r_R1(z)) return 0; /* call R1, line 116 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_from_s(z, 4, s_29); /* <-, line 117 */ - break; - case 2: - slice_from_s(z, 3, s_30); /* <-, line 118 */ - break; - case 3: - slice_from_s(z, 2, s_31); /* <-, line 119 */ - break; - case 4: - slice_from_s(z, 2, s_32); /* <-, line 121 */ - break; - case 5: - slice_del(z); /* delete, line 123 */ - break; - case 6: - if (!r_R2(z)) return 0; /* call R2, line 125 */ - slice_del(z); /* delete, line 125 */ - break; - } - return 1; +static int +r_Step_2(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 89 */ + among_var = find_among_b(z, a_4, 24); /* substring, line 89 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 89 */ + if (!r_R1(z)) + return 0; /* call R1, line 89 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_from_s(z, 4, s_13); /* <-, line 90 */ + break; + case 2: + slice_from_s(z, 4, s_14); /* <-, line 91 */ + break; + case 3: + slice_from_s(z, 4, s_15); /* <-, line 92 */ + break; + case 4: + slice_from_s(z, 4, s_16); /* <-, line 93 */ + break; + case 5: + slice_from_s(z, 3, s_17); /* <-, line 94 */ + break; + case 6: + slice_from_s(z, 3, s_18); /* <-, line 96 */ + break; + case 7: + slice_from_s(z, 3, s_19); /* <-, line 98 */ + break; + case 8: + slice_from_s(z, 2, s_20); /* <-, line 100 */ + break; + case 9: + slice_from_s(z, 3, s_21); /* <-, line 101 */ + break; + case 10: + slice_from_s(z, 3, s_22); /* <-, line 103 */ + break; + case 11: + slice_from_s(z, 3, s_23); /* <-, line 105 */ + break; + case 12: + slice_from_s(z, 3, s_24); /* <-, line 107 */ + break; + case 13: + if (!(eq_s_b(z, 1, s_25))) + return 0; + slice_from_s(z, 2, s_26); /* <-, line 108 */ + break; + case 14: + slice_from_s(z, 3, s_27); /* <-, line 109 */ + break; + case 15: + slice_from_s(z, 4, s_28); /* <-, line 110 */ + break; + case 16: + if (!(in_grouping_b(z, g_valid_LI, 99, 116))) + return 0; + slice_del(z); /* delete, line 111 */ + break; + } + return 1; } -static int r_Step_4(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 130 */ - among_var = find_among_b(z, a_6, 18); /* substring, line 130 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 130 */ - if (!r_R2(z)) return 0; /* call R2, line 130 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 133 */ - break; - case 2: - { int m = z->l - z->c; /* or, line 134 */ - if (!(eq_s_b(z, 1, s_33))) goto lab1; - goto lab0; - lab1: - z->c = z->l - m; - if (!(eq_s_b(z, 1, s_34))) return 0; - } - lab0: - slice_del(z); /* delete, line 134 */ - break; - } - return 1; +static int +r_Step_3(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 116 */ + among_var = find_among_b(z, a_5, 9); /* substring, line 116 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 116 */ + if (!r_R1(z)) + return 0; /* call R1, line 116 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_from_s(z, 4, s_29); /* <-, line 117 */ + break; + case 2: + slice_from_s(z, 3, s_30); /* <-, line 118 */ + break; + case 3: + slice_from_s(z, 2, s_31); /* <-, line 119 */ + break; + case 4: + slice_from_s(z, 2, s_32); /* <-, line 121 */ + break; + case 5: + slice_del(z); /* delete, line 123 */ + break; + case 6: + if (!r_R2(z)) + return 0; /* call R2, line 125 */ + slice_del(z); /* delete, line 125 */ + break; + } + return 1; } -static int r_Step_5(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 139 */ - among_var = find_among_b(z, a_7, 2); /* substring, line 139 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 139 */ - switch(among_var) { - case 0: return 0; - case 1: - { int m = z->l - z->c; /* or, line 140 */ - if (!r_R2(z)) goto lab1; /* call R2, line 140 */ - goto lab0; - lab1: - z->c = z->l - m; - if (!r_R1(z)) return 0; /* call R1, line 140 */ - { int m = z->l - z->c; /* not, line 140 */ - if (!r_shortv(z)) goto lab2; /* call shortv, line 140 */ - return 0; - lab2: - z->c = z->l - m; - } - } - lab0: - slice_del(z); /* delete, line 140 */ - break; - case 2: - if (!r_R2(z)) return 0; /* call R2, line 141 */ - if (!(eq_s_b(z, 1, s_35))) return 0; - slice_del(z); /* delete, line 141 */ - break; - } - return 1; +static int +r_Step_4(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 130 */ + among_var = find_among_b(z, a_6, 18); /* substring, line 130 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 130 */ + if (!r_R2(z)) + return 0; /* call R2, line 130 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 133 */ + break; + case 2: + { + int m = z->l - z->c; /* or, line 134 */ + + if (!(eq_s_b(z, 1, s_33))) + goto lab1; + goto lab0; + lab1: + z->c = z->l - m; + if (!(eq_s_b(z, 1, s_34))) + return 0; + } + lab0: + slice_del(z); /* delete, line 134 */ + break; + } + return 1; } -static int r_exception2(struct SN_env * z) { - z->ket = z->c; /* [, line 147 */ - if (!(find_among_b(z, a_8, 8))) return 0; /* substring, line 147 */ - z->bra = z->c; /* ], line 147 */ - if (z->c > z->lb) return 0; /* atlimit, line 147 */ - return 1; +static int +r_Step_5(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 139 */ + among_var = find_among_b(z, a_7, 2); /* substring, line 139 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 139 */ + switch (among_var) + { + case 0: + return 0; + case 1: + { + int m = z->l - z->c; /* or, line 140 */ + + if (!r_R2(z)) + goto lab1; /* call R2, line 140 */ + goto lab0; + lab1: + z->c = z->l - m; + if (!r_R1(z)) + return 0; /* call R1, line 140 */ + { + int m = z->l - z->c; /* not, line 140 */ + + if (!r_shortv(z)) + goto lab2; /* call shortv, line 140 */ + return 0; + lab2: + z->c = z->l - m; + } + } + lab0: + slice_del(z); /* delete, line 140 */ + break; + case 2: + if (!r_R2(z)) + return 0; /* call R2, line 141 */ + if (!(eq_s_b(z, 1, s_35))) + return 0; + slice_del(z); /* delete, line 141 */ + break; + } + return 1; } -static int r_exception1(struct SN_env * z) { - int among_var; - z->bra = z->c; /* [, line 159 */ - among_var = find_among(z, a_9, 18); /* substring, line 159 */ - if (!(among_var)) return 0; - z->ket = z->c; /* ], line 159 */ - if (z->c < z->l) return 0; /* atlimit, line 159 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_from_s(z, 3, s_36); /* <-, line 163 */ - break; - case 2: - slice_from_s(z, 3, s_37); /* <-, line 164 */ - break; - case 3: - slice_from_s(z, 3, s_38); /* <-, line 165 */ - break; - case 4: - slice_from_s(z, 3, s_39); /* <-, line 166 */ - break; - case 5: - slice_from_s(z, 3, s_40); /* <-, line 167 */ - break; - case 6: - slice_from_s(z, 3, s_41); /* <-, line 171 */ - break; - case 7: - slice_from_s(z, 5, s_42); /* <-, line 172 */ - break; - case 8: - slice_from_s(z, 4, s_43); /* <-, line 173 */ - break; - case 9: - slice_from_s(z, 5, s_44); /* <-, line 174 */ - break; - case 10: - slice_from_s(z, 4, s_45); /* <-, line 175 */ - break; - case 11: - slice_from_s(z, 5, s_46); /* <-, line 176 */ - break; - } - return 1; +static int +r_exception2(struct SN_env * z) +{ + z->ket = z->c; /* [, line 147 */ + if (!(find_among_b(z, a_8, 8))) + return 0; /* substring, line 147 */ + z->bra = z->c; /* ], line 147 */ + if (z->c > z->lb) + return 0; /* atlimit, line 147 */ + return 1; } -static int r_postlude(struct SN_env * z) { - if (!(z->B[0])) return 0; /* Boolean test Y_found, line 192 */ - while(1) { /* repeat, line 192 */ - int c = z->c; - while(1) { /* goto, line 192 */ - int c = z->c; - z->bra = z->c; /* [, line 192 */ - if (!(eq_s(z, 1, s_47))) goto lab1; - z->ket = z->c; /* ], line 192 */ - z->c = c; - break; - lab1: - z->c = c; - if (z->c >= z->l) goto lab0; - z->c++; - } - slice_from_s(z, 1, s_48); /* <-, line 192 */ - continue; - lab0: - z->c = c; - break; - } - return 1; +static int +r_exception1(struct SN_env * z) +{ + int among_var; + + z->bra = z->c; /* [, line 159 */ + among_var = find_among(z, a_9, 18); /* substring, line 159 */ + if (!(among_var)) + return 0; + z->ket = z->c; /* ], line 159 */ + if (z->c < z->l) + return 0; /* atlimit, line 159 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_from_s(z, 3, s_36); /* <-, line 163 */ + break; + case 2: + slice_from_s(z, 3, s_37); /* <-, line 164 */ + break; + case 3: + slice_from_s(z, 3, s_38); /* <-, line 165 */ + break; + case 4: + slice_from_s(z, 3, s_39); /* <-, line 166 */ + break; + case 5: + slice_from_s(z, 3, s_40); /* <-, line 167 */ + break; + case 6: + slice_from_s(z, 3, s_41); /* <-, line 171 */ + break; + case 7: + slice_from_s(z, 5, s_42); /* <-, line 172 */ + break; + case 8: + slice_from_s(z, 4, s_43); /* <-, line 173 */ + break; + case 9: + slice_from_s(z, 5, s_44); /* <-, line 174 */ + break; + case 10: + slice_from_s(z, 4, s_45); /* <-, line 175 */ + break; + case 11: + slice_from_s(z, 5, s_46); /* <-, line 176 */ + break; + } + return 1; } -extern int english_stem(struct SN_env * z) { - { int c = z->c; /* or, line 196 */ - if (!r_exception1(z)) goto lab1; /* call exception1, line 196 */ - goto lab0; - lab1: - z->c = c; - { int c_test = z->c; /* test, line 198 */ - { int c = z->c + 3; - if (0 > c || c > z->l) return 0; - z->c = c; /* hop, line 198 */ - } - z->c = c_test; - } - { int c = z->c; /* do, line 199 */ - if (!r_prelude(z)) goto lab2; /* call prelude, line 199 */ - lab2: - z->c = c; - } - { int c = z->c; /* do, line 200 */ - if (!r_mark_regions(z)) goto lab3; /* call mark_regions, line 200 */ - lab3: - z->c = c; - } - z->lb = z->c; z->c = z->l; /* backwards, line 201 */ - - { int m = z->l - z->c; /* do, line 203 */ - if (!r_Step_1a(z)) goto lab4; /* call Step_1a, line 203 */ - lab4: - z->c = z->l - m; - } - { int m = z->l - z->c; /* or, line 205 */ - if (!r_exception2(z)) goto lab6; /* call exception2, line 205 */ - goto lab5; - lab6: - z->c = z->l - m; - { int m = z->l - z->c; /* do, line 207 */ - if (!r_Step_1b(z)) goto lab7; /* call Step_1b, line 207 */ - lab7: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 208 */ - if (!r_Step_1c(z)) goto lab8; /* call Step_1c, line 208 */ - lab8: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 210 */ - if (!r_Step_2(z)) goto lab9; /* call Step_2, line 210 */ - lab9: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 211 */ - if (!r_Step_3(z)) goto lab10; /* call Step_3, line 211 */ - lab10: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 212 */ - if (!r_Step_4(z)) goto lab11; /* call Step_4, line 212 */ - lab11: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 214 */ - if (!r_Step_5(z)) goto lab12; /* call Step_5, line 214 */ - lab12: - z->c = z->l - m; - } - } - lab5: - z->c = z->lb; - { int c = z->c; /* do, line 217 */ - if (!r_postlude(z)) goto lab13; /* call postlude, line 217 */ - lab13: - z->c = c; - } - } +static int +r_postlude(struct SN_env * z) +{ + if (!(z->B[0])) + return 0; /* Boolean test Y_found, line 192 */ + while (1) + { /* repeat, line 192 */ + int c = z->c; + + while (1) + { /* goto, line 192 */ + int c = z->c; + + z->bra = z->c; /* [, line 192 */ + if (!(eq_s(z, 1, s_47))) + goto lab1; + z->ket = z->c; /* ], line 192 */ + z->c = c; + break; + lab1: + z->c = c; + if (z->c >= z->l) + goto lab0; + z->c++; + } + slice_from_s(z, 1, s_48); /* <-, line 192 */ + continue; lab0: - return 1; + z->c = c; + break; + } + return 1; } -extern struct SN_env * english_create_env(void) { return SN_create_env(0, 2, 1); } +extern int +english_stem(struct SN_env * z) +{ + { + int c = z->c; /* or, line 196 */ + + if (!r_exception1(z)) + goto lab1; /* call exception1, line 196 */ + goto lab0; +lab1: + z->c = c; + { + int c_test = z->c; /* test, line 198 */ + + { + int c = z->c + 3; + + if (0 > c || c > z->l) + return 0; + z->c = c; /* hop, line 198 */ + } + z->c = c_test; + } + { + int c = z->c; /* do, line 199 */ + + if (!r_prelude(z)) + goto lab2; /* call prelude, line 199 */ + lab2: + z->c = c; + } + { + int c = z->c; /* do, line 200 */ -extern void english_close_env(struct SN_env * z) { SN_close_env(z); } + if (!r_mark_regions(z)) + goto lab3; /* call mark_regions, line 200 */ + lab3: + z->c = c; + } + z->lb = z->c; + z->c = z->l; /* backwards, line 201 */ + { + int m = z->l - z->c; /* do, line 203 */ + + if (!r_Step_1a(z)) + goto lab4; /* call Step_1a, line 203 */ + lab4: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* or, line 205 */ + + if (!r_exception2(z)) + goto lab6; /* call exception2, line 205 */ + goto lab5; + lab6: + z->c = z->l - m; + { + int m = z->l - z->c; /* do, line 207 */ + + if (!r_Step_1b(z)) + goto lab7; /* call Step_1b, line 207 */ + lab7: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 208 */ + + if (!r_Step_1c(z)) + goto lab8; /* call Step_1c, line 208 */ + lab8: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 210 */ + + if (!r_Step_2(z)) + goto lab9; /* call Step_2, line 210 */ + lab9: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 211 */ + + if (!r_Step_3(z)) + goto lab10; /* call Step_3, line 211 */ + lab10: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 212 */ + + if (!r_Step_4(z)) + goto lab11; /* call Step_4, line 212 */ + lab11: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 214 */ + + if (!r_Step_5(z)) + goto lab12; /* call Step_5, line 214 */ + lab12: + z->c = z->l - m; + } + } +lab5: + z->c = z->lb; + { + int c = z->c; /* do, line 217 */ + + if (!r_postlude(z)) + goto lab13; /* call postlude, line 217 */ + lab13: + z->c = c; + } + } +lab0: + return 1; +} + +extern struct SN_env *english_create_env(void) +{ + return SN_create_env(0, 2, 1); +} + +extern void english_close_env(struct SN_env * z) +{ + SN_close_env(z); +} diff --git a/contrib/tsearch2/snowball/english_stem.h b/contrib/tsearch2/snowball/english_stem.h index bfefcd56565..7a52f9cde13 100644 --- a/contrib/tsearch2/snowball/english_stem.h +++ b/contrib/tsearch2/snowball/english_stem.h @@ -1,8 +1,7 @@ /* This file was generated automatically by the Snowball to ANSI C compiler */ -extern struct SN_env * english_create_env(void); +extern struct SN_env *english_create_env(void); extern void english_close_env(struct SN_env * z); -extern int english_stem(struct SN_env * z); - +extern int english_stem(struct SN_env * z); diff --git a/contrib/tsearch2/snowball/header.h b/contrib/tsearch2/snowball/header.h index 6b636972022..4d4f127d6a1 100644 --- a/contrib/tsearch2/snowball/header.h +++ b/contrib/tsearch2/snowball/header.h @@ -2,41 +2,42 @@ #define HEAD 2*sizeof(int) -#define SIZE(p) ((int *)(p))[-1] +#define SIZE(p) ((int *)(p))[-1] #define SET_SIZE(p, n) ((int *)(p))[-1] = n #define CAPACITY(p) ((int *)(p))[-2] struct among -{ int s_size; /* number of chars in string */ - symbol * s; /* search string */ - int substring_i;/* index to longest matching substring */ - int result; /* result of the lookup */ - int (* function)(struct SN_env *); +{ + int s_size; /* number of chars in string */ + symbol *s; /* search string */ + int substring_i; /* index to longest matching substring */ + int result; /* result of the lookup */ + int (*function) (struct SN_env *); }; -extern symbol * create_s(void); +extern symbol *create_s(void); extern void lose_s(symbol * p); -extern int in_grouping(struct SN_env * z, unsigned char * s, int min, int max); -extern int in_grouping_b(struct SN_env * z, unsigned char * s, int min, int max); -extern int out_grouping(struct SN_env * z, unsigned char * s, int min, int max); -extern int out_grouping_b(struct SN_env * z, unsigned char * s, int min, int max); +extern int in_grouping(struct SN_env * z, unsigned char *s, int min, int max); +extern int in_grouping_b(struct SN_env * z, unsigned char *s, int min, int max); +extern int out_grouping(struct SN_env * z, unsigned char *s, int min, int max); +extern int out_grouping_b(struct SN_env * z, unsigned char *s, int min, int max); -extern int in_range(struct SN_env * z, int min, int max); -extern int in_range_b(struct SN_env * z, int min, int max); -extern int out_range(struct SN_env * z, int min, int max); -extern int out_range_b(struct SN_env * z, int min, int max); +extern int in_range(struct SN_env * z, int min, int max); +extern int in_range_b(struct SN_env * z, int min, int max); +extern int out_range(struct SN_env * z, int min, int max); +extern int out_range_b(struct SN_env * z, int min, int max); -extern int eq_s(struct SN_env * z, int s_size, symbol * s); -extern int eq_s_b(struct SN_env * z, int s_size, symbol * s); -extern int eq_v(struct SN_env * z, symbol * p); -extern int eq_v_b(struct SN_env * z, symbol * p); +extern int eq_s(struct SN_env * z, int s_size, symbol * s); +extern int eq_s_b(struct SN_env * z, int s_size, symbol * s); +extern int eq_v(struct SN_env * z, symbol * p); +extern int eq_v_b(struct SN_env * z, symbol * p); -extern int find_among(struct SN_env * z, struct among * v, int v_size); -extern int find_among_b(struct SN_env * z, struct among * v, int v_size); +extern int find_among(struct SN_env * z, struct among * v, int v_size); +extern int find_among_b(struct SN_env * z, struct among * v, int v_size); -extern symbol * increase_size(symbol * p, int n); -extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s); +extern symbol *increase_size(symbol * p, int n); +extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s); extern void slice_from_s(struct SN_env * z, int s_size, symbol * s); extern void slice_from_v(struct SN_env * z, symbol * p); extern void slice_del(struct SN_env * z); @@ -44,8 +45,7 @@ extern void slice_del(struct SN_env * z); extern void insert_s(struct SN_env * z, int bra, int ket, int s_size, symbol * s); extern void insert_v(struct SN_env * z, int bra, int ket, symbol * p); -extern symbol * slice_to(struct SN_env * z, symbol * p); -extern symbol * assign_to(struct SN_env * z, symbol * p); +extern symbol *slice_to(struct SN_env * z, symbol * p); +extern symbol *assign_to(struct SN_env * z, symbol * p); extern void debug(struct SN_env * z, int number, int line_count); - diff --git a/contrib/tsearch2/snowball/russian_stem.c b/contrib/tsearch2/snowball/russian_stem.c index 14fd49156b7..db698ce53fb 100644 --- a/contrib/tsearch2/snowball/russian_stem.c +++ b/contrib/tsearch2/snowball/russian_stem.c @@ -3,624 +3,762 @@ #include "header.h" -extern int russian_stem(struct SN_env * z); -static int r_tidy_up(struct SN_env * z); -static int r_derivational(struct SN_env * z); -static int r_noun(struct SN_env * z); -static int r_verb(struct SN_env * z); -static int r_reflexive(struct SN_env * z); -static int r_adjectival(struct SN_env * z); -static int r_adjective(struct SN_env * z); -static int r_perfective_gerund(struct SN_env * z); -static int r_R2(struct SN_env * z); -static int r_mark_regions(struct SN_env * z); - -extern struct SN_env * russian_create_env(void); +extern int russian_stem(struct SN_env * z); +static int r_tidy_up(struct SN_env * z); +static int r_derivational(struct SN_env * z); +static int r_noun(struct SN_env * z); +static int r_verb(struct SN_env * z); +static int r_reflexive(struct SN_env * z); +static int r_adjectival(struct SN_env * z); +static int r_adjective(struct SN_env * z); +static int r_perfective_gerund(struct SN_env * z); +static int r_R2(struct SN_env * z); +static int r_mark_regions(struct SN_env * z); + +extern struct SN_env *russian_create_env(void); extern void russian_close_env(struct SN_env * z); -static symbol s_0_0[3] = { 215, 219, 201 }; -static symbol s_0_1[4] = { 201, 215, 219, 201 }; -static symbol s_0_2[4] = { 217, 215, 219, 201 }; -static symbol s_0_3[1] = { 215 }; -static symbol s_0_4[2] = { 201, 215 }; -static symbol s_0_5[2] = { 217, 215 }; -static symbol s_0_6[5] = { 215, 219, 201, 211, 216 }; -static symbol s_0_7[6] = { 201, 215, 219, 201, 211, 216 }; -static symbol s_0_8[6] = { 217, 215, 219, 201, 211, 216 }; +static symbol s_0_0[3] = {215, 219, 201}; +static symbol s_0_1[4] = {201, 215, 219, 201}; +static symbol s_0_2[4] = {217, 215, 219, 201}; +static symbol s_0_3[1] = {215}; +static symbol s_0_4[2] = {201, 215}; +static symbol s_0_5[2] = {217, 215}; +static symbol s_0_6[5] = {215, 219, 201, 211, 216}; +static symbol s_0_7[6] = {201, 215, 219, 201, 211, 216}; +static symbol s_0_8[6] = {217, 215, 219, 201, 211, 216}; static struct among a_0[9] = { -/* 0 */ { 3, s_0_0, -1, 1, 0}, -/* 1 */ { 4, s_0_1, 0, 2, 0}, -/* 2 */ { 4, s_0_2, 0, 2, 0}, -/* 3 */ { 1, s_0_3, -1, 1, 0}, -/* 4 */ { 2, s_0_4, 3, 2, 0}, -/* 5 */ { 2, s_0_5, 3, 2, 0}, -/* 6 */ { 5, s_0_6, -1, 1, 0}, -/* 7 */ { 6, s_0_7, 6, 2, 0}, -/* 8 */ { 6, s_0_8, 6, 2, 0} + /* 0 */ {3, s_0_0, -1, 1, 0}, + /* 1 */ {4, s_0_1, 0, 2, 0}, + /* 2 */ {4, s_0_2, 0, 2, 0}, + /* 3 */ {1, s_0_3, -1, 1, 0}, + /* 4 */ {2, s_0_4, 3, 2, 0}, + /* 5 */ {2, s_0_5, 3, 2, 0}, + /* 6 */ {5, s_0_6, -1, 1, 0}, + /* 7 */ {6, s_0_7, 6, 2, 0}, + /* 8 */ {6, s_0_8, 6, 2, 0} }; -static symbol s_1_0[2] = { 192, 192 }; -static symbol s_1_1[2] = { 197, 192 }; -static symbol s_1_2[2] = { 207, 192 }; -static symbol s_1_3[2] = { 213, 192 }; -static symbol s_1_4[2] = { 197, 197 }; -static symbol s_1_5[2] = { 201, 197 }; -static symbol s_1_6[2] = { 207, 197 }; -static symbol s_1_7[2] = { 217, 197 }; -static symbol s_1_8[2] = { 201, 200 }; -static symbol s_1_9[2] = { 217, 200 }; -static symbol s_1_10[3] = { 201, 205, 201 }; -static symbol s_1_11[3] = { 217, 205, 201 }; -static symbol s_1_12[2] = { 197, 202 }; -static symbol s_1_13[2] = { 201, 202 }; -static symbol s_1_14[2] = { 207, 202 }; -static symbol s_1_15[2] = { 217, 202 }; -static symbol s_1_16[2] = { 197, 205 }; -static symbol s_1_17[2] = { 201, 205 }; -static symbol s_1_18[2] = { 207, 205 }; -static symbol s_1_19[2] = { 217, 205 }; -static symbol s_1_20[3] = { 197, 199, 207 }; -static symbol s_1_21[3] = { 207, 199, 207 }; -static symbol s_1_22[2] = { 193, 209 }; -static symbol s_1_23[2] = { 209, 209 }; -static symbol s_1_24[3] = { 197, 205, 213 }; -static symbol s_1_25[3] = { 207, 205, 213 }; +static symbol s_1_0[2] = {192, 192}; +static symbol s_1_1[2] = {197, 192}; +static symbol s_1_2[2] = {207, 192}; +static symbol s_1_3[2] = {213, 192}; +static symbol s_1_4[2] = {197, 197}; +static symbol s_1_5[2] = {201, 197}; +static symbol s_1_6[2] = {207, 197}; +static symbol s_1_7[2] = {217, 197}; +static symbol s_1_8[2] = {201, 200}; +static symbol s_1_9[2] = {217, 200}; +static symbol s_1_10[3] = {201, 205, 201}; +static symbol s_1_11[3] = {217, 205, 201}; +static symbol s_1_12[2] = {197, 202}; +static symbol s_1_13[2] = {201, 202}; +static symbol s_1_14[2] = {207, 202}; +static symbol s_1_15[2] = {217, 202}; +static symbol s_1_16[2] = {197, 205}; +static symbol s_1_17[2] = {201, 205}; +static symbol s_1_18[2] = {207, 205}; +static symbol s_1_19[2] = {217, 205}; +static symbol s_1_20[3] = {197, 199, 207}; +static symbol s_1_21[3] = {207, 199, 207}; +static symbol s_1_22[2] = {193, 209}; +static symbol s_1_23[2] = {209, 209}; +static symbol s_1_24[3] = {197, 205, 213}; +static symbol s_1_25[3] = {207, 205, 213}; static struct among a_1[26] = { -/* 0 */ { 2, s_1_0, -1, 1, 0}, -/* 1 */ { 2, s_1_1, -1, 1, 0}, -/* 2 */ { 2, s_1_2, -1, 1, 0}, -/* 3 */ { 2, s_1_3, -1, 1, 0}, -/* 4 */ { 2, s_1_4, -1, 1, 0}, -/* 5 */ { 2, s_1_5, -1, 1, 0}, -/* 6 */ { 2, s_1_6, -1, 1, 0}, -/* 7 */ { 2, s_1_7, -1, 1, 0}, -/* 8 */ { 2, s_1_8, -1, 1, 0}, -/* 9 */ { 2, s_1_9, -1, 1, 0}, -/* 10 */ { 3, s_1_10, -1, 1, 0}, -/* 11 */ { 3, s_1_11, -1, 1, 0}, -/* 12 */ { 2, s_1_12, -1, 1, 0}, -/* 13 */ { 2, s_1_13, -1, 1, 0}, -/* 14 */ { 2, s_1_14, -1, 1, 0}, -/* 15 */ { 2, s_1_15, -1, 1, 0}, -/* 16 */ { 2, s_1_16, -1, 1, 0}, -/* 17 */ { 2, s_1_17, -1, 1, 0}, -/* 18 */ { 2, s_1_18, -1, 1, 0}, -/* 19 */ { 2, s_1_19, -1, 1, 0}, -/* 20 */ { 3, s_1_20, -1, 1, 0}, -/* 21 */ { 3, s_1_21, -1, 1, 0}, -/* 22 */ { 2, s_1_22, -1, 1, 0}, -/* 23 */ { 2, s_1_23, -1, 1, 0}, -/* 24 */ { 3, s_1_24, -1, 1, 0}, -/* 25 */ { 3, s_1_25, -1, 1, 0} + /* 0 */ {2, s_1_0, -1, 1, 0}, + /* 1 */ {2, s_1_1, -1, 1, 0}, + /* 2 */ {2, s_1_2, -1, 1, 0}, + /* 3 */ {2, s_1_3, -1, 1, 0}, + /* 4 */ {2, s_1_4, -1, 1, 0}, + /* 5 */ {2, s_1_5, -1, 1, 0}, + /* 6 */ {2, s_1_6, -1, 1, 0}, + /* 7 */ {2, s_1_7, -1, 1, 0}, + /* 8 */ {2, s_1_8, -1, 1, 0}, + /* 9 */ {2, s_1_9, -1, 1, 0}, + /* 10 */ {3, s_1_10, -1, 1, 0}, + /* 11 */ {3, s_1_11, -1, 1, 0}, + /* 12 */ {2, s_1_12, -1, 1, 0}, + /* 13 */ {2, s_1_13, -1, 1, 0}, + /* 14 */ {2, s_1_14, -1, 1, 0}, + /* 15 */ {2, s_1_15, -1, 1, 0}, + /* 16 */ {2, s_1_16, -1, 1, 0}, + /* 17 */ {2, s_1_17, -1, 1, 0}, + /* 18 */ {2, s_1_18, -1, 1, 0}, + /* 19 */ {2, s_1_19, -1, 1, 0}, + /* 20 */ {3, s_1_20, -1, 1, 0}, + /* 21 */ {3, s_1_21, -1, 1, 0}, + /* 22 */ {2, s_1_22, -1, 1, 0}, + /* 23 */ {2, s_1_23, -1, 1, 0}, + /* 24 */ {3, s_1_24, -1, 1, 0}, + /* 25 */ {3, s_1_25, -1, 1, 0} }; -static symbol s_2_0[2] = { 197, 205 }; -static symbol s_2_1[2] = { 206, 206 }; -static symbol s_2_2[2] = { 215, 219 }; -static symbol s_2_3[3] = { 201, 215, 219 }; -static symbol s_2_4[3] = { 217, 215, 219 }; -static symbol s_2_5[1] = { 221 }; -static symbol s_2_6[2] = { 192, 221 }; -static symbol s_2_7[3] = { 213, 192, 221 }; +static symbol s_2_0[2] = {197, 205}; +static symbol s_2_1[2] = {206, 206}; +static symbol s_2_2[2] = {215, 219}; +static symbol s_2_3[3] = {201, 215, 219}; +static symbol s_2_4[3] = {217, 215, 219}; +static symbol s_2_5[1] = {221}; +static symbol s_2_6[2] = {192, 221}; +static symbol s_2_7[3] = {213, 192, 221}; static struct among a_2[8] = { -/* 0 */ { 2, s_2_0, -1, 1, 0}, -/* 1 */ { 2, s_2_1, -1, 1, 0}, -/* 2 */ { 2, s_2_2, -1, 1, 0}, -/* 3 */ { 3, s_2_3, 2, 2, 0}, -/* 4 */ { 3, s_2_4, 2, 2, 0}, -/* 5 */ { 1, s_2_5, -1, 1, 0}, -/* 6 */ { 2, s_2_6, 5, 1, 0}, -/* 7 */ { 3, s_2_7, 6, 2, 0} + /* 0 */ {2, s_2_0, -1, 1, 0}, + /* 1 */ {2, s_2_1, -1, 1, 0}, + /* 2 */ {2, s_2_2, -1, 1, 0}, + /* 3 */ {3, s_2_3, 2, 2, 0}, + /* 4 */ {3, s_2_4, 2, 2, 0}, + /* 5 */ {1, s_2_5, -1, 1, 0}, + /* 6 */ {2, s_2_6, 5, 1, 0}, + /* 7 */ {3, s_2_7, 6, 2, 0} }; -static symbol s_3_0[2] = { 211, 209 }; -static symbol s_3_1[2] = { 211, 216 }; +static symbol s_3_0[2] = {211, 209}; +static symbol s_3_1[2] = {211, 216}; static struct among a_3[2] = { -/* 0 */ { 2, s_3_0, -1, 1, 0}, -/* 1 */ { 2, s_3_1, -1, 1, 0} + /* 0 */ {2, s_3_0, -1, 1, 0}, + /* 1 */ {2, s_3_1, -1, 1, 0} }; -static symbol s_4_0[1] = { 192 }; -static symbol s_4_1[2] = { 213, 192 }; -static symbol s_4_2[2] = { 204, 193 }; -static symbol s_4_3[3] = { 201, 204, 193 }; -static symbol s_4_4[3] = { 217, 204, 193 }; -static symbol s_4_5[2] = { 206, 193 }; -static symbol s_4_6[3] = { 197, 206, 193 }; -static symbol s_4_7[3] = { 197, 212, 197 }; -static symbol s_4_8[3] = { 201, 212, 197 }; -static symbol s_4_9[3] = { 202, 212, 197 }; -static symbol s_4_10[4] = { 197, 202, 212, 197 }; -static symbol s_4_11[4] = { 213, 202, 212, 197 }; -static symbol s_4_12[2] = { 204, 201 }; -static symbol s_4_13[3] = { 201, 204, 201 }; -static symbol s_4_14[3] = { 217, 204, 201 }; -static symbol s_4_15[1] = { 202 }; -static symbol s_4_16[2] = { 197, 202 }; -static symbol s_4_17[2] = { 213, 202 }; -static symbol s_4_18[1] = { 204 }; -static symbol s_4_19[2] = { 201, 204 }; -static symbol s_4_20[2] = { 217, 204 }; -static symbol s_4_21[2] = { 197, 205 }; -static symbol s_4_22[2] = { 201, 205 }; -static symbol s_4_23[2] = { 217, 205 }; -static symbol s_4_24[1] = { 206 }; -static symbol s_4_25[2] = { 197, 206 }; -static symbol s_4_26[2] = { 204, 207 }; -static symbol s_4_27[3] = { 201, 204, 207 }; -static symbol s_4_28[3] = { 217, 204, 207 }; -static symbol s_4_29[2] = { 206, 207 }; -static symbol s_4_30[3] = { 197, 206, 207 }; -static symbol s_4_31[3] = { 206, 206, 207 }; -static symbol s_4_32[2] = { 192, 212 }; -static symbol s_4_33[3] = { 213, 192, 212 }; -static symbol s_4_34[2] = { 197, 212 }; -static symbol s_4_35[3] = { 213, 197, 212 }; -static symbol s_4_36[2] = { 201, 212 }; -static symbol s_4_37[2] = { 209, 212 }; -static symbol s_4_38[2] = { 217, 212 }; -static symbol s_4_39[2] = { 212, 216 }; -static symbol s_4_40[3] = { 201, 212, 216 }; -static symbol s_4_41[3] = { 217, 212, 216 }; -static symbol s_4_42[3] = { 197, 219, 216 }; -static symbol s_4_43[3] = { 201, 219, 216 }; -static symbol s_4_44[2] = { 206, 217 }; -static symbol s_4_45[3] = { 197, 206, 217 }; +static symbol s_4_0[1] = {192}; +static symbol s_4_1[2] = {213, 192}; +static symbol s_4_2[2] = {204, 193}; +static symbol s_4_3[3] = {201, 204, 193}; +static symbol s_4_4[3] = {217, 204, 193}; +static symbol s_4_5[2] = {206, 193}; +static symbol s_4_6[3] = {197, 206, 193}; +static symbol s_4_7[3] = {197, 212, 197}; +static symbol s_4_8[3] = {201, 212, 197}; +static symbol s_4_9[3] = {202, 212, 197}; +static symbol s_4_10[4] = {197, 202, 212, 197}; +static symbol s_4_11[4] = {213, 202, 212, 197}; +static symbol s_4_12[2] = {204, 201}; +static symbol s_4_13[3] = {201, 204, 201}; +static symbol s_4_14[3] = {217, 204, 201}; +static symbol s_4_15[1] = {202}; +static symbol s_4_16[2] = {197, 202}; +static symbol s_4_17[2] = {213, 202}; +static symbol s_4_18[1] = {204}; +static symbol s_4_19[2] = {201, 204}; +static symbol s_4_20[2] = {217, 204}; +static symbol s_4_21[2] = {197, 205}; +static symbol s_4_22[2] = {201, 205}; +static symbol s_4_23[2] = {217, 205}; +static symbol s_4_24[1] = {206}; +static symbol s_4_25[2] = {197, 206}; +static symbol s_4_26[2] = {204, 207}; +static symbol s_4_27[3] = {201, 204, 207}; +static symbol s_4_28[3] = {217, 204, 207}; +static symbol s_4_29[2] = {206, 207}; +static symbol s_4_30[3] = {197, 206, 207}; +static symbol s_4_31[3] = {206, 206, 207}; +static symbol s_4_32[2] = {192, 212}; +static symbol s_4_33[3] = {213, 192, 212}; +static symbol s_4_34[2] = {197, 212}; +static symbol s_4_35[3] = {213, 197, 212}; +static symbol s_4_36[2] = {201, 212}; +static symbol s_4_37[2] = {209, 212}; +static symbol s_4_38[2] = {217, 212}; +static symbol s_4_39[2] = {212, 216}; +static symbol s_4_40[3] = {201, 212, 216}; +static symbol s_4_41[3] = {217, 212, 216}; +static symbol s_4_42[3] = {197, 219, 216}; +static symbol s_4_43[3] = {201, 219, 216}; +static symbol s_4_44[2] = {206, 217}; +static symbol s_4_45[3] = {197, 206, 217}; static struct among a_4[46] = { -/* 0 */ { 1, s_4_0, -1, 2, 0}, -/* 1 */ { 2, s_4_1, 0, 2, 0}, -/* 2 */ { 2, s_4_2, -1, 1, 0}, -/* 3 */ { 3, s_4_3, 2, 2, 0}, -/* 4 */ { 3, s_4_4, 2, 2, 0}, -/* 5 */ { 2, s_4_5, -1, 1, 0}, -/* 6 */ { 3, s_4_6, 5, 2, 0}, -/* 7 */ { 3, s_4_7, -1, 1, 0}, -/* 8 */ { 3, s_4_8, -1, 2, 0}, -/* 9 */ { 3, s_4_9, -1, 1, 0}, -/* 10 */ { 4, s_4_10, 9, 2, 0}, -/* 11 */ { 4, s_4_11, 9, 2, 0}, -/* 12 */ { 2, s_4_12, -1, 1, 0}, -/* 13 */ { 3, s_4_13, 12, 2, 0}, -/* 14 */ { 3, s_4_14, 12, 2, 0}, -/* 15 */ { 1, s_4_15, -1, 1, 0}, -/* 16 */ { 2, s_4_16, 15, 2, 0}, -/* 17 */ { 2, s_4_17, 15, 2, 0}, -/* 18 */ { 1, s_4_18, -1, 1, 0}, -/* 19 */ { 2, s_4_19, 18, 2, 0}, -/* 20 */ { 2, s_4_20, 18, 2, 0}, -/* 21 */ { 2, s_4_21, -1, 1, 0}, -/* 22 */ { 2, s_4_22, -1, 2, 0}, -/* 23 */ { 2, s_4_23, -1, 2, 0}, -/* 24 */ { 1, s_4_24, -1, 1, 0}, -/* 25 */ { 2, s_4_25, 24, 2, 0}, -/* 26 */ { 2, s_4_26, -1, 1, 0}, -/* 27 */ { 3, s_4_27, 26, 2, 0}, -/* 28 */ { 3, s_4_28, 26, 2, 0}, -/* 29 */ { 2, s_4_29, -1, 1, 0}, -/* 30 */ { 3, s_4_30, 29, 2, 0}, -/* 31 */ { 3, s_4_31, 29, 1, 0}, -/* 32 */ { 2, s_4_32, -1, 1, 0}, -/* 33 */ { 3, s_4_33, 32, 2, 0}, -/* 34 */ { 2, s_4_34, -1, 1, 0}, -/* 35 */ { 3, s_4_35, 34, 2, 0}, -/* 36 */ { 2, s_4_36, -1, 2, 0}, -/* 37 */ { 2, s_4_37, -1, 2, 0}, -/* 38 */ { 2, s_4_38, -1, 2, 0}, -/* 39 */ { 2, s_4_39, -1, 1, 0}, -/* 40 */ { 3, s_4_40, 39, 2, 0}, -/* 41 */ { 3, s_4_41, 39, 2, 0}, -/* 42 */ { 3, s_4_42, -1, 1, 0}, -/* 43 */ { 3, s_4_43, -1, 2, 0}, -/* 44 */ { 2, s_4_44, -1, 1, 0}, -/* 45 */ { 3, s_4_45, 44, 2, 0} + /* 0 */ {1, s_4_0, -1, 2, 0}, + /* 1 */ {2, s_4_1, 0, 2, 0}, + /* 2 */ {2, s_4_2, -1, 1, 0}, + /* 3 */ {3, s_4_3, 2, 2, 0}, + /* 4 */ {3, s_4_4, 2, 2, 0}, + /* 5 */ {2, s_4_5, -1, 1, 0}, + /* 6 */ {3, s_4_6, 5, 2, 0}, + /* 7 */ {3, s_4_7, -1, 1, 0}, + /* 8 */ {3, s_4_8, -1, 2, 0}, + /* 9 */ {3, s_4_9, -1, 1, 0}, + /* 10 */ {4, s_4_10, 9, 2, 0}, + /* 11 */ {4, s_4_11, 9, 2, 0}, + /* 12 */ {2, s_4_12, -1, 1, 0}, + /* 13 */ {3, s_4_13, 12, 2, 0}, + /* 14 */ {3, s_4_14, 12, 2, 0}, + /* 15 */ {1, s_4_15, -1, 1, 0}, + /* 16 */ {2, s_4_16, 15, 2, 0}, + /* 17 */ {2, s_4_17, 15, 2, 0}, + /* 18 */ {1, s_4_18, -1, 1, 0}, + /* 19 */ {2, s_4_19, 18, 2, 0}, + /* 20 */ {2, s_4_20, 18, 2, 0}, + /* 21 */ {2, s_4_21, -1, 1, 0}, + /* 22 */ {2, s_4_22, -1, 2, 0}, + /* 23 */ {2, s_4_23, -1, 2, 0}, + /* 24 */ {1, s_4_24, -1, 1, 0}, + /* 25 */ {2, s_4_25, 24, 2, 0}, + /* 26 */ {2, s_4_26, -1, 1, 0}, + /* 27 */ {3, s_4_27, 26, 2, 0}, + /* 28 */ {3, s_4_28, 26, 2, 0}, + /* 29 */ {2, s_4_29, -1, 1, 0}, + /* 30 */ {3, s_4_30, 29, 2, 0}, + /* 31 */ {3, s_4_31, 29, 1, 0}, + /* 32 */ {2, s_4_32, -1, 1, 0}, + /* 33 */ {3, s_4_33, 32, 2, 0}, + /* 34 */ {2, s_4_34, -1, 1, 0}, + /* 35 */ {3, s_4_35, 34, 2, 0}, + /* 36 */ {2, s_4_36, -1, 2, 0}, + /* 37 */ {2, s_4_37, -1, 2, 0}, + /* 38 */ {2, s_4_38, -1, 2, 0}, + /* 39 */ {2, s_4_39, -1, 1, 0}, + /* 40 */ {3, s_4_40, 39, 2, 0}, + /* 41 */ {3, s_4_41, 39, 2, 0}, + /* 42 */ {3, s_4_42, -1, 1, 0}, + /* 43 */ {3, s_4_43, -1, 2, 0}, + /* 44 */ {2, s_4_44, -1, 1, 0}, + /* 45 */ {3, s_4_45, 44, 2, 0} }; -static symbol s_5_0[1] = { 192 }; -static symbol s_5_1[2] = { 201, 192 }; -static symbol s_5_2[2] = { 216, 192 }; -static symbol s_5_3[1] = { 193 }; -static symbol s_5_4[1] = { 197 }; -static symbol s_5_5[2] = { 201, 197 }; -static symbol s_5_6[2] = { 216, 197 }; -static symbol s_5_7[2] = { 193, 200 }; -static symbol s_5_8[2] = { 209, 200 }; -static symbol s_5_9[3] = { 201, 209, 200 }; -static symbol s_5_10[1] = { 201 }; -static symbol s_5_11[2] = { 197, 201 }; -static symbol s_5_12[2] = { 201, 201 }; -static symbol s_5_13[3] = { 193, 205, 201 }; -static symbol s_5_14[3] = { 209, 205, 201 }; -static symbol s_5_15[4] = { 201, 209, 205, 201 }; -static symbol s_5_16[1] = { 202 }; -static symbol s_5_17[2] = { 197, 202 }; -static symbol s_5_18[3] = { 201, 197, 202 }; -static symbol s_5_19[2] = { 201, 202 }; -static symbol s_5_20[2] = { 207, 202 }; -static symbol s_5_21[2] = { 193, 205 }; -static symbol s_5_22[2] = { 197, 205 }; -static symbol s_5_23[3] = { 201, 197, 205 }; -static symbol s_5_24[2] = { 207, 205 }; -static symbol s_5_25[2] = { 209, 205 }; -static symbol s_5_26[3] = { 201, 209, 205 }; -static symbol s_5_27[1] = { 207 }; -static symbol s_5_28[1] = { 209 }; -static symbol s_5_29[2] = { 201, 209 }; -static symbol s_5_30[2] = { 216, 209 }; -static symbol s_5_31[1] = { 213 }; -static symbol s_5_32[2] = { 197, 215 }; -static symbol s_5_33[2] = { 207, 215 }; -static symbol s_5_34[1] = { 216 }; -static symbol s_5_35[1] = { 217 }; +static symbol s_5_0[1] = {192}; +static symbol s_5_1[2] = {201, 192}; +static symbol s_5_2[2] = {216, 192}; +static symbol s_5_3[1] = {193}; +static symbol s_5_4[1] = {197}; +static symbol s_5_5[2] = {201, 197}; +static symbol s_5_6[2] = {216, 197}; +static symbol s_5_7[2] = {193, 200}; +static symbol s_5_8[2] = {209, 200}; +static symbol s_5_9[3] = {201, 209, 200}; +static symbol s_5_10[1] = {201}; +static symbol s_5_11[2] = {197, 201}; +static symbol s_5_12[2] = {201, 201}; +static symbol s_5_13[3] = {193, 205, 201}; +static symbol s_5_14[3] = {209, 205, 201}; +static symbol s_5_15[4] = {201, 209, 205, 201}; +static symbol s_5_16[1] = {202}; +static symbol s_5_17[2] = {197, 202}; +static symbol s_5_18[3] = {201, 197, 202}; +static symbol s_5_19[2] = {201, 202}; +static symbol s_5_20[2] = {207, 202}; +static symbol s_5_21[2] = {193, 205}; +static symbol s_5_22[2] = {197, 205}; +static symbol s_5_23[3] = {201, 197, 205}; +static symbol s_5_24[2] = {207, 205}; +static symbol s_5_25[2] = {209, 205}; +static symbol s_5_26[3] = {201, 209, 205}; +static symbol s_5_27[1] = {207}; +static symbol s_5_28[1] = {209}; +static symbol s_5_29[2] = {201, 209}; +static symbol s_5_30[2] = {216, 209}; +static symbol s_5_31[1] = {213}; +static symbol s_5_32[2] = {197, 215}; +static symbol s_5_33[2] = {207, 215}; +static symbol s_5_34[1] = {216}; +static symbol s_5_35[1] = {217}; static struct among a_5[36] = { -/* 0 */ { 1, s_5_0, -1, 1, 0}, -/* 1 */ { 2, s_5_1, 0, 1, 0}, -/* 2 */ { 2, s_5_2, 0, 1, 0}, -/* 3 */ { 1, s_5_3, -1, 1, 0}, -/* 4 */ { 1, s_5_4, -1, 1, 0}, -/* 5 */ { 2, s_5_5, 4, 1, 0}, -/* 6 */ { 2, s_5_6, 4, 1, 0}, -/* 7 */ { 2, s_5_7, -1, 1, 0}, -/* 8 */ { 2, s_5_8, -1, 1, 0}, -/* 9 */ { 3, s_5_9, 8, 1, 0}, -/* 10 */ { 1, s_5_10, -1, 1, 0}, -/* 11 */ { 2, s_5_11, 10, 1, 0}, -/* 12 */ { 2, s_5_12, 10, 1, 0}, -/* 13 */ { 3, s_5_13, 10, 1, 0}, -/* 14 */ { 3, s_5_14, 10, 1, 0}, -/* 15 */ { 4, s_5_15, 14, 1, 0}, -/* 16 */ { 1, s_5_16, -1, 1, 0}, -/* 17 */ { 2, s_5_17, 16, 1, 0}, -/* 18 */ { 3, s_5_18, 17, 1, 0}, -/* 19 */ { 2, s_5_19, 16, 1, 0}, -/* 20 */ { 2, s_5_20, 16, 1, 0}, -/* 21 */ { 2, s_5_21, -1, 1, 0}, -/* 22 */ { 2, s_5_22, -1, 1, 0}, -/* 23 */ { 3, s_5_23, 22, 1, 0}, -/* 24 */ { 2, s_5_24, -1, 1, 0}, -/* 25 */ { 2, s_5_25, -1, 1, 0}, -/* 26 */ { 3, s_5_26, 25, 1, 0}, -/* 27 */ { 1, s_5_27, -1, 1, 0}, -/* 28 */ { 1, s_5_28, -1, 1, 0}, -/* 29 */ { 2, s_5_29, 28, 1, 0}, -/* 30 */ { 2, s_5_30, 28, 1, 0}, -/* 31 */ { 1, s_5_31, -1, 1, 0}, -/* 32 */ { 2, s_5_32, -1, 1, 0}, -/* 33 */ { 2, s_5_33, -1, 1, 0}, -/* 34 */ { 1, s_5_34, -1, 1, 0}, -/* 35 */ { 1, s_5_35, -1, 1, 0} + /* 0 */ {1, s_5_0, -1, 1, 0}, + /* 1 */ {2, s_5_1, 0, 1, 0}, + /* 2 */ {2, s_5_2, 0, 1, 0}, + /* 3 */ {1, s_5_3, -1, 1, 0}, + /* 4 */ {1, s_5_4, -1, 1, 0}, + /* 5 */ {2, s_5_5, 4, 1, 0}, + /* 6 */ {2, s_5_6, 4, 1, 0}, + /* 7 */ {2, s_5_7, -1, 1, 0}, + /* 8 */ {2, s_5_8, -1, 1, 0}, + /* 9 */ {3, s_5_9, 8, 1, 0}, + /* 10 */ {1, s_5_10, -1, 1, 0}, + /* 11 */ {2, s_5_11, 10, 1, 0}, + /* 12 */ {2, s_5_12, 10, 1, 0}, + /* 13 */ {3, s_5_13, 10, 1, 0}, + /* 14 */ {3, s_5_14, 10, 1, 0}, + /* 15 */ {4, s_5_15, 14, 1, 0}, + /* 16 */ {1, s_5_16, -1, 1, 0}, + /* 17 */ {2, s_5_17, 16, 1, 0}, + /* 18 */ {3, s_5_18, 17, 1, 0}, + /* 19 */ {2, s_5_19, 16, 1, 0}, + /* 20 */ {2, s_5_20, 16, 1, 0}, + /* 21 */ {2, s_5_21, -1, 1, 0}, + /* 22 */ {2, s_5_22, -1, 1, 0}, + /* 23 */ {3, s_5_23, 22, 1, 0}, + /* 24 */ {2, s_5_24, -1, 1, 0}, + /* 25 */ {2, s_5_25, -1, 1, 0}, + /* 26 */ {3, s_5_26, 25, 1, 0}, + /* 27 */ {1, s_5_27, -1, 1, 0}, + /* 28 */ {1, s_5_28, -1, 1, 0}, + /* 29 */ {2, s_5_29, 28, 1, 0}, + /* 30 */ {2, s_5_30, 28, 1, 0}, + /* 31 */ {1, s_5_31, -1, 1, 0}, + /* 32 */ {2, s_5_32, -1, 1, 0}, + /* 33 */ {2, s_5_33, -1, 1, 0}, + /* 34 */ {1, s_5_34, -1, 1, 0}, + /* 35 */ {1, s_5_35, -1, 1, 0} }; -static symbol s_6_0[3] = { 207, 211, 212 }; -static symbol s_6_1[4] = { 207, 211, 212, 216 }; +static symbol s_6_0[3] = {207, 211, 212}; +static symbol s_6_1[4] = {207, 211, 212, 216}; static struct among a_6[2] = { -/* 0 */ { 3, s_6_0, -1, 1, 0}, -/* 1 */ { 4, s_6_1, -1, 1, 0} + /* 0 */ {3, s_6_0, -1, 1, 0}, + /* 1 */ {4, s_6_1, -1, 1, 0} }; -static symbol s_7_0[4] = { 197, 202, 219, 197 }; -static symbol s_7_1[1] = { 206 }; -static symbol s_7_2[1] = { 216 }; -static symbol s_7_3[3] = { 197, 202, 219 }; +static symbol s_7_0[4] = {197, 202, 219, 197}; +static symbol s_7_1[1] = {206}; +static symbol s_7_2[1] = {216}; +static symbol s_7_3[3] = {197, 202, 219}; static struct among a_7[4] = { -/* 0 */ { 4, s_7_0, -1, 1, 0}, -/* 1 */ { 1, s_7_1, -1, 2, 0}, -/* 2 */ { 1, s_7_2, -1, 3, 0}, -/* 3 */ { 3, s_7_3, -1, 1, 0} + /* 0 */ {4, s_7_0, -1, 1, 0}, + /* 1 */ {1, s_7_1, -1, 2, 0}, + /* 2 */ {1, s_7_2, -1, 3, 0}, + /* 3 */ {3, s_7_3, -1, 1, 0} }; -static unsigned char g_v[] = { 35, 130, 34, 18 }; - -static symbol s_0[] = { 193 }; -static symbol s_1[] = { 209 }; -static symbol s_2[] = { 193 }; -static symbol s_3[] = { 209 }; -static symbol s_4[] = { 193 }; -static symbol s_5[] = { 209 }; -static symbol s_6[] = { 206 }; -static symbol s_7[] = { 206 }; -static symbol s_8[] = { 206 }; -static symbol s_9[] = { 201 }; - -static int r_mark_regions(struct SN_env * z) { - z->I[0] = z->l; - z->I[1] = z->l; - { int c = z->c; /* do, line 100 */ - while(1) { /* gopast, line 101 */ - if (!(in_grouping(z, g_v, 192, 220))) goto lab1; - break; - lab1: - if (z->c >= z->l) goto lab0; - z->c++; - } - z->I[0] = z->c; /* setmark pV, line 101 */ - while(1) { /* gopast, line 101 */ - if (!(out_grouping(z, g_v, 192, 220))) goto lab2; - break; - lab2: - if (z->c >= z->l) goto lab0; - z->c++; - } - while(1) { /* gopast, line 102 */ - if (!(in_grouping(z, g_v, 192, 220))) goto lab3; - break; - lab3: - if (z->c >= z->l) goto lab0; - z->c++; - } - while(1) { /* gopast, line 102 */ - if (!(out_grouping(z, g_v, 192, 220))) goto lab4; - break; - lab4: - if (z->c >= z->l) goto lab0; - z->c++; - } - z->I[1] = z->c; /* setmark p2, line 102 */ - lab0: - z->c = c; - } - return 1; +static unsigned char g_v[] = {35, 130, 34, 18}; + +static symbol s_0[] = {193}; +static symbol s_1[] = {209}; +static symbol s_2[] = {193}; +static symbol s_3[] = {209}; +static symbol s_4[] = {193}; +static symbol s_5[] = {209}; +static symbol s_6[] = {206}; +static symbol s_7[] = {206}; +static symbol s_8[] = {206}; +static symbol s_9[] = {201}; + +static int +r_mark_regions(struct SN_env * z) +{ + z->I[0] = z->l; + z->I[1] = z->l; + { + int c = z->c; /* do, line 100 */ + + while (1) + { /* gopast, line 101 */ + if (!(in_grouping(z, g_v, 192, 220))) + goto lab1; + break; + lab1: + if (z->c >= z->l) + goto lab0; + z->c++; + } + z->I[0] = z->c; /* setmark pV, line 101 */ + while (1) + { /* gopast, line 101 */ + if (!(out_grouping(z, g_v, 192, 220))) + goto lab2; + break; + lab2: + if (z->c >= z->l) + goto lab0; + z->c++; + } + while (1) + { /* gopast, line 102 */ + if (!(in_grouping(z, g_v, 192, 220))) + goto lab3; + break; + lab3: + if (z->c >= z->l) + goto lab0; + z->c++; + } + while (1) + { /* gopast, line 102 */ + if (!(out_grouping(z, g_v, 192, 220))) + goto lab4; + break; + lab4: + if (z->c >= z->l) + goto lab0; + z->c++; + } + z->I[1] = z->c; /* setmark p2, line 102 */ +lab0: + z->c = c; + } + return 1; } -static int r_R2(struct SN_env * z) { - if (!(z->I[1] <= z->c)) return 0; - return 1; +static int +r_R2(struct SN_env * z) +{ + if (!(z->I[1] <= z->c)) + return 0; + return 1; } -static int r_perfective_gerund(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 111 */ - among_var = find_among_b(z, a_0, 9); /* substring, line 111 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 111 */ - switch(among_var) { - case 0: return 0; - case 1: - { int m = z->l - z->c; /* or, line 115 */ - if (!(eq_s_b(z, 1, s_0))) goto lab1; - goto lab0; - lab1: - z->c = z->l - m; - if (!(eq_s_b(z, 1, s_1))) return 0; - } - lab0: - slice_del(z); /* delete, line 115 */ - break; - case 2: - slice_del(z); /* delete, line 122 */ - break; - } - return 1; +static int +r_perfective_gerund(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 111 */ + among_var = find_among_b(z, a_0, 9); /* substring, line 111 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 111 */ + switch (among_var) + { + case 0: + return 0; + case 1: + { + int m = z->l - z->c; /* or, line 115 */ + + if (!(eq_s_b(z, 1, s_0))) + goto lab1; + goto lab0; + lab1: + z->c = z->l - m; + if (!(eq_s_b(z, 1, s_1))) + return 0; + } + lab0: + slice_del(z); /* delete, line 115 */ + break; + case 2: + slice_del(z); /* delete, line 122 */ + break; + } + return 1; } -static int r_adjective(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 127 */ - among_var = find_among_b(z, a_1, 26); /* substring, line 127 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 127 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 136 */ - break; - } - return 1; +static int +r_adjective(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 127 */ + among_var = find_among_b(z, a_1, 26); /* substring, line 127 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 127 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 136 */ + break; + } + return 1; } -static int r_adjectival(struct SN_env * z) { - int among_var; - if (!r_adjective(z)) return 0; /* call adjective, line 141 */ - { int m = z->l - z->c; /* try, line 148 */ - z->ket = z->c; /* [, line 149 */ - among_var = find_among_b(z, a_2, 8); /* substring, line 149 */ - if (!(among_var)) { z->c = z->l - m; goto lab0; } - z->bra = z->c; /* ], line 149 */ - switch(among_var) { - case 0: { z->c = z->l - m; goto lab0; } - case 1: - { int m = z->l - z->c; /* or, line 154 */ - if (!(eq_s_b(z, 1, s_2))) goto lab2; - goto lab1; - lab2: - z->c = z->l - m; - if (!(eq_s_b(z, 1, s_3))) { z->c = z->l - m; goto lab0; } - } - lab1: - slice_del(z); /* delete, line 154 */ - break; - case 2: - slice_del(z); /* delete, line 161 */ - break; - } - lab0: - ; - } - return 1; +static int +r_adjectival(struct SN_env * z) +{ + int among_var; + + if (!r_adjective(z)) + return 0; /* call adjective, line 141 */ + { + int m = z->l - z->c; /* try, line 148 */ + + z->ket = z->c; /* [, line 149 */ + among_var = find_among_b(z, a_2, 8); /* substring, line 149 */ + if (!(among_var)) + { + z->c = z->l - m; + goto lab0; + } + z->bra = z->c; /* ], line 149 */ + switch (among_var) + { + case 0: + { + z->c = z->l - m; + goto lab0; + } + case 1: + { + int m = z->l - z->c; /* or, line 154 */ + + if (!(eq_s_b(z, 1, s_2))) + goto lab2; + goto lab1; + lab2: + z->c = z->l - m; + if (!(eq_s_b(z, 1, s_3))) + { + z->c = z->l - m; + goto lab0; + } + } + lab1: + slice_del(z); /* delete, line 154 */ + break; + case 2: + slice_del(z); /* delete, line 161 */ + break; + } +lab0: + ; + } + return 1; } -static int r_reflexive(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 168 */ - among_var = find_among_b(z, a_3, 2); /* substring, line 168 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 168 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 171 */ - break; - } - return 1; +static int +r_reflexive(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 168 */ + among_var = find_among_b(z, a_3, 2); /* substring, line 168 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 168 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 171 */ + break; + } + return 1; } -static int r_verb(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 176 */ - among_var = find_among_b(z, a_4, 46); /* substring, line 176 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 176 */ - switch(among_var) { - case 0: return 0; - case 1: - { int m = z->l - z->c; /* or, line 182 */ - if (!(eq_s_b(z, 1, s_4))) goto lab1; - goto lab0; - lab1: - z->c = z->l - m; - if (!(eq_s_b(z, 1, s_5))) return 0; - } - lab0: - slice_del(z); /* delete, line 182 */ - break; - case 2: - slice_del(z); /* delete, line 190 */ - break; - } - return 1; +static int +r_verb(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 176 */ + among_var = find_among_b(z, a_4, 46); /* substring, line 176 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 176 */ + switch (among_var) + { + case 0: + return 0; + case 1: + { + int m = z->l - z->c; /* or, line 182 */ + + if (!(eq_s_b(z, 1, s_4))) + goto lab1; + goto lab0; + lab1: + z->c = z->l - m; + if (!(eq_s_b(z, 1, s_5))) + return 0; + } + lab0: + slice_del(z); /* delete, line 182 */ + break; + case 2: + slice_del(z); /* delete, line 190 */ + break; + } + return 1; } -static int r_noun(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 199 */ - among_var = find_among_b(z, a_5, 36); /* substring, line 199 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 199 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 206 */ - break; - } - return 1; +static int +r_noun(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 199 */ + among_var = find_among_b(z, a_5, 36); /* substring, line 199 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 199 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 206 */ + break; + } + return 1; } -static int r_derivational(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 215 */ - among_var = find_among_b(z, a_6, 2); /* substring, line 215 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 215 */ - if (!r_R2(z)) return 0; /* call R2, line 215 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 218 */ - break; - } - return 1; +static int +r_derivational(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 215 */ + among_var = find_among_b(z, a_6, 2); /* substring, line 215 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 215 */ + if (!r_R2(z)) + return 0; /* call R2, line 215 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 218 */ + break; + } + return 1; } -static int r_tidy_up(struct SN_env * z) { - int among_var; - z->ket = z->c; /* [, line 223 */ - among_var = find_among_b(z, a_7, 4); /* substring, line 223 */ - if (!(among_var)) return 0; - z->bra = z->c; /* ], line 223 */ - switch(among_var) { - case 0: return 0; - case 1: - slice_del(z); /* delete, line 227 */ - z->ket = z->c; /* [, line 228 */ - if (!(eq_s_b(z, 1, s_6))) return 0; - z->bra = z->c; /* ], line 228 */ - if (!(eq_s_b(z, 1, s_7))) return 0; - slice_del(z); /* delete, line 228 */ - break; - case 2: - if (!(eq_s_b(z, 1, s_8))) return 0; - slice_del(z); /* delete, line 231 */ - break; - case 3: - slice_del(z); /* delete, line 233 */ - break; - } - return 1; +static int +r_tidy_up(struct SN_env * z) +{ + int among_var; + + z->ket = z->c; /* [, line 223 */ + among_var = find_among_b(z, a_7, 4); /* substring, line 223 */ + if (!(among_var)) + return 0; + z->bra = z->c; /* ], line 223 */ + switch (among_var) + { + case 0: + return 0; + case 1: + slice_del(z); /* delete, line 227 */ + z->ket = z->c; /* [, line 228 */ + if (!(eq_s_b(z, 1, s_6))) + return 0; + z->bra = z->c; /* ], line 228 */ + if (!(eq_s_b(z, 1, s_7))) + return 0; + slice_del(z); /* delete, line 228 */ + break; + case 2: + if (!(eq_s_b(z, 1, s_8))) + return 0; + slice_del(z); /* delete, line 231 */ + break; + case 3: + slice_del(z); /* delete, line 233 */ + break; + } + return 1; } -extern int russian_stem(struct SN_env * z) { - { int c = z->c; /* do, line 240 */ - if (!r_mark_regions(z)) goto lab0; /* call mark_regions, line 240 */ - lab0: - z->c = c; - } - z->lb = z->c; z->c = z->l; /* backwards, line 241 */ - - { int m = z->l - z->c; /* setlimit, line 241 */ - int m3; - if (z->c < z->I[0]) return 0; - z->c = z->I[0]; /* tomark, line 241 */ - m3 = z->lb; z->lb = z->c; - z->c = z->l - m; - { int m = z->l - z->c; /* do, line 242 */ - { int m = z->l - z->c; /* or, line 243 */ - if (!r_perfective_gerund(z)) goto lab3; /* call perfective_gerund, line 243 */ - goto lab2; - lab3: - z->c = z->l - m; - { int m = z->l - z->c; /* try, line 244 */ - if (!r_reflexive(z)) { z->c = z->l - m; goto lab4; } /* call reflexive, line 244 */ - lab4: - ; - } - { int m = z->l - z->c; /* or, line 245 */ - if (!r_adjectival(z)) goto lab6; /* call adjectival, line 245 */ - goto lab5; - lab6: - z->c = z->l - m; - if (!r_verb(z)) goto lab7; /* call verb, line 245 */ - goto lab5; - lab7: - z->c = z->l - m; - if (!r_noun(z)) goto lab1; /* call noun, line 245 */ - } - lab5: - ; - } - lab2: - lab1: - z->c = z->l - m; - } - { int m = z->l - z->c; /* try, line 248 */ - z->ket = z->c; /* [, line 248 */ - if (!(eq_s_b(z, 1, s_9))) { z->c = z->l - m; goto lab8; } - z->bra = z->c; /* ], line 248 */ - slice_del(z); /* delete, line 248 */ - lab8: - ; - } - { int m = z->l - z->c; /* do, line 251 */ - if (!r_derivational(z)) goto lab9; /* call derivational, line 251 */ - lab9: - z->c = z->l - m; - } - { int m = z->l - z->c; /* do, line 252 */ - if (!r_tidy_up(z)) goto lab10; /* call tidy_up, line 252 */ - lab10: - z->c = z->l - m; - } - z->lb = m3; - } - z->c = z->lb; - return 1; +extern int +russian_stem(struct SN_env * z) +{ + { + int c = z->c; /* do, line 240 */ + + if (!r_mark_regions(z)) + goto lab0; /* call mark_regions, line 240 */ +lab0: + z->c = c; + } + z->lb = z->c; + z->c = z->l; /* backwards, line 241 */ + + { + int m = z->l - z->c; /* setlimit, line 241 */ + int m3; + + if (z->c < z->I[0]) + return 0; + z->c = z->I[0]; /* tomark, line 241 */ + m3 = z->lb; + z->lb = z->c; + z->c = z->l - m; + { + int m = z->l - z->c; /* do, line 242 */ + + { + int m = z->l - z->c; /* or, line 243 */ + + if (!r_perfective_gerund(z)) + goto lab3; /* call perfective_gerund, line 243 */ + goto lab2; + lab3: + z->c = z->l - m; + { + int m = z->l - z->c; /* try, line 244 */ + + if (!r_reflexive(z)) + { + z->c = z->l - m; + goto lab4; + } /* call reflexive, line 244 */ + lab4: + ; + } + { + int m = z->l - z->c; /* or, line 245 */ + + if (!r_adjectival(z)) + goto lab6; /* call adjectival, line 245 */ + goto lab5; + lab6: + z->c = z->l - m; + if (!r_verb(z)) + goto lab7; /* call verb, line 245 */ + goto lab5; + lab7: + z->c = z->l - m; + if (!r_noun(z)) + goto lab1; /* call noun, line 245 */ + } + lab5: + ; + } + lab2: + lab1: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* try, line 248 */ + + z->ket = z->c; /* [, line 248 */ + if (!(eq_s_b(z, 1, s_9))) + { + z->c = z->l - m; + goto lab8; + } + z->bra = z->c; /* ], line 248 */ + slice_del(z); /* delete, line 248 */ + lab8: + ; + } + { + int m = z->l - z->c; /* do, line 251 */ + + if (!r_derivational(z)) + goto lab9; /* call derivational, line 251 */ + lab9: + z->c = z->l - m; + } + { + int m = z->l - z->c; /* do, line 252 */ + + if (!r_tidy_up(z)) + goto lab10; /* call tidy_up, line 252 */ + lab10: + z->c = z->l - m; + } + z->lb = m3; + } + z->c = z->lb; + return 1; } -extern struct SN_env * russian_create_env(void) { return SN_create_env(0, 2, 0); } - -extern void russian_close_env(struct SN_env * z) { SN_close_env(z); } +extern struct SN_env *russian_create_env(void) +{ + return SN_create_env(0, 2, 0); +} +extern void russian_close_env(struct SN_env * z) +{ + SN_close_env(z); +} diff --git a/contrib/tsearch2/snowball/russian_stem.h b/contrib/tsearch2/snowball/russian_stem.h index 7dc26d45c82..69369e295b9 100644 --- a/contrib/tsearch2/snowball/russian_stem.h +++ b/contrib/tsearch2/snowball/russian_stem.h @@ -1,8 +1,7 @@ /* This file was generated automatically by the Snowball to ANSI C compiler */ -extern struct SN_env * russian_create_env(void); +extern struct SN_env *russian_create_env(void); extern void russian_close_env(struct SN_env * z); -extern int russian_stem(struct SN_env * z); - +extern int russian_stem(struct SN_env * z); diff --git a/contrib/tsearch2/snowball/utilities.c b/contrib/tsearch2/snowball/utilities.c index 5dc752445b5..374d2c11bb7 100644 --- a/contrib/tsearch2/snowball/utilities.c +++ b/contrib/tsearch2/snowball/utilities.c @@ -9,320 +9,507 @@ #define CREATE_SIZE 1 -extern symbol * create_s(void) -{ symbol * p = (symbol *) (HEAD + (char *) malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol))); - CAPACITY(p) = CREATE_SIZE; - SET_SIZE(p, CREATE_SIZE); - return p; +extern symbol * +create_s(void) +{ + symbol *p = (symbol *) (HEAD + (char *) malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol))); + + CAPACITY(p) = CREATE_SIZE; + SET_SIZE(p, CREATE_SIZE); + return p; } -extern void lose_s(symbol * p) { free((char *) p - HEAD); } +extern void lose_s(symbol * p) +{ + free((char *) p - HEAD); +} -extern int in_grouping(struct SN_env * z, unsigned char * s, int min, int max) -{ if (z->c >= z->l) return 0; - { int ch = z->p[z->c]; - if - (ch > max || (ch -= min) < 0 || - (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; - } - z->c++; return 1; +extern int +in_grouping(struct SN_env * z, unsigned char *s, int min, int max) +{ + if (z->c >= z->l) + return 0; + { + int ch = z->p[z->c]; + + if + (ch > max || (ch -= min) < 0 || + (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) + return 0; + } + z->c++; + return 1; } -extern int in_grouping_b(struct SN_env * z, unsigned char * s, int min, int max) -{ if (z->c <= z->lb) return 0; - { int ch = z->p[z->c - 1]; - if - (ch > max || (ch -= min) < 0 || - (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; - } - z->c--; return 1; +extern int +in_grouping_b(struct SN_env * z, unsigned char *s, int min, int max) +{ + if (z->c <= z->lb) + return 0; + { + int ch = z->p[z->c - 1]; + + if + (ch > max || (ch -= min) < 0 || + (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) + return 0; + } + z->c--; + return 1; } -extern int out_grouping(struct SN_env * z, unsigned char * s, int min, int max) -{ if (z->c >= z->l) return 0; - { int ch = z->p[z->c]; - unless - (ch > max || (ch -= min) < 0 || - (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; - } - z->c++; return 1; +extern int +out_grouping(struct SN_env * z, unsigned char *s, int min, int max) +{ + if (z->c >= z->l) + return 0; + { + int ch = z->p[z->c]; + + unless + (ch > max || (ch -= min) < 0 || + (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; + } + z->c++; + return 1; } -extern int out_grouping_b(struct SN_env * z, unsigned char * s, int min, int max) -{ if (z->c <= z->lb) return 0; - { int ch = z->p[z->c - 1]; - unless - (ch > max || (ch -= min) < 0 || - (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; - } - z->c--; return 1; +extern int +out_grouping_b(struct SN_env * z, unsigned char *s, int min, int max) +{ + if (z->c <= z->lb) + return 0; + { + int ch = z->p[z->c - 1]; + + unless + (ch > max || (ch -= min) < 0 || + (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0; + } + z->c--; + return 1; } -extern int in_range(struct SN_env * z, int min, int max) -{ if (z->c >= z->l) return 0; - { int ch = z->p[z->c]; - if - (ch > max || ch < min) return 0; - } - z->c++; return 1; +extern int +in_range(struct SN_env * z, int min, int max) +{ + if (z->c >= z->l) + return 0; + { + int ch = z->p[z->c]; + + if + (ch > max || ch < min) + return 0; + } + z->c++; + return 1; } -extern int in_range_b(struct SN_env * z, int min, int max) -{ if (z->c <= z->lb) return 0; - { int ch = z->p[z->c - 1]; - if - (ch > max || ch < min) return 0; - } - z->c--; return 1; +extern int +in_range_b(struct SN_env * z, int min, int max) +{ + if (z->c <= z->lb) + return 0; + { + int ch = z->p[z->c - 1]; + + if + (ch > max || ch < min) + return 0; + } + z->c--; + return 1; } -extern int out_range(struct SN_env * z, int min, int max) -{ if (z->c >= z->l) return 0; - { int ch = z->p[z->c]; - unless - (ch > max || ch < min) return 0; - } - z->c++; return 1; +extern int +out_range(struct SN_env * z, int min, int max) +{ + if (z->c >= z->l) + return 0; + { + int ch = z->p[z->c]; + + unless + (ch > max || ch < min) return 0; + } + z->c++; + return 1; } -extern int out_range_b(struct SN_env * z, int min, int max) -{ if (z->c <= z->lb) return 0; - { int ch = z->p[z->c - 1]; - unless - (ch > max || ch < min) return 0; - } - z->c--; return 1; +extern int +out_range_b(struct SN_env * z, int min, int max) +{ + if (z->c <= z->lb) + return 0; + { + int ch = z->p[z->c - 1]; + + unless + (ch > max || ch < min) return 0; + } + z->c--; + return 1; } -extern int eq_s(struct SN_env * z, int s_size, symbol * s) -{ if (z->l - z->c < s_size || - memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0; - z->c += s_size; return 1; +extern int +eq_s(struct SN_env * z, int s_size, symbol * s) +{ + if (z->l - z->c < s_size || + memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) + return 0; + z->c += s_size; + return 1; } -extern int eq_s_b(struct SN_env * z, int s_size, symbol * s) -{ if (z->c - z->lb < s_size || - memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0; - z->c -= s_size; return 1; +extern int +eq_s_b(struct SN_env * z, int s_size, symbol * s) +{ + if (z->c - z->lb < s_size || + memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) + return 0; + z->c -= s_size; + return 1; } -extern int eq_v(struct SN_env * z, symbol * p) -{ return eq_s(z, SIZE(p), p); +extern int +eq_v(struct SN_env * z, symbol * p) +{ + return eq_s(z, SIZE(p), p); } -extern int eq_v_b(struct SN_env * z, symbol * p) -{ return eq_s_b(z, SIZE(p), p); +extern int +eq_v_b(struct SN_env * z, symbol * p) +{ + return eq_s_b(z, SIZE(p), p); } -extern int find_among(struct SN_env * z, struct among * v, int v_size) +extern int +find_among(struct SN_env * z, struct among * v, int v_size) { - int i = 0; - int j = v_size; - - int c = z->c; int l = z->l; - symbol * q = z->p + c; - - struct among * w; - - int common_i = 0; - int common_j = 0; - - int first_key_inspected = 0; - - while(1) - { int k = i + ((j - i) >> 1); - int diff = 0; - int common = common_i < common_j ? common_i : common_j; /* smaller */ - w = v + k; - { int i; for (i = common; i < w->s_size; i++) - { if (c + common == l) { diff = -1; break; } - diff = q[common] - w->s[i]; - if (diff != 0) break; - common++; - } - } - if (diff < 0) { j = k; common_j = common; } - else { i = k; common_i = common; } - if (j - i <= 1) - { if (i > 0) break; /* v->s has been inspected */ - if (j == i) break; /* only one item in v */ - - /* - but now we need to go round once more to get - v->s inspected. This looks messy, but is actually - the optimal approach. */ - - if (first_key_inspected) break; - first_key_inspected = 1; - } - } - while(1) - { w = v + i; - if (common_i >= w->s_size) - { z->c = c + w->s_size; - if (w->function == 0) return w->result; - { int res = w->function(z); - z->c = c + w->s_size; - if (res) return w->result; - } - } - i = w->substring_i; - if (i < 0) return 0; - } + int i = 0; + int j = v_size; + + int c = z->c; + int l = z->l; + symbol *q = z->p + c; + + struct among *w; + + int common_i = 0; + int common_j = 0; + + int first_key_inspected = 0; + + while (1) + { + int k = i + ((j - i) >> 1); + int diff = 0; + int common = common_i < common_j ? common_i : common_j; /* smaller */ + + w = v + k; + { + int i; + + for (i = common; i < w->s_size; i++) + { + if (c + common == l) + { + diff = -1; + break; + } + diff = q[common] - w->s[i]; + if (diff != 0) + break; + common++; + } + } + if (diff < 0) + { + j = k; + common_j = common; + } + else + { + i = k; + common_i = common; + } + if (j - i <= 1) + { + if (i > 0) + break; /* v->s has been inspected */ + if (j == i) + break; /* only one item in v */ + + /* + * - but now we need to go round once more to get v->s + * inspected. This looks messy, but is actually the optimal + * approach. + */ + + if (first_key_inspected) + break; + first_key_inspected = 1; + } + } + while (1) + { + w = v + i; + if (common_i >= w->s_size) + { + z->c = c + w->s_size; + if (w->function == 0) + return w->result; + { + int res = w->function(z); + + z->c = c + w->s_size; + if (res) + return w->result; + } + } + i = w->substring_i; + if (i < 0) + return 0; + } } /* find_among_b is for backwards processing. Same comments apply */ -extern int find_among_b(struct SN_env * z, struct among * v, int v_size) +extern int +find_among_b(struct SN_env * z, struct among * v, int v_size) { - int i = 0; - int j = v_size; - - int c = z->c; int lb = z->lb; - symbol * q = z->p + c - 1; - - struct among * w; - - int common_i = 0; - int common_j = 0; - - int first_key_inspected = 0; - - while(1) - { int k = i + ((j - i) >> 1); - int diff = 0; - int common = common_i < common_j ? common_i : common_j; - w = v + k; - { int i; for (i = w->s_size - 1 - common; i >= 0; i--) - { if (c - common == lb) { diff = -1; break; } - diff = q[- common] - w->s[i]; - if (diff != 0) break; - common++; - } - } - if (diff < 0) { j = k; common_j = common; } - else { i = k; common_i = common; } - if (j - i <= 1) - { if (i > 0) break; - if (j == i) break; - if (first_key_inspected) break; - first_key_inspected = 1; - } - } - while(1) - { w = v + i; - if (common_i >= w->s_size) - { z->c = c - w->s_size; - if (w->function == 0) return w->result; - { int res = w->function(z); - z->c = c - w->s_size; - if (res) return w->result; - } - } - i = w->substring_i; - if (i < 0) return 0; - } + int i = 0; + int j = v_size; + + int c = z->c; + int lb = z->lb; + symbol *q = z->p + c - 1; + + struct among *w; + + int common_i = 0; + int common_j = 0; + + int first_key_inspected = 0; + + while (1) + { + int k = i + ((j - i) >> 1); + int diff = 0; + int common = common_i < common_j ? common_i : common_j; + + w = v + k; + { + int i; + + for (i = w->s_size - 1 - common; i >= 0; i--) + { + if (c - common == lb) + { + diff = -1; + break; + } + diff = q[-common] - w->s[i]; + if (diff != 0) + break; + common++; + } + } + if (diff < 0) + { + j = k; + common_j = common; + } + else + { + i = k; + common_i = common; + } + if (j - i <= 1) + { + if (i > 0) + break; + if (j == i) + break; + if (first_key_inspected) + break; + first_key_inspected = 1; + } + } + while (1) + { + w = v + i; + if (common_i >= w->s_size) + { + z->c = c - w->s_size; + if (w->function == 0) + return w->result; + { + int res = w->function(z); + + z->c = c - w->s_size; + if (res) + return w->result; + } + } + i = w->substring_i; + if (i < 0) + return 0; + } } -extern symbol * increase_size(symbol * p, int n) -{ int new_size = n + 20; - symbol * q = (symbol *) (HEAD + (char *) malloc(HEAD + (new_size + 1) * sizeof(symbol))); - CAPACITY(q) = new_size; - memmove(q, p, CAPACITY(p) * sizeof(symbol)); lose_s(p); return q; +extern symbol * +increase_size(symbol * p, int n) +{ + int new_size = n + 20; + symbol *q = (symbol *) (HEAD + (char *) malloc(HEAD + (new_size + 1) * sizeof(symbol))); + + CAPACITY(q) = new_size; + memmove(q, p, CAPACITY(p) * sizeof(symbol)); + lose_s(p); + return q; } /* to replace symbols between c_bra and c_ket in z->p by the s_size symbols at s */ -extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s) -{ int adjustment = s_size - (c_ket - c_bra); - int len = SIZE(z->p); - if (adjustment != 0) - { if (adjustment + len > CAPACITY(z->p)) z->p = increase_size(z->p, adjustment + len); - memmove(z->p + c_ket + adjustment, z->p + c_ket, (len - c_ket) * sizeof(symbol)); - SET_SIZE(z->p, adjustment + len); - z->l += adjustment; - if (z->c >= c_ket) z->c += adjustment; else - if (z->c > c_bra) z->c = c_bra; - } - unless (s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol)); - return adjustment; +extern int +replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s) +{ + int adjustment = s_size - (c_ket - c_bra); + int len = SIZE(z->p); + + if (adjustment != 0) + { + if (adjustment + len > CAPACITY(z->p)) + z->p = increase_size(z->p, adjustment + len); + memmove(z->p + c_ket + adjustment, z->p + c_ket, (len - c_ket) * sizeof(symbol)); + SET_SIZE(z->p, adjustment + len); + z->l += adjustment; + if (z->c >= c_ket) + z->c += adjustment; + else if (z->c > c_bra) + z->c = c_bra; + } + unless(s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol)); + return adjustment; } -static void slice_check(struct SN_env * z) +static void +slice_check(struct SN_env * z) { - if (!(0 <= z->bra && - z->bra <= z->ket && - z->ket <= z->l && - z->l <= SIZE(z->p))) /* this line could be removed */ - { - fprintf(stderr, "faulty slice operation:\n"); - debug(z, -1, 0); - exit(1); - } + if (!(0 <= z->bra && + z->bra <= z->ket && + z->ket <= z->l && + z->l <= SIZE(z->p))) /* this line could be removed */ + { + fprintf(stderr, "faulty slice operation:\n"); + debug(z, -1, 0); + exit(1); + } } -extern void slice_from_s(struct SN_env * z, int s_size, symbol * s) -{ slice_check(z); - replace_s(z, z->bra, z->ket, s_size, s); +extern void +slice_from_s(struct SN_env * z, int s_size, symbol * s) +{ + slice_check(z); + replace_s(z, z->bra, z->ket, s_size, s); } -extern void slice_from_v(struct SN_env * z, symbol * p) -{ slice_from_s(z, SIZE(p), p); +extern void +slice_from_v(struct SN_env * z, symbol * p) +{ + slice_from_s(z, SIZE(p), p); } -extern void slice_del(struct SN_env * z) -{ slice_from_s(z, 0, 0); +extern void +slice_del(struct SN_env * z) +{ + slice_from_s(z, 0, 0); } -extern void insert_s(struct SN_env * z, int bra, int ket, int s_size, symbol * s) -{ int adjustment = replace_s(z, bra, ket, s_size, s); - if (bra <= z->bra) z->bra += adjustment; - if (bra <= z->ket) z->ket += adjustment; +extern void +insert_s(struct SN_env * z, int bra, int ket, int s_size, symbol * s) +{ + int adjustment = replace_s(z, bra, ket, s_size, s); + + if (bra <= z->bra) + z->bra += adjustment; + if (bra <= z->ket) + z->ket += adjustment; } -extern void insert_v(struct SN_env * z, int bra, int ket, symbol * p) -{ int adjustment = replace_s(z, bra, ket, SIZE(p), p); - if (bra <= z->bra) z->bra += adjustment; - if (bra <= z->ket) z->ket += adjustment; +extern void +insert_v(struct SN_env * z, int bra, int ket, symbol * p) +{ + int adjustment = replace_s(z, bra, ket, SIZE(p), p); + + if (bra <= z->bra) + z->bra += adjustment; + if (bra <= z->ket) + z->ket += adjustment; } -extern symbol * slice_to(struct SN_env * z, symbol * p) -{ slice_check(z); - { int len = z->ket - z->bra; - if (CAPACITY(p) < len) p = increase_size(p, len); - memmove(p, z->p + z->bra, len * sizeof(symbol)); - SET_SIZE(p, len); - } - return p; +extern symbol * +slice_to(struct SN_env * z, symbol * p) +{ + slice_check(z); + { + int len = z->ket - z->bra; + + if (CAPACITY(p) < len) + p = increase_size(p, len); + memmove(p, z->p + z->bra, len * sizeof(symbol)); + SET_SIZE(p, len); + } + return p; } -extern symbol * assign_to(struct SN_env * z, symbol * p) -{ int len = z->l; - if (CAPACITY(p) < len) p = increase_size(p, len); - memmove(p, z->p, len * sizeof(symbol)); - SET_SIZE(p, len); - return p; +extern symbol * +assign_to(struct SN_env * z, symbol * p) +{ + int len = z->l; + + if (CAPACITY(p) < len) + p = increase_size(p, len); + memmove(p, z->p, len * sizeof(symbol)); + SET_SIZE(p, len); + return p; } -extern void debug(struct SN_env * z, int number, int line_count) -{ int i; - int limit = SIZE(z->p); - /*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/ - if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit); - for (i = 0; i <= limit; i++) - { if (z->lb == i) printf("{"); - if (z->bra == i) printf("["); - if (z->c == i) printf("|"); - if (z->ket == i) printf("]"); - if (z->l == i) printf("}"); - if (i < limit) - { int ch = z->p[i]; - if (ch == 0) ch = '#'; - printf("%c", ch); - } - } - printf("'\n"); +extern void +debug(struct SN_env * z, int number, int line_count) +{ + int i; + int limit = SIZE(z->p); + + /* if (number >= 0) printf("%3d (line %4d): '", number, line_count); */ + if (number >= 0) + printf("%3d (line %4d): [%d]'", number, line_count, limit); + for (i = 0; i <= limit; i++) + { + if (z->lb == i) + printf("{"); + if (z->bra == i) + printf("["); + if (z->c == i) + printf("|"); + if (z->ket == i) + printf("]"); + if (z->l == i) + printf("}"); + if (i < limit) + { + int ch = z->p[i]; + + if (ch == 0) + ch = '#'; + printf("%c", ch); + } + } + printf("'\n"); } diff --git a/contrib/tsearch2/stopword.c b/contrib/tsearch2/stopword.c index 2c2874b7f88..f91abeb460e 100644 --- a/contrib/tsearch2/stopword.c +++ b/contrib/tsearch2/stopword.c @@ -1,4 +1,4 @@ -/* +/* * stopword library * Teodor Sigaev <teodor@sigaev.ru> */ @@ -13,97 +13,114 @@ #define STOPBUFLEN 4096 -char* -lowerstr(char *str) { - char *ptr=str; - while(*ptr) { - *ptr = tolower(*(unsigned char*)ptr); +char * +lowerstr(char *str) +{ + char *ptr = str; + + while (*ptr) + { + *ptr = tolower(*(unsigned char *) ptr); ptr++; } return str; } void -freestoplist(StopList *s) { - char **ptr=s->stop; - if ( ptr ) - while( *ptr && s->len >0 ) { +freestoplist(StopList * s) +{ + char **ptr = s->stop; + + if (ptr) + while (*ptr && s->len > 0) + { free(*ptr); - ptr++; s->len--; - free(s->stop); - } - memset(s,0,sizeof(StopList)); + ptr++; + s->len--; + free(s->stop); + } + memset(s, 0, sizeof(StopList)); } void -readstoplist(text *in, StopList *s) { - char **stop=NULL; - s->len=0; - if ( in && VARSIZE(in) - VARHDRSZ > 0 ) { - char *filename=text2char(in); - FILE *hin=NULL; - char buf[STOPBUFLEN]; - int reallen=0; - - if ( (hin=fopen(filename,"r")) == NULL ) +readstoplist(text *in, StopList * s) +{ + char **stop = NULL; + + s->len = 0; + if (in && VARSIZE(in) - VARHDRSZ > 0) + { + char *filename = text2char(in); + FILE *hin = NULL; + char buf[STOPBUFLEN]; + int reallen = 0; + + if ((hin = fopen(filename, "r")) == NULL) ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not open file \"%s\": %m", - filename))); + filename))); - while( fgets(buf,STOPBUFLEN,hin) ) { - buf[strlen(buf)-1] = '\0'; - if ( *buf=='\0' ) continue; + while (fgets(buf, STOPBUFLEN, hin)) + { + buf[strlen(buf) - 1] = '\0'; + if (*buf == '\0') + continue; - if ( s->len>= reallen ) { - char **tmp; - reallen=(reallen) ? reallen*2 : 16; - tmp=(char**)realloc((void*)stop, sizeof(char*)*reallen); - if (!tmp) { + if (s->len >= reallen) + { + char **tmp; + + reallen = (reallen) ? reallen * 2 : 16; + tmp = (char **) realloc((void *) stop, sizeof(char *) * reallen); + if (!tmp) + { freestoplist(s); - fclose(hin); + fclose(hin); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - stop=tmp; + stop = tmp; } - - stop[s->len]=strdup(buf); - if ( !stop[s->len] ) { + + stop[s->len] = strdup(buf); + if (!stop[s->len]) + { freestoplist(s); - fclose(hin); + fclose(hin); ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); } - if ( s->wordop ) - stop[s->len]=(s->wordop)(stop[s->len]); + if (s->wordop) + stop[s->len] = (s->wordop) (stop[s->len]); - (s->len)++; + (s->len)++; } fclose(hin); - pfree(filename); + pfree(filename); } - s->stop=stop; -} + s->stop = stop; +} static int -comparestr(const void *a, const void *b) { - return strcmp( *(char**)a, *(char**)b ); +comparestr(const void *a, const void *b) +{ + return strcmp(*(char **) a, *(char **) b); } void -sortstoplist(StopList *s) { - if (s->stop && s->len>0) - qsort(s->stop, s->len, sizeof(char*), comparestr); +sortstoplist(StopList * s) +{ + if (s->stop && s->len > 0) + qsort(s->stop, s->len, sizeof(char *), comparestr); } bool -searchstoplist(StopList *s, char *key) { - if ( s->wordop ) - key=(*(s->wordop))(key); - return ( s->stop && s->len>0 && bsearch(&key, s->stop, s->len, sizeof(char*), comparestr) ) ? true : false; +searchstoplist(StopList * s, char *key) +{ + if (s->wordop) + key = (*(s->wordop)) (key); + return (s->stop && s->len > 0 && bsearch(&key, s->stop, s->len, sizeof(char *), comparestr)) ? true : false; } - - diff --git a/contrib/tsearch2/ts_cfg.c b/contrib/tsearch2/ts_cfg.c index d964aae573e..ad06d90d9ab 100644 --- a/contrib/tsearch2/ts_cfg.c +++ b/contrib/tsearch2/ts_cfg.c @@ -1,5 +1,5 @@ -/* - * interface functions to tscfg +/* + * interface functions to tscfg * Teodor Sigaev <teodor@sigaev.ru> */ #include <errno.h> @@ -23,263 +23,299 @@ /*********top interface**********/ -static void *plan_getcfg_bylocale=NULL; -static void *plan_getcfg=NULL; -static void *plan_getmap=NULL; -static void *plan_name2id=NULL; -static Oid current_cfg_id=0; +static void *plan_getcfg_bylocale = NULL; +static void *plan_getcfg = NULL; +static void *plan_getmap = NULL; +static void *plan_name2id = NULL; +static Oid current_cfg_id = 0; void -init_cfg(Oid id, TSCfgInfo *cfg) { - Oid arg[2]={ OIDOID, OIDOID }; - bool isnull; - Datum pars[2]={ ObjectIdGetDatum(id), ObjectIdGetDatum(id) } ; - int stat,i,j; - text *ptr; - text *prsname=NULL; - MemoryContext oldcontext; - - memset(cfg,0,sizeof(TSCfgInfo)); +init_cfg(Oid id, TSCfgInfo * cfg) +{ + Oid arg[2] = {OIDOID, OIDOID}; + bool isnull; + Datum pars[2] = {ObjectIdGetDatum(id), ObjectIdGetDatum(id)}; + int stat, + i, + j; + text *ptr; + text *prsname = NULL; + MemoryContext oldcontext; + + memset(cfg, 0, sizeof(TSCfgInfo)); SPI_connect(); - if ( !plan_getcfg ) { - plan_getcfg = SPI_saveplan( SPI_prepare( "select prs_name from pg_ts_cfg where oid = $1" , 1, arg ) ); - if ( !plan_getcfg ) + if (!plan_getcfg) + { + plan_getcfg = SPI_saveplan(SPI_prepare("select prs_name from pg_ts_cfg where oid = $1", 1, arg)); + if (!plan_getcfg) ts_error(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_getcfg, pars, " ", 1); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) { - prsname = (text*) DatumGetPointer( - SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) - ); + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + { + prsname = (text *) DatumGetPointer( + SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) + ); oldcontext = MemoryContextSwitchTo(TopMemoryContext); - prsname = ptextdup( prsname ); + prsname = ptextdup(prsname); MemoryContextSwitchTo(oldcontext); - - cfg->id=id; - } else + + cfg->id = id; + } + else ts_error(ERROR, "No tsearch cfg with id %d", id); - arg[0]=TEXTOID; - if ( !plan_getmap ) { - plan_getmap = SPI_saveplan( SPI_prepare( "select lt.tokid, pg_ts_cfgmap.dict_name from pg_ts_cfgmap, pg_ts_cfg, token_type( $1 ) as lt where lt.alias = pg_ts_cfgmap.tok_alias and pg_ts_cfgmap.ts_name = pg_ts_cfg.ts_name and pg_ts_cfg.oid= $2 order by lt.tokid desc;" , 2, arg ) ); - if ( !plan_getmap ) + arg[0] = TEXTOID; + if (!plan_getmap) + { + plan_getmap = SPI_saveplan(SPI_prepare("select lt.tokid, pg_ts_cfgmap.dict_name from pg_ts_cfgmap, pg_ts_cfg, token_type( $1 ) as lt where lt.alias = pg_ts_cfgmap.tok_alias and pg_ts_cfgmap.ts_name = pg_ts_cfg.ts_name and pg_ts_cfg.oid= $2 order by lt.tokid desc;", 2, arg)); + if (!plan_getmap) ts_error(ERROR, "SPI_prepare() failed"); } - pars[0]=PointerGetDatum( prsname ); + pars[0] = PointerGetDatum(prsname); stat = SPI_execp(plan_getmap, pars, " ", 0); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed <= 0 ) + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed <= 0) ts_error(ERROR, "No parser with id %d", id); - for(i=0;i<SPI_processed;i++) { - int lexid = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull)); - ArrayType *toasted_a = (ArrayType*)PointerGetDatum(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &isnull)); - ArrayType *a; - - if ( !cfg->map ) { - cfg->len=lexid+1; - cfg->map = (ListDictionary*)malloc( sizeof(ListDictionary)*cfg->len ); - if ( !cfg->map ) + for (i = 0; i < SPI_processed; i++) + { + int lexid = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull)); + ArrayType *toasted_a = (ArrayType *) PointerGetDatum(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &isnull)); + ArrayType *a; + + if (!cfg->map) + { + cfg->len = lexid + 1; + cfg->map = (ListDictionary *) malloc(sizeof(ListDictionary) * cfg->len); + if (!cfg->map) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - memset( cfg->map, 0, sizeof(ListDictionary)*cfg->len ); + memset(cfg->map, 0, sizeof(ListDictionary) * cfg->len); } if (isnull) continue; - a=(ArrayType*)PointerGetDatum( PG_DETOAST_DATUM( DatumGetPointer(toasted_a) ) ); - - if ( ARR_NDIM(a) != 1 ) - ts_error(ERROR,"Wrong dimension"); - if ( ARRNELEMS(a) < 1 ) + a = (ArrayType *) PointerGetDatum(PG_DETOAST_DATUM(DatumGetPointer(toasted_a))); + + if (ARR_NDIM(a) != 1) + ts_error(ERROR, "Wrong dimension"); + if (ARRNELEMS(a) < 1) continue; - cfg->map[lexid].len=ARRNELEMS(a); - cfg->map[lexid].dict_id=(Datum*)malloc( sizeof(Datum)*cfg->map[lexid].len ); - memset(cfg->map[lexid].dict_id,0,sizeof(Datum)*cfg->map[lexid].len ); - ptr=(text*)ARR_DATA_PTR(a); + cfg->map[lexid].len = ARRNELEMS(a); + cfg->map[lexid].dict_id = (Datum *) malloc(sizeof(Datum) * cfg->map[lexid].len); + memset(cfg->map[lexid].dict_id, 0, sizeof(Datum) * cfg->map[lexid].len); + ptr = (text *) ARR_DATA_PTR(a); oldcontext = MemoryContextSwitchTo(TopMemoryContext); - for(j=0;j<cfg->map[lexid].len;j++) { + for (j = 0; j < cfg->map[lexid].len; j++) + { cfg->map[lexid].dict_id[j] = PointerGetDatum(ptextdup(ptr)); - ptr=NEXTVAL(ptr); - } + ptr = NEXTVAL(ptr); + } MemoryContextSwitchTo(oldcontext); - if ( a != toasted_a ) + if (a != toasted_a) pfree(a); } - + SPI_finish(); - cfg->prs_id = name2id_prs( prsname ); + cfg->prs_id = name2id_prs(prsname); pfree(prsname); - for(i=0;i<cfg->len;i++) { - for(j=0;j<cfg->map[i].len;j++) { - ptr = (text*)DatumGetPointer( cfg->map[i].dict_id[j] ); - cfg->map[i].dict_id[j] = ObjectIdGetDatum( name2id_dict(ptr) ); + for (i = 0; i < cfg->len; i++) + { + for (j = 0; j < cfg->map[i].len; j++) + { + ptr = (text *) DatumGetPointer(cfg->map[i].dict_id[j]); + cfg->map[i].dict_id[j] = ObjectIdGetDatum(name2id_dict(ptr)); pfree(ptr); } } } -typedef struct { - TSCfgInfo *last_cfg; - int len; - int reallen; - TSCfgInfo *list; +typedef struct +{ + TSCfgInfo *last_cfg; + int len; + int reallen; + TSCfgInfo *list; SNMap name2id_map; -} CFGList; +} CFGList; -static CFGList CList = {NULL,0,0,NULL,{0,0,NULL}}; +static CFGList CList = {NULL, 0, 0, NULL, {0, 0, NULL}}; void -reset_cfg(void) { - freeSNMap( &(CList.name2id_map) ); - if ( CList.list ) { - int i,j; - for(i=0;i<CList.len;i++) - if ( CList.list[i].map ) { - for(j=0;j<CList.list[i].len;j++) - if ( CList.list[i].map[j].dict_id ) +reset_cfg(void) +{ + freeSNMap(&(CList.name2id_map)); + if (CList.list) + { + int i, + j; + + for (i = 0; i < CList.len; i++) + if (CList.list[i].map) + { + for (j = 0; j < CList.list[i].len; j++) + if (CList.list[i].map[j].dict_id) free(CList.list[i].map[j].dict_id); - free( CList.list[i].map ); + free(CList.list[i].map); } - free(CList.list); + free(CList.list); } - memset(&CList,0,sizeof(CFGList)); + memset(&CList, 0, sizeof(CFGList)); } static int -comparecfg(const void *a, const void *b) { - return ((TSCfgInfo*)a)->id - ((TSCfgInfo*)b)->id; +comparecfg(const void *a, const void *b) +{ + return ((TSCfgInfo *) a)->id - ((TSCfgInfo *) b)->id; } TSCfgInfo * -findcfg(Oid id) { +findcfg(Oid id) +{ /* last used cfg */ - if ( CList.last_cfg && CList.last_cfg->id==id ) + if (CList.last_cfg && CList.last_cfg->id == id) return CList.last_cfg; /* already used cfg */ - if ( CList.len != 0 ) { - TSCfgInfo key; - key.id=id; + if (CList.len != 0) + { + TSCfgInfo key; + + key.id = id; CList.last_cfg = bsearch(&key, CList.list, CList.len, sizeof(TSCfgInfo), comparecfg); - if ( CList.last_cfg != NULL ) + if (CList.last_cfg != NULL) return CList.last_cfg; } /* last chance */ - if ( CList.len==CList.reallen ) { - TSCfgInfo *tmp; - int reallen = ( CList.reallen ) ? 2*CList.reallen : 16; - tmp=(TSCfgInfo*)realloc(CList.list,sizeof(TSCfgInfo)*reallen); - if ( !tmp ) - ts_error(ERROR,"No memory"); - CList.reallen=reallen; - CList.list=tmp; + if (CList.len == CList.reallen) + { + TSCfgInfo *tmp; + int reallen = (CList.reallen) ? 2 * CList.reallen : 16; + + tmp = (TSCfgInfo *) realloc(CList.list, sizeof(TSCfgInfo) * reallen); + if (!tmp) + ts_error(ERROR, "No memory"); + CList.reallen = reallen; + CList.list = tmp; } - CList.last_cfg=&(CList.list[CList.len]); + CList.last_cfg = &(CList.list[CList.len]); init_cfg(id, CList.last_cfg); CList.len++; qsort(CList.list, CList.len, sizeof(TSCfgInfo), comparecfg); - return findcfg(id); /* qsort changed order!! */; + return findcfg(id); /* qsort changed order!! */ ; } Oid -name2id_cfg(text *name) { - Oid arg[1]={ TEXTOID }; - bool isnull; - Datum pars[1]={ PointerGetDatum(name) }; - int stat; - Oid id=findSNMap_t( &(CList.name2id_map), name ); - - if ( id ) +name2id_cfg(text *name) +{ + Oid arg[1] = {TEXTOID}; + bool isnull; + Datum pars[1] = {PointerGetDatum(name)}; + int stat; + Oid id = findSNMap_t(&(CList.name2id_map), name); + + if (id) return id; - + SPI_connect(); - if ( !plan_name2id ) { - plan_name2id = SPI_saveplan( SPI_prepare( "select oid from pg_ts_cfg where ts_name = $1" , 1, arg ) ); - if ( !plan_name2id ) + if (!plan_name2id) + { + plan_name2id = SPI_saveplan(SPI_prepare("select oid from pg_ts_cfg where ts_name = $1", 1, arg)); + if (!plan_name2id) /* internal error */ elog(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_name2id, pars, " ", 1); - if ( stat < 0 ) + if (stat < 0) /* internal error */ - elog (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) { - id=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - if ( isnull ) + elog(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + { + id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + if (isnull) ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("null id for tsearch config"))); - } else + } + else ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("no tsearch config"))); SPI_finish(); - addSNMap_t( &(CList.name2id_map), name, id ); + addSNMap_t(&(CList.name2id_map), name, id); return id; } -void -parsetext_v2(TSCfgInfo *cfg, PRSTEXT * prs, char *buf, int4 buflen) { - int type, lenlemm, i; - char *lemm=NULL; +void +parsetext_v2(TSCfgInfo * cfg, PRSTEXT * prs, char *buf, int4 buflen) +{ + int type, + lenlemm, + i; + char *lemm = NULL; WParserInfo *prsobj = findprs(cfg->prs_id); - prsobj->prs=(void*)DatumGetPointer( - FunctionCall2( - &(prsobj->start_info), - PointerGetDatum(buf), - Int32GetDatum(buflen) - ) - ); - - while( ( type=DatumGetInt32(FunctionCall3( - &(prsobj->getlexeme_info), - PointerGetDatum(prsobj->prs), - PointerGetDatum(&lemm), - PointerGetDatum(&lenlemm))) ) != 0 ) { - - if ( lenlemm >= MAXSTRLEN ) + prsobj->prs = (void *) DatumGetPointer( + FunctionCall2( + &(prsobj->start_info), + PointerGetDatum(buf), + Int32GetDatum(buflen) + ) + ); + + while ((type = DatumGetInt32(FunctionCall3( + &(prsobj->getlexeme_info), + PointerGetDatum(prsobj->prs), + PointerGetDatum(&lemm), + PointerGetDatum(&lenlemm)))) != 0) + { + + if (lenlemm >= MAXSTRLEN) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); - if ( type >= cfg->len ) /* skip this type of lexem */ - continue; - - for(i=0;i<cfg->map[type].len;i++) { - DictInfo *dict=finddict( DatumGetObjectId(cfg->map[type].dict_id[i]) ); - char **norms, **ptr; - - norms = ptr = (char**)DatumGetPointer( - FunctionCall3( - &(dict->lexize_info), - PointerGetDatum(dict->dictionary), - PointerGetDatum(lemm), - PointerGetDatum(lenlemm) - ) - ); - if ( !norms ) /* dictionary doesn't know this lexem */ + if (type >= cfg->len) /* skip this type of lexem */ + continue; + + for (i = 0; i < cfg->map[type].len; i++) + { + DictInfo *dict = finddict(DatumGetObjectId(cfg->map[type].dict_id[i])); + char **norms, + **ptr; + + norms = ptr = (char **) DatumGetPointer( + FunctionCall3( + &(dict->lexize_info), + PointerGetDatum(dict->dictionary), + PointerGetDatum(lemm), + PointerGetDatum(lenlemm) + ) + ); + if (!norms) /* dictionary doesn't know this lexem */ continue; - prs->pos++; /*set pos*/ + prs->pos++; /* set pos */ - while( *ptr ) { - if (prs->curwords == prs->lenwords) { + while (*ptr) + { + if (prs->curwords == prs->lenwords) + { prs->lenwords *= 2; prs->words = (WORD *) repalloc((void *) prs->words, prs->lenwords * sizeof(WORD)); } @@ -292,191 +328,220 @@ parsetext_v2(TSCfgInfo *cfg, PRSTEXT * prs, char *buf, int4 buflen) { prs->curwords++; } pfree(norms); - break; /* lexem already normalized or is stop word*/ + break; /* lexem already normalized or is stop + * word */ } } FunctionCall1( - &(prsobj->end_info), - PointerGetDatum(prsobj->prs) - ); + &(prsobj->end_info), + PointerGetDatum(prsobj->prs) + ); } static void -hladdword(HLPRSTEXT * prs, char *buf, int4 buflen, int type) { - while (prs->curwords >= prs->lenwords) { +hladdword(HLPRSTEXT * prs, char *buf, int4 buflen, int type) +{ + while (prs->curwords >= prs->lenwords) + { prs->lenwords *= 2; prs->words = (HLWORD *) repalloc((void *) prs->words, prs->lenwords * sizeof(HLWORD)); } - memset( &(prs->words[prs->curwords]), 0, sizeof(HLWORD) ); - prs->words[prs->curwords].type = (uint8)type; - prs->words[prs->curwords].len = buflen; + memset(&(prs->words[prs->curwords]), 0, sizeof(HLWORD)); + prs->words[prs->curwords].type = (uint8) type; + prs->words[prs->curwords].len = buflen; prs->words[prs->curwords].word = palloc(buflen); memcpy(prs->words[prs->curwords].word, buf, buflen); - prs->curwords++; + prs->curwords++; } static void -hlfinditem(HLPRSTEXT * prs, QUERYTYPE *query, char *buf, int buflen ) { - int i; - ITEM *item=GETQUERY(query); - HLWORD *word=&( prs->words[prs->curwords-1] ); - - while (prs->curwords + query->size >= prs->lenwords) { +hlfinditem(HLPRSTEXT * prs, QUERYTYPE * query, char *buf, int buflen) +{ + int i; + ITEM *item = GETQUERY(query); + HLWORD *word = &(prs->words[prs->curwords - 1]); + + while (prs->curwords + query->size >= prs->lenwords) + { prs->lenwords *= 2; prs->words = (HLWORD *) repalloc((void *) prs->words, prs->lenwords * sizeof(HLWORD)); } - for(i=0; i<query->size; i++) { - if ( item->type == VAL && item->length == buflen && strncmp( GETOPERAND(query) + item->distance, buf, buflen )==0 ) { - if ( word->item ) { - memcpy( &(prs->words[prs->curwords]), word, sizeof(HLWORD) ); - prs->words[prs->curwords].item=item; - prs->words[prs->curwords].repeated=1; + for (i = 0; i < query->size; i++) + { + if (item->type == VAL && item->length == buflen && strncmp(GETOPERAND(query) + item->distance, buf, buflen) == 0) + { + if (word->item) + { + memcpy(&(prs->words[prs->curwords]), word, sizeof(HLWORD)); + prs->words[prs->curwords].item = item; + prs->words[prs->curwords].repeated = 1; prs->curwords++; - } else - word->item=item; + } + else + word->item = item; } item++; } } -void -hlparsetext(TSCfgInfo *cfg, HLPRSTEXT * prs, QUERYTYPE *query, char *buf, int4 buflen) { - int type, lenlemm, i; - char *lemm=NULL; +void +hlparsetext(TSCfgInfo * cfg, HLPRSTEXT * prs, QUERYTYPE * query, char *buf, int4 buflen) +{ + int type, + lenlemm, + i; + char *lemm = NULL; WParserInfo *prsobj = findprs(cfg->prs_id); - prsobj->prs=(void*)DatumGetPointer( - FunctionCall2( - &(prsobj->start_info), - PointerGetDatum(buf), - Int32GetDatum(buflen) - ) - ); - - while( ( type=DatumGetInt32(FunctionCall3( - &(prsobj->getlexeme_info), - PointerGetDatum(prsobj->prs), - PointerGetDatum(&lemm), - PointerGetDatum(&lenlemm))) ) != 0 ) { - - if ( lenlemm >= MAXSTRLEN ) + prsobj->prs = (void *) DatumGetPointer( + FunctionCall2( + &(prsobj->start_info), + PointerGetDatum(buf), + Int32GetDatum(buflen) + ) + ); + + while ((type = DatumGetInt32(FunctionCall3( + &(prsobj->getlexeme_info), + PointerGetDatum(prsobj->prs), + PointerGetDatum(&lemm), + PointerGetDatum(&lenlemm)))) != 0) + { + + if (lenlemm >= MAXSTRLEN) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); - hladdword(prs,lemm,lenlemm,type); - - if ( type >= cfg->len ) - continue; - - for(i=0;i<cfg->map[type].len;i++) { - DictInfo *dict=finddict( DatumGetObjectId(cfg->map[type].dict_id[i]) ); - char **norms, **ptr; - - norms = ptr = (char**)DatumGetPointer( - FunctionCall3( - &(dict->lexize_info), - PointerGetDatum(dict->dictionary), - PointerGetDatum(lemm), - PointerGetDatum(lenlemm) - ) - ); - if ( !norms ) /* dictionary doesn't know this lexem */ + hladdword(prs, lemm, lenlemm, type); + + if (type >= cfg->len) + continue; + + for (i = 0; i < cfg->map[type].len; i++) + { + DictInfo *dict = finddict(DatumGetObjectId(cfg->map[type].dict_id[i])); + char **norms, + **ptr; + + norms = ptr = (char **) DatumGetPointer( + FunctionCall3( + &(dict->lexize_info), + PointerGetDatum(dict->dictionary), + PointerGetDatum(lemm), + PointerGetDatum(lenlemm) + ) + ); + if (!norms) /* dictionary doesn't know this lexem */ continue; - while( *ptr ) { - hlfinditem(prs,query,*ptr,strlen(*ptr)); + while (*ptr) + { + hlfinditem(prs, query, *ptr, strlen(*ptr)); pfree(*ptr); ptr++; } pfree(norms); - break; /* lexem already normalized or is stop word*/ + break; /* lexem already normalized or is stop + * word */ } } FunctionCall1( - &(prsobj->end_info), - PointerGetDatum(prsobj->prs) - ); + &(prsobj->end_info), + PointerGetDatum(prsobj->prs) + ); } -text* -genhl(HLPRSTEXT * prs) { - text *out; - int len=128; - char *ptr; - HLWORD *wrd=prs->words; +text * +genhl(HLPRSTEXT * prs) +{ + text *out; + int len = 128; + char *ptr; + HLWORD *wrd = prs->words; - out = (text*)palloc( len ); - ptr=((char*)out) + VARHDRSZ; + out = (text *) palloc(len); + ptr = ((char *) out) + VARHDRSZ; - while( wrd - prs->words < prs->curwords ) { - while ( wrd->len + prs->stopsellen + prs->startsellen + (ptr - ((char*)out)) >= len ) { - int dist = ptr - ((char*)out); - len*= 2; + while (wrd - prs->words < prs->curwords) + { + while (wrd->len + prs->stopsellen + prs->startsellen + (ptr - ((char *) out)) >= len) + { + int dist = ptr - ((char *) out); + + len *= 2; out = (text *) repalloc(out, len); - ptr=((char*)out) + dist; + ptr = ((char *) out) + dist; } - if ( wrd->in && !wrd->skip && !wrd->repeated ) { - if ( wrd->replace ) { - *ptr=' '; + if (wrd->in && !wrd->skip && !wrd->repeated) + { + if (wrd->replace) + { + *ptr = ' '; ptr++; - } else { - if (wrd->selected) { - memcpy(ptr,prs->startsel,prs->startsellen); - ptr+=prs->startsellen; + } + else + { + if (wrd->selected) + { + memcpy(ptr, prs->startsel, prs->startsellen); + ptr += prs->startsellen; } - memcpy(ptr,wrd->word,wrd->len); - ptr+=wrd->len; - if (wrd->selected) { - memcpy(ptr,prs->stopsel,prs->stopsellen); - ptr+=prs->stopsellen; + memcpy(ptr, wrd->word, wrd->len); + ptr += wrd->len; + if (wrd->selected) + { + memcpy(ptr, prs->stopsel, prs->stopsellen); + ptr += prs->stopsellen; } } } - if ( !wrd->repeated ) + if (!wrd->repeated) pfree(wrd->word); wrd++; } - VARATT_SIZEP(out)=ptr - ((char*)out); - return out; + VARATT_SIZEP(out) = ptr - ((char *) out); + return out; } -int -get_currcfg(void) { - Oid arg[1]={ TEXTOID }; +int +get_currcfg(void) +{ + Oid arg[1] = {TEXTOID}; const char *curlocale; - Datum pars[1]; - bool isnull; - int stat; + Datum pars[1]; + bool isnull; + int stat; - if ( current_cfg_id > 0 ) + if (current_cfg_id > 0) return current_cfg_id; SPI_connect(); - if ( !plan_getcfg_bylocale ) { - plan_getcfg_bylocale=SPI_saveplan( SPI_prepare( "select oid from pg_ts_cfg where locale = $1 ", 1, arg ) ); - if ( !plan_getcfg_bylocale ) + if (!plan_getcfg_bylocale) + { + plan_getcfg_bylocale = SPI_saveplan(SPI_prepare("select oid from pg_ts_cfg where locale = $1 ", 1, arg)); + if (!plan_getcfg_bylocale) /* internal error */ elog(ERROR, "SPI_prepare() failed"); } curlocale = setlocale(LC_CTYPE, NULL); - pars[0] = PointerGetDatum( char2text((char*)curlocale) ); + pars[0] = PointerGetDatum(char2text((char *) curlocale)); stat = SPI_execp(plan_getcfg_bylocale, pars, " ", 1); - if ( stat < 0 ) + if (stat < 0) /* internal error */ - elog (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) - current_cfg_id = DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - else + elog(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + current_cfg_id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + else ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not find tsearch config by locale"))); @@ -487,39 +552,43 @@ get_currcfg(void) { } PG_FUNCTION_INFO_V1(set_curcfg); -Datum set_curcfg(PG_FUNCTION_ARGS); +Datum set_curcfg(PG_FUNCTION_ARGS); Datum -set_curcfg(PG_FUNCTION_ARGS) { - findcfg(PG_GETARG_OID(0)); - current_cfg_id=PG_GETARG_OID(0); - PG_RETURN_VOID(); +set_curcfg(PG_FUNCTION_ARGS) +{ + findcfg(PG_GETARG_OID(0)); + current_cfg_id = PG_GETARG_OID(0); + PG_RETURN_VOID(); } - + PG_FUNCTION_INFO_V1(set_curcfg_byname); -Datum set_curcfg_byname(PG_FUNCTION_ARGS); +Datum set_curcfg_byname(PG_FUNCTION_ARGS); Datum -set_curcfg_byname(PG_FUNCTION_ARGS) { - text *name=PG_GETARG_TEXT_P(0); - - DirectFunctionCall1( - set_curcfg, - ObjectIdGetDatum( name2id_cfg(name) ) - ); - PG_FREE_IF_COPY(name, 0); - PG_RETURN_VOID(); -} +set_curcfg_byname(PG_FUNCTION_ARGS) +{ + text *name = PG_GETARG_TEXT_P(0); + + DirectFunctionCall1( + set_curcfg, + ObjectIdGetDatum(name2id_cfg(name)) + ); + PG_FREE_IF_COPY(name, 0); + PG_RETURN_VOID(); +} PG_FUNCTION_INFO_V1(show_curcfg); -Datum show_curcfg(PG_FUNCTION_ARGS); +Datum show_curcfg(PG_FUNCTION_ARGS); Datum -show_curcfg(PG_FUNCTION_ARGS) { - PG_RETURN_OID( get_currcfg() ); +show_curcfg(PG_FUNCTION_ARGS) +{ + PG_RETURN_OID(get_currcfg()); } PG_FUNCTION_INFO_V1(reset_tsearch); -Datum reset_tsearch(PG_FUNCTION_ARGS); +Datum reset_tsearch(PG_FUNCTION_ARGS); Datum -reset_tsearch(PG_FUNCTION_ARGS) { - ts_error(NOTICE,"TSearch cache cleaned"); - PG_RETURN_VOID(); +reset_tsearch(PG_FUNCTION_ARGS) +{ + ts_error(NOTICE, "TSearch cache cleaned"); + PG_RETURN_VOID(); } diff --git a/contrib/tsearch2/ts_cfg.h b/contrib/tsearch2/ts_cfg.h index 01006c1f93c..cde35ca2a6e 100644 --- a/contrib/tsearch2/ts_cfg.h +++ b/contrib/tsearch2/ts_cfg.h @@ -3,66 +3,73 @@ #include "postgres.h" #include "query.h" -typedef struct { - int len; - Datum *dict_id; -} ListDictionary; +typedef struct +{ + int len; + Datum *dict_id; +} ListDictionary; -typedef struct { - Oid id; - Oid prs_id; - int len; - ListDictionary *map; +typedef struct +{ + Oid id; + Oid prs_id; + int len; + ListDictionary *map; } TSCfgInfo; -Oid name2id_cfg(text *name); -TSCfgInfo * findcfg(Oid id); -void init_cfg(Oid id, TSCfgInfo *cfg); -void reset_cfg(void); +Oid name2id_cfg(text *name); +TSCfgInfo *findcfg(Oid id); +void init_cfg(Oid id, TSCfgInfo * cfg); +void reset_cfg(void); -typedef struct { - uint16 len; - union { +typedef struct +{ + uint16 len; + union + { uint16 pos; - uint16 *apos; - } pos; - char *word; - uint32 alen; -} WORD; - -typedef struct { - WORD *words; - int4 lenwords; - int4 curwords; + uint16 *apos; + } pos; + char *word; + uint32 alen; +} WORD; + +typedef struct +{ + WORD *words; + int4 lenwords; + int4 curwords; int4 pos; -} PRSTEXT; +} PRSTEXT; + +typedef struct +{ + uint16 len; + uint8 selected:1, + in:1, + skip:1, + replace:1, + repeated:1; + uint8 type; + char *word; + ITEM *item; +} HLWORD; -typedef struct { - uint16 len; - uint8 selected:1, - in:1, - skip:1, - replace:1, - repeated:1; - uint8 type; - char *word; - ITEM *item; -} HLWORD; - -typedef struct { - HLWORD *words; - int4 lenwords; - int4 curwords; - char *startsel; - char *stopsel; - int2 startsellen; - int2 stopsellen; -} HLPRSTEXT; +typedef struct +{ + HLWORD *words; + int4 lenwords; + int4 curwords; + char *startsel; + char *stopsel; + int2 startsellen; + int2 stopsellen; +} HLPRSTEXT; -void hlparsetext(TSCfgInfo *cfg, HLPRSTEXT * prs, QUERYTYPE *query, char *buf, int4 buflen); -text* genhl(HLPRSTEXT * prs); +void hlparsetext(TSCfgInfo * cfg, HLPRSTEXT * prs, QUERYTYPE * query, char *buf, int4 buflen); +text *genhl(HLPRSTEXT * prs); -void parsetext_v2(TSCfgInfo *cfg, PRSTEXT * prs, char *buf, int4 buflen); -int get_currcfg(void); +void parsetext_v2(TSCfgInfo * cfg, PRSTEXT * prs, char *buf, int4 buflen); +int get_currcfg(void); #endif diff --git a/contrib/tsearch2/ts_stat.c b/contrib/tsearch2/ts_stat.c index a09e0572143..47353fc579e 100644 --- a/contrib/tsearch2/ts_stat.c +++ b/contrib/tsearch2/ts_stat.c @@ -10,108 +10,128 @@ #include "common.h" PG_FUNCTION_INFO_V1(tsstat_in); -Datum tsstat_in(PG_FUNCTION_ARGS); -Datum -tsstat_in(PG_FUNCTION_ARGS) { - tsstat *stat=palloc(STATHDRSIZE); - stat->len=STATHDRSIZE; - stat->size=0; +Datum tsstat_in(PG_FUNCTION_ARGS); +Datum +tsstat_in(PG_FUNCTION_ARGS) +{ + tsstat *stat = palloc(STATHDRSIZE); + + stat->len = STATHDRSIZE; + stat->size = 0; PG_RETURN_POINTER(stat); } PG_FUNCTION_INFO_V1(tsstat_out); -Datum tsstat_out(PG_FUNCTION_ARGS); -Datum -tsstat_out(PG_FUNCTION_ARGS) { +Datum tsstat_out(PG_FUNCTION_ARGS); +Datum +tsstat_out(PG_FUNCTION_ARGS) +{ ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("tsstat_out not implemented"))); PG_RETURN_NULL(); } -static WordEntry** -SEI_realloc( WordEntry** in, uint32 *len ) { - if ( *len==0 || in==NULL ) { - *len=8; - in=palloc( sizeof(WordEntry*)* (*len) ); - } else { +static WordEntry ** +SEI_realloc(WordEntry ** in, uint32 *len) +{ + if (*len == 0 || in == NULL) + { + *len = 8; + in = palloc(sizeof(WordEntry *) * (*len)); + } + else + { *len *= 2; - in=repalloc( in, sizeof(WordEntry*)* (*len) ); + in = repalloc(in, sizeof(WordEntry *) * (*len)); } return in; } static int -compareStatWord(StatEntry *a, WordEntry *b, tsstat *stat, tsvector *txt) { - if ( a->len == b->len ) +compareStatWord(StatEntry * a, WordEntry * b, tsstat * stat, tsvector * txt) +{ + if (a->len == b->len) return strncmp( - STATSTRPTR(stat) + a->pos, - STRPTR(txt) + b->pos, - a->len + STATSTRPTR(stat) + a->pos, + STRPTR(txt) + b->pos, + a->len ); - return ( a->len > b->len ) ? 1 : -1; + return (a->len > b->len) ? 1 : -1; } -static tsstat* -formstat(tsstat *stat, tsvector *txt, WordEntry** entry, uint32 len) { - tsstat *newstat; - uint32 totallen, nentry; - uint32 slen=0; - WordEntry **ptr=entry; - char *curptr; - StatEntry *sptr,*nptr; - - while(ptr-entry<len) { +static tsstat * +formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len) +{ + tsstat *newstat; + uint32 totallen, + nentry; + uint32 slen = 0; + WordEntry **ptr = entry; + char *curptr; + StatEntry *sptr, + *nptr; + + while (ptr - entry < len) + { slen += (*ptr)->len; ptr++; } - nentry=stat->size + len; - slen+=STATSTRSIZE(stat); - totallen=CALCSTATSIZE(nentry,slen); - newstat=palloc(totallen); - newstat->len=totallen; - newstat->size=nentry; + nentry = stat->size + len; + slen += STATSTRSIZE(stat); + totallen = CALCSTATSIZE(nentry, slen); + newstat = palloc(totallen); + newstat->len = totallen; + newstat->size = nentry; memcpy(STATSTRPTR(newstat), STATSTRPTR(stat), STATSTRSIZE(stat)); - curptr=STATSTRPTR(newstat) + STATSTRSIZE(stat); + curptr = STATSTRPTR(newstat) + STATSTRSIZE(stat); - ptr=entry; - sptr=STATPTR(stat); - nptr=STATPTR(newstat); + ptr = entry; + sptr = STATPTR(stat); + nptr = STATPTR(newstat); - if ( len == 1 ) { - StatEntry *StopLow = STATPTR(stat); - StatEntry *StopHigh = (StatEntry*)STATSTRPTR(stat); + if (len == 1) + { + StatEntry *StopLow = STATPTR(stat); + StatEntry *StopHigh = (StatEntry *) STATSTRPTR(stat); - while (StopLow < StopHigh) { - sptr=StopLow + (StopHigh - StopLow) / 2; - if ( compareStatWord(sptr,*ptr,stat,txt) < 0 ) + while (StopLow < StopHigh) + { + sptr = StopLow + (StopHigh - StopLow) / 2; + if (compareStatWord(sptr, *ptr, stat, txt) < 0) StopLow = sptr + 1; else - StopHigh = sptr; + StopHigh = sptr; } - nptr =STATPTR(newstat) + (StopLow-STATPTR(stat)); - memcpy( STATPTR(newstat), STATPTR(stat), sizeof(StatEntry) * (StopLow-STATPTR(stat)) ); - nptr->nentry=POSDATALEN(txt,*ptr); - if ( nptr->nentry==0 ) - nptr->nentry=1; - nptr->ndoc=1; - nptr->len=(*ptr)->len; + nptr = STATPTR(newstat) + (StopLow - STATPTR(stat)); + memcpy(STATPTR(newstat), STATPTR(stat), sizeof(StatEntry) * (StopLow - STATPTR(stat))); + nptr->nentry = POSDATALEN(txt, *ptr); + if (nptr->nentry == 0) + nptr->nentry = 1; + nptr->ndoc = 1; + nptr->len = (*ptr)->len; memcpy(curptr, STRPTR(txt) + (*ptr)->pos, nptr->len); nptr->pos = curptr - STATSTRPTR(newstat); - memcpy( nptr+1, StopLow, sizeof(StatEntry) * ( ((StatEntry*)STATSTRPTR(stat))-StopLow ) ); - } else { - while( sptr-STATPTR(stat) < stat->size && ptr-entry<len) { - if ( compareStatWord(sptr,*ptr,stat,txt) < 0 ) { + memcpy(nptr + 1, StopLow, sizeof(StatEntry) * (((StatEntry *) STATSTRPTR(stat)) - StopLow)); + } + else + { + while (sptr - STATPTR(stat) < stat->size && ptr - entry < len) + { + if (compareStatWord(sptr, *ptr, stat, txt) < 0) + { memcpy(nptr, sptr, sizeof(StatEntry)); sptr++; - } else { - nptr->nentry=POSDATALEN(txt,*ptr); - if ( nptr->nentry==0 ) - nptr->nentry=1; - nptr->ndoc=1; - nptr->len=(*ptr)->len; + } + else + { + nptr->nentry = POSDATALEN(txt, *ptr); + if (nptr->nentry == 0) + nptr->nentry = 1; + nptr->ndoc = 1; + nptr->len = (*ptr)->len; memcpy(curptr, STRPTR(txt) + (*ptr)->pos, nptr->len); nptr->pos = curptr - STATSTRPTR(newstat); curptr += nptr->len; @@ -120,138 +140,168 @@ formstat(tsstat *stat, tsvector *txt, WordEntry** entry, uint32 len) { nptr++; } - memcpy( nptr, sptr, sizeof(StatEntry)*( stat->size - (sptr-STATPTR(stat)) ) ); - - while(ptr-entry<len) { - nptr->nentry=POSDATALEN(txt,*ptr); - if ( nptr->nentry==0 ) - nptr->nentry=1; - nptr->ndoc=1; - nptr->len=(*ptr)->len; + memcpy(nptr, sptr, sizeof(StatEntry) * (stat->size - (sptr - STATPTR(stat)))); + + while (ptr - entry < len) + { + nptr->nentry = POSDATALEN(txt, *ptr); + if (nptr->nentry == 0) + nptr->nentry = 1; + nptr->ndoc = 1; + nptr->len = (*ptr)->len; memcpy(curptr, STRPTR(txt) + (*ptr)->pos, nptr->len); nptr->pos = curptr - STATSTRPTR(newstat); curptr += nptr->len; - ptr++; nptr++; + ptr++; + nptr++; } } return newstat; -} +} PG_FUNCTION_INFO_V1(ts_accum); -Datum ts_accum(PG_FUNCTION_ARGS); -Datum -ts_accum(PG_FUNCTION_ARGS) { - tsstat *newstat,*stat= (tsstat*)PG_GETARG_POINTER(0); - tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); - WordEntry **newentry=NULL; - uint32 len=0, cur=0; - StatEntry *sptr; - WordEntry *wptr; - - if ( stat==NULL || PG_ARGISNULL(0) ) { /* Init in first */ - stat=palloc(STATHDRSIZE); - stat->len=STATHDRSIZE; - stat->size=0; +Datum ts_accum(PG_FUNCTION_ARGS); +Datum +ts_accum(PG_FUNCTION_ARGS) +{ + tsstat *newstat, + *stat = (tsstat *) PG_GETARG_POINTER(0); + tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + WordEntry **newentry = NULL; + uint32 len = 0, + cur = 0; + StatEntry *sptr; + WordEntry *wptr; + + if (stat == NULL || PG_ARGISNULL(0)) + { /* Init in first */ + stat = palloc(STATHDRSIZE); + stat->len = STATHDRSIZE; + stat->size = 0; } /* simple check of correctness */ - if ( txt==NULL || PG_ARGISNULL(1) || txt->size==0 ) { - PG_FREE_IF_COPY(txt,1); + if (txt == NULL || PG_ARGISNULL(1) || txt->size == 0) + { + PG_FREE_IF_COPY(txt, 1); PG_RETURN_POINTER(stat); } - sptr=STATPTR(stat); - wptr=ARRPTR(txt); + sptr = STATPTR(stat); + wptr = ARRPTR(txt); - if ( stat->size < 100*txt->size ) { /* merge */ - while( sptr-STATPTR(stat) < stat->size && wptr-ARRPTR(txt) < txt->size ) { - int cmp = compareStatWord(sptr,wptr,stat,txt); - if ( cmp<0 ) { + if (stat->size < 100 * txt->size) + { /* merge */ + while (sptr - STATPTR(stat) < stat->size && wptr - ARRPTR(txt) < txt->size) + { + int cmp = compareStatWord(sptr, wptr, stat, txt); + + if (cmp < 0) sptr++; - } else if ( cmp==0 ) { - int n=POSDATALEN(txt,wptr); - - if (n==0) n=1; + else if (cmp == 0) + { + int n = POSDATALEN(txt, wptr); + + if (n == 0) + n = 1; sptr->ndoc++; - sptr->nentry +=n ; - sptr++; wptr++; - } else { - if ( cur==len ) - newentry=SEI_realloc(newentry, &len); - newentry[cur]=wptr; - wptr++; cur++; + sptr->nentry += n; + sptr++; + wptr++; + } + else + { + if (cur == len) + newentry = SEI_realloc(newentry, &len); + newentry[cur] = wptr; + wptr++; + cur++; } } - while( wptr-ARRPTR(txt) < txt->size ) { - if ( cur==len ) - newentry=SEI_realloc(newentry, &len); - newentry[cur]=wptr; - wptr++; cur++; + while (wptr - ARRPTR(txt) < txt->size) + { + if (cur == len) + newentry = SEI_realloc(newentry, &len); + newentry[cur] = wptr; + wptr++; + cur++; } - } else { /* search */ - while( wptr-ARRPTR(txt) < txt->size ) { - StatEntry *StopLow = STATPTR(stat); - StatEntry *StopHigh = (StatEntry*)STATSTRPTR(stat); - int cmp; - - while (StopLow < StopHigh) { - sptr=StopLow + (StopHigh - StopLow) / 2; - cmp = compareStatWord(sptr,wptr,stat,txt); - if (cmp==0) { - int n=POSDATALEN(txt,wptr); - if (n==0) n=1; + } + else + { /* search */ + while (wptr - ARRPTR(txt) < txt->size) + { + StatEntry *StopLow = STATPTR(stat); + StatEntry *StopHigh = (StatEntry *) STATSTRPTR(stat); + int cmp; + + while (StopLow < StopHigh) + { + sptr = StopLow + (StopHigh - StopLow) / 2; + cmp = compareStatWord(sptr, wptr, stat, txt); + if (cmp == 0) + { + int n = POSDATALEN(txt, wptr); + + if (n == 0) + n = 1; sptr->ndoc++; - sptr->nentry +=n ; + sptr->nentry += n; break; - } else if ( cmp < 0 ) + } + else if (cmp < 0) StopLow = sptr + 1; else - StopHigh = sptr; + StopHigh = sptr; } - - if ( StopLow >= StopHigh ) { /* not found */ - if ( cur==len ) - newentry=SEI_realloc(newentry, &len); - newentry[cur]=wptr; + + if (StopLow >= StopHigh) + { /* not found */ + if (cur == len) + newentry = SEI_realloc(newentry, &len); + newentry[cur] = wptr; cur++; } wptr++; - } + } } - - if ( cur==0 ) { /* no new words */ - PG_FREE_IF_COPY(txt,1); + + if (cur == 0) + { /* no new words */ + PG_FREE_IF_COPY(txt, 1); PG_RETURN_POINTER(stat); } newstat = formstat(stat, txt, newentry, cur); pfree(newentry); - PG_FREE_IF_COPY(txt,1); + PG_FREE_IF_COPY(txt, 1); /* pfree(stat); */ PG_RETURN_POINTER(newstat); } -typedef struct { - uint32 cur; - tsvector *stat; -} StatStorage; +typedef struct +{ + uint32 cur; + tsvector *stat; +} StatStorage; static void -ts_setup_firstcall(FuncCallContext *funcctx, tsstat *stat) { - TupleDesc tupdesc; - MemoryContext oldcontext; - StatStorage *st; - +ts_setup_firstcall(FuncCallContext *funcctx, tsstat * stat) +{ + TupleDesc tupdesc; + MemoryContext oldcontext; + StatStorage *st; + oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - st=palloc( sizeof(StatStorage) ); - st->cur=0; - st->stat=palloc( stat->len ); + st = palloc(sizeof(StatStorage)); + st->cur = 0; + st->stat = palloc(stat->len); memcpy(st->stat, stat, stat->len); - funcctx->user_fctx = (void*)st; + funcctx->user_fctx = (void *) st; tupdesc = RelationNameGetTupleDesc("statinfo"); funcctx->slot = TupleDescGetSlot(tupdesc); funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc); @@ -260,162 +310,175 @@ ts_setup_firstcall(FuncCallContext *funcctx, tsstat *stat) { static Datum -ts_process_call(FuncCallContext *funcctx) { - StatStorage *st; - st=(StatStorage*)funcctx->user_fctx; - - if ( st->cur < st->stat->size ) { - Datum result; - char* values[3]; - char ndoc[16]; - char nentry[16]; - StatEntry *entry=STATPTR(st->stat) + st->cur; - HeapTuple tuple; - - values[1]=ndoc; - sprintf(ndoc,"%d",entry->ndoc); - values[2]=nentry; - sprintf(nentry,"%d",entry->nentry); - values[0]=palloc( entry->len+1 ); - memcpy( values[0], STATSTRPTR(st->stat)+entry->pos, entry->len); - (values[0])[entry->len]='\0'; +ts_process_call(FuncCallContext *funcctx) +{ + StatStorage *st; + + st = (StatStorage *) funcctx->user_fctx; + + if (st->cur < st->stat->size) + { + Datum result; + char *values[3]; + char ndoc[16]; + char nentry[16]; + StatEntry *entry = STATPTR(st->stat) + st->cur; + HeapTuple tuple; + + values[1] = ndoc; + sprintf(ndoc, "%d", entry->ndoc); + values[2] = nentry; + sprintf(nentry, "%d", entry->nentry); + values[0] = palloc(entry->len + 1); + memcpy(values[0], STATSTRPTR(st->stat) + entry->pos, entry->len); + (values[0])[entry->len] = '\0'; tuple = BuildTupleFromCStrings(funcctx->attinmeta, values); result = TupleGetDatum(funcctx->slot, tuple); pfree(values[0]); st->cur++; - return result; - } else { + return result; + } + else + { pfree(st->stat); pfree(st); } - - return (Datum)0; + + return (Datum) 0; } PG_FUNCTION_INFO_V1(ts_accum_finish); -Datum ts_accum_finish(PG_FUNCTION_ARGS); -Datum -ts_accum_finish(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; - - if (SRF_IS_FIRSTCALL()) { +Datum ts_accum_finish(PG_FUNCTION_ARGS); +Datum +ts_accum_finish(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { funcctx = SRF_FIRSTCALL_INIT(); - ts_setup_firstcall(funcctx, (tsstat*)PG_GETARG_POINTER(0) ); + ts_setup_firstcall(funcctx, (tsstat *) PG_GETARG_POINTER(0)); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=ts_process_call(funcctx)) != (Datum)0 ) + if ((result = ts_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } -static Oid tiOid=InvalidOid; -static void -get_ti_Oid(void) { - int ret; - bool isnull; +static Oid tiOid = InvalidOid; +static void +get_ti_Oid(void) +{ + int ret; + bool isnull; - if ( (ret = SPI_exec("select oid from pg_type where typname='tsvector'",1)) < 0 ) + if ((ret = SPI_exec("select oid from pg_type where typname='tsvector'", 1)) < 0) /* internal error */ elog(ERROR, "SPI_exec to get tsvector oid returns %d", ret); - if ( SPI_processed<0 ) + if (SPI_processed < 0) /* internal error */ elog(ERROR, "There is no tsvector type"); - tiOid = DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - if ( tiOid==InvalidOid ) + tiOid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + if (tiOid == InvalidOid) /* internal error */ elog(ERROR, "tsvector type has InvalidOid"); } -static tsstat* -ts_stat_sql(text *txt) { - char *query=text2char(txt); - int i; - tsstat *newstat,*stat; - bool isnull; - Portal portal; - void *plan; - - if ( tiOid==InvalidOid ) +static tsstat * +ts_stat_sql(text *txt) +{ + char *query = text2char(txt); + int i; + tsstat *newstat, + *stat; + bool isnull; + Portal portal; + void *plan; + + if (tiOid == InvalidOid) get_ti_Oid(); - if ( (plan = SPI_prepare(query,0,NULL))==NULL ) + if ((plan = SPI_prepare(query, 0, NULL)) == NULL) /* internal error */ - elog(ERROR, "SPI_prepare('%s') returns NULL",query); + elog(ERROR, "SPI_prepare('%s') returns NULL", query); - if ( (portal = SPI_cursor_open(NULL, plan, NULL, NULL)) == NULL ) + if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL)) == NULL) /* internal error */ - elog(ERROR, "SPI_cursor_open('%s') returns NULL",query); + elog(ERROR, "SPI_cursor_open('%s') returns NULL", query); SPI_cursor_fetch(portal, true, 100); - if ( SPI_tuptable->tupdesc->natts != 1 ) + if (SPI_tuptable->tupdesc->natts != 1) /* internal error */ elog(ERROR, "number of fields doesn't equal to 1"); - if ( SPI_gettypeid(SPI_tuptable->tupdesc, 1) != tiOid ) + if (SPI_gettypeid(SPI_tuptable->tupdesc, 1) != tiOid) /* internal error */ elog(ERROR, "column isn't of tsvector type"); - stat=palloc(STATHDRSIZE); - stat->len=STATHDRSIZE; - stat->size=0; - - while(SPI_processed>0) { - for(i=0;i<SPI_processed;i++) { - Datum data=SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull); - - if ( !isnull ) { - newstat = (tsstat*)DatumGetPointer(DirectFunctionCall2( - ts_accum, - PointerGetDatum(stat), - data - )); - if ( stat!=newstat && stat ) + stat = palloc(STATHDRSIZE); + stat->len = STATHDRSIZE; + stat->size = 0; + + while (SPI_processed > 0) + { + for (i = 0; i < SPI_processed; i++) + { + Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull); + + if (!isnull) + { + newstat = (tsstat *) DatumGetPointer(DirectFunctionCall2( + ts_accum, + PointerGetDatum(stat), + data + )); + if (stat != newstat && stat) pfree(stat); - stat=newstat; + stat = newstat; } - } + } SPI_freetuptable(SPI_tuptable); - SPI_cursor_fetch(portal, true, 100); - } + SPI_cursor_fetch(portal, true, 100); + } SPI_freetuptable(SPI_tuptable); SPI_cursor_close(portal); SPI_freeplan(plan); pfree(query); - return stat; + return stat; } PG_FUNCTION_INFO_V1(ts_stat); -Datum ts_stat(PG_FUNCTION_ARGS); -Datum -ts_stat(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; - - if (SRF_IS_FIRSTCALL()) { - tsstat *stat; - text *txt=PG_GETARG_TEXT_P(0); - +Datum ts_stat(PG_FUNCTION_ARGS); +Datum +ts_stat(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { + tsstat *stat; + text *txt = PG_GETARG_TEXT_P(0); + funcctx = SRF_FIRSTCALL_INIT(); SPI_connect(); stat = ts_stat_sql(txt); - PG_FREE_IF_COPY(txt,0); - ts_setup_firstcall(funcctx, stat ); + PG_FREE_IF_COPY(txt, 0); + ts_setup_firstcall(funcctx, stat); SPI_finish(); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=ts_process_call(funcctx)) != (Datum)0 ) + if ((result = ts_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } - - diff --git a/contrib/tsearch2/ts_stat.h b/contrib/tsearch2/ts_stat.h index 023a83346cb..37d1e7b660a 100644 --- a/contrib/tsearch2/ts_stat.h +++ b/contrib/tsearch2/ts_stat.h @@ -8,14 +8,16 @@ #include "utils/builtins.h" #include "storage/bufpage.h" -typedef struct { - uint32 len; - uint32 pos; - uint32 ndoc; - uint32 nentry; +typedef struct +{ + uint32 len; + uint32 pos; + uint32 ndoc; + uint32 nentry; } StatEntry; -typedef struct { +typedef struct +{ int4 len; int4 size; char data[1]; diff --git a/contrib/tsearch2/tsvector.c b/contrib/tsearch2/tsvector.c index 7c258f1305f..c8002c0ab3f 100644 --- a/contrib/tsearch2/tsvector.c +++ b/contrib/tsearch2/tsvector.c @@ -31,8 +31,10 @@ Datum tsvector_out(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(to_tsvector); Datum to_tsvector(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(to_tsvector_current); Datum to_tsvector_current(PG_FUNCTION_ARGS); + PG_FUNCTION_INFO_V1(to_tsvector_name); Datum to_tsvector_name(PG_FUNCTION_ARGS); @@ -45,32 +47,38 @@ Datum tsvector_length(PG_FUNCTION_ARGS); /* * in/out text index type */ -static int -comparePos(const void *a, const void *b) { - if ( ((WordEntryPos *) a)->pos == ((WordEntryPos *) b)->pos ) +static int +comparePos(const void *a, const void *b) +{ + if (((WordEntryPos *) a)->pos == ((WordEntryPos *) b)->pos) return 1; - return ( ((WordEntryPos *) a)->pos > ((WordEntryPos *) b)->pos ) ? 1 : -1; + return (((WordEntryPos *) a)->pos > ((WordEntryPos *) b)->pos) ? 1 : -1; } static int -uniquePos(WordEntryPos *a, int4 l) { - WordEntryPos *ptr, *res; +uniquePos(WordEntryPos * a, int4 l) +{ + WordEntryPos *ptr, + *res; - res=a; - if (l==1) + res = a; + if (l == 1) return l; qsort((void *) a, l, sizeof(WordEntryPos), comparePos); ptr = a + 1; - while (ptr - a < l) { - if ( ptr->pos != res->pos ) { + while (ptr - a < l) + { + if (ptr->pos != res->pos) + { res++; res->pos = ptr->pos; res->weight = ptr->weight; - if ( res-a >= MAXNUMPOS-1 || res->pos == MAXENTRYPOS-1 ) + if (res - a >= MAXNUMPOS - 1 || res->pos == MAXENTRYPOS - 1) break; - } else if ( ptr->weight > res->weight ) + } + else if (ptr->weight > res->weight) res->weight = ptr->weight; ptr++; } @@ -81,27 +89,29 @@ static char *BufferStr; static int compareentry(const void *a, const void *b) { - if ( ((WordEntryIN *) a)->entry.len == ((WordEntryIN *) b)->entry.len) + if (((WordEntryIN *) a)->entry.len == ((WordEntryIN *) b)->entry.len) { return strncmp( &BufferStr[((WordEntryIN *) a)->entry.pos], &BufferStr[((WordEntryIN *) b)->entry.pos], ((WordEntryIN *) a)->entry.len); } - return ( ((WordEntryIN *) a)->entry.len > ((WordEntryIN *) b)->entry.len ) ? 1 : -1; + return (((WordEntryIN *) a)->entry.len > ((WordEntryIN *) b)->entry.len) ? 1 : -1; } static int uniqueentry(WordEntryIN * a, int4 l, char *buf, int4 *outbuflen) { - WordEntryIN *ptr, + WordEntryIN *ptr, *res; res = a; - if (l == 1) { - if ( a->entry.haspos ) { - *(uint16*)(a->pos) = uniquePos( &(a->pos[1]), *(uint16*)(a->pos)); - *outbuflen = SHORTALIGN(res->entry.len) + (*(uint16*)(a->pos) +1 )*sizeof(WordEntryPos); + if (l == 1) + { + if (a->entry.haspos) + { + *(uint16 *) (a->pos) = uniquePos(&(a->pos[1]), *(uint16 *) (a->pos)); + *outbuflen = SHORTALIGN(res->entry.len) + (*(uint16 *) (a->pos) + 1) * sizeof(WordEntryPos); } return l; } @@ -115,31 +125,39 @@ uniqueentry(WordEntryIN * a, int4 l, char *buf, int4 *outbuflen) if (!(ptr->entry.len == res->entry.len && strncmp(&buf[ptr->entry.pos], &buf[res->entry.pos], res->entry.len) == 0)) { - if ( res->entry.haspos ) { - *(uint16*)(res->pos) = uniquePos( &(res->pos[1]), *(uint16*)(res->pos)); - *outbuflen += *(uint16*)(res->pos) * sizeof(WordEntryPos); + if (res->entry.haspos) + { + *(uint16 *) (res->pos) = uniquePos(&(res->pos[1]), *(uint16 *) (res->pos)); + *outbuflen += *(uint16 *) (res->pos) * sizeof(WordEntryPos); } *outbuflen += SHORTALIGN(res->entry.len); res++; - memcpy(res,ptr,sizeof(WordEntryIN)); - } else if ( ptr->entry.haspos ){ - if ( res->entry.haspos ) { - int4 len=*(uint16*)(ptr->pos) + 1 + *(uint16*)(res->pos); - res->pos=(WordEntryPos*)repalloc( res->pos, len*sizeof(WordEntryPos)); - memcpy( &(res->pos[ *(uint16*)(res->pos) + 1 ]), - &(ptr->pos[1]), *(uint16*)(ptr->pos) * sizeof(WordEntryPos)); - *(uint16*)(res->pos) += *(uint16*)(ptr->pos); - pfree( ptr->pos ); - } else { - res->entry.haspos=1; + memcpy(res, ptr, sizeof(WordEntryIN)); + } + else if (ptr->entry.haspos) + { + if (res->entry.haspos) + { + int4 len = *(uint16 *) (ptr->pos) + 1 + *(uint16 *) (res->pos); + + res->pos = (WordEntryPos *) repalloc(res->pos, len * sizeof(WordEntryPos)); + memcpy(&(res->pos[*(uint16 *) (res->pos) + 1]), + &(ptr->pos[1]), *(uint16 *) (ptr->pos) * sizeof(WordEntryPos)); + *(uint16 *) (res->pos) += *(uint16 *) (ptr->pos); + pfree(ptr->pos); + } + else + { + res->entry.haspos = 1; res->pos = ptr->pos; } } ptr++; } - if ( res->entry.haspos ) { - *(uint16*)(res->pos) = uniquePos( &(res->pos[1]), *(uint16*)(res->pos)); - *outbuflen += *(uint16*)(res->pos) * sizeof(WordEntryPos); + if (res->entry.haspos) + { + *(uint16 *) (res->pos) = uniquePos(&(res->pos[1]), *(uint16 *) (res->pos)); + *outbuflen += *(uint16 *) (res->pos) * sizeof(WordEntryPos); } *outbuflen += SHORTALIGN(res->entry.len); @@ -150,7 +168,7 @@ uniqueentry(WordEntryIN * a, int4 l, char *buf, int4 *outbuflen) #define WAITENDWORD 2 #define WAITNEXTCHAR 3 #define WAITENDCMPLX 4 -#define WAITPOSINFO 5 +#define WAITPOSINFO 5 #define INPOSINFO 6 #define WAITPOSDELIM 7 @@ -172,7 +190,7 @@ gettoken_tsvector(TI_IN_STATE * state) state->curpos = state->word; state->state = WAITWORD; - state->alen=0; + state->alen = 0; while (1) { @@ -228,14 +246,16 @@ gettoken_tsvector(TI_IN_STATE * state) (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); *(state->curpos) = '\0'; - return 1; - } else if ( *(state->prsbuf) == ':' ) { + return 1; + } + else if (*(state->prsbuf) == ':') + { if (state->curpos == state->word) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); *(state->curpos) = '\0'; - if ( state->oprisdelim ) + if (state->oprisdelim) return 1; else state->state = INPOSINFO; @@ -257,10 +277,12 @@ gettoken_tsvector(TI_IN_STATE * state) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - if ( state->oprisdelim ) { + if (state->oprisdelim) + { state->prsbuf++; return 1; - } else + } + else state->state = WAITPOSINFO; } else if (*(state->prsbuf) == '\\') @@ -278,67 +300,87 @@ gettoken_tsvector(TI_IN_STATE * state) *(state->curpos) = *(state->prsbuf); state->curpos++; } - } else if (state->state == WAITPOSINFO) { - if ( *(state->prsbuf) == ':' ) - state->state=INPOSINFO; + } + else if (state->state == WAITPOSINFO) + { + if (*(state->prsbuf) == ':') + state->state = INPOSINFO; else return 1; - } else if (state->state == INPOSINFO) { - if ( isdigit(*(state->prsbuf)) ) { - if ( state->alen==0 ) { - state->alen=4; - state->pos = (WordEntryPos*)palloc( sizeof(WordEntryPos)*state->alen ); - *(uint16*)(state->pos)=0; - } else if ( *(uint16*)(state->pos) +1 >= state->alen ) { - state->alen *= 2; - state->pos = (WordEntryPos*)repalloc( state->pos, sizeof(WordEntryPos)*state->alen ); + } + else if (state->state == INPOSINFO) + { + if (isdigit(*(state->prsbuf))) + { + if (state->alen == 0) + { + state->alen = 4; + state->pos = (WordEntryPos *) palloc(sizeof(WordEntryPos) * state->alen); + *(uint16 *) (state->pos) = 0; } - ( *(uint16*)(state->pos) )++; - state->pos[ *(uint16*)(state->pos) ].pos = LIMITPOS(atoi(state->prsbuf)); - if ( state->pos[ *(uint16*)(state->pos) ].pos == 0 ) + else if (*(uint16 *) (state->pos) + 1 >= state->alen) + { + state->alen *= 2; + state->pos = (WordEntryPos *) repalloc(state->pos, sizeof(WordEntryPos) * state->alen); + } + (*(uint16 *) (state->pos))++; + state->pos[*(uint16 *) (state->pos)].pos = LIMITPOS(atoi(state->prsbuf)); + if (state->pos[*(uint16 *) (state->pos)].pos == 0) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("wrong position info"))); - state->pos[ *(uint16*)(state->pos) ].weight = 0; + state->pos[*(uint16 *) (state->pos)].weight = 0; state->state = WAITPOSDELIM; - } else + } + else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - } else if (state->state == WAITPOSDELIM) { - if ( *(state->prsbuf) == ',' ) { + } + else if (state->state == WAITPOSDELIM) + { + if (*(state->prsbuf) == ',') state->state = INPOSINFO; - } else if ( tolower(*(state->prsbuf)) == 'a' || *(state->prsbuf)=='*' ) { - if ( state->pos[ *(uint16*)(state->pos) ].weight ) + else if (tolower(*(state->prsbuf)) == 'a' || *(state->prsbuf) == '*') + { + if (state->pos[*(uint16 *) (state->pos)].weight) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[ *(uint16*)(state->pos) ].weight = 3; - } else if ( tolower(*(state->prsbuf)) == 'b' ) { - if ( state->pos[ *(uint16*)(state->pos) ].weight ) + state->pos[*(uint16 *) (state->pos)].weight = 3; + } + else if (tolower(*(state->prsbuf)) == 'b') + { + if (state->pos[*(uint16 *) (state->pos)].weight) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[ *(uint16*)(state->pos) ].weight = 2; - } else if ( tolower(*(state->prsbuf)) == 'c' ) { - if ( state->pos[ *(uint16*)(state->pos) ].weight ) + state->pos[*(uint16 *) (state->pos)].weight = 2; + } + else if (tolower(*(state->prsbuf)) == 'c') + { + if (state->pos[*(uint16 *) (state->pos)].weight) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[ *(uint16*)(state->pos) ].weight = 1; - } else if ( tolower(*(state->prsbuf)) == 'd' ) { - if ( state->pos[ *(uint16*)(state->pos) ].weight ) + state->pos[*(uint16 *) (state->pos)].weight = 1; + } + else if (tolower(*(state->prsbuf)) == 'd') + { + if (state->pos[*(uint16 *) (state->pos)].weight) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[ *(uint16*)(state->pos) ].weight = 0; - } else if ( isspace(*(state->prsbuf)) || *(state->prsbuf) == '\0' ) { + state->pos[*(uint16 *) (state->pos)].weight = 0; + } + else if (isspace(*(state->prsbuf)) || *(state->prsbuf) == '\0') return 1; - } else if ( !isdigit(*(state->prsbuf)) ) + else if (!isdigit(*(state->prsbuf))) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - } else + } + else /* internal error */ elog(ERROR, "internal error"); state->prsbuf++; @@ -352,11 +394,11 @@ tsvector_in(PG_FUNCTION_ARGS) { char *buf = PG_GETARG_CSTRING(0); TI_IN_STATE state; - WordEntryIN *arr; + WordEntryIN *arr; WordEntry *inarr; int4 len = 0, totallen = 64; - tsvector *in; + tsvector *in; char *tmpbuf, *cur; int4 i, @@ -388,28 +430,30 @@ tsvector_in(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("word is too long"))); - arr[len].entry.len= state.curpos - state.word; + arr[len].entry.len = state.curpos - state.word; if (cur - tmpbuf > MAXSTRPOS) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("too long value"))); - arr[len].entry.pos=cur - tmpbuf; + arr[len].entry.pos = cur - tmpbuf; memcpy((void *) cur, (void *) state.word, arr[len].entry.len); cur += arr[len].entry.len; - if ( state.alen ) { - arr[len].entry.haspos=1; + if (state.alen) + { + arr[len].entry.haspos = 1; arr[len].pos = state.pos; - } else - arr[len].entry.haspos=0; + } + else + arr[len].entry.haspos = 0; len++; } pfree(state.word); - if ( len > 0 ) + if (len > 0) len = uniqueentry(arr, len, tmpbuf, &buflen); totallen = CALCDATASIZE(len, buflen); in = (tsvector *) palloc(totallen); - memset(in,0,totallen); + memset(in, 0, totallen); in->len = totallen; in->size = len; cur = STRPTR(in); @@ -417,14 +461,15 @@ tsvector_in(PG_FUNCTION_ARGS) for (i = 0; i < len; i++) { memcpy((void *) cur, (void *) &tmpbuf[arr[i].entry.pos], arr[i].entry.len); - arr[i].entry.pos=cur - STRPTR(in); + arr[i].entry.pos = cur - STRPTR(in); cur += SHORTALIGN(arr[i].entry.len); - if ( arr[i].entry.haspos ) { - memcpy( cur, arr[i].pos, (*(uint16*)arr[i].pos + 1) * sizeof(WordEntryPos)); - cur += (*(uint16*)arr[i].pos + 1) * sizeof(WordEntryPos); - pfree( arr[i].pos ); + if (arr[i].entry.haspos) + { + memcpy(cur, arr[i].pos, (*(uint16 *) arr[i].pos + 1) * sizeof(WordEntryPos)); + cur += (*(uint16 *) arr[i].pos + 1) * sizeof(WordEntryPos); + pfree(arr[i].pos); } - memcpy( &(inarr[i]), &(arr[i].entry), sizeof(WordEntry) ); + memcpy(&(inarr[i]), &(arr[i].entry), sizeof(WordEntry)); } pfree(tmpbuf); pfree(arr); @@ -434,7 +479,7 @@ tsvector_in(PG_FUNCTION_ARGS) Datum tsvector_length(PG_FUNCTION_ARGS) { - tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); int4 ret = in->size; PG_FREE_IF_COPY(in, 0); @@ -444,26 +489,28 @@ tsvector_length(PG_FUNCTION_ARGS) Datum tsvector_out(PG_FUNCTION_ARGS) { - tsvector *out = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + tsvector *out = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); char *outbuf; int4 i, j, - lenbuf = 0, pp; + lenbuf = 0, + pp; WordEntry *ptr = ARRPTR(out); char *curin, *curout; - lenbuf=out->size * 2 /* '' */ + out->size - 1 /* space */ + 2 /*\0*/; - for (i = 0; i < out->size; i++) { - lenbuf += ptr[i].len*2 /*for escape */; - if ( ptr[i].haspos ) - lenbuf += 7*POSDATALEN(out, &(ptr[i])); - } + lenbuf = out->size * 2 /* '' */ + out->size - 1 /* space */ + 2 /* \0 */ ; + for (i = 0; i < out->size; i++) + { + lenbuf += ptr[i].len * 2 /* for escape */ ; + if (ptr[i].haspos) + lenbuf += 7 * POSDATALEN(out, &(ptr[i])); + } curout = outbuf = (char *) palloc(lenbuf); for (i = 0; i < out->size; i++) { - curin = STRPTR(out)+ptr->pos; + curin = STRPTR(out) + ptr->pos; if (i != 0) *curout++ = ' '; *curout++ = '\''; @@ -481,27 +528,40 @@ tsvector_out(PG_FUNCTION_ARGS) *curout++ = *curin++; } *curout++ = '\''; - if ( (pp=POSDATALEN(out,ptr)) != 0 ) { + if ((pp = POSDATALEN(out, ptr)) != 0) + { WordEntryPos *wptr; + *curout++ = ':'; - wptr=POSDATAPTR(out,ptr); - while(pp) { - sprintf(curout,"%d",wptr->pos); - curout=strchr(curout,'\0'); - switch( wptr->weight ) { - case 3: *curout++ = 'A'; break; - case 2: *curout++ = 'B'; break; - case 1: *curout++ = 'C'; break; - case 0: - default: break; + wptr = POSDATAPTR(out, ptr); + while (pp) + { + sprintf(curout, "%d", wptr->pos); + curout = strchr(curout, '\0'); + switch (wptr->weight) + { + case 3: + *curout++ = 'A'; + break; + case 2: + *curout++ = 'B'; + break; + case 1: + *curout++ = 'C'; + break; + case 0: + default: + break; } - if ( pp>1 ) *curout++ = ','; - pp--; wptr++; + if (pp > 1) + *curout++ = ','; + pp--; + wptr++; } } ptr++; } - *curout='\0'; + *curout = '\0'; outbuf[lenbuf - 1] = '\0'; PG_FREE_IF_COPY(out, 0); PG_RETURN_POINTER(outbuf); @@ -510,13 +570,15 @@ tsvector_out(PG_FUNCTION_ARGS) static int compareWORD(const void *a, const void *b) { - if (((WORD *) a)->len == ((WORD *) b)->len) { - int res = strncmp( - ((WORD *) a)->word, - ((WORD *) b)->word, - ((WORD *) b)->len); - if ( res==0 ) - return ( ((WORD *) a)->pos.pos > ((WORD *) b)->pos.pos ) ? 1 : -1; + if (((WORD *) a)->len == ((WORD *) b)->len) + { + int res = strncmp( + ((WORD *) a)->word, + ((WORD *) b)->word, + ((WORD *) b)->len); + + if (res == 0) + return (((WORD *) a)->pos.pos > ((WORD *) b)->pos.pos) ? 1 : -1; return res; } return (((WORD *) a)->len > ((WORD *) b)->len) ? 1 : -1; @@ -527,14 +589,15 @@ uniqueWORD(WORD * a, int4 l) { WORD *ptr, *res; - int tmppos; - - if (l == 1) { - tmppos=LIMITPOS(a->pos.pos); - a->alen=2; - a->pos.apos=(uint16*)palloc( sizeof(uint16)*a->alen ); - a->pos.apos[0]=1; - a->pos.apos[1]=tmppos; + int tmppos; + + if (l == 1) + { + tmppos = LIMITPOS(a->pos.pos); + a->alen = 2; + a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen); + a->pos.apos[0] = 1; + a->pos.apos[1] = tmppos; return l; } @@ -542,11 +605,11 @@ uniqueWORD(WORD * a, int4 l) ptr = a + 1; qsort((void *) a, l, sizeof(WORD), compareWORD); - tmppos=LIMITPOS(a->pos.pos); - a->alen=2; - a->pos.apos=(uint16*)palloc( sizeof(uint16)*a->alen ); - a->pos.apos[0]=1; - a->pos.apos[1]=tmppos; + tmppos = LIMITPOS(a->pos.pos); + a->alen = 2; + a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen); + a->pos.apos[0] = 1; + a->pos.apos[1] = tmppos; while (ptr - a < l) { @@ -556,20 +619,24 @@ uniqueWORD(WORD * a, int4 l) res++; res->len = ptr->len; res->word = ptr->word; - tmppos=LIMITPOS(ptr->pos.pos); - res->alen=2; - res->pos.apos=(uint16*)palloc( sizeof(uint16)*res->alen ); - res->pos.apos[0]=1; - res->pos.apos[1]=tmppos; - } else { + tmppos = LIMITPOS(ptr->pos.pos); + res->alen = 2; + res->pos.apos = (uint16 *) palloc(sizeof(uint16) * res->alen); + res->pos.apos[0] = 1; + res->pos.apos[1] = tmppos; + } + else + { pfree(ptr->word); - if ( res->pos.apos[0] < MAXNUMPOS-1 && res->pos.apos[ res->pos.apos[0] ] != MAXENTRYPOS-1 ) { - if ( res->pos.apos[0]+1 >= res->alen ) { - res->alen*=2; - res->pos.apos=(uint16*)repalloc( res->pos.apos, sizeof(uint16)*res->alen ); + if (res->pos.apos[0] < MAXNUMPOS - 1 && res->pos.apos[res->pos.apos[0]] != MAXENTRYPOS - 1) + { + if (res->pos.apos[0] + 1 >= res->alen) + { + res->alen *= 2; + res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen); } - res->pos.apos[ res->pos.apos[0]+1 ] = LIMITPOS(ptr->pos.pos); - res->pos.apos[0]++; + res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos); + res->pos.apos[0]++; } } ptr++; @@ -584,25 +651,27 @@ uniqueWORD(WORD * a, int4 l) static tsvector * makevalue(PRSTEXT * prs) { - int4 i,j, + int4 i, + j, lenstr = 0, totallen; - tsvector *in; + tsvector *in; WordEntry *ptr; char *str, *cur; prs->curwords = uniqueWORD(prs->words, prs->curwords); - for (i = 0; i < prs->curwords; i++) { + for (i = 0; i < prs->curwords; i++) + { lenstr += SHORTALIGN(prs->words[i].len); - if ( prs->words[i].alen ) + if (prs->words[i].alen) lenstr += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos); } totallen = CALCDATASIZE(prs->curwords, lenstr); in = (tsvector *) palloc(totallen); - memset(in,0,totallen); + memset(in, 0, totallen); in->len = totallen; in->size = prs->curwords; @@ -615,24 +684,27 @@ makevalue(PRSTEXT * prs) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("value is too big"))); - ptr->pos= cur - str; + ptr->pos = cur - str; memcpy((void *) cur, (void *) prs->words[i].word, prs->words[i].len); pfree(prs->words[i].word); cur += SHORTALIGN(prs->words[i].len); - if ( prs->words[i].alen ) { + if (prs->words[i].alen) + { WordEntryPos *wptr; - - ptr->haspos=1; - *(uint16*)cur = prs->words[i].pos.apos[0]; - wptr=POSDATAPTR(in,ptr); - for(j=0;j<*(uint16*)cur;j++) { - wptr[j].weight=0; - wptr[j].pos=prs->words[i].pos.apos[j+1]; + + ptr->haspos = 1; + *(uint16 *) cur = prs->words[i].pos.apos[0]; + wptr = POSDATAPTR(in, ptr); + for (j = 0; j < *(uint16 *) cur; j++) + { + wptr[j].weight = 0; + wptr[j].pos = prs->words[i].pos.apos[j + 1]; } cur += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos); pfree(prs->words[i].pos.apos); - } else - ptr->haspos=0; + } + else + ptr->haspos = 0; ptr++; } pfree(prs->words); @@ -645,70 +717,78 @@ to_tsvector(PG_FUNCTION_ARGS) { text *in = PG_GETARG_TEXT_P(1); PRSTEXT prs; - tsvector *out = NULL; - TSCfgInfo *cfg=findcfg(PG_GETARG_INT32(0)); + tsvector *out = NULL; + TSCfgInfo *cfg = findcfg(PG_GETARG_INT32(0)); prs.lenwords = 32; prs.curwords = 0; prs.pos = 0; prs.words = (WORD *) palloc(sizeof(WORD) * prs.lenwords); - + parsetext_v2(cfg, &prs, VARDATA(in), VARSIZE(in) - VARHDRSZ); PG_FREE_IF_COPY(in, 1); if (prs.curwords) out = makevalue(&prs); - else { + else + { pfree(prs.words); - out = palloc(CALCDATASIZE(0,0)); - out->len = CALCDATASIZE(0,0); + out = palloc(CALCDATASIZE(0, 0)); + out->len = CALCDATASIZE(0, 0); out->size = 0; - } + } PG_RETURN_POINTER(out); } Datum -to_tsvector_name(PG_FUNCTION_ARGS) { - text *cfg=PG_GETARG_TEXT_P(0); - Datum res = DirectFunctionCall3( - to_tsvector, - Int32GetDatum( name2id_cfg( cfg ) ), - PG_GETARG_DATUM(1), - (Datum)0 +to_tsvector_name(PG_FUNCTION_ARGS) +{ + text *cfg = PG_GETARG_TEXT_P(0); + Datum res = DirectFunctionCall3( + to_tsvector, + Int32GetDatum(name2id_cfg(cfg)), + PG_GETARG_DATUM(1), + (Datum) 0 ); - PG_FREE_IF_COPY(cfg,0); - PG_RETURN_DATUM(res); + + PG_FREE_IF_COPY(cfg, 0); + PG_RETURN_DATUM(res); } Datum -to_tsvector_current(PG_FUNCTION_ARGS) { - Datum res = DirectFunctionCall3( - to_tsvector, - Int32GetDatum( get_currcfg() ), - PG_GETARG_DATUM(0), - (Datum)0 +to_tsvector_current(PG_FUNCTION_ARGS) +{ + Datum res = DirectFunctionCall3( + to_tsvector, + Int32GetDatum(get_currcfg()), + PG_GETARG_DATUM(0), + (Datum) 0 ); - PG_RETURN_DATUM(res); + + PG_RETURN_DATUM(res); } static Oid -findFunc(char *fname) { - FuncCandidateList clist,ptr; - Oid funcid = InvalidOid; - List *names=makeList1(makeString(fname)); +findFunc(char *fname) +{ + FuncCandidateList clist, + ptr; + Oid funcid = InvalidOid; + List *names = makeList1(makeString(fname)); ptr = clist = FuncnameGetCandidates(names, 1); freeList(names); - if ( !ptr ) + if (!ptr) return funcid; - while(ptr) { - if ( ptr->args[0] == TEXTOID && funcid == InvalidOid ) - funcid=ptr->oid; - clist=ptr->next; + while (ptr) + { + if (ptr->args[0] == TEXTOID && funcid == InvalidOid) + funcid = ptr->oid; + clist = ptr->next; pfree(ptr); - ptr=clist; + ptr = clist; } return funcid; @@ -724,12 +804,12 @@ tsearch2(PG_FUNCTION_ARGS) Trigger *trigger; Relation rel; HeapTuple rettuple = NULL; - TSCfgInfo *cfg=findcfg(get_currcfg()); + TSCfgInfo *cfg = findcfg(get_currcfg()); int numidxattr, i; PRSTEXT prs; Datum datum = (Datum) 0; - Oid funcoid = InvalidOid; + Oid funcoid = InvalidOid; if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */ @@ -782,8 +862,8 @@ tsearch2(PG_FUNCTION_ARGS) numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]); if (numattr == SPI_ERROR_NOATTRIBUTE) { - funcoid=findFunc(trigger->tgargs[i]); - if ( funcoid==InvalidOid ) + funcoid = findFunc(trigger->tgargs[i]); + if (funcoid == InvalidOid) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("could not find function or field \"%s\"", @@ -805,19 +885,22 @@ tsearch2(PG_FUNCTION_ARGS) if (isnull) continue; - if ( funcoid!=InvalidOid ) { - text *txttmp = (text *) DatumGetPointer( OidFunctionCall1( - funcoid, - PointerGetDatum(txt_toasted) - )); + if (funcoid != InvalidOid) + { + text *txttmp = (text *) DatumGetPointer(OidFunctionCall1( + funcoid, + PointerGetDatum(txt_toasted) + )); + txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txttmp))); - if ( txt == txttmp ) + if (txt == txttmp) txt_toasted = PointerGetDatum(txt); - } else - txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txt_toasted))); + } + else + txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txt_toasted))); parsetext_v2(cfg, &prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ); - if (txt != (text*)DatumGetPointer(txt_toasted) ) + if (txt != (text *) DatumGetPointer(txt_toasted)) pfree(txt); } @@ -831,8 +914,9 @@ tsearch2(PG_FUNCTION_ARGS) } else { - tsvector *out = palloc(CALCDATASIZE(0,0)); - out->len = CALCDATASIZE(0,0); + tsvector *out = palloc(CALCDATASIZE(0, 0)); + + out->len = CALCDATASIZE(0, 0); out->size = 0; datum = PointerGetDatum(out); pfree(prs.words); diff --git a/contrib/tsearch2/tsvector.h b/contrib/tsearch2/tsvector.h index af06e3cf52f..bb695247463 100644 --- a/contrib/tsearch2/tsvector.h +++ b/contrib/tsearch2/tsvector.h @@ -12,23 +12,27 @@ #include "utils/builtins.h" #include "storage/bufpage.h" -typedef struct { +typedef struct +{ uint32 - haspos:1, - len:11, /* MAX 2Kb */ - pos:20; /* MAX 1Mb */ + haspos:1, + len:11, /* MAX 2Kb */ + pos:20; /* MAX 1Mb */ } WordEntry; + #define MAXSTRLEN ( 1<<11 ) #define MAXSTRPOS ( 1<<20 ) -typedef struct { +typedef struct +{ uint16 - weight:2, - pos:14; -} WordEntryPos; -#define MAXENTRYPOS (1<<14) + weight:2, + pos:14; +} WordEntryPos; + +#define MAXENTRYPOS (1<<14) #define MAXNUMPOS 256 -#define LIMITPOS(x) ( ( (x) >= MAXENTRYPOS ) ? (MAXENTRYPOS-1) : (x) ) +#define LIMITPOS(x) ( ( (x) >= MAXENTRYPOS ) ? (MAXENTRYPOS-1) : (x) ) typedef struct { @@ -43,13 +47,14 @@ typedef struct #define STRPTR(x) ( (char*)x + DATAHDRSIZE + ( sizeof(WordEntry) * ((tsvector*)x)->size ) ) #define STRSIZE(x) ( ((tsvector*)x)->len - DATAHDRSIZE - ( sizeof(WordEntry) * ((tsvector*)x)->size ) ) #define _POSDATAPTR(x,e) (STRPTR(x)+((WordEntry*)(e))->pos+SHORTALIGN(((WordEntry*)(e))->len)) -#define POSDATALEN(x,e) ( ( ((WordEntry*)(e))->haspos ) ? (*(uint16*)_POSDATAPTR(x,e)) : 0 ) -#define POSDATAPTR(x,e) ( (WordEntryPos*)( _POSDATAPTR(x,e)+sizeof(uint16) ) ) +#define POSDATALEN(x,e) ( ( ((WordEntry*)(e))->haspos ) ? (*(uint16*)_POSDATAPTR(x,e)) : 0 ) +#define POSDATAPTR(x,e) ( (WordEntryPos*)( _POSDATAPTR(x,e)+sizeof(uint16) ) ) -typedef struct { - WordEntry entry; - WordEntryPos *pos; +typedef struct +{ + WordEntry entry; + WordEntryPos *pos; } WordEntryIN; typedef struct @@ -60,7 +65,7 @@ typedef struct int4 len; int4 state; int4 alen; - WordEntryPos *pos; + WordEntryPos *pos; bool oprisdelim; } TI_IN_STATE; diff --git a/contrib/tsearch2/tsvector_op.c b/contrib/tsearch2/tsvector_op.c index cfef02db85d..2ffd4ca5848 100644 --- a/contrib/tsearch2/tsvector_op.c +++ b/contrib/tsearch2/tsvector_op.c @@ -33,30 +33,33 @@ Datum concat(PG_FUNCTION_ARGS); Datum strip(PG_FUNCTION_ARGS) { - tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - tsvector *out; - int i,len=0; - WordEntry *arrin=ARRPTR(in), *arrout; - char *cur; + tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + tsvector *out; + int i, + len = 0; + WordEntry *arrin = ARRPTR(in), + *arrout; + char *cur; - for(i=0;i<in->size;i++) - len += SHORTALIGN( arrin[i].len ); + for (i = 0; i < in->size; i++) + len += SHORTALIGN(arrin[i].len); len = CALCDATASIZE(in->size, len); - out=(tsvector*)palloc(len); - memset(out,0,len); - out->len=len; - out->size=in->size; - arrout=ARRPTR(out); - cur=STRPTR(out); - for(i=0;i<in->size;i++) { - memcpy(cur, STRPTR(in)+arrin[i].pos, arrin[i].len); + out = (tsvector *) palloc(len); + memset(out, 0, len); + out->len = len; + out->size = in->size; + arrout = ARRPTR(out); + cur = STRPTR(out); + for (i = 0; i < in->size; i++) + { + memcpy(cur, STRPTR(in) + arrin[i].pos, arrin[i].len); arrout[i].haspos = 0; arrout[i].len = arrin[i].len; arrout[i].pos = cur - STRPTR(out); - cur += SHORTALIGN( arrout[i].len ); + cur += SHORTALIGN(arrout[i].len); } - + PG_FREE_IF_COPY(in, 0); PG_RETURN_POINTER(out); } @@ -64,200 +67,263 @@ strip(PG_FUNCTION_ARGS) Datum setweight(PG_FUNCTION_ARGS) { - tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char cw = PG_GETARG_CHAR(1); - tsvector *out; - int i,j; - WordEntry *entry; + tsvector *in = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char cw = PG_GETARG_CHAR(1); + tsvector *out; + int i, + j; + WordEntry *entry; WordEntryPos *p; - int w=0; + int w = 0; - switch(tolower(cw)) { - case 'a': w=3; break; - case 'b': w=2; break; - case 'c': w=1; break; - case 'd': w=0; break; - /* internal error */ - default: elog(ERROR,"unrecognized weight"); + switch (tolower(cw)) + { + case 'a': + w = 3; + break; + case 'b': + w = 2; + break; + case 'c': + w = 1; + break; + case 'd': + w = 0; + break; + /* internal error */ + default: + elog(ERROR, "unrecognized weight"); } - out=(tsvector*)palloc(in->len); - memcpy(out,in,in->len); - entry=ARRPTR(out); - i=out->size; - while(i--) { - if ( (j=POSDATALEN(out,entry)) != 0 ) { - p=POSDATAPTR(out,entry); - while(j--) { - p->weight=w; + out = (tsvector *) palloc(in->len); + memcpy(out, in, in->len); + entry = ARRPTR(out); + i = out->size; + while (i--) + { + if ((j = POSDATALEN(out, entry)) != 0) + { + p = POSDATAPTR(out, entry); + while (j--) + { + p->weight = w; p++; } } entry++; } - + PG_FREE_IF_COPY(in, 0); PG_RETURN_POINTER(out); } static int -compareEntry(char *ptra, WordEntry* a, char *ptrb, WordEntry* b) +compareEntry(char *ptra, WordEntry * a, char *ptrb, WordEntry * b) { - if ( a->len == b->len) - { - return strncmp( - ptra + a->pos, - ptrb + b->pos, - a->len); - } - return ( a->len > b->len ) ? 1 : -1; + if (a->len == b->len) + { + return strncmp( + ptra + a->pos, + ptrb + b->pos, + a->len); + } + return (a->len > b->len) ? 1 : -1; } static int4 -add_pos(tsvector *src, WordEntry *srcptr, tsvector *dest, WordEntry *destptr, int4 maxpos ) { - uint16 *clen = (uint16*)_POSDATAPTR(dest,destptr); - int i; - uint16 slen = POSDATALEN(src, srcptr), startlen; - WordEntryPos *spos=POSDATAPTR(src, srcptr), *dpos=POSDATAPTR(dest,destptr); +add_pos(tsvector * src, WordEntry * srcptr, tsvector * dest, WordEntry * destptr, int4 maxpos) +{ + uint16 *clen = (uint16 *) _POSDATAPTR(dest, destptr); + int i; + uint16 slen = POSDATALEN(src, srcptr), + startlen; + WordEntryPos *spos = POSDATAPTR(src, srcptr), + *dpos = POSDATAPTR(dest, destptr); - if ( ! destptr->haspos ) - *clen=0; + if (!destptr->haspos) + *clen = 0; startlen = *clen; - for(i=0; i<slen && *clen<MAXNUMPOS && ( *clen==0 || dpos[ *clen-1 ].pos != MAXENTRYPOS-1 ) ;i++) { - dpos[ *clen ].weight = spos[i].weight; - dpos[ *clen ].pos = LIMITPOS(spos[i].pos + maxpos); + for (i = 0; i < slen && *clen < MAXNUMPOS && (*clen == 0 || dpos[*clen - 1].pos != MAXENTRYPOS - 1); i++) + { + dpos[*clen].weight = spos[i].weight; + dpos[*clen].pos = LIMITPOS(spos[i].pos + maxpos); (*clen)++; } - if ( *clen != startlen ) - destptr->haspos=1; - return *clen - startlen; + if (*clen != startlen) + destptr->haspos = 1; + return *clen - startlen; } Datum -concat(PG_FUNCTION_ARGS) { - tsvector *in1 = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - tsvector *in2 = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); - tsvector *out; - WordEntry *ptr; - WordEntry *ptr1,*ptr2; +concat(PG_FUNCTION_ARGS) +{ + tsvector *in1 = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + tsvector *in2 = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + tsvector *out; + WordEntry *ptr; + WordEntry *ptr1, + *ptr2; WordEntryPos *p; - int maxpos=0,i,j,i1,i2; - char *cur; - char *data,*data1,*data2; + int maxpos = 0, + i, + j, + i1, + i2; + char *cur; + char *data, + *data1, + *data2; - ptr=ARRPTR(in1); - i=in1->size; - while(i--) { - if ( (j=POSDATALEN(in1,ptr)) != 0 ) { - p=POSDATAPTR(in1,ptr); - while(j--) { - if ( p->pos > maxpos ) + ptr = ARRPTR(in1); + i = in1->size; + while (i--) + { + if ((j = POSDATALEN(in1, ptr)) != 0) + { + p = POSDATAPTR(in1, ptr); + while (j--) + { + if (p->pos > maxpos) maxpos = p->pos; p++; } } ptr++; } - - ptr1=ARRPTR(in1); ptr2=ARRPTR(in2); - data1=STRPTR(in1); data2=STRPTR(in2); - i1=in1->size; i2=in2->size; - out=(tsvector*)palloc( in1->len + in2->len ); - memset(out,0,in1->len + in2->len); + + ptr1 = ARRPTR(in1); + ptr2 = ARRPTR(in2); + data1 = STRPTR(in1); + data2 = STRPTR(in2); + i1 = in1->size; + i2 = in2->size; + out = (tsvector *) palloc(in1->len + in2->len); + memset(out, 0, in1->len + in2->len); out->len = in1->len + in2->len; out->size = in1->size + in2->size; - data=cur=STRPTR(out); - ptr=ARRPTR(out); - while( i1 && i2 ) { - int cmp=compareEntry(data1,ptr1,data2,ptr2); - if ( cmp < 0 ) { /* in1 first */ + data = cur = STRPTR(out); + ptr = ARRPTR(out); + while (i1 && i2) + { + int cmp = compareEntry(data1, ptr1, data2, ptr2); + + if (cmp < 0) + { /* in1 first */ ptr->haspos = ptr1->haspos; ptr->len = ptr1->len; - memcpy( cur, data1 + ptr1->pos, ptr1->len ); - ptr->pos = cur - data; - cur+=SHORTALIGN(ptr1->len); - if ( ptr->haspos ) { - memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16)); - cur+=POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16); + memcpy(cur, data1 + ptr1->pos, ptr1->len); + ptr->pos = cur - data; + cur += SHORTALIGN(ptr1->len); + if (ptr->haspos) + { + memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16)); + cur += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16); } - ptr++; ptr1++; i1--; - } else if ( cmp>0 ) { /* in2 first */ + ptr++; + ptr1++; + i1--; + } + else if (cmp > 0) + { /* in2 first */ ptr->haspos = ptr2->haspos; ptr->len = ptr2->len; - memcpy( cur, data2 + ptr2->pos, ptr2->len ); - ptr->pos = cur - data; - cur+=SHORTALIGN(ptr2->len); - if ( ptr->haspos ) { - int addlen = add_pos(in2, ptr2, out, ptr, maxpos ); - if ( addlen == 0 ) - ptr->haspos=0; + memcpy(cur, data2 + ptr2->pos, ptr2->len); + ptr->pos = cur - data; + cur += SHORTALIGN(ptr2->len); + if (ptr->haspos) + { + int addlen = add_pos(in2, ptr2, out, ptr, maxpos); + + if (addlen == 0) + ptr->haspos = 0; else - cur += addlen*sizeof(WordEntryPos) + sizeof(uint16); + cur += addlen * sizeof(WordEntryPos) + sizeof(uint16); } - ptr++; ptr2++; i2--; - } else { + ptr++; + ptr2++; + i2--; + } + else + { ptr->haspos = ptr1->haspos | ptr2->haspos; ptr->len = ptr1->len; - memcpy( cur, data1 + ptr1->pos, ptr1->len ); - ptr->pos = cur - data; - cur+=SHORTALIGN(ptr1->len); - if ( ptr->haspos ) { - if ( ptr1->haspos ) { - memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16)); - cur+=POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16); - if ( ptr2->haspos ) - cur += add_pos(in2, ptr2, out, ptr, maxpos )*sizeof(WordEntryPos); - } else if ( ptr2->haspos ) { - int addlen = add_pos(in2, ptr2, out, ptr, maxpos ); - if ( addlen == 0 ) - ptr->haspos=0; + memcpy(cur, data1 + ptr1->pos, ptr1->len); + ptr->pos = cur - data; + cur += SHORTALIGN(ptr1->len); + if (ptr->haspos) + { + if (ptr1->haspos) + { + memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16)); + cur += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16); + if (ptr2->haspos) + cur += add_pos(in2, ptr2, out, ptr, maxpos) * sizeof(WordEntryPos); + } + else if (ptr2->haspos) + { + int addlen = add_pos(in2, ptr2, out, ptr, maxpos); + + if (addlen == 0) + ptr->haspos = 0; else - cur += addlen*sizeof(WordEntryPos) + sizeof(uint16); + cur += addlen * sizeof(WordEntryPos) + sizeof(uint16); } } - ptr++; ptr1++; ptr2++; i1--; i2--; + ptr++; + ptr1++; + ptr2++; + i1--; + i2--; } } - while(i1) { + while (i1) + { ptr->haspos = ptr1->haspos; ptr->len = ptr1->len; - memcpy( cur, data1 + ptr1->pos, ptr1->len ); - ptr->pos = cur - data; - cur+=SHORTALIGN(ptr1->len); - if ( ptr->haspos ) { - memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16)); - cur+=POSDATALEN(in1, ptr1)*sizeof(WordEntryPos) + sizeof(uint16); + memcpy(cur, data1 + ptr1->pos, ptr1->len); + ptr->pos = cur - data; + cur += SHORTALIGN(ptr1->len); + if (ptr->haspos) + { + memcpy(cur, _POSDATAPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16)); + cur += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16); } - ptr++; ptr1++; i1--; + ptr++; + ptr1++; + i1--; } - while(i2) { + while (i2) + { ptr->haspos = ptr2->haspos; ptr->len = ptr2->len; - memcpy( cur, data2 + ptr2->pos, ptr2->len ); - ptr->pos = cur - data; - cur+=SHORTALIGN(ptr2->len); - if ( ptr->haspos ) { - int addlen = add_pos(in2, ptr2, out, ptr, maxpos ); - if ( addlen == 0 ) - ptr->haspos=0; + memcpy(cur, data2 + ptr2->pos, ptr2->len); + ptr->pos = cur - data; + cur += SHORTALIGN(ptr2->len); + if (ptr->haspos) + { + int addlen = add_pos(in2, ptr2, out, ptr, maxpos); + + if (addlen == 0) + ptr->haspos = 0; else - cur += addlen*sizeof(WordEntryPos) + sizeof(uint16); + cur += addlen * sizeof(WordEntryPos) + sizeof(uint16); } - ptr++; ptr2++; i2--; + ptr++; + ptr2++; + i2--; } - - out->size=ptr-ARRPTR(out); - out->len = CALCDATASIZE( out->size, cur-data ); - if ( data != STRPTR(out) ) - memmove( STRPTR(out), data, cur-data ); + + out->size = ptr - ARRPTR(out); + out->len = CALCDATASIZE(out->size, cur - data); + if (data != STRPTR(out)) + memmove(STRPTR(out), data, cur - data); PG_FREE_IF_COPY(in1, 0); PG_FREE_IF_COPY(in2, 1); PG_RETURN_POINTER(out); } - diff --git a/contrib/tsearch2/wordparser/deflex.c b/contrib/tsearch2/wordparser/deflex.c index ea596c507ca..bbf3271b666 100644 --- a/contrib/tsearch2/wordparser/deflex.c +++ b/contrib/tsearch2/wordparser/deflex.c @@ -1,6 +1,6 @@ #include "deflex.h" -const char *lex_descr[]={ +const char *lex_descr[] = { "", "Latin word", "Non-latin word", @@ -27,7 +27,7 @@ const char *lex_descr[]={ "HTML Entity" }; -const char *tok_alias[]={ +const char *tok_alias[] = { "", "lword", "nlword", @@ -53,4 +53,3 @@ const char *tok_alias[]={ "uint", "entity" }; - diff --git a/contrib/tsearch2/wparser.c b/contrib/tsearch2/wparser.c index deff94ce904..b7e45e51885 100644 --- a/contrib/tsearch2/wparser.c +++ b/contrib/tsearch2/wparser.c @@ -1,5 +1,5 @@ -/* - * interface functions to parser +/* + * interface functions to parser * Teodor Sigaev <teodor@sigaev.ru> */ #include <errno.h> @@ -21,154 +21,171 @@ /*********top interface**********/ -static void *plan_getparser=NULL; -static Oid current_parser_id=InvalidOid; +static void *plan_getparser = NULL; +static Oid current_parser_id = InvalidOid; void -init_prs(Oid id, WParserInfo *prs) { - Oid arg[1]={ OIDOID }; - bool isnull; - Datum pars[1]={ ObjectIdGetDatum(id) }; - int stat; - - memset(prs,0,sizeof(WParserInfo)); +init_prs(Oid id, WParserInfo * prs) +{ + Oid arg[1] = {OIDOID}; + bool isnull; + Datum pars[1] = {ObjectIdGetDatum(id)}; + int stat; + + memset(prs, 0, sizeof(WParserInfo)); SPI_connect(); - if ( !plan_getparser ) { - plan_getparser = SPI_saveplan( SPI_prepare( "select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from pg_ts_parser where oid = $1" , 1, arg ) ); - if ( !plan_getparser ) + if (!plan_getparser) + { + plan_getparser = SPI_saveplan(SPI_prepare("select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from pg_ts_parser where oid = $1", 1, arg)); + if (!plan_getparser) ts_error(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_getparser, pars, " ", 1); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) { - Oid oid=InvalidOid; - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + { + Oid oid = InvalidOid; + + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); fmgr_info_cxt(oid, &(prs->start_info), TopMemoryContext); - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull) ); + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull)); fmgr_info_cxt(oid, &(prs->getlexeme_info), TopMemoryContext); - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull) ); + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull)); fmgr_info_cxt(oid, &(prs->end_info), TopMemoryContext); - prs->lextype=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &isnull) ); - oid=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &isnull) ); + prs->lextype = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &isnull)); + oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &isnull)); fmgr_info_cxt(oid, &(prs->headline_info), TopMemoryContext); - prs->prs_id=id; - } else + prs->prs_id = id; + } + else ts_error(ERROR, "No parser with id %d", id); SPI_finish(); } -typedef struct { - WParserInfo *last_prs; - int len; - int reallen; - WParserInfo *list; +typedef struct +{ + WParserInfo *last_prs; + int len; + int reallen; + WParserInfo *list; SNMap name2id_map; -} PrsList; +} PrsList; -static PrsList PList = {NULL,0,0,NULL,{0,0,NULL}}; +static PrsList PList = {NULL, 0, 0, NULL, {0, 0, NULL}}; -void -reset_prs(void) { - freeSNMap( &(PList.name2id_map) ); - if ( PList.list ) +void +reset_prs(void) +{ + freeSNMap(&(PList.name2id_map)); + if (PList.list) free(PList.list); - memset(&PList,0,sizeof(PrsList)); + memset(&PList, 0, sizeof(PrsList)); } static int -compareprs(const void *a, const void *b) { - return ((WParserInfo*)a)->prs_id - ((WParserInfo*)b)->prs_id; +compareprs(const void *a, const void *b) +{ + return ((WParserInfo *) a)->prs_id - ((WParserInfo *) b)->prs_id; } WParserInfo * -findprs(Oid id) { +findprs(Oid id) +{ /* last used prs */ - if ( PList.last_prs && PList.last_prs->prs_id==id ) + if (PList.last_prs && PList.last_prs->prs_id == id) return PList.last_prs; /* already used prs */ - if ( PList.len != 0 ) { + if (PList.len != 0) + { WParserInfo key; - key.prs_id=id; + + key.prs_id = id; PList.last_prs = bsearch(&key, PList.list, PList.len, sizeof(WParserInfo), compareprs); - if ( PList.last_prs != NULL ) + if (PList.last_prs != NULL) return PList.last_prs; } /* last chance */ - if ( PList.len==PList.reallen ) { + if (PList.len == PList.reallen) + { WParserInfo *tmp; - int reallen = ( PList.reallen ) ? 2*PList.reallen : 16; - tmp=(WParserInfo*)realloc(PList.list,sizeof(WParserInfo)*reallen); - if ( !tmp ) - ts_error(ERROR,"No memory"); - PList.reallen=reallen; - PList.list=tmp; + int reallen = (PList.reallen) ? 2 * PList.reallen : 16; + + tmp = (WParserInfo *) realloc(PList.list, sizeof(WParserInfo) * reallen); + if (!tmp) + ts_error(ERROR, "No memory"); + PList.reallen = reallen; + PList.list = tmp; } - PList.last_prs=&(PList.list[PList.len]); + PList.last_prs = &(PList.list[PList.len]); init_prs(id, PList.last_prs); PList.len++; qsort(PList.list, PList.len, sizeof(WParserInfo), compareprs); - return findprs(id); /* qsort changed order!! */; + return findprs(id); /* qsort changed order!! */ ; } -static void *plan_name2id=NULL; +static void *plan_name2id = NULL; Oid -name2id_prs(text *name) { - Oid arg[1]={ TEXTOID }; - bool isnull; - Datum pars[1]={ PointerGetDatum(name) }; - int stat; - Oid id=findSNMap_t( &(PList.name2id_map), name ); - - if ( id ) +name2id_prs(text *name) +{ + Oid arg[1] = {TEXTOID}; + bool isnull; + Datum pars[1] = {PointerGetDatum(name)}; + int stat; + Oid id = findSNMap_t(&(PList.name2id_map), name); + + if (id) return id; - + SPI_connect(); - if ( !plan_name2id ) { - plan_name2id = SPI_saveplan( SPI_prepare( "select oid from pg_ts_parser where prs_name = $1" , 1, arg ) ); - if ( !plan_name2id ) + if (!plan_name2id) + { + plan_name2id = SPI_saveplan(SPI_prepare("select oid from pg_ts_parser where prs_name = $1", 1, arg)); + if (!plan_name2id) ts_error(ERROR, "SPI_prepare() failed"); } stat = SPI_execp(plan_name2id, pars, " ", 1); - if ( stat < 0 ) - ts_error (ERROR, "SPI_execp return %d", stat); - if ( SPI_processed > 0 ) - id=DatumGetObjectId( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); - else + if (stat < 0) + ts_error(ERROR, "SPI_execp return %d", stat); + if (SPI_processed > 0) + id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); + else ts_error(ERROR, "No parser '%s'", text2char(name)); SPI_finish(); - addSNMap_t( &(PList.name2id_map), name, id ); + addSNMap_t(&(PList.name2id_map), name, id); return id; } /******sql-level interface******/ -typedef struct { - int cur; - LexDescr *list; -} TypeStorage; +typedef struct +{ + int cur; + LexDescr *list; +} TypeStorage; static void -setup_firstcall(FuncCallContext *funcctx, Oid prsid) { - TupleDesc tupdesc; - MemoryContext oldcontext; - TypeStorage *st; - WParserInfo *prs = findprs(prsid); +setup_firstcall(FuncCallContext *funcctx, Oid prsid) +{ + TupleDesc tupdesc; + MemoryContext oldcontext; + TypeStorage *st; + WParserInfo *prs = findprs(prsid); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - st=(TypeStorage*)palloc( sizeof(TypeStorage) ); - st->cur=0; - st->list = (LexDescr*)DatumGetPointer( - OidFunctionCall1( prs->lextype, PointerGetDatum(prs->prs) ) - ); - funcctx->user_fctx = (void*)st; + st = (TypeStorage *) palloc(sizeof(TypeStorage)); + st->cur = 0; + st->list = (LexDescr *) DatumGetPointer( + OidFunctionCall1(prs->lextype, PointerGetDatum(prs->prs)) + ); + funcctx->user_fctx = (void *) st; tupdesc = RelationNameGetTupleDesc("tokentype"); funcctx->slot = TupleDescGetSlot(tupdesc); funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc); @@ -176,20 +193,22 @@ setup_firstcall(FuncCallContext *funcctx, Oid prsid) { } static Datum -process_call(FuncCallContext *funcctx) { - TypeStorage *st; - - st=(TypeStorage*)funcctx->user_fctx; - if ( st->list && st->list[st->cur].lexid ) { - Datum result; - char* values[3]; - char txtid[16]; - HeapTuple tuple; - - values[0]=txtid; - sprintf(txtid,"%d",st->list[st->cur].lexid); - values[1]=st->list[st->cur].alias; - values[2]=st->list[st->cur].descr; +process_call(FuncCallContext *funcctx) +{ + TypeStorage *st; + + st = (TypeStorage *) funcctx->user_fctx; + if (st->list && st->list[st->cur].lexid) + { + Datum result; + char *values[3]; + char txtid[16]; + HeapTuple tuple; + + values[0] = txtid; + sprintf(txtid, "%d", st->list[st->cur].lexid); + values[1] = st->list[st->cur].alias; + values[2] = st->list[st->cur].descr; tuple = BuildTupleFromCStrings(funcctx->attinmeta, values); result = TupleGetDatum(funcctx->slot, tuple); @@ -198,161 +217,179 @@ process_call(FuncCallContext *funcctx) { pfree(values[2]); st->cur++; return result; - } else { - if ( st->list ) pfree(st->list); + } + else + { + if (st->list) + pfree(st->list); pfree(st); } - return (Datum)0; + return (Datum) 0; } PG_FUNCTION_INFO_V1(token_type); -Datum token_type(PG_FUNCTION_ARGS); +Datum token_type(PG_FUNCTION_ARGS); Datum -token_type(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +token_type(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; - if (SRF_IS_FIRSTCALL()) { + if (SRF_IS_FIRSTCALL()) + { funcctx = SRF_FIRSTCALL_INIT(); - setup_firstcall(funcctx, PG_GETARG_OID(0) ); + setup_firstcall(funcctx, PG_GETARG_OID(0)); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=process_call(funcctx)) != (Datum)0 ) + if ((result = process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(token_type_byname); -Datum token_type_byname(PG_FUNCTION_ARGS); +Datum token_type_byname(PG_FUNCTION_ARGS); Datum -token_type_byname(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +token_type_byname(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { + text *name = PG_GETARG_TEXT_P(0); - if (SRF_IS_FIRSTCALL()) { - text *name = PG_GETARG_TEXT_P(0); funcctx = SRF_FIRSTCALL_INIT(); - setup_firstcall(funcctx, name2id_prs( name ) ); - PG_FREE_IF_COPY(name,0); + setup_firstcall(funcctx, name2id_prs(name)); + PG_FREE_IF_COPY(name, 0); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=process_call(funcctx)) != (Datum)0 ) + if ((result = process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(token_type_current); -Datum token_type_current(PG_FUNCTION_ARGS); +Datum token_type_current(PG_FUNCTION_ARGS); Datum -token_type_current(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +token_type_current(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; - if (SRF_IS_FIRSTCALL()) { + if (SRF_IS_FIRSTCALL()) + { funcctx = SRF_FIRSTCALL_INIT(); - if ( current_parser_id==InvalidOid ) - current_parser_id = name2id_prs( char2text("default") ); - setup_firstcall(funcctx, current_parser_id ); + if (current_parser_id == InvalidOid) + current_parser_id = name2id_prs(char2text("default")); + setup_firstcall(funcctx, current_parser_id); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=process_call(funcctx)) != (Datum)0 ) + if ((result = process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(set_curprs); -Datum set_curprs(PG_FUNCTION_ARGS); +Datum set_curprs(PG_FUNCTION_ARGS); Datum -set_curprs(PG_FUNCTION_ARGS) { - findprs(PG_GETARG_OID(0)); - current_parser_id=PG_GETARG_OID(0); - PG_RETURN_VOID(); +set_curprs(PG_FUNCTION_ARGS) +{ + findprs(PG_GETARG_OID(0)); + current_parser_id = PG_GETARG_OID(0); + PG_RETURN_VOID(); } PG_FUNCTION_INFO_V1(set_curprs_byname); -Datum set_curprs_byname(PG_FUNCTION_ARGS); +Datum set_curprs_byname(PG_FUNCTION_ARGS); Datum -set_curprs_byname(PG_FUNCTION_ARGS) { - text *name=PG_GETARG_TEXT_P(0); - - DirectFunctionCall1( - set_curprs, - ObjectIdGetDatum( name2id_prs(name) ) - ); - PG_FREE_IF_COPY(name, 0); - PG_RETURN_VOID(); +set_curprs_byname(PG_FUNCTION_ARGS) +{ + text *name = PG_GETARG_TEXT_P(0); + + DirectFunctionCall1( + set_curprs, + ObjectIdGetDatum(name2id_prs(name)) + ); + PG_FREE_IF_COPY(name, 0); + PG_RETURN_VOID(); } -typedef struct { - int type; - char *lexem; -} LexemEntry; +typedef struct +{ + int type; + char *lexem; +} LexemEntry; + +typedef struct +{ + int cur; + int len; + LexemEntry *list; +} PrsStorage; -typedef struct { - int cur; - int len; - LexemEntry *list; -} PrsStorage; - static void -prs_setup_firstcall(FuncCallContext *funcctx, int prsid, text *txt) { - TupleDesc tupdesc; - MemoryContext oldcontext; - PrsStorage *st; - WParserInfo *prs = findprs(prsid); - char *lex=NULL; - int llen=0, type=0; +prs_setup_firstcall(FuncCallContext *funcctx, int prsid, text *txt) +{ + TupleDesc tupdesc; + MemoryContext oldcontext; + PrsStorage *st; + WParserInfo *prs = findprs(prsid); + char *lex = NULL; + int llen = 0, + type = 0; oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - st=(PrsStorage*)palloc( sizeof(PrsStorage) ); - st->cur=0; - st->len=16; - st->list=(LexemEntry*)palloc( sizeof(LexemEntry)*st->len ); - - prs->prs = (void*)DatumGetPointer( - FunctionCall2( - &(prs->start_info), - PointerGetDatum(VARDATA(txt)), - Int32GetDatum(VARSIZE(txt)-VARHDRSZ) - ) - ); - - while( ( type=DatumGetInt32(FunctionCall3( - &(prs->getlexeme_info), - PointerGetDatum(prs->prs), - PointerGetDatum(&lex), - PointerGetDatum(&llen))) ) != 0 ) { - - if ( st->cur>=st->len ) { - st->len=2*st->len; - st->list=(LexemEntry*)repalloc(st->list, sizeof(LexemEntry)*st->len); + st = (PrsStorage *) palloc(sizeof(PrsStorage)); + st->cur = 0; + st->len = 16; + st->list = (LexemEntry *) palloc(sizeof(LexemEntry) * st->len); + + prs->prs = (void *) DatumGetPointer( + FunctionCall2( + &(prs->start_info), + PointerGetDatum(VARDATA(txt)), + Int32GetDatum(VARSIZE(txt) - VARHDRSZ) + ) + ); + + while ((type = DatumGetInt32(FunctionCall3( + &(prs->getlexeme_info), + PointerGetDatum(prs->prs), + PointerGetDatum(&lex), + PointerGetDatum(&llen)))) != 0) + { + + if (st->cur >= st->len) + { + st->len = 2 * st->len; + st->list = (LexemEntry *) repalloc(st->list, sizeof(LexemEntry) * st->len); } - st->list[st->cur].lexem = palloc(llen+1); - memcpy( st->list[st->cur].lexem, lex, llen); - st->list[st->cur].lexem[llen]='\0'; - st->list[st->cur].type=type; + st->list[st->cur].lexem = palloc(llen + 1); + memcpy(st->list[st->cur].lexem, lex, llen); + st->list[st->cur].lexem[llen] = '\0'; + st->list[st->cur].type = type; st->cur++; } - + FunctionCall1( - &(prs->end_info), - PointerGetDatum(prs->prs) - ); + &(prs->end_info), + PointerGetDatum(prs->prs) + ); - st->len=st->cur; - st->cur=0; - - funcctx->user_fctx = (void*)st; + st->len = st->cur; + st->cur = 0; + + funcctx->user_fctx = (void *) st; tupdesc = RelationNameGetTupleDesc("tokenout"); funcctx->slot = TupleDescGetSlot(tupdesc); funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc); @@ -360,132 +397,148 @@ prs_setup_firstcall(FuncCallContext *funcctx, int prsid, text *txt) { } static Datum -prs_process_call(FuncCallContext *funcctx) { - PrsStorage *st; - - st=(PrsStorage*)funcctx->user_fctx; - if ( st->cur < st->len ) { - Datum result; - char* values[2]; - char tid[16]; - HeapTuple tuple; - - values[0]=tid; - sprintf(tid,"%d",st->list[st->cur].type); - values[1]=st->list[st->cur].lexem; +prs_process_call(FuncCallContext *funcctx) +{ + PrsStorage *st; + + st = (PrsStorage *) funcctx->user_fctx; + if (st->cur < st->len) + { + Datum result; + char *values[2]; + char tid[16]; + HeapTuple tuple; + + values[0] = tid; + sprintf(tid, "%d", st->list[st->cur].type); + values[1] = st->list[st->cur].lexem; tuple = BuildTupleFromCStrings(funcctx->attinmeta, values); result = TupleGetDatum(funcctx->slot, tuple); pfree(values[1]); st->cur++; return result; - } else { - if ( st->list ) pfree(st->list); + } + else + { + if (st->list) + pfree(st->list); pfree(st); } - return (Datum)0; + return (Datum) 0; } - + PG_FUNCTION_INFO_V1(parse); -Datum parse(PG_FUNCTION_ARGS); +Datum parse(PG_FUNCTION_ARGS); Datum -parse(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +parse(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { + text *txt = PG_GETARG_TEXT_P(1); - if (SRF_IS_FIRSTCALL()) { - text *txt = PG_GETARG_TEXT_P(1); funcctx = SRF_FIRSTCALL_INIT(); - prs_setup_firstcall(funcctx, PG_GETARG_OID(0),txt ); - PG_FREE_IF_COPY(txt,1); + prs_setup_firstcall(funcctx, PG_GETARG_OID(0), txt); + PG_FREE_IF_COPY(txt, 1); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=prs_process_call(funcctx)) != (Datum)0 ) + if ((result = prs_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(parse_byname); -Datum parse_byname(PG_FUNCTION_ARGS); +Datum parse_byname(PG_FUNCTION_ARGS); Datum -parse_byname(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +parse_byname(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { + text *name = PG_GETARG_TEXT_P(0); + text *txt = PG_GETARG_TEXT_P(1); - if (SRF_IS_FIRSTCALL()) { - text *name = PG_GETARG_TEXT_P(0); - text *txt = PG_GETARG_TEXT_P(1); funcctx = SRF_FIRSTCALL_INIT(); - prs_setup_firstcall(funcctx, name2id_prs( name ),txt ); - PG_FREE_IF_COPY(name,0); - PG_FREE_IF_COPY(txt,1); + prs_setup_firstcall(funcctx, name2id_prs(name), txt); + PG_FREE_IF_COPY(name, 0); + PG_FREE_IF_COPY(txt, 1); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=prs_process_call(funcctx)) != (Datum)0 ) + if ((result = prs_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(parse_current); -Datum parse_current(PG_FUNCTION_ARGS); +Datum parse_current(PG_FUNCTION_ARGS); Datum -parse_current(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; +parse_current(PG_FUNCTION_ARGS) +{ + FuncCallContext *funcctx; + Datum result; + + if (SRF_IS_FIRSTCALL()) + { + text *txt = PG_GETARG_TEXT_P(0); - if (SRF_IS_FIRSTCALL()) { - text *txt = PG_GETARG_TEXT_P(0); funcctx = SRF_FIRSTCALL_INIT(); - if ( current_parser_id==InvalidOid ) - current_parser_id = name2id_prs( char2text("default") ); - prs_setup_firstcall(funcctx, current_parser_id,txt ); - PG_FREE_IF_COPY(txt,0); + if (current_parser_id == InvalidOid) + current_parser_id = name2id_prs(char2text("default")); + prs_setup_firstcall(funcctx, current_parser_id, txt); + PG_FREE_IF_COPY(txt, 0); } funcctx = SRF_PERCALL_SETUP(); - if ( (result=prs_process_call(funcctx)) != (Datum)0 ) + if ((result = prs_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); } PG_FUNCTION_INFO_V1(headline); -Datum headline(PG_FUNCTION_ARGS); +Datum headline(PG_FUNCTION_ARGS); Datum -headline(PG_FUNCTION_ARGS) { - TSCfgInfo *cfg=findcfg(PG_GETARG_OID(0)); - text *in = PG_GETARG_TEXT_P(1); +headline(PG_FUNCTION_ARGS) +{ + TSCfgInfo *cfg = findcfg(PG_GETARG_OID(0)); + text *in = PG_GETARG_TEXT_P(1); QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(2))); - text *opt=( PG_NARGS()>3 && PG_GETARG_POINTER(3) ) ? PG_GETARG_TEXT_P(3) : NULL; + text *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL; HLPRSTEXT prs; - text *out; + text *out; WParserInfo *prsobj = findprs(cfg->prs_id); - memset(&prs,0,sizeof(HLPRSTEXT)); + memset(&prs, 0, sizeof(HLPRSTEXT)); prs.lenwords = 32; prs.words = (HLWORD *) palloc(sizeof(HLWORD) * prs.lenwords); hlparsetext(cfg, &prs, query, VARDATA(in), VARSIZE(in) - VARHDRSZ); FunctionCall3( - &(prsobj->headline_info), - PointerGetDatum(&prs), - PointerGetDatum(opt), - PointerGetDatum(query) - ); + &(prsobj->headline_info), + PointerGetDatum(&prs), + PointerGetDatum(opt), + PointerGetDatum(query) + ); out = genhl(&prs); - PG_FREE_IF_COPY(in,1); - PG_FREE_IF_COPY(query,2); - if ( opt ) PG_FREE_IF_COPY(opt,3); + PG_FREE_IF_COPY(in, 1); + PG_FREE_IF_COPY(query, 2); + if (opt) + PG_FREE_IF_COPY(opt, 3); pfree(prs.words); pfree(prs.startsel); pfree(prs.stopsel); @@ -495,35 +548,34 @@ headline(PG_FUNCTION_ARGS) { PG_FUNCTION_INFO_V1(headline_byname); -Datum headline_byname(PG_FUNCTION_ARGS); +Datum headline_byname(PG_FUNCTION_ARGS); Datum -headline_byname(PG_FUNCTION_ARGS) { - text *cfg=PG_GETARG_TEXT_P(0); - - Datum out=DirectFunctionCall4( - headline, - ObjectIdGetDatum(name2id_cfg( cfg ) ), - PG_GETARG_DATUM(1), - PG_GETARG_DATUM(2), - ( PG_NARGS()>3 ) ? PG_GETARG_DATUM(3) : PointerGetDatum(NULL) +headline_byname(PG_FUNCTION_ARGS) +{ + text *cfg = PG_GETARG_TEXT_P(0); + + Datum out = DirectFunctionCall4( + 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); + PG_FREE_IF_COPY(cfg, 0); + PG_RETURN_DATUM(out); } PG_FUNCTION_INFO_V1(headline_current); -Datum headline_current(PG_FUNCTION_ARGS); +Datum headline_current(PG_FUNCTION_ARGS); Datum -headline_current(PG_FUNCTION_ARGS) { +headline_current(PG_FUNCTION_ARGS) +{ PG_RETURN_DATUM(DirectFunctionCall4( - headline, - ObjectIdGetDatum(get_currcfg()), - PG_GETARG_DATUM(0), - PG_GETARG_DATUM(1), - ( PG_NARGS()>2 ) ? PG_GETARG_DATUM(2) : PointerGetDatum(NULL) - )); + headline, + ObjectIdGetDatum(get_currcfg()), + PG_GETARG_DATUM(0), + PG_GETARG_DATUM(1), + (PG_NARGS() > 2) ? PG_GETARG_DATUM(2) : PointerGetDatum(NULL) + )); } - - - diff --git a/contrib/tsearch2/wparser.h b/contrib/tsearch2/wparser.h index a8afc564a1a..c3c44151f0e 100644 --- a/contrib/tsearch2/wparser.h +++ b/contrib/tsearch2/wparser.h @@ -3,26 +3,28 @@ #include "postgres.h" #include "fmgr.h" -typedef struct { - Oid prs_id; - FmgrInfo start_info; - FmgrInfo getlexeme_info; - FmgrInfo end_info; - FmgrInfo headline_info; - Oid lextype; - void *prs; -} WParserInfo; +typedef struct +{ + Oid prs_id; + FmgrInfo start_info; + FmgrInfo getlexeme_info; + FmgrInfo end_info; + FmgrInfo headline_info; + Oid lextype; + void *prs; +} WParserInfo; -void init_prs(Oid id, WParserInfo *prs); -WParserInfo* findprs(Oid id); -Oid name2id_prs(text *name); -void reset_prs(void); +void init_prs(Oid id, WParserInfo * prs); +WParserInfo *findprs(Oid id); +Oid name2id_prs(text *name); +void reset_prs(void); -typedef struct { - int lexid; - char *alias; - char *descr; -} LexDescr; +typedef struct +{ + int lexid; + char *alias; + char *descr; +} LexDescr; #endif diff --git a/contrib/tsearch2/wparser_def.c b/contrib/tsearch2/wparser_def.c index c3b03067600..4680d746b3e 100644 --- a/contrib/tsearch2/wparser_def.c +++ b/contrib/tsearch2/wparser_def.c @@ -1,5 +1,5 @@ -/* - * default word parser +/* + * default word parser * Teodor Sigaev <teodor@sigaev.ru> */ #include <errno.h> @@ -17,40 +17,44 @@ #include "wordparser/deflex.h" PG_FUNCTION_INFO_V1(prsd_lextype); -Datum prsd_lextype(PG_FUNCTION_ARGS); +Datum prsd_lextype(PG_FUNCTION_ARGS); + +Datum +prsd_lextype(PG_FUNCTION_ARGS) +{ + LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (LASTNUM + 1)); + int i; + + for (i = 1; i <= LASTNUM; i++) + { + descr[i - 1].lexid = i; + descr[i - 1].alias = pstrdup(tok_alias[i]); + descr[i - 1].descr = pstrdup(lex_descr[i]); + } -Datum -prsd_lextype(PG_FUNCTION_ARGS) { - LexDescr *descr=(LexDescr*)palloc(sizeof(LexDescr)*(LASTNUM+1)); - int i; + descr[LASTNUM].lexid = 0; - for(i=1;i<=LASTNUM;i++) { - descr[i-1].lexid = i; - descr[i-1].alias = pstrdup(tok_alias[i]); - descr[i-1].descr = pstrdup(lex_descr[i]); - } - - descr[LASTNUM].lexid=0; - PG_RETURN_POINTER(descr); } PG_FUNCTION_INFO_V1(prsd_start); -Datum prsd_start(PG_FUNCTION_ARGS); -Datum -prsd_start(PG_FUNCTION_ARGS) { - start_parse_str( (char*)PG_GETARG_POINTER(0), PG_GETARG_INT32(1) ); +Datum prsd_start(PG_FUNCTION_ARGS); +Datum +prsd_start(PG_FUNCTION_ARGS) +{ + start_parse_str((char *) PG_GETARG_POINTER(0), PG_GETARG_INT32(1)); PG_RETURN_POINTER(NULL); } PG_FUNCTION_INFO_V1(prsd_getlexeme); -Datum prsd_getlexeme(PG_FUNCTION_ARGS); -Datum -prsd_getlexeme(PG_FUNCTION_ARGS) { +Datum prsd_getlexeme(PG_FUNCTION_ARGS); +Datum +prsd_getlexeme(PG_FUNCTION_ARGS) +{ /* ParserState *p=(ParserState*)PG_GETARG_POINTER(0); */ - char **t=(char**)PG_GETARG_POINTER(1); - int *tlen=(int*)PG_GETARG_POINTER(2); - int type=tsearch2_yylex(); + char **t = (char **) PG_GETARG_POINTER(1); + int *tlen = (int *) PG_GETARG_POINTER(2); + int type = tsearch2_yylex(); *t = token; *tlen = tokenlen; @@ -58,34 +62,39 @@ prsd_getlexeme(PG_FUNCTION_ARGS) { } PG_FUNCTION_INFO_V1(prsd_end); -Datum prsd_end(PG_FUNCTION_ARGS); -Datum -prsd_end(PG_FUNCTION_ARGS) { +Datum prsd_end(PG_FUNCTION_ARGS); +Datum +prsd_end(PG_FUNCTION_ARGS) +{ /* ParserState *p=(ParserState*)PG_GETARG_POINTER(0); */ end_parse(); PG_RETURN_VOID(); } #define LEAVETOKEN(x) ( (x)==12 ) -#define COMPLEXTOKEN(x) ( (x)==5 || (x)==15 || (x)==16 || (x)==17 ) -#define ENDPUNCTOKEN(x) ( (x)==12 ) +#define COMPLEXTOKEN(x) ( (x)==5 || (x)==15 || (x)==16 || (x)==17 ) +#define ENDPUNCTOKEN(x) ( (x)==12 ) #define IDIGNORE(x) ( (x)==13 || (x)==14 || (x)==12 || (x)==23 ) #define HLIDIGNORE(x) ( (x)==5 || (x)==13 || (x)==15 || (x)==16 || (x)==17 ) -#define NONWORDTOKEN(x) ( (x)==12 || HLIDIGNORE(x) ) +#define NONWORDTOKEN(x) ( (x)==12 || HLIDIGNORE(x) ) #define NOENDTOKEN(x) ( NONWORDTOKEN(x) || (x)==7 || (x)==8 || (x)==20 || (x)==21 || (x)==22 || IDIGNORE(x) ) -typedef struct { - HLWORD *words; - int len; -} hlCheck; +typedef struct +{ + HLWORD *words; + int len; +} hlCheck; static bool -checkcondition_HL(void *checkval, ITEM *val) { - int i; - for(i=0;i<((hlCheck*)checkval)->len;i++) { - if ( ((hlCheck*)checkval)->words[i].item==val ) +checkcondition_HL(void *checkval, ITEM * val) +{ + int i; + + for (i = 0; i < ((hlCheck *) checkval)->len; i++) + { + if (((hlCheck *) checkval)->words[i].item == val) return true; } return false; @@ -93,21 +102,28 @@ checkcondition_HL(void *checkval, ITEM *val) { static bool -hlCover(HLPRSTEXT *prs, QUERYTYPE *query, int *p, int *q) { - int i,j; - ITEM *item=GETQUERY(query); - int pos=*p; - *q=0; - *p=0x7fffffff; - - for(j=0;j<query->size;j++) { - if ( item->type != VAL ) { +hlCover(HLPRSTEXT * prs, QUERYTYPE * query, int *p, int *q) +{ + int i, + j; + ITEM *item = GETQUERY(query); + int pos = *p; + + *q = 0; + *p = 0x7fffffff; + + for (j = 0; j < query->size; j++) + { + if (item->type != VAL) + { item++; continue; } - for(i=pos;i<prs->curwords;i++) { - if ( prs->words[i].item == item ) { - if ( i>*q) + for (i = pos; i < prs->curwords; i++) + { + if (prs->words[i].item == item) + { + if (i > *q) *q = i; break; } @@ -115,32 +131,39 @@ hlCover(HLPRSTEXT *prs, QUERYTYPE *query, int *p, int *q) { item++; } - if ( *q==0 ) + if (*q == 0) return false; - item=GETQUERY(query); - for(j=0;j<query->size;j++) { - if ( item->type != VAL ) { + item = GETQUERY(query); + for (j = 0; j < query->size; j++) + { + if (item->type != VAL) + { item++; continue; } - for(i=*q;i>=pos;i--) { - if ( prs->words[i].item == item ) { - if ( i<*p ) - *p=i; + for (i = *q; i >= pos; i--) + { + if (prs->words[i].item == item) + { + if (i < *p) + *p = i; break; } } item++; - } + } + + if (*p <= *q) + { + hlCheck ch = {&(prs->words[*p]), *q - *p + 1}; - if ( *p<=*q ) { - hlCheck ch={ &(prs->words[*p]), *q-*p+1 }; - if ( TS_execute(GETQUERY(query), &ch, false, checkcondition_HL) ) { + if (TS_execute(GETQUERY(query), &ch, false, checkcondition_HL)) return true; - } else { + else + { (*p)++; - return hlCover(prs,query,p,q); + return hlCover(prs, query, p, q); } } @@ -148,45 +171,54 @@ hlCover(HLPRSTEXT *prs, QUERYTYPE *query, int *p, int *q) { } PG_FUNCTION_INFO_V1(prsd_headline); -Datum prsd_headline(PG_FUNCTION_ARGS); -Datum -prsd_headline(PG_FUNCTION_ARGS) { - HLPRSTEXT *prs=(HLPRSTEXT*)PG_GETARG_POINTER(0); - text *opt=(text*)PG_GETARG_POINTER(1); /* can't be toasted */ - QUERYTYPE *query=(QUERYTYPE*)PG_GETARG_POINTER(2); /* can't be toasted */ +Datum prsd_headline(PG_FUNCTION_ARGS); +Datum +prsd_headline(PG_FUNCTION_ARGS) +{ + HLPRSTEXT *prs = (HLPRSTEXT *) PG_GETARG_POINTER(0); + text *opt = (text *) PG_GETARG_POINTER(1); /* can't be toasted */ + QUERYTYPE *query = (QUERYTYPE *) PG_GETARG_POINTER(2); /* can't be toasted */ + /* from opt + start and and tag */ - int min_words=15; - int max_words=35; - int shortword=3; - - int p=0,q=0; - int bestb=-1,beste=-1; - int bestlen=-1; - int pose=0, poslen, curlen; - - int i; - - /*config*/ - prs->startsel=NULL; - prs->stopsel=NULL; - if ( opt ) { - Map *map,*mptr; - - parse_cfgdict(opt,&map); - mptr=map; - - while(mptr && mptr->key) { - if ( strcasecmp(mptr->key,"MaxWords")==0 ) - max_words=pg_atoi(mptr->value,4,1); - else if ( strcasecmp(mptr->key,"MinWords")==0 ) - min_words=pg_atoi(mptr->value,4,1); - else if ( strcasecmp(mptr->key,"ShortWord")==0 ) - shortword=pg_atoi(mptr->value,4,1); - else if ( strcasecmp(mptr->key,"StartSel")==0 ) - prs->startsel=pstrdup(mptr->value); - else if ( strcasecmp(mptr->key,"StopSel")==0 ) - prs->stopsel=pstrdup(mptr->value); - + int min_words = 15; + int max_words = 35; + int shortword = 3; + + int p = 0, + q = 0; + int bestb = -1, + beste = -1; + int bestlen = -1; + int pose = 0, + poslen, + curlen; + + int i; + + /* config */ + prs->startsel = NULL; + prs->stopsel = NULL; + if (opt) + { + Map *map, + *mptr; + + parse_cfgdict(opt, &map); + mptr = map; + + while (mptr && mptr->key) + { + if (strcasecmp(mptr->key, "MaxWords") == 0) + max_words = pg_atoi(mptr->value, 4, 1); + else if (strcasecmp(mptr->key, "MinWords") == 0) + min_words = pg_atoi(mptr->value, 4, 1); + else if (strcasecmp(mptr->key, "ShortWord") == 0) + shortword = pg_atoi(mptr->value, 4, 1); + else if (strcasecmp(mptr->key, "StartSel") == 0) + prs->startsel = pstrdup(mptr->value); + else if (strcasecmp(mptr->key, "StopSel") == 0) + prs->stopsel = pstrdup(mptr->value); + pfree(mptr->key); pfree(mptr->value); @@ -194,104 +226,118 @@ prsd_headline(PG_FUNCTION_ARGS) { } pfree(map); - if ( min_words >= max_words ) + if (min_words >= max_words) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("must be MinWords < MaxWords"))); - if ( min_words<=0 ) + if (min_words <= 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("must be MinWords > 0"))); - if ( shortword<0 ) + if (shortword < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("must be ShortWord >= 0"))); } - while( hlCover(prs,query,&p,&q) ) { + while (hlCover(prs, query, &p, &q)) + { /* find cover len in words */ - curlen=0; - poslen=0; - for(i=p;i<=q && curlen < max_words ; i++) { - if ( !NONWORDTOKEN(prs->words[i].type) ) + curlen = 0; + poslen = 0; + for (i = p; i <= q && curlen < max_words; i++) + { + if (!NONWORDTOKEN(prs->words[i].type)) curlen++; - if ( prs->words[i].item && !prs->words[i].repeated ) - poslen++; - pose=i; + if (prs->words[i].item && !prs->words[i].repeated) + poslen++; + pose = i; } - if ( poslen<bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword) ) { + if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)) + { /* best already finded, so try one more cover */ p++; continue; } - if ( curlen < max_words ) { /* find good end */ - for(i=i-1 ;i<prs->curwords && curlen<max_words; i++) { - if ( i!=q ) { - if ( !NONWORDTOKEN(prs->words[i].type) ) + if (curlen < max_words) + { /* find good end */ + for (i = i - 1; i < prs->curwords && curlen < max_words; i++) + { + if (i != q) + { + if (!NONWORDTOKEN(prs->words[i].type)) curlen++; - if ( prs->words[i].item && !prs->words[i].repeated ) + if (prs->words[i].item && !prs->words[i].repeated) poslen++; } - pose=i; - if ( NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword ) + pose = i; + if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword) continue; - if ( curlen>=min_words ) + if (curlen >= min_words) break; } - } else { /* shorter cover :((( */ - for(;curlen>min_words;i--) { - if ( !NONWORDTOKEN(prs->words[i].type) ) + } + else + { /* shorter cover :((( */ + for (; curlen > min_words; i--) + { + if (!NONWORDTOKEN(prs->words[i].type)) curlen--; - if ( prs->words[i].item && !prs->words[i].repeated ) + if (prs->words[i].item && !prs->words[i].repeated) poslen--; - pose=i; - if ( NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword ) + pose = i; + if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword) continue; 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) ) ) { - bestb=p; beste=pose; - bestlen=poslen; - } + 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))) + { + bestb = p; + beste = pose; + bestlen = poslen; + } p++; } - if ( bestlen<0 ) { - curlen=0; - poslen=0; - for(i=0;i<prs->curwords && curlen<min_words ; i++) { - if ( !NONWORDTOKEN(prs->words[i].type) ) + if (bestlen < 0) + { + curlen = 0; + poslen = 0; + for (i = 0; i < prs->curwords && curlen < min_words; i++) + { + if (!NONWORDTOKEN(prs->words[i].type)) curlen++; - pose=i; + pose = i; } - bestb=0; beste=pose; + bestb = 0; + beste = pose; } - for(i=bestb;i<=beste;i++) { - if ( prs->words[i].item ) - prs->words[i].selected=1; - if ( prs->words[i].repeated ) - prs->words[i].skip=1; - if ( HLIDIGNORE(prs->words[i].type) ) - prs->words[i].replace=1; + for (i = bestb; i <= beste; i++) + { + if (prs->words[i].item) + prs->words[i].selected = 1; + if (prs->words[i].repeated) + prs->words[i].skip = 1; + if (HLIDIGNORE(prs->words[i].type)) + prs->words[i].replace = 1; - prs->words[i].in=1; + prs->words[i].in = 1; } if (!prs->startsel) - prs->startsel=pstrdup("<b>"); + prs->startsel = pstrdup("<b>"); if (!prs->stopsel) - prs->stopsel=pstrdup("</b>"); - prs->startsellen=strlen(prs->startsel); - prs->stopsellen=strlen(prs->stopsel); + prs->stopsel = pstrdup("</b>"); + prs->startsellen = strlen(prs->startsel); + prs->stopsellen = strlen(prs->stopsel); PG_RETURN_POINTER(prs); } - |