//: C03:iwchar_traits.h {-g++} // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Creating your own wide-character traits. #ifndef IWCHAR_TRAITS_H #define IWCHAR_TRAITS_H #include #include #include #include #include #include using std::allocator; using std::basic_string; using std::char_traits; using std::size_t; using std::towlower; using std::towupper; using std::wostream; using std::wstring; struct iwchar_traits : char_traits { // We'll only change character-by- // character comparison functions static bool eq(wchar_t c1st, wchar_t c2nd) { return towupper(c1st) == towupper(c2nd); } static bool ne(wchar_t c1st, wchar_t c2nd) { return towupper(c1st) != towupper(c2nd); } static bool lt(wchar_t c1st, wchar_t c2nd) { return towupper(c1st) < towupper(c2nd); } static int compare( const wchar_t* str1, const wchar_t* str2, size_t n) { for(size_t i = 0; i < n; i++) { if(str1 == 0) return -1; else if(str2 == 0) return 1; else if(towlower(*str1) < towlower(*str2)) return -1; else if(towlower(*str1) > towlower(*str2)) return 1; assert(towlower(*str1) == towlower(*str2)); ++str1; ++str2; // Compare the other wchar_ts } return 0; } static const wchar_t* find(const wchar_t* s1, size_t n, wchar_t c) { while(n-- > 0) if(towupper(*s1) == towupper(c)) return s1; else ++s1; return 0; } }; typedef basic_string iwstring; inline wostream& operator<<(wostream& os, const iwstring& s) { return os << wstring(s.c_str(), s.length()); } #endif // IWCHAR_TRAITS_H ///:~