diff --git a/src/hb-aat-layout.cc b/src/hb-aat-layout.cc index 4e506de16..6d93f1efa 100644 --- a/src/hb-aat-layout.cc +++ b/src/hb-aat-layout.cc @@ -172,11 +172,7 @@ static const hb_aat_feature_mapping_t feature_mappings[] = const hb_aat_feature_mapping_t * hb_aat_layout_find_feature_mapping (hb_tag_t tag) { - return (const hb_aat_feature_mapping_t *) hb_bsearch (&tag, - feature_mappings, - ARRAY_LENGTH (feature_mappings), - sizeof (feature_mappings[0]), - hb_aat_feature_mapping_t::cmp); + return hb_bsearch (tag, feature_mappings, ARRAY_LENGTH (feature_mappings)); } #endif diff --git a/src/hb-aat-layout.hh b/src/hb-aat-layout.hh index 8310bfcc2..5e4e3bda1 100644 --- a/src/hb-aat-layout.hh +++ b/src/hb-aat-layout.hh @@ -39,14 +39,8 @@ struct hb_aat_feature_mapping_t hb_aat_layout_feature_selector_t selectorToEnable; hb_aat_layout_feature_selector_t selectorToDisable; - HB_INTERNAL static int cmp (const void *key_, const void *entry_) - { - hb_tag_t key = * (unsigned int *) key_; - const hb_aat_feature_mapping_t * entry = (const hb_aat_feature_mapping_t *) entry_; - return key < entry->otFeatureTag ? -1 : - key > entry->otFeatureTag ? 1 : - 0; - } + int cmp (hb_tag_t key) const + { return key < otFeatureTag ? -1 : key > otFeatureTag ? 1 : 0; } }; HB_INTERNAL const hb_aat_feature_mapping_t * diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 042e1c20d..7ddc4b191 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -636,25 +636,36 @@ hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) /* * Sort and search. */ -template -static inline void * -hb_bsearch (const void *key, const void *base, - size_t nmemb, size_t size, - int (*compar)(const void *_key, const void *_item, Ts... _ds), + +template +static int +_cmp_method (const void *pkey, const void *pval, Ts... ds) +{ + const K& key = * (const K*) pkey; + const V& val = * (const V*) pval; + + return val.cmp (key, ds...); +} + +template +static inline V* +hb_bsearch (const K& key, V* base, + size_t nmemb, size_t stride = sizeof (V), + int (*compar)(const void *_key, const void *_item, Ts... _ds) = _cmp_method, Ts... ds) { int min = 0, max = (int) nmemb - 1; while (min <= max) { int mid = ((unsigned int) min + (unsigned int) max) / 2; - const void *p = (const void *) (((const char *) base) + (mid * size)); - int c = compar (key, p, ds...); + V* p = (V*) (((const char *) base) + (mid * stride)); + int c = compar ((const void *) &key, (const void *) p, ds...); if (c < 0) max = mid - 1; else if (c > 0) min = mid + 1; else - return (void *) p; + return p; } return nullptr; } diff --git a/src/hb-ot-name-language-static.hh b/src/hb-ot-name-language-static.hh index 580e7637b..c496dc298 100644 --- a/src/hb-ot-name-language-static.hh +++ b/src/hb-ot-name-language-static.hh @@ -37,12 +37,8 @@ struct hb_ot_language_map_t { - static int cmp (const void *key, const void *item) - { - unsigned int a = * (unsigned int *) key; - unsigned int b = ((const hb_ot_language_map_t *) item)->code; - return a < b ? -1 : a > b ? +1 : 0; - } + int cmp (unsigned int key) const + { return key < code ? -1 : key > code ? +1 : 0; } uint16_t code; char lang[6]; @@ -433,12 +429,7 @@ _hb_ot_name_language_for (unsigned int code, #ifdef HB_NO_OT_NAME_LANGUAGE return HB_LANGUAGE_INVALID; #endif - const hb_ot_language_map_t *entry = (const hb_ot_language_map_t *) - hb_bsearch (&code, - array, - len, - sizeof (array[0]), - hb_ot_language_map_t::cmp); + auto *entry = hb_bsearch (code, array, len); if (entry) return hb_language_from_string (entry->lang, -1); diff --git a/src/hb-ot-name-table.hh b/src/hb-ot-name-table.hh index c0f9fbc1d..a95261d36 100644 --- a/src/hb-ot-name-table.hh +++ b/src/hb-ot-name-table.hh @@ -281,16 +281,14 @@ struct name this->table.destroy (); } - int get_index (hb_ot_name_id_t name_id, - hb_language_t language, - unsigned int *width=nullptr) const + int get_index (hb_ot_name_id_t name_id, + hb_language_t language, + unsigned int *width=nullptr) const { const hb_ot_name_entry_t key = {name_id, {0}, language}; - const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *) - hb_bsearch (&key, - (const hb_ot_name_entry_t *) this->names, + const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names, this->names.length, - sizeof (key), + sizeof (hb_ot_name_entry_t), _hb_ot_name_entry_cmp_key); if (!entry) return -1; diff --git a/src/hb-ot-os2-unicode-ranges.hh b/src/hb-ot-os2-unicode-ranges.hh index b0ccd00d7..fd432edd6 100644 --- a/src/hb-ot-os2-unicode-ranges.hh +++ b/src/hb-ot-os2-unicode-ranges.hh @@ -33,19 +33,8 @@ namespace OT { struct OS2Range { - static int - cmp (const void *_key, const void *_item) - { - hb_codepoint_t cp = *((hb_codepoint_t *) _key); - const OS2Range *range = (OS2Range *) _item; - - if (cp < range->start) - return -1; - else if (cp <= range->end) - return 0; - else - return +1; - } + int cmp (hb_codepoint_t key) const + { return (key < start) ? -1 : key <= end ? 0 : +1; } hb_codepoint_t start; hb_codepoint_t end; @@ -233,10 +222,7 @@ static const OS2Range _hb_os2_unicode_ranges[] = static unsigned int _hb_ot_os2_get_unicode_range_bit (hb_codepoint_t cp) { - OS2Range *range = (OS2Range*) hb_bsearch (&cp, _hb_os2_unicode_ranges, - ARRAY_LENGTH (_hb_os2_unicode_ranges), - sizeof (OS2Range), - OS2Range::cmp); + auto* range = hb_bsearch (cp, _hb_os2_unicode_ranges, ARRAY_LENGTH (_hb_os2_unicode_ranges)); if (range != nullptr) return range->bit; return -1; diff --git a/src/hb-ot-post-table.hh b/src/hb-ot-post-table.hh index 661a917e9..06e6c6103 100644 --- a/src/hb-ot-post-table.hh +++ b/src/hb-ot-post-table.hh @@ -165,8 +165,7 @@ struct post } hb_bytes_t st (name, len); - const uint16_t *gid = (const uint16_t *) hb_bsearch (hb_addressof (st), gids, count, - sizeof (gids[0]), cmp_key, (void *) this); + auto* gid = hb_bsearch (hb_addressof (st), gids, count, sizeof (gids[0]), cmp_key, (void *) this); if (gid) { *glyph = *gid; diff --git a/src/hb-ot-var-mvar-table.hh b/src/hb-ot-var-mvar-table.hh index 5a9d2afb7..1b7fad9ce 100644 --- a/src/hb-ot-var-mvar-table.hh +++ b/src/hb-ot-var-mvar-table.hh @@ -77,7 +77,9 @@ struct MVAR const int *coords, unsigned int coord_count) const { const VariationValueRecord *record; - record = (VariationValueRecord *) hb_bsearch (&tag, valuesZ.arrayZ, + record = (VariationValueRecord *) hb_bsearch (tag, + (const VariationValueRecord *) + (const HBUINT8 *) valuesZ, valueRecordCount, valueRecordSize, tag_compare); if (!record) diff --git a/src/hb-ucd.cc b/src/hb-ucd.cc index b29f2a9c7..ad72a26c0 100644 --- a/src/hb-ucd.cc +++ b/src/hb-ucd.cc @@ -136,20 +136,22 @@ hb_ucd_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED, if ((a & 0xFFFFF800u) == 0x0000u && (b & 0xFFFFFF80) == 0x0300u) { uint32_t k = HB_CODEPOINT_ENCODE3_11_7_14 (a, b, 0); - uint32_t *v = (uint32_t*) hb_bsearch (&k, _hb_ucd_dm2_u32_map, - ARRAY_LENGTH (_hb_ucd_dm2_u32_map), - sizeof (*_hb_ucd_dm2_u32_map), - _cmp_pair_11_7_14); + const uint32_t *v = hb_bsearch (k, + _hb_ucd_dm2_u32_map, + ARRAY_LENGTH (_hb_ucd_dm2_u32_map), + sizeof (*_hb_ucd_dm2_u32_map), + _cmp_pair_11_7_14); if (likely (!v)) return false; u = HB_CODEPOINT_DECODE3_11_7_14_3 (*v); } else { uint64_t k = HB_CODEPOINT_ENCODE3 (a, b, 0); - uint64_t *v = (uint64_t*) hb_bsearch (&k, _hb_ucd_dm2_u64_map, - ARRAY_LENGTH (_hb_ucd_dm2_u64_map), - sizeof (*_hb_ucd_dm2_u64_map), - _cmp_pair); + const uint64_t *v = hb_bsearch (k, + _hb_ucd_dm2_u64_map, + ARRAY_LENGTH (_hb_ucd_dm2_u64_map), + sizeof (*_hb_ucd_dm2_u64_map), + _cmp_pair); if (likely (!v)) return false; u = HB_CODEPOINT_DECODE3_3 (*v); }