This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Bug in ci_string example in web pages
- From: "Giovanni Bajo" <rasky at develer dot com>
- To: <libstdc++ at gcc dot gnu dot org>
- Cc: <pcarlini at suse dot de>
- Date: Fri, 17 Jun 2005 16:15:14 +0200
- Subject: Bug in ci_string example in web pages
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.
--
Giovanni Bajo