Back to index

glibc  2.9
wcsstr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 /*
00020  * The original strstr() file contains the following comment:
00021  *
00022  * My personal strstr() implementation that beats most other algorithms.
00023  * Until someone tells me otherwise, I assume that this is the
00024  * fastest implementation of strstr() in C.
00025  * I deliberately chose not to comment it.  You should have at least
00026  * as much fun trying to understand it, as I had to write it :-).
00027  *
00028  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
00029 
00030 #include <wchar.h>
00031 
00032 wchar_t *
00033 wcsstr (haystack, needle)
00034      const wchar_t *haystack;
00035      const wchar_t *needle;
00036 {
00037   register wchar_t b, c;
00038 
00039   if ((b = *needle) != L'\0')
00040     {
00041       haystack--;                         /* possible ANSI violation */
00042       do
00043        if ((c = *++haystack) == L'\0')
00044          goto ret0;
00045       while (c != b);
00046 
00047       if (!(c = *++needle))
00048        goto foundneedle;
00049       ++needle;
00050       goto jin;
00051 
00052       for (;;)
00053        {
00054          register wchar_t a;
00055          register const wchar_t *rhaystack, *rneedle;
00056 
00057          do
00058            {
00059              if (!(a = *++haystack))
00060               goto ret0;
00061              if (a == b)
00062               break;
00063              if ((a = *++haystack) == L'\0')
00064               goto ret0;
00065 shloop:             ;
00066            }
00067          while (a != b);
00068 
00069 jin:     if (!(a = *++haystack))
00070            goto ret0;
00071 
00072          if (a != c)
00073            goto shloop;
00074 
00075          if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
00076            do
00077              {
00078               if (a == L'\0')
00079                 goto foundneedle;
00080               if (*++rhaystack != (a = *++needle))
00081                 break;
00082               if (a == L'\0')
00083                 goto foundneedle;
00084              }
00085            while (*++rhaystack == (a = *++needle));
00086 
00087          needle = rneedle;           /* took the register-poor approach */
00088 
00089          if (a == L'\0')
00090            break;
00091        }
00092     }
00093 foundneedle:
00094   return (wchar_t*) haystack;
00095 ret0:
00096   return NULL;
00097 }
00098 /* This alias is for backward compatibility with drafts of the ISO C
00099    standard.  Unfortunately the Unix(TM) standard requires this name.  */
00100 weak_alias (wcsstr, wcswcs)