Mercurial > hg > octave-kai > gnulib-hg
annotate lib/strstr.c @ 6075:ea0e673b670d
Recent regex patches.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sat, 20 Aug 2005 01:03:31 +0000 |
parents | 199c20f568d7 |
children | 83bee8e13391 |
rev | line source |
---|---|
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
1 /* Searching in a string. |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
2 Copyright (C) 2005 Free Software Foundation, Inc. |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
3 Written by Bruno Haible <bruno@clisp.org>, 2005. |
5 | 4 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
5 This program is free software; you can redistribute it and/or modify |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
7 the Free Software Foundation; either version 2, or (at your option) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
8 any later version. |
5 | 9 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
13 GNU General Public License for more details. |
5 | 14 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
16 along with this program; if not, write to the Free Software Foundation, |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
5 | 18 |
2020
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
19 #if HAVE_CONFIG_H |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
20 # include <config.h> |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
21 #endif |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
22 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
23 /* Specification. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
24 #include "strstr.h" |
5 | 25 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
26 #if HAVE_MBRTOWC |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
27 # include "mbuiter.h" |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
28 #endif |
2988 | 29 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
30 /* Find the first occurrence of NEEDLE in HAYSTACK. */ |
5 | 31 char * |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
32 strstr (const char *haystack, const char *needle) |
5 | 33 { |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
34 /* Be careful not to look at the entire extent of haystack or needle |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
35 until needed. This is useful because of these two cases: |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
36 - haystack may be very long, and a match of needle found early, |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
37 - needle may be very long, and not even a short initial segment of |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
38 needle may be found in haystack. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
39 #if HAVE_MBRTOWC |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
40 if (MB_CUR_MAX > 1) |
656 | 41 { |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
42 mbui_iterator_t iter_needle; |
656 | 43 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
44 mbui_init (iter_needle, needle); |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
45 if (mbui_avail (iter_needle)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
46 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
47 mbui_iterator_t iter_haystack; |
656 | 48 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
49 mbui_init (iter_haystack, haystack); |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
50 for (;; mbui_advance (iter_haystack)) |
656 | 51 { |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
52 if (!mbui_avail (iter_haystack)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
53 /* No match. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
54 return NULL; |
656 | 55 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
56 if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle))) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
57 /* The first character matches. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
58 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
59 mbui_iterator_t rhaystack; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
60 mbui_iterator_t rneedle; |
656 | 61 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
62 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t)); |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
63 mbui_advance (rhaystack); |
656 | 64 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
65 mbui_init (rneedle, needle); |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
66 if (!mbui_avail (rneedle)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
67 abort (); |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
68 mbui_advance (rneedle); |
656 | 69 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
70 for (;; mbui_advance (rhaystack), mbui_advance (rneedle)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
71 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
72 if (!mbui_avail (rneedle)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
73 /* Found a match. */ |
6064 | 74 return (char *) mbui_cur_ptr (iter_haystack); |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
75 if (!mbui_avail (rhaystack)) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
76 /* No match. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
77 return NULL; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
78 if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle))) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
79 /* Nothing in this round. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
80 break; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
81 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
82 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
83 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
84 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
85 else |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
86 return (char *) haystack; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
87 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
88 else |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
89 #endif |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
90 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
91 if (*needle != '\0') |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
92 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
93 /* Speed up the following searches of needle by caching its first |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
94 character. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
95 char b = *needle++; |
656 | 96 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
97 for (;; haystack++) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
98 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
99 if (*haystack == '\0') |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
100 /* No match. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
101 return NULL; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
102 if (*haystack == b) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
103 /* The first character matches. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
104 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
105 const char *rhaystack = haystack + 1; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
106 const char *rneedle = needle; |
656 | 107 |
6057
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
108 for (;; rhaystack++, rneedle++) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
109 { |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
110 if (*rneedle == '\0') |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
111 /* Found a match. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
112 return (char *) haystack; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
113 if (*rhaystack == '\0') |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
114 /* No match. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
115 return NULL; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
116 if (*rhaystack != *rneedle) |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
117 /* Nothing in this round. */ |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
118 break; |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
119 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
120 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
121 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
122 } |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
123 else |
a878a8d58823
Make strstr() work in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
124 return (char *) haystack; |
5 | 125 } |
126 } |