Find subarray in the source array. : Array Util « Collections Data Structure « C# / C Sharp






Find subarray in the source array.

        


using System;

namespace Keyki.XNA.Video
{
    /// <summary>
    /// Some internal utilities for handling arrays.
    /// </summary>
    /// 
    internal static class ByteArrayUtils
    {
        /// <summary>
        /// Check if the array contains needle at specified position.
        /// </summary>
        /// 
        /// <param name="array">Source array to check for needle.</param>
        /// <param name="needle">Needle we are searching for.</param>
        /// <param name="startIndex">Start index in source array.</param>
        /// 
        /// <returns>Returns <b>true</b> if the source array contains the needle at
        /// the specified index. Otherwise it returns <b>false</b>.</returns>
        /// 
        public static bool Compare(byte[] array, byte[] needle, int startIndex)
        {
            int needleLen = needle.Length;
            // compare
            for (int i = 0, p = startIndex; i < needleLen; i++, p++)
            {
                if (array[p] != needle[i])
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Find subarray in the source array.
        /// </summary>
        /// 
        /// <param name="array">Source array to search for needle.</param>
        /// <param name="needle">Needle we are searching for.</param>
        /// <param name="startIndex">Start index in source array.</param>
        /// <param name="sourceLength">Number of bytes in source array, where the needle is searched for.</param>
        /// 
        /// <returns>Returns starting position of the needle if it was found or <b>-1</b> otherwise.</returns>
        /// 
        public static int Find(byte[] array, byte[] needle, int startIndex, int sourceLength)
        {
            int needleLen = needle.Length;
            int index;

            while (sourceLength >= needleLen)
            {
                // find needle's starting element
                index = Array.IndexOf(array, needle[0], startIndex, sourceLength - needleLen + 1);

                // if we did not find even the first element of the needls, then the search is failed
                if (index == -1)
                    return -1;

                int i, p;
                // check for needle
                for (i = 0, p = index; i < needleLen; i++, p++)
                {
                    if (array[p] != needle[i])
                    {
                        break;
                    }
                }

                if (i == needleLen)
                {
                    // needle was found
                    return index;
                }

                // continue to search for needle
                sourceLength -= (index - startIndex + 1);
                startIndex = index + 1;
            }
            return -1;
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Return the average of the given values
2.Return the percentage of a given value.
3.return the min value in the list of double
4.return the max value in the list of double
5.Returns a string representation of the sbyte array
6.Returns a string representation of the char array
7.Returns a string representation of the double array
8.Returns a string representation of the float array
9.Returns a string representation of the int array
10.Returns a string representation of the long array
11.Returns a string representation of the object array
12.Returns a string representation of the short value array
13.Returns a string representation of the bool value array
14.Ensures that the sbyte array cannot hold more than maxCapacity elements.
15.Ensures that the char array cannot hold more than maxCapacity elements.
16.Ensures that the double array cannot hold more than maxCapacity elements.
17.Ensures that the float array cannot hold more than maxCapacity elements.
18.Ensures that the int array cannot hold more than maxCapacity elements.
19.Ensures that the long array cannot hold more than maxCapacity elements.
20.Ensures that the object array cannot hold more than maxCapacity elements.
21.Ensures that the short array cannot hold more than maxCapacity elements.
22.Ensures that the bool array cannot hold more than maxCapacity elements.
23.Copy an array into another array.
24.Swap two elements in a array
25.Generic Growable Array
26.foreach is used to display the contents of an array of integers.
27.Are two arrays equal.
28.Are thos two arrays having the save contents
29.Convert object array to string
30.Get array size
31.Convert array content to generic type array
32.Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional System.Array.
33.Creates a new array with just the specified elements.
34.Array 2D
35.Get a array of object from a enum datatype.
36.Reinitializes an int array to the given value in an optimized way: intArraySet
37.Returns a Boolean indicating whether the Array is Empty (is Null or has a length of zero).
38.Places elements from an enumerable into an array.
39.Bit Array 2D
40.Counting the distribution of the values in an array.
41.Collection of static methods for operations on arrays
42.Char Array Writer
43.Computes the indices
44.Compare Array
45.Remove element from Array
46.Concatenate Array
47.Get the array slice between the two indexes
48.Check if the array contains needle on specified position