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
This commit is contained in:
Matthias Clasen 2020-10-03 13:30:42 -04:00
parent d0b7a4a4ec
commit ac79c0e799
3 changed files with 77 additions and 63 deletions

View File

@ -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,10 +3250,14 @@ 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
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;
}
/**
@ -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,10 +3300,14 @@ 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
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;
}
/**
@ -3306,16 +3318,18 @@ gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
*
* 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);
}
/**
@ -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,

View File

@ -369,12 +369,12 @@ 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,
gboolean 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,
gboolean gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
GtkTextIter *iter,
int line_number,
int byte_index);
@ -383,7 +383,7 @@ 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,
gboolean gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
GtkTextIter *iter,
int line_number);
GDK_AVAILABLE_IN_ALL

View File

@ -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() */