From 472337a477f0ce516a5aa26fd880ed4a4956dfd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Thu, 7 Jan 2021 18:41:35 -0300 Subject: [PATCH] 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. --- browser.c | 3 +++ string-array.c | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/browser.c b/browser.c index 5bc462a..1e27edd 100644 --- a/browser.c +++ b/browser.c @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -32,6 +33,8 @@ static void finish(int sig) int main() { + setlocale(LC_ALL, ""); + const char delim = '\n'; struct str_array entries = { 0 }; diff --git a/string-array.c b/string-array.c index 199f20b..4296e65 100644 --- a/string-array.c +++ b/string-array.c @@ -1,6 +1,8 @@ +#define _GNU_SOURCE #include #include #include +#include #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++; } }