diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 94fd8a0f9..ad2e45e3c 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -607,9 +607,10 @@ struct hb_concat_iter_t : __item_t__ __item_at__ (unsigned i) const { - if (i < a.len()) + unsigned a_len = a.len (); + if (i < a_len) return a[i]; - return b[i - a.len()]; + return b[i - a_len]; } bool __more__ () const { return bool (a) || bool (b); } @@ -626,9 +627,18 @@ struct hb_concat_iter_t : void __forward__ (unsigned n) { - if (n > a.len ()) { - n -= a.len (); - a.__forward__ (a.len ()); + if (!n) return; + if (!is_random_access_iterator) { + while (n-- && *this) { + (*this)++; + } + return; + } + + unsigned a_len = a.len (); + if (n > a_len) { + n -= a_len; + a.__forward__ (a_len); b.__forward__ (n); } else { a.__forward__ (n); diff --git a/src/test-iter.cc b/src/test-iter.cc index 6182eb1e8..dc85b7214 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -133,6 +133,7 @@ static void test_concat () auto it1 = hb_concat (a, b); assert (it1.len () == 5); + assert (it1.is_random_access_iterator); auto it2 = hb_concat (c, d); assert (it2.len () == 5); auto it3 = hb_concat (d, c); @@ -148,6 +149,9 @@ static void test_concat () check_sequential (it3); auto it4 = +it1; + it4 += 0; + assert (*it4 == 1); + it4 += 2; assert (*it4 == 3); assert (it4); @@ -165,6 +169,21 @@ static void test_concat () auto it5 = +it1; it5 += 3; assert (*it5 == 4); + + hb_set_t s_a = {1, 2, 3}; + hb_set_t s_b = {4, 5}; + auto it6 = hb_concat (s_a, s_b); + assert (!it6.is_random_access_iterator); + check_sequential (it6); + assert (it6.len () == 5); + + it6 += 0; + assert (*it6 == 1); + + it6 += 3; + assert (*it6 == 4); + assert (it6); + assert (it6.len () == 2); } int