Speed this function up, using the fact that visibility is constant across

2005-12-12  Matthias Clasen  <mclasen@redhat.com>

	* gtk/gtktextiter.c (gtk_text_iter_set_visible_line_index):
	Speed this function up, using the fact that visibility is
	constant across segments. (#321548, Paolo Borelli)
This commit is contained in:
Matthias Clasen 2005-12-12 19:33:23 +00:00 committed by Matthias Clasen
parent 104772f87a
commit a1577e964c
3 changed files with 46 additions and 30 deletions

View File

@ -1,5 +1,9 @@
2005-12-12 Matthias Clasen <mclasen@redhat.com> 2005-12-12 Matthias Clasen <mclasen@redhat.com>
* gtk/gtktextiter.c (gtk_text_iter_set_visible_line_index):
Speed this function up, using the fact that visibility is
constant across segments. (#321548, Paolo Borelli)
* gtk/gtktextbtree.c (_gtk_text_line_char_to_byte_offsets): * gtk/gtktextbtree.c (_gtk_text_line_char_to_byte_offsets):
* gtk/gtktextiter.c (gtk_text_iter_backward_chars): Speed up * gtk/gtktextiter.c (gtk_text_iter_backward_chars): Speed up
stepping backwards. (#320638, Larry Ewing, Paolo Borelli) stepping backwards. (#320638, Larry Ewing, Paolo Borelli)

View File

@ -1,5 +1,9 @@
2005-12-12 Matthias Clasen <mclasen@redhat.com> 2005-12-12 Matthias Clasen <mclasen@redhat.com>
* gtk/gtktextiter.c (gtk_text_iter_set_visible_line_index):
Speed this function up, using the fact that visibility is
constant across segments. (#321548, Paolo Borelli)
* gtk/gtktextbtree.c (_gtk_text_line_char_to_byte_offsets): * gtk/gtktextbtree.c (_gtk_text_line_char_to_byte_offsets):
* gtk/gtktextiter.c (gtk_text_iter_backward_chars): Speed up * gtk/gtktextiter.c (gtk_text_iter_backward_chars): Speed up
stepping backwards. (#320638, Larry Ewing, Paolo Borelli) stepping backwards. (#320638, Larry Ewing, Paolo Borelli)

View File

@ -3890,12 +3890,6 @@ gtk_text_iter_set_visible_line_offset (GtkTextIter *iter,
gtk_text_iter_forward_line (iter); gtk_text_iter_forward_line (iter);
} }
static gint
bytes_in_char (GtkTextIter *iter)
{
return g_unichar_to_utf8 (gtk_text_iter_get_char (iter), NULL);
}
/** /**
* gtk_text_iter_set_visible_line_index: * gtk_text_iter_set_visible_line_index:
* @iter: a #GtkTextIter * @iter: a #GtkTextIter
@ -3906,40 +3900,54 @@ bytes_in_char (GtkTextIter *iter)
* in the index. * in the index.
**/ **/
void void
gtk_text_iter_set_visible_line_index (GtkTextIter *iter, gtk_text_iter_set_visible_line_index (GtkTextIter *iter,
gint byte_on_line) gint byte_on_line)
{ {
gint bytes_seen = 0; GtkTextRealIter *real;
gint skipped = 0; gint bytes_in_line = 0;
gint offset = 0;
GtkTextIter pos; GtkTextIter pos;
GtkTextLineSegment *seg;
g_return_if_fail (iter != NULL); g_return_if_fail (iter != NULL);
gtk_text_iter_set_line_offset (iter, 0); gtk_text_iter_set_line_offset (iter, 0);
bytes_in_line = gtk_text_iter_get_bytes_in_line (iter);
pos = *iter; pos = *iter;
/* For now we use a ludicrously slow implementation */ real = gtk_text_iter_make_real (&pos);
while (bytes_seen < byte_on_line)
if (real == NULL)
return;
ensure_byte_offsets (real);
check_invariants (&pos);
seg = _gtk_text_iter_get_indexable_segment (&pos);
while (seg != NULL && byte_on_line > 0)
{ {
if (!_gtk_text_btree_char_is_invisible (&pos)) if (!_gtk_text_btree_char_is_invisible (&pos))
bytes_seen += bytes_in_char (&pos); {
else skipped++; if (byte_on_line < seg->byte_count)
{
iter_set_from_byte_offset (real, real->line, offset + byte_on_line);
byte_on_line = 0;
break;
}
else
byte_on_line -= seg->byte_count;
}
if (!gtk_text_iter_forward_char (&pos)) offset += seg->byte_count;
break; _gtk_text_iter_forward_indexable_segment (&pos);
seg = _gtk_text_iter_get_indexable_segment (&pos);
if (bytes_seen >= byte_on_line)
break;
} }
if (bytes_seen > byte_on_line) if (byte_on_line == 0)
g_warning ("%s: Incorrect visible byte index %d falls in the middle of a UTF-8 "
"character; this will crash the text buffer. "
"Byte indexes must refer to the start of a character.",
G_STRLOC, byte_on_line);
if (_gtk_text_iter_get_text_line (&pos) == _gtk_text_iter_get_text_line (iter))
*iter = pos; *iter = pos;
else else
gtk_text_iter_forward_line (iter); gtk_text_iter_forward_line (iter);