J'ai écrit une version insensible à la casse de char_traits à utiliser avec std :: basic_string afin de générer une chaîne std :: qui ne respecte pas la casse lors des comparaisons, des recherches, etc. à l'aide des fonctions membres std :: basic_string intégrées.
En d'autres termes, je voulais faire quelque chose comme ça.
std::string a = "Hello, World!";
std::string b = "hello, world!";
assert( a == b );
... que std :: string ne peut pas gérer. Voici l'utilisation de mes nouveaux char_traits:
std::istring a = "Hello, World!";
std::istring b = "hello, world!";
assert( a == b );
... et voici l'implémentation:
/*  ---
        Case-Insensitive char_traits for std::string's
        Use:
            To declare a std::string which preserves case but ignores case in comparisons & search,
            use the following syntax:
                std::basic_string<char, char_traits_nocase<char> > noCaseString;
            A typedef is declared below which simplifies this use for chars:
                typedef std::basic_string<char, char_traits_nocase<char> > istring;
    --- */
    template<class C>
    struct char_traits_nocase : public std::char_traits<C>
    {
        static bool eq( const C& c1, const C& c2 )
        { 
            return ::toupper(c1) == ::toupper(c2); 
        }
        static bool lt( const C& c1, const C& c2 )
        { 
            return ::toupper(c1) < ::toupper(c2);
        }
        static int compare( const C* s1, const C* s2, size_t N )
        {
            return _strnicmp(s1, s2, N);
        }
        static const char* find( const C* s, size_t N, const C& a )
        {
            for( size_t i=0 ; i<N ; ++i )
            {
                if( ::toupper(s[i]) == ::toupper(a) ) 
                    return s+i ;
            }
            return 0 ;
        }
        static bool eq_int_type( const int_type& c1, const int_type& c2 )
        { 
            return ::toupper(c1) == ::toupper(c2) ; 
        }       
    };
    template<>
    struct char_traits_nocase<wchar_t> : public std::char_traits<wchar_t>
    {
        static bool eq( const wchar_t& c1, const wchar_t& c2 )
        { 
            return ::towupper(c1) == ::towupper(c2); 
        }
        static bool lt( const wchar_t& c1, const wchar_t& c2 )
        { 
            return ::towupper(c1) < ::towupper(c2);
        }
        static int compare( const wchar_t* s1, const wchar_t* s2, size_t N )
        {
            return _wcsnicmp(s1, s2, N);
        }
        static const wchar_t* find( const wchar_t* s, size_t N, const wchar_t& a )
        {
            for( size_t i=0 ; i<N ; ++i )
            {
                if( ::towupper(s[i]) == ::towupper(a) ) 
                    return s+i ;
            }
            return 0 ;
        }
        static bool eq_int_type( const int_type& c1, const int_type& c2 )
        { 
            return ::towupper(c1) == ::towupper(c2) ; 
        }       
    };
    typedef std::basic_string<char, char_traits_nocase<char> > istring;
    typedef std::basic_string<wchar_t, char_traits_nocase<wchar_t> > iwstring;
               
              
std::stricmp. Sinon, lisez ce que Herb a à dire .