示例#1
0
        public int SearchChars(char[] text, int textStart, int textEnd, char[] pattern, CharIntMap processed)
        {
            char firstChar = pattern[0];
            int secondChar = 1;
            char middleChar = pattern[pattern.Length / 2];
            char lastChar = pattern[pattern.Length - 1];

            int j = textStart;
            while (j <= text.Length - pattern.Length)
            {
                char c = text[j + (pattern.Length - 1)];

                if (
                    // right most character of the pattern and the window are compared
                    lastChar == c &&
                    // left most character of the pattern and the window are compared
                    firstChar == text[j] &&
                    // the middle character of both pattern and the window are compared
                    middleChar == text[j + pattern.Length / 2])
                {
                    while (secondChar < pattern.Length - 1 && pattern[secondChar] == text[j + secondChar])
                    {
                        secondChar++;
                    }
                    if (secondChar == pattern.Length - 1)
                        return j;
                }

                j += processed.Get(c);
            }
            return -1;
        }
示例#2
0
 public int SearchChars(char[] text, int textStart, int textEnd, char[] pattern, CharIntMap processed)
 {
     //CharIntMap b = processed;
     int d, j, pos, last;
     pos = textStart;
     while (pos <= textEnd - pattern.Length)
     {
         j = pattern.Length - 1;
         last = pattern.Length;
         d = -1;
         while (d != 0)
         {
             d &= processed.Get(text[pos + j]);
             if (d != 0)
             {
                 if (j == 0)
                 {
                     return pos;
                 }
                 last = j;
             }
             --j;
             d <<= 1;
         }
         pos += last;
     }
     return -1;
 }