/*
Super fast linear text search algorithms:
searchi = search ignore case
search = search case sensitive
searchiw = search ignore case words only (e.g. words delimited by whitespace only,
not words within words)
searchw() = search case sensitive words only
All functions return the number of matches for keyword in buffer, or -1 on error.
by James Buchanan
No license ristrictions on this code.
Email: jamesb@northnet.com.au
*/
#include
#include
#include
#include
#include "textsearch.h"
int searchi(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (!b_len || !k_len)
return -1;
ch_matches = found = 0;
for (i=0; i ch_matches = 0;
for (j=0; j if (tolower(buffer[i+j]) == tolower(keyword[j])) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
return found;
}
int search(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (!b_len || !k_len)
return -1;
ch_matches = found = 0;
for (i=0; i ch_matches = 0;
for (j=0; j if (buffer[i+j] == keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
return found;
}
int searchiw(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
char *temp_keyword;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (b_len < 2 || k_len < 2) /* Useless, for words only */
return -1;
if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+3, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
}
else {
/* If we get to here and no if statement has executed, keyword already has whitespaces
surrounding it */
temp_keyword = (char *)calloc(k_len+1, sizeof(char));
if (!temp_keyword)
return -1;
strcpy(temp_keyword, keyword);
}
ch_matches = found = 0;
k_len = strlen(temp_keyword); /* Calculate new string length */
for (i=0; i ch_matches = 0;
for (j=0; j if (buffer[i+j] == temp_keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
if (temp_keyword != NULL)
free(temp_keyword);
return found;
}
int searchw(const char *buffer, const char *keyword)
{
int k_len, b_len, ch_matches, found, i, j;
char *temp_keyword;
b_len = strlen(buffer);
k_len = strlen(keyword);
if (b_len < 2 || k_len < 2) /* Useless, for words only */
return -1;
if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+3, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
strcat(temp_keyword, keyword);
strcat(temp_keyword, " ");
}
else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
temp_keyword = (char *)calloc(k_len+2, sizeof(char));
if (!temp_keyword)
return -1;
temp_keyword[0] = ' ';
strcat(temp_keyword, keyword);
}
else {
/* If we get to here and no if statement has executed, keyword already has whitespaces
surrounding it */
temp_keyword = (char *)calloc(k_len+1, sizeof(char));
if (!temp_keyword)
return -1;
strcpy(temp_keyword, keyword);
}
ch_matches = found = 0;
k_len = strlen(temp_keyword); /* Calculate new string length */
for (i=0; i ch_matches = 0;
for (j=0; j if (buffer[i+j] == temp_keyword[j]) {
ch_matches++;
if (ch_matches == k_len) {
found++;
i += k_len;
}
}
}
}
if (temp_keyword != NULL)
free(temp_keyword);
return found;
}