Commit 87c33268 authored by Sébastien Wilmet's avatar Sébastien Wilmet

Move utils function to gspell-utils to write unit test

parent 77069294
......@@ -334,46 +334,6 @@ gspell_checker_get_language (GspellChecker *checker)
return priv->active_lang;
}
/* Replaces unicode (non-ascii) apostrophes by the ascii apostrophe.
* Because with unicode apostrophes, the word is marked as misspelled. It should
* probably be fixed in hunspell, aspell, etc.
* Returns: %TRUE if @sanitzed_word has been set, %FALSE is @word must be used
* (to avoid a malloc).
*/
static gboolean
sanitize_word (const gchar *word,
gssize word_length,
gchar **sanitized_word)
{
gchar *word_to_free = NULL;
const gchar *nul_terminated_word;
if (g_utf8_strchr (word, word_length, _GSPELL_MODIFIER_LETTER_APOSTROPHE) == NULL &&
g_utf8_strchr (word, word_length, _GSPELL_RIGHT_SINGLE_QUOTATION_MARK) == NULL)
{
return FALSE;
}
if (word_length == -1)
{
nul_terminated_word = word;
}
else
{
word_to_free = g_strndup (word, word_length);
nul_terminated_word = word_to_free;
}
*sanitized_word = _gspell_utils_str_replace (nul_terminated_word, "\xCA\xBC", "'");
g_free (word_to_free);
word_to_free = *sanitized_word;
*sanitized_word = _gspell_utils_str_replace (*sanitized_word, "\xE2\x80\x99", "'");
g_free (word_to_free);
return TRUE;
}
/**
* gspell_checker_check_word:
* @checker: a #GspellChecker.
......@@ -414,7 +374,7 @@ gspell_checker_check_word (GspellChecker *checker,
return TRUE;
}
if (sanitize_word (word, word_length, &sanitized_word))
if (_gspell_utils_str_to_ascii_apostrophe (word, word_length, &sanitized_word))
{
enchant_result = enchant_dict_check (priv->dict, sanitized_word, -1);
g_free (sanitized_word);
......@@ -483,7 +443,7 @@ gspell_checker_get_suggestions (GspellChecker *checker,
return NULL;
}
if (sanitize_word (word, word_length, &sanitized_word))
if (_gspell_utils_str_to_ascii_apostrophe (word, word_length, &sanitized_word))
{
suggestions = enchant_dict_suggest (priv->dict, sanitized_word, -1, NULL);
g_free (sanitized_word);
......
......@@ -149,4 +149,48 @@ _gspell_utils_str_replace (const gchar *string,
return ret;
}
/* Replaces unicode (non-ascii) apostrophes by the ascii apostrophe.
* Because with unicode apostrophes, the word is marked as misspelled. It should
* probably be fixed in hunspell, aspell, etc.
* Returns: %TRUE if @result has been set, %FALSE if @word must be used
* (to avoid a malloc).
*/
gboolean
_gspell_utils_str_to_ascii_apostrophe (const gchar *word,
gssize word_length,
gchar **result)
{
gchar *word_to_free = NULL;
const gchar *nul_terminated_word;
g_return_val_if_fail (word != NULL, FALSE);
g_return_val_if_fail (word_length >= -1, FALSE);
g_return_val_if_fail (result != NULL, FALSE);
if (g_utf8_strchr (word, word_length, _GSPELL_MODIFIER_LETTER_APOSTROPHE) == NULL &&
g_utf8_strchr (word, word_length, _GSPELL_RIGHT_SINGLE_QUOTATION_MARK) == NULL)
{
return FALSE;
}
if (word_length == -1)
{
nul_terminated_word = word;
}
else
{
word_to_free = g_strndup (word, word_length);
nul_terminated_word = word_to_free;
}
*result = _gspell_utils_str_replace (nul_terminated_word, "\xCA\xBC", "'");
g_free (word_to_free);
word_to_free = *result;
*result = _gspell_utils_str_replace (*result, "\xE2\x80\x99", "'");
g_free (word_to_free);
return TRUE;
}
/* ex:set ts=8 noet: */
......@@ -46,6 +46,11 @@ gchar * _gspell_utils_str_replace (const gchar *string,
const gchar *search,
const gchar *replacement);
G_GNUC_INTERNAL
gboolean _gspell_utils_str_to_ascii_apostrophe (const gchar *word,
gssize word_length,
gchar **result);
G_END_DECLS
#endif /* __GSPELL_UTILS_H__ */
......
......@@ -21,4 +21,7 @@ test_inline_checker_text_buffer_SOURCES = test-inline-checker-text-buffer.c
UNIT_TEST_PROGS += test-text-iter
test_text_iter_SOURCES = test-text-iter.c
UNIT_TEST_PROGS += test-utils
test_utils_SOURCES = test-utils.c
-include $(top_srcdir)/git.mk
/*
* This file is part of gspell, a spell-checking library.
*
* Copyright 2016 - Sébastien Wilmet <swilmet@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "gspell/gspell-utils.h"
static void
test_str_replace (void)
{
gchar *result;
result = _gspell_utils_str_replace ("$filename", "$filename", "blah");
g_assert_cmpstr (result, ==, "blah");
g_free (result);
result = _gspell_utils_str_replace ("$shortname.pdf", "$shortname", "blah");
g_assert_cmpstr (result, ==, "blah.pdf");
g_free (result);
result = _gspell_utils_str_replace ("abcdabcd", "ab", "r");
g_assert_cmpstr (result, ==, "rcdrcd");
g_free (result);
result = _gspell_utils_str_replace ("abcd", "ef", "r");
g_assert_cmpstr (result, ==, "abcd");
g_free (result);
}
static void
test_str_to_ascii_apostrophe (void)
{
gchar *result = NULL;
gboolean ret;
ret = _gspell_utils_str_to_ascii_apostrophe ("rock'n'roll", -1, &result);
g_assert (!ret);
g_assert (result == NULL);
ret = _gspell_utils_str_to_ascii_apostrophe ("rock\xCA\xBCn\xCA\xBCroll", -1, &result);
g_assert (ret);
g_assert_cmpstr (result, ==, "rock'n'roll");
g_free (result);
result = NULL;
ret = _gspell_utils_str_to_ascii_apostrophe ("rock\xE2\x80\x99n\xE2\x80\x99roll", -1, &result);
g_assert (ret);
g_assert_cmpstr (result, ==, "rock'n'roll");
g_free (result);
result = NULL;
ret = _gspell_utils_str_to_ascii_apostrophe ("rock\xCA\xBCn\xE2\x80\x99roll", -1, &result);
g_assert (ret);
g_assert_cmpstr (result, ==, "rock'n'roll");
g_free (result);
result = NULL;
ret = _gspell_utils_str_to_ascii_apostrophe ("rock\xCA\xBCn\xE2\x80\x99roll", 7, &result);
g_assert (ret);
g_assert_cmpstr (result, ==, "rock'n");
g_free (result);
result = NULL;
ret = _gspell_utils_str_to_ascii_apostrophe ("rock\xCA\xBCn\xE2\x80\x99roll", 4, &result);
g_assert (!ret);
g_assert (result == NULL);
}
gint
main (gint argc,
gchar **argv)
{
gtk_test_init (&argc, &argv);
g_test_add_func ("/utils/str_replace", test_str_replace);
g_test_add_func ("/utils/str_to_ascii_apostrophe", test_str_to_ascii_apostrophe);
return g_test_run ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment