Set hb_buffer_t to use array_t.reverse().

This commit is contained in:
Garret Rieger 2020-02-26 15:09:04 -08:00
parent 5935a1dc0b
commit 14a7b6f1ab
4 changed files with 97 additions and 28 deletions

View File

@ -352,6 +352,10 @@ test_buffer_serialize_SOURCES = test-buffer-serialize.cc
test_buffer_serialize_CPPFLAGS = $(HBCFLAGS) test_buffer_serialize_CPPFLAGS = $(HBCFLAGS)
test_buffer_serialize_LDADD = libharfbuzz.la $(HBLIBS) 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_SOURCES = test-ot-meta.cc
test_ot_meta_CPPFLAGS = $(HBCFLAGS) test_ot_meta_CPPFLAGS = $(HBCFLAGS)
test_ot_meta_LDADD = libharfbuzz.la $(HBLIBS) 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_CPPFLAGS = $(HBCFLAGS)
dump_use_data_LDADD = libharfbuzz.la $(HBLIBS) 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_CPPFLAGS = $(HBCFLAGS) -DMAIN -UNDEBUG
COMPILED_TESTS_LDADD = libharfbuzz.la $(HBLIBS) COMPILED_TESTS_LDADD = libharfbuzz.la $(HBLIBS)
check_PROGRAMS += $(COMPILED_TESTS) check_PROGRAMS += $(COMPILED_TESTS)

View File

@ -171,18 +171,21 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
unsigned int get_size () const { return length * this->get_item_size (); } 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; start = hb_min (start, length);
int lhs = 0; end = hb_min (end, length);
while (rhs > lhs)
{ if (end < start + 2)
Type value_rhs = arrayZ[rhs]; return;
Type value_lhs = arrayZ[lhs];
arrayZ[rhs] = value_lhs; for (unsigned lhs = start, rhs = end - 1; lhs < rhs; lhs++, rhs--) {
arrayZ[lhs] = value_rhs; Type temp = arrayZ[rhs];
rhs--; arrayZ[rhs] = arrayZ[lhs];
lhs++; arrayZ[lhs] = temp;
} }
} }

View File

@ -448,27 +448,13 @@ void
hb_buffer_t::reverse_range (unsigned int start, hb_buffer_t::reverse_range (unsigned int start,
unsigned int end) unsigned int end)
{ {
unsigned int i, j;
if (end - start < 2) if (end - start < 2)
return; return;
for (i = start, j = end - 1; i < j; i++, j--) { hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
hb_glyph_info_t t;
t = info[i];
info[i] = info[j];
info[j] = t;
}
if (have_positions) { if (have_positions) {
for (i = start, j = end - 1; i < j; i++, j--) { hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
hb_glyph_position_t t;
t = pos[i];
pos[i] = pos[j];
pos[j] = t;
}
} }
} }

76
src/test-array.cc Normal file
View File

@ -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<int> a (values, 9);
a.reverse();
int expected_values[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
hb_array_t<int> 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<int> a (values, 9);
a.reverse(2, 6);
int expected_values[] = {1, 2, 6, 5, 4, 3, 7, 8, 9};
hb_array_t<int> 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<int> 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<int> expected (expected_values, 9);
assert (a == expected);
}
int
main (int argc, char **argv)
{
test_reverse ();
test_reverse_range ();
test_reverse_invalid ();
}