Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : /*
4 : *******************************************************************************
5 : * Copyright (C) 2014, International Business Machines
6 : * Corporation and others. All Rights Reserved.
7 : *******************************************************************************
8 : * loadednormalizer2impl.h
9 : *
10 : * created on: 2014sep07
11 : * created by: Markus W. Scherer
12 : */
13 :
14 : #ifndef __NORM2ALLMODES_H__
15 : #define __NORM2ALLMODES_H__
16 :
17 : #include "unicode/utypes.h"
18 :
19 : #if !UCONFIG_NO_NORMALIZATION
20 :
21 : #include "unicode/normalizer2.h"
22 : #include "unicode/unistr.h"
23 : #include "cpputils.h"
24 : #include "normalizer2impl.h"
25 :
26 : U_NAMESPACE_BEGIN
27 :
28 : // Intermediate class:
29 : // Has Normalizer2Impl and does boilerplate argument checking and setup.
30 : class Normalizer2WithImpl : public Normalizer2 {
31 : public:
32 20 : Normalizer2WithImpl(const Normalizer2Impl &ni) : impl(ni) {}
33 : virtual ~Normalizer2WithImpl();
34 :
35 : // normalize
36 : virtual UnicodeString &
37 0 : normalize(const UnicodeString &src,
38 : UnicodeString &dest,
39 : UErrorCode &errorCode) const {
40 0 : if(U_FAILURE(errorCode)) {
41 0 : dest.setToBogus();
42 0 : return dest;
43 : }
44 0 : const UChar *sArray=src.getBuffer();
45 0 : if(&dest==&src || sArray==NULL) {
46 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
47 0 : dest.setToBogus();
48 0 : return dest;
49 : }
50 0 : dest.remove();
51 0 : ReorderingBuffer buffer(impl, dest);
52 0 : if(buffer.init(src.length(), errorCode)) {
53 0 : normalize(sArray, sArray+src.length(), buffer, errorCode);
54 : }
55 0 : return dest;
56 : }
57 : virtual void
58 : normalize(const UChar *src, const UChar *limit,
59 : ReorderingBuffer &buffer, UErrorCode &errorCode) const = 0;
60 :
61 : // normalize and append
62 : virtual UnicodeString &
63 0 : normalizeSecondAndAppend(UnicodeString &first,
64 : const UnicodeString &second,
65 : UErrorCode &errorCode) const {
66 0 : return normalizeSecondAndAppend(first, second, TRUE, errorCode);
67 : }
68 : virtual UnicodeString &
69 0 : append(UnicodeString &first,
70 : const UnicodeString &second,
71 : UErrorCode &errorCode) const {
72 0 : return normalizeSecondAndAppend(first, second, FALSE, errorCode);
73 : }
74 : UnicodeString &
75 0 : normalizeSecondAndAppend(UnicodeString &first,
76 : const UnicodeString &second,
77 : UBool doNormalize,
78 : UErrorCode &errorCode) const {
79 0 : uprv_checkCanGetBuffer(first, errorCode);
80 0 : if(U_FAILURE(errorCode)) {
81 0 : return first;
82 : }
83 0 : const UChar *secondArray=second.getBuffer();
84 0 : if(&first==&second || secondArray==NULL) {
85 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
86 0 : return first;
87 : }
88 0 : int32_t firstLength=first.length();
89 0 : UnicodeString safeMiddle;
90 : {
91 0 : ReorderingBuffer buffer(impl, first);
92 0 : if(buffer.init(firstLength+second.length(), errorCode)) {
93 0 : normalizeAndAppend(secondArray, secondArray+second.length(), doNormalize,
94 0 : safeMiddle, buffer, errorCode);
95 : }
96 : } // The ReorderingBuffer destructor finalizes the first string.
97 0 : if(U_FAILURE(errorCode)) {
98 : // Restore the modified suffix of the first string.
99 0 : first.replace(firstLength-safeMiddle.length(), 0x7fffffff, safeMiddle);
100 : }
101 0 : return first;
102 : }
103 : virtual void
104 : normalizeAndAppend(const UChar *src, const UChar *limit, UBool doNormalize,
105 : UnicodeString &safeMiddle,
106 : ReorderingBuffer &buffer, UErrorCode &errorCode) const = 0;
107 : virtual UBool
108 0 : getDecomposition(UChar32 c, UnicodeString &decomposition) const {
109 : UChar buffer[4];
110 : int32_t length;
111 0 : const UChar *d=impl.getDecomposition(c, buffer, length);
112 0 : if(d==NULL) {
113 0 : return FALSE;
114 : }
115 0 : if(d==buffer) {
116 0 : decomposition.setTo(buffer, length); // copy the string (Jamos from Hangul syllable c)
117 : } else {
118 0 : decomposition.setTo(FALSE, d, length); // read-only alias
119 : }
120 0 : return TRUE;
121 : }
122 : virtual UBool
123 0 : getRawDecomposition(UChar32 c, UnicodeString &decomposition) const {
124 : UChar buffer[30];
125 : int32_t length;
126 0 : const UChar *d=impl.getRawDecomposition(c, buffer, length);
127 0 : if(d==NULL) {
128 0 : return FALSE;
129 : }
130 0 : if(d==buffer) {
131 0 : decomposition.setTo(buffer, length); // copy the string (algorithmic decomposition)
132 : } else {
133 0 : decomposition.setTo(FALSE, d, length); // read-only alias
134 : }
135 0 : return TRUE;
136 : }
137 : virtual UChar32
138 0 : composePair(UChar32 a, UChar32 b) const {
139 0 : return impl.composePair(a, b);
140 : }
141 :
142 : virtual uint8_t
143 0 : getCombiningClass(UChar32 c) const {
144 0 : return impl.getCC(impl.getNorm16(c));
145 : }
146 :
147 : // quick checks
148 : virtual UBool
149 0 : isNormalized(const UnicodeString &s, UErrorCode &errorCode) const {
150 0 : if(U_FAILURE(errorCode)) {
151 0 : return FALSE;
152 : }
153 0 : const UChar *sArray=s.getBuffer();
154 0 : if(sArray==NULL) {
155 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
156 0 : return FALSE;
157 : }
158 0 : const UChar *sLimit=sArray+s.length();
159 0 : return sLimit==spanQuickCheckYes(sArray, sLimit, errorCode);
160 : }
161 : virtual UNormalizationCheckResult
162 0 : quickCheck(const UnicodeString &s, UErrorCode &errorCode) const {
163 0 : return Normalizer2WithImpl::isNormalized(s, errorCode) ? UNORM_YES : UNORM_NO;
164 : }
165 : virtual int32_t
166 0 : spanQuickCheckYes(const UnicodeString &s, UErrorCode &errorCode) const {
167 0 : if(U_FAILURE(errorCode)) {
168 0 : return 0;
169 : }
170 0 : const UChar *sArray=s.getBuffer();
171 0 : if(sArray==NULL) {
172 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
173 0 : return 0;
174 : }
175 0 : return (int32_t)(spanQuickCheckYes(sArray, sArray+s.length(), errorCode)-sArray);
176 : }
177 : virtual const UChar *
178 : spanQuickCheckYes(const UChar *src, const UChar *limit, UErrorCode &errorCode) const = 0;
179 :
180 0 : virtual UNormalizationCheckResult getQuickCheck(UChar32) const {
181 0 : return UNORM_YES;
182 : }
183 :
184 : const Normalizer2Impl &impl;
185 : };
186 :
187 : class DecomposeNormalizer2 : public Normalizer2WithImpl {
188 : public:
189 5 : DecomposeNormalizer2(const Normalizer2Impl &ni) : Normalizer2WithImpl(ni) {}
190 : virtual ~DecomposeNormalizer2();
191 :
192 : private:
193 : virtual void
194 0 : normalize(const UChar *src, const UChar *limit,
195 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
196 0 : impl.decompose(src, limit, &buffer, errorCode);
197 0 : }
198 : using Normalizer2WithImpl::normalize; // Avoid warning about hiding base class function.
199 : virtual void
200 0 : normalizeAndAppend(const UChar *src, const UChar *limit, UBool doNormalize,
201 : UnicodeString &safeMiddle,
202 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
203 0 : impl.decomposeAndAppend(src, limit, doNormalize, safeMiddle, buffer, errorCode);
204 0 : }
205 : virtual const UChar *
206 0 : spanQuickCheckYes(const UChar *src, const UChar *limit, UErrorCode &errorCode) const {
207 0 : return impl.decompose(src, limit, NULL, errorCode);
208 : }
209 : using Normalizer2WithImpl::spanQuickCheckYes; // Avoid warning about hiding base class function.
210 0 : virtual UNormalizationCheckResult getQuickCheck(UChar32 c) const {
211 0 : return impl.isDecompYes(impl.getNorm16(c)) ? UNORM_YES : UNORM_NO;
212 : }
213 0 : virtual UBool hasBoundaryBefore(UChar32 c) const { return impl.hasDecompBoundary(c, TRUE); }
214 0 : virtual UBool hasBoundaryAfter(UChar32 c) const { return impl.hasDecompBoundary(c, FALSE); }
215 0 : virtual UBool isInert(UChar32 c) const { return impl.isDecompInert(c); }
216 : };
217 :
218 : class ComposeNormalizer2 : public Normalizer2WithImpl {
219 : public:
220 10 : ComposeNormalizer2(const Normalizer2Impl &ni, UBool fcc) :
221 10 : Normalizer2WithImpl(ni), onlyContiguous(fcc) {}
222 : virtual ~ComposeNormalizer2();
223 :
224 : private:
225 : virtual void
226 0 : normalize(const UChar *src, const UChar *limit,
227 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
228 0 : impl.compose(src, limit, onlyContiguous, TRUE, buffer, errorCode);
229 0 : }
230 : using Normalizer2WithImpl::normalize; // Avoid warning about hiding base class function.
231 : virtual void
232 0 : normalizeAndAppend(const UChar *src, const UChar *limit, UBool doNormalize,
233 : UnicodeString &safeMiddle,
234 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
235 0 : impl.composeAndAppend(src, limit, doNormalize, onlyContiguous, safeMiddle, buffer, errorCode);
236 0 : }
237 :
238 : virtual UBool
239 0 : isNormalized(const UnicodeString &s, UErrorCode &errorCode) const {
240 0 : if(U_FAILURE(errorCode)) {
241 0 : return FALSE;
242 : }
243 0 : const UChar *sArray=s.getBuffer();
244 0 : if(sArray==NULL) {
245 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
246 0 : return FALSE;
247 : }
248 0 : UnicodeString temp;
249 0 : ReorderingBuffer buffer(impl, temp);
250 0 : if(!buffer.init(5, errorCode)) { // small destCapacity for substring normalization
251 0 : return FALSE;
252 : }
253 0 : return impl.compose(sArray, sArray+s.length(), onlyContiguous, FALSE, buffer, errorCode);
254 : }
255 : virtual UNormalizationCheckResult
256 0 : quickCheck(const UnicodeString &s, UErrorCode &errorCode) const {
257 0 : if(U_FAILURE(errorCode)) {
258 0 : return UNORM_MAYBE;
259 : }
260 0 : const UChar *sArray=s.getBuffer();
261 0 : if(sArray==NULL) {
262 0 : errorCode=U_ILLEGAL_ARGUMENT_ERROR;
263 0 : return UNORM_MAYBE;
264 : }
265 0 : UNormalizationCheckResult qcResult=UNORM_YES;
266 0 : impl.composeQuickCheck(sArray, sArray+s.length(), onlyContiguous, &qcResult);
267 0 : return qcResult;
268 : }
269 : virtual const UChar *
270 0 : spanQuickCheckYes(const UChar *src, const UChar *limit, UErrorCode &) const {
271 0 : return impl.composeQuickCheck(src, limit, onlyContiguous, NULL);
272 : }
273 : using Normalizer2WithImpl::spanQuickCheckYes; // Avoid warning about hiding base class function.
274 0 : virtual UNormalizationCheckResult getQuickCheck(UChar32 c) const {
275 0 : return impl.getCompQuickCheck(impl.getNorm16(c));
276 : }
277 0 : virtual UBool hasBoundaryBefore(UChar32 c) const {
278 0 : return impl.hasCompBoundaryBefore(c);
279 : }
280 0 : virtual UBool hasBoundaryAfter(UChar32 c) const {
281 0 : return impl.hasCompBoundaryAfter(c, onlyContiguous, FALSE);
282 : }
283 0 : virtual UBool isInert(UChar32 c) const {
284 0 : return impl.hasCompBoundaryAfter(c, onlyContiguous, TRUE);
285 : }
286 :
287 : const UBool onlyContiguous;
288 : };
289 :
290 : class FCDNormalizer2 : public Normalizer2WithImpl {
291 : public:
292 5 : FCDNormalizer2(const Normalizer2Impl &ni) : Normalizer2WithImpl(ni) {}
293 : virtual ~FCDNormalizer2();
294 :
295 : private:
296 : virtual void
297 0 : normalize(const UChar *src, const UChar *limit,
298 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
299 0 : impl.makeFCD(src, limit, &buffer, errorCode);
300 0 : }
301 : using Normalizer2WithImpl::normalize; // Avoid warning about hiding base class function.
302 : virtual void
303 0 : normalizeAndAppend(const UChar *src, const UChar *limit, UBool doNormalize,
304 : UnicodeString &safeMiddle,
305 : ReorderingBuffer &buffer, UErrorCode &errorCode) const {
306 0 : impl.makeFCDAndAppend(src, limit, doNormalize, safeMiddle, buffer, errorCode);
307 0 : }
308 : virtual const UChar *
309 0 : spanQuickCheckYes(const UChar *src, const UChar *limit, UErrorCode &errorCode) const {
310 0 : return impl.makeFCD(src, limit, NULL, errorCode);
311 : }
312 : using Normalizer2WithImpl::spanQuickCheckYes; // Avoid warning about hiding base class function.
313 0 : virtual UBool hasBoundaryBefore(UChar32 c) const { return impl.hasFCDBoundaryBefore(c); }
314 0 : virtual UBool hasBoundaryAfter(UChar32 c) const { return impl.hasFCDBoundaryAfter(c); }
315 0 : virtual UBool isInert(UChar32 c) const { return impl.isFCDInert(c); }
316 : };
317 :
318 : struct Norm2AllModes : public UMemory {
319 5 : Norm2AllModes(Normalizer2Impl *i)
320 5 : : impl(i), comp(*i, FALSE), decomp(*i), fcd(*i), fcc(*i, TRUE) {}
321 : ~Norm2AllModes();
322 :
323 : static Norm2AllModes *createInstance(Normalizer2Impl *impl, UErrorCode &errorCode);
324 : static Norm2AllModes *createNFCInstance(UErrorCode &errorCode);
325 : static Norm2AllModes *createInstance(const char *packageName,
326 : const char *name,
327 : UErrorCode &errorCode);
328 :
329 : static const Norm2AllModes *getNFCInstance(UErrorCode &errorCode);
330 : static const Norm2AllModes *getNFKCInstance(UErrorCode &errorCode);
331 : static const Norm2AllModes *getNFKC_CFInstance(UErrorCode &errorCode);
332 :
333 : Normalizer2Impl *impl;
334 : ComposeNormalizer2 comp;
335 : DecomposeNormalizer2 decomp;
336 : FCDNormalizer2 fcd;
337 : ComposeNormalizer2 fcc;
338 : };
339 :
340 : U_NAMESPACE_END
341 :
342 : #endif // !UCONFIG_NO_NORMALIZATION
343 : #endif // __NORM2ALLMODES_H__
|