Mail Archives: djgpp-workers/2004/01/20/04:58:41
I noticed that bsearch currently casts between void * and char * quite
unnecessarily, and uses unconst earlier than needed. I also changed the
literal 0 to NULL (I wish there were some easy way to do this
automatically):
Index: bsearch.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/stdlib/bsearch.c,v
retrieving revision 1.1
diff -u -r1.1 bsearch.c
--- bsearch.c 26 Dec 1994 20:35:04 -0000 1.1
+++ bsearch.c 20 Jan 2004 09:40:34 -0000
@@ -3,24 +3,23 @@
#include <libc/unconst.h>
void *
-bsearch(const void *key, const void *base0, size_t nelem,
+bsearch(const void *key, const void *base, size_t nelem,
size_t size, int (*cmp)(const void *ck, const void *ce))
{
- char *base = unconst(base0, char *);
int lim, cmpval;
- void *p;
+ const void *p;
for (lim = nelem; lim != 0; lim >>= 1)
{
p = base + (lim >> 1) * size;
cmpval = (*cmp)(key, p);
if (cmpval == 0)
- return p;
+ return unconst(p, void *);
if (cmpval > 0)
{ /* key > p: move right */
- base = (char *)p + size;
+ base = p + size;
lim--;
} /* else move left */
}
- return 0;
+ return NULL;
}
--
Esa Peuha
student of mathematics at the University of Helsinki
http://www.helsinki.fi/~peuha/
- Raw text -