Skip to contentSkip to conversation

2.6.19: slight performance optimization for lib/string.c's strstrip()

DISCUSSION Amit Choudhary 1 participant 1 message Dec 11, 2006

lore
Share & tools
lore
#0 lore raw
Quoted 13 lines

Suggested replacement:

char *strstrip(char *s)
{
size_t size;
char *end;

while (*s && isspace(*s))
s++;
if (!*s)
return s;
size = strlen(s);

Quoted 8 lines

end = s + size - 1;
while (end > s && isspace(*end))
end--;
*(end + 1) = '\0';

return s;
}
EXPORT_SYMBOL(strstrip);

How about this:

char *strstrip(char *s)

{
        size_t less = 0;
        char c = 0;
        char *e = NULL;

        while ((c=*s) && isspace(c))
                s++;
        if (!c)
                return s;

        e = s;

        while (c=*e) {
                 less = isspace(c) ? (less + 1) : 0;
                 e++;
        }

        *(e-less) = 0;

        return s;
}

1. no need to scan trailing spaces twice (once in strlen and then again).
2. pointer dereference only once per loop rather than multiple times.

Regards,
Amit

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com