diff --git a/src/print.c b/src/print.c index f0de9f3..71abbf6 100644 --- a/src/print.c +++ b/src/print.c @@ -140,14 +140,14 @@ window_fill(int window_identifier, if (opts->ignore_case || (opts->ignore_lower_case && !scrdesc->has_upperchr)) { if (opts->force8bit) - str = strstr(str, scrdesc->searchterm); + str = nstrstr(str, scrdesc->searchterm); else str = utf8_nstrstr(str, scrdesc->searchterm); } else if (opts->ignore_lower_case && scrdesc->has_upperchr) { if (opts->force8bit) - str = strstr(str, scrdesc->searchterm); + str = nstrstr_ignore_lower_case(str, scrdesc->searchterm); else str = utf8_nstrstr_ignore_lower_case(str, scrdesc->searchterm); } @@ -194,14 +194,14 @@ window_fill(int window_identifier, if (opts->ignore_case || (opts->ignore_lower_case && !scrdesc->has_upperchr)) { if (opts->force8bit) - str = strstr(str, scrdesc->searchterm); + str = nstrstr(str, scrdesc->searchterm); else str = utf8_nstrstr(str, scrdesc->searchterm); } else if (opts->ignore_lower_case && scrdesc->has_upperchr) { if (opts->force8bit) - str = strstr(str, scrdesc->searchterm); + str = nstrstr_ignore_lower_case(str, scrdesc->searchterm); else str = utf8_nstrstr_ignore_lower_case(str, scrdesc->searchterm); } diff --git a/src/pspg.c b/src/pspg.c index 1a5d4cd..2f849da 100644 --- a/src/pspg.c +++ b/src/pspg.c @@ -49,6 +49,103 @@ min_int(int a, int b) return b; } +/* + * special case insensitive searching routines + */ +const char * +nstrstr(const char *haystack, const char *needle) +{ + const char *haystack_cur, *needle_cur, *needle_prev; + int f1 = 0, f2 = 0; + + needle_cur = needle; + needle_prev = NULL; + haystack_cur = haystack; + + while (*needle_cur != '\0') + { + if (*haystack_cur == '\0') + return NULL; + + if (needle_prev != needle_cur) + { + needle_prev = needle_cur; + f1 = toupper(*needle_cur); + } + + f2 = toupper(*haystack_cur); + if (f1 == f2) + { + needle_cur += 1; + haystack_cur += 1; + } + else + { + needle_cur = needle; + haystack_cur = haystack += 1; + } + } + + return haystack; +} + +/* + * Special string searching, lower chars are case insensitive, + * upper chars are case sensitive. + */ +const char * +nstrstr_ignore_lower_case(const char *haystack, const char *needle) +{ + const char *haystack_cur, *needle_cur, *needle_prev; + int f1 = 0; + bool eq; + + needle_cur = needle; + needle_prev = NULL; + haystack_cur = haystack; + + while (*needle_cur != '\0') + { + bool needle_char_is_upper; + + if (*haystack_cur == '\0') + return NULL; + + if (needle_prev != needle_cur) + { + needle_prev = needle_cur; + needle_char_is_upper = isupper(*needle_cur); + f1 = toupper(*needle_cur); + } + + if (needle_char_is_upper) + { + /* case sensitive */ + eq = *haystack_cur == *needle_cur; + } + else + { + /* case insensitive */ + eq = f1 == toupper(*haystack_cur); + } + + if (eq) + { + needle_cur += 1; + haystack_cur += 1; + } + else + { + needle_cur = needle; + haystack_cur = haystack += 1; + } + } + + return haystack; +} + + + static const char * strcasestr(const char *s1, const char *s2) { @@ -2435,7 +2532,7 @@ main(int argc, char *argv[]) if (opts.ignore_case || (opts.ignore_lower_case && !scrdesc.has_upperchr)) { if (opts.force8bit) - str = strcasestr(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); + str = nstrstr(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); else str = utf8_nstrstr(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); } @@ -2443,7 +2540,7 @@ main(int argc, char *argv[]) { if (opts.force8bit) /* isn't correct */ - str = strcasestr(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); + str = nstrstr_ignore_lower_case(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); else str = utf8_nstrstr_ignore_lower_case(lnb->rows[rownum_cursor_row] + skip_bytes, scrdesc.searchterm); @@ -2589,9 +2686,15 @@ main(int argc, char *argv[]) while (str != NULL) { if (opts.ignore_case || (opts.ignore_lower_case && !scrdesc.has_upperchr)) - str = utf8_nstrstr(str, scrdesc.searchterm); + if (opts.force8bit) + nstrstr(str, scrdesc.searchterm); + else + str = utf8_nstrstr(str, scrdesc.searchterm); else if (opts.ignore_lower_case && scrdesc.has_upperchr) - str = utf8_nstrstr_ignore_lower_case(str, scrdesc.searchterm); + if (opts.force8bit) + nstrstr_ignore_lower_case(str, scrdesc.searchterm); + else + str = utf8_nstrstr_ignore_lower_case(str, scrdesc.searchterm); else str = str = strstr(str, scrdesc.searchterm); diff --git a/src/pspg.h b/src/pspg.h index f64c8bc..cd3e0d4 100644 --- a/src/pspg.h +++ b/src/pspg.h @@ -140,11 +140,13 @@ typedef struct #define w_bottom_bar(scrdesc) ((scrdesc)->wins[WINDOW_BOTTOM_BAR]) /* from print.c */ -void window_fill(int window_identifier, int srcy, int srcx, int cursor_row, DataDesc *desc, ScrDesc *scrdesc, Options *opts); -void draw_data(Options *opts, ScrDesc *scrdesc, DataDesc *desc, int first_data_row, int first_row, int cursor_col, int footer_cursor_col, int fix_rows_offset); +extern void window_fill(int window_identifier, int srcy, int srcx, int cursor_row, DataDesc *desc, ScrDesc *scrdesc, Options *opts); +extern void draw_data(Options *opts, ScrDesc *scrdesc, DataDesc *desc, int first_data_row, int first_row, int cursor_col, int footer_cursor_col, int fix_rows_offset); /* from pspg.c */ -bool is_expanded_header(Options *opts, char *str, int *ei_minx, int *ei_maxx); -int min_int(int a, int b); +extern bool is_expanded_header(Options *opts, char *str, int *ei_minx, int *ei_maxx); +extern int min_int(int a, int b); +extern const char *nstrstr(const char *haystack, const char *needle); +extern const char *nstrstr_ignore_lower_case(const char *haystack, const char *needle); #endif \ No newline at end of file diff --git a/src/themes.h b/src/themes.h index 94a71a5..61acc1b 100644 --- a/src/themes.h +++ b/src/themes.h @@ -45,7 +45,7 @@ typedef struct #define WINDOW_TOP_BAR 5 #define WINDOW_BOTTOM_BAR 6 -void initialize_color_pairs(int theme); -void initialize_theme(int theme, int window_identifier, bool is_tabular_fmt, bool no_highlight_lines, Theme *t); +extern void initialize_color_pairs(int theme); +extern void initialize_theme(int theme, int window_identifier, bool is_tabular_fmt, bool no_highlight_lines, Theme *t); #endif \ No newline at end of file