[gspell] checker: add string length parameter to check_word()



commit cceb35e0a62ab0c10b0cda25414f0d2c0a841e84
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Nov 22 18:01:43 2015 +0100

    checker: add string length parameter to check_word()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=758455

 gspell/gspell-checker-dialog.c     |    4 ++--
 gspell/gspell-checker.c            |   21 ++++++++++++++++++---
 gspell/gspell-checker.h            |    1 +
 gspell/gspell-inline-checker-gtv.c |    2 +-
 gspell/gspell-navigator-gtv.c      |    2 +-
 gspell/gspell-utils.c              |   11 +++++++++--
 gspell/gspell-utils.h              |    3 ++-
 7 files changed, 34 insertions(+), 10 deletions(-)
---
diff --git a/gspell/gspell-checker-dialog.c b/gspell/gspell-checker-dialog.c
index 720e6b1..ab23b8f 100644
--- a/gspell/gspell-checker-dialog.c
+++ b/gspell/gspell-checker-dialog.c
@@ -184,7 +184,7 @@ set_misspelled_word (GspellCheckerDialog *dialog,
 
        priv = gspell_checker_dialog_get_instance_private (dialog);
 
-       g_return_if_fail (!gspell_checker_check_word (priv->checker, word, NULL));
+       g_return_if_fail (!gspell_checker_check_word (priv->checker, word, -1, NULL));
 
        g_free (priv->misspelled_word);
        priv->misspelled_word = g_strdup (word);
@@ -475,7 +475,7 @@ check_word_button_clicked_handler (GtkButton           *button,
 
        word = gtk_entry_get_text (priv->word_entry);
 
-       correctly_spelled = gspell_checker_check_word (priv->checker, word, &error);
+       correctly_spelled = gspell_checker_check_word (priv->checker, word, -1, &error);
 
        if (error != NULL)
        {
diff --git a/gspell/gspell-checker.c b/gspell/gspell-checker.c
index aea4061..d210d54 100644
--- a/gspell/gspell-checker.c
+++ b/gspell/gspell-checker.c
@@ -22,6 +22,7 @@
 #include "gspell-checker.h"
 #include <enchant.h>
 #include <glib/gi18n-lib.h>
+#include <string.h>
 #include "gspell-utils.h"
 
 #ifdef OS_OSX
@@ -427,6 +428,7 @@ gspell_checker_get_language (GspellChecker *checker)
  * gspell_checker_check_word:
  * @checker: a #GspellChecker.
  * @word: the word to check.
+ * @word_length: the byte length of @word, or -1 if @word is nul-terminated.
  * @error: (out) (optional): a location to a %NULL #GError, or %NULL.
  *
  * Returns: whether @word is correctly spelled.
@@ -434,6 +436,7 @@ gspell_checker_get_language (GspellChecker *checker)
 gboolean
 gspell_checker_check_word (GspellChecker  *checker,
                           const gchar    *word,
+                          gint            word_length,
                           GError        **error)
 {
        GspellCheckerPrivate *priv;
@@ -442,6 +445,7 @@ gspell_checker_check_word (GspellChecker  *checker,
 
        g_return_val_if_fail (GSPELL_IS_CHECKER (checker), FALSE);
        g_return_val_if_fail (word != NULL, FALSE);
+       g_return_val_if_fail (word_length >= -1, FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
        /* If no dictionaries are available, limit the damage by returning TRUE. */
@@ -449,23 +453,34 @@ gspell_checker_check_word (GspellChecker  *checker,
 
        priv = gspell_checker_get_instance_private (checker);
 
-       if (_gspell_utils_is_digit (word))
+       if (_gspell_utils_is_digit (word, word_length))
        {
                return TRUE;
        }
 
-       enchant_result = enchant_dict_check (priv->dict, word, -1);
+       enchant_result = enchant_dict_check (priv->dict, word, word_length);
 
        correctly_spelled = enchant_result == 0;
 
        if (enchant_result < 0)
        {
+               gchar *nul_terminated_word;
+
+               if (word_length == -1)
+               {
+                       word_length = strlen (word);
+               }
+
+               nul_terminated_word = g_strndup (word, word_length);
+
                g_set_error (error,
                             GSPELL_CHECKER_ERROR,
                             GSPELL_CHECKER_ERROR_DICTIONARY,
                             _("Error when checking the spelling of word “%s”: %s"),
-                            word,
+                            nul_terminated_word,
                             enchant_dict_get_error (priv->dict));
+
+               g_free (nul_terminated_word);
        }
 
        return correctly_spelled;
diff --git a/gspell/gspell-checker.h b/gspell/gspell-checker.h
index b1534f2..73b43a8 100644
--- a/gspell/gspell-checker.h
+++ b/gspell/gspell-checker.h
@@ -87,6 +87,7 @@ const GspellLanguage *
 
 gboolean       gspell_checker_check_word               (GspellChecker  *checker,
                                                         const gchar    *word,
+                                                        gint            word_length,
                                                         GError        **error);
 
 GSList *       gspell_checker_get_suggestions          (GspellChecker *checker,
diff --git a/gspell/gspell-inline-checker-gtv.c b/gspell/gspell-inline-checker-gtv.c
index 8c2858c..47f6d6a 100644
--- a/gspell/gspell-inline-checker-gtv.c
+++ b/gspell/gspell-inline-checker-gtv.c
@@ -109,7 +109,7 @@ check_word (GspellInlineCheckerGtv *spell,
        word = gtk_text_buffer_get_text (spell->buffer, start, end, FALSE);
 
        correctly_spelled = gspell_checker_check_word (spell->spell_checker,
-                                                      word,
+                                                      word, -1,
                                                       &error);
 
        if (error != NULL)
diff --git a/gspell/gspell-navigator-gtv.c b/gspell/gspell-navigator-gtv.c
index c405246..d9133ca 100644
--- a/gspell/gspell-navigator-gtv.c
+++ b/gspell/gspell-navigator-gtv.c
@@ -404,7 +404,7 @@ gspell_navigator_gtv_goto_next (GspellNavigator  *navigator,
 
                word = gtk_text_buffer_get_text (priv->buffer, &word_start, &word_end, FALSE);
 
-               correctly_spelled = gspell_checker_check_word (priv->spell_checker, word, &error);
+               correctly_spelled = gspell_checker_check_word (priv->spell_checker, word, -1, &error);
 
                if (error != NULL)
                {
diff --git a/gspell/gspell-utils.c b/gspell/gspell-utils.c
index 55050c9..e0ad4a1 100644
--- a/gspell/gspell-utils.c
+++ b/gspell/gspell-utils.c
@@ -23,15 +23,22 @@
 #include <gtksourceview/gtksource.h>
 
 gboolean
-_gspell_utils_is_digit (const gchar *text)
+_gspell_utils_is_digit (const gchar *text,
+                       gint         text_length)
 {
        const gchar *p;
        const gchar *end;
 
        g_return_val_if_fail (text != NULL, FALSE);
+       g_return_val_if_fail (text_length >= -1, FALSE);
+
+       if (text_length == -1)
+       {
+               text_length = strlen (text);
+       }
 
        p = text;
-       end = text + strlen (text);
+       end = text + text_length;
 
        while (p != NULL && *p != '\0')
        {
diff --git a/gspell/gspell-utils.h b/gspell/gspell-utils.h
index 9bfe3e7..7f2bddb 100644
--- a/gspell/gspell-utils.h
+++ b/gspell/gspell-utils.h
@@ -26,7 +26,8 @@
 G_BEGIN_DECLS
 
 G_GNUC_INTERNAL
-gboolean       _gspell_utils_is_digit                  (const gchar *text);
+gboolean       _gspell_utils_is_digit                  (const gchar *text,
+                                                        gint         text_length);
 
 G_GNUC_INTERNAL
 gboolean       _gspell_utils_skip_no_spell_check       (GtkTextIter       *start,


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