Merge pull request #2012 from harfbuzz/lepcha-oo

Reorder U+1C29 LEPCHA VOWEL SIGN OO
diff --git a/src/hb-algs.hh b/src/hb-algs.hh
index c57481b..042e1c2 100644
--- a/src/hb-algs.hh
+++ b/src/hb-algs.hh
@@ -588,10 +588,18 @@
   /* It's illegal to pass NULL to memcmp(), even if len is zero.
    * So, wrap it.
    * https://sourceware.org/bugzilla/show_bug.cgi?id=23878 */
-  if (!len) return 0;
+  if (unlikely (!len)) return 0;
   return memcmp (a, b, len);
 }
 
+static inline void *
+hb_memset (void *s, int c, unsigned int n)
+{
+  /* It's illegal to pass NULL to memset(), even if n is zero. */
+  if (unlikely (!n)) return 0;
+  return memset (s, c, n);
+}
+
 static inline bool
 hb_unsigned_mul_overflows (unsigned int count, unsigned int size)
 {
diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc
index bb7f3c7..40ac55c 100644
--- a/src/hb-buffer.cc
+++ b/src/hb-buffer.cc
@@ -324,8 +324,7 @@
   out_len = 0;
   out_info = info;
 
-  if (likely (len))
-    memset (pos, 0, sizeof (pos[0]) * len);
+  hb_memset (pos, 0, sizeof (pos[0]) * len);
 }
 
 void
diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh
index 051bfda..f2f3b05 100644
--- a/src/hb-ot-layout-common.hh
+++ b/src/hb-ot-layout-common.hh
@@ -66,6 +66,60 @@
 
 #define NOT_COVERED		((unsigned int) -1)
 
+template<typename OutputArray>
+struct subset_offset_array_t
+{
+  subset_offset_array_t
+  (hb_subset_context_t *subset_context,
+   OutputArray& out,
+   const void *src_base,
+   const void *dest_base)
+      : _subset_context(subset_context), _out (out), _src_base (src_base), _dest_base (dest_base) {}
+
+  template <typename T>
+  bool
+  operator ()
+  (T&& offset)
+  {
+    auto *o = _out.serialize_append (_subset_context->serializer);
+    if (unlikely (!o)) return false;
+    auto snap = _subset_context->serializer->snapshot ();
+    bool ret = o->serialize_subset (_subset_context, offset, _src_base, _dest_base);
+    if (!ret)
+    {
+      _out.pop ();
+      _subset_context->serializer->revert (snap);
+    }
+    return ret;
+  }
+
+  private:
+  hb_subset_context_t *_subset_context;
+  OutputArray &_out;
+  const void *_src_base;
+  const void *_dest_base;
+};
+
+/*
+ * Helper to subset an array of offsets. Subsets the thing pointed to by each offset
+ * and discards the offset in the array if the subset operation results in an empty
+ * thing.
+ */
+struct
+{
+  template<typename OutputArray>
+  subset_offset_array_t<OutputArray>
+  operator ()
+  (hb_subset_context_t *subset_context,
+   OutputArray& out,
+   const void *src_base,
+   const void *dest_base) const
+  {
+    return subset_offset_array_t<OutputArray> (subset_context, out, src_base, dest_base);
+  }
+}
+HB_FUNCOBJ (subset_offset_array);
+
 
 /*
  *
diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh
index a2722ad..fc21cb0 100644
--- a/src/hb-ot-layout-gsub-table.hh
+++ b/src/hb-ot-layout-gsub-table.hh
@@ -452,20 +452,7 @@
     hb_sorted_vector_t<hb_codepoint_t> new_coverage;
     + hb_zip (this+coverage, sequence)
     | hb_filter (glyphset, hb_first)
-    | hb_filter ([this, c, out] (const OffsetTo<Sequence>& _)
-		 {
-		   auto *o = out->sequence.serialize_append (c->serializer);
-		   if (unlikely (!o)) return false;
-		   auto snap = c->serializer->snapshot ();
-		   bool ret = o->serialize_subset (c, _, this, out);
-		   if (!ret)
-		   {
-		     out->sequence.pop ();
-		     c->serializer->revert (snap);
-		   }
-		   return ret;
-		 },
-		 hb_second)
+    | hb_filter (subset_offset_array (c, out->sequence, this, out), hb_second)
     | hb_map (hb_first)
     | hb_map (glyph_map)
     | hb_sink (new_coverage)
@@ -675,20 +662,7 @@
     hb_sorted_vector_t<hb_codepoint_t> new_coverage;
     + hb_zip (this+coverage, alternateSet)
     | hb_filter (glyphset, hb_first)
-    | hb_filter ([this, c, out] (const OffsetTo<AlternateSet>& _)
-		 {
-		   auto *o = out->alternateSet.serialize_append (c->serializer);
-		   if (unlikely (!o)) return false;
-		   auto snap = c->serializer->snapshot ();
-		   bool ret = o->serialize_subset (c, _, this, out);
-		   if (!ret)
-		   {
-		     out->alternateSet.pop ();
-		     c->serializer->revert (snap);
-		   }
-		   return ret;
-		 },
-		 hb_second)
+    | hb_filter (subset_offset_array (c, out->alternateSet, this, out), hb_second)
     | hb_map (hb_first)
     | hb_map (glyph_map)
     | hb_sink (new_coverage)
@@ -948,19 +922,7 @@
     if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
 
     + hb_iter (ligature)
-    | hb_filter ([this, c, out] (const OffsetTo<Ligature>& _)
-		 {
-		   auto *o = out->ligature.serialize_append (c->serializer);
-		   if (unlikely (!o)) return false;
-		   auto snap = c->serializer->snapshot ();
-		   bool ret = o->serialize_subset (c, _, this, out);
-		   if (!ret)
-		   {
-		     out->ligature.pop ();
-		     c->serializer->revert (snap);
-		   }
-		   return ret;
-		 })
+    | hb_filter (subset_offset_array (c, out->ligature, this, out))
     | hb_drain
     ;
     return_trace (bool (out->ligature));
@@ -1074,20 +1036,7 @@
     hb_sorted_vector_t<hb_codepoint_t> new_coverage;
     + hb_zip (this+coverage, ligatureSet)
     | hb_filter (glyphset, hb_first)
-    | hb_filter ([this, c, out] (const OffsetTo<LigatureSet>& _)
-		 {
-		   auto *o = out->ligatureSet.serialize_append (c->serializer);
-		   if (unlikely (!o)) return false;
-		   auto snap = c->serializer->snapshot ();
-		   bool ret = o->serialize_subset (c, _, this, out);
-		   if (!ret)
-		   {
-		     out->ligatureSet.pop ();
-		     c->serializer->revert (snap);
-		   }
-		   return ret;
-		 },
-		 hb_second)
+    | hb_filter (subset_offset_array (c, out->ligatureSet, this, out), hb_second)
     | hb_map (hb_first)
     | hb_map (glyph_map)
     | hb_sink (new_coverage)