case_insensitive_string.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include "case_insensitive_char_traits.h"
  3. #include <util/generic/strbuf.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/hash.h>
  6. #include <util/string/split.h>
  7. using TCaseInsensitiveString = TBasicString<char, TCaseInsensitiveCharTraits>;
  8. using TCaseInsensitiveStringBuf = TBasicStringBuf<char, TCaseInsensitiveCharTraits>;
  9. // WARN: comparison works incorrectly if strings contain null bytes (`TCaseInsensitiveAsciiString{"ab\0c", 4} == TCaseInsensitiveAsciiString{"ab\0d", 4}`).
  10. using TCaseInsensitiveAsciiString = TBasicString<char, TCaseInsensitiveAsciiCharTraits>;
  11. // WARN: comparison works incorrectly if strings contain null bytes.
  12. using TCaseInsensitiveAsciiStringBuf = TBasicStringBuf<char, TCaseInsensitiveAsciiCharTraits>;
  13. // Convert chars using std::tolower and hash the resulting string.
  14. // Locale may affect the result.
  15. size_t CaseInsensitiveStringHash(const char* s, size_t n) noexcept;
  16. // Convert chars using AsciiToLower and hash the resulting string.
  17. size_t CaseInsensitiveAsciiStringHash(const char* s, size_t n) noexcept;
  18. template <>
  19. struct THash<TCaseInsensitiveStringBuf> {
  20. size_t operator()(TCaseInsensitiveStringBuf str) const noexcept {
  21. return CaseInsensitiveStringHash(str.data(), str.size());
  22. }
  23. };
  24. template <>
  25. struct THash<TCaseInsensitiveAsciiStringBuf> {
  26. size_t operator()(TCaseInsensitiveAsciiStringBuf str) const noexcept {
  27. return CaseInsensitiveAsciiStringHash(str.data(), str.size());
  28. }
  29. };
  30. template <>
  31. struct THash<TCaseInsensitiveString> : THash<TCaseInsensitiveStringBuf> {};
  32. template <>
  33. struct THash<TCaseInsensitiveAsciiString> : THash<TCaseInsensitiveAsciiStringBuf> {};
  34. namespace NStringSplitPrivate {
  35. template<>
  36. struct TStringBufOfImpl<TCaseInsensitiveStringBuf> {
  37. /*
  38. * WARN:
  39. * StringSplitter does not use TCharTraits properly.
  40. * Splitting such strings is explicitly disabled.
  41. */
  42. // using type = TCaseInsensitiveStringBuf;
  43. };
  44. template<>
  45. struct TStringBufOfImpl<TCaseInsensitiveString> : TStringBufOfImpl<TCaseInsensitiveStringBuf> {
  46. };
  47. } // namespace NStringSplitPrivate
  48. template <>
  49. struct TEqualTo<TCaseInsensitiveString>: public TEqualTo<TCaseInsensitiveStringBuf> {
  50. using is_transparent = void;
  51. };
  52. template <>
  53. struct TEqualTo<TCaseInsensitiveAsciiString>: public TEqualTo<TCaseInsensitiveAsciiStringBuf> {
  54. using is_transparent = void;
  55. };