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

/*
	str_nstr(s1, s2, n)

		Like str_str(), but only the first "n" characters
		of the substring s2 (or until a null-character is encountered
		in s2, if s2 should be shorter than "n") are compared.
*/

CharPtr LIBCALL
str_nstr(s1, s2, n)
	register CharPtr s1, s2;
	register size_t	n;
{
	register char	ch;
	register CharPtr	s1s, s2s;
	register size_t	ns;

	/* An empty string always matches */
	if ((ch = *s2++) == NULLB || n-- == 0)
		return s1;
	for (;;) {
		while (*s1 != ch)
			if (*s1++ == NULLB)
				return NULL;
		s1s = ++s1; s2s = s2; ns = n;
		while (ns > 0 && *s1s == *s2s++) {
			--ns;
			if (*s1s++ == NULLB)
				return --s1;
		}
		if (ns == 0)
			return --s1;
	}
	/*NOTREACHED*/
}
