[iter] Check for more before forwarding/rewinding past ends
authorBehdad Esfahbod <behdad@behdad.org>
Sat, 11 May 2019 18:54:30 +0000 (11:54 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Sat, 11 May 2019 18:56:26 +0000 (11:56 -0700)
src/hb-iter.hh
src/test-iter.cc

index 1b5c427..dcc49ee 100644 (file)
@@ -196,11 +196,11 @@ struct hb_iter_fallback_mixin_t
 
   /* Advancing: Implement __next__(), or __forward__() if random-access. */
   void __next__ () { *thiz() += 1; }
-  void __forward__ (unsigned n) { while (n--) ++*thiz(); }
+  void __forward__ (unsigned n) { while (*thiz() && n--) ++*thiz(); }
 
   /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
   void __prev__ () { *thiz() -= 1; }
-  void __rewind__ (unsigned n) { while (n--) --*thiz(); }
+  void __rewind__ (unsigned n) { while (*thiz() && n--) --*thiz(); }
 
   /* Range-based for: Implement __end__() if can be done faster,
    * and operator!=. */
index d9e2a97..f834640 100644 (file)
@@ -82,10 +82,8 @@ test_iterator_non_default_constructable (Iter it)
     (void) _;
 
   it += it.len ();
-  if (0)
-    it = it + 10;
-  if (0)
-    it = 10 + it;
+  it = it + 10;
+  it = 10 + it;
 
   assert (*it == it[0]);