[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: memmem issues
From: |
Bruno Haible |
Subject: |
Re: memmem issues |
Date: |
Wed, 26 Dec 2007 18:17:55 +0100 |
User-agent: |
KMail/1.5.4 |
Hi Paul,
Most of your comments apply to all copies of the KMP code in gnulib.
> Eric Blake <address@hidden> writes:
> > + size_t *table = (size_t *) malloca (m * sizeof (size_t));
> > + if (table == NULL)
> > + return false;
>
> Shouldn't this check for overflow in the multiplication?
Yes, it should. I'm always lazy about this.
> > + unsigned char b = (unsigned char) needle[i - 1];
> > ...
> > + if (b == (unsigned char) needle[j])
>
> Would it be cleaner to declare 'b' to be of type 'char' and avoid the
> casts?
No; ISO C 99 section 7.21.4 says that when byte strings are compared the
elements are considered as 'unsigned char' values. Why risk bugs when the
approach is very simple: after fetching any 'char' from any of the strings,
cast it to 'unsigned char'. Simple rule, simple to remember, works fine.
> > + if ((unsigned char) needle[j] == (unsigned char) *phaystack)
>
> Can both casts be omitted?
Same answer.
> > + return memchr (haystack, (unsigned char) *Needle, haystack_len);
>
> Can the cast be omitted?
Same answer.
> > + return (void *) haystack;
>
> How about "return Haystack;"? That avoids a cast.
... but will not compile when a C++ compiler is used.
> (As you can tell, I prefer to avoid casts....)
As you can tell, I prefer to write code in the intersection of C and C++.
> > + if (needle_len == 0)
>
> Perhaps a __builtin_expect would be helpful here?
Hardly. Nowadays gcc knows that comparison with == against a particular
value is "unlikely", AFAIK. No need to help it with __builtin_expect here.
Bruno
- memmem issues, Eric Blake, 2007/12/19
- Re: memmem issues, Jim Meyering, 2007/12/20
- Re: memmem issues, Jim Meyering, 2007/12/20
- Re: memmem issues, Simon Josefsson, 2007/12/20
- Re: memmem issues, Paul Eggert, 2007/12/21
- Re: memmem issues, Paul Eggert, 2007/12/29
- Re: memmem issues, Bruno Haible, 2007/12/31
- Re: memmem issues, Paul Eggert, 2007/12/31
Re: memmem issues, Bruno Haible, 2007/12/26
Re: memmem issues, Bruno Haible, 2007/12/26