Commit dffad91b authored by Rasmus Villemoes's avatar Rasmus Villemoes Committed by akpm

lib/string.c: simplify str[c]spn

Use strchr(), which makes them a lot shorter, and more obviously symmetric
in their treatment of accept/reject.  It also saves a little bit of .text;
bloat-o-meter for an arm build says

Function                                     old     new   delta
strcspn                                       92      76     -16
strspn                                       108      76     -32

While here, also remove a stray empty line before EXPORT_SYMBOL().

Link: https://lkml.kernel.org/r/20220328224119.3003834-2-linux@rasmusvillemoes.dkSigned-off-by: default avatarRasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andy Shevchenko <andy@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent e0fa2ab3
...@@ -517,21 +517,13 @@ EXPORT_SYMBOL(strnlen); ...@@ -517,21 +517,13 @@ EXPORT_SYMBOL(strnlen);
size_t strspn(const char *s, const char *accept) size_t strspn(const char *s, const char *accept)
{ {
const char *p; const char *p;
const char *a;
size_t count = 0;
for (p = s; *p != '\0'; ++p) { for (p = s; *p != '\0'; ++p) {
for (a = accept; *a != '\0'; ++a) { if (!strchr(accept, *p))
if (*p == *a) break;
break;
}
if (*a == '\0')
return count;
++count;
} }
return count; return p - s;
} }
EXPORT_SYMBOL(strspn); EXPORT_SYMBOL(strspn);
#endif #endif
...@@ -544,17 +536,12 @@ EXPORT_SYMBOL(strspn); ...@@ -544,17 +536,12 @@ EXPORT_SYMBOL(strspn);
size_t strcspn(const char *s, const char *reject) size_t strcspn(const char *s, const char *reject)
{ {
const char *p; const char *p;
const char *r;
size_t count = 0;
for (p = s; *p != '\0'; ++p) { for (p = s; *p != '\0'; ++p) {
for (r = reject; *r != '\0'; ++r) { if (strchr(reject, *p))
if (*p == *r) break;
return count;
}
++count;
} }
return count; return p - s;
} }
EXPORT_SYMBOL(strcspn); EXPORT_SYMBOL(strcspn);
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment