lib/string.c: Improve strrchr
Subsystems (2)
PATCH 1/1 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 <[email protected]>
--- lib/string.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
lib/string.c +6 -6
@@ -321,12 +321,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
Signature · 1 line
2.1.3