/// <summary>
        ///     Find the length of the sequence of bytes that are equal from array a, starring at index index,
        ///     and array b, starting at 0.
        /// </summary>
        /// <param name="a"> Larger array to look at from index. </param>
        /// <param name="index"> Index in a to look for match. </param>
        /// <param name="b"> Smaller array to match in a at index. </param>
        /// <returns></returns>
        public static int MatchingBytesCount(ByteArrayIndexer a, int index, ByteArrayIndexer b)
        {
            var i = 0;

            for (; i < b.Length && index + i < a.Length; ++i)
            {
                if (a[index + i] != b[i])
                {
                    return(i);
                }
            }
            return(i);
        }
        /// <summary>
        ///     This method finds the longest match of the needle in the haystack.
        /// </summary>
        /// <param name="haystack"> Large array to search in. </param>
        /// <param name="needle"> Needle to search after. </param>
        /// <returns> A MatchPointer describing the index of the needle and the length of the match. </returns>
        public static MatchPointer FindLongestMatch(ByteArrayIndexer haystack, ByteArrayIndexer needle)
        {
            var longestMatch        = 1;
            var indexOfLongestMatch = 0;

            //Find the longest match in the haystack
            for (var i = 0; i < haystack.Length - longestMatch; ++i)
            {
                var matchedBytes = MatchingBytesCount(haystack, i, needle);
                if (matchedBytes > longestMatch)
                {
                    longestMatch        = matchedBytes;
                    indexOfLongestMatch = i;
                }
            }

            return(longestMatch > 1 ? new MatchPointer(indexOfLongestMatch, longestMatch) : default(MatchPointer));
        }
예제 #3
0
 public static extern MatchPointer FindLongestMatch(ByteArrayIndexer haystack, ByteArrayIndexer needle);