Bug in ci_string example in web pages

Giovanni Bajo rasky@develer.com
Fri Jun 17 14:15:00 GMT 2005


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) ) {
      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.
Giovanni Bajo

More information about the Libstdc++ mailing list