annotate lib/c-strcasestr.c @ 6912:314715e0260d

Merge from coreutils.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 03 Jul 2006 08:32:46 +0000
parents 74a5018452c0
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6360
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* c-strcasestr.c -- case insensitive substring search in C locale
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2005 Free Software Foundation, Inc.
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #ifdef HAVE_CONFIG_H
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 # include <config.h>
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #endif
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "c-strcasestr.h"
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stddef.h>
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "c-ctype.h"
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 comparison.
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 Note: This function may, in multibyte locales, return success even if
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 strlen (haystack) < strlen (needle) ! */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 char *
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 c_strcasestr (const char *haystack, const char *needle)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 /* Be careful not to look at the entire extent of haystack or needle
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 until needed. This is useful because of these two cases:
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 - haystack may be very long, and a match of needle found early,
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 - needle may be very long, and not even a short initial segment of
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 needle may be found in haystack. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 if (*needle != '\0')
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Speed up the following searches of needle by caching its first
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 character. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 unsigned char b = c_tolower ((unsigned char) *needle);
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 needle++;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 for (;; haystack++)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 {
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 if (*haystack == '\0')
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* No match. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 return NULL;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (c_tolower ((unsigned char) *haystack) == b)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* The first character matches. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 const char *rhaystack = haystack + 1;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 const char *rneedle = needle;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 for (;; rhaystack++, rneedle++)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (*rneedle == '\0')
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* Found a match. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 return (char *) haystack;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 if (*rhaystack == '\0')
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 /* No match. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 return NULL;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (c_tolower ((unsigned char) *rhaystack)
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 != c_tolower ((unsigned char) *rneedle))
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 /* Nothing in this round. */
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 break;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 }
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 }
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 else
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 return (char *) haystack;
74a5018452c0 New module 'c-strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 }