driver core: Use strcspn(3) instead of open-coding it
PATCH 1/1 driver core: Use strcspn(3) instead of open-coding it
This code was an obfuscated way of spelling strcspn(3).
Signed-off-by Alejandro Colomar <[email protected]>
---
Hi,
I wasn't sure what to do with the existing comments. I've merged them, but feel free to suggest something else.
BTW, the strnchr(3) call was superfluous. It would have been fine to call strchr(3), and it would be faster. Since the input is required to be a string already (we call strlen(3) just before, so I assume that), strchr(3) would have been equivalent. Also, strchrnul(3) would have reduced a branch. And given that strchrnul(3) works fine on empty strings, the outer conditional is superfluous.
So, this is really a multi-step simplification.
1)
- cp = strnchr(s, len, '\n'); + cp = strchr(s, '\n'); if (cp) len = cp - s;
2)
- cp = strchr(s, '\n'); - if (cp) - len = cp - s; + cp = strchrnul(s, '\n'); + len = cp - s;
3)
+ char *cp;
+
- if (len) {
- char *cp;
-
cp = strchrnul(s, '\n');
len = cp - s;
- }4)
- char *cp; - - cp = strchrnul(s, '\n'); - len = cp - s; + len = strcspn(s, "\n");
Have a lovely day!
Alex
drivers/base/dd.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/base/dd.c +3 -10
@@ -396,17 +396,10 @@ int __device_set_driver_override(struct device *dev, const char *s, size_t len)/* * Compute the real length of the string in case userspace sends us a * bunch of \0 characters like python likes to do. +* +* Handle trailing newline. */ - len =strlen (s); - -/* Handle trailing newline */ -if (len) { -char *cp; - - cp =strnchr (s, len,'\n' ); -if (cp) - len = cp - s; - } + len =strcspn (s,"\n" );/* * If empty string or "\n" passed, new remains NULL, clearing
Signature · 1 line
2.55.0