httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Douglass <>
Subject Re: Too soon for 1.2 commits ??
Date Thu, 11 Jul 1996 06:03:04 GMT
On Wed, 10 Jul 1996, Alexei Kosut wrote:

> On Wed, 10 Jul 1996, Ben Laurie wrote:
> > For two reasons. Firstly, it burns memory. Secondly, I find it unlikely that
> > a pstrdup followed by 2 strtolowers followed by a strstr is any faster than
> > simply coding strcasestr standalone.
> > 
> > When measuring the efficiency of a function, the context in which it is used
> > is immaterial. The context determines the desirability of improving the
> > efficiency. Furthermore, if you leave a dangerous weapon like a memory-burning
> > strcasestr lying around, someone less wise than you will use it where it can
> > really do some damage.
> I think I'd tend to agree. There are certainly cases where one can say
> "this function sucks eggs, but it's not called very often, and it'd be a
> pain to make it efficient". But I don't think this is one of them -
> especially not for a simple utility function like this. How about:
> char *strcasestr(const char *s1, const char *s2) {
>     int len = strlen(s2);
>     while (*s1) {
> 	if (!strncasecmp(s1, s2, len))
> 	    return s1;
> 	s1++;
>     }
>     return NULL;
> }

So what you are saying is that it would be faster to step through the
entire string and to a strncasecmp on it?  Ok, lemme get this straight,
instead of doing the conversion to lower once on each string (i'll
agree that it would be better to use str_tolower())  doing the original
strstr, you are saying that you'd rather call strncasecmp steping
through the string one character at a time?  Isn't that even more
inefficient since you have to consider that eacy time it goes through
the strnCASEcmp it has to do some type of conversion on each character
as it compares it?  I may be off base here, but that seems like it
would be slower to me...

Heck, if you were really concerned about time and all, why not write it
like this:

char *strcasestr( const char *s1, const char s2) {
  int len = strlen(s2)
  char *ptr = s1 + len;

  while( s1 < ptr ) {
    if ( !strncasecmp( s1, s2, len ) )
      return s1;

  return NULL;

Michael Douglass
Texas Networking, Inc.

  "To be a saint is to be an exception; to be a true man is the rule.
   Err, fail, sin if you must, but be upright.  To sin as little as
   possible is the law for men; to sin not at all is a dream for angels."

              - Victor Hugo, "Les Miserables"

View raw message