This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Bug in ci_string example in web pages


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]