summaryrefslogtreecommitdiff
path: root/lib/wcsstr-impl.h
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-02-06 15:18:24 +0100
committerBruno Haible <bruno@clisp.org>2011-02-07 23:36:07 +0100
commit8e5e25f79c3ff689fc3a3aab0fe439064ef76d02 (patch)
tree9a5ed7a76917027d013c147a0878a7b58aaedd77 /lib/wcsstr-impl.h
parentcaf727a459d4f451f29c2edf565d7d4eb12df6fc (diff)
downloadgnulib-8e5e25f79c3ff689fc3a3aab0fe439064ef76d02.tar.gz
New module 'wcsstr'.
* modules/wcsstr: New file. * lib/wchar.in.h (wcsstr): New declaration. * lib/wcsstr.c: New file. * lib/wcsstr-impl.h: New file, from libutf8 with modifications. * m4/wcsstr.m4: New file. * m4/wchar_h.m4 (gl_WCHAR_H): Test whether wcsstr is declared. (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCSSTR, HAVE_WCSSTR. * modules/wchar (Makefile.am): Substitute GNULIB_WCSSTR, HAVE_WCSSTR. * tests/test-wchar-c++.cc: Test the declaration of wcsstr. * doc/posix-functions/wcsstr.texi: Mention the new module.
Diffstat (limited to 'lib/wcsstr-impl.h')
-rw-r--r--lib/wcsstr-impl.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/wcsstr-impl.h b/lib/wcsstr-impl.h
new file mode 100644
index 0000000000..f3fb3fb967
--- /dev/null
+++ b/lib/wcsstr-impl.h
@@ -0,0 +1,51 @@
+/* Locate a substring in a wide string.
+ Copyright (C) 1999, 2011 Free Software Foundation, Inc.
+ Written by Bruno Haible <bruno@clisp.org>, 1999.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+wchar_t *
+wcsstr (const wchar_t *haystack, const wchar_t *needle)
+{
+ wchar_t n = needle[0];
+
+ /* Is needle empty? */
+ if (n == (wchar_t)'\0')
+ return (wchar_t *) haystack;
+
+ /* Is needle nearly empty? */
+ if (needle[1] == (wchar_t)'\0')
+ return wcschr (haystack, n);
+
+ /* Search for needle's first character. */
+ for (; *haystack != (wchar_t)'\0'; haystack++)
+ {
+ if (*haystack == n)
+ {
+ /* Compare with needle's remaining characters. */
+ const wchar_t *hptr = haystack + 1;
+ const wchar_t *nptr = needle + 1;
+ for (;;)
+ {
+ if (*hptr != *nptr)
+ break;
+ hptr++; nptr++;
+ if (*nptr == (wchar_t)'\0')
+ return (wchar_t *) haystack;
+ }
+ }
+ }
+
+ return NULL;
+}