comparison regexParser/threadedSearch.cc @ 298:63213964502a

refactoring ....
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Feb 2016 12:24:34 +0900
parents 948428caf616
children bdfe0a32c48f
comparison
equal deleted inserted replaced
297:c5a7caa37f61 298:63213964502a
16 if (tsv.matchEnd) { 16 if (tsv.matchEnd) {
17 fwrite(tsv.matchBegin,tsv.matchEnd-tsv.matchBegin,1,stdout); 17 fwrite(tsv.matchBegin,tsv.matchEnd-tsv.matchBegin,1,stdout);
18 puts(""); 18 puts("");
19 tsv.matchEnd = NULL; 19 tsv.matchEnd = NULL;
20 } 20 }
21 tsv.matchBegin = tsv.buff.buffptr; // next char may be matchBegin
21 return tsv; 22 return tsv;
22 } 23 }
23 24
24 static 25 static
25 TSValue stateMatch(TSValue tsv) { 26 TSValue stateMatch(TSValue tsv) {
26 tsv.matchEnd = tsv.buff.buffptr; // next char of the match 27 tsv.matchEnd = tsv.buff.buffptr; // next char of the match
27 return tsv; 28 return tsv;
28 } 29 }
29 30
30 TStatePtr generateTState(StatePtr state, TransitionGeneratorPtr tg) { 31 TStatePtr generateTState(StatePtr state, TransitionGeneratorPtr tg) {
31 TStatePtr tState = NEW(TState); 32 TStatePtr tState = NEW(TState);
36 getNext(ccw); 37 getNext(ccw);
37 ccvSize++; 38 ccvSize++;
38 } 39 }
39 tState->ccvSize = ccvSize; 40 tState->ccvSize = ccvSize;
40 if (state->accept) { 41 if (state->accept) {
41 tState->stateSkip = tg->stateSkip; 42 tState->stateMatch = tg->stateMatch;
42 tState->stateMatch = tg->stateMatch; 43 tState->stateSkip = tg->stateSkip;
43 } else { 44 } else {
44 tState->stateSkip = tg->stateSkip; 45 tState->stateMatch = tg->stateNothing;
45 tState->stateMatch = tg->stateNothing; 46 tState->stateSkip = tg->stateSkip;
46 } 47 }
47 if (ccvSize == 0) { 48 if (ccvSize == 0) {
48 tState->ccv = NULL; 49 tState->ccv = NULL;
49 state->tState = tState; 50 state->tState = tState;
50 return tState; 51 return tState;
87 TSValue tSearch(TSValue tsv) { 88 TSValue tSearch(TSValue tsv) {
88 #if DEBUG 89 #if DEBUG
89 TSValuePtr tsvp = &tsv; // make tsv visible in lldb 90 TSValuePtr tsvp = &tsv; // make tsv visible in lldb
90 #endif 91 #endif
91 next: while (tsv.buff.buffptr < tsv.buff.buffend) { 92 next: while (tsv.buff.buffptr < tsv.buff.buffend) {
93 tsv = tsv.current->stateMatch(tsv);
94 if (tsv.current->ccvSize==0) {
95 // matched start again
96 tsv.current = tsv.tg->stateStart->tState;
97 }
92 unsigned char c = *tsv.buff.buffptr++; 98 unsigned char c = *tsv.buff.buffptr++;
93 // printState(tsv.current->state); 99 // printState(tsv.current->state);
94 for (int i = 0; i < tsv.current->ccvSize; i++) { 100 for (int i = 0; i < tsv.current->ccvSize; i++) {
95 CCVPtr ccv = &tsv.current->ccv[i]; 101 CCVPtr ccv = &tsv.current->ccv[i];
96 if (c<ccv->begin) { 102 if (c<ccv->begin) {
97 tsv = tsv.current->stateSkip(tsv); 103 tsv = tsv.current->stateSkip(tsv);
98 tsv.matchBegin = tsv.buff.buffptr;
99 goto next; 104 goto next;
100 } else if (c<=ccv->end) { 105 } else if (c<=ccv->end) {
101 // range matched. 106 // range matched.
102 if (ccv->w.word) { 107 if (ccv->w.word) {
103 // match the word. 108 // match the word.
104 // if (not match) continue; 109 // if (not match) continue;
105 } 110 }
106 tsv = tsv.current->stateMatch(tsv);
107 if (ccv->tState) { 111 if (ccv->tState) {
108 tsv.current = ccv->tState; 112 tsv.current = ccv->tState;
109 } else { 113 } else {
110 tsv.current = nextTState(ccv->state,tsv.tg); 114 tsv.current = nextTState(ccv->state,tsv.tg);
111 ccv->tState = tsv.current; 115 ccv->tState = tsv.current;
112 } 116 }
113 goto next; 117 goto next;
114 } 118 }
115 } 119 }
116 tsv = tsv.current->stateSkip(tsv); 120 tsv = tsv.current->stateSkip(tsv);
117 tsv.matchBegin = tsv.buff.buffptr;
118 } 121 }
119 #if DEBUG 122 #if DEBUG
120 *tsvp = tsv; 123 *tsvp = tsv;
121 return *tsvp; 124 return *tsvp;
122 #else 125 #else
123 return tsv; 126 return tsv;
124 #endif 127 #endif
125 } 128 }
126 129
127 void threadedSearch(TransitionGeneratorPtr tg, Buffer buff) { 130 TSValue
131 createTSValue(TransitionGeneratorPtr tg, Buffer buff) {
128 TSValue tsv; 132 TSValue tsv;
133 if (!tg) {
134 tg = createTransitionGenerator();
135 }
129 tsv.buff = buff; 136 tsv.buff = buff;
130 tsv.tg = tg; 137 tsv.tg = tg;
131 tsv.blk = NULL; 138 tsv.blk = NULL;
139 tsv.matchBegin = buff.buffptr;
140 tsv.matchEnd = NULL;
132 tsv.tg->stateSkip = stateSkip; 141 tsv.tg->stateSkip = stateSkip;
133 tsv.tg->stateMatch = stateMatch; 142 tsv.tg->stateMatch = stateMatch;
134 tsv.tg->stateNothing = stateNothing; 143 tsv.tg->stateNothing = stateNothing;
135 tsv.matchBegin = buff.buffptr; 144 return tsv;
136 tsv.matchEnd = NULL; 145 }
146
147
148 void threadedSearch(TransitionGeneratorPtr tg, Buffer buff) {
149 TSValue tsv = createTSValue(tg,buff);
137 tsv.current = generateTState(tg->stateList,tg); 150 tsv.current = generateTState(tg->stateList,tg);
138 tg->stateStart = NEW(State); 151 tg->stateStart = NEW(State);
139 *tg->stateStart = *tg->stateList; 152 *tg->stateStart = *tg->stateList;
140 tg->stateStart->accept = false; // Start state never accept 153 tg->stateStart->accept = false; // Start state never accept
141 generateTState(tg->stateStart,tg); 154 generateTState(tg->stateStart,tg);
142
143 tSearch(tsv); 155 tSearch(tsv);
144 } 156 }
157
158 /* end */