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.
This commit is contained in:
Emmanuele Bassi 2008-12-11 11:59:50 +00:00
parent 2fedd3263c
commit b1c366a143
3 changed files with 66 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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);