[gtksourceview] view: fix and simplify get_leading_trailing()



commit 4ede5f063df400500d4e7bb7e391aac4263e4f62
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Aug 16 14:50:15 2015 +0200

    view: fix and simplify get_leading_trailing()
    
    The trailing iter was not set correctly, it was set one character too
    far on the left. The _previous_ character needs to be checked, since the
    character pointed to by an iter is the character on the right of the
    iter location.
    
    There was other problems in the implementation, like re-using the
    "start" variable for... the end of the line. Or hiding the iteration
    operation of the loops at the end of the long conditions. The code was
    not really clear, and as a proof of that, there was a bug.

 gtksourceview/gtksourceview.c |   48 +++++++++++++++++++++-------------------
 1 files changed, 25 insertions(+), 23 deletions(-)
---
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 2b1c61d..dc1d958 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -2466,49 +2466,51 @@ space_needs_drawing (GtkSourceView *view,
 }
 
 static void
-get_leading_trailing (GtkTextIter *iter,
-                     GtkTextIter *leading,
-                     GtkTextIter *trailing)
+get_leading_trailing (const GtkTextIter *iter,
+                     GtkTextIter       *leading,
+                     GtkTextIter       *trailing)
 {
-       GtkTextIter start;
+       g_assert (leading != NULL);
+       g_assert (trailing != NULL);
 
        /* Find end of leading */
-       start = *iter;
-       gtk_text_iter_set_line_offset (&start, 0);
+       *leading = *iter;
+       gtk_text_iter_set_line_offset (leading, 0);
 
-       while (TRUE)
+       while (!gtk_text_iter_is_end (leading))
        {
-               gunichar ch = gtk_text_iter_get_char (&start);
+               gunichar ch = gtk_text_iter_get_char (leading);
 
-               if (!g_unichar_isspace (ch) ||
-                   gtk_text_iter_ends_line (&start) ||
-                   !gtk_text_iter_forward_char (&start))
+               if (!g_unichar_isspace (ch))
                {
-                       *leading = start;
                        break;
                }
+
+               gtk_text_iter_forward_char (leading);
        }
 
        /* Find start of trailing */
-       start = *iter;
-       if (!gtk_text_iter_ends_line (&start))
+       *trailing = *iter;
+       if (!gtk_text_iter_ends_line (trailing))
        {
-               gtk_text_iter_forward_to_line_end (&start);
+               gtk_text_iter_forward_to_line_end (trailing);
        }
 
-       while (TRUE)
+       while (!gtk_text_iter_starts_line (trailing))
        {
-               gunichar ch = gtk_text_iter_get_char (&start);
+               GtkTextIter prev;
+               gunichar ch;
+
+               prev = *trailing;
+               gtk_text_iter_backward_char (&prev);
 
-               /* NOTE: ch can be 0 when iter is at the end
-                  of the buffer */
-               if (!(g_unichar_isspace (ch) || ch == 0) ||
-                    gtk_text_iter_starts_line (&start) ||
-                   !gtk_text_iter_backward_char (&start))
+               ch = gtk_text_iter_get_char (&prev);
+               if (!g_unichar_isspace (ch))
                {
-                       *trailing = start;
                        break;
                }
+
+               *trailing = prev;
        }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]