Accord.Video.ByteArrayUtils.Find C# (CSharp) Method

Find() public static method

Find subarray in the source array.
public static Find ( byte array, byte needle, int startIndex, int sourceLength ) : int
array byte Source array to search for needle.
needle byte Needle we are searching for.
startIndex int Start index in source array.
sourceLength int Number of bytes in source array, where the needle is searched for.
return int
        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;
        }
    }

Usage Example

Example #1
0
        private int FindBoundary()
        {
            byte[] imageDelimiter;

            if (_boundary.Length != 0)
            {
                imageDelimiter = (byte[])_boundary;
            }
            else
            {
                imageDelimiter = _header;
            }

            return(ByteArrayUtils.Find(_buffer, imageDelimiter, _position, RemainingBytes));
        }
All Usage Examples Of Accord.Video.ByteArrayUtils::Find
ByteArrayUtils