X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fmemchr.c;h=4b28e481f88e4789c52e74bf14483b924e38c64e;hb=8d49f90d69a06c652c97d38f417ffb42e6e48214;hp=71b1ab5f6ecd9e26c08727a42f69c35939d6e3c6;hpb=69d778f260410d7586953f15e9903cbf41a8a157;p=gnulib.git diff --git a/lib/memchr.c b/lib/memchr.c index 71b1ab5f6..4b28e481f 100644 --- a/lib/memchr.c +++ b/lib/memchr.c @@ -1,5 +1,5 @@ -/* Copyright (C) 1991, 1993 Free Software Foundation, Inc. - Based on strlen implemention by Torbjorn Granlund (tege@sics.se), +/* Copyright (C) 1991,93,96,97,99,2000 Free Software Foundation, Inc. + Based on strlen implementation by Torbjorn Granlund (tege@sics.se), with help from Dan Sahlin (dan@sics.se) and commentary by Jim Blandy (jimb@ai.mit.edu); adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu), @@ -20,10 +20,11 @@ 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, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +USA. */ #ifdef HAVE_CONFIG_H -#include +# include #endif #undef __ptr_t @@ -33,41 +34,56 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ # define __ptr_t char * #endif /* C++ or ANSI C. */ -#if defined (HAVE_STRING_H) || defined (_LIBC) +#if defined _LIBC # include +# include +#else +# define reg_char char +#endif + +#if HAVE_STDLIB_H || defined _LIBC +# include #endif -#if defined (HAVE_LIMIT_H) || defined (_LIBC) -# include +#if HAVE_LIMITS_H || defined _LIBC +# include #endif #define LONG_MAX_32_BITS 2147483647 #ifndef LONG_MAX -#define LONG_MAX LONG_MAX_32_BITS +# define LONG_MAX LONG_MAX_32_BITS #endif #include +#if HAVE_BP_SYM_H || defined _LIBC +# include +#else +# define BP_SYM(sym) sym +#endif +#undef memchr +#undef __memchr /* Search no more than N bytes of S for C. */ - __ptr_t -memchr (s, c, n) +__memchr (s, c_in, n) const __ptr_t s; - int c; + int c_in; size_t n; { const unsigned char *char_ptr; const unsigned long int *longword_ptr; unsigned long int longword, magic_bits, charmask; + unsigned reg_char c; - c = (unsigned char) c; + c = (unsigned char) c_in; /* Handle the first few characters by reading one character at a time. Do this until CHAR_PTR is aligned on a longword boundary. */ - for (char_ptr = s; n > 0 && ((unsigned long int) char_ptr - & (sizeof (longword) - 1)) != 0; + for (char_ptr = (const unsigned char *) s; + n > 0 && ((unsigned long int) char_ptr + & (sizeof (longword) - 1)) != 0; --n, ++char_ptr) if (*char_ptr == c) return (__ptr_t) char_ptr; @@ -80,9 +96,9 @@ memchr (s, c, n) /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits the "holes." Note that there is a hole just to the left of each byte, with an extra at the end: - + bits: 01111110 11111110 11111110 11111111 - bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD + bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ @@ -100,7 +116,7 @@ memchr (s, c, n) charmask = c | (c << 8); charmask |= charmask << 16; #if LONG_MAX > LONG_MAX_32_BITS - charmask |= charmask << 32; + charmask |= charmask << 32; #endif /* Instead of the traditional loop which tests each character, @@ -146,10 +162,10 @@ memchr (s, c, n) /* Add MAGIC_BITS to LONGWORD. */ if ((((longword + magic_bits) - + /* Set those bits that were unchanged by the addition. */ ^ ~longword) - + /* Look at only the hole bits. If any of the hole bits are unchanged, most likely one of the bytes was a zero. */ @@ -195,3 +211,6 @@ memchr (s, c, n) return 0; } +#ifdef weak_alias +weak_alias (__memchr, BP_SYM (memchr)) +#endif