Add hb_bsearch_r()

This commit is contained in:
Behdad Esfahbod 2017-10-29 17:33:32 -06:00
parent 0712e915b4
commit 977679f229
2 changed files with 26 additions and 0 deletions

View File

@ -28,6 +28,7 @@
#define HB_OT_POST_TABLE_HH
#include "hb-open-type-private.hh"
#include "hb-sort-r.hh"
#define HB_STRING_ARRAY_NAME format1_names
#define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh"

View File

@ -4,6 +4,31 @@
#include <hb-private.hh>
static inline void *
hb_bsearch_r(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *_a, const void *_b, const void *_arg),
void *arg)
{
int min = 0, max = (int) nmemb - 1;
while (min <= max)
{
int mid = (min + max) / 2;
const void *p = (const void *) (((const char *) base) + (mid * size));
int c = compar (key, p, arg);
if (c < 0)
max = mid - 1;
else if (c > 0)
min = mid + 1;
else
return (void *) p;
}
return NULL;
}
/* From https://github.com/noporpoise/sort_r */
/*