From: Chris Allegretta Date: Wed, 16 May 2001 04:26:01 +0000 (+0000) Subject: strcasestr() - Replaced by mutt's mutt_stristr function, because the thought of dynam... X-Git-Tag: v1.0.3~22 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=6accd621a392607f7e23c504298b78ec14531ad6;p=nano.git strcasestr() - Replaced by mutt's mutt_stristr function, because the thought of dynamically allocating memory and copying each line in a file to do a search or replace was causing me to lose sleep git-svn-id: svn://svn.savannah.gnu.org/nano/branches/nano_1_0_branch/nano@657 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 66a41443..2eea41fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,11 @@ CVS code - do_alt_speller() - Try to go to the same line we were on before before spell check (Rocco). +- utils.c: + strcasestr() + - Replaced by mutt's mutt_stristr function, because the thought + of dynamically allocating memory and copying each line in a file + to do a search or replace was causing me to lose sleep. nano 1.0.2 - 05/12/2001 - General: diff --git a/utils.c b/utils.c index b21683e8..7dbec7ae 100644 --- a/utils.c +++ b/utils.c @@ -48,37 +48,26 @@ void lowercase(char *src) } -/* I can't believe I have to write this function */ +/* This is now mutt's version (called mutt_stristr) because it doesn't + use memory allocation to do a simple search (yuck). */ char *strcasestr(char *haystack, char *needle) { - char *localneedle, *localhaystack, *found, *tmp, *tmp2; + const char *p, *q; - /* Make a copy of the search string and search space */ - localneedle = nmalloc(strlen(needle) + 2); - localhaystack = nmalloc(strlen(haystack) + 2); - - strcpy(localneedle, needle); - strcpy(localhaystack, haystack); - - /* Make them lowercase */ - lowercase(localneedle); - lowercase(localhaystack); - - /* Look for the lowercased substring in the lowercased search space - - return NULL if we didn't find anything */ - if ((found = strstr(localhaystack, localneedle)) == NULL) { - free(localneedle); - free(localhaystack); + if (!haystack) return NULL; + if (!needle) + return (haystack); + + while (*(p = haystack)) + { + for (q = needle; *p && *q && tolower (*p) == tolower (*q); p++, q++) + ; + if (!*q) + return (haystack); + haystack++; } - /* Else return the pointer to the same place in the real search space */ - tmp2 = haystack; - for (tmp = localhaystack; tmp != found; tmp++) - tmp2++; - - free(localneedle); - free(localhaystack); - return tmp2; + return NULL; } char *strstrwrapper(char *haystack, char *needle)