aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unicode/normalization/normalization.h
blob: 4f5f57881c399df73b9605661c88ed28dac93b13 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#pragma once

#include "decomposition_table.h"

#include <util/charset/unidata.h>
#include <util/charset/wide.h>
#include <util/generic/hash.h>
#include <util/generic/vector.h>
#include <util/generic/algorithm.h>
#include <util/generic/singleton.h>
#include <util/generic/noncopyable.h>
#include <utility>

namespace NUnicode {
    enum ENormalization {
        NFD,
        NFC,
        NFKD,
        NFKC,
    };

    // Грубо говоря:
    // NFD расскладывает "ё" на "е + диакритику"
    // NFC сначала всё раскладывает, потом всё что может - складывает
    // NFKD делает то же, что и NFD. Кроме того, например, римскую IV (\x2163)
    //     превращает в латинские I и V
    // NFKC - NFKD + композиция (римская четвёрка из I и V, естественно, не образуется)

    // Формальная спецификация: http://www.unicode.org/reports/tr15/

    namespace NPrivate {
        inline const wchar32* Decomposition(const TDecompositionTable& table, wchar32 ch) {
            return table.Get(ch, static_cast<const wchar32*>(nullptr));
        }

        class TDecompositor {
        private:
            const TDecompositionTable& Table;

        public:
            inline TDecompositor(const TDecompositionTable& table)
                : Table(table)
            {
            }

            inline const wchar32* Decomposition(wchar32 ch) const {
                return NPrivate::Decomposition(Table, ch);
            }
        };

        template <bool IsCompat>
        struct TStandartDecompositor: public TDecompositor {
            TStandartDecompositor()
                : TDecompositor(NPrivate::DecompositionTable<IsCompat>())
            {
            }
        };

        template <ENormalization N>
        struct TShift;

        template <>
        struct TShift<NFD> {
            static const WC_TYPE Value = NFD_QC;
        };
        template <>
        struct TShift<NFC> {
            static const WC_TYPE Value = NFC_QC;
        };
        template <>
        struct TShift<NFKD> {
            static const WC_TYPE Value = NFKD_QC;
        };
        template <>
        struct TShift<NFKC> {
            static const WC_TYPE Value = NFKC_QC;
        };

        template <ENormalization N>
        inline bool Normalized(wchar32 ch) {
            return CharInfo(ch) & NPrivate::TShift<N>::Value;
        }

        class TComposition {
        private:
            struct TRawData {
                wchar32 Lead;
                wchar32 Tail;
                wchar32 Comp;
            };

            static const TRawData RawData[];
            static const size_t RawDataSize;

            class TKey: public std::pair<wchar32, wchar32> {
            public:
                inline TKey(wchar32 a, wchar32 b)
                    : std::pair<wchar32, wchar32>(a, b)
                {
                }

                inline size_t Hash() const {
                    return CombineHashes(first, second);
                }
            };

            template <class T>
            struct THash {
                inline size_t operator()(const T& t) const {
                    return t.Hash();
                }
            };

            typedef THashMap<TKey, wchar32, THash<TKey>> TData;
            TData Data;

        public:
            TComposition();

            inline wchar32 Composite(wchar32 lead, wchar32 tail) const {
                TData::const_iterator i = Data.find(TKey(lead, tail));
                if (i == Data.end())
                    return 0;

                return i->second;
            }
        };

        typedef std::pair<wchar32, TCombining> TSymbol;
        typedef TVector<TSymbol> TBuffer;

        template <bool doCompose>
        class TCompositor;

        template <>
        class TCompositor<false> {
        public:
            inline void DoComposition(TBuffer& buffer) {
                Y_UNUSED(buffer);
            }
        };

        template <>
        class TCompositor<true> {
        private:
            static const wchar32 NonComposite = 0;
            const TComposition* Composition;

        public:
            inline TCompositor()
                : Composition(Singleton<TComposition>())
            {
            }

            inline void DoComposition(TBuffer& buffer) {
                if (buffer.size() < 2)
                    return;

                const TSymbol& leadSymbol = buffer[0];
                if (leadSymbol.second != 0)
                    return;

                wchar32 lead = leadSymbol.first;
                bool oneMoreTurnPlease = false;
                do {
                    oneMoreTurnPlease = false;
                    TCombining lastCombining = 0;
                    for (TBuffer::iterator i = buffer.begin() + 1, mi = buffer.end(); i != mi; ++i) {
                        TCombining currentCombining = i->second;
                        if (!(currentCombining != lastCombining && currentCombining != 0 || lastCombining == 0 && currentCombining == 0))
                            continue;

                        lastCombining = currentCombining;
                        wchar32 comb = Composition->Composite(lead, i->first);
                        if (comb == NonComposite)
                            continue;

                        lead = comb;
                        buffer.erase(i);
                        oneMoreTurnPlease = true;
                        break;
                    }
                } while (oneMoreTurnPlease);

                Y_ASSERT(DecompositionCombining(lead) == 0);
                buffer[0] = TSymbol(lead, 0);
            }
        };

        template <ENormalization N, typename TCharType>
        inline bool Normalized(const TCharType* begin, const TCharType* end) {
            TCombining lastCanonicalClass = 0;
            for (const TCharType* i = begin; i != end;) {
                wchar32 ch = ReadSymbolAndAdvance(i, end);

                TCombining canonicalClass = DecompositionCombining(ch);
                if (lastCanonicalClass > canonicalClass && canonicalClass != 0)
                    return false;

                if (!Normalized<N>(ch))
                    return false;

                lastCanonicalClass = canonicalClass;
            }
            return true;
        }
    }

    template <bool compat>
    inline const wchar32* Decomposition(wchar32 ch) {
        return NPrivate::Decomposition(NPrivate::DecompositionTable<compat>(), ch);
    }

    template <ENormalization N, class TDecompositor = NPrivate::TDecompositor>
    class TNormalizer : NNonCopyable::TNonCopyable {
    private:
        static const ENormalization Norm = N;
        static const bool IsCompat = Norm == NFKD || Norm == NFKC;
        static const bool RequireComposition = Norm == NFC || Norm == NFKC;

        typedef NPrivate::TSymbol TSymbol;
        typedef NPrivate::TBuffer TBuffer;

        TBuffer Buffer;

        NPrivate::TCompositor<RequireComposition> Compositor;
        const TDecompositor& Decompositor;

    private:
        static inline bool Compare(const TSymbol& a, const TSymbol& b) {
            return a.second < b.second;
        }

        struct TComparer {
            inline bool operator()(const TSymbol& a, const TSymbol& b) {
                return Compare(a, b);
            }
        };

        template <class T>
        static inline void Write(const TBuffer::const_iterator& begin, const TBuffer::const_iterator& end, T& out) {
            for (TBuffer::const_iterator i = begin; i != end; ++i) {
                WriteSymbol(i->first, out);
            }
        }

        static inline void Write(const TBuffer::const_iterator& begin, const TBuffer::const_iterator& end, TUtf32String& out) {  // because WriteSymbol from util/charset/wide.h works wrong in this case
            for (TBuffer::const_iterator i = begin; i != end; ++i) {
                out += i->first;
            }
        }

        inline void SortBuffer() {
            if (Buffer.size() < 2)
                return;

            StableSort(Buffer.begin(), Buffer.end(), TComparer());
        }

        template <class T>
        inline void AddCharNoDecomposition(wchar32 c, T& out) {
            TCombining cc = DecompositionCombining(c);
            if (cc == 0) {
                SortBuffer();
                Buffer.push_back(TBuffer::value_type(c, cc));

                Compositor.DoComposition(Buffer);

                if (Buffer.size() > 1) {
                    Write(Buffer.begin(), Buffer.end() - 1, out);
                    Buffer.erase(Buffer.begin(), Buffer.end() - 1); // TODO I don't like this
                }
            } else {
                Buffer.push_back(TBuffer::value_type(c, cc));
            }
        }

        template <class T>
        inline void AddChar(wchar32 c, T& out) {
            const wchar32* decompBegin = Decompositor.Decomposition(c);
            if (decompBegin) {
                while (*decompBegin) {
                    Y_ASSERT(Decompositor.Decomposition(*decompBegin) == nullptr);
                    AddCharNoDecomposition(*(decompBegin++), out);
                }
                return;
            } else {
                AddCharNoDecomposition(c, out);
            }
        }

        template <class T, typename TCharType>
        inline void DoNormalize(const TCharType* begin, const TCharType* end, T& out) {
            Buffer.clear();

            for (const TCharType* i = begin; i != end;) {
                AddChar(ReadSymbolAndAdvance(i, end), out);
            }

            SortBuffer();
            Compositor.DoComposition(Buffer);
            Write(Buffer.begin(), Buffer.end(), out);
        }

    public:
        TNormalizer()
            : Decompositor(*Singleton<NPrivate::TStandartDecompositor<IsCompat>>())
        {
        }

        TNormalizer(const TDecompositor& decompositor)
            : Decompositor(decompositor)
        {
        }

        template <class T, typename TCharType>
        inline void Normalize(const TCharType* begin, const TCharType* end, T& out) {
            if (NPrivate::Normalized<Norm>(begin, end)) {
                for (const TCharType* i = begin; i != end; ++i) {
                    WriteSymbol(*i, out);
                }
            } else {
                DoNormalize(begin, end, out);
            }
        }

        template <typename TCharType>
        inline void Normalize(const TCharType* begin, const TCharType* end, TUtf32String& out) {
            if (NPrivate::Normalized<Norm>(begin, end)) {
                for (const TCharType* i = begin; i != end;) {
                    out += ReadSymbolAndAdvance(i, end);
                }
            } else {
                DoNormalize(begin, end, out);
            }
        }

        template <class T, typename TCharType>
        inline void Normalize(const TCharType* begin, size_t len, T& out) {
            return Normalize(begin, begin + len, out);
        }

        template <typename TCharType>
        inline TBasicString<TCharType> Normalize(const TBasicString<TCharType>& src) {
            if (NPrivate::Normalized<Norm>(src.begin(), src.end())) {
                // nothing to normalize
                return src;
            } else {
                TBasicString<TCharType> res;
                res.reserve(src.length());
                DoNormalize(src.begin(), src.end(), res);
                return res;
            }
        }
    };
}

//! decompose utf16 or utf32 string to any container supporting push_back or to T*
template <NUnicode::ENormalization Norm, class T, typename TCharType>
inline void Normalize(const TCharType* begin, size_t len, T& out) {
    ::NUnicode::TNormalizer<Norm> dec;
    dec.Normalize(begin, len, out);
}

template <NUnicode::ENormalization N, typename TCharType>
inline TBasicString<TCharType> Normalize(const TCharType* str, size_t len) {
    TBasicString<TCharType> res;
    res.reserve(len);

    Normalize<N>(str, len, res);

    return res;
}

template <NUnicode::ENormalization N, typename TCharType>
inline TBasicString<TCharType> Normalize(const TBasicString<TCharType>& str) {
    ::NUnicode::TNormalizer<N> dec;
    return dec.Normalize(str);
}

template <NUnicode::ENormalization N, typename TCharType>
inline TBasicString<TCharType> Normalize(const TBasicStringBuf<TCharType> str) {
    return Normalize<N>(str.data(), str.size());
}