From 6c04dcb28dcafc7d97799c80c0bc714c76d51d1c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 31 Oct 2017 20:11:00 -0600 Subject: [PATCH] Use bsearch() for large SFNT table directories --- src/hb-open-file-private.hh | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/hb-open-file-private.hh b/src/hb-open-file-private.hh index 77caddd27..a6d5a6b15 100644 --- a/src/hb-open-file-private.hh +++ b/src/hb-open-file-private.hh @@ -53,6 +53,9 @@ struct TTCHeader; typedef struct TableRecord { + int cmp (Tag t) const + { return t.cmp (tag); } + inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -100,18 +103,12 @@ typedef struct OffsetTable { Tag t; t.set (tag); - unsigned int count = tables.len; - /* TODO bsearch() */ - for (unsigned int i = 0; i < count; i++) - { - if (t == tables.array[i].tag) - { - if (table_index) *table_index = i; - return true; - } - } - if (table_index) *table_index = Index::NOT_FOUND_INDEX; - return false; + /* Linear-search for small tables to work around fonts with unsorted + * table list. */ + int i = tables.len < 64 ? tables.lsearch (t) : tables.bsearch (t); + if (table_index) + *table_index = i == -1 ? Index::NOT_FOUND_INDEX : (unsigned int) i; + return i != -1; } inline const TableRecord& get_table_by_tag (hb_tag_t tag) const {