#include <gtk/gtk.h>

gint
main (gint argc, gchar *argv[])
{
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  gint i;
  GTimer *timer;

  gtk_init (&argc, &argv);

  buffer = gtk_text_buffer_new (NULL);

  gtk_text_buffer_get_start_iter (buffer, &iter);
  for (i = 0; i < 100000; i++)
    gtk_text_buffer_insert (buffer, &iter, "some text some text some text some text\n", -1);

  /* Before patch */

  timer = g_timer_new ();

  gtk_text_buffer_get_end_iter (buffer, &iter);

  while (gtk_text_iter_get_line (&iter) > 0)
    {
      gtk_text_iter_backward_line (&iter);

      if (!gtk_text_iter_ends_line (&iter))
        gtk_text_iter_forward_to_line_end (&iter);
    }

  g_timer_stop (timer);
  g_print ("Before patch: %lf seconds.\n",
           g_timer_elapsed (timer, NULL));

  /* After patch */

  g_timer_start (timer);

  gtk_text_buffer_get_end_iter (buffer, &iter);

  while (TRUE)
    {
      gtk_text_iter_set_line_offset (&iter, 0);

      if (gtk_text_iter_backward_line (&iter))
        {
          if (!gtk_text_iter_ends_line (&iter))
            gtk_text_iter_forward_to_line_end (&iter);
        }
      else
        {
          break;
        }
    }

  g_timer_stop (timer);
  g_print ("After patch: %lf seconds.\n",
           g_timer_elapsed (timer, NULL));

  g_object_unref (buffer);
  g_timer_destroy (timer);
  return 0;
}
