2009-11-08 16:25:24

by André Goddard Rosa

[permalink] [raw]
Subject: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available

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 <[email protected]>
---
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


2009-11-08 15:54:08

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available


On Saturday 2009-11-07 16:16, André Goddard Rosa wrote:

> /**
>+ * skip_spaces - Removes leading whitespace from @s.
>+ * @s: The string to be stripped.
>+ *
>+ * Returns a pointer to the first non-whitespace character in @s.
>+ */
>+const char *skip_spaces(const char *str)
>+{
>+ while (isspace(*str))
>+ ++str;
>+ return str;
>+}
>+EXPORT_SYMBOL(skip_spaces);
>+

I would make this
char *skip_spaces(const char *)

just like most of the stdc functions, so that you do not need
ugly casts like this (v) in callers of skip_spaces.

>- while (*s && isspace(*s))
>- s++;
>-
>- return s;
>+ return (char *)skip_spaces(s);
> }
> EXPORT_SYMBOL(strstrip);

2009-11-08 16:38:07

by James Bottomley

[permalink] [raw]
Subject: Re: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available

On Sat, 2009-11-07 at 14:23 -0200, André Goddard Rosa wrote:
> + * 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)

OK, so this now becomes a bad interface because it silently promotes
const to non const ... and I'm sure the compiler would warn about this
too in string.c ... did this get compiled?

James

2009-11-08 16:54:09

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available


On Sunday 2009-11-08 17:38, James Bottomley wrote:

>On Sat, 2009-11-07 at 14:23 -0200, André Goddard Rosa wrote:
>> + * 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)
>
>OK, so this now becomes a bad interface because it silently promotes
>const to non const ...

strchr, etc. all do the same. Do you think they are bad too?

2009-11-08 16:56:33

by Jan Engelhardt

[permalink] [raw]
Subject: Re: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available


On Saturday 2009-11-07 17:30, André Goddard Rosa wrote:

>+char *skip_spaces(const char *str)

>@@ -352,10 +366,7 @@ char *strstrip(char *s)
> end--;
> *(end + 1) = '\0';
>
>- while (*s && isspace(*s))
>- s++;
>-
>- return s;
>+ return (char *)skip_spaces(s);
> }

sparse would probably say "how much more casting to the same type do you want?"
It is already char*.

2009-11-08 16:59:23

by André Goddard Rosa

[permalink] [raw]
Subject: Re: [PATCH v4 10/12] string: factorize skip_spaces and export it to be generally available

On Sun, Nov 8, 2009 at 2:54 PM, Jan Engelhardt <[email protected]> wrote:
>
> On Sunday 2009-11-08 17:38, James Bottomley wrote:
>
>>On Sat, 2009-11-07 at 14:23 -0200, Andr? Goddard Rosa wrote:
>>> + * 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)
>>
>>OK, so this now becomes a bad interface because it silently promotes
>>const to non const ...
>
> strchr, etc. all do the same. Do you think they are bad too?

Yeap, I just based on those.

James, what would you suggest instead?

Thanks,
Andr?