X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdlib%2Fbsearch.c;h=835407f5ee8e2314100cee595a1e25d44346d5a3;hp=e8e4f3125f53ef61677defaf38787103269c50b4;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=b08f4b52b1cd1f7a9553c0f357a7c90859fa3e73 diff --git a/functions/stdlib/bsearch.c b/functions/stdlib/bsearch.c index e8e4f31..835407f 100644 --- a/functions/stdlib/bsearch.c +++ b/functions/stdlib/bsearch.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* bsearch( const void *, const void *, size_t, size_t, int(*)( const void *, const void * ) ) This file is part of the Public Domain C Library (PDCLib). @@ -14,18 +12,19 @@ void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, { const void * pivot; int rc; - int corr; + size_t corr; while ( nmemb ) { /* algorithm needs -1 correction if remaining elements are an even number. */ - corr = ( nmemb % 2 ) - 1; + corr = nmemb % 2; nmemb /= 2; pivot = (const char *)base + (nmemb * size); rc = compar( key, pivot ); if ( rc > 0 ) { base = (const char *)pivot + size; - nmemb += corr; + /* applying correction */ + nmemb -= ( 1 - corr ); } else if ( rc == 0 ) { @@ -38,7 +37,7 @@ void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, #endif #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" static int compare( const void * left, const void * right ) {