[gspell/wip/entry-context-menu: 1/2] EntryUtils: store character positions in GspellEntryWord



commit dfc743759ed48008af65c7a817fa20ccf8b2b36a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Nov 25 15:21:09 2016 +0100

    EntryUtils: store character positions in GspellEntryWord

 gspell/gspell-entry-utils.c |    5 ++++
 gspell/gspell-entry-utils.h |    6 +++++
 testsuite/test-entry.c      |   48 ++++++++++++++++++++++++++++++++++++------
 3 files changed, 52 insertions(+), 7 deletions(-)
---
diff --git a/gspell/gspell-entry-utils.c b/gspell/gspell-entry-utils.c
index 32f681c..fa53b79 100644
--- a/gspell/gspell-entry-utils.c
+++ b/gspell/gspell-entry-utils.c
@@ -48,6 +48,7 @@ _gspell_entry_utils_get_words (GtkEntry *entry)
        const gchar *text;
        const gchar *cur_text_pos;
        const gchar *word_start;
+       gint word_start_char_pos;
        const PangoLogAttr *attrs;
        gint n_attrs = 0;
        gint attr_num;
@@ -62,6 +63,7 @@ _gspell_entry_utils_get_words (GtkEntry *entry)
        attr_num = 0;
        cur_text_pos = text;
        word_start = NULL;
+       word_start_char_pos = 0;
 
        while (attr_num < n_attrs)
        {
@@ -83,6 +85,8 @@ _gspell_entry_utils_get_words (GtkEntry *entry)
                        word = _gspell_entry_word_new ();
                        word->byte_start = word_start - text;
                        word->byte_end = word_end - text;
+                       word->char_start = word_start_char_pos;
+                       word->char_end = attr_num;
                        word->word_str = g_strndup (word_start, word_end - word_start);
 
                        list = g_slist_prepend (list, word);
@@ -95,6 +99,7 @@ _gspell_entry_utils_get_words (GtkEntry *entry)
                    attrs[attr_num].is_word_start)
                {
                        word_start = cur_text_pos;
+                       word_start_char_pos = attr_num;
                }
 
                if (cur_text_pos == NULL &&
diff --git a/gspell/gspell-entry-utils.h b/gspell/gspell-entry-utils.h
index 3aa101e..813b5b2 100644
--- a/gspell/gspell-entry-utils.h
+++ b/gspell/gspell-entry-utils.h
@@ -34,6 +34,12 @@ struct _GspellEntryWord
         */
        gint byte_start;
        gint byte_end;
+
+       /* The same as @byte_start and @byte_end, but in characters.
+        * Useful for example for the #GtkEditable functions.
+        */
+       gint char_start;
+       gint char_end;
 };
 
 G_GNUC_INTERNAL
diff --git a/testsuite/test-entry.c b/testsuite/test-entry.c
index 9ac7ef1..c372893 100644
--- a/testsuite/test-entry.c
+++ b/testsuite/test-entry.c
@@ -63,6 +63,28 @@ add_word (GSList      *list,
        word->word_str = g_strdup (word_str);
        word->byte_start = byte_start;
        word->byte_end = byte_end;
+       word->char_start = -1;
+       word->char_end = -1;
+
+       return g_slist_append (list, word);
+}
+
+static GSList *
+add_word_full (GSList      *list,
+              const gchar *word_str,
+              gint         byte_start,
+              gint         byte_end,
+              gint         char_start,
+              gint         char_end)
+{
+       GspellEntryWord *word;
+
+       word = _gspell_entry_word_new ();
+       word->word_str = g_strdup (word_str);
+       word->byte_start = byte_start;
+       word->byte_end = byte_end;
+       word->char_start = char_start;
+       word->char_end = char_end;
 
        return g_slist_append (list, word);
 }
@@ -83,6 +105,18 @@ check_entry_word_equal (GspellEntryWord *word1,
        g_assert_cmpstr (word1->word_str, ==, word2->word_str);
        g_assert_cmpint (word1->byte_start, ==, word2->byte_start);
        g_assert_cmpint (word1->byte_end, ==, word2->byte_end);
+
+       if (word1->char_start != -1 &&
+           word2->char_start != -1)
+       {
+               g_assert_cmpint (word1->char_start, ==, word2->char_start);
+       }
+
+       if (word1->char_end != -1 &&
+           word2->char_end != -1)
+       {
+               g_assert_cmpint (word1->char_end, ==, word2->char_end);
+       }
 }
 
 static void
@@ -122,7 +156,7 @@ test_get_words (void)
 
        /* Only one word */
        gtk_entry_set_text (entry, "Finntroll");
-       expected_list = add_word (NULL, "Finntroll", 0, 9);
+       expected_list = add_word_full (NULL, "Finntroll", 0, 9, 0, 9);
        received_list = _gspell_entry_utils_get_words (entry);
        check_entry_word_list_equal (expected_list, received_list);
        free_word_list (expected_list);
@@ -130,7 +164,7 @@ test_get_words (void)
 
        /* Only one word, not at the start and end */
        gtk_entry_set_text (entry, " Finntroll ");
-       expected_list = add_word (NULL, "Finntroll", 1, 10);
+       expected_list = add_word_full (NULL, "Finntroll", 1, 10, 1, 10);
        received_list = _gspell_entry_utils_get_words (entry);
        check_entry_word_list_equal (expected_list, received_list);
        free_word_list (expected_list);
@@ -138,9 +172,9 @@ test_get_words (void)
 
        /* Several words */
        gtk_entry_set_text (entry, "Finntroll - Svart Djup");
-       expected_list = add_word (NULL, "Finntroll", 0, 9);
-       expected_list = add_word (expected_list, "Svart", 12, 17);
-       expected_list = add_word (expected_list, "Djup", 18, 22);
+       expected_list = add_word_full (NULL, "Finntroll", 0, 9, 0, 9);
+       expected_list = add_word_full (expected_list, "Svart", 12, 17, 12, 17);
+       expected_list = add_word_full (expected_list, "Djup", 18, 22, 18, 22);
        received_list = _gspell_entry_utils_get_words (entry);
        check_entry_word_list_equal (expected_list, received_list);
        free_word_list (expected_list);
@@ -150,8 +184,8 @@ test_get_words (void)
        // å takes two bytes.
        // ö takes two bytes.
        gtk_entry_set_text (entry, "Asfågelns Död");
-       expected_list = add_word (NULL, "Asfågelns", 0, 10);
-       expected_list = add_word (expected_list, "Död", 11, 15);
+       expected_list = add_word_full (NULL, "Asfågelns", 0, 10, 0, 9);
+       expected_list = add_word_full (expected_list, "Död", 11, 15, 10, 13);
        received_list = _gspell_entry_utils_get_words (entry);
        check_entry_word_list_equal (expected_list, received_list);
        free_word_list (expected_list);


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