view regexParser/threadedSearch.cc @ 257:ebb429c2b6a7

fix allocate state in generateTransition
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Mon, 25 Jan 2016 19:20:32 +0900
parents 21b9ba76f91b
children 29e467a491ba
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include "regexParser.h"
#include "subsetConstruction.h"

void stateSkip(TSValue tsv) {
    tsv.buff.matchBegin = tsv.buff.buffptr;
    tsv.current->stateSkip(tsv);
}

void stateMatch(TSValue tsv) {
    tsv.buff.matchBegin = tsv.buff.buffptr;
    tsv.current->stateSkip(tsv);
}

TStatePtr generateTState(StatePtr state) {
    TStatePtr tState = NEW(TState);
    int ccvSize = 0;
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    while (hasNext(ccw)) {
        ccvSize++;
    }
    if (ccvSize == 0) return tState;
    else tState->ccv = (ccv*)malloc(sizeof(ccv)*ccvSize);
    ccw = createCharClassWalker(state->cc);
    int i = 0;
    while (hasNext(ccw)) {
        CharClassPtr cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        struct ccv *ccv = &tState->ccv[i++];
        ccv->begin = begin;
        ccv->end = end;
        ccv->tState = NULL;
        ccv->state = cc->nextState;
        ccv->w = cc->cond.w;
    }
    free(ccw);
    return tState;
}

void tSearch(TSValue tsv) {
    next: while (tsv.buff.buffptr < tsv.buff.buffend) {
        unsigned char c = *tsv.buff.buffptr++;
        for (int i = 0; i < tsv.current->ccvSize; i++) {
            CCVPtr ccv = &tsv.current->ccv[i];
            if (c<ccv->begin) {
                tsv.stateSkip(tsv);
                goto next;
            } else if (c<=ccv->end) {
                // range matched.
                if (ccv->w.word) {
                    // match the word.
                    // if (not match) continue;
                }
                TStatePtr current = ccv->tState;
                if (current == NULL) {
                    // create tSearch in next state.
                    StatePtr state = tsv.tg->stateArray[ccv->state.bitContainer];
                    if (state == NULL) {
                        // on the fly subset construction.
                        state = createState(tsv.tg,state->bitState);
                        tsv.tg->stateArray[state->bitState.bitContainer] = state;
                        determinize(state,tsv.tg);
                    }
                    if (state->tState == NULL) {
                        current = generateTState(state);
                        ccv->tState = current;
                    } else {
                        ccv->tState = state->tState;
                    }
                }
                tsv.current = ccv->tState;
                if (tsv.current->state->bitState.bitContainer & 2) {
                    tsv.stateMatch();
                }
                goto next;
            }
        }
        tsv.current->stateSkip(tsv);
    }
}