-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
texteditor: make search case insensitive
- Loading branch information
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <limits.h> | ||
#include <ctype.h> | ||
|
||
const char * boyer_moore(const char *haystack, const char *needle) { | ||
size_t plen = strlen(needle), slen = strlen(haystack); | ||
|
||
if (plen > slen) { | ||
return NULL; | ||
} | ||
|
||
int skip[UCHAR_MAX+1]; | ||
int i, j, k, * next; | ||
|
||
/* calc skip table („bad rule“) */ | ||
for (i = 0; i <= UCHAR_MAX; i++) { | ||
skip[i] = plen; | ||
} | ||
|
||
for (i = 0; i < plen; i++) { | ||
skip[tolower((unsigned char)needle[i])] = plen - i - 1; | ||
} | ||
|
||
|
||
/* calc next table („good rule“) */ | ||
next = (int*)malloc((plen+1) * sizeof(int)); | ||
|
||
for (j = 0; j <= plen; j++) { | ||
for (i = plen - 1; i >= 1; i--) { | ||
for (k = 1; k <= j; k++) { | ||
if (i - k < 0) { | ||
break; | ||
} | ||
if (tolower((unsigned char)needle[plen - k]) != tolower((unsigned char)needle[i - k])) { | ||
goto nexttry; | ||
} | ||
} | ||
goto matched; | ||
nexttry: | ||
; | ||
} | ||
matched: | ||
next[j] = plen - i; | ||
} | ||
|
||
plen--; | ||
i = plen; /* position of last p letter in s */ | ||
|
||
while (i < slen) { | ||
j = 0; /* matched letter count */ | ||
while (j <= plen) { | ||
if (tolower((unsigned char)haystack[i - j]) == tolower((unsigned char)needle[plen - j])) { | ||
j++; | ||
} else { | ||
i += skip[tolower((unsigned char)haystack[i - j])] > next[j] ? skip[tolower((unsigned char)haystack[i - j])] - j : next[j]; | ||
goto newi; | ||
} | ||
} | ||
free(next); | ||
return haystack + i - plen; | ||
newi: | ||
; | ||
} | ||
free(next); | ||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef __BM_H__ | ||
#define __BM_H__ | ||
|
||
char * boyer_moore(const char *haystack, const char *needle); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters