This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Bug in ci_string example in web pages
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Giovanni Bajo <rasky at develer dot com>
- Cc: libstdc++ at gcc dot gnu dot org, pcarlini at suse dot de
- Date: Fri, 17 Jun 2005 15:28:44 +0100
- Subject: Re: Bug in ci_string example in web pages
- References: <063101c57346$faf6cb30$bf03030a@trilan>
On Fri, Jun 17, 2005 at 04:15:14PM +0200, Giovanni Bajo wrote:
> Hello,
>
> this page http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/gotw29a.txt
> (linked from v3 manual) contains an example of how to use a custom char
> trait to implement a case insensitive string (specialization of
> basic_string). The article is actually a newsgroup message by Herb Sutter.
>
> The problem is that the provided code contains a bug, in this function:
>
> static const char*
> find( const char* s, int n, char a ) {
> while( n-- > 0 && tolower(*s) != tolower(a) ) {
> ++s;
> }
> return s;
> }
>
> In fact, the find() function of a char trait is supposed to return 0 if the
> character is not found, while this implementation obviously never returns 0.
> A fixed version is:
>
> static const char*
> find(const char* s, int n, char a) {
> for (; n > 0; --n, ++s)
> if (eq(*s, a))
> return s;
> return 0;
> }
>
> I believe the article should be fixed (with an editor's note or something)
> or the users warned somehow about this.
Hi Giovanni,
maybe we should just link to www.gotw.ca instead of hosting the message?
Then we can punt the responsibility to Herb and ask him to fix it :)
Hmm, http://www.gotw.ca/gotw/029.htm has the same code, but his book has
this:
static const char*
find( const char* s, int n, char a ) {
while( n-- > 0 && tolower(*s) != tolower(a) ) {
++s;
}
return n >= 0 ? s : 0;
}
jon
--
"Once, during Prohibition, I was forced to live for days on nothing but
food and water."
- W.C. Fields