/*
	mem_index(s1, len1, s2, len2)

		Find the start of consecutive bytes matching s2 within s1.
		Return the address within s1 of the start of s2.
		Return NULL if s2 is not contained in s1.
*/
#include <ncbi.h>
#include <gishlib.h>

Nlm_VoidPtr _cdecl
mem_index(str1, len1, str2, len2)
	Nlm_VoidPtr	str1, str2;
	register size_t	len1, len2;
{
	register CharPtr	s1 = str1, s2 = str2;
	register char	ch;
	register CharPtr	s1s, s2s;
	register size_t	ns;
	size_t	n;

	/* An empty buffer matches right away */
	if (len2 == 0)
		return s1;

	if (len2 > len1)
		return NULL;

	ch = *s2++;
	--len2;
	ns = len1 - len2;
	for (;;) {
		while (ns > 0 && *s1++ != ch)
			--ns;
		if (ns == 0)
			return NULL;

		s1s = s1; s2s = s2; n = len2;
		while (n > 0 && *s1s++ == *s2s++)
			--n;
		if (n == 0)
			return --s1;
	}
	/* NOT REACHED */
}
