diff --git a/src/hb-dsalgs.hh b/src/hb-dsalgs.hh index cc1a1d425..1680bf914 100644 --- a/src/hb-dsalgs.hh +++ b/src/hb-dsalgs.hh @@ -650,6 +650,13 @@ template inline hb_array_t hb_array (T *array, unsigned int len) { return hb_array_t (array, len); } +enum hb_bfind_not_found_t +{ + HB_BFIND_NOT_FOUND_DONT_STORE, + HB_BFIND_NOT_FOUND_STORE, + HB_BFIND_NOT_FOUND_STORE_CLOSEST, +}; + template struct hb_sorted_array_t : hb_array_t { @@ -669,7 +676,9 @@ struct hb_sorted_array_t : hb_array_t return bfind (x, &i) ? &this->arrayZ[i] : not_found; } template - inline bool bfind (const T &x, unsigned int *i = nullptr) const + inline bool bfind (const T &x, unsigned int *i = nullptr, + hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const { int min = 0, max = (int) this->len - 1; const Type *array = this->arrayZ; @@ -690,9 +699,21 @@ struct hb_sorted_array_t : hb_array_t } if (i) { - if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0)) - max++; - *i = max; + switch (not_found) + { + case HB_BFIND_NOT_FOUND_DONT_STORE: + break; + + case HB_BFIND_NOT_FOUND_STORE: + *i = to_store; + break; + + case HB_BFIND_NOT_FOUND_STORE_CLOSEST: + if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0)) + max++; + *i = max; + break; + } } return false; } diff --git a/src/hb-open-file.hh b/src/hb-open-file.hh index d3ecc606d..af68892ab 100644 --- a/src/hb-open-file.hh +++ b/src/hb-open-file.hh @@ -111,14 +111,7 @@ typedef struct OffsetTable { Tag t; t.set (tag); - unsigned int i; - if (tables.bfind (t, &i)) - { - if (table_index) *table_index = i; - return true; - } - if (table_index) *table_index = (unsigned) Index::NOT_FOUND_INDEX; - return false; + return tables.bfind (t, table_index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); } inline const TableRecord& get_table_by_tag (hb_tag_t tag) const { diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index c7154c424..1c810e712 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -475,8 +475,10 @@ struct SortedUnsizedArrayOf : UnsizedArrayOf inline const Type &bsearch (unsigned int len, const T &x) const { return *as_array (len).bsearch (x, &Null (Type)); } template - inline bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr) const - { return as_array (len).bfind (x, i); } + inline bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr, + hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_array (len).bfind (x, i, not_found, to_store); } }; @@ -782,8 +784,10 @@ struct SortedArrayOf : ArrayOf inline const Type &bsearch (const T &x) const { return *as_array ().bsearch (x, &Null (Type)); } template - inline bool bfind (const T &x, unsigned int *i = nullptr) const - { return as_array ().bfind (x, i); } + inline bool bfind (const T &x, unsigned int *i = nullptr, + hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_array ().bfind (x, i, not_found, to_store); } }; /* diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 6c6bcf580..b5af73af6 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -128,12 +128,7 @@ struct RecordArrayOf : SortedArrayOf > } inline bool find_index (hb_tag_t tag, unsigned int *index) const { - if (!this->bfind (tag, index)) - { - if (index) *index = Index::NOT_FOUND_INDEX; - return false; - } - return true; + return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); } }; @@ -821,8 +816,7 @@ struct CoverageFormat1 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const { unsigned int i; - if (!glyphArray.bfind (glyph_id, &i)) - return NOT_COVERED; + glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED); return i; } diff --git a/src/hb-set.hh b/src/hb-set.hh index a464131d4..8b7a0f3d0 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -544,7 +544,7 @@ struct hb_set_t page_map_t map = {get_major (*codepoint), 0}; unsigned int i; - page_map.bfind (map, &i); + page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST); if (i < page_map.len && page_map[i].major == map.major) { if (pages[page_map[i].index].next (codepoint)) @@ -575,7 +575,7 @@ struct hb_set_t page_map_t map = {get_major (*codepoint), 0}; unsigned int i; - page_map.bfind (map, &i); + page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST); if (i < page_map.len && page_map[i].major == map.major) { if (pages[page_map[i].index].previous (codepoint)) @@ -670,7 +670,7 @@ struct hb_set_t { page_map_t map = {get_major (g), pages.len}; unsigned int i; - if (!page_map.bfind (map, &i)) + if (!page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST)) { if (!resize (pages.len + 1)) return nullptr; diff --git a/src/hb-vector.hh b/src/hb-vector.hh index a8c98d221..787512f92 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -239,8 +239,10 @@ struct hb_vector_t inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const { return as_sorted_array ().bsearch (x, not_found); } template - inline bool bfind (const T &x, unsigned int *i = nullptr) const - { return as_sorted_array ().bfind (x, i); } + inline bool bfind (const T &x, unsigned int *i = nullptr, + hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_sorted_array ().bfind (x, i, not_found, to_store); } };