aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/case_insensitive_string/case_insensitive_string.h
blob: 0d0504519028d85501d050395e5ecb747253fc8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once

#include "case_insensitive_char_traits.h"

#include <util/generic/strbuf.h>
#include <util/generic/string.h>
#include <util/generic/hash.h>
#include <util/string/split.h>

using TCaseInsensitiveString = TBasicString<char, TCaseInsensitiveCharTraits>;
using TCaseInsensitiveStringBuf = TBasicStringBuf<char, TCaseInsensitiveCharTraits>;

// WARN: comparison works incorrectly if strings contain null bytes (`TCaseInsensitiveAsciiString{"ab\0c", 4} == TCaseInsensitiveAsciiString{"ab\0d", 4}`).
using TCaseInsensitiveAsciiString = TBasicString<char, TCaseInsensitiveAsciiCharTraits>;
// WARN: comparison works incorrectly if strings contain null bytes.
using TCaseInsensitiveAsciiStringBuf = TBasicStringBuf<char, TCaseInsensitiveAsciiCharTraits>;

// Convert chars using std::tolower and hash the resulting string.
// Locale may affect the result.
size_t CaseInsensitiveStringHash(const char* s, size_t n) noexcept;
// Convert chars using AsciiToLower and hash the resulting string.
size_t CaseInsensitiveAsciiStringHash(const char* s, size_t n) noexcept;

template <>
struct THash<TCaseInsensitiveStringBuf> {
    size_t operator()(TCaseInsensitiveStringBuf str) const noexcept {
        return CaseInsensitiveStringHash(str.data(), str.size());
    }
};

template <>
struct THash<TCaseInsensitiveAsciiStringBuf> {
    size_t operator()(TCaseInsensitiveAsciiStringBuf str) const noexcept {
        return CaseInsensitiveAsciiStringHash(str.data(), str.size());
    }
};

template <>
struct THash<TCaseInsensitiveString> : THash<TCaseInsensitiveStringBuf> {};

template <>
struct THash<TCaseInsensitiveAsciiString> : THash<TCaseInsensitiveAsciiStringBuf> {};

namespace NStringSplitPrivate {

    template<>
    struct TStringBufOfImpl<TCaseInsensitiveStringBuf> {
        /*
         * WARN:
         * StringSplitter does not use TCharTraits properly.
         * Splitting such strings is explicitly disabled.
         */
        // using type = TCaseInsensitiveStringBuf;
    };

    template<>
    struct TStringBufOfImpl<TCaseInsensitiveString> : TStringBufOfImpl<TCaseInsensitiveStringBuf> {
    };

} // namespace NStringSplitPrivate

template <>
struct TEqualTo<TCaseInsensitiveString>: public TEqualTo<TCaseInsensitiveStringBuf> {
    using is_transparent = void;
};

template <>
struct TEqualTo<TCaseInsensitiveAsciiString>: public TEqualTo<TCaseInsensitiveAsciiStringBuf> {
    using is_transparent = void;
};