aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/charset/ci_string.h
blob: 0daa2587b35bf153f00a0ceec5364e0059548dde (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once

#include "codepage.h"

#include <util/generic/string.h>
#include <util/str_stl.h>

// Same as TString but uses CASE INSENSITIVE comparator and hash. Use with care.
class TCiString: public TString {
public:
    TCiString() {
    }

    TCiString(const TString& s)
        : TString(s)
    {
    }

    TCiString(const TString& s, size_t pos, size_t n)
        : TString(s, pos, n)
    {
    }

    TCiString(const char* pc)
        : TString(pc)
    {
    }

    TCiString(const char* pc, size_t n)
        : TString(pc, n)
    {
    }

    TCiString(const char* pc, size_t pos, size_t n)
        : TString(pc, pos, n)
    {
    }

    TCiString(size_t n, char c)
        : TString(n, c)
    {
    }

    TCiString(const TUninitialized& uninitialized)
        : TString(uninitialized)
    {
    }

    TCiString(const char* b, const char* e)
        : TString(b, e)
    {
    }

    explicit TCiString(const TStringBuf& s)
        : TString(s)
    {
    }

    // ~~~ Comparison ~~~ : FAMILY0(int, compare)
    static int compare(const TCiString& s1, const TCiString& s2, const CodePage& cp = csYandex);
    static int compare(const char* p, const TCiString& s2, const CodePage& cp = csYandex);
    static int compare(const TCiString& s1, const char* p, const CodePage& cp = csYandex);
    static int compare(const TStringBuf& p1, const TStringBuf& p2, const CodePage& cp = csYandex);

    // TODO: implement properly in TString via enum ECaseSensitivity 
    static bool is_prefix(const TStringBuf& what, const TStringBuf& of, const CodePage& cp = csYandex);
    static bool is_suffix(const TStringBuf& what, const TStringBuf& of, const CodePage& cp = csYandex);

    bool StartsWith(const TStringBuf& s, const CodePage& cp = csYandex) const {
        return is_prefix(s, *this, cp);
    }

    bool EndsWith(const TStringBuf& s, const CodePage& cp = csYandex) const {
        return is_suffix(s, *this, cp);
    }

    friend bool operator==(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) == 0;
    }

    friend bool operator==(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) == 0;
    }

    friend bool operator==(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) == 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator==(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) {
        return TCiString::compare(s, pc) == 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator==(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) {
        return TCiString::compare(pc, s) == 0;
    }

    friend bool operator!=(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) != 0;
    }

    friend bool operator!=(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) != 0;
    }

    friend bool operator!=(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) != 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator!=(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) {
        return TCiString::compare(s, pc) != 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator!=(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) {
        return TCiString::compare(pc, s) != 0;
    }

    friend bool operator<(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) < 0;
    }

    friend bool operator<(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) < 0;
    }

    friend bool operator<(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) < 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator<(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) {
        return TCiString::compare(s, pc) < 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator<(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) {
        return TCiString::compare(pc, s) < 0;
    }

    friend bool operator<=(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) <= 0;
    }

    friend bool operator<=(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) <= 0;
    }

    friend bool operator<=(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) <= 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator<=(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) {
        return TCiString::compare(s, pc) <= 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator<=(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) {
        return TCiString::compare(pc, s) <= 0;
    }

    friend bool operator>(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) > 0;
    }

    friend bool operator>(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) > 0;
    }

    friend bool operator>(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) > 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator>(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) noexcept {
        return TCiString::compare(s, pc) > 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator>(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) noexcept {
        return TCiString::compare(pc, s) > 0;
    }

    friend bool operator>=(const TCiString& s1, const TCiString& s2) {
        return TCiString::compare(s1, s2) >= 0;
    }

    friend bool operator>=(const TCiString& s, const char* pc) {
        return TCiString::compare(s, pc) >= 0;
    }

    friend bool operator>=(const char* pc, const TCiString& s) {
        return TCiString::compare(pc, s) >= 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator>=(const TCiString& s, const TStringBase<TDerived2, TChar, TTraits2>& pc) {
        return TCiString::compare(s, pc) >= 0;
    }

    template <typename TDerived2, typename TTraits2>
    friend bool operator>=(const TStringBase<TDerived2, TChar, TTraits2>& pc, const TCiString& s) {
        return TCiString::compare(pc, s) >= 0;
    }

    static size_t hashVal(const char* pc, size_t len, const CodePage& cp = csYandex);

    size_t hash() const {
        return TCiString::hashVal(data(), length());
    }
};

struct ci_hash {
    inline size_t operator()(const char* s) const {
        return TCiString::hashVal(s, strlen(s));
    }
    inline size_t operator()(const TStringBuf& s) const {
        return TCiString::hashVal(s.data(), s.size());
    }
};

struct ci_hash32 { // not the same as ci_hash under 64-bit
    inline ui32 operator()(const char* s) const {
        return (ui32)TCiString::hashVal(s, strlen(s));
    }
};

//template <class T> struct hash;

template <>
struct hash<TCiString>: public ci_hash {
};

template <class T>
struct TCIHash {
};

template <>
struct TCIHash<const char*> {
    inline size_t operator()(const TStringBuf& s) const {
        return TCiString::hashVal(s.data(), s.size());
    }
};

template <>
struct TCIHash<TStringBuf> {
    inline size_t operator()(const TStringBuf& s) const {
        return TCiString::hashVal(s.data(), s.size());
    }
};

template <>
struct TCIHash<TString> {
    inline size_t operator()(const TString& s) const {
        return TCiString::hashVal(s.data(), s.size());
    }
};

struct ci_less {
    inline bool operator()(const char* x, const char* y) const {
        return csYandex.stricmp(x, y) < 0;
    }
};

struct ci_equal_to {
    inline bool operator()(const char* x, const char* y) const {
        return csYandex.stricmp(x, y) == 0;
    }
    // this implementation is not suitable for strings with zero characters inside, sorry
    bool operator()(const TStringBuf& x, const TStringBuf& y) const {
        return x.size() == y.size() && csYandex.strnicmp(x.data(), y.data(), y.size()) == 0;
    }
};

template <>
struct TEqualTo<TCiString>: public ci_equal_to {
};