From b1c366a143eb1db68b46cb235e43761dfbb93745 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 11 Dec 2008 11:59:50 +0000 Subject: [PATCH] Add Text::get_chars() implementation and tests The clutter_text_get_chars() function returns a section of the contents of the Text actor, delimited by a start and an end position. This commit adds the implementation for that function and a test unit that guarantees the offset-to-bytes computations are correct. --- clutter/clutter-text.c | 35 ++++++++++++++++++++++++++++++- tests/conform/test-clutter-text.c | 31 +++++++++++++++++++++++++++ tests/conform/test-conform-main.c | 7 +------ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/clutter/clutter-text.c b/clutter/clutter-text.c index db9cf469a..678defbca 100644 --- a/clutter/clutter-text.c +++ b/clutter/clutter-text.c @@ -2998,14 +2998,47 @@ clutter_text_delete_chars (ClutterText *self, g_object_notify (G_OBJECT (self), "text"); } +/** + * clutter_text_get_chars: + * @self: a #ClutterTex + * @start_pos: start of text, in characters + * @end_pos: end of text, in characters + * + * Retrieves the contents of the #ClutterText actor between + * @start_pos and @end_pos. + * + * The positions are specified in characters, not in bytes. + * + * Return value: a newly allocated string with the contents of + * the text actor between the specified positions. Use g_free() + * to free the resources when done + * + * Since: 1.0 + */ gchar * clutter_text_get_chars (ClutterText *self, gssize start_pos, gssize end_pos) { + ClutterTextPrivate *priv; + gint start_index, end_index; + g_return_val_if_fail (CLUTTER_IS_TEXT (self), NULL); - return NULL; + priv = self->priv; + + if (end_pos < 0) + end_pos = priv->n_chars; + + start_pos = MIN (priv->n_chars, start_pos); + end_pos = MIN (priv->n_chars, end_pos); + + start_index = g_utf8_offset_to_pointer (priv->text, start_pos) + - priv->text; + end_index = g_utf8_offset_to_pointer (priv->text, end_pos) + - priv->text; + + return g_strndup (priv->text + start_index, end_index - start_index); } void diff --git a/tests/conform/test-clutter-text.c b/tests/conform/test-clutter-text.c index e6662182a..b47002a40 100644 --- a/tests/conform/test-clutter-text.c +++ b/tests/conform/test-clutter-text.c @@ -238,6 +238,37 @@ test_text_delete_chars (TestConformSimpleFixture *fixture, clutter_actor_destroy (CLUTTER_ACTOR (text)); } +void +test_text_get_chars (TestConformSimpleFixture *fixture, + gconstpointer data) +{ + ClutterText *text = CLUTTER_TEXT (clutter_text_new ()); + gchar *chars; + + clutter_text_set_text (text, "00abcdef11"); + g_assert_cmpint (get_nchars (text), ==, 10); + g_assert_cmpint (get_nbytes (text), ==, 10); + g_assert_cmpstr (clutter_text_get_text (text), ==, "00abcdef11"); + + chars = clutter_text_get_chars (text, 2, -1); + g_assert_cmpstr (chars, ==, "abcdef11"); + g_free (chars); + + chars = clutter_text_get_chars (text, 0, 8); + g_assert_cmpstr (chars, ==, "00abcdef"); + g_free (chars); + + chars = clutter_text_get_chars (text, 2, 8); + g_assert_cmpstr (chars, ==, "abcdef"); + g_free (chars); + + chars = clutter_text_get_chars (text, 8, 12); + g_assert_cmpstr (chars, ==, "11"); + g_free (chars); + + clutter_actor_destroy (CLUTTER_ACTOR (text)); +} + void test_text_delete_text (TestConformSimpleFixture *fixture, gconstpointer data) diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c index 9fb08c58d..d12c4b83a 100644 --- a/tests/conform/test-conform-main.c +++ b/tests/conform/test-conform-main.c @@ -75,14 +75,11 @@ main (int argc, char **argv) TEST_CONFORM_SIMPLE ("/entry", test_entry_empty); TEST_CONFORM_SIMPLE ("/entry", test_entry_set_empty); TEST_CONFORM_SIMPLE ("/entry", test_entry_set_text); - TEST_CONFORM_SIMPLE ("/entry", test_entry_append_some); TEST_CONFORM_SIMPLE ("/entry", test_entry_prepend_some); TEST_CONFORM_SIMPLE ("/entry", test_entry_insert); - TEST_CONFORM_SIMPLE ("/entry", test_entry_delete_chars); TEST_CONFORM_SIMPLE ("/entry", test_entry_delete_text); - TEST_CONFORM_SIMPLE ("/entry", test_entry_cursor); TEST_CONFORM_SIMPLE ("/entry", test_entry_event); @@ -91,16 +88,14 @@ main (int argc, char **argv) TEST_CONFORM_SIMPLE ("/text", test_text_empty); TEST_CONFORM_SIMPLE ("/text", test_text_set_empty); TEST_CONFORM_SIMPLE ("/text", test_text_set_text); - TEST_CONFORM_SIMPLE ("/text", test_text_append_some); TEST_CONFORM_SIMPLE ("/text", test_text_prepend_some); TEST_CONFORM_SIMPLE ("/text", test_text_insert); - TEST_CONFORM_SIMPLE ("/text", test_text_delete_chars); TEST_CONFORM_SIMPLE ("/text", test_text_delete_text); - TEST_CONFORM_SIMPLE ("/text", test_text_cursor); TEST_CONFORM_SIMPLE ("/text", test_text_event); + TEST_CONFORM_SIMPLE ("/text", test_text_get_chars); TEST_CONFORM_SIMPLE ("/rectangle", test_rect_set_size); TEST_CONFORM_SIMPLE ("/rectangle", test_rect_set_color);