public static int Count(string pattern, string source, SearchAlgorithm sa) { int id = (int)sa; List <int> res = new List <int>(); if (id == 0) { res = ApostolicoCrochemore.Search(pattern, source); preProcessTime = ApostolicoCrochemore.preProcessTime; searchTime = ApostolicoCrochemore.searchTime; return(res.Count); } else if (id == 1) { res = ApostolicoGiancarlo.Search(pattern, source); preProcessTime = ApostolicoGiancarlo.preProcessTime; searchTime = ApostolicoGiancarlo.searchTime; return(res.Count); } else if (id == 2) { res = BackwardNondeterministicDawgMatching.Search(pattern, source); preProcessTime = BackwardNondeterministicDawgMatching.preProcessTime; searchTime = BackwardNondeterministicDawgMatching.searchTime; return(res.Count); } else if (id == 3) { res = BackwardOracleMatching.Search(pattern, source); preProcessTime = BackwardOracleMatching.preProcessTime; searchTime = BackwardOracleMatching.searchTime; return(res.Count); } else if (id == 4) { res = BerryRavindran.Search(pattern, source); preProcessTime = BerryRavindran.preProcessTime; searchTime = BerryRavindran.searchTime; return(res.Count); } else if (id == 5) { res = BoyerMoore.Search(pattern, source); preProcessTime = BoyerMoore.preProcessTime; searchTime = BoyerMoore.searchTime; return(res.Count); } else if (id == 6) { res = BruteForce.Search(pattern, source); preProcessTime = BruteForce.preProcessTime; searchTime = BruteForce.searchTime; return(res.Count); } else if (id == 7) { res = Colussi.Search(pattern, source); preProcessTime = Colussi.preProcessTime; searchTime = Colussi.searchTime; return(res.Count); } else if (id == 8) { res = DeterministicFiniteAutomaton.Search(pattern, source); preProcessTime = DeterministicFiniteAutomaton.preProcessTime; searchTime = DeterministicFiniteAutomaton.searchTime; return(res.Count); } else if (id == 9) { res = ForwardDawgMatching.Search(pattern, source); preProcessTime = ForwardDawgMatching.preProcessTime; searchTime = ForwardDawgMatching.searchTime; return(res.Count); } else if (id == 10) { res = GalilGiancarlo.Search(pattern, source); preProcessTime = GalilGiancarlo.preProcessTime; searchTime = GalilGiancarlo.searchTime; return(res.Count); } else if (id == 11) { res = Horspool.Search(pattern, source); preProcessTime = Horspool.preProcessTime; searchTime = Horspool.searchTime; return(res.Count); } else if (id == 12) { res = KarpRabin.Search(pattern, source); preProcessTime = KarpRabin.preProcessTime; searchTime = KarpRabin.searchTime; return(res.Count); } else if (id == 13) { res = KMPSkipSearch.Search(pattern, source); preProcessTime = KMPSkipSearch.preProcessTime; searchTime = KMPSkipSearch.searchTime; return(res.Count); } else if (id == 14) { res = KnuthMorrisPratt.Search(pattern, source); preProcessTime = KnuthMorrisPratt.preProcessTime; searchTime = KnuthMorrisPratt.searchTime; return(res.Count); } else if (id == 15) { res = MaximalShift.Search(pattern, source); preProcessTime = MaximalShift.preProcessTime; searchTime = MaximalShift.searchTime; return(res.Count); } else if (id == 16) { res = MorrisPratt.Search(pattern, source); preProcessTime = MorrisPratt.preProcessTime; searchTime = MorrisPratt.searchTime; return(res.Count); } else if (id == 17) { res = NotSoNaive.Search(pattern, source); preProcessTime = NotSoNaive.preProcessTime; searchTime = NotSoNaive.searchTime; return(res.Count); } else if (id == 18) { res = OptimalMismatch.Search(pattern, source); preProcessTime = OptimalMismatch.preProcessTime; searchTime = OptimalMismatch.searchTime; return(res.Count); } else if (id == 19) { res = QuickSearch.Search(pattern, source); preProcessTime = QuickSearch.preProcessTime; searchTime = QuickSearch.searchTime; return(res.Count); } else if (id == 20) { res = Raita.Search(pattern, source); preProcessTime = Raita.preProcessTime; searchTime = Raita.searchTime; return(res.Count); } else if (id == 21) { res = ReverseColussi.Search(pattern, source); preProcessTime = ReverseColussi.preProcessTime; searchTime = ReverseColussi.searchTime; return(res.Count); } else if (id == 22) { res = ReverseFactor.Search(pattern, source); preProcessTime = ReverseFactor.preProcessTime; searchTime = ReverseFactor.searchTime; return(res.Count); } else if (id == 23) { res = ShiftOr.Search(pattern, source); preProcessTime = ShiftOr.preProcessTime; searchTime = ShiftOr.searchTime; return(res.Count); } else if (id == 24) { res = Simon.Search(pattern, source); preProcessTime = Simon.preProcessTime; searchTime = Simon.searchTime; return(res.Count); } else if (id == 25) { res = SkipSearch.Search(pattern, source); preProcessTime = SkipSearch.preProcessTime; searchTime = SkipSearch.searchTime; return(res.Count); } else if (id == 26) { res = Smith.Search(pattern, source); preProcessTime = Smith.preProcessTime; searchTime = Smith.searchTime; return(res.Count); } else if (id == 27) { res = StringMatchingonOrderedAlphabets.Search(pattern, source); preProcessTime = StringMatchingonOrderedAlphabets.preProcessTime; searchTime = StringMatchingonOrderedAlphabets.searchTime; return(res.Count); } else if (id == 28) { res = TunedBoyerMoore.Search(pattern, source); preProcessTime = TunedBoyerMoore.preProcessTime; searchTime = TunedBoyerMoore.searchTime; return(res.Count); } else if (id == 29) { res = TurboBM.Search(pattern, source); preProcessTime = TurboBM.preProcessTime; searchTime = TurboBM.searchTime; return(res.Count); } else if (id == 30) { res = TurboReverseFactor.Search(pattern, source); preProcessTime = TurboReverseFactor.preProcessTime; searchTime = TurboReverseFactor.searchTime; return(res.Count); } else if (id == 31) { res = TwoWay.Search(pattern, source); preProcessTime = TwoWay.preProcessTime; searchTime = TwoWay.searchTime; return(res.Count); } else if (id == 32) { res = ZhuTakaoka.Search(pattern, source); preProcessTime = ZhuTakaoka.preProcessTime; searchTime = ZhuTakaoka.searchTime; return(res.Count); } else if (id == 33) { res = NET_IndexOf.Search(pattern, source); preProcessTime = NET_IndexOf.preProcessTime; searchTime = NET_IndexOf.searchTime; return(res.Count); } else if (id == 34) { res = NET_IndexOf_Ordinal.Search(pattern, source); preProcessTime = NET_IndexOf_Ordinal.preProcessTime; searchTime = NET_IndexOf_Ordinal.searchTime; return(res.Count); } else { return(0); } }
private bool Eval(Mode mode, ref int ref_ptr, int pc) { int ref_ptr2 = ref_ptr; while (true) { ushort num = program[pc]; OpCode opCode = (OpCode)(num & 0xFF); OpFlags opFlags = (OpFlags)(num & 0xFF00); int num13; int num12; bool flag; int num15; int num11; RepeatContext repeatContext; int start; int count; int count2; switch (opCode) { default: continue; case OpCode.Anchor: { num13 = program[pc + 1]; num12 = program[pc + 2]; flag = ((opFlags & OpFlags.RightToLeft) != OpFlags.None); num11 = ((!flag) ? (ref_ptr2 + num12) : (ref_ptr2 - num12)); num15 = text_end - match_min + num12; int num16 = 0; OpCode opCode3 = (OpCode)(program[pc + 3] & 0xFF); if (opCode3 == OpCode.Position && num13 == 6) { switch (program[pc + 4]) { case 2: break; case 3: goto IL_0165; case 4: goto IL_0234; default: goto end_IL_0028; } if (flag || num12 == 0) { if (flag) { ref_ptr2 = num12; } if (TryMatch(ref ref_ptr2, pc + num13)) { goto case OpCode.True; } break; } break; } if (qs != null || (opCode3 == OpCode.String && num13 == 6 + program[pc + 4])) { bool flag4 = (ushort)(program[pc + 3] & 0x400) != 0; if (qs == null) { bool ignore = (ushort)(program[pc + 3] & 0x200) != 0; string @string = GetString(pc + 3); qs = new QuickSearch(@string, ignore, flag4); } while ((flag && num11 >= num16) || (!flag && num11 <= num15)) { if (flag4) { num11 = qs.Search(text, num11, num16); if (num11 != -1) { num11 += qs.Length; } } else { num11 = qs.Search(text, num11, num15); } if (num11 < 0) { break; } ref_ptr2 = ((!flag4) ? (num11 - num12) : (num11 + num12)); if (TryMatch(ref ref_ptr2, pc + num13)) { goto case OpCode.True; } num11 = ((!flag4) ? (num11 + 1) : (num11 - 2)); } break; } if (opCode3 == OpCode.True) { while ((flag && num11 >= num16) || (!flag && num11 <= num15)) { ref_ptr2 = num11; if (TryMatch(ref ref_ptr2, pc + num13)) { goto case OpCode.True; } num11 = ((!flag) ? (num11 + 1) : (num11 - 1)); } break; } for (; (flag && num11 >= num16) || (!flag && num11 <= num15); num11 = ((!flag) ? (num11 + 1) : (num11 - 1))) { ref_ptr2 = num11; if (!Eval(Mode.Match, ref ref_ptr2, pc + 3)) { continue; } ref_ptr2 = ((!flag) ? (num11 - num12) : (num11 + num12)); if (!TryMatch(ref ref_ptr2, pc + num13)) { continue; } goto case OpCode.True; } break; } case OpCode.Position: if (!IsPosition((Position)program[pc + 1], ref_ptr2)) { break; } pc += 2; continue; case OpCode.String: { bool flag5 = (opFlags & OpFlags.RightToLeft) != OpFlags.None; bool flag6 = (opFlags & OpFlags.IgnoreCase) != OpFlags.None; int num17 = program[pc + 1]; if (flag5) { ref_ptr2 -= num17; if (ref_ptr2 < 0) { break; } } else if (ref_ptr2 + num17 > text_end) { break; } pc += 2; for (int k = 0; k < num17; k++) { char c = text[ref_ptr2 + k]; if (flag6) { c = char.ToLower(c); } if (c != program[pc++]) { goto end_IL_0028; } } if (!flag5) { ref_ptr2 += num17; } continue; } case OpCode.Reference: { bool flag2 = (opFlags & OpFlags.RightToLeft) != OpFlags.None; bool flag3 = (opFlags & OpFlags.IgnoreCase) != OpFlags.None; int lastDefined2 = GetLastDefined(program[pc + 1]); if (lastDefined2 < 0) { break; } int index = marks[lastDefined2].Index; int length = marks[lastDefined2].Length; if (flag2) { ref_ptr2 -= length; if (ref_ptr2 < 0) { break; } } else if (ref_ptr2 + length > text_end) { break; } pc += 2; if (flag3) { for (int i = 0; i < length; i++) { if (char.ToLower(text[ref_ptr2 + i]) != char.ToLower(text[index + i])) { goto end_IL_0028; } } } else { for (int j = 0; j < length; j++) { if (text[ref_ptr2 + j] != text[index + j]) { goto end_IL_0028; } } } if (!flag2) { ref_ptr2 += length; } continue; } case OpCode.Character: case OpCode.Category: case OpCode.NotCategory: case OpCode.Range: case OpCode.Set: if (!EvalChar(mode, ref ref_ptr2, ref pc, multi: false)) { break; } continue; case OpCode.In: { int num10 = pc + program[pc + 1]; pc += 2; if (!EvalChar(mode, ref ref_ptr2, ref pc, multi: true)) { break; } pc = num10; continue; } case OpCode.Open: Open(program[pc + 1], ref_ptr2); pc += 2; continue; case OpCode.Close: Close(program[pc + 1], ref_ptr2); pc += 2; continue; case OpCode.BalanceStart: { int ptr = ref_ptr2; if (!Eval(Mode.Match, ref ref_ptr2, pc + 5) || !Balance(program[pc + 1], program[pc + 2], (program[pc + 3] == 1) ? true : false, ptr)) { break; } pc += program[pc + 4]; continue; } case OpCode.IfDefined: { int lastDefined = GetLastDefined(program[pc + 2]); pc = ((lastDefined >= 0) ? (pc + 3) : (pc + program[pc + 1])); continue; } case OpCode.Sub: if (!Eval(Mode.Match, ref ref_ptr2, pc + 2)) { break; } pc += program[pc + 1]; continue; case OpCode.Test: { int cp4 = Checkpoint(); int ref_ptr3 = ref_ptr2; if (Eval(Mode.Match, ref ref_ptr3, pc + 3)) { pc += program[pc + 1]; continue; } Backtrack(cp4); pc += program[pc + 2]; continue; } case OpCode.Branch: while (true) { int cp2 = Checkpoint(); if (Eval(Mode.Match, ref ref_ptr2, pc + 2)) { break; } Backtrack(cp2); pc += program[pc + 1]; if ((ushort)(program[pc] & 0xFF) == 0) { goto end_IL_0028; } } goto case OpCode.True; case OpCode.Jump: pc += program[pc + 1]; continue; case OpCode.Repeat: repeat = new RepeatContext(repeat, ReadProgramCount(pc + 2), ReadProgramCount(pc + 4), (opFlags & OpFlags.Lazy) != OpFlags.None, pc + 6); if (Eval(Mode.Match, ref ref_ptr2, pc + program[pc + 1])) { goto case OpCode.True; } repeat = repeat.Previous; break; case OpCode.Until: repeatContext = repeat; if (deep != repeatContext) { start = repeatContext.Start; count = repeatContext.Count; while (!repeatContext.IsMinimum) { repeatContext.Count++; repeatContext.Start = ref_ptr2; deep = repeatContext; if (!Eval(Mode.Match, ref ref_ptr2, repeatContext.Expression)) { goto IL_09bc; } if (deep == repeatContext) { continue; } goto case OpCode.True; } if (ref_ptr2 == repeatContext.Start) { repeat = repeatContext.Previous; deep = null; if (!Eval(Mode.Match, ref ref_ptr2, pc + 1)) { repeat = repeatContext; break; } } else if (repeatContext.IsLazy) { while (true) { repeat = repeatContext.Previous; deep = null; int cp3 = Checkpoint(); if (Eval(Mode.Match, ref ref_ptr2, pc + 1)) { break; } Backtrack(cp3); repeat = repeatContext; if (repeatContext.IsMaximum) { goto end_IL_0028; } repeatContext.Count++; repeatContext.Start = ref_ptr2; deep = repeatContext; if (!Eval(Mode.Match, ref ref_ptr2, repeatContext.Expression)) { repeatContext.Start = start; repeatContext.Count = count; goto end_IL_0028; } if (deep != repeatContext) { break; } if (ref_ptr2 == repeatContext.Start) { goto end_IL_0028; } } } else { count2 = stack.Count; while (true) { if (!repeatContext.IsMaximum) { int num14 = Checkpoint(); int value = ref_ptr2; int start2 = repeatContext.Start; repeatContext.Count++; repeatContext.Start = ref_ptr2; deep = repeatContext; if (!Eval(Mode.Match, ref ref_ptr2, repeatContext.Expression)) { repeatContext.Count--; repeatContext.Start = start2; Backtrack(num14); } else { if (deep != repeatContext) { break; } stack.Push(num14); stack.Push(value); if (ref_ptr2 != repeatContext.Start) { continue; } } } repeat = repeatContext.Previous; goto IL_0bf5; } stack.Count = count2; } } goto case OpCode.True; case OpCode.FastRepeat: { fast = new RepeatContext(fast, ReadProgramCount(pc + 2), ReadProgramCount(pc + 4), (opFlags & OpFlags.Lazy) != OpFlags.None, pc + 6); fast.Start = ref_ptr2; int cp = Checkpoint(); pc += program[pc + 1]; ushort num2 = program[pc]; int num3 = -1; int num4 = -1; int num5 = 0; OpCode opCode2 = (OpCode)(num2 & 0xFF); if (opCode2 == OpCode.Character || opCode2 == OpCode.String) { OpFlags opFlags2 = (OpFlags)(num2 & 0xFF00); if ((opFlags2 & OpFlags.Negate) == OpFlags.None) { if (opCode2 == OpCode.String) { int num6 = 0; if ((opFlags2 & OpFlags.RightToLeft) != 0) { num6 = program[pc + 1] - 1; } num3 = program[pc + 2 + num6]; } else { num3 = program[pc + 1]; } num4 = (((opFlags2 & OpFlags.IgnoreCase) == OpFlags.None) ? num3 : char.ToUpper((char)num3)); num5 = (((opFlags2 & OpFlags.RightToLeft) != 0) ? (-1) : 0); } } if (fast.IsLazy) { if (!fast.IsMinimum && !Eval(Mode.Count, ref ref_ptr2, fast.Expression)) { fast = fast.Previous; break; } while (true) { int num7 = ref_ptr2 + num5; if (num3 < 0 || (num7 >= 0 && num7 < text_end && (num3 == text[num7] || num4 == text[num7]))) { deep = null; if (Eval(Mode.Match, ref ref_ptr2, pc)) { break; } } if (fast.IsMaximum) { goto IL_0e57; } Backtrack(cp); if (Eval(Mode.Count, ref ref_ptr2, fast.Expression)) { continue; } goto IL_0e8e; } fast = fast.Previous; } else { if (!Eval(Mode.Count, ref ref_ptr2, fast.Expression)) { fast = fast.Previous; break; } int num8 = (fast.Count > 0) ? ((ref_ptr2 - fast.Start) / fast.Count) : 0; while (true) { int num9 = ref_ptr2 + num5; if (num3 < 0 || (num9 >= 0 && num9 < text_end && (num3 == text[num9] || num4 == text[num9]))) { deep = null; if (Eval(Mode.Match, ref ref_ptr2, pc)) { break; } } fast.Count--; if (fast.IsMinimum) { ref_ptr2 -= num8; Backtrack(cp); continue; } goto IL_0fab; } fast = fast.Previous; } goto case OpCode.True; } case OpCode.True: case OpCode.Balance: ref_ptr = ref_ptr2; switch (mode) { case Mode.Match: return(true); case Mode.Count: fast.Count++; if (!fast.IsMaximum && (!fast.IsLazy || !fast.IsMinimum)) { pc = fast.Expression; continue; } return(true); } break; case OpCode.False: case OpCode.Info: break; IL_0fab: fast = fast.Previous; break; IL_0234: if (num11 == scan_ptr) { ref_ptr2 = ((!flag) ? (scan_ptr - num12) : (scan_ptr + num12)); if (TryMatch(ref ref_ptr2, pc + num13)) { goto case OpCode.True; } break; } break; IL_0165: if (num11 == 0) { ref_ptr2 = 0; if (TryMatch(ref ref_ptr2, pc + num13)) { goto case OpCode.True; } num11++; } for (; (flag && num11 >= 0) || (!flag && num11 <= num15); num11 = ((!flag) ? (num11 + 1) : (num11 - 1))) { if (num11 != 0 && text[num11 - 1] != '\n') { continue; } ref_ptr2 = ((!flag) ? ((num11 != 0) ? (num11 - num12) : num11) : ((num11 != num15) ? (num11 + num12) : num11)); if (!TryMatch(ref ref_ptr2, pc + num13)) { continue; } goto case OpCode.True; } break; IL_0c31: repeat = repeatContext; break; IL_09bc: repeatContext.Start = start; repeatContext.Count = count; break; IL_0e57: fast = fast.Previous; break; IL_0e8e: fast = fast.Previous; break; IL_0bf5: while (true) { deep = null; if (Eval(Mode.Match, ref ref_ptr2, pc + 1)) { break; } if (stack.Count != count2) { repeatContext.Count--; ref_ptr2 = stack.Pop(); Backtrack(stack.Pop()); continue; } goto IL_0c31; } stack.Count = count2; goto case OpCode.True; end_IL_0028: break; } break; } switch (mode) { case Mode.Match: return(false); case Mode.Count: if (!fast.IsLazy && fast.IsMinimum) { return(true); } ref_ptr = fast.Start; return(false); default: return(false); } }