Back to index

glibc  2.9
wmemchr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,97,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <wchar.h>
00021 
00022 wchar_t *
00023 wmemchr (s, c, n)
00024      register const wchar_t *s;
00025      register wchar_t c;
00026      register size_t n;
00027 {
00028   /* For performance reasons unfold the loop four times.  */
00029   while (n >= 4)
00030     {
00031       if (s[0] == c)
00032        return (wchar_t *) s;
00033       if (s[1] == c)
00034        return (wchar_t *) &s[1];
00035       if (s[2] == c)
00036        return (wchar_t *) &s[2];
00037       if (s[3] == c)
00038        return (wchar_t *) &s[3];
00039       s += 4;
00040       n -= 4;
00041     }
00042 
00043   if (n > 0)
00044     {
00045       if (*s == c)
00046        return (wchar_t *) s;
00047       ++s;
00048       --n;
00049     }
00050   if (n > 0)
00051     {
00052       if (*s == c)
00053        return (wchar_t *) s;
00054       ++s;
00055       --n;
00056     }
00057   if (n > 0)
00058     if (*s == c)
00059       return (wchar_t *) s;
00060 
00061   return NULL;
00062 }
00063 libc_hidden_def (wmemchr)