case_insensitive.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2006-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // This file implements a locale aware case insenstive equality predicate and
  5. // hash function. Unfortunately it still falls short of full
  6. // internationalization as it only deals with a single character at a time
  7. // (some languages have tricky cases where the characters in an upper case
  8. // string don't have a one-to-one correspondence with the lower case version of
  9. // the text, eg. )
  10. #if !defined(BOOST_HASH_EXAMPLES_CASE_INSENSITIVE_HEADER)
  11. #define BOOST_HASH_EXAMPLES_CASE_INSENSITIVE_HEADER
  12. #include <boost/algorithm/string/predicate.hpp>
  13. #include <boost/functional/hash.hpp>
  14. namespace hash_examples
  15. {
  16. struct iequal_to
  17. {
  18. iequal_to() {}
  19. explicit iequal_to(std::locale const& l) : locale_(l) {}
  20. template <typename String1, typename String2>
  21. bool operator()(String1 const& x1, String2 const& x2) const
  22. {
  23. return boost::algorithm::iequals(x1, x2, locale_);
  24. }
  25. private:
  26. std::locale locale_;
  27. };
  28. struct ihash
  29. {
  30. ihash() {}
  31. explicit ihash(std::locale const& l) : locale_(l) {}
  32. template <typename String>
  33. std::size_t operator()(String const& x) const
  34. {
  35. std::size_t seed = 0;
  36. for(typename String::const_iterator it = x.begin();
  37. it != x.end(); ++it)
  38. {
  39. boost::hash_combine(seed, std::toupper(*it, locale_));
  40. }
  41. return seed;
  42. }
  43. private:
  44. std::locale locale_;
  45. };
  46. }
  47. #endif