view regexParser/grepWalk.cc @ 295:0c663f46954d

todo
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 02 Feb 2016 11:24:29 +0900
parents 948428caf616
children bdfe0a32c48f
line wrap: on
line source

#include <stdio.h>

#include "grepWalk.h"
#include "subsetConstruction.h"

void grep(TransitionGeneratorPtr tg,unsigned char *matchBegin,Buffer buff,unsigned long d) ;

void grepSkip(TransitionGeneratorPtr tg,unsigned char *matchBegin, Buffer buff) {
    matchBegin = buff.buffptr;
    grep(tg,matchBegin,buff,1); // 1 is initState
}

void grepWalk(TransitionGeneratorPtr tg, unsigned char *matchBegin, Buffer buff) {
    grepSkip(tg,matchBegin,buff);
}

void grepMatch(TransitionGeneratorPtr tg,unsigned char *matchBegin, Buffer buff) {
    fwrite(matchBegin,buff.buffptr-matchBegin,1,stdout);
    puts("\n");
    grepSkip(tg,matchBegin,buff);
}

void grep(TransitionGeneratorPtr tg,unsigned char *matchBegin,Buffer buff,unsigned long d) {
    unsigned char c = *buff.buffptr++;
    if (c=='\0') return;
    StatePtr state = tg->stateList;

    while (state->bitState.bitContainer != d) state = state->next; // 配列へのアクセスへ変更
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    CharClassPtr cc = NULL;
    bool found = false;
    while (hasNext(ccw)) {
        cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        if (begin == end) {
            if (c == begin) found = true;
            else found = false;
        } else {
            if (c < begin) found = false;
            else if (c < end) found = true;
        }
        if (found == true) break;
    }

    if (found == false) {
        grepSkip(tg,matchBegin,buff);
    } else if (found == true && (cc->nextState.bitContainer | 2)) { // Accept
        grepMatch(tg,matchBegin,buff);
    } else {
        grep(tg,matchBegin,buff,cc->nextState.bitContainer);
    }
}