Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to support case-sensitive search #190

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/enchant.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ void enchant_broker_describe (EnchantBroker * broker,
*/
int enchant_dict_check (EnchantDict * dict, const char *const word, ssize_t len);

/**
* enchant_dict_check
* @dict: A non-null #EnchantDict
* @word: The non-null word you wish to check, in UTF-8 encoding
* @len: The byte length of @word, or -1 for strlen (@word)
*
* Will return an "incorrect" value if any of those pre-conditions
* are not met.
* Does not attempt to search for titlecase/lowercase variations of @word.
*
* Returns: 0 if the word is correctly spelled, positive if not, negative if error
*/
int enchant_dict_check_case_sensitive (EnchantDict * dict, const char *const word, ssize_t len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this compiles. Missing the semi-colon at the end.


/**
* enchant_dict_suggest
* @dict: A non-null #EnchantDict
Expand Down
38 changes: 28 additions & 10 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,30 @@ enchant_session_remove_exclude (EnchantSession * session, const char * const wor
* AND the word has not been added to the session include list
*/
static gboolean
enchant_session_exclude (EnchantSession * session, const char * const word, size_t len)
enchant_session_exclude (EnchantSession * session, const char * const word, size_t len, gboolean normalize_case)
{
char * utf = g_strndup (word, len);
/* TODO: enchant_pwl_contains does not refresh the file */
gboolean result = !g_hash_table_lookup (session->session_include, utf) &&
(g_hash_table_lookup (session->session_exclude, utf) ||
enchant_pwl_check (session->exclude, word, len) == 0);
(normalize_case ?
enchant_pwl_check (session->exclude, word, len) == 0) :
enchant_pwl_contains (session->exclude, word, len) == 0);
g_free (utf);

return result;
}

static gboolean
enchant_session_contains (EnchantSession * session, const char * const word, size_t len)
enchant_session_contains (EnchantSession * session, const char * const word, size_t len, gboolean normalize_case)
{
char * utf = g_strndup (word, len);
gboolean result = g_hash_table_lookup (session->session_include, utf) ||
(enchant_pwl_check (session->personal, word, len) == 0 &&
(!enchant_pwl_check (session->exclude, word, len)) == 0);
(normalize_case ?
(enchant_pwl_check (session->personal, word, len) == 0 &&
(!enchant_pwl_check (session->exclude, word, len)) == 0) :
(enchant_pwl_contains (session->personal, word, len) == 0 &&
(!enchant_pwl_contains (session->exclude, word, len)) == 0));
g_free (utf);

return result;
Expand Down Expand Up @@ -417,6 +423,18 @@ enchant_dict_get_error (EnchantDict * dict)

int
enchant_dict_check (EnchantDict * dict, const char *const word, ssize_t len)
{
return enchant_dict_contains (dict, word, len, TRUE);
}

int
enchant_dict_check_case_sensitive (EnchantDict * dict, const char *const word, ssize_t len)
{
return enchant_dict_contains (dict, word, len, FALSE);
}

int
enchant_dict_contains (EnchantDict * dict, const char *const word, ssize_t len, gboolean normalize_case)
{
g_return_val_if_fail (dict, -1);
g_return_val_if_fail (word, -1);
Expand All @@ -431,11 +449,11 @@ enchant_dict_check (EnchantDict * dict, const char *const word, ssize_t len)
enchant_session_clear_error (session);

/* first, see if it's to be excluded*/
if (enchant_session_exclude (session, word, len))
if (enchant_session_exclude (session, word, len, normalize_case))
return 1;

/* then, see if it's in our pwl or session*/
if (enchant_session_contains(session, word, len))
if (enchant_session_contains(session, word, len, normalize_case))
return 0;

if (dict->check)
Expand Down Expand Up @@ -489,7 +507,7 @@ enchant_dict_get_good_suggestions(EnchantDict * dict, char ** suggs, size_t n_su
continue;

if (g_utf8_validate(suggs[i], sugg_len, NULL) &&
!enchant_session_exclude(session, suggs[i], sugg_len))
!enchant_session_exclude(session, suggs[i], sugg_len, TRUE))
filtered_suggs[n_filtered_suggs++] = strdup (suggs[i]);
}

Expand Down Expand Up @@ -618,7 +636,7 @@ enchant_dict_is_added (EnchantDict * dict, const char *const word, ssize_t len)
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);

return enchant_session_contains (session, word, len);
return enchant_session_contains (session, word, len, TRUE);
}

void
Expand Down Expand Up @@ -676,7 +694,7 @@ enchant_dict_is_removed (EnchantDict * dict, const char *const word, ssize_t len
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);

return enchant_session_exclude (session, word, len);
return enchant_session_exclude (session, word, len, TRUE);
}

void
Expand Down