[latexila] UTF-8 friendly: Utils.char_is_escaped ()



commit 731ee1ca789a01effe8ee53c27d6b5775899966b
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Sun Jul 24 00:51:32 2011 +0200

    UTF-8 friendly: Utils.char_is_escaped ()
    
    Some functions are not UTF-8 friendly. This is the first fixed.

 src/utils.vala |   38 +++++++++++++++++++++++++++++++++-----
 1 files changed, 33 insertions(+), 5 deletions(-)
---
diff --git a/src/utils.vala b/src/utils.vala
index 21265d2..c68bfb1 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -265,19 +265,47 @@ namespace Utils
         return button;
     }
 
-    public bool char_is_escaped (string text, long index)
+    public bool char_is_escaped (string text, long char_index)
     {
+        return_val_if_fail (char_index < text.length, false);
+
+        int index = (int) char_index;
+        if (! string_get_prev_char (text, ref index, null))
+            return false;
+
         bool escaped = false;
-        for (long i = index - 1 ; i >= 0 ; i--)
+        while (true)
         {
-            if (text[i] == '\\')
-                escaped = ! escaped;
-            else
+            unichar cur_char;
+            bool first_char = ! string_get_prev_char (text, ref index, out cur_char);
+
+            if (cur_char != '\\')
+                break;
+
+            escaped = ! escaped;
+
+            if (first_char)
                 break;
         }
+
         return escaped;
     }
 
+    // The opposite of string.get_next_char ().
+    // TODO remove this function when it is included upstream
+    // See https://bugzilla.gnome.org/show_bug.cgi?id=655185
+    private bool string_get_prev_char (string str, ref int index, out unichar c)
+    {
+        c = str.get_char (index);
+        if (index <= 0 || c == '\0')
+            return false;
+
+        unowned string str_at_index = (string) ((char*) str + index);
+        unowned string str_prev = str_at_index.prev_char ();
+        index = (int) ((char*) str_prev - (char*) str);
+        return true;
+    }
+
     // origin can be equal to common_dir, but target must be different
     public string? get_relative_path (File origin, File target, File common_dir)
     {



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