diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 81f8db958..84e80885e 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -203,7 +203,7 @@ struct hb_bit_set_t bool set_sorted_array (bool v, const T *array, unsigned int count, unsigned int stride=sizeof(T)) { if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */ - if (!count) return true; + if (unlikely (!count)) return true; dirty (); hb_codepoint_t g = *array; hb_codepoint_t last_g = g; @@ -222,7 +222,7 @@ struct hb_bit_set_t if (v || page) /* The v check is to optimize out the page check if v is true. */ page->add (g); - array = (const T *) ((const char *) array + stride); + array = &StructAtOffsetUnaligned (array, stride); count--; } while (count && (g = *array, g < end)); diff --git a/src/hb-set.cc b/src/hb-set.cc index 204dbb564..54c5b2136 100644 --- a/src/hb-set.cc +++ b/src/hb-set.cc @@ -61,6 +61,24 @@ hb_set_create () return set; } +/** + * Creates a set initially populated with num_codepoint codepoints, which must + * be in increasing order. + * + * Since: REPLACEME + */ +HB_EXTERN void +hb_set_add_sorted_array (hb_set_t *set, + const hb_codepoint_t *sorted_codepoints, + unsigned int num_codepoints) +{ + if (unlikely(!set || !sorted_codepoints || !num_codepoints)) return; + + set->add_sorted_array (sorted_codepoints, + num_codepoints, + sizeof(hb_codepoint_t)); +} + /** * hb_set_get_empty: * diff --git a/src/hb-set.h b/src/hb-set.h index 423225bf9..19374e022 100644 --- a/src/hb-set.h +++ b/src/hb-set.h @@ -60,6 +60,11 @@ typedef struct hb_set_t hb_set_t; HB_EXTERN hb_set_t * hb_set_create (void); +HB_EXTERN void +hb_set_add_sorted_array (hb_set_t *set, + const hb_codepoint_t *sorted_codepoints, + unsigned int num_codepoints); + HB_EXTERN hb_set_t * hb_set_get_empty (void); diff --git a/test/api/test-set.c b/test/api/test-set.c index f2f9419bc..4ac95d9bb 100644 --- a/test/api/test-set.c +++ b/test/api/test-set.c @@ -1067,6 +1067,23 @@ test_set_inverted_operations (void) g_assert (all_succeeded); } +static void +test_hb_set_add_sorted_array (void) +{ + hb_set_t *set = hb_set_create (); + hb_codepoint_t array[7] = {1, 2, 3, 1000, 2000, 2001, 2002}; + hb_set_add_sorted_array (set, array, 7); + g_assert_cmpint (hb_set_get_population (set), ==, 7); + g_assert (hb_set_has (set, 1)); + g_assert (hb_set_has (set, 2)); + g_assert (hb_set_has (set, 3)); + g_assert (hb_set_has (set, 1000)); + g_assert (hb_set_has (set, 2000)); + g_assert (hb_set_has (set, 2001)); + g_assert (hb_set_has (set, 2002)); + hb_set_destroy (set); +} + int main (int argc, char **argv) { @@ -1090,5 +1107,7 @@ main (int argc, char **argv) hb_test_add (test_set_inverted_equality); hb_test_add (test_set_inverted_operations); + hb_test_add (test_hb_set_add_sorted_array); + return hb_test_run(); }