Skip to content

Commit

Permalink
fix 8bit searching
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed Mar 4, 2018
1 parent 798d174 commit 608e94f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
111 changes: 107 additions & 4 deletions src/pspg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -2435,15 +2532,15 @@ 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);
}
else if (opts.ignore_lower_case && scrdesc.has_upperchr)
{
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);
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 6 additions & 4 deletions src/pspg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/themes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 608e94f

Please sign in to comment.