From 977679f229a10868dc668294082bd82125e4fe48 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 29 Oct 2017 17:33:32 -0600 Subject: [PATCH] Add hb_bsearch_r() --- src/hb-ot-post-table.hh | 1 + src/hb-sort-r.hh | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/hb-ot-post-table.hh b/src/hb-ot-post-table.hh index 273a6d027..94d1e429c 100644 --- a/src/hb-ot-post-table.hh +++ b/src/hb-ot-post-table.hh @@ -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" diff --git a/src/hb-sort-r.hh b/src/hb-sort-r.hh index 1b91b4da6..80cd3c807 100644 --- a/src/hb-sort-r.hh +++ b/src/hb-sort-r.hh @@ -4,6 +4,31 @@ #include + +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 */ /*