Skip to content

Commit

Permalink
texteditor: make search case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoot committed Sep 10, 2016
1 parent 2f85463 commit 8379eac
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TITLE_ID = VITASHELL
TARGET = VitaShell
OBJS = main.o init.o io_process.o package_installer.o network_update.o context_menu.o archive.o photo.o file.o text.o hex.o sfo.o \
uncommon_dialog.o message_dialog.o ime_dialog.o config.o theme.o language.o utils.o sha1.o \
audioplayer.o minizip/unzip.o minizip/ioapi.o
audioplayer.o minizip/unzip.o minizip/ioapi.o bm.o

RESOURCES_PNG = resources/folder_icon.png resources/file_icon.png resources/archive_icon.png resources/image_icon.png \
resources/audio_icon.png resources/sfo_icon.png resources/text_icon.png\
Expand Down
68 changes: 68 additions & 0 deletions bm.c
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;
}
6 changes: 6 additions & 0 deletions bm.h
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
17 changes: 13 additions & 4 deletions text.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ static int search_thread(SceSize args, SearchParams *argp) {

char *r;
while (state->search_running && offset < state->size && state->n_search_results < MAX_SEARCH_RESULTS) {
r = strstr(state->buffer + offset, search_term);
r = strcasestr(state->buffer + offset, search_term);

if (r == NULL) {
state->search_running = 0;
Expand Down Expand Up @@ -981,10 +981,12 @@ int textViewer(char *file) {

char *search_highlight = NULL;
if (search_result_on_line) {
search_highlight = strstr(line, s->search_term);
search_highlight = strcasestr(line, s->search_term);
}

char tmp = '\0';
if (search_highlight) {
tmp = *search_highlight;
*search_highlight = '\0';
}

Expand All @@ -999,9 +1001,16 @@ int textViewer(char *file) {
}

if (search_highlight) {
*search_highlight = s->search_term[0];
*search_highlight = tmp;

int search_term_length = strlen(s->search_term);
tmp = search_highlight[search_term_length];
search_highlight[search_term_length] = '\0';

x += width;
x += pgf_draw_text(x, START_Y + (i * FONT_Y_SPACE), TEXT_HIGHLIGHT_COLOR, FONT_SIZE, s->search_term);
x += pgf_draw_text(x, START_Y + (i * FONT_Y_SPACE), TEXT_HIGHLIGHT_COLOR, FONT_SIZE, line);

search_highlight[search_term_length] = tmp;
line += strlen(s->search_term);
}
}
Expand Down
6 changes: 6 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "theme.h"
#include "language.h"
#include "utils.h"
#include "bm.h"

SceCtrlData pad;
uint32_t old_buttons, current_buttons, pressed_buttons, hold_buttons, hold2_buttons, released_buttons;
Expand Down Expand Up @@ -319,4 +320,9 @@ int launchAppByUriExit(char *titleid) {
sceKernelExitProcess(0);

return 0;
}


char *strcasestr(const char *haystack, const char *needle) {
return boyer_moore(haystack, needle);
}
2 changes: 2 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ int debugPrintf(char *text, ...);

int launchAppByUriExit(char *titleid);

char *strcasestr(const char *haystack, const char *needle);

#endif

0 comments on commit 8379eac

Please sign in to comment.