Blame wcsmbs/wcsstr.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * The original strstr() file contains the following comment:
Packit 6c4009
 *
Packit 6c4009
 * My personal strstr() implementation that beats most other algorithms.
Packit 6c4009
 * Until someone tells me otherwise, I assume that this is the
Packit 6c4009
 * fastest implementation of strstr() in C.
Packit 6c4009
 * I deliberately chose not to comment it.  You should have at least
Packit 6c4009
 * as much fun trying to understand it, as I had to write it :-).
Packit 6c4009
 *
Packit 6c4009
 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
Packit 6c4009
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
wchar_t *
Packit 6c4009
wcsstr (const wchar_t *haystack, const wchar_t *needle)
Packit 6c4009
{
Packit 6c4009
  wchar_t b, c;
Packit 6c4009
Packit 6c4009
  if ((b = *needle) != L'\0')
Packit 6c4009
    {
Packit 6c4009
      haystack--;				/* possible ANSI violation */
Packit 6c4009
      do
Packit 6c4009
	if ((c = *++haystack) == L'\0')
Packit 6c4009
	  goto ret0;
Packit 6c4009
      while (c != b);
Packit 6c4009
Packit 6c4009
      if (!(c = *++needle))
Packit 6c4009
	goto foundneedle;
Packit 6c4009
      ++needle;
Packit 6c4009
      goto jin;
Packit 6c4009
Packit 6c4009
      for (;;)
Packit 6c4009
	{
Packit 6c4009
	  wchar_t a;
Packit 6c4009
	  const wchar_t *rhaystack, *rneedle;
Packit 6c4009
Packit 6c4009
	  do
Packit 6c4009
	    {
Packit 6c4009
	      if (!(a = *++haystack))
Packit 6c4009
		goto ret0;
Packit 6c4009
	      if (a == b)
Packit 6c4009
		break;
Packit 6c4009
	      if ((a = *++haystack) == L'\0')
Packit 6c4009
		goto ret0;
Packit 6c4009
shloop:	      ;
Packit 6c4009
	    }
Packit 6c4009
	  while (a != b);
Packit 6c4009
Packit 6c4009
jin:	  if (!(a = *++haystack))
Packit 6c4009
	    goto ret0;
Packit 6c4009
Packit 6c4009
	  if (a != c)
Packit 6c4009
	    goto shloop;
Packit 6c4009
Packit 6c4009
	  if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
Packit 6c4009
	    do
Packit 6c4009
	      {
Packit 6c4009
		if (a == L'\0')
Packit 6c4009
		  goto foundneedle;
Packit 6c4009
		if (*++rhaystack != (a = *++needle))
Packit 6c4009
		  break;
Packit 6c4009
		if (a == L'\0')
Packit 6c4009
		  goto foundneedle;
Packit 6c4009
	      }
Packit 6c4009
	    while (*++rhaystack == (a = *++needle));
Packit 6c4009
Packit 6c4009
	  needle = rneedle;		  /* took the register-poor approach */
Packit 6c4009
Packit 6c4009
	  if (a == L'\0')
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
foundneedle:
Packit 6c4009
  return (wchar_t*) haystack;
Packit 6c4009
ret0:
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
/* This alias is for backward compatibility with drafts of the ISO C
Packit 6c4009
   standard.  Unfortunately the Unix(TM) standard requires this name.  */
Packit 6c4009
weak_alias (wcsstr, wcswcs)