From e8b45c19330d8718cd6d7aef0ca2db0539a53294 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 8 May 2019 16:37:38 -0700 Subject: [PATCH] [array] Add .copy() --- src/hb-array.hh | 11 +++++++++++ src/hb-open-type.hh | 10 ++++------ src/hb-serialize.hh | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index 2da8df0bb..8902e7aaf 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -176,6 +176,17 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> void free () { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; } + template + hb_array_t copy (hb_serialize_context_t *c) const + { + TRACE_SERIALIZE (this); + auto* out = c->template start_embed (arrayZ); + if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ()); + for (unsigned i = 0; i < length; i++) + out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */ + return_trace (hb_array_t (out, length)); + } + template bool sanitize (hb_sanitize_context_t *c) const { return c->check_array (arrayZ, length); } diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 9d388ff3f..cacb1a7b8 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -436,9 +436,7 @@ struct UnsizedArrayOf { TRACE_SERIALIZE (this); auto *out = c->start_embed (this); - if (unlikely (!out->serialize (c, count))) return_trace (nullptr); - for (unsigned i = 0; i < count; i++) - out->arrayZ[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */ + if (unlikely (!as_array (count).copy (c))) return_trace (nullptr); return_trace (out); } @@ -618,9 +616,9 @@ struct ArrayOf TRACE_SERIALIZE (this); auto *out = c->start_embed (this); unsigned count = len; - if (unlikely (!out->serialize (c, count))) return_trace (nullptr); - for (unsigned i = 0; i < count; i++) - out->arrayZ[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */ + if (unlikely (!c->extend_min (out))) return_trace (nullptr); + c->check_assign (out->len, len); + if (unlikely (!as_array ().copy (c))) return_trace (nullptr); return_trace (out); } diff --git a/src/hb-serialize.hh b/src/hb-serialize.hh index e0f79809f..76f701608 100644 --- a/src/hb-serialize.hh +++ b/src/hb-serialize.hh @@ -323,7 +323,7 @@ struct hb_serialize_context_t allocate_size (alignment - l); } - template + template Type *start_embed (const Type *obj HB_UNUSED = nullptr) const { return reinterpret_cast (this->head); } template