From 14a7b6f1ab1926680369bf43efc9ee8d5f89402d Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 26 Feb 2020 15:09:04 -0800 Subject: [PATCH] Set hb_buffer_t to use array_t.reverse(). --- src/Makefile.am | 6 +++- src/hb-array.hh | 25 +++++++++------- src/hb-buffer.cc | 18 ++--------- src/test-array.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 src/test-array.cc diff --git a/src/Makefile.am b/src/Makefile.am index eb9551fc9..9d9767a38 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -352,6 +352,10 @@ test_buffer_serialize_SOURCES = test-buffer-serialize.cc test_buffer_serialize_CPPFLAGS = $(HBCFLAGS) test_buffer_serialize_LDADD = libharfbuzz.la $(HBLIBS) +test_array_SOURCES = test-array.cc +test_array_CPPFLAGS = $(HBCFLAGS) +test_array_LDADD = libharfbuzz.la $(HBLIBS) + test_ot_meta_SOURCES = test-ot-meta.cc test_ot_meta_CPPFLAGS = $(HBCFLAGS) test_ot_meta_LDADD = libharfbuzz.la $(HBLIBS) @@ -407,7 +411,7 @@ dump_use_data_SOURCES = dump-use-data.cc hb-ot-shape-complex-use-table.cc dump_use_data_CPPFLAGS = $(HBCFLAGS) dump_use_data_LDADD = libharfbuzz.la $(HBLIBS) -COMPILED_TESTS = test-algs test-iter test-meta test-number test-ot-tag test-unicode-ranges test-bimap +COMPILED_TESTS = test-algs test-array test-iter test-meta test-number test-ot-tag test-unicode-ranges test-bimap COMPILED_TESTS_CPPFLAGS = $(HBCFLAGS) -DMAIN -UNDEBUG COMPILED_TESTS_LDADD = libharfbuzz.la $(HBLIBS) check_PROGRAMS += $(COMPILED_TESTS) diff --git a/src/hb-array.hh b/src/hb-array.hh index c9c6dee9c..172d9c13c 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -171,18 +171,21 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> unsigned int get_size () const { return length * this->get_item_size (); } - void reverse () + /* + * Reverse the order of items in this array in the range [start, end). + */ + void reverse (unsigned start = 0, unsigned end = -1) { - int rhs = length - 1; - int lhs = 0; - while (rhs > lhs) - { - Type value_rhs = arrayZ[rhs]; - Type value_lhs = arrayZ[lhs]; - arrayZ[rhs] = value_lhs; - arrayZ[lhs] = value_rhs; - rhs--; - lhs++; + start = hb_min (start, length); + end = hb_min (end, length); + + if (end < start + 2) + return; + + for (unsigned lhs = start, rhs = end - 1; lhs < rhs; lhs++, rhs--) { + Type temp = arrayZ[rhs]; + arrayZ[rhs] = arrayZ[lhs]; + arrayZ[lhs] = temp; } } diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 7a953d75f..f49b35f2d 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -448,27 +448,13 @@ void hb_buffer_t::reverse_range (unsigned int start, unsigned int end) { - unsigned int i, j; - if (end - start < 2) return; - for (i = start, j = end - 1; i < j; i++, j--) { - hb_glyph_info_t t; - - t = info[i]; - info[i] = info[j]; - info[j] = t; - } + hb_array_t (info, len).reverse (start, end); if (have_positions) { - for (i = start, j = end - 1; i < j; i++, j--) { - hb_glyph_position_t t; - - t = pos[i]; - pos[i] = pos[j]; - pos[j] = t; - } + hb_array_t (pos, len).reverse (start, end); } } diff --git a/src/test-array.cc b/src/test-array.cc new file mode 100644 index 000000000..6c888138e --- /dev/null +++ b/src/test-array.cc @@ -0,0 +1,76 @@ +/* + * Copyright © 2020 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Garret Rieger + */ + +#include "hb.hh" +#include "hb-array.hh" + +static void +test_reverse () +{ + int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + hb_array_t a (values, 9); + a.reverse(); + + int expected_values[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; + hb_array_t expected (expected_values, 9); + assert (a == expected); +} + +static void +test_reverse_range () +{ + int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + hb_array_t a (values, 9); + a.reverse(2, 6); + + int expected_values[] = {1, 2, 6, 5, 4, 3, 7, 8, 9}; + hb_array_t expected (expected_values, 9); + assert (a == expected); +} + +static void +test_reverse_invalid () +{ + int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + hb_array_t a (values, 9); + + a.reverse(4, 3); + a.reverse(2, 3); + a.reverse(5, 5); + a.reverse(12, 15); + + int expected_values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + hb_array_t expected (expected_values, 9); + assert (a == expected); +} + +int +main (int argc, char **argv) +{ + test_reverse (); + test_reverse_range (); + test_reverse_invalid (); +}