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.
master
Benno Schulenberg 2016-05-02 22:20:19 +02:00
parent e666f8c50d
commit cc0a3d8490
3 changed files with 20 additions and 28 deletions

View File

@ -721,7 +721,7 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream);
const char *fixbounds(const char *r); const char *fixbounds(const char *r);
#endif #endif
#ifndef DISABLE_SPELLER #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 #endif
const char *strstrwrapper(const char *haystack, const char *needle, const char *strstrwrapper(const char *haystack, const char *needle,
const char *start); const char *start);

View File

@ -305,10 +305,6 @@ int findnextstr(
found = strstrwrapper(fileptr->data, needle, rev_start); found = strstrwrapper(fileptr->data, needle, rev_start);
if (found != NULL) { 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. */ /* Remember the length of the potential match. */
found_len = found_len =
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
@ -318,19 +314,13 @@ int findnextstr(
strlen(needle); strlen(needle);
#ifndef DISABLE_SPELLER #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) { if (whole_word_only) {
char *word = mallocstrncpy(NULL, found, found_len + 1); if (is_separate_word(found - fileptr->data, found_len,
word[found_len] = '\0'; fileptr->data))
break;
found_whole = is_whole_word(found - fileptr->data, } else
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)
#endif #endif
break; break;
} }

View File

@ -278,27 +278,29 @@ const char *fixbounds(const char *r)
#endif /* HAVE_REGEX_H */ #endif /* HAVE_REGEX_H */
#ifndef DISABLE_SPELLER #ifndef DISABLE_SPELLER
/* Is the word starting at position pos in buf a whole word? */ /* Is the word starting at the given position in buf and of the given length
bool is_whole_word(size_t pos, const char *buf, const char *word) * 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()); char *before = charalloc(mb_cur_max()), *after = charalloc(mb_cur_max());
size_t word_end = pos + strlen(word); size_t word_end = position + length;
bool retval; 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); /* Get the characters before and after the word, if any. */
parse_mbchar(buf + word_end, r, NULL); 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 /* 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 * 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 * the end of the line or the character after the word isn't a
* non-punctuation "word" character, we have a whole word. */ * non-punctuation "word" character, we have a whole word. */
retval = (pos == 0 || !is_word_mbchar(p, FALSE)) && retval = (position == 0 || !is_word_mbchar(before, FALSE)) &&
(word_end == strlen(buf) || !is_word_mbchar(r, FALSE)); (word_end == strlen(buf) || !is_word_mbchar(after, FALSE));
free(p); free(before);
free(r); free(after);
return retval; return retval;
} }