From: "André Goddard Rosa" <andre.goddard@gmail.com>
To: jengelh@medozas.de, linux-kernel@vger.kernel.org,
James.Bottomley@hansenpartnership.com
Cc: "André Goddard Rosa" <andre.goddard@gmail.com>
Subject: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available
Date: Sat, 7 Nov 2009 14:23:50 -0200 [thread overview]
Message-ID: <c7d3b02b5e28eaa54a5360d57dfd177c44320187.1257602781.git.andre.goddard@gmail.com> (raw)
In-Reply-To: <cover.1257602781.git.andre.goddard@gmail.com>
On the following sentence:
while (*s && isspace(*s))
s++;
If *s == 0, isspace() evaluates to ((_ctype[*s] & 0x20) != 0), which
evaluates to ((0x08 & 0x20) != 0) which equals to 0 as well.
If *s == 1, we depend on isspace() result anyway.
In other words, "a char equals zero is never a space". So remove this check.
Also, *s != 0 is by far the most common case (non-empty string).
Fixed const return as noticed by Jan Engelhardt and James Bottomley
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
---
include/linux/string.h | 1 +
lib/string.c | 19 +++++++++++++++----
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index b850886..3bba9ee 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int);
#ifndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,int);
#endif
+extern char * __must_check skip_spaces(const char *);
extern char * __must_check strstrip(char *);
#ifndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *);
diff --git a/lib/string.c b/lib/string.c
index b19b87a..d9a51d5 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -330,6 +330,20 @@ EXPORT_SYMBOL(strnchr);
#endif
/**
+ * skip_spaces - Removes leading whitespace from @s.
+ * @s: The string to be stripped.
+ *
+ * Returns a pointer to the first non-whitespace character in @s.
+ */
+char *skip_spaces(const char *str)
+{
+ while (isspace(*str))
+ ++str;
+ return str;
+}
+EXPORT_SYMBOL(skip_spaces);
+
+/**
* strstrip - Removes leading and trailing whitespace from @s.
* @s: The string to be stripped.
*
@@ -352,10 +366,7 @@ char *strstrip(char *s)
end--;
*(end + 1) = '\0';
- while (*s && isspace(*s))
- s++;
-
- return s;
+ return (char *)skip_spaces(s);
}
EXPORT_SYMBOL(strstrip);
--
1.6.5.2.153.g6e31f.dirty
next parent reply other threads:[~2009-11-08 16:25 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1257602781.git.andre.goddard@gmail.com>
2009-11-07 16:23 ` André Goddard Rosa [this message]
2009-11-08 15:54 ` [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available Jan Engelhardt
2009-11-08 16:38 ` James Bottomley
2009-11-08 16:54 ` Jan Engelhardt
2009-11-08 16:59 ` André Goddard Rosa
2009-11-08 16:56 ` Jan Engelhardt
[not found] ` <20091108165000.374714cb@lxorguk.ukuu.org.uk>
2009-11-09 14:02 ` André Goddard Rosa
2009-11-07 16:33 ` [PATCH v4 08/12] vsprintf: reuse almost identical simple_strtoulX() functions André Goddard Rosa
[not found] ` <643e6260a571b533d8985b377a82410aded4ddae.1257602781.git.andre.goddard@gmail.com>
2009-11-08 15:37 ` [PATCH v4 01/12] vsprintf: factorize "(null)" string Jan Engelhardt
2009-11-08 15:49 ` André Goddard Rosa
2009-11-09 3:28 ` Rusty Russell
2009-11-10 14:33 ` André Goddard Rosa
[not found] ` <7206ef594e67a240a842339f520284de6569b1fc.1257602781.git.andre.goddard@gmail.com>
2009-11-08 15:44 ` [PATCH v4 07/12] vsprintf: factor out skip_space code in a separate function Jan Engelhardt
2009-11-08 15:52 ` André Goddard Rosa
[not found] ` <31525.1257770343@redhat.com>
2009-11-09 15:31 ` André Goddard Rosa
[not found] ` <1257696303.4184.8.camel@mulgrave.site>
[not found] ` <b8bf37780911080852l10d11f4ele5bf6a6aed94c5fe@mail.gmail.com>
2009-11-08 16:55 ` [dm-devel] [PATCH v4 00/12] introduce skip_spaces(), reducing code size plus some clean-ups Jan Engelhardt
2009-11-08 17:02 ` Alexey Dobriyan
[not found] ` <7d5883637aa976b54e944998f635d47a41618a75.1257602781.git.andre.goddard@gmail.com>
2009-11-14 4:20 ` [ibm-acpi-devel] [PATCH v4 12/12] tree-wide: convert open calls to remove spaces to skip_spaces() lib function Henrique de Moraes Holschuh
2009-11-14 7:44 ` André Goddard Rosa
[not found] ` <20091108184722.GA1647@mit.edu>
[not found] ` <Pine.LNX.4.64.0911082103000.8143@ask.diku.dk>
2009-11-15 6:19 ` André Goddard Rosa
2009-11-15 6:37 ` André Goddard Rosa
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=c7d3b02b5e28eaa54a5360d57dfd177c44320187.1257602781.git.andre.goddard@gmail.com \
--to=andre.goddard@gmail.com \
--cc=James.Bottomley@hansenpartnership.com \
--cc=jengelh@medozas.de \
--cc=linux-kernel@vger.kernel.org \
/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 NNTP newsgroup(s).