source: trunk/libs/newlib/src/newlib/libc/string/strcasestr.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.7 KB
Line 
1/*
2FUNCTION
3        <<strcasestr>>---case-insensitive character string search
4
5INDEX
6        strcasestr
7
8SYNOPSIS
9        #include <string.h>
10        char *strcasestr(const char *<[s]>, const char *<[find]>);
11
12DESCRIPTION
13        <<strcasestr>> searchs the string <[s]> for
14        the first occurrence of the sequence <[find]>.  <<strcasestr>>
15        is identical to <<strstr>> except the search is
16        case-insensitive.
17
18RETURNS
19
20        A pointer to the first case-insensitive occurrence of the sequence
21        <[find]> or <<NULL>> if no match was found.
22
23PORTABILITY
24<<strcasestr>> is in the Berkeley Software Distribution.
25
26<<strcasestr>> requires no supporting OS subroutines. It uses
27tolower() from elsewhere in this library.
28
29QUICKREF
30        strcasestr
31*/
32
33/*-
34 * Copyright (c) 1990, 1993
35 *      The Regents of the University of California.  All rights reserved.
36 *
37 * The quadratic code is derived from software contributed to Berkeley by
38 * Chris Torek.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 * 4. Neither the name of the University nor the names of its contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64/* Linear algorithm Copyright (C) 2008 Eric Blake
65 * Permission to use, copy, modify, and distribute the linear portion of
66 * software is freely granted, provided that this notice is preserved.
67 */
68
69#include <sys/cdefs.h>
70
71#include <ctype.h>
72#include <string.h>
73#include <strings.h>
74
75#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
76# define RETURN_TYPE char *
77# define AVAILABLE(h, h_l, j, n_l)                      \
78  (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
79   && ((h_l) = (j) + (n_l)))
80# define CANON_ELEMENT(c) tolower (c)
81#if __GNUC_PREREQ (4, 2)
82/* strncasecmp uses signed char, CMP_FUNC is expected to use unsigned char. */
83#pragma GCC diagnostic ignored "-Wpointer-sign"
84#endif
85# define CMP_FUNC strncasecmp
86# include "str-two-way.h"
87#endif
88
89/*
90 * Find the first occurrence of find in s, ignore case.
91 */
92char *
93strcasestr (const char *s,
94        const char *find)
95{
96#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
97
98  /* Less code size, but quadratic performance in the worst case.  */
99        char c, sc;
100        size_t len;
101
102        if ((c = *find++) != 0) {
103                c = tolower((unsigned char)c);
104                len = strlen(find);
105                do {
106                        do {
107                                if ((sc = *s++) == 0)
108                                        return (NULL);
109                        } while ((char)tolower((unsigned char)sc) != c);
110                } while (strncasecmp(s, find, len) != 0);
111                s--;
112        }
113        return ((char *)s);
114
115#else /* compilation for speed */
116
117  /* Larger code size, but guaranteed linear performance.  */
118  const char *haystack = s;
119  const char *needle = find;
120  size_t needle_len; /* Length of NEEDLE.  */
121  size_t haystack_len; /* Known minimum length of HAYSTACK.  */
122  int ok = 1; /* True if NEEDLE is prefix of HAYSTACK.  */
123
124  /* Determine length of NEEDLE, and in the process, make sure
125     HAYSTACK is at least as long (no point processing all of a long
126     NEEDLE if HAYSTACK is too short).  */
127  while (*haystack && *needle)
128    ok &= (tolower ((unsigned char) *haystack++)
129           == tolower ((unsigned char) *needle++));
130  if (*needle)
131    return NULL;
132  if (ok)
133    return (char *) s;
134  needle_len = needle - find;
135  haystack = s + 1;
136  haystack_len = needle_len - 1;
137
138  /* Perform the search.  */
139  if (needle_len < LONG_NEEDLE_THRESHOLD)
140    return two_way_short_needle ((const unsigned char *) haystack,
141                                 haystack_len,
142                                 (const unsigned char *) find, needle_len);
143  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
144                              (const unsigned char *) find, needle_len);
145#endif /* compilation for speed */
146}
Note: See TracBrowser for help on using the repository browser.