From 03cd9c5cbab9c70c091d4bc5d634fd67469f0658 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 22 Jul 2021 11:27:33 -0700 Subject: [PATCH] Align lfind() API to match bfind() --- src/hb-array.hh | 43 +++++++++++++++++++++++++++----------- src/hb-open-file.hh | 2 +- src/hb-open-type.hh | 16 ++++++++------ src/hb-ot-layout-common.hh | 4 ++-- src/hb-set.hh | 6 +++--- src/hb-vector.hh | 2 +- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index e60e03789..ab0dd6ebe 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -36,6 +36,14 @@ template struct hb_sorted_array_t; +enum hb_not_found_t +{ + HB_NOT_FOUND_DONT_STORE, + HB_NOT_FOUND_STORE, + HB_NOT_FOUND_STORE_CLOSEST, +}; + + template struct hb_array_t : hb_iter_with_fallback_t, Type&> { @@ -139,7 +147,9 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> return lfind (x, &i) ? &this->arrayZ[i] : not_found; } template - bool lfind (const T &x, unsigned *pos = nullptr) const + bool lfind (const T &x, unsigned *pos = nullptr, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const { for (unsigned i = 0; i < length; ++i) if (hb_equal (x, this->arrayZ[i])) @@ -149,6 +159,22 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> return true; } + if (pos) + { + switch (not_found) + { + case HB_NOT_FOUND_DONT_STORE: + break; + + case HB_NOT_FOUND_STORE: + *pos = to_store; + break; + + case HB_NOT_FOUND_STORE_CLOSEST: + *pos = length; + break; + } + } return false; } @@ -266,13 +292,6 @@ template inline hb_array_t hb_array (T (&array_)[length_]) { return hb_array_t (array_); } -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_iter_t, Type&>, @@ -323,7 +342,7 @@ struct hb_sorted_array_t : } template bool bfind (const T &x, unsigned int *i = nullptr, - hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, unsigned int to_store = (unsigned int) -1) const { unsigned pos; @@ -339,14 +358,14 @@ struct hb_sorted_array_t : { switch (not_found) { - case HB_BFIND_NOT_FOUND_DONT_STORE: + case HB_NOT_FOUND_DONT_STORE: break; - case HB_BFIND_NOT_FOUND_STORE: + case HB_NOT_FOUND_STORE: *i = to_store; break; - case HB_BFIND_NOT_FOUND_STORE_CLOSEST: + case HB_NOT_FOUND_STORE_CLOSEST: *i = pos; break; } diff --git a/src/hb-open-file.hh b/src/hb-open-file.hh index 65169e251..fc94836a2 100644 --- a/src/hb-open-file.hh +++ b/src/hb-open-file.hh @@ -102,7 +102,7 @@ typedef struct OpenTypeOffsetTable { Tag t; t = tag; - return tables.bfind (t, table_index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); + return tables.bfind (t, table_index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); } 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 0aa97cac4..fa6ccbd93 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -468,8 +468,10 @@ struct UnsizedArrayOf const Type &lsearch (unsigned int len, const T &x, const Type ¬_found = Null (Type)) const { return *as_array (len).lsearch (x, ¬_found); } template - bool lfind (unsigned int len, const T &x, unsigned *pos = nullptr) const - { return as_array (len).lfind (x, pos); } + bool lfind (unsigned int len, const T &x, unsigned int *i = nullptr, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_array (len).lfind (x, i, not_found, to_store); } void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1) { as_array (len).qsort (start, end); } @@ -577,7 +579,7 @@ struct SortedUnsizedArrayOf : UnsizedArrayOf { return *as_array (len).bsearch (x, ¬_found); } template 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, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, unsigned int to_store = (unsigned int) -1) const { return as_array (len).bfind (x, i, not_found, to_store); } }; @@ -639,8 +641,10 @@ struct ArrayOf const Type &lsearch (const T &x, const Type ¬_found = Null (Type)) const { return *as_array ().lsearch (x, ¬_found); } template - bool lfind (const T &x, unsigned *pos = nullptr) const - { return as_array ().lfind (x, pos); } + bool lfind (const T &x, unsigned int *i = nullptr, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_array ().lfind (x, i, not_found, to_store); } void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) { as_array ().qsort (start, end); } @@ -941,7 +945,7 @@ struct SortedArrayOf : ArrayOf { return *as_array ().bsearch (x, ¬_found); } template bool bfind (const T &x, unsigned int *i = nullptr, - hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + hb_not_found_t not_found = HB_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 187a8816c..5da487dd2 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -415,7 +415,7 @@ struct RecordArrayOf : SortedArray16Of> } bool find_index (hb_tag_t tag, unsigned int *index) const { - return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); + return this->bfind (tag, index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); } }; @@ -1393,7 +1393,7 @@ struct CoverageFormat1 unsigned int get_coverage (hb_codepoint_t glyph_id) const { unsigned int i; - glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED); + glyphArray.bfind (glyph_id, &i, HB_NOT_FOUND_STORE, NOT_COVERED); return i; } diff --git a/src/hb-set.hh b/src/hb-set.hh index 4356a81c4..4ee6386b4 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -758,7 +758,7 @@ struct hb_set_t if (unlikely (i >= page_map.length || page_map_array[i].major != major)) { - page_map.bfind (major, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST); + page_map.bfind (major, &i, HB_NOT_FOUND_STORE_CLOSEST); if (i >= page_map.length) { *codepoint = INVALID; return false; @@ -802,7 +802,7 @@ struct hb_set_t page_map_t map = {get_major (*codepoint), 0}; unsigned int i; - page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST); + page_map.bfind (map, &i, HB_NOT_FOUND_STORE_CLOSEST); if (i < page_map.length && page_map[i].major == map.major) { if (pages[page_map[i].index].previous (codepoint)) @@ -933,7 +933,7 @@ struct hb_set_t { page_map_t map = {get_major (g), pages.length}; unsigned int i; - if (!page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST)) + if (!page_map.bfind (map, &i, HB_NOT_FOUND_STORE_CLOSEST)) { if (!resize (pages.length + 1)) return nullptr; diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 9f55fd378..110d457ca 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -315,7 +315,7 @@ struct hb_sorted_vector_t : hb_vector_t { return as_array ().bsearch (x, not_found); } template bool bfind (const T &x, unsigned int *i = nullptr, - hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, unsigned int to_store = (unsigned int) -1) const { return as_array ().bfind (x, i, not_found, to_store); } };