diff options
author | Matthias Clasen <mclasen@redhat.com> | 2020-10-03 13:30:42 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2020-10-03 14:29:07 -0400 |
commit | ac79c0e799457ac57cecfa38e8d45bef195b2cfd (patch) | |
tree | 6a68de56a72bdbcbbb96b656997bd886d1ef3c21 | |
parent | d0b7a4a4eccd2550d6647ee3162882c19e2fcae5 (diff) | |
download | gtk+-ac79c0e799457ac57cecfa38e8d45bef195b2cfd.tar.gz |
textbuffer: Improve some get_iter apis
Make these functions return FALSE if they did not
return the exact position that was requested.
Adapt tests.
Based on a patch by Sebastien Wilmet
Fixes: #506
-rw-r--r-- | gtk/gtktextbuffer.c | 66 | ||||
-rw-r--r-- | gtk/gtktextbuffer.h | 34 | ||||
-rw-r--r-- | testsuite/gtk/textbuffer.c | 46 |
3 files changed, 80 insertions, 66 deletions
diff --git a/gtk/gtktextbuffer.c b/gtk/gtktextbuffer.c index 121813c54e..2e846785e7 100644 --- a/gtk/gtktextbuffer.c +++ b/gtk/gtktextbuffer.c @@ -3224,8 +3224,10 @@ gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer, * If @line_number is greater than the number of lines * in the @buffer, the end iterator is returned. And if @char_offset is off the * end of the line, the iterator at the end of the line is returned. + * + * Returns: whether the exact position has been found **/ -void +gboolean gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, GtkTextIter *iter, int line_number, @@ -3233,13 +3235,13 @@ gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, { GtkTextIter end_line_iter; - g_return_if_fail (iter != NULL); - g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer)); + g_return_val_if_fail (iter != NULL, FALSE); + g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); if (line_number >= gtk_text_buffer_get_line_count (buffer)) { gtk_text_buffer_get_end_iter (buffer, iter); - return; + return FALSE; } _gtk_text_btree_get_iter_at_line_char (get_btree (buffer), iter, line_number, 0); @@ -3248,16 +3250,20 @@ gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, if (!gtk_text_iter_ends_line (&end_line_iter)) gtk_text_iter_forward_to_line_end (&end_line_iter); - if (char_offset <= gtk_text_iter_get_line_offset (&end_line_iter)) - gtk_text_iter_set_line_offset (iter, char_offset); - else - *iter = end_line_iter; + if (char_offset > gtk_text_iter_get_line_offset (&end_line_iter)) + { + *iter = end_line_iter; + return FALSE; + } + + gtk_text_iter_set_line_offset (iter, char_offset); + return TRUE; } /** * gtk_text_buffer_get_iter_at_line_index: - * @buffer: a #GtkTextBuffer - * @iter: (out): iterator to initialize + * @buffer: a #GtkTextBuffer + * @iter: (out): iterator to initialize * @line_number: line number counting from 0 * @byte_index: byte index from start of line * @@ -3268,8 +3274,10 @@ gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, * If @line_number is greater than the number of lines * in the @buffer, the end iterator is returned. And if @byte_index is off the * end of the line, the iterator at the end of the line is returned. + * + * Returns: whether the exact position has been found **/ -void +gboolean gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer, GtkTextIter *iter, int line_number, @@ -3277,13 +3285,13 @@ gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer, { GtkTextIter end_line_iter; - g_return_if_fail (iter != NULL); - g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer)); + g_return_val_if_fail (iter != NULL, FALSE); + g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); if (line_number >= gtk_text_buffer_get_line_count (buffer)) { gtk_text_buffer_get_end_iter (buffer, iter); - return; + return FALSE; } gtk_text_buffer_get_iter_at_line (buffer, iter, line_number); @@ -3292,35 +3300,41 @@ gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer, if (!gtk_text_iter_ends_line (&end_line_iter)) gtk_text_iter_forward_to_line_end (&end_line_iter); - if (byte_index <= gtk_text_iter_get_line_index (&end_line_iter)) - gtk_text_iter_set_line_index (iter, byte_index); - else - *iter = end_line_iter; + if (byte_index > gtk_text_iter_get_line_index (&end_line_iter)) + { + *iter = end_line_iter; + return FALSE; + } + + gtk_text_iter_set_line_index (iter, byte_index); + return TRUE; } /** * gtk_text_buffer_get_iter_at_line: - * @buffer: a #GtkTextBuffer + * @buffer: a #GtkTextBuffer * @iter: (out): iterator to initialize * @line_number: line number counting from 0 * * Initializes @iter to the start of the given line. If @line_number is greater * than the number of lines in the @buffer, the end iterator is returned. - **/ -void + * + * Returns: whether the exact position has been found + */ +gboolean gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer, GtkTextIter *iter, int line_number) { - g_return_if_fail (iter != NULL); - g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer)); + g_return_val_if_fail (iter != NULL, FALSE); + g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); - gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0); + return gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0); } /** * gtk_text_buffer_get_iter_at_offset: - * @buffer: a #GtkTextBuffer + * @buffer: a #GtkTextBuffer * @iter: (out): iterator to initialize * @char_offset: char offset from start of buffer, counting from 0, or -1 * @@ -3328,7 +3342,7 @@ gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer, * of the entire buffer. If @char_offset is -1 or greater than the number * of characters in the buffer, @iter is initialized to the end iterator, * the iterator one past the last valid character in the buffer. - **/ + */ void gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer, GtkTextIter *iter, diff --git a/gtk/gtktextbuffer.h b/gtk/gtktextbuffer.h index 1c382bb7c5..c084ff838b 100644 --- a/gtk/gtktextbuffer.h +++ b/gtk/gtktextbuffer.h @@ -369,23 +369,23 @@ GtkTextTag *gtk_text_buffer_create_tag (GtkTextBuffer *buffer, * iterator around using the GtkTextIter operators */ GDK_AVAILABLE_IN_ALL -void gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, - GtkTextIter *iter, - int line_number, - int char_offset); -GDK_AVAILABLE_IN_ALL -void gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer, - GtkTextIter *iter, - int line_number, - int byte_index); -GDK_AVAILABLE_IN_ALL -void gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer, - GtkTextIter *iter, - int char_offset); -GDK_AVAILABLE_IN_ALL -void gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer, - GtkTextIter *iter, - int line_number); +gboolean gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer, + GtkTextIter *iter, + int line_number, + int char_offset); +GDK_AVAILABLE_IN_ALL +gboolean gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer, + GtkTextIter *iter, + int line_number, + int byte_index); +GDK_AVAILABLE_IN_ALL +void gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer, + GtkTextIter *iter, + int char_offset); +GDK_AVAILABLE_IN_ALL +gboolean gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer, + GtkTextIter *iter, + int line_number); GDK_AVAILABLE_IN_ALL void gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer, GtkTextIter *iter); diff --git a/testsuite/gtk/textbuffer.c b/testsuite/gtk/textbuffer.c index b0a2e243c2..485571f568 100644 --- a/testsuite/gtk/textbuffer.c +++ b/testsuite/gtk/textbuffer.c @@ -1472,88 +1472,88 @@ test_get_iter (void) gtk_text_buffer_set_text (buffer, "ab\nßd\r\nef", -1); /* Test get_iter_at_line() */ - gtk_text_buffer_get_iter_at_line (buffer, &iter, 0); + g_assert (gtk_text_buffer_get_iter_at_line (buffer, &iter, 0)); g_assert (gtk_text_iter_is_start (&iter)); - gtk_text_buffer_get_iter_at_line (buffer, &iter, 1); + g_assert (gtk_text_buffer_get_iter_at_line (buffer, &iter, 1)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 3); - gtk_text_buffer_get_iter_at_line (buffer, &iter, 2); + g_assert (gtk_text_buffer_get_iter_at_line (buffer, &iter, 2)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 7); - gtk_text_buffer_get_iter_at_line (buffer, &iter, 3); + g_assert (!gtk_text_buffer_get_iter_at_line (buffer, &iter, 3)); g_assert (gtk_text_iter_is_end (&iter)); /* Test get_iter_at_line_offset() */ - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 0); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 0)); g_assert (gtk_text_iter_is_start (&iter)); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 1); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 1)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 1); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 2); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 2)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 2); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 3); + g_assert (!gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 0, 3)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 2); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 1, 1); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 1, 1)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 4); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 1); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 1)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 8); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 2); + g_assert (gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 2)); g_assert (gtk_text_iter_is_end (&iter)); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 3); + g_assert (!gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 2, 3)); g_assert (gtk_text_iter_is_end (&iter)); - gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 3, 1); + g_assert (!gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, 3, 1)); g_assert (gtk_text_iter_is_end (&iter)); /* Test get_iter_at_line_index() */ - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 0); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 0)); g_assert (gtk_text_iter_is_start (&iter)); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 1); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 1)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 1); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 2); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 2)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 2); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 3); + g_assert (!gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 0, 3)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 2); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 0); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 0)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 3); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 2); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 2)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 4); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 3); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 1, 3)); offset = gtk_text_iter_get_offset (&iter); g_assert_cmpint (offset, ==, 5); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 2, 2); + g_assert (gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 2, 2)); g_assert (gtk_text_iter_is_end (&iter)); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 2, 3); + g_assert (!gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 2, 3)); g_assert (gtk_text_iter_is_end (&iter)); - gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 3, 1); + g_assert (!gtk_text_buffer_get_iter_at_line_index (buffer, &iter, 3, 1)); g_assert (gtk_text_iter_is_end (&iter)); /* Test get_iter_at_offset() */ |