Commit cc0a3d84 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

search: modify a function to take a length as parameter instead of a word

When verifying that a match is a separate word (during spell checking),
instead of first copying out the word, then passing the word, and then
measuring its length, just pass its length straigtaway.
No related merge requests found
Showing with 20 additions and 28 deletions
+20 -28
......@@ -721,7 +721,7 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream);
const char *fixbounds(const char *r);
#endif
#ifndef DISABLE_SPELLER
bool is_whole_word(size_t pos, const char *buf, const char *word);
bool is_separate_word(size_t position, size_t length, const char *buf);
#endif
const char *strstrwrapper(const char *haystack, const char *needle,
const char *start);
......
......@@ -305,10 +305,6 @@ int findnextstr(
found = strstrwrapper(fileptr->data, needle, rev_start);
if (found != NULL) {
#ifndef DISABLE_SPELLER
bool found_whole = FALSE;
/* Is this potential match a whole word? */
#endif
/* Remember the length of the potential match. */
found_len =
#ifdef HAVE_REGEX_H
......@@ -318,19 +314,13 @@ int findnextstr(
strlen(needle);
#ifndef DISABLE_SPELLER
/* If we're searching for whole words, see if it is. */
/* When we're spell checking, a match is only a true match when
* it is a separate word. */
if (whole_word_only) {
char *word = mallocstrncpy(NULL, found, found_len + 1);
word[found_len] = '\0';
found_whole = is_whole_word(found - fileptr->data,
fileptr->data, word);
free(word);
}
/* If we're searching for whole words and this potential
* match isn't a whole word, continue searching. */
if (!whole_word_only || found_whole)
if (is_separate_word(found - fileptr->data, found_len,
fileptr->data))
break;
} else
#endif
break;
}
......
......@@ -278,27 +278,29 @@ const char *fixbounds(const char *r)
#endif /* HAVE_REGEX_H */
#ifndef DISABLE_SPELLER
/* Is the word starting at position pos in buf a whole word? */
bool is_whole_word(size_t pos, const char *buf, const char *word)
/* Is the word starting at the given position in buf and of the given length
* a separate word? That is: is it not part of a longer word?*/
bool is_separate_word(size_t position, size_t length, const char *buf)
{
char *p = charalloc(mb_cur_max()), *r = charalloc(mb_cur_max());
size_t word_end = pos + strlen(word);
char *before = charalloc(mb_cur_max()), *after = charalloc(mb_cur_max());
size_t word_end = position + length;
bool retval;
assert(buf != NULL && pos <= strlen(buf) && word != NULL);
assert(buf != NULL && position < strlen(buf) && position + length <= strlen(buf));
parse_mbchar(buf + move_mbleft(buf, pos), p, NULL);
parse_mbchar(buf + word_end, r, NULL);
/* Get the characters before and after the word, if any. */
parse_mbchar(buf + move_mbleft(buf, position), before, NULL);
parse_mbchar(buf + word_end, after, NULL);
/* If we're at the beginning of the line or the character before the
* word isn't a non-punctuation "word" character, and if we're at
* the end of the line or the character after the word isn't a
* non-punctuation "word" character, we have a whole word. */
retval = (pos == 0 || !is_word_mbchar(p, FALSE)) &&
(word_end == strlen(buf) || !is_word_mbchar(r, FALSE));
retval = (position == 0 || !is_word_mbchar(before, FALSE)) &&
(word_end == strlen(buf) || !is_word_mbchar(after, FALSE));
free(p);
free(r);
free(before);
free(after);
return retval;
}
......
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