Skip to content

Instantly share code, notes, and snippets.

@kingsamchen
Created May 14, 2013 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingsamchen/5573414 to your computer and use it in GitHub Desktop.
Save kingsamchen/5573414 to your computer and use it in GitHub Desktop.
user_defined stl::basic_string traits
using std::basic_string;
using std::char_traits;
using std::string;
using std::toupper;;
struct ichar_traits : char_traits<char>
{
static bool eq(const char& _Left, const char& _Right)
{
return toupper(_Left) == toupper(_Right);
}
static bool lt(const char& _Left, const char& _Right)
{
return toupper(_Left) < toupper(_Right);
}
static int compare(const char_type* _Str1, const char_type* _Str2, size_t _Num )
{
const char_type* ps1 = _Str1;
const char_type* ps2 = _Str2;
for (size_t i = 0; i < _Num; ++i)
{
if (ps1 == NULL)
{
return -1;
}
else if (ps2 == NULL)
{
return 1;
}
else if (toupper(*ps1) < toupper(*ps2))
{
return -1;
}
else if (toupper(*ps1) > toupper(*ps2))
{
return 1;
}
assert(toupper(*ps1) == toupper(*ps2));
++ps1;
++ps2;
}
return 0;
}
};
typedef basic_string<char, ichar_traits> istring;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment