/* FUNCTION <>---locate a substring - ignores the case of both strings INDEX strcasestr ANSI_SYNOPSIS #include char *strcasestr(const char *<[s1]>, const char *<[s2]>); TRAD_SYNOPSIS #include char *strcasestr(<[s1]>, <[s2]>) char *<[s1]>; char *<[s2]>; DESCRIPTION <> Similar to strstr but this function ignores the case of both strings. RETURNS Returns a pointer to the located string segment, or a null pointer if the string <[s2]> is not found. If <[s2]> points to a string with zero length, the <[s1]> is returned. PORTABILITY <> is in the glibc. <> requires no supporting OS subroutines. It uses tolower() from elsewhere in this library. QUICKREF strcasestr */ /* * Adapted for Cygwin from Stephen R. van den Berg, * (berg@pool.informatik.rwth-aachen.de) procedure of glibc * by Olivier Langlois (olivier.langlois@streamtheworld.com) * June 06, 2007. * * Improved by removing the ANSI C violation where the haystray * pointer was moved before s1 array which results to undefined * result according to the standard. * Changed location where haystray pointer is incremented so that * exit conditions are evaluated first. This has totally removed * the need to decrement the pointer also. This has optimized few * pointer operations per call. */ /* Copyright (c) 2007, Stephen R. van den Berg All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3- Neither the name of the Cubic Circle BV nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * My personal strstr() implementation that beats most other algorithms. * Until someone tells me otherwise, I assume that this is the * fastest implementation of strstr() in C. * I deliberately chose not to comment it. You should have at least * as much fun trying to understand it, as I had to write it :-). * * Stephen R. van den Berg, */ /* Faster looping by precalculating bl, bu, cl, cu before looping. * 2004 Apr 08 Jose Da Silva, digital@joescat@com */ #include #include char * _DEFUN (strcasestr, (s1, s2), _CONST char *s1 _AND _CONST char *s2) { register const unsigned char *haystack, *needle; register int bl, bu, cl, cu; haystack = (const unsigned char *) s1; needle = (const unsigned char *) s2; bl = tolower (*needle); if (bl != '\0') { bu = toupper (bl); /* Find first needle char in the searched string */ do { cl = *haystack; if (cl == '\0') { goto ret0; } ++haystack; } while ((cl != bl) && (cl != bu)); cl = tolower (*++needle); /* If needle is only 1 char long, we are done */ if (cl == '\0') { goto foundneedle; } cu = toupper (cl); ++needle; goto jin; for (;;) { register char a; register const unsigned char *rhaystack, *rneedle; do { /* Find first needle char in the searched string */ /* loop unrolling possibly for optimization ?? */ a = *haystack; if (a == '\0') { goto ret0; } ++haystack; if ((a == bl) || (a == bu)) { break; } a = *haystack; if (a == '\0') { goto ret0; } shloop: ++haystack; } while ((a != bl) && (a != bu)); jin: a = *haystack; if (a == '\0') { goto ret0; } /* Test second needle char */ if ((a != cl) && (a != cu)) { goto shloop; } rhaystack = haystack + 1; rneedle = needle; a = tolower (*rneedle); if (tolower (*rhaystack) == (int) a) { do { /* * Verify the rest of the needle chars against the searched * string */ /* loop unrolling possibly for optimization ?? */ if (a == '\0') { goto foundneedle; } ++rhaystack; a = tolower (*++needle); if (tolower (*rhaystack) != (int) a) { break; } if (a == '\0') { goto foundneedle; } ++rhaystack; a = tolower (*++needle); } while (tolower (*rhaystack) == (int) a); } needle = rneedle; /* took the register-poor approach */ if (a == '\0') { break; } } /* End of the for(;;) loop */ } /* End of if (b != '\0') block */ foundneedle: return (char*) haystack-1; ret0: return (char*)NULL; }