Support case insensitive search.

Enabled only if there is an upper string character in the search tokens.

Also call setlocale() in main() to guarantee correct behavior.
This commit is contained in:
Érico Rolim 2021-01-07 18:41:35 -03:00
parent 68226bc337
commit 472337a477
2 changed files with 24 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <locale.h>
#include <curses.h>
@ -32,6 +33,8 @@ static void finish(int sig)
int main()
{
setlocale(LC_ALL, "");
const char delim = '\n';
struct str_array entries = { 0 };

View File

@ -1,6 +1,8 @@
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "string-array.h"
#include "util.h"
@ -24,6 +26,8 @@ char *pop_entry(struct str_array *a)
return get_entry(a, a->n);
}
typedef char * (*substring_search)(const char *, const char*);
void filter_entries(struct str_array *a, const struct str_array *m)
{
if (!m) {
@ -31,13 +35,29 @@ void filter_entries(struct str_array *a, const struct str_array *m)
return;
}
/* find if any strings are capitalized */
bool capitalized = false;
for (size_t i = 0; i < m->n; i++) {
char *s = get_entry(m, i);
size_t l = strlen(s);
for (size_t j = 0; j < l; j++) {
if (isupper(s[j])) {
capitalized = true;
break;
}
}
if (capitalized) break;
}
/* we only care about case if there is an upper char */
substring_search fn = capitalized ? strstr : strcasestr;
a->ms = 0;
for (size_t i = 0; i < m->n; i++) {
for (size_t j = 0; j < a->n; j++) {
/* first token or was previously true */
if (i == 0 || a->m[j]) {
/* only count matches in the last round */
if ((a->m[j] = strstr(a->v[j], m->v[i])) && (i == m->n - 1)) {
if ((a->m[j] = fn(a->v[j], m->v[i])) && (i == m->n - 1)) {
a->ms++;
}
}