From 710583064f25511f85154e269d0595b89122de94 Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Mon, 2 Jul 2007 15:45:13 +0000 Subject: [PATCH] in nstrcasestr(), mbstrcasestr(), revstrstr(), revstrcasestr(), and mbrevstrcasestr(), for efficiency, return haystack immediately if needle is blank git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4122 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 6 ++++++ src/chars.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ChangeLog b/ChangeLog index 4dc67f4a..d1011fd5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-07-02 David Lawrence Ramsey + + * chars.c (nstrcasestr, mbstrcasestr, revstrstr, revstrcasestr, + mbrevstrcasestr): For efficiency, return haystack immediately if + needle is blank. + 2007-07-01 David Lawrence Ramsey * chars.c (nstrncasecmp, mbstrncasecmp): For efficiency, return diff --git a/src/chars.c b/src/chars.c index 9a79065a..888c035f 100644 --- a/src/chars.c +++ b/src/chars.c @@ -606,6 +606,9 @@ const char *nstrcasestr(const char *haystack, const char *needle) { assert(haystack != NULL && needle != NULL); + if (needle == '\0') + return haystack; + for (; *haystack != '\0'; haystack++) { const char *r = haystack, *q = needle; @@ -631,6 +634,9 @@ const char *mbstrcasestr(const char *haystack, const char *needle) assert(haystack != NULL && needle != NULL); + if (needle == '\0') + return haystack; + r_mb = charalloc(MB_CUR_MAX); q_mb = charalloc(MB_CUR_MAX); @@ -690,6 +696,9 @@ const char *revstrstr(const char *haystack, const char *needle, const { assert(haystack != NULL && needle != NULL && rev_start != NULL); + if (needle == '\0') + return rev_start; + for (; rev_start >= haystack; rev_start--) { const char *r, *q; @@ -712,6 +721,9 @@ const char *revstrcasestr(const char *haystack, const char *needle, { assert(haystack != NULL && needle != NULL && rev_start != NULL); + if (needle == '\0') + return rev_start; + for (; rev_start >= haystack; rev_start--) { const char *r = rev_start, *q = needle; @@ -739,6 +751,9 @@ const char *mbrevstrcasestr(const char *haystack, const char *needle, assert(haystack != NULL && needle != NULL && rev_start != NULL); + if (needle == '\0') + return rev_start; + r_mb = charalloc(MB_CUR_MAX); q_mb = charalloc(MB_CUR_MAX); -- 2.39.5