www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2004/01/20/04:58:41

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Date: Tue, 20 Jan 2004 11:57:20 +0200 (EET)
From: Esa A E Peuha <peuha AT cc DOT helsinki DOT fi>
Sender: peuha AT sirppi DOT helsinki DOT fi
To: djgpp-workers AT delorie DOT com
Subject: Patch for bsearch
Message-ID: <Pine.OSF.4.58.0401201143590.29095@sirppi.helsinki.fi>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019