/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <string.h>

/** @addtogroup string */
/** @{ */

/**
*	Find a substring in a string.
*
*	The function looks for the first occurance of the string \p needle
*	in the string \p haystack. If \p needle is an empty string, then
*	it is considered to be immediately found, that is, a pointer to the
*	first character of \p haystack is returned, even if that haystack
*	was itself empty (i.e. you can find an empty string in an empty string).
*
*	@param	haystack	Pointer to the string in which to search
*	@param	needle		Pointer to the string you want to find
*	@return				Pointer to the first occurance of \p needle in
*						\p haystack or NULL if \p needle is not found.
*/

char	*strstr( const char *haystack, const char *needle )
{
	if ( ! *needle ) return( (char *) haystack );

	// This is a *very* primitive implementation. You can achieve much
	// better performance by establishing a state machine that is based
	// on the search pattern. However, you need some extra memory space
	// to do that, which we are not given. So, we have to resort to the
	// rather inefficient naive algorithm.

	for ( ;; ) {

		if ( ! *haystack ) return( NULL );

		if ( strcmp( haystack, needle ) )

			haystack++;
		else
			return( (char *) haystack );
	}
}

/** @} */

