14 #define REG_EX_DIGIT (char)(0xef)
15 #define REG_EX_DIGIT_NOT (char)(0xf0)
16 #define REG_EX_DOT (char)(0xf1)
17 #define REG_EX_END (char)(0xf2)
18 #define REG_EX_GROUP (char)(0xf3)
19 #define REG_EX_OK (char)(0xf4)
20 #define REG_EX_OR (char)(0xf5)
21 #define REG_EX_OR_END (char)(0xf6)
22 #define REG_EX_OR_FAST (char)(0xf7)
23 #define REG_EX_OR_NOT (char)(0xf8)
24 #define REG_EX_RANGE (char)(0xf9)
25 #define REG_EX_RETURN (char)(0xda)
26 #define REG_EX_SPACE (char)(0xfb)
27 #define REG_EX_SPACE_NOT (char)(0xfc)
28 #define REG_EX_START (char)(0xfd)
29 #define REG_EX_WORD (char)(0xfe)
30 #define REG_EX_WORD_NOT (char)(0xff)
32 #define REG_EX_FLAG_OR 0x8000
33 #define REG_EX_LINK_MASK 0x7fff
38 #define REG_EX_CREATE(T,S,C) RegEx_Create( (T), (S), (C), sizeof(S) / sizeof(S[0]) )
40 #define REG_EX_STATE(C,I,A) { (C) , (I), (A), 0 }
41 #define REG_EX_STATE_DIGIT(I,A) { REG_EX_DIGIT , (I), (A), 0 }
42 #define REG_EX_STATE_DIGIT_NOT(I,A) { REG_EX_DIGIT_NOT, (I), (A), 0 }
43 #define REG_EX_STATE_DOT(I,A) { REG_EX_DOT , (I), (A), 0 }
44 #define REG_EX_STATE_END { REG_EX_END , 0, 0, 0 }
45 #define REG_EX_STATE_GROUP(I,A,L) { REG_EX_GROUP , (I), (A), (L) }
46 #define REG_EX_STATE_OK { REG_EX_OK , 0, 0, 0 }
47 #define REG_EX_STATE_OR(I,A,L) { REG_EX_OR , (I), (A), (L) }
48 #define REG_EX_STATE_OR_END { REG_EX_OR_END , 0, 0, 0 }
49 #define REG_EX_STATE_OR_FAST(I,A,L) { REG_EX_OR_FAST , (I), (A), (L) }
50 #define REG_EX_STATE_OR_NOT(I,A,L) { REG_EX_OR_NOT , (I), (A), (L) }
51 #define REG_EX_STATE_RANGE(B,E) { REG_EX_RANGE , (B), (E), 0 }
52 #define REG_EX_STATE_RETURN(L) { REG_EX_RETURN , 0, 0, (L) }
53 #define REG_EX_STATE_SPACE(I,A) { REG_EX_SPACE , (I), (A), 0 }
54 #define REG_EX_STATE_SPACE_NOT(I,A) { REG_EX_SPACE_NOT, (I), (A), 0 }
55 #define REG_EX_STATE_START { REG_EX_START , 0, 0, 0 }
56 #define REG_EX_STATE_WORD(I,A) { REG_EX_WORD , (I), (A), 0 }
57 #define REG_EX_STATE_WORD_NOT(I,A) { REG_EX_WORD_NOT , (I), (A), 0 }
79 unsigned short mFlagAndLink;
95 unsigned char * mCounters;
98 unsigned short mStateCount;
100 unsigned short mThreads [15];
101 unsigned char mThreadCount ;
102 unsigned char mThreadCurrent;
104 unsigned char mRunning;
109 #ifndef _OPEN_NET_NO_FUNCTION_
114 int RegEx_IsCharValid(
char aInput)
116 return (((9 <= aInput) && ( 10 >= aInput))
118 || ((32 <= aInput) && (126 >= aInput)));
121 unsigned short RegEx_StateIndex_Get(
RegEx * aThis)
123 return aThis->mThreads[aThis->mThreadCurrent];
126 void RegEx_StateIndex_Set(
RegEx * aThis,
unsigned short aState)
128 aThis->mThreads[aThis->mThreadCurrent] = aState;
133 unsigned short RegEx_Link_Get(
RegEx * aThis)
135 return (aThis->mStates[RegEx_StateIndex_Get(aThis)].mFlagAndLink & REG_EX_LINK_MASK);
138 void RegEx_Thread_Create(
RegEx * aThis,
unsigned short aState)
140 aThis->mThreads[aThis->mThreadCount] = aState;
141 aThis->mThreadCount++;
144 void RegEx_Thread_Delete(
RegEx * aThis)
146 aThis->mCounters[RegEx_StateIndex_Get(aThis)] = 0;
147 aThis->mThreadCount--;
149 for (
unsigned int i = aThis->mThreadCurrent; i < aThis->mThreadCount; i++)
151 aThis->mThreads[i] = aThis->mThreads[i + 1];
154 aThis->mThreadCurrent--;
159 void RegEx_Or_Handle(
RegEx * aThis)
161 while (0 != (aThis->mStates[aThis->mThreads[aThis->mThreadCurrent]].mFlagAndLink & REG_EX_FLAG_OR))
163 RegEx_Thread_Create(aThis, aThis->mThreads[aThis->mThreadCurrent]);
164 aThis->mThreads[aThis->mThreadCurrent]++;
168 void RegEx_Reset(
RegEx * aThis)
170 aThis->mThreadCount = 0;
171 aThis->mThreadCurrent = 0;
173 for (
unsigned int i = 0; i < aThis->mStateCount; i++)
175 aThis->mCounters[i] = 0;
178 RegEx_Thread_Create(aThis, 0);
180 RegEx_Or_Handle(aThis);
183 void RegEx_StateIndex_Next(
RegEx * aThis)
185 aThis->mCounters[RegEx_StateIndex_Get(aThis)] = 0;
187 if (0 != (aThis->mStates[aThis->mThreads[aThis->mThreadCurrent]].mFlagAndLink & REG_EX_FLAG_OR))
189 aThis->mThreads[aThis->mThreadCurrent]++;
192 aThis->mThreads[aThis->mThreadCurrent]++;
194 RegEx_Or_Handle(aThis);
199 void RegEx_Counter_Inc(
RegEx * aThis)
201 unsigned short lState = RegEx_StateIndex_Get(aThis);
203 aThis->mCounters[lState]++;
204 if (aThis->mStates[lState].mMax <= aThis->mCounters[lState])
206 RegEx_StateIndex_Next(aThis);
210 int RegEx_Repeat_Min(
RegEx * aThis)
212 unsigned short lState = RegEx_StateIndex_Get(aThis);
214 if (aThis->mStates[lState].mMin <= aThis->mCounters[lState])
216 RegEx_StateIndex_Next(aThis);
220 RegEx_Thread_Delete(aThis);
224 void RegEx_Start(
RegEx * aThis)
228 if (REG_EX_START == aThis->mStates[RegEx_StateIndex_Get(aThis)].mC)
230 RegEx_StateIndex_Next(aThis);
238 void RegEx_OK(
RegEx * aThis)
240 while (0 < aThis->mThreadCount)
242 aThis->mThreadCurrent = 0;
244 RegEx_Thread_Delete(aThis);
252 int RegEx_Execute_C(
RegEx * aThis,
char aInput)
254 if (aThis->mStates[RegEx_StateIndex_Get(aThis)].mC == aInput)
256 RegEx_Counter_Inc(aThis);
260 return RegEx_Repeat_Min(aThis);
263 int RegEx_Execute_Digit(
RegEx * aThis,
char aInput)
265 if ((
'0' <= aInput) && (
'9' >= aInput))
267 RegEx_Counter_Inc(aThis);
271 return RegEx_Repeat_Min(aThis);
274 int RegEx_Execute_Digit_Not(
RegEx * aThis,
char aInput)
276 if ((
'0' > aInput) || (
'9' < aInput))
278 RegEx_Counter_Inc(aThis);
282 return RegEx_Repeat_Min(aThis);
285 void RegEx_Execute_Group(
RegEx * aThis)
287 unsigned short lState = RegEx_StateIndex_Get(aThis);
289 if (aThis->mStates[lState].mMin <= aThis->mCounters[lState])
291 RegEx_Thread_Create(aThis, lState + 1);
294 RegEx_StateIndex_Set(aThis, RegEx_Link_Get(aThis));
296 RegEx_Or_Handle(aThis);
299 void RegEx_Execute_Or(
RegEx * aThis)
301 unsigned short lState = RegEx_StateIndex_Get(aThis);
303 if (aThis->mStates[lState].mMin <= aThis->mCounters[lState])
305 RegEx_Thread_Create(aThis, lState + 1);
308 unsigned short lLink = RegEx_Link_Get(aThis);
310 RegEx_StateIndex_Set(aThis, lLink);
316 if (REG_EX_OR_END == aThis->mStates[lLink].mC)
321 RegEx_Thread_Create(aThis, lLink);
325 int RegEx_Execute_Or_Fast(
RegEx * aThis,
char aInput)
327 unsigned short lLink = RegEx_Link_Get(aThis);
331 switch (aThis->mStates[lLink].mC)
334 return RegEx_Repeat_Min(aThis);
337 if ((aThis->mStates[lLink].mMin <= aInput) && (aThis->mStates[lLink].mMax >= aInput))
339 RegEx_Counter_Inc(aThis);
345 if (aThis->mStates[lLink].mC == aInput)
347 RegEx_Counter_Inc(aThis);
356 int RegEx_Execute_Or_Not(
RegEx * aThis,
char aInput)
358 unsigned short lLink = RegEx_Link_Get(aThis);
362 switch (aThis->mStates[lLink].mC)
365 RegEx_Counter_Inc(aThis);
369 if ((aThis->mStates[lLink].mMin <= aInput) && (aThis->mStates[lLink].mMax >= aInput))
371 return RegEx_Repeat_Min(aThis);
376 if (aThis->mStates[lLink].mC == aInput)
378 return RegEx_Repeat_Min(aThis);
386 void RegEx_Execute_Range(
RegEx * aThis,
char aInput)
388 unsigned short lState = RegEx_StateIndex_Get(aThis);
390 if ((aThis->mStates[lState].mMin <= aInput) && (aThis->mStates[lState].mMax >= aInput))
392 RegEx_StateIndex_Next(aThis);
396 RegEx_Thread_Delete(aThis);
400 void RegEx_Execute_Return(
RegEx * aThis)
402 unsigned short lLink = RegEx_Link_Get(aThis);
404 RegEx_StateIndex_Set(aThis, lLink);
406 RegEx_Counter_Inc(aThis);
409 int RegEx_Execute_Space(
RegEx * aThis,
char aInput)
417 RegEx_Counter_Inc(aThis);
421 return RegEx_Repeat_Min(aThis);
424 int RegEx_Execute_Space_Not(
RegEx * aThis,
char aInput)
432 return RegEx_Repeat_Min(aThis);
435 RegEx_Counter_Inc(aThis);
439 int RegEx_Execute_Word(
RegEx * aThis,
char aInput)
441 if ( ((
'0' <= aInput) && (
'9' >= aInput))
442 || ((
'a' <= aInput) && (
'z' >= aInput))
443 || ((
'A' <= aInput) && (
'Z' >= aInput))
446 RegEx_Counter_Inc(aThis);
450 return RegEx_Repeat_Min(aThis);
453 int RegEx_Execute_Word_Not(
RegEx * aThis,
char aInput)
455 if ( ((
'0' <= aInput) && (
'9' >= aInput))
456 || ((
'a' <= aInput) && (
'z' >= aInput))
457 || ((
'A' <= aInput) && (
'Z' >= aInput))
460 return RegEx_Repeat_Min(aThis);
463 RegEx_Counter_Inc(aThis);
470 void RegEx_Create(
RegEx * aThis, OPEN_NET_CONSTANT
RegEx_State * aStates,
unsigned char * aCounters,
unsigned int aCount)
472 aThis->mCounters = aCounters;
473 aThis->mStateCount = aCount ;
474 aThis->mStates = aStates ;
479 int RegEx_End(
RegEx * aThis)
481 while (0 < aThis->mThreadCount)
483 aThis->mThreadCurrent = 0;
485 switch (aThis->mStates[RegEx_StateIndex_Get(aThis)].mC)
487 case REG_EX_DIGIT_NOT:
490 case REG_EX_SPACE_NOT:
491 case REG_EX_WORD_NOT :
492 RegEx_Repeat_Min(aThis);
500 case REG_EX_GROUP : RegEx_Execute_Group (aThis);
break;
501 case REG_EX_OR : RegEx_Execute_Or (aThis);
break;
502 case REG_EX_RETURN: RegEx_Execute_Return(aThis);
break;
504 case REG_EX_DIGIT : RegEx_Execute_Digit (aThis, REG_EX_END);
break;
505 case REG_EX_OR_FAST: RegEx_Execute_Or_Fast(aThis, REG_EX_END);
break;
506 case REG_EX_SPACE : RegEx_Execute_Space (aThis, REG_EX_END);
break;
507 case REG_EX_WORD : RegEx_Execute_Word (aThis, REG_EX_END);
break;
508 default : RegEx_Execute_C (aThis, REG_EX_END);
break;
516 int RegEx_Execute(
RegEx * aThis,
char aInput)
518 if (RegEx_IsCharValid(aInput))
521 aThis->mThreadCurrent = 0;
523 while (aThis->mThreadCurrent < aThis->mThreadCount)
529 switch (aThis->mStates[RegEx_StateIndex_Get(aThis)].mC)
531 case REG_EX_DIGIT : lContinue = RegEx_Execute_Digit (aThis, aInput);
break;
532 case REG_EX_DIGIT_NOT: lContinue = RegEx_Execute_Digit_Not(aThis, aInput);
break;
533 case REG_EX_OR_FAST : lContinue = RegEx_Execute_Or_Fast (aThis, aInput);
break;
534 case REG_EX_OR_NOT : lContinue = RegEx_Execute_Or_Not (aThis, aInput);
break;
535 case REG_EX_SPACE : lContinue = RegEx_Execute_Space (aThis, aInput);
break;
536 case REG_EX_SPACE_NOT: lContinue = RegEx_Execute_Space_Not(aThis, aInput);
break;
537 case REG_EX_WORD : lContinue = RegEx_Execute_Word (aThis, aInput);
break;
538 case REG_EX_WORD_NOT : lContinue = RegEx_Execute_Word_Not (aThis, aInput);
break;
540 case REG_EX_DOT : lContinue = 0; RegEx_Counter_Inc (aThis);
break;
541 case REG_EX_END : lContinue = 0; RegEx_Thread_Delete (aThis);
break;
542 case REG_EX_GROUP : lContinue = 1; RegEx_Execute_Group (aThis);
break;
543 case REG_EX_OR : lContinue = 1; RegEx_Execute_Or (aThis);
break;
544 case REG_EX_RETURN: lContinue = 1; RegEx_Execute_Return(aThis);
break;
546 case REG_EX_OK: RegEx_OK(aThis);
return 1;
548 case REG_EX_RANGE: lContinue = 0; RegEx_Execute_Range(aThis, aInput);
break;
550 default: lContinue = RegEx_Execute_C(aThis, aInput);
break;
555 aThis->mThreadCurrent++;
558 if (0 == aThis->mThreadCount)
567 return RegEx_End(aThis);
574 #endif // ! _OPEN_NET_NO_FUNCTION_
État de la machine à états pour une expression reguliere
Definition: RegEx.h:72
Contexte de la machine à états pour une expression reguliere.
Definition: RegEx.h:90