public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Sichen Zhao <1473996754@qq.com>
To: newlib@sourceware.org
Cc: gedare@rtems.org,	joel@rtems.org,
	christian.mauderer@embedded-brains.de,
	sebastian.huber@embedded-brains.de,
	Sichen Zhao <1473996754@qq.com>
Subject: [PATCH] Modify strnstr.c.
Date: Mon, 28 Aug 2017 08:38:00 -0000	[thread overview]
Message-ID: <1503754090-28619-1-git-send-email-1473996754@qq.com> (raw)

---
 newlib/libc/string/strnstr.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/newlib/libc/string/strnstr.c b/newlib/libc/string/strnstr.c
index da5e5bd..7c87bbd 100644
--- a/newlib/libc/string/strnstr.c
+++ b/newlib/libc/string/strnstr.c
@@ -44,22 +44,14 @@ __FBSDID("$FreeBSD: head/lib/libc/string/strnstr.c 251069 2013-05-28 20:57:40Z e
  * first slen characters of s.
  */
 char *
-strnstr(const char *s, const char *find, size_t slen)
+strnstr(const char *haystack, const char *needle, size_t haystack_len)
 {
-	char c, sc;
-	size_t len;
+  size_t needle_len = strnlen(needle, haystack_len);
 
-	if ((c = *find++) != '\0') {
-		len = strlen(find);
-		do {
-			do {
-				if (slen-- < 1 || (sc = *s++) == '\0')
-					return (NULL);
-			} while (sc != c);
-			if (len > slen)
-				return (NULL);
-		} while (strncmp(s, find, len) != 0);
-		s--;
-	}
-	return ((char *)s);
+  if (needle_len < haystack_len || !needle[needle_len]) {
+    char *x = memmem(haystack, haystack_len, needle, needle_len);
+    if (x && !memchr(haystack, 0, x - haystack))
+      return x;
+  }
+  return NULL;
 }
-- 
2.7.4



             reply	other threads:[~2017-08-26 13:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-28  8:38 Sichen Zhao [this message]
2017-08-28  8:57 ` Corinna Vinschen
2017-08-28 10:50   ` Sichen Zhao
2017-08-28 13:38   ` Sichen Zhao
2017-08-28 14:43     ` Corinna Vinschen
2017-08-28 15:15   ` Eric Blake
2017-08-28 15:32     ` Sichen Zhao
2017-08-28 16:09       ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1503754090-28619-1-git-send-email-1473996754@qq.com \
    --to=1473996754@qq.com \
    --cc=christian.mauderer@embedded-brains.de \
    --cc=gedare@rtems.org \
    --cc=joel@rtems.org \
    --cc=newlib@sourceware.org \
    --cc=sebastian.huber@embedded-brains.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).