Problem mit case insensitivem string
-
Ich wollte einen case insensitiven string haben. Dazu gibts einen artikel von Herb Sutter auf http://www.gotw.ca/gotw/029.htm. Das hab ich mal genommen, bekomme aber immer einen Absturz in compare mit dem Hinweis s1 sei ein Bad_Ptr. Kann mir einer sagen warum er so böse ist, der Zeiger? Plattform ist VC++ 2005.
#include <string> struct ci_char_traits : public std::char_traits<char> // just inherit all the other functions that we don't need to override { static bool eq( char c1, char c2 ) { return toupper(c1) == toupper(c2); } static bool ne( char c1, char c2 ) { return toupper(c1) != toupper(c2); } static bool lt( char c1, char c2 ) { return toupper(c1) < toupper(c2); } static int compare( const char* s1, const char* s2, size_t n ) { return _memicmp( s1, s2, n ); // if available on your compiler, otherwise you can roll your own } static const char* find( const char* s, int n, char a ) { while( n-- > 0 && toupper(*s) != toupper(a) ) { ++s; } return s; } }; typedef std::basic_string<char, ci_char_traits> ci_string; int main() { ci_string ip("Suche"); ci_string str("Hier steht nur irgendwelches Zeug drin."); ci_string::size_type pos = str.find(ip, 0); }
-
Das Problem dürfte sein, dass find irgendwann 0 zurückliefern sollte, und nicht (s+n). Ich hab zwar nicht im Standard geschaut, aber das deckt sich auch mit der Interpretation von Dinkumware und GNU, deshalb gehe ich davon aus dass das auch so gefordert ist.
Dadurch dass Dein find es nicht tut, sucht er immer weiter hinter dem String "Hier steht...", bis er irgendwann in ein illegales Segment gerät. Und dann knallt es.
-
Tatsache! Komisch, daß das Herb nicht aufgefallen ist. Der ist doch eigentlich recht angesehen und das Item auf seiner Seite recht alt. (Außerdem arbeitet er bei MS und die setzten doch Dinkumware ein, oder?)