[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: |
Mon, 31 Dec 2007 11:42:34 +0100 |
User-agent: |
KMail/1.5.4 |
Ben Pfaff wrote:
> >> > + 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.
>
> The first cast to unsigned char quoted above seems to be
> unnecessary: assigning a value to an object of type unsigned char
> will implicitly convert it to unsigned char.
The answer was already in the mail to which you replied:
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.
When you omit the cast, the next person who does a refactoring of the code
and who was not aware that you were relying on implicit conversions will
introduce a bug.
Code maintainability is about clearly expressing the code's intent. Either
in comments or - here - through seemingly redundant casts.
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