From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Date: Fri, 13 Feb 2015 22:36:44 +0000 (-0800)
Subject: lib/string.c: improve strrchr()
X-Git-Tag: v4.0-rc1~90^2~91
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8da53d4595a53fb9a3380dd4d1c9bc24c7c9aab8;p=users%2Fdwmw2%2Flinux.git

lib/string.c: improve strrchr()

Instead of potentially passing over the string twice in case c is not
found, just keep track of the last occurrence.  According to
bloat-o-meter, this also cuts the generated code by a third (54 vs 36
bytes).  Oh, and we get rid of those 7-space indented lines.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

diff --git a/lib/string.c b/lib/string.c
index 3206d01782969..cdd97f431ae26 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -313,12 +313,12 @@ EXPORT_SYMBOL(strchrnul);
  */
 char *strrchr(const char *s, int c)
 {
-       const char *p = s + strlen(s);
-       do {
-           if (*p == (char)c)
-               return (char *)p;
-       } while (--p >= s);
-       return NULL;
+	const char *last = NULL;
+	do {
+		if (*s == (char)c)
+			last = s;
+	} while (*s++);
+	return (char *)last;
 }
 EXPORT_SYMBOL(strrchr);
 #endif