1 /* c-strcasestr.c -- case insensitive substring search in C locale
2 Copyright (C) 2005-2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "c-strcasestr.h"
28 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
30 Note: This function may, in multibyte locales, return success even if
31 strlen (haystack) < strlen (needle) ! */
33 c_strcasestr (const char *haystack, const char *needle)
35 /* Be careful not to look at the entire extent of haystack or needle
36 until needed. This is useful because of these two cases:
37 - haystack may be very long, and a match of needle found early,
38 - needle may be very long, and not even a short initial segment of
39 needle may be found in haystack. */
42 /* Speed up the following searches of needle by caching its first
44 unsigned char b = c_tolower ((unsigned char) *needle);
49 if (*haystack == '\0')
52 if (c_tolower ((unsigned char) *haystack) == b)
53 /* The first character matches. */
55 const char *rhaystack = haystack + 1;
56 const char *rneedle = needle;
58 for (;; rhaystack++, rneedle++)
62 return (char *) haystack;
63 if (*rhaystack == '\0')
66 if (c_tolower ((unsigned char) *rhaystack)
67 != c_tolower ((unsigned char) *rneedle))
68 /* Nothing in this round. */
75 return (char *) haystack;