From 08d57d9eca94c3695a495be504e9e63b6ad1aa59 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sun, 28 Jun 2020 13:13:25 +0430 Subject: [PATCH] Add hb_array_t::lfind --- src/hb-array.hh | 25 ++++++++++++++++--------- src/hb-open-type.hh | 6 ++++++ src/hb-ot-stat-table.hh | 17 ++--------------- src/hb-ot-var-fvar-table.hh | 26 ++++++++------------------ src/hb-vector.hh | 3 +++ 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index 172d9c13c..568cd02c7 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -129,20 +129,27 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> template Type *lsearch (const T &x, Type *not_found = nullptr) { - unsigned int count = length; - for (unsigned int i = 0; i < count; i++) - if (!this->arrayZ[i].cmp (x)) - return &this->arrayZ[i]; - return not_found; + unsigned i; + return lfind (x, &i) ? &this->arrayZ[i] : not_found; } template const Type *lsearch (const T &x, const Type *not_found = nullptr) const { - unsigned int count = length; - for (unsigned int i = 0; i < count; i++) + unsigned i; + return lfind (x, &i) ? &this->arrayZ[i] : not_found; + } + template + bool lfind (const T &x, unsigned *pos = nullptr) const + { + for (unsigned i = 0; i < length; ++i) if (!this->arrayZ[i].cmp (x)) - return &this->arrayZ[i]; - return not_found; + { + if (pos) + *pos = i; + return true; + } + + return false; } hb_sorted_array_t qsort (int (*cmp_)(const void*, const void*)) diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index fee71df84..2bffbd7b0 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -442,6 +442,9 @@ struct UnsizedArrayOf template 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); } void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1) { as_array (len).qsort (start, end); } @@ -667,6 +670,9 @@ struct ArrayOf template 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); } void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) { as_array ().qsort (start, end); } diff --git a/src/hb-ot-stat-table.hh b/src/hb-ot-stat-table.hh index 655064bdf..6aa4fa449 100644 --- a/src/hb-ot-stat-table.hh +++ b/src/hb-ot-stat-table.hh @@ -292,28 +292,15 @@ struct STAT bool has_data () const { return version.to_int (); } - bool find_axis_index (hb_tag_t tag, unsigned int *axis_index) const - { - hb_array_t axes = get_design_axes (); - /* TODO: add lfind in hb_array_t and use it in here and fvar's find_axis_info */ - for (unsigned int i = 0; i < axes.length; i++) - if (!axes[i].cmp (tag)) - { - *axis_index = i; - return true; - } - return false; - } - bool get_value (hb_tag_t tag, float *value) const { unsigned int axis_index; - if (!find_axis_index (tag, &axis_index)) return false; + if (!get_design_axes ().lfind (tag, &axis_index)) return false; hb_array_t> axis_values = get_axis_value_offsets (); for (unsigned int i = 0; i < axis_values.length; i++) { - const AxisValue& axis_value = (this + axis_values[i]); + const AxisValue& axis_value = this+axis_values[i]; if (axis_value.get_axis_index () == axis_index) { if (value) diff --git a/src/hb-ot-var-fvar-table.hh b/src/hb-ot-var-fvar-table.hh index 9185f755c..1d1b9a3a3 100644 --- a/src/hb-ot-var-fvar-table.hh +++ b/src/hb-ot-var-fvar-table.hh @@ -70,6 +70,8 @@ struct InstanceRecord struct AxisRecord { + int cmp (hb_tag_t key) const { return axisTag.cmp (key); } + enum { AXIS_FLAG_HIDDEN = 0x0001, @@ -171,18 +173,11 @@ struct fvar bool find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const { + unsigned i; + if (!axis_index) axis_index = &i; + *axis_index = HB_OT_VAR_NO_AXIS_INDEX; hb_array_t axes = get_axes (); - for (unsigned i = 0; i < axes.length; i++) - if (axes[i].axisTag == tag) - { - if (axis_index) - *axis_index = i; - axes[i].get_axis_deprecated (info); - return true; - } - if (axis_index) - *axis_index = HB_OT_VAR_NO_AXIS_INDEX; - return false; + return axes.lfind (tag, axis_index) && (axes[*axis_index].get_axis_deprecated (info), true); } #endif @@ -190,13 +185,8 @@ struct fvar find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const { hb_array_t axes = get_axes (); - for (unsigned i = 0; i < axes.length; i++) - if (axes[i].axisTag == tag) - { - axes[i].get_axis_info (i, info); - return true; - } - return false; + unsigned i; + return axes.lfind (tag, &i) && (axes[i].get_axis_info (i, info), true); } int normalize_axis_value (unsigned int axis_index, float v) const diff --git a/src/hb-vector.hh b/src/hb-vector.hh index d979169de..079b94a6b 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -277,6 +277,9 @@ struct hb_vector_t template const Type *lsearch (const T &x, const Type *not_found = nullptr) const { return as_array ().lsearch (x, not_found); } + template + bool lfind (const T &x, unsigned *pos = nullptr) const + { return as_array ().lfind (x, pos); } }; template