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

/*
	str_casestr(s1, s2)

		Return the address of the start of substring s2 within s1,
		using alphabetic case-independent character comparisons.
		Return NULL if s2 is not a case-independent substring of s1.

*/

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

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

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