#include <ncbi.h>
#include <gishlib.h>

/*
	str_ncasestr(s1, s2, n)

		Like str_casestr(), but only the first "n" characters of s2
		are compared.

*/

CharPtr LIBCALL
str_ncasestr(s1, s2, n)
	CharPtr	s1, s2;
	register size_t	n;
{
	register BytePtr
			us1 = (BytePtr)s1,
			us2 = (BytePtr)s2;
	register Byte	ch;
	register int	*map = _ucasemap;

	/* An empty string always matches */
	if ((ch = *us2++) == NULLB || n-- == 0)
		return (CharPtr)us1;

	for (;;) {
		while (map[*us1] != map[ch])
			if (*us1++ == NULLB)
				return NULL;
		if (str_ncasecmp((CharPtr)++us1, (CharPtr)us2, n) == 0)
			return (CharPtr)--us1;
	}
	/*NOTREACHED*/
}
