]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/unorm.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / unorm.cpp
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3* Copyright (c) 1996-2003, International Business Machines
4* Corporation and others. All Rights Reserved.
5******************************************************************************
6* File unorm.cpp
7*
8* Created by: Vladimir Weinstein 12052000
9*
10* Modification history :
11*
12* Date Name Description
13* 02/01/01 synwee Added normalization quickcheck enum and method.
14* 02/12/01 synwee Commented out quickcheck util api has been approved
15* Added private method for doing FCD checks
16* 02/23/01 synwee Modified quickcheck and checkFCE to run through
17* string for codepoints < 0x300 for the normalization
18* mode NFC.
19* 05/25/01+ Markus Scherer total rewrite, implement all normalization here
20* instead of just wrappers around normlzr.cpp,
21* load unorm.dat, support Unicode 3.1 with
22* supplementary code points, etc.
23*/
24
25#include "unicode/utypes.h"
26
27// moved up to make unorm_cmpEquivFold work without normalization
28#include "unicode/ustring.h"
29#include "unormimp.h"
30#include "ustr_imp.h"
31
32#if !UCONFIG_NO_NORMALIZATION
33
34#include "unicode/udata.h"
35#include "unicode/uchar.h"
36#include "unicode/uiter.h"
37#include "unicode/uniset.h"
38#include "unicode/usetiter.h"
39#include "unicode/unorm.h"
40#include "cmemory.h"
41#include "umutex.h"
42#include "utrie.h"
43#include "unicode/uset.h"
44
45/*
46 * Status of tailored normalization
47 *
48 * This was done initially for investigation on Unicode public review issue 7
49 * (http://www.unicode.org/review/). See Jitterbug 2481.
50 * While the UTC at meeting #94 (2003mar) did not take up the issue, this is
51 * a permanent feature in ICU 2.6 in support of IDNA which requires true
52 * Unicode 3.2 normalization.
53 * (NormalizationCorrections are rolled into IDNA mapping tables.)
54 *
55 * Tailored normalization as implemented here allows to "normalize less"
56 * than full Unicode normalization would.
57 * Based internally on a UnicodeSet of code points that are
58 * "excluded from normalization", the normalization functions leave those
59 * code points alone ("inert"). This means that tailored normalization
60 * still transforms text into a canonically equivalent form.
61 * It does not add decompositions to code points that do not have any or
62 * change decomposition results.
63 *
64 * Any function that searches for a safe boundary has not been touched,
65 * which means that these functions will be over-pessimistic when
66 * exclusions are applied.
67 * This should not matter because subsequent checks and normalizations
68 * do apply the exclusions; only a little more of the text may be processed
69 * than necessary under exclusions.
70 *
71 * Normalization exclusions have the following effect on excluded code points c:
72 * - c is not decomposed
73 * - c is not a composition target
74 * - c does not combine forward or backward for composition
75 * except that this is not implemented for Jamo
76 * - c is treated as having a combining class of 0
77 */
78#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
79
80/*
81 * This new implementation of the normalization code loads its data from
82 * unorm.dat, which is generated with the gennorm tool.
83 * The format of that file is described in unormimp.h .
84 */
85
86/* -------------------------------------------------------------------------- */
87
88enum {
89 _STACK_BUFFER_CAPACITY=100
90};
91
92/*
93 * Constants for the bit fields in the options bit set parameter.
94 * These need not be public.
95 * A user only needs to know the currently assigned values.
96 * The number and positions of reserved bits per field can remain private
97 * and may change in future implementations.
98 */
99enum {
100 _NORM_OPTIONS_NX_MASK=0x1f,
101 _NORM_OPTIONS_UNICODE_MASK=0xe0,
102 _NORM_OPTIONS_SETS_MASK=0xff,
103
104 _NORM_OPTIONS_UNICODE_SHIFT=5
105};
106
107static inline UBool
108isHangulWithoutJamoT(UChar c) {
109 c-=HANGUL_BASE;
110 return c<HANGUL_COUNT && c%JAMO_T_COUNT==0;
111}
112
113/* norm32 helpers */
114
115/* is this a norm32 with a regular index? */
116static inline UBool
117isNorm32Regular(uint32_t norm32) {
118 return norm32<_NORM_MIN_SPECIAL;
119}
120
121/* is this a norm32 with a special index for a lead surrogate? */
122static inline UBool
123isNorm32LeadSurrogate(uint32_t norm32) {
124 return _NORM_MIN_SPECIAL<=norm32 && norm32<_NORM_SURROGATES_TOP;
125}
126
127/* is this a norm32 with a special index for a Hangul syllable or a Jamo? */
128static inline UBool
129isNorm32HangulOrJamo(uint32_t norm32) {
130 return norm32>=_NORM_MIN_HANGUL;
131}
132
133/*
134 * Given isNorm32HangulOrJamo(),
135 * is this a Hangul syllable or a Jamo?
136 */
137static inline UBool
138isHangulJamoNorm32HangulOrJamoL(uint32_t norm32) {
139 return norm32<_NORM_MIN_JAMO_V;
140}
141
142/*
143 * Given norm32 for Jamo V or T,
144 * is this a Jamo V?
145 */
146static inline UBool
147isJamoVTNorm32JamoV(uint32_t norm32) {
148 return norm32<_NORM_JAMO_V_TOP;
149}
150
151/* some prototypes ---------------------------------------------------------- */
152
153static const UChar *
154_findPreviousStarter(const UChar *start, const UChar *src,
155 uint32_t ccOrQCMask, uint32_t decompQCMask, UChar minNoMaybe);
156
157static const UChar *
158_findNextStarter(const UChar *src, const UChar *limit,
159 uint32_t qcMask, uint32_t decompQCMask, UChar minNoMaybe);
160
161static const UChar *
162_composePart(UChar *stackBuffer, UChar *&buffer, int32_t &bufferCapacity, int32_t &length,
163 const UChar *prevStarter, const UChar *src,
164 uint32_t qcMask, uint8_t &prevCC,
165 const UnicodeSet *nx,
166 UErrorCode *pErrorCode);
167
168/* load unorm.dat ----------------------------------------------------------- */
169
170#define DATA_NAME "unorm"
171#define DATA_TYPE "icu"
172
173static UDataMemory *normData=NULL;
174static UErrorCode dataErrorCode=U_ZERO_ERROR;
175static int8_t haveNormData=0;
176
177static int32_t indexes[_NORM_INDEX_TOP]={ 0 };
178static UTrie normTrie={ 0,0,0,0,0,0,0 }, fcdTrie={ 0,0,0,0,0,0,0 }, auxTrie={ 0,0,0,0,0,0,0 };
179
180/*
181 * pointers into the memory-mapped unorm.icu
182 */
183static const uint16_t *extraData=NULL,
184 *combiningTable=NULL,
185 *canonStartSets=NULL;
186
187static uint8_t formatVersion[4]={ 0, 0, 0, 0 };
188static UBool formatVersion_2_1=FALSE, formatVersion_2_2=FALSE;
189
190/* the Unicode version of the normalization data */
191static UVersionInfo dataVersion={ 0, 0, 0, 0 };
192
193/* cache UnicodeSets for each combination of exclusion flags */
194static UnicodeSet *nxCache[_NORM_OPTIONS_SETS_MASK+1]={ NULL };
195
196U_CDECL_BEGIN
197
198UBool
199unorm_cleanup() {
200 int32_t i;
201
202 if(normData!=NULL) {
203 udata_close(normData);
204 normData=NULL;
205 }
206 dataErrorCode=U_ZERO_ERROR;
207 haveNormData=0;
208
209 for(i=0; i<(int32_t)LENGTHOF(nxCache); ++i) {
210 delete nxCache[i];
211 }
212 uprv_memset(nxCache, 0, sizeof(nxCache));
213
214 return TRUE;
215}
216
217/* normTrie: 32-bit trie result may contain a special extraData index with the folding offset */
218static int32_t U_CALLCONV
219getFoldingNormOffset(uint32_t norm32) {
220 if(isNorm32LeadSurrogate(norm32)) {
221 return
222 UTRIE_BMP_INDEX_LENGTH+
223 (((int32_t)norm32>>(_NORM_EXTRA_SHIFT-UTRIE_SURROGATE_BLOCK_BITS))&
224 (0x3ff<<UTRIE_SURROGATE_BLOCK_BITS));
225 } else {
226 return 0;
227 }
228}
229
230/* fcdTrie: the folding offset is the lead FCD value itself */
231static int32_t U_CALLCONV
232getFoldingFCDOffset(uint32_t data) {
233 return (int32_t)data;
234}
235
236/* auxTrie: the folding offset is in bits 9..0 of the 16-bit trie result */
237static int32_t U_CALLCONV
238getFoldingAuxOffset(uint32_t data) {
239 return (int32_t)(data&_NORM_AUX_FNC_MASK)<<UTRIE_SURROGATE_BLOCK_BITS;
240}
241
242static UBool U_CALLCONV
243isAcceptable(void * /* context */,
244 const char * /* type */, const char * /* name */,
245 const UDataInfo *pInfo) {
246 if(
247 pInfo->size>=20 &&
248 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
249 pInfo->charsetFamily==U_CHARSET_FAMILY &&
250 pInfo->dataFormat[0]==0x4e && /* dataFormat="Norm" */
251 pInfo->dataFormat[1]==0x6f &&
252 pInfo->dataFormat[2]==0x72 &&
253 pInfo->dataFormat[3]==0x6d &&
254 pInfo->formatVersion[0]==2 &&
255 pInfo->formatVersion[2]==UTRIE_SHIFT &&
256 pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT
257 ) {
258 uprv_memcpy(formatVersion, pInfo->formatVersion, 4);
259 uprv_memcpy(dataVersion, pInfo->dataVersion, 4);
260 return TRUE;
261 } else {
262 return FALSE;
263 }
264}
265
266static UBool U_CALLCONV
267_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*limit*/, uint32_t /*value*/) {
268 /* add the start code point to the USet */
269 uset_add((USet *)context, start);
270 return TRUE;
271}
272
273U_CDECL_END
274
275static int8_t
276loadNormData(UErrorCode &errorCode) {
277 /* load Unicode normalization data from file */
278
279 /*
280 * This lazy intialization with double-checked locking (without mutex protection for
281 * haveNormData==0) is transiently unsafe under certain circumstances.
282 * Check the readme and use u_init() if necessary.
283 *
284 * While u_init() initializes the main normalization data via this functions,
285 * it does not do so for exclusion sets (which are fully mutexed).
286 * This is because
287 * - there can be many exclusion sets
288 * - they are rarely used
289 * - they are not usually used in execution paths that are
290 * as performance-sensitive as others
291 * (e.g., IDNA takes more time than unorm_quickCheck() anyway)
292 */
293 if(haveNormData==0) {
294 UTrie _normTrie={ 0,0,0,0,0,0,0 }, _fcdTrie={ 0,0,0,0,0,0,0 }, _auxTrie={ 0,0,0,0,0,0,0 };
295 UDataMemory *data;
296 const int32_t *p=NULL;
297 const uint8_t *pb;
298
299 if(&errorCode==NULL || U_FAILURE(errorCode)) {
300 return 0;
301 }
302
303 /* open the data outside the mutex block */
304 data=udata_openChoice(NULL, DATA_TYPE, DATA_NAME, isAcceptable, NULL, &errorCode);
305 dataErrorCode=errorCode;
306 if(U_FAILURE(errorCode)) {
307 return haveNormData=-1;
308 }
309
310 p=(const int32_t *)udata_getMemory(data);
311 pb=(const uint8_t *)(p+_NORM_INDEX_TOP);
312 utrie_unserialize(&_normTrie, pb, p[_NORM_INDEX_TRIE_SIZE], &errorCode);
313 _normTrie.getFoldingOffset=getFoldingNormOffset;
314
315 pb+=p[_NORM_INDEX_TRIE_SIZE]+p[_NORM_INDEX_UCHAR_COUNT]*2+p[_NORM_INDEX_COMBINE_DATA_COUNT]*2;
316 utrie_unserialize(&_fcdTrie, pb, p[_NORM_INDEX_FCD_TRIE_SIZE], &errorCode);
317 _fcdTrie.getFoldingOffset=getFoldingFCDOffset;
318
319 if(p[_NORM_INDEX_FCD_TRIE_SIZE]!=0) {
320 pb+=p[_NORM_INDEX_FCD_TRIE_SIZE];
321 utrie_unserialize(&_auxTrie, pb, p[_NORM_INDEX_AUX_TRIE_SIZE], &errorCode);
322 _auxTrie.getFoldingOffset=getFoldingAuxOffset;
323 }
324
325 if(U_FAILURE(errorCode)) {
326 dataErrorCode=errorCode;
327 udata_close(data);
328 return haveNormData=-1;
329 }
330
331 /* in the mutex block, set the data for this process */
332 umtx_lock(NULL);
333 if(normData==NULL) {
334 normData=data;
335 data=NULL;
336
337 uprv_memcpy(&indexes, p, sizeof(indexes));
338 uprv_memcpy(&normTrie, &_normTrie, sizeof(UTrie));
339 uprv_memcpy(&fcdTrie, &_fcdTrie, sizeof(UTrie));
340 uprv_memcpy(&auxTrie, &_auxTrie, sizeof(UTrie));
341 } else {
342 p=(const int32_t *)udata_getMemory(normData);
343 }
344 umtx_unlock(NULL);
345
346 /* initialize some variables */
347 extraData=(uint16_t *)((uint8_t *)(p+_NORM_INDEX_TOP)+indexes[_NORM_INDEX_TRIE_SIZE]);
348 combiningTable=extraData+indexes[_NORM_INDEX_UCHAR_COUNT];
349 formatVersion_2_1=formatVersion[0]>2 || (formatVersion[0]==2 && formatVersion[1]>=1);
350 formatVersion_2_2=formatVersion[0]>2 || (formatVersion[0]==2 && formatVersion[1]>=2);
351 if(formatVersion_2_1) {
352 canonStartSets=combiningTable+
353 indexes[_NORM_INDEX_COMBINE_DATA_COUNT]+
354 (indexes[_NORM_INDEX_FCD_TRIE_SIZE]+indexes[_NORM_INDEX_AUX_TRIE_SIZE])/2;
355 }
356 haveNormData=1;
357
358 /* if a different thread set it first, then close the extra data */
359 if(data!=NULL) {
360 udata_close(data); /* NULL if it was set correctly */
361 }
362 }
363
364 return haveNormData;
365}
366
367static inline UBool
368_haveData(UErrorCode &errorCode) {
369 if(haveNormData!=0) {
370 errorCode=dataErrorCode;
371 return (UBool)(haveNormData>0);
372 } else {
373 return (UBool)(loadNormData(errorCode)>0);
374 }
375}
376
377U_CAPI UBool U_EXPORT2
378unorm_haveData(UErrorCode *pErrorCode) {
379 return _haveData(*pErrorCode);
380}
381
382U_CAPI const uint16_t * U_EXPORT2
383unorm_getFCDTrie(UErrorCode *pErrorCode) {
384 if(_haveData(*pErrorCode)) {
385 return fcdTrie.index;
386 } else {
387 return NULL;
388 }
389}
390
391/* data access primitives --------------------------------------------------- */
392
393static inline uint32_t
394_getNorm32(UChar c) {
395 return UTRIE_GET32_FROM_LEAD(&normTrie, c);
396}
397
398static inline uint32_t
399_getNorm32FromSurrogatePair(uint32_t norm32, UChar c2) {
400 /*
401 * the surrogate index in norm32 stores only the number of the surrogate index block
402 * see gennorm/store.c/getFoldedNormValue()
403 */
404 norm32=
405 UTRIE_BMP_INDEX_LENGTH+
406 ((norm32>>(_NORM_EXTRA_SHIFT-UTRIE_SURROGATE_BLOCK_BITS))&
407 (0x3ff<<UTRIE_SURROGATE_BLOCK_BITS));
408 return UTRIE_GET32_FROM_OFFSET_TRAIL(&normTrie, norm32, c2);
409}
410
411/*
412 * get a norm32 from text with complete code points
413 * (like from decompositions)
414 */
415static inline uint32_t
416_getNorm32(const UChar *p, uint32_t mask) {
417 uint32_t norm32=_getNorm32(*p);
418 if((norm32&mask) && isNorm32LeadSurrogate(norm32)) {
419 /* *p is a lead surrogate, get the real norm32 */
420 norm32=_getNorm32FromSurrogatePair(norm32, *(p+1));
421 }
422 return norm32;
423}
424
425static inline uint16_t
426_getFCD16(UChar c) {
427 return UTRIE_GET16_FROM_LEAD(&fcdTrie, c);
428}
429
430static inline uint16_t
431_getFCD16FromSurrogatePair(uint16_t fcd16, UChar c2) {
432 /* the surrogate index in fcd16 is an absolute offset over the start of stage 1 */
433 return UTRIE_GET16_FROM_OFFSET_TRAIL(&fcdTrie, fcd16, c2);
434}
435
436static inline const uint16_t *
437_getExtraData(uint32_t norm32) {
438 return extraData+(norm32>>_NORM_EXTRA_SHIFT);
439}
440
441/* normalization exclusion sets --------------------------------------------- */
442
443/*
444 * Normalization exclusion UnicodeSets are used for tailored normalization;
445 * see the comment near the beginning of this file.
446 *
447 * By specifying one or several sets of code points,
448 * those code points become inert for normalization.
449 */
450
451static const UnicodeSet *
452internalGetNXHangul(UErrorCode &errorCode) {
453 /* internal function, does not check for incoming U_FAILURE */
454
455 UBool isCached;
456
457 /* do this because double-checked locking is broken */
458 umtx_lock(NULL);
459 isCached=nxCache[UNORM_NX_HANGUL]!=NULL;
460 umtx_unlock(NULL);
461
462 if(!isCached) {
463 UnicodeSet *set=new UnicodeSet(0xac00, 0xd7a3);
464 if(set==NULL) {
465 errorCode=U_MEMORY_ALLOCATION_ERROR;
466 return NULL;
467 }
468
469 umtx_lock(NULL);
470 if(nxCache[UNORM_NX_HANGUL]==NULL) {
471 nxCache[UNORM_NX_HANGUL]=set;
472 set=NULL;
473 }
474 umtx_unlock(NULL);
475
476 delete set;
477 }
478
479 return nxCache[UNORM_NX_HANGUL];
480}
481
482static const UnicodeSet *
483internalGetNXCJKCompat(UErrorCode &errorCode) {
484 /* internal function, does not check for incoming U_FAILURE */
485
486 UBool isCached;
487
488 /* do this because double-checked locking is broken */
489 umtx_lock(NULL);
490 isCached=nxCache[UNORM_NX_CJK_COMPAT]!=NULL;
491 umtx_unlock(NULL);
492
493 if(!isCached) {
494 /* build a set from [CJK Ideographs]&[has canonical decomposition] */
495 UnicodeSet *set, *hasDecomp;
496
497 set=new UnicodeSet(UNICODE_STRING("[:Ideographic:]", 15), errorCode);
498 if(set==NULL) {
499 errorCode=U_MEMORY_ALLOCATION_ERROR;
500 return NULL;
501 }
502 if(U_FAILURE(errorCode)) {
503 delete set;
504 return NULL;
505 }
506
507 /* start with an empty set for [has canonical decomposition] */
508 hasDecomp=new UnicodeSet();
509 if(hasDecomp==NULL) {
510 delete set;
511 errorCode=U_MEMORY_ALLOCATION_ERROR;
512 return NULL;
513 }
514
515 /* iterate over all ideographs and remember which canonically decompose */
516 UnicodeSetIterator it(*set);
517 UChar32 start, end;
518 uint32_t norm32;
519
520 while(it.nextRange() && !it.isString()) {
521 start=it.getCodepoint();
522 end=it.getCodepointEnd();
523 while(start<=end) {
524 UTRIE_GET32(&normTrie, start, norm32);
525 if(norm32&_NORM_QC_NFD) {
526 hasDecomp->add(start);
527 }
528 ++start;
529 }
530 }
531
532 /* hasDecomp now contains all ideographs that decompose canonically */
533
534 umtx_lock(NULL);
535 if(nxCache[UNORM_NX_CJK_COMPAT]==NULL) {
536 nxCache[UNORM_NX_CJK_COMPAT]=hasDecomp;
537 hasDecomp=NULL;
538 }
539 umtx_unlock(NULL);
540
541 delete hasDecomp;
542 delete set;
543 }
544
545 return nxCache[UNORM_NX_CJK_COMPAT];
546}
547
548static const UnicodeSet *
549internalGetNXUnicode(uint32_t options, UErrorCode &errorCode) {
550 /* internal function, does not check for incoming U_FAILURE */
551 options&=_NORM_OPTIONS_UNICODE_MASK;
552 if(options==0) {
553 return NULL;
554 }
555
556 UBool isCached;
557
558 /* do this because double-checked locking is broken */
559 umtx_lock(NULL);
560 isCached=nxCache[options]!=NULL;
561 umtx_unlock(NULL);
562
563 if(!isCached) {
564 /* build a set with all code points that were not designated by the specified Unicode version */
565 UnicodeSet *set;
566
567 switch(options) {
568 case UNORM_UNICODE_3_2:
569 set=new UnicodeSet(UNICODE_STRING("[:^Age=3.2:]", 12), errorCode);
570 break;
571 default:
572 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
573 return NULL;
574 }
575
576 if(set==NULL) {
577 errorCode=U_MEMORY_ALLOCATION_ERROR;
578 return NULL;
579 }
580 if(U_FAILURE(errorCode)) {
581 delete set;
582 return NULL;
583 }
584
585 umtx_lock(NULL);
586 if(nxCache[options]==NULL) {
587 nxCache[options]=set;
588 set=NULL;
589 }
590 umtx_unlock(NULL);
591
592 delete set;
593 }
594
595 return nxCache[options];
596}
597
598/* Get a decomposition exclusion set. The data must be loaded. */
599static const UnicodeSet *
600internalGetNX(int32_t options, UErrorCode &errorCode) {
601 options&=_NORM_OPTIONS_SETS_MASK;
602
603 UBool isCached;
604
605 /* do this because double-checked locking is broken */
606 umtx_lock(NULL);
607 isCached=nxCache[options]!=NULL;
608 umtx_unlock(NULL);
609
610 if(!isCached) {
611 /* return basic sets */
612 if(options==UNORM_NX_HANGUL) {
613 return internalGetNXHangul(errorCode);
614 }
615 if(options==UNORM_NX_CJK_COMPAT) {
616 return internalGetNXCJKCompat(errorCode);
617 }
618 if((options&_NORM_OPTIONS_UNICODE_MASK)!=0 && (options&_NORM_OPTIONS_NX_MASK)==0) {
619 return internalGetNXUnicode(options, errorCode);
620 }
621
622 /* build a set from multiple subsets */
623 UnicodeSet *set;
624 const UnicodeSet *other;
625
626 set=new UnicodeSet();
627 if(set==NULL) {
628 errorCode=U_MEMORY_ALLOCATION_ERROR;
629 return NULL;
630 }
631
632 if((options&UNORM_NX_HANGUL)!=0 && NULL!=(other=internalGetNXHangul(errorCode))) {
633 set->addAll(*other);
634 }
635 if((options&UNORM_NX_CJK_COMPAT)!=0 && NULL!=(other=internalGetNXCJKCompat(errorCode))) {
636 set->addAll(*other);
637 }
638 if((options&_NORM_OPTIONS_UNICODE_MASK)!=0 && NULL!=(other=internalGetNXUnicode(options, errorCode))) {
639 set->addAll(*other);
640 }
641
642 if(U_FAILURE(errorCode)) {
643 delete set;
644 return NULL;
645 }
646
647 umtx_lock(NULL);
648 if(nxCache[options]==NULL) {
649 nxCache[options]=set;
650 set=NULL;
651 }
652 umtx_unlock(NULL);
653
654 delete set;
655 }
656
657 return nxCache[options];
658}
659
660static inline const UnicodeSet *
661getNX(int32_t options, UErrorCode &errorCode) {
662 if(U_FAILURE(errorCode) || (options&=_NORM_OPTIONS_SETS_MASK)==0) {
663 /* incoming failure, or no decomposition exclusions requested */
664 return NULL;
665 } else {
666 return internalGetNX(options, errorCode);
667 }
668}
669
670static inline UBool
671nx_contains(const UnicodeSet *nx, UChar32 c) {
672 return nx!=NULL && nx->contains(c);
673}
674
675static inline UBool
676nx_contains(const UnicodeSet *nx, UChar c, UChar c2) {
677 return nx!=NULL && nx->contains(c2==0 ? c : U16_GET_SUPPLEMENTARY(c, c2));
678}
679
680/* other normalization primitives ------------------------------------------- */
681
682/* get the canonical or compatibility decomposition for one character */
683static inline const UChar *
684_decompose(uint32_t norm32, uint32_t qcMask, int32_t &length,
685 uint8_t &cc, uint8_t &trailCC) {
686 const UChar *p=(const UChar *)_getExtraData(norm32);
687 length=*p++;
688
689 if((norm32&qcMask&_NORM_QC_NFKD)!=0 && length>=0x100) {
690 /* use compatibility decomposition, skip canonical data */
691 p+=((length>>7)&1)+(length&_NORM_DECOMP_LENGTH_MASK);
692 length>>=8;
693 }
694
695 if(length&_NORM_DECOMP_FLAG_LENGTH_HAS_CC) {
696 /* get the lead and trail cc's */
697 UChar bothCCs=*p++;
698 cc=(uint8_t)(bothCCs>>8);
699 trailCC=(uint8_t)bothCCs;
700 } else {
701 /* lead and trail cc's are both 0 */
702 cc=trailCC=0;
703 }
704
705 length&=_NORM_DECOMP_LENGTH_MASK;
706 return p;
707}
708
709/* get the canonical decomposition for one character */
710static inline const UChar *
711_decompose(uint32_t norm32, int32_t &length,
712 uint8_t &cc, uint8_t &trailCC) {
713 const UChar *p=(const UChar *)_getExtraData(norm32);
714 length=*p++;
715
716 if(length&_NORM_DECOMP_FLAG_LENGTH_HAS_CC) {
717 /* get the lead and trail cc's */
718 UChar bothCCs=*p++;
719 cc=(uint8_t)(bothCCs>>8);
720 trailCC=(uint8_t)bothCCs;
721 } else {
722 /* lead and trail cc's are both 0 */
723 cc=trailCC=0;
724 }
725
726 length&=_NORM_DECOMP_LENGTH_MASK;
727 return p;
728}
729
730/**
731 * Get the canonical decomposition for one code point.
732 * @param c code point
733 * @param buffer out-only buffer for algorithmic decompositions of Hangul
734 * @param length out-only, takes the length of the decomposition, if any
735 * @return pointer to decomposition, or 0 if none
736 * @internal
737 */
738static const UChar *
739_decompose(UChar32 c, UChar buffer[4], int32_t &length) {
740 uint32_t norm32;
741
742 UTRIE_GET32(&normTrie, c, norm32);
743 if(norm32&_NORM_QC_NFD) {
744 if(isNorm32HangulOrJamo(norm32)) {
745 /* Hangul syllable: decompose algorithmically */
746 UChar c2;
747
748 c-=HANGUL_BASE;
749
750 c2=(UChar)(c%JAMO_T_COUNT);
751 c/=JAMO_T_COUNT;
752 if(c2>0) {
753 buffer[2]=(UChar)(JAMO_T_BASE+c2);
754 length=3;
755 } else {
756 length=2;
757 }
758
759 buffer[1]=(UChar)(JAMO_V_BASE+c%JAMO_V_COUNT);
760 buffer[0]=(UChar)(JAMO_L_BASE+c/JAMO_V_COUNT);
761 return buffer;
762 } else {
763 /* normal decomposition */
764 uint8_t cc, trailCC;
765 return _decompose(norm32, length, cc, trailCC);
766 }
767 } else {
768 return 0;
769 }
770}
771
772/*
773 * get the combining class of (c, c2)=*p++
774 * before: p<limit after: p<=limit
775 * if only one code unit is used, then c2==0
776 */
777static inline uint8_t
778_getNextCC(const UChar *&p, const UChar *limit, UChar &c, UChar &c2) {
779 uint32_t norm32;
780
781 c=*p++;
782 norm32=_getNorm32(c);
783 if((norm32&_NORM_CC_MASK)==0) {
784 c2=0;
785 return 0;
786 } else {
787 if(!isNorm32LeadSurrogate(norm32)) {
788 c2=0;
789 } else {
790 /* c is a lead surrogate, get the real norm32 */
791 if(p!=limit && UTF_IS_SECOND_SURROGATE(c2=*p)) {
792 ++p;
793 norm32=_getNorm32FromSurrogatePair(norm32, c2);
794 } else {
795 c2=0;
796 return 0;
797 }
798 }
799
800 return (uint8_t)(norm32>>_NORM_CC_SHIFT);
801 }
802}
803
804/*
805 * read backwards and get norm32
806 * return 0 if the character is <minC
807 * if c2!=0 then (c2, c) is a surrogate pair (reversed - c2 is first surrogate but read second!)
808 */
809static inline uint32_t
810_getPrevNorm32(const UChar *start, const UChar *&src,
811 uint32_t minC, uint32_t mask,
812 UChar &c, UChar &c2) {
813 uint32_t norm32;
814
815 c=*--src;
816 c2=0;
817
818 /* check for a surrogate before getting norm32 to see if we need to predecrement further */
819 if(c<minC) {
820 return 0;
821 } else if(!UTF_IS_SURROGATE(c)) {
822 return _getNorm32(c);
823 } else if(UTF_IS_SURROGATE_FIRST(c)) {
824 /* unpaired first surrogate */
825 return 0;
826 } else if(src!=start && UTF_IS_FIRST_SURROGATE(c2=*(src-1))) {
827 --src;
828 norm32=_getNorm32(c2);
829
830 if((norm32&mask)==0) {
831 /* all surrogate pairs with this lead surrogate have only irrelevant data */
832 return 0;
833 } else {
834 /* norm32 must be a surrogate special */
835 return _getNorm32FromSurrogatePair(norm32, c);
836 }
837 } else {
838 /* unpaired second surrogate */
839 c2=0;
840 return 0;
841 }
842}
843
844/*
845 * get the combining class of (c, c2)=*--p
846 * before: start<p after: start<=p
847 */
848static inline uint8_t
849_getPrevCC(const UChar *start, const UChar *&p) {
850 UChar c, c2;
851
852 return (uint8_t)(_getPrevNorm32(start, p, _NORM_MIN_WITH_LEAD_CC, _NORM_CC_MASK, c, c2)>>_NORM_CC_SHIFT);
853}
854
855/*
856 * is this a safe boundary character for NF*D?
857 * (lead cc==0)
858 */
859static inline UBool
860_isNFDSafe(uint32_t norm32, uint32_t ccOrQCMask, uint32_t decompQCMask) {
861 if((norm32&ccOrQCMask)==0) {
862 return TRUE; /* cc==0 and no decomposition: this is NF*D safe */
863 }
864
865 /* inspect its decomposition - maybe a Hangul but not a surrogate here */
866 if(isNorm32Regular(norm32) && (norm32&decompQCMask)!=0) {
867 int32_t length;
868 uint8_t cc, trailCC;
869
870 /* decomposes, get everything from the variable-length extra data */
871 _decompose(norm32, decompQCMask, length, cc, trailCC);
872 return cc==0;
873 } else {
874 /* no decomposition (or Hangul), test the cc directly */
875 return (norm32&_NORM_CC_MASK)==0;
876 }
877}
878
879/*
880 * is this (or does its decomposition begin with) a "true starter"?
881 * (cc==0 and NF*C_YES)
882 */
883static inline UBool
884_isTrueStarter(uint32_t norm32, uint32_t ccOrQCMask, uint32_t decompQCMask) {
885 if((norm32&ccOrQCMask)==0) {
886 return TRUE; /* this is a true starter (could be Hangul or Jamo L) */
887 }
888
889 /* inspect its decomposition - not a Hangul or a surrogate here */
890 if((norm32&decompQCMask)!=0) {
891 const UChar *p;
892 int32_t length;
893 uint8_t cc, trailCC;
894
895 /* decomposes, get everything from the variable-length extra data */
896 p=_decompose(norm32, decompQCMask, length, cc, trailCC);
897 if(cc==0) {
898 uint32_t qcMask=ccOrQCMask&_NORM_QC_MASK;
899
900 /* does it begin with NFC_YES? */
901 if((_getNorm32(p, qcMask)&qcMask)==0) {
902 /* yes, the decomposition begins with a true starter */
903 return TRUE;
904 }
905 }
906 }
907 return FALSE;
908}
909
910/* uchar.h */
911U_CAPI uint8_t U_EXPORT2
912u_getCombiningClass(UChar32 c) {
913 UErrorCode errorCode=U_ZERO_ERROR;
914 if(_haveData(errorCode)) {
915 uint32_t norm32;
916
917 UTRIE_GET32(&normTrie, c, norm32);
918 return (uint8_t)(norm32>>_NORM_CC_SHIFT);
919 } else {
920 return 0;
921 }
922}
923
924U_CAPI UBool U_EXPORT2
925unorm_internalIsFullCompositionExclusion(UChar32 c) {
926 UErrorCode errorCode=U_ZERO_ERROR;
927 if(_haveData(errorCode) && formatVersion_2_1) {
928 uint16_t aux;
929
930 UTRIE_GET16(&auxTrie, c, aux);
931 return (UBool)((aux&_NORM_AUX_COMP_EX_MASK)!=0);
932 } else {
933 return FALSE;
934 }
935}
936
937U_CAPI UBool U_EXPORT2
938unorm_isCanonSafeStart(UChar32 c) {
939 UErrorCode errorCode=U_ZERO_ERROR;
940 if(_haveData(errorCode) && formatVersion_2_1) {
941 uint16_t aux;
942
943 UTRIE_GET16(&auxTrie, c, aux);
944 return (UBool)((aux&_NORM_AUX_UNSAFE_MASK)==0);
945 } else {
946 return FALSE;
947 }
948}
949
950U_CAPI UBool U_EXPORT2
951unorm_getCanonStartSet(UChar32 c, USerializedSet *fillSet) {
952 UErrorCode errorCode=U_ZERO_ERROR;
953 if( fillSet!=NULL && (uint32_t)c<=0x10ffff &&
954 _haveData(errorCode) && canonStartSets!=NULL
955 ) {
956 const uint16_t *table;
957 int32_t i, start, limit;
958
959 /*
960 * binary search for c
961 *
962 * There are two search tables,
963 * one for BMP code points and one for supplementary ones.
964 * See unormimp.h for details.
965 */
966 if(c<=0xffff) {
967 table=canonStartSets+canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH];
968 start=0;
969 limit=canonStartSets[_NORM_SET_INDEX_CANON_BMP_TABLE_LENGTH];
970
971 /* each entry is a pair { c, result } */
972 while(start<limit-2) {
973 i=(uint16_t)(((start+limit)/4)*2); /* (start+limit)/2 and address pairs */
974 if(c<table[i]) {
975 limit=i;
976 } else {
977 start=i;
978 }
979 }
980
981 /* found? */
982 if(c==table[start]) {
983 i=table[start+1];
984 if((i&_NORM_CANON_SET_BMP_MASK)==_NORM_CANON_SET_BMP_IS_INDEX) {
985 /* result 01xxxxxx xxxxxx contains index x to a USerializedSet */
986 i&=(_NORM_MAX_CANON_SETS-1);
987 return uset_getSerializedSet(fillSet,
988 canonStartSets+i,
989 canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]-i);
990 } else {
991 /* other result values are BMP code points for single-code point sets */
992 uset_setSerializedToOne(fillSet, (UChar32)i);
993 return TRUE;
994 }
995 }
996 } else {
997 uint16_t high, low, h;
998
999 table=canonStartSets+canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]+
1000 canonStartSets[_NORM_SET_INDEX_CANON_BMP_TABLE_LENGTH];
1001 start=0;
1002 limit=canonStartSets[_NORM_SET_INDEX_CANON_SUPP_TABLE_LENGTH];
1003
1004 high=(uint16_t)(c>>16);
1005 low=(uint16_t)c;
1006
1007 /* each entry is a triplet { high(c), low(c), result } */
1008 while(start<limit-3) {
1009 i=(uint16_t)(((start+limit)/6)*3); /* (start+limit)/2 and address triplets */
1010 h=table[i]&0x1f; /* high word */
1011 if(high<h || (high==h && low<table[i+1])) {
1012 limit=i;
1013 } else {
1014 start=i;
1015 }
1016 }
1017
1018 /* found? */
1019 h=table[start];
1020 if(high==(h&0x1f) && low==table[start+1]) {
1021 i=table[start+2];
1022 if((h&0x8000)==0) {
1023 /* the result is an index to a USerializedSet */
1024 return uset_getSerializedSet(fillSet,
1025 canonStartSets+i,
1026 canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]-i);
1027 } else {
1028 /*
1029 * single-code point set {x} in
1030 * triplet { 100xxxxx 000hhhhh llllllll llllllll xxxxxxxx xxxxxxxx }
1031 */
1032 i|=((int32_t)h&0x1f00)<<8; /* add high bits from high(c) */
1033 uset_setSerializedToOne(fillSet, (UChar32)i);
1034 return TRUE;
1035 }
1036 }
1037 }
1038 }
1039
1040 return FALSE; /* not found */
1041}
1042
1043U_CAPI int32_t U_EXPORT2
1044u_getFC_NFKC_Closure(UChar32 c, UChar *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
1045 uint16_t aux;
1046
1047 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
1048 return 0;
1049 }
1050 if(destCapacity<0 || (dest==NULL && destCapacity>0)) {
1051 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1052 return 0;
1053 }
1054 if(!_haveData(*pErrorCode) || !formatVersion_2_1) {
1055 return 0;
1056 }
1057
1058 UTRIE_GET16(&auxTrie, c, aux);
1059 aux&=_NORM_AUX_FNC_MASK;
1060 if(aux!=0) {
1061 const UChar *s;
1062 int32_t length;
1063
1064 s=(const UChar *)(extraData+aux);
1065 if(*s<0xff00) {
1066 /* s points to the single-unit string */
1067 length=1;
1068 } else {
1069 length=*s&0xff;
1070 ++s;
1071 }
1072 if(0<length && length<=destCapacity) {
1073 uprv_memcpy(dest, s, length*U_SIZEOF_UCHAR);
1074 }
1075 return u_terminateUChars(dest, destCapacity, length, pErrorCode);
1076 } else {
1077 return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
1078 }
1079}
1080
1081/* Is c an NF<mode>-skippable code point? See unormimp.h. */
1082U_CAPI UBool U_EXPORT2
1083unorm_isNFSkippable(UChar32 c, UNormalizationMode mode) {
1084 UErrorCode errorCode;
1085 uint32_t norm32, mask;
1086 uint16_t aux, fcd;
1087
1088 errorCode=U_ZERO_ERROR;
1089 if(!_haveData(errorCode)) {
1090 return FALSE;
1091 }
1092
1093 /* handle trivial cases; set the comparison mask for the normal ones */
1094 switch(mode) {
1095 case UNORM_NONE:
1096 return TRUE;
1097 case UNORM_NFD:
1098 mask=_NORM_CC_MASK|_NORM_QC_NFD;
1099 break;
1100 case UNORM_NFKD:
1101 mask=_NORM_CC_MASK|_NORM_QC_NFKD;
1102 break;
1103 case UNORM_NFC:
1104 /* case UNORM_FCC: */
1105 mask=_NORM_CC_MASK|_NORM_COMBINES_ANY|(_NORM_QC_NFC&_NORM_QC_ANY_NO);
1106 break;
1107 case UNORM_NFKC:
1108 mask=_NORM_CC_MASK|_NORM_COMBINES_ANY|(_NORM_QC_NFKC&_NORM_QC_ANY_NO);
1109 break;
1110 case UNORM_FCD:
1111 /* FCD: skippable if lead cc==0 and trail cc<=1 */
1112 UTRIE_GET16(&fcdTrie, c, fcd);
1113 return fcd<=1;
1114 default:
1115 return FALSE;
1116 }
1117
1118 /* check conditions (a)..(e), see unormimp.h */
1119 UTRIE_GET32(&normTrie, c, norm32);
1120 if((norm32&mask)!=0) {
1121 return FALSE; /* fails (a)..(e), not skippable */
1122 }
1123
1124 if(mode<UNORM_NFC) {
1125 return TRUE; /* NF*D, passed (a)..(c), is skippable */
1126 }
1127
1128 /* NF*C/FCC, passed (a)..(e) */
1129 if((norm32&_NORM_QC_NFD)==0) {
1130 return TRUE; /* no canonical decomposition, is skippable */
1131 }
1132
1133 /* check Hangul syllables algorithmically */
1134 if(isNorm32HangulOrJamo(norm32)) {
1135 /* Jamo passed (a)..(e) above, must be Hangul */
1136 return !isHangulWithoutJamoT((UChar)c); /* LVT are skippable, LV are not */
1137 }
1138
1139 /* if(mode<=UNORM_NFKC) { -- enable when implementing FCC */
1140 /* NF*C, test (f) flag */
1141 if(!formatVersion_2_2) {
1142 return FALSE; /* no (f) data, say not skippable to be safe */
1143 }
1144
1145 UTRIE_GET16(&auxTrie, c, aux);
1146 return (aux&_NORM_AUX_NFC_SKIP_F_MASK)==0; /* TRUE=skippable if the (f) flag is not set */
1147
1148 /* } else { FCC, test fcd<=1 instead of the above } */
1149}
1150
1151U_CAPI void U_EXPORT2
1152unorm_addPropertyStarts(USet *set, UErrorCode *pErrorCode) {
1153 UChar c;
1154
1155 if(!_haveData(*pErrorCode)) {
1156 return;
1157 }
1158
1159 /* add the start code point of each same-value range of each trie */
1160 utrie_enum(&normTrie, NULL, _enumPropertyStartsRange, set);
1161 utrie_enum(&fcdTrie, NULL, _enumPropertyStartsRange, set);
1162 if(formatVersion_2_1) {
1163 utrie_enum(&auxTrie, NULL, _enumPropertyStartsRange, set);
1164 }
1165
1166 /* add Hangul LV syllables and LV+1 because of skippables */
1167 for(c=HANGUL_BASE; c<HANGUL_BASE+HANGUL_COUNT; c+=JAMO_T_COUNT) {
1168 uset_add(set, c);
1169 uset_add(set, c+1);
1170 }
1171 uset_add(set, HANGUL_BASE+HANGUL_COUNT); /* add Hangul+1 to continue with other properties */
1172}
1173
1174/* reorder UTF-16 in-place -------------------------------------------------- */
1175
1176/*
1177 * simpler, single-character version of _mergeOrdered() -
1178 * bubble-insert one single code point into the preceding string
1179 * which is already canonically ordered
1180 * (c, c2) may or may not yet have been inserted at [current..p[
1181 *
1182 * it must be p=current+lengthof(c, c2) i.e. p=current+(c2==0 ? 1 : 2)
1183 *
1184 * before: [start..current[ is already ordered, and
1185 * [current..p[ may or may not hold (c, c2) but
1186 * must be exactly the same length as (c, c2)
1187 * after: [start..p[ is ordered
1188 *
1189 * returns the trailing combining class
1190 */
1191static uint8_t
1192_insertOrdered(const UChar *start, UChar *current, UChar *p,
1193 UChar c, UChar c2, uint8_t cc) {
1194 const UChar *pBack, *pPreBack;
1195 UChar *r;
1196 uint8_t prevCC, trailCC=cc;
1197
1198 if(start<current && cc!=0) {
1199 /* search for the insertion point where cc>=prevCC */
1200 pPreBack=pBack=current;
1201 prevCC=_getPrevCC(start, pPreBack);
1202 if(cc<prevCC) {
1203 /* this will be the last code point, so keep its cc */
1204 trailCC=prevCC;
1205 pBack=pPreBack;
1206 while(start<pPreBack) {
1207 prevCC=_getPrevCC(start, pPreBack);
1208 if(cc>=prevCC) {
1209 break;
1210 }
1211 pBack=pPreBack;
1212 }
1213
1214 /*
1215 * this is where we are right now with all these pointers:
1216 * [start..pPreBack[ 0..? code points that we can ignore
1217 * [pPreBack..pBack[ 0..1 code points with prevCC<=cc
1218 * [pBack..current[ 0..n code points with >cc, move up to insert (c, c2)
1219 * [current..p[ 1 code point (c, c2) with cc
1220 */
1221
1222 /* move the code units in between up */
1223 r=p;
1224 do {
1225 *--r=*--current;
1226 } while(pBack!=current);
1227 }
1228 }
1229
1230 /* insert (c, c2) */
1231 *current=c;
1232 if(c2!=0) {
1233 *(current+1)=c2;
1234 }
1235
1236 /* we know the cc of the last code point */
1237 return trailCC;
1238}
1239
1240/*
1241 * merge two UTF-16 string parts together
1242 * to canonically order (order by combining classes) their concatenation
1243 *
1244 * the two strings may already be adjacent, so that the merging is done in-place
1245 * if the two strings are not adjacent, then the buffer holding the first one
1246 * must be large enough
1247 * the second string may or may not be ordered in itself
1248 *
1249 * before: [start..current[ is already ordered, and
1250 * [next..limit[ may be ordered in itself, but
1251 * is not in relation to [start..current[
1252 * after: [start..current+(limit-next)[ is ordered
1253 *
1254 * the algorithm is a simple bubble-sort that takes the characters from *next++
1255 * and inserts them in correct combining class order into the preceding part
1256 * of the string
1257 *
1258 * since this function is called much less often than the single-code point
1259 * _insertOrdered(), it just uses that for easier maintenance
1260 * (see file version from before 2001aug31 for a more optimized version)
1261 *
1262 * returns the trailing combining class
1263 */
1264static uint8_t
1265_mergeOrdered(UChar *start, UChar *current,
1266 const UChar *next, const UChar *limit, UBool isOrdered=TRUE) {
1267 UChar *r;
1268 UChar c, c2;
1269 uint8_t cc, trailCC=0;
1270 UBool adjacent;
1271
1272 adjacent= current==next;
1273
1274 if(start!=current || !isOrdered) {
1275 while(next<limit) {
1276 cc=_getNextCC(next, limit, c, c2);
1277 if(cc==0) {
1278 /* does not bubble back */
1279 trailCC=0;
1280 if(adjacent) {
1281 current=(UChar *)next;
1282 } else {
1283 *current++=c;
1284 if(c2!=0) {
1285 *current++=c2;
1286 }
1287 }
1288 if(isOrdered) {
1289 break;
1290 } else {
1291 start=current;
1292 }
1293 } else {
1294 r=current+(c2==0 ? 1 : 2);
1295 trailCC=_insertOrdered(start, current, r, c, c2, cc);
1296 current=r;
1297 }
1298 }
1299 }
1300
1301 if(next==limit) {
1302 /* we know the cc of the last code point */
1303 return trailCC;
1304 } else {
1305 if(!adjacent) {
1306 /* copy the second string part */
1307 do {
1308 *current++=*next++;
1309 } while(next!=limit);
1310 limit=current;
1311 }
1312 return _getPrevCC(start, limit);
1313 }
1314}
1315
1316/* quick check functions ---------------------------------------------------- */
1317
1318static UBool
1319unorm_checkFCD(const UChar *src, int32_t srcLength, const UnicodeSet *nx) {
1320 const UChar *limit;
1321 UChar c, c2;
1322 uint16_t fcd16;
1323 int16_t prevCC, cc;
1324
1325 /* initialize */
1326 prevCC=0;
1327
1328 if(srcLength>=0) {
1329 /* string with length */
1330 limit=src+srcLength;
1331 } else /* srcLength==-1 */ {
1332 /* zero-terminated string */
1333 limit=NULL;
1334 }
1335
1336 U_ALIGN_CODE(16);
1337
1338 for(;;) {
1339 /* skip a run of code units below the minimum or with irrelevant data for the FCD check */
1340 if(limit==NULL) {
1341 for(;;) {
1342 c=*src++;
1343 if(c<_NORM_MIN_WITH_LEAD_CC) {
1344 if(c==0) {
1345 return TRUE;
1346 }
1347 /*
1348 * delay _getFCD16(c) for any character <_NORM_MIN_WITH_LEAD_CC
1349 * because chances are good that the next one will have
1350 * a leading cc of 0;
1351 * _getFCD16(-prevCC) is later called when necessary -
1352 * -c fits into int16_t because it is <_NORM_MIN_WITH_LEAD_CC==0x300
1353 */
1354 prevCC=(int16_t)-c;
1355 } else if((fcd16=_getFCD16(c))==0) {
1356 prevCC=0;
1357 } else {
1358 break;
1359 }
1360 }
1361 } else {
1362 for(;;) {
1363 if(src==limit) {
1364 return TRUE;
1365 } else if((c=*src++)<_NORM_MIN_WITH_LEAD_CC) {
1366 prevCC=(int16_t)-c;
1367 } else if((fcd16=_getFCD16(c))==0) {
1368 prevCC=0;
1369 } else {
1370 break;
1371 }
1372 }
1373 }
1374
1375 /* check one above-minimum, relevant code unit */
1376 if(UTF_IS_FIRST_SURROGATE(c)) {
1377 /* c is a lead surrogate, get the real fcd16 */
1378 if(src!=limit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
1379 ++src;
1380 fcd16=_getFCD16FromSurrogatePair(fcd16, c2);
1381 } else {
1382 c2=0;
1383 fcd16=0;
1384 }
1385 } else {
1386 c2=0;
1387 }
1388
1389 if(nx_contains(nx, c, c2)) {
1390 prevCC=0; /* excluded: fcd16==0 */
1391 continue;
1392 }
1393
1394 /*
1395 * prevCC has values from the following ranges:
1396 * 0..0xff - the previous trail combining class
1397 * <0 - the negative value of the previous code unit;
1398 * that code unit was <_NORM_MIN_WITH_LEAD_CC and its _getFCD16()
1399 * was deferred so that average text is checked faster
1400 */
1401
1402 /* check the combining order */
1403 cc=(int16_t)(fcd16>>8);
1404 if(cc!=0) {
1405 if(prevCC<0) {
1406 /* the previous character was <_NORM_MIN_WITH_LEAD_CC, we need to get its trail cc */
1407 if(!nx_contains(nx, (UChar32)-prevCC)) {
1408 prevCC=(int16_t)(_getFCD16((UChar)-prevCC)&0xff);
1409 } else {
1410 prevCC=0; /* excluded: fcd16==0 */
1411 }
1412 }
1413
1414 if(cc<prevCC) {
1415 return FALSE;
1416 }
1417 }
1418 prevCC=(int16_t)(fcd16&0xff);
1419 }
1420}
1421
1422static UNormalizationCheckResult
1423_quickCheck(const UChar *src,
1424 int32_t srcLength,
1425 UNormalizationMode mode,
1426 UBool allowMaybe,
1427 const UnicodeSet *nx,
1428 UErrorCode *pErrorCode) {
1429 UChar stackBuffer[_STACK_BUFFER_CAPACITY];
1430 UChar *buffer;
1431 int32_t bufferCapacity;
1432
1433 const UChar *start, *limit;
1434 uint32_t norm32, qcNorm32, ccOrQCMask, qcMask;
1435 UChar c, c2, minNoMaybe;
1436 uint8_t cc, prevCC;
1437 UNormalizationCheckResult result;
1438
1439 /* check arguments */
1440 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
1441 return UNORM_MAYBE;
1442 }
1443
1444 if(src==NULL || srcLength<-1) {
1445 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1446 return UNORM_MAYBE;
1447 }
1448
1449 if(!_haveData(*pErrorCode)) {
1450 return UNORM_MAYBE;
1451 }
1452
1453 /* check for a valid mode and set the quick check minimum and mask */
1454 switch(mode) {
1455 case UNORM_NFC:
1456 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFC_NO_MAYBE];
1457 qcMask=_NORM_QC_NFC;
1458 break;
1459 case UNORM_NFKC:
1460 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFKC_NO_MAYBE];
1461 qcMask=_NORM_QC_NFKC;
1462 break;
1463 case UNORM_NFD:
1464 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFD_NO_MAYBE];
1465 qcMask=_NORM_QC_NFD;
1466 break;
1467 case UNORM_NFKD:
1468 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFKD_NO_MAYBE];
1469 qcMask=_NORM_QC_NFKD;
1470 break;
1471 case UNORM_FCD:
1472 return unorm_checkFCD(src, srcLength, nx) ? UNORM_YES : UNORM_NO;
1473 default:
1474 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1475 return UNORM_MAYBE;
1476 }
1477
1478 /* initialize */
1479 buffer=stackBuffer;
1480 bufferCapacity=_STACK_BUFFER_CAPACITY;
1481
1482 ccOrQCMask=_NORM_CC_MASK|qcMask;
1483 result=UNORM_YES;
1484 prevCC=0;
1485
1486 start=src;
1487 if(srcLength>=0) {
1488 /* string with length */
1489 limit=src+srcLength;
1490 } else /* srcLength==-1 */ {
1491 /* zero-terminated string */
1492 limit=NULL;
1493 }
1494
1495 U_ALIGN_CODE(16);
1496
1497 for(;;) {
1498 /* skip a run of code units below the minimum or with irrelevant data for the quick check */
1499 if(limit==NULL) {
1500 for(;;) {
1501 c=*src++;
1502 if(c<minNoMaybe) {
1503 if(c==0) {
1504 goto endloop; /* break out of outer loop */
1505 }
1506 } else if(((norm32=_getNorm32(c))&ccOrQCMask)!=0) {
1507 break;
1508 }
1509 prevCC=0;
1510 }
1511 } else {
1512 for(;;) {
1513 if(src==limit) {
1514 goto endloop; /* break out of outer loop */
1515 } else if((c=*src++)>=minNoMaybe && ((norm32=_getNorm32(c))&ccOrQCMask)!=0) {
1516 break;
1517 }
1518 prevCC=0;
1519 }
1520 }
1521
1522 /* check one above-minimum, relevant code unit */
1523 if(isNorm32LeadSurrogate(norm32)) {
1524 /* c is a lead surrogate, get the real norm32 */
1525 if(src!=limit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
1526 ++src;
1527 norm32=_getNorm32FromSurrogatePair(norm32, c2);
1528 } else {
1529 c2=0;
1530 norm32=0;
1531 }
1532 } else {
1533 c2=0;
1534 }
1535
1536 if(nx_contains(nx, c, c2)) {
1537 /* excluded: norm32==0 */
1538 norm32=0;
1539 }
1540
1541 /* check the combining order */
1542 cc=(uint8_t)(norm32>>_NORM_CC_SHIFT);
1543 if(cc!=0 && cc<prevCC) {
1544 result=UNORM_NO;
1545 break;
1546 }
1547 prevCC=cc;
1548
1549 /* check for "no" or "maybe" quick check flags */
1550 qcNorm32=norm32&qcMask;
1551 if(qcNorm32&_NORM_QC_ANY_NO) {
1552 result=UNORM_NO;
1553 break;
1554 } else if(qcNorm32!=0) {
1555 /* "maybe" can only occur for NFC and NFKC */
1556 if(allowMaybe) {
1557 result=UNORM_MAYBE;
1558 } else {
1559 /* normalize a section around here to see if it is really normalized or not */
1560 const UChar *prevStarter;
1561 uint32_t decompQCMask;
1562 int32_t length;
1563
1564 decompQCMask=(qcMask<<2)&0xf; /* decomposition quick check mask */
1565
1566 /* find the previous starter */
1567 prevStarter=src-1; /* set prevStarter to the beginning of the current character */
1568 if(UTF_IS_TRAIL(*prevStarter)) {
1569 --prevStarter; /* safe because unpaired surrogates do not result in "maybe" */
1570 }
1571 prevStarter=_findPreviousStarter(start, prevStarter, ccOrQCMask, decompQCMask, minNoMaybe);
1572
1573 /* find the next true starter in [src..limit[ - modifies src to point to the next starter */
1574 src=_findNextStarter(src, limit, qcMask, decompQCMask, minNoMaybe);
1575
1576 /* decompose and recompose [prevStarter..src[ */
1577 _composePart(stackBuffer, buffer, bufferCapacity,
1578 length,
1579 prevStarter,
1580 src,
1581 qcMask,
1582 prevCC, nx, pErrorCode);
1583 if(U_FAILURE(*pErrorCode)) {
1584 result=UNORM_MAYBE; /* error (out of memory) */
1585 break;
1586 }
1587
1588 /* compare the normalized version with the original */
1589 if(0!=uprv_strCompare(prevStarter, (int32_t)(src-prevStarter), buffer, length, FALSE, FALSE)) {
1590 result=UNORM_NO; /* normalization differs */
1591 break;
1592 }
1593
1594 /* continue after the next starter */
1595 }
1596 }
1597 }
1598endloop:
1599
1600 if(buffer!=stackBuffer) {
1601 uprv_free(buffer);
1602 }
1603
1604 return result;
1605}
1606
1607U_CAPI UNormalizationCheckResult U_EXPORT2
1608unorm_quickCheck(const UChar *src,
1609 int32_t srcLength,
1610 UNormalizationMode mode,
1611 UErrorCode *pErrorCode) {
1612 return _quickCheck(src, srcLength, mode, TRUE, NULL, pErrorCode);
1613}
1614
1615U_CAPI UNormalizationCheckResult U_EXPORT2
1616unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength,
1617 UNormalizationMode mode, int32_t options,
1618 UErrorCode *pErrorCode) {
1619 return _quickCheck(src, srcLength, mode, TRUE, getNX(options, *pErrorCode), pErrorCode);
1620}
1621
1622U_CAPI UBool U_EXPORT2
1623unorm_isNormalized(const UChar *src, int32_t srcLength,
1624 UNormalizationMode mode,
1625 UErrorCode *pErrorCode) {
1626 return (UBool)(UNORM_YES==_quickCheck(src, srcLength, mode, FALSE, NULL, pErrorCode));
1627}
1628
1629U_CAPI UBool U_EXPORT2
1630unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength,
1631 UNormalizationMode mode, int32_t options,
1632 UErrorCode *pErrorCode) {
1633 return (UBool)(UNORM_YES==_quickCheck(src, srcLength, mode, FALSE, getNX(options, *pErrorCode), pErrorCode));
1634}
1635
1636/* make NFD & NFKD ---------------------------------------------------------- */
1637
1638U_CAPI int32_t U_EXPORT2
1639unorm_getDecomposition(UChar32 c, UBool compat,
1640 UChar *dest, int32_t destCapacity) {
1641 UErrorCode errorCode=U_ZERO_ERROR;
1642 if( (uint32_t)c<=0x10ffff &&
1643 _haveData(errorCode) &&
1644 ((dest!=NULL && destCapacity>0) || destCapacity==0)
1645 ) {
1646 uint32_t norm32, qcMask;
1647 UChar32 minNoMaybe;
1648 int32_t length;
1649
1650 /* initialize */
1651 if(!compat) {
1652 minNoMaybe=(UChar32)indexes[_NORM_INDEX_MIN_NFD_NO_MAYBE];
1653 qcMask=_NORM_QC_NFD;
1654 } else {
1655 minNoMaybe=(UChar32)indexes[_NORM_INDEX_MIN_NFKD_NO_MAYBE];
1656 qcMask=_NORM_QC_NFKD;
1657 }
1658
1659 if(c<minNoMaybe) {
1660 /* trivial case */
1661 if(destCapacity>0) {
1662 dest[0]=(UChar)c;
1663 }
1664 return -1;
1665 }
1666
1667 /* data lookup */
1668 UTRIE_GET32(&normTrie, c, norm32);
1669 if((norm32&qcMask)==0) {
1670 /* simple case: no decomposition */
1671 if(c<=0xffff) {
1672 if(destCapacity>0) {
1673 dest[0]=(UChar)c;
1674 }
1675 return -1;
1676 } else {
1677 if(destCapacity>=2) {
1678 dest[0]=UTF16_LEAD(c);
1679 dest[1]=UTF16_TRAIL(c);
1680 }
1681 return -2;
1682 }
1683 } else if(isNorm32HangulOrJamo(norm32)) {
1684 /* Hangul syllable: decompose algorithmically */
1685 UChar c2;
1686
1687 c-=HANGUL_BASE;
1688
1689 c2=(UChar)(c%JAMO_T_COUNT);
1690 c/=JAMO_T_COUNT;
1691 if(c2>0) {
1692 if(destCapacity>=3) {
1693 dest[2]=(UChar)(JAMO_T_BASE+c2);
1694 }
1695 length=3;
1696 } else {
1697 length=2;
1698 }
1699
1700 if(destCapacity>=2) {
1701 dest[1]=(UChar)(JAMO_V_BASE+c%JAMO_V_COUNT);
1702 dest[0]=(UChar)(JAMO_L_BASE+c/JAMO_V_COUNT);
1703 }
1704 return length;
1705 } else {
1706 /* c decomposes, get everything from the variable-length extra data */
1707 const UChar *p, *limit;
1708 uint8_t cc, trailCC;
1709
1710 p=_decompose(norm32, qcMask, length, cc, trailCC);
1711 if(length<=destCapacity) {
1712 limit=p+length;
1713 do {
1714 *dest++=*p++;
1715 } while(p<limit);
1716 }
1717 return length;
1718 }
1719 } else {
1720 return 0;
1721 }
1722}
1723
1724static int32_t
1725_decompose(UChar *dest, int32_t destCapacity,
1726 const UChar *src, int32_t srcLength,
1727 UBool compat, const UnicodeSet *nx,
1728 uint8_t &outTrailCC) {
1729 UChar buffer[3];
1730 const UChar *limit, *prevSrc, *p;
1731 uint32_t norm32, ccOrQCMask, qcMask;
1732 int32_t destIndex, reorderStartIndex, length;
1733 UChar c, c2, minNoMaybe;
1734 uint8_t cc, prevCC, trailCC;
1735
1736 if(!compat) {
1737 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFD_NO_MAYBE];
1738 qcMask=_NORM_QC_NFD;
1739 } else {
1740 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFKD_NO_MAYBE];
1741 qcMask=_NORM_QC_NFKD;
1742 }
1743
1744 /* initialize */
1745 ccOrQCMask=_NORM_CC_MASK|qcMask;
1746 destIndex=reorderStartIndex=0;
1747 prevCC=0;
1748
1749 /* avoid compiler warnings */
1750 norm32=0;
1751 c=0;
1752
1753 if(srcLength>=0) {
1754 /* string with length */
1755 limit=src+srcLength;
1756 } else /* srcLength==-1 */ {
1757 /* zero-terminated string */
1758 limit=NULL;
1759 }
1760
1761 U_ALIGN_CODE(16);
1762
1763 for(;;) {
1764 /* count code units below the minimum or with irrelevant data for the quick check */
1765 prevSrc=src;
1766 if(limit==NULL) {
1767 while((c=*src)<minNoMaybe ? c!=0 : ((norm32=_getNorm32(c))&ccOrQCMask)==0) {
1768 prevCC=0;
1769 ++src;
1770 }
1771 } else {
1772 while(src!=limit && ((c=*src)<minNoMaybe || ((norm32=_getNorm32(c))&ccOrQCMask)==0)) {
1773 prevCC=0;
1774 ++src;
1775 }
1776 }
1777
1778 /* copy these code units all at once */
1779 if(src!=prevSrc) {
1780 length=(int32_t)(src-prevSrc);
1781 if((destIndex+length)<=destCapacity) {
1782 uprv_memcpy(dest+destIndex, prevSrc, length*U_SIZEOF_UCHAR);
1783 }
1784 destIndex+=length;
1785 reorderStartIndex=destIndex;
1786 }
1787
1788 /* end of source reached? */
1789 if(limit==NULL ? c==0 : src==limit) {
1790 break;
1791 }
1792
1793 /* c already contains *src and norm32 is set for it, increment src */
1794 ++src;
1795
1796 /* check one above-minimum, relevant code unit */
1797 /*
1798 * generally, set p and length to the decomposition string
1799 * in simple cases, p==NULL and (c, c2) will hold the length code units to append
1800 * in all cases, set cc to the lead and trailCC to the trail combining class
1801 *
1802 * the following merge-sort of the current character into the preceding,
1803 * canonically ordered result text will use the optimized _insertOrdered()
1804 * if there is only one single code point to process;
1805 * this is indicated with p==NULL, and (c, c2) is the character to insert
1806 * ((c, 0) for a BMP character and (lead surrogate, trail surrogate)
1807 * for a supplementary character)
1808 * otherwise, p[length] is merged in with _mergeOrdered()
1809 */
1810 if(isNorm32HangulOrJamo(norm32)) {
1811 if(nx_contains(nx, c)) {
1812 c2=0;
1813 p=NULL;
1814 length=1;
1815 } else {
1816 /* Hangul syllable: decompose algorithmically */
1817 p=buffer;
1818 cc=trailCC=0;
1819
1820 c-=HANGUL_BASE;
1821
1822 c2=(UChar)(c%JAMO_T_COUNT);
1823 c/=JAMO_T_COUNT;
1824 if(c2>0) {
1825 buffer[2]=(UChar)(JAMO_T_BASE+c2);
1826 length=3;
1827 } else {
1828 length=2;
1829 }
1830
1831 buffer[1]=(UChar)(JAMO_V_BASE+c%JAMO_V_COUNT);
1832 buffer[0]=(UChar)(JAMO_L_BASE+c/JAMO_V_COUNT);
1833 }
1834 } else {
1835 if(isNorm32Regular(norm32)) {
1836 c2=0;
1837 length=1;
1838 } else {
1839 /* c is a lead surrogate, get the real norm32 */
1840 if(src!=limit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
1841 ++src;
1842 length=2;
1843 norm32=_getNorm32FromSurrogatePair(norm32, c2);
1844 } else {
1845 c2=0;
1846 length=1;
1847 norm32=0;
1848 }
1849 }
1850
1851 /* get the decomposition and the lead and trail cc's */
1852 if(nx_contains(nx, c, c2)) {
1853 /* excluded: norm32==0 */
1854 cc=trailCC=0;
1855 p=NULL;
1856 } else if((norm32&qcMask)==0) {
1857 /* c does not decompose */
1858 cc=trailCC=(uint8_t)(norm32>>_NORM_CC_SHIFT);
1859 p=NULL;
1860 } else {
1861 /* c decomposes, get everything from the variable-length extra data */
1862 p=_decompose(norm32, qcMask, length, cc, trailCC);
1863 if(length==1) {
1864 /* fastpath a single code unit from decomposition */
1865 c=*p;
1866 c2=0;
1867 p=NULL;
1868 }
1869 }
1870 }
1871
1872 /* append the decomposition to the destination buffer, assume length>0 */
1873 if((destIndex+length)<=destCapacity) {
1874 UChar *reorderSplit=dest+destIndex;
1875 if(p==NULL) {
1876 /* fastpath: single code point */
1877 if(cc!=0 && cc<prevCC) {
1878 /* (c, c2) is out of order with respect to the preceding text */
1879 destIndex+=length;
1880 trailCC=_insertOrdered(dest+reorderStartIndex, reorderSplit, dest+destIndex, c, c2, cc);
1881 } else {
1882 /* just append (c, c2) */
1883 dest[destIndex++]=c;
1884 if(c2!=0) {
1885 dest[destIndex++]=c2;
1886 }
1887 }
1888 } else {
1889 /* general: multiple code points (ordered by themselves) from decomposition */
1890 if(cc!=0 && cc<prevCC) {
1891 /* the decomposition is out of order with respect to the preceding text */
1892 destIndex+=length;
1893 trailCC=_mergeOrdered(dest+reorderStartIndex, reorderSplit, p, p+length);
1894 } else {
1895 /* just append the decomposition */
1896 do {
1897 dest[destIndex++]=*p++;
1898 } while(--length>0);
1899 }
1900 }
1901 } else {
1902 /* buffer overflow */
1903 /* keep incrementing the destIndex for preflighting */
1904 destIndex+=length;
1905 }
1906
1907 prevCC=trailCC;
1908 if(prevCC==0) {
1909 reorderStartIndex=destIndex;
1910 }
1911 }
1912
1913 outTrailCC=prevCC;
1914 return destIndex;
1915}
1916
1917U_CAPI int32_t U_EXPORT2
1918unorm_decompose(UChar *dest, int32_t destCapacity,
1919 const UChar *src, int32_t srcLength,
1920 UBool compat, int32_t options,
1921 UErrorCode *pErrorCode) {
1922 const UnicodeSet *nx;
1923 int32_t destIndex;
1924 uint8_t trailCC;
1925
1926 if(!_haveData(*pErrorCode)) {
1927 return 0;
1928 }
1929
1930 nx=getNX(options, *pErrorCode);
1931 if(U_FAILURE(*pErrorCode)) {
1932 return 0;
1933 }
1934
1935 destIndex=_decompose(dest, destCapacity,
1936 src, srcLength,
1937 compat, nx,
1938 trailCC);
1939
1940 return u_terminateUChars(dest, destCapacity, destIndex, pErrorCode);
1941}
1942
1943/* make FCD ----------------------------------------------------------------- */
1944
1945static const UChar *
1946_findSafeFCD(const UChar *src, const UChar *limit, uint16_t fcd16) {
1947 UChar c, c2;
1948
1949 /*
1950 * find the first position in [src..limit[ after some cc==0 according to FCD data
1951 *
1952 * at the beginning of the loop, we have fcd16 from before src
1953 *
1954 * stop at positions:
1955 * - after trail cc==0
1956 * - at the end of the source
1957 * - before lead cc==0
1958 */
1959 for(;;) {
1960 /* stop if trail cc==0 for the previous character */
1961 if((fcd16&0xff)==0) {
1962 break;
1963 }
1964
1965 /* get c=*src - stop at end of string */
1966 if(src==limit) {
1967 break;
1968 }
1969 c=*src;
1970
1971 /* stop if lead cc==0 for this character */
1972 if(c<_NORM_MIN_WITH_LEAD_CC || (fcd16=_getFCD16(c))==0) {
1973 break; /* catches terminating NUL, too */
1974 }
1975
1976 if(!UTF_IS_FIRST_SURROGATE(c)) {
1977 if(fcd16<=0xff) {
1978 break;
1979 }
1980 ++src;
1981 } else if((src+1)!=limit && (c2=*(src+1), UTF_IS_SECOND_SURROGATE(c2))) {
1982 /* c is a lead surrogate, get the real fcd16 */
1983 fcd16=_getFCD16FromSurrogatePair(fcd16, c2);
1984 if(fcd16<=0xff) {
1985 break;
1986 }
1987 src+=2;
1988 } else {
1989 /* c is an unpaired first surrogate, lead cc==0 */
1990 break;
1991 }
1992 }
1993
1994 return src;
1995}
1996
1997static uint8_t
1998_decomposeFCD(const UChar *src, const UChar *decompLimit,
1999 UChar *dest, int32_t &destIndex, int32_t destCapacity,
2000 const UnicodeSet *nx) {
2001 const UChar *p;
2002 uint32_t norm32;
2003 int32_t reorderStartIndex, length;
2004 UChar c, c2;
2005 uint8_t cc, prevCC, trailCC;
2006
2007 /*
2008 * canonically decompose [src..decompLimit[
2009 *
2010 * all characters in this range have some non-zero cc,
2011 * directly or in decomposition,
2012 * so that we do not need to check in the following for quick-check limits etc.
2013 *
2014 * there _are_ _no_ Hangul syllables or Jamos in here because they are FCD-safe (cc==0)!
2015 *
2016 * we also do not need to check for c==0 because we have an established decompLimit
2017 */
2018 reorderStartIndex=destIndex;
2019 prevCC=0;
2020
2021 while(src<decompLimit) {
2022 c=*src++;
2023 norm32=_getNorm32(c);
2024 if(isNorm32Regular(norm32)) {
2025 c2=0;
2026 length=1;
2027 } else {
2028 /*
2029 * reminder: this function is called with [src..decompLimit[
2030 * not containing any Hangul/Jamo characters,
2031 * therefore the only specials are lead surrogates
2032 */
2033 /* c is a lead surrogate, get the real norm32 */
2034 if(src!=decompLimit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
2035 ++src;
2036 length=2;
2037 norm32=_getNorm32FromSurrogatePair(norm32, c2);
2038 } else {
2039 c2=0;
2040 length=1;
2041 norm32=0;
2042 }
2043 }
2044
2045 /* get the decomposition and the lead and trail cc's */
2046 if(nx_contains(nx, c, c2)) {
2047 /* excluded: norm32==0 */
2048 cc=trailCC=0;
2049 p=NULL;
2050 } else if((norm32&_NORM_QC_NFD)==0) {
2051 /* c does not decompose */
2052 cc=trailCC=(uint8_t)(norm32>>_NORM_CC_SHIFT);
2053 p=NULL;
2054 } else {
2055 /* c decomposes, get everything from the variable-length extra data */
2056 p=_decompose(norm32, length, cc, trailCC);
2057 if(length==1) {
2058 /* fastpath a single code unit from decomposition */
2059 c=*p;
2060 c2=0;
2061 p=NULL;
2062 }
2063 }
2064
2065 /* append the decomposition to the destination buffer, assume length>0 */
2066 if((destIndex+length)<=destCapacity) {
2067 UChar *reorderSplit=dest+destIndex;
2068 if(p==NULL) {
2069 /* fastpath: single code point */
2070 if(cc!=0 && cc<prevCC) {
2071 /* (c, c2) is out of order with respect to the preceding text */
2072 destIndex+=length;
2073 trailCC=_insertOrdered(dest+reorderStartIndex, reorderSplit, dest+destIndex, c, c2, cc);
2074 } else {
2075 /* just append (c, c2) */
2076 dest[destIndex++]=c;
2077 if(c2!=0) {
2078 dest[destIndex++]=c2;
2079 }
2080 }
2081 } else {
2082 /* general: multiple code points (ordered by themselves) from decomposition */
2083 if(cc!=0 && cc<prevCC) {
2084 /* the decomposition is out of order with respect to the preceding text */
2085 destIndex+=length;
2086 trailCC=_mergeOrdered(dest+reorderStartIndex, reorderSplit, p, p+length);
2087 } else {
2088 /* just append the decomposition */
2089 do {
2090 dest[destIndex++]=*p++;
2091 } while(--length>0);
2092 }
2093 }
2094 } else {
2095 /* buffer overflow */
2096 /* keep incrementing the destIndex for preflighting */
2097 destIndex+=length;
2098 }
2099
2100 prevCC=trailCC;
2101 if(prevCC==0) {
2102 reorderStartIndex=destIndex;
2103 }
2104 }
2105
2106 return prevCC;
2107}
2108
2109static int32_t
2110unorm_makeFCD(UChar *dest, int32_t destCapacity,
2111 const UChar *src, int32_t srcLength,
2112 const UnicodeSet *nx,
2113 UErrorCode *pErrorCode) {
2114 const UChar *limit, *prevSrc, *decompStart;
2115 int32_t destIndex, length;
2116 UChar c, c2;
2117 uint16_t fcd16;
2118 int16_t prevCC, cc;
2119
2120 if(!_haveData(*pErrorCode)) {
2121 return 0;
2122 }
2123
2124 /* initialize */
2125 decompStart=src;
2126 destIndex=0;
2127 prevCC=0;
2128
2129 /* avoid compiler warnings */
2130 c=0;
2131 fcd16=0;
2132
2133 if(srcLength>=0) {
2134 /* string with length */
2135 limit=src+srcLength;
2136 } else /* srcLength==-1 */ {
2137 /* zero-terminated string */
2138 limit=NULL;
2139 }
2140
2141 U_ALIGN_CODE(16);
2142
2143 for(;;) {
2144 /* skip a run of code units below the minimum or with irrelevant data for the FCD check */
2145 prevSrc=src;
2146 if(limit==NULL) {
2147 for(;;) {
2148 c=*src;
2149 if(c<_NORM_MIN_WITH_LEAD_CC) {
2150 if(c==0) {
2151 break;
2152 }
2153 prevCC=(int16_t)-c;
2154 } else if((fcd16=_getFCD16(c))==0) {
2155 prevCC=0;
2156 } else {
2157 break;
2158 }
2159 ++src;
2160 }
2161 } else {
2162 for(;;) {
2163 if(src==limit) {
2164 break;
2165 } else if((c=*src)<_NORM_MIN_WITH_LEAD_CC) {
2166 prevCC=(int16_t)-c;
2167 } else if((fcd16=_getFCD16(c))==0) {
2168 prevCC=0;
2169 } else {
2170 break;
2171 }
2172 ++src;
2173 }
2174 }
2175
2176 /*
2177 * prevCC has values from the following ranges:
2178 * 0..0xff - the previous trail combining class
2179 * <0 - the negative value of the previous code unit;
2180 * that code unit was <_NORM_MIN_WITH_LEAD_CC and its _getFCD16()
2181 * was deferred so that average text is checked faster
2182 */
2183
2184 /* copy these code units all at once */
2185 if(src!=prevSrc) {
2186 length=(int32_t)(src-prevSrc);
2187 if((destIndex+length)<=destCapacity) {
2188 uprv_memcpy(dest+destIndex, prevSrc, length*U_SIZEOF_UCHAR);
2189 }
2190 destIndex+=length;
2191 prevSrc=src;
2192
2193 /* prevCC<0 is only possible from the above loop, i.e., only if prevSrc<src */
2194 if(prevCC<0) {
2195 /* the previous character was <_NORM_MIN_WITH_LEAD_CC, we need to get its trail cc */
2196 if(!nx_contains(nx, (UChar32)-prevCC)) {
2197 prevCC=(int16_t)(_getFCD16((UChar)-prevCC)&0xff);
2198 } else {
2199 prevCC=0; /* excluded: fcd16==0 */
2200 }
2201
2202 /*
2203 * set a pointer to this below-U+0300 character;
2204 * if prevCC==0 then it will moved to after this character below
2205 */
2206 decompStart=prevSrc-1;
2207 }
2208 }
2209 /*
2210 * now:
2211 * prevSrc==src - used later to adjust destIndex before decomposition
2212 * prevCC>=0
2213 */
2214
2215 /* end of source reached? */
2216 if(limit==NULL ? c==0 : src==limit) {
2217 break;
2218 }
2219
2220 /* set a pointer to after the last source position where prevCC==0 */
2221 if(prevCC==0) {
2222 decompStart=prevSrc;
2223 }
2224
2225 /* c already contains *src and fcd16 is set for it, increment src */
2226 ++src;
2227
2228 /* check one above-minimum, relevant code unit */
2229 if(UTF_IS_FIRST_SURROGATE(c)) {
2230 /* c is a lead surrogate, get the real fcd16 */
2231 if(src!=limit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
2232 ++src;
2233 fcd16=_getFCD16FromSurrogatePair(fcd16, c2);
2234 } else {
2235 c2=0;
2236 fcd16=0;
2237 }
2238 } else {
2239 c2=0;
2240 }
2241
2242 /* we are looking at the character (c, c2) at [prevSrc..src[ */
2243 if(nx_contains(nx, c, c2)) {
2244 fcd16=0; /* excluded: fcd16==0 */
2245 }
2246
2247 /* check the combining order, get the lead cc */
2248 cc=(int16_t)(fcd16>>8);
2249 if(cc==0 || cc>=prevCC) {
2250 /* the order is ok */
2251 if(cc==0) {
2252 decompStart=prevSrc;
2253 }
2254 prevCC=(int16_t)(fcd16&0xff);
2255
2256 /* just append (c, c2) */
2257 length= c2==0 ? 1 : 2;
2258 if((destIndex+length)<=destCapacity) {
2259 dest[destIndex++]=c;
2260 if(c2!=0) {
2261 dest[destIndex++]=c2;
2262 }
2263 } else {
2264 destIndex+=length;
2265 }
2266 } else {
2267 /*
2268 * back out the part of the source that we copied already but
2269 * is now going to be decomposed;
2270 * prevSrc is set to after what was copied
2271 */
2272 destIndex-=(int32_t)(prevSrc-decompStart);
2273
2274 /*
2275 * find the part of the source that needs to be decomposed;
2276 * to be safe and simple, decompose to before the next character with lead cc==0
2277 */
2278 src=_findSafeFCD(src, limit, fcd16);
2279
2280 /*
2281 * the source text does not fulfill the conditions for FCD;
2282 * decompose and reorder a limited piece of the text
2283 */
2284 prevCC=_decomposeFCD(decompStart, src,
2285 dest, destIndex, destCapacity,
2286 nx);
2287 decompStart=src;
2288 }
2289 }
2290
2291 return u_terminateUChars(dest, destCapacity, destIndex, pErrorCode);
2292}
2293
2294/* make NFC & NFKC ---------------------------------------------------------- */
2295
2296/* get the composition properties of the next character */
2297static inline uint32_t
2298_getNextCombining(UChar *&p, const UChar *limit,
2299 UChar &c, UChar &c2,
2300 uint16_t &combiningIndex, uint8_t &cc,
2301 const UnicodeSet *nx) {
2302 uint32_t norm32, combineFlags;
2303
2304 /* get properties */
2305 c=*p++;
2306 norm32=_getNorm32(c);
2307
2308 /* preset output values for most characters */
2309 c2=0;
2310 combiningIndex=0;
2311 cc=0;
2312
2313 if((norm32&(_NORM_CC_MASK|_NORM_COMBINES_ANY))==0) {
2314 return 0;
2315 } else {
2316 if(isNorm32Regular(norm32)) {
2317 /* set cc etc. below */
2318 } else if(isNorm32HangulOrJamo(norm32)) {
2319 /* a compatibility decomposition contained Jamos */
2320 combiningIndex=(uint16_t)(0xfff0|(norm32>>_NORM_EXTRA_SHIFT));
2321 return norm32&_NORM_COMBINES_ANY;
2322 } else {
2323 /* c is a lead surrogate, get the real norm32 */
2324 if(p!=limit && UTF_IS_SECOND_SURROGATE(c2=*p)) {
2325 ++p;
2326 norm32=_getNorm32FromSurrogatePair(norm32, c2);
2327 } else {
2328 c2=0;
2329 return 0;
2330 }
2331 }
2332
2333 if(nx_contains(nx, c, c2)) {
2334 return 0; /* excluded: norm32==0 */
2335 }
2336
2337 cc=(uint8_t)(norm32>>_NORM_CC_SHIFT);
2338
2339 combineFlags=norm32&_NORM_COMBINES_ANY;
2340 if(combineFlags!=0) {
2341 combiningIndex=*(_getExtraData(norm32)-1);
2342 }
2343 return combineFlags;
2344 }
2345}
2346
2347/*
2348 * given a composition-result starter (c, c2) - which means its cc==0,
2349 * it combines forward, it has extra data, its norm32!=0,
2350 * it is not a Hangul or Jamo,
2351 * get just its combineFwdIndex
2352 *
2353 * norm32(c) is special if and only if c2!=0
2354 */
2355static inline uint16_t
2356_getCombiningIndexFromStarter(UChar c, UChar c2) {
2357 uint32_t norm32;
2358
2359 norm32=_getNorm32(c);
2360 if(c2!=0) {
2361 norm32=_getNorm32FromSurrogatePair(norm32, c2);
2362 }
2363 return *(_getExtraData(norm32)-1);
2364}
2365
2366/*
2367 * Find the recomposition result for
2368 * a forward-combining character
2369 * (specified with a pointer to its part of the combiningTable[])
2370 * and a backward-combining character
2371 * (specified with its combineBackIndex).
2372 *
2373 * If these two characters combine, then set (value, value2)
2374 * with the code unit(s) of the composition character.
2375 *
2376 * Return value:
2377 * 0 do not combine
2378 * 1 combine
2379 * >1 combine, and the composition is a forward-combining starter
2380 *
2381 * See unormimp.h for a description of the composition table format.
2382 */
2383static inline uint16_t
2384_combine(const uint16_t *table, uint16_t combineBackIndex,
2385 uint16_t &value, uint16_t &value2) {
2386 uint16_t key;
2387
2388 /* search in the starter's composition table */
2389 for(;;) {
2390 key=*table++;
2391 if(key>=combineBackIndex) {
2392 break;
2393 }
2394 table+= *table&0x8000 ? 2 : 1;
2395 }
2396
2397 /* mask off bit 15, the last-entry-in-the-list flag */
2398 if((key&0x7fff)==combineBackIndex) {
2399 /* found! combine! */
2400 value=*table;
2401
2402 /* is the composition a starter that combines forward? */
2403 key=(uint16_t)((value&0x2000)+1);
2404
2405 /* get the composition result code point from the variable-length result value */
2406 if(value&0x8000) {
2407 if(value&0x4000) {
2408 /* surrogate pair composition result */
2409 value=(uint16_t)((value&0x3ff)|0xd800);
2410 value2=*(table+1);
2411 } else {
2412 /* BMP composition result U+2000..U+ffff */
2413 value=*(table+1);
2414 value2=0;
2415 }
2416 } else {
2417 /* BMP composition result U+0000..U+1fff */
2418 value&=0x1fff;
2419 value2=0;
2420 }
2421
2422 return key;
2423 } else {
2424 /* not found */
2425 return 0;
2426 }
2427}
2428
2429/*
2430 * recompose the characters in [p..limit[
2431 * (which is in NFD - decomposed and canonically ordered),
2432 * adjust limit, and return the trailing cc
2433 *
2434 * since for NFKC we may get Jamos in decompositions, we need to
2435 * recompose those too
2436 *
2437 * note that recomposition never lengthens the text:
2438 * any character consists of either one or two code units;
2439 * a composition may contain at most one more code unit than the original starter,
2440 * while the combining mark that is removed has at least one code unit
2441 */
2442static uint8_t
2443_recompose(UChar *p, UChar *&limit, const UnicodeSet *nx) {
2444 UChar *starter, *pRemove, *q, *r;
2445 uint32_t combineFlags;
2446 UChar c, c2;
2447 uint16_t combineFwdIndex, combineBackIndex;
2448 uint16_t result, value, value2;
2449 uint8_t cc, prevCC;
2450 UBool starterIsSupplementary;
2451
2452 starter=NULL; /* no starter */
2453 combineFwdIndex=0; /* will not be used until starter!=NULL - avoid compiler warnings */
2454 combineBackIndex=0; /* will always be set if combineFlags!=0 - avoid compiler warnings */
2455 value=value2=0; /* always set by _combine() before used - avoid compiler warnings */
2456 starterIsSupplementary=FALSE; /* will not be used until starter!=NULL - avoid compiler warnings */
2457 prevCC=0;
2458
2459 for(;;) {
2460 combineFlags=_getNextCombining(p, limit, c, c2, combineBackIndex, cc, nx);
2461 if((combineFlags&_NORM_COMBINES_BACK) && starter!=NULL) {
2462 if(combineBackIndex&0x8000) {
2463 /* c is a Jamo V/T, see if we can compose it with the previous character */
2464 pRemove=NULL; /* NULL while no Hangul composition */
2465 c2=*starter;
2466 if(combineBackIndex==0xfff2) {
2467 /* Jamo V, compose with previous Jamo L and following Jamo T */
2468 c2=(UChar)(c2-JAMO_L_BASE);
2469 if(c2<JAMO_L_COUNT) {
2470 pRemove=p-1;
2471 c=(UChar)(HANGUL_BASE+(c2*JAMO_V_COUNT+(c-JAMO_V_BASE))*JAMO_T_COUNT);
2472 if(p!=limit && (c2=(UChar)(*p-JAMO_T_BASE))<JAMO_T_COUNT) {
2473 ++p;
2474 c+=c2;
2475 }
2476 if(!nx_contains(nx, c)) {
2477 *starter=c;
2478 } else {
2479 /* excluded */
2480 if(!isHangulWithoutJamoT(c)) {
2481 --p; /* undo the ++p from reading the Jamo T */
2482 }
2483 /* c is modified but not used any more -- c=*(p-1); -- re-read the Jamo V/T */
2484 pRemove=NULL;
2485 }
2486 }
2487#if 0
2488 /*
2489 * The following is disabled with #if 0 because it can not occur:
2490 * Since the input is in NFD, there are no Hangul LV syllables that
2491 * a Jamo T could combine with.
2492 * All Jamo Ts are combined above when handling Jamo Vs.
2493 */
2494 } else {
2495 /* Jamo T, compose with previous Hangul that does not have a Jamo T */
2496 if(isHangulWithoutJamoT(c2)) {
2497 pRemove=p-1;
2498 *starter=(UChar)(c2+(c-JAMO_T_BASE));
2499 }
2500#endif
2501 }
2502
2503 if(pRemove!=NULL) {
2504 /* remove the Jamo(s) */
2505 q=pRemove;
2506 r=p;
2507 while(r<limit) {
2508 *q++=*r++;
2509 }
2510 p=pRemove;
2511 limit=q;
2512 }
2513
2514 c2=0; /* c2 held *starter temporarily */
2515
2516 /*
2517 * now: cc==0 and the combining index does not include "forward" ->
2518 * the rest of the loop body will reset starter to NULL;
2519 * technically, a composed Hangul syllable is a starter, but it
2520 * does not combine forward now that we have consumed all eligible Jamos;
2521 * for Jamo V/T, combineFlags does not contain _NORM_COMBINES_FWD
2522 */
2523
2524 } else if(
2525 /* the starter is not a Jamo V/T and */
2526 !(combineFwdIndex&0x8000) &&
2527 /* the combining mark is not blocked and */
2528 (prevCC<cc || prevCC==0) &&
2529 /* the starter and the combining mark (c, c2) do combine and */
2530 0!=(result=_combine(combiningTable+combineFwdIndex, combineBackIndex, value, value2)) &&
2531 /* the composition result is not excluded */
2532 !nx_contains(nx, value, value2)
2533 ) {
2534 /* replace the starter with the composition, remove the combining mark */
2535 pRemove= c2==0 ? p-1 : p-2; /* pointer to the combining mark */
2536
2537 /* replace the starter with the composition */
2538 *starter=(UChar)value;
2539 if(starterIsSupplementary) {
2540 if(value2!=0) {
2541 /* both are supplementary */
2542 *(starter+1)=(UChar)value2;
2543 } else {
2544 /* the composition is shorter than the starter, move the intermediate characters forward one */
2545 starterIsSupplementary=FALSE;
2546 q=starter+1;
2547 r=q+1;
2548 while(r<pRemove) {
2549 *q++=*r++;
2550 }
2551 --pRemove;
2552 }
2553 } else if(value2!=0) {
2554 /* the composition is longer than the starter, move the intermediate characters back one */
2555 starterIsSupplementary=TRUE;
2556 ++starter; /* temporarily increment for the loop boundary */
2557 q=pRemove;
2558 r=++pRemove;
2559 while(starter<q) {
2560 *--r=*--q;
2561 }
2562 *starter=(UChar)value2;
2563 --starter; /* undo the temporary increment */
2564 /* } else { both are on the BMP, nothing more to do */
2565 }
2566
2567 /* remove the combining mark by moving the following text over it */
2568 if(pRemove<p) {
2569 q=pRemove;
2570 r=p;
2571 while(r<limit) {
2572 *q++=*r++;
2573 }
2574 p=pRemove;
2575 limit=q;
2576 }
2577
2578 /* keep prevCC because we removed the combining mark */
2579
2580 /* done? */
2581 if(p==limit) {
2582 return prevCC;
2583 }
2584
2585 /* is the composition a starter that combines forward? */
2586 if(result>1) {
2587 combineFwdIndex=_getCombiningIndexFromStarter((UChar)value, (UChar)value2);
2588 } else {
2589 starter=NULL;
2590 }
2591
2592 /* we combined and set prevCC, continue with looking for compositions */
2593 continue;
2594 }
2595 }
2596
2597 /* no combination this time */
2598 prevCC=cc;
2599 if(p==limit) {
2600 return prevCC;
2601 }
2602
2603 /* if (c, c2) did not combine, then check if it is a starter */
2604 if(cc==0) {
2605 /* found a new starter; combineFlags==0 if (c, c2) is excluded */
2606 if(combineFlags&_NORM_COMBINES_FWD) {
2607 /* it may combine with something, prepare for it */
2608 if(c2==0) {
2609 starterIsSupplementary=FALSE;
2610 starter=p-1;
2611 } else {
2612 starterIsSupplementary=TRUE;
2613 starter=p-2;
2614 }
2615 combineFwdIndex=combineBackIndex;
2616 } else {
2617 /* it will not combine with anything */
2618 starter=NULL;
2619 }
2620 }
2621 }
2622}
2623
2624/* find the last true starter in [start..src[ and return the pointer to it */
2625static const UChar *
2626_findPreviousStarter(const UChar *start, const UChar *src,
2627 uint32_t ccOrQCMask, uint32_t decompQCMask, UChar minNoMaybe) {
2628 uint32_t norm32;
2629 UChar c, c2;
2630
2631 while(start<src) {
2632 norm32=_getPrevNorm32(start, src, minNoMaybe, ccOrQCMask|decompQCMask, c, c2);
2633 if(_isTrueStarter(norm32, ccOrQCMask, decompQCMask)) {
2634 break;
2635 }
2636 }
2637 return src;
2638}
2639
2640/* find the first true starter in [src..limit[ and return the pointer to it */
2641static const UChar *
2642_findNextStarter(const UChar *src, const UChar *limit,
2643 uint32_t qcMask, uint32_t decompQCMask, UChar minNoMaybe) {
2644 const UChar *p;
2645 uint32_t norm32, ccOrQCMask;
2646 int32_t length;
2647 UChar c, c2;
2648 uint8_t cc, trailCC;
2649
2650 ccOrQCMask=_NORM_CC_MASK|qcMask;
2651
2652 for(;;) {
2653 if(src==limit) {
2654 break; /* end of string */
2655 }
2656 c=*src;
2657 if(c<minNoMaybe) {
2658 break; /* catches NUL terminater, too */
2659 }
2660
2661 norm32=_getNorm32(c);
2662 if((norm32&ccOrQCMask)==0) {
2663 break; /* true starter */
2664 }
2665
2666 if(isNorm32LeadSurrogate(norm32)) {
2667 /* c is a lead surrogate, get the real norm32 */
2668 if((src+1)==limit || !UTF_IS_SECOND_SURROGATE(c2=*(src+1))) {
2669 break; /* unmatched first surrogate: counts as a true starter */
2670 }
2671 norm32=_getNorm32FromSurrogatePair(norm32, c2);
2672
2673 if((norm32&ccOrQCMask)==0) {
2674 break; /* true starter */
2675 }
2676 } else {
2677 c2=0;
2678 }
2679
2680 /* (c, c2) is not a true starter but its decomposition may be */
2681 if(norm32&decompQCMask) {
2682 /* (c, c2) decomposes, get everything from the variable-length extra data */
2683 p=_decompose(norm32, decompQCMask, length, cc, trailCC);
2684
2685 /* get the first character's norm32 to check if it is a true starter */
2686 if(cc==0 && (_getNorm32(p, qcMask)&qcMask)==0) {
2687 break; /* true starter */
2688 }
2689 }
2690
2691 src+= c2==0 ? 1 : 2; /* not a true starter, continue */
2692 }
2693
2694 return src;
2695}
2696
2697/* decompose and recompose [prevStarter..src[ */
2698static const UChar *
2699_composePart(UChar *stackBuffer, UChar *&buffer, int32_t &bufferCapacity, int32_t &length,
2700 const UChar *prevStarter, const UChar *src,
2701 uint32_t qcMask, uint8_t &prevCC,
2702 const UnicodeSet *nx,
2703 UErrorCode *pErrorCode) {
2704 UChar *recomposeLimit;
2705 uint8_t trailCC;
2706 UBool compat;
2707
2708 compat=(UBool)((qcMask&_NORM_QC_NFKC)!=0);
2709
2710 /* decompose [prevStarter..src[ */
2711 length=_decompose(buffer, bufferCapacity,
2712 prevStarter, src-prevStarter,
2713 compat, nx,
2714 trailCC);
2715 if(length>bufferCapacity) {
2716 if(!u_growBufferFromStatic(stackBuffer, &buffer, &bufferCapacity, 2*length, 0)) {
2717 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
2718 return NULL;
2719 }
2720 length=_decompose(buffer, bufferCapacity,
2721 prevStarter, src-prevStarter,
2722 compat, nx,
2723 trailCC);
2724 }
2725
2726 /* recompose the decomposition */
2727 recomposeLimit=buffer+length;
2728 if(length>=2) {
2729 prevCC=_recompose(buffer, recomposeLimit, nx);
2730 }
2731
2732 /* return with a pointer to the recomposition and its length */
2733 length=recomposeLimit-buffer;
2734 return buffer;
2735}
2736
2737static inline UBool
2738_composeHangul(UChar prev, UChar c, uint32_t norm32, const UChar *&src, const UChar *limit,
2739 UBool compat, UChar *dest, const UnicodeSet *nx) {
2740 if(isJamoVTNorm32JamoV(norm32)) {
2741 /* c is a Jamo V, compose with previous Jamo L and following Jamo T */
2742 prev=(UChar)(prev-JAMO_L_BASE);
2743 if(prev<JAMO_L_COUNT) {
2744 c=(UChar)(HANGUL_BASE+(prev*JAMO_V_COUNT+(c-JAMO_V_BASE))*JAMO_T_COUNT);
2745
2746 /* check if the next character is a Jamo T (normal or compatibility) */
2747 if(src!=limit) {
2748 UChar next, t;
2749
2750 next=*src;
2751 if((t=(UChar)(next-JAMO_T_BASE))<JAMO_T_COUNT) {
2752 /* normal Jamo T */
2753 ++src;
2754 c+=t;
2755 } else if(compat) {
2756 /* if NFKC, then check for compatibility Jamo T (BMP only) */
2757 norm32=_getNorm32(next);
2758 if(isNorm32Regular(norm32) && (norm32&_NORM_QC_NFKD)) {
2759 const UChar *p;
2760 int32_t length;
2761 uint8_t cc, trailCC;
2762
2763 p=_decompose(norm32, _NORM_QC_NFKD, length, cc, trailCC);
2764 if(length==1 && (t=(UChar)(*p-JAMO_T_BASE))<JAMO_T_COUNT) {
2765 /* compatibility Jamo T */
2766 ++src;
2767 c+=t;
2768 }
2769 }
2770 }
2771 }
2772 if(nx_contains(nx, c)) {
2773 if(!isHangulWithoutJamoT(c)) {
2774 --src; /* undo ++src from reading the Jamo T */
2775 }
2776 return FALSE;
2777 }
2778 if(dest!=0) {
2779 *dest=c;
2780 }
2781 return TRUE;
2782 }
2783 } else if(isHangulWithoutJamoT(prev)) {
2784 /* c is a Jamo T, compose with previous Hangul LV that does not contain a Jamo T */
2785 c=(UChar)(prev+(c-JAMO_T_BASE));
2786 if(nx_contains(nx, c)) {
2787 return FALSE;
2788 }
2789 if(dest!=0) {
2790 *dest=c;
2791 }
2792 return TRUE;
2793 }
2794 return FALSE;
2795}
2796
2797static int32_t
2798_compose(UChar *dest, int32_t destCapacity,
2799 const UChar *src, int32_t srcLength,
2800 UBool compat, const UnicodeSet *nx,
2801 UErrorCode *pErrorCode) {
2802 UChar stackBuffer[_STACK_BUFFER_CAPACITY];
2803 UChar *buffer;
2804 int32_t bufferCapacity;
2805
2806 const UChar *limit, *prevSrc, *prevStarter;
2807 uint32_t norm32, ccOrQCMask, qcMask;
2808 int32_t destIndex, reorderStartIndex, length;
2809 UChar c, c2, minNoMaybe;
2810 uint8_t cc, prevCC;
2811
2812 if(!compat) {
2813 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFC_NO_MAYBE];
2814 qcMask=_NORM_QC_NFC;
2815 } else {
2816 minNoMaybe=(UChar)indexes[_NORM_INDEX_MIN_NFKC_NO_MAYBE];
2817 qcMask=_NORM_QC_NFKC;
2818 }
2819
2820 /* initialize */
2821 buffer=stackBuffer;
2822 bufferCapacity=_STACK_BUFFER_CAPACITY;
2823
2824 /*
2825 * prevStarter points to the last character before the current one
2826 * that is a "true" starter with cc==0 and quick check "yes".
2827 *
2828 * prevStarter will be used instead of looking for a true starter
2829 * while incrementally decomposing [prevStarter..prevSrc[
2830 * in _composePart(). Having a good prevStarter allows to just decompose
2831 * the entire [prevStarter..prevSrc[.
2832 *
2833 * When _composePart() backs out from prevSrc back to prevStarter,
2834 * then it also backs out destIndex by the same amount.
2835 * Therefore, at all times, the (prevSrc-prevStarter) source units
2836 * must correspond 1:1 to destination units counted with destIndex,
2837 * except for reordering.
2838 * This is true for the qc "yes" characters copied in the fast loop,
2839 * and for pure reordering.
2840 * prevStarter must be set forward to src when this is not true:
2841 * In _composePart() and after composing a Hangul syllable.
2842 *
2843 * This mechanism relies on the assumption that the decomposition of a true starter
2844 * also begins with a true starter. gennorm/store.c checks for this.
2845 */
2846 prevStarter=src;
2847
2848 ccOrQCMask=_NORM_CC_MASK|qcMask;
2849 destIndex=reorderStartIndex=0;
2850 prevCC=0;
2851
2852 /* avoid compiler warnings */
2853 norm32=0;
2854 c=0;
2855
2856 if(srcLength>=0) {
2857 /* string with length */
2858 limit=src+srcLength;
2859 } else /* srcLength==-1 */ {
2860 /* zero-terminated string */
2861 limit=NULL;
2862 }
2863
2864 U_ALIGN_CODE(16);
2865
2866 for(;;) {
2867 /* count code units below the minimum or with irrelevant data for the quick check */
2868 prevSrc=src;
2869 if(limit==NULL) {
2870 while((c=*src)<minNoMaybe ? c!=0 : ((norm32=_getNorm32(c))&ccOrQCMask)==0) {
2871 prevCC=0;
2872 ++src;
2873 }
2874 } else {
2875 while(src!=limit && ((c=*src)<minNoMaybe || ((norm32=_getNorm32(c))&ccOrQCMask)==0)) {
2876 prevCC=0;
2877 ++src;
2878 }
2879 }
2880
2881 /* copy these code units all at once */
2882 if(src!=prevSrc) {
2883 length=(int32_t)(src-prevSrc);
2884 if((destIndex+length)<=destCapacity) {
2885 uprv_memcpy(dest+destIndex, prevSrc, length*U_SIZEOF_UCHAR);
2886 }
2887 destIndex+=length;
2888 reorderStartIndex=destIndex;
2889
2890 /* set prevStarter to the last character in the quick check loop */
2891 prevStarter=src-1;
2892 if(UTF_IS_SECOND_SURROGATE(*prevStarter) && prevSrc<prevStarter && UTF_IS_FIRST_SURROGATE(*(prevStarter-1))) {
2893 --prevStarter;
2894 }
2895
2896 prevSrc=src;
2897 }
2898
2899 /* end of source reached? */
2900 if(limit==NULL ? c==0 : src==limit) {
2901 break;
2902 }
2903
2904 /* c already contains *src and norm32 is set for it, increment src */
2905 ++src;
2906
2907 /*
2908 * source buffer pointers:
2909 *
2910 * all done quick check current char not yet
2911 * "yes" but (c, c2) processed
2912 * may combine
2913 * forward
2914 * [-------------[-------------[-------------[-------------[
2915 * | | | | |
2916 * start prevStarter prevSrc src limit
2917 *
2918 *
2919 * destination buffer pointers and indexes:
2920 *
2921 * all done might take not filled yet
2922 * characters for
2923 * reordering
2924 * [-------------[-------------[-------------[
2925 * | | | |
2926 * dest reorderStartIndex destIndex destCapacity
2927 */
2928
2929 /* check one above-minimum, relevant code unit */
2930 /*
2931 * norm32 is for c=*(src-1), and the quick check flag is "no" or "maybe", and/or cc!=0
2932 * check for Jamo V/T, then for surrogates and regular characters
2933 * c is not a Hangul syllable or Jamo L because
2934 * they are not marked with no/maybe for NFC & NFKC (and their cc==0)
2935 */
2936 if(isNorm32HangulOrJamo(norm32)) {
2937 /*
2938 * c is a Jamo V/T:
2939 * try to compose with the previous character, Jamo V also with a following Jamo T,
2940 * and set values here right now in case we just continue with the main loop
2941 */
2942 prevCC=cc=0;
2943 reorderStartIndex=destIndex;
2944
2945 if(
2946 destIndex>0 &&
2947 _composeHangul(
2948 *(prevSrc-1), c, norm32, src, limit, compat,
2949 destIndex<=destCapacity ? dest+(destIndex-1) : 0,
2950 nx)
2951 ) {
2952 prevStarter=src;
2953 continue;
2954 }
2955
2956 /* the Jamo V/T did not compose into a Hangul syllable, just append to dest */
2957 c2=0;
2958 length=1;
2959 prevStarter=prevSrc;
2960 } else {
2961 if(isNorm32Regular(norm32)) {
2962 c2=0;
2963 length=1;
2964 } else {
2965 /* c is a lead surrogate, get the real norm32 */
2966 if(src!=limit && UTF_IS_SECOND_SURROGATE(c2=*src)) {
2967 ++src;
2968 length=2;
2969 norm32=_getNorm32FromSurrogatePair(norm32, c2);
2970 } else {
2971 /* c is an unpaired lead surrogate, nothing to do */
2972 c2=0;
2973 length=1;
2974 norm32=0;
2975 }
2976 }
2977
2978 /* we are looking at the character (c, c2) at [prevSrc..src[ */
2979 if(nx_contains(nx, c, c2)) {
2980 /* excluded: norm32==0 */
2981 cc=0;
2982 } else if((norm32&qcMask)==0) {
2983 cc=(uint8_t)(norm32>>_NORM_CC_SHIFT);
2984 } else {
2985 const UChar *p;
2986 uint32_t decompQCMask;
2987
2988 /*
2989 * find appropriate boundaries around this character,
2990 * decompose the source text from between the boundaries,
2991 * and recompose it
2992 *
2993 * this puts the intermediate text into the side buffer because
2994 * it might be longer than the recomposition end result,
2995 * or the destination buffer may be too short or missing
2996 *
2997 * note that destIndex may be adjusted backwards to account
2998 * for source text that passed the quick check but needed to
2999 * take part in the recomposition
3000 */
3001 decompQCMask=(qcMask<<2)&0xf; /* decomposition quick check mask */
3002
3003 /*
3004 * find the last true starter in [prevStarter..src[
3005 * it is either the decomposition of the current character (at prevSrc),
3006 * or prevStarter
3007 */
3008 if(_isTrueStarter(norm32, ccOrQCMask, decompQCMask)) {
3009 prevStarter=prevSrc;
3010 } else {
3011 /* adjust destIndex: back out what had been copied with qc "yes" */
3012 destIndex-=(int32_t)(prevSrc-prevStarter);
3013 }
3014
3015 /* find the next true starter in [src..limit[ - modifies src to point to the next starter */
3016 src=_findNextStarter(src, limit, qcMask, decompQCMask, minNoMaybe);
3017
3018 /* compose [prevStarter..src[ */
3019 p=_composePart(stackBuffer, buffer, bufferCapacity,
3020 length, /* output */
3021 prevStarter, src,
3022 qcMask,
3023 prevCC, /* output */
3024 nx,
3025 pErrorCode);
3026
3027 if(p==NULL) {
3028 destIndex=0; /* an error occurred (out of memory) */
3029 break;
3030 }
3031
3032 /* append the recomposed buffer contents to the destination buffer */
3033 if((destIndex+length)<=destCapacity) {
3034 while(length>0) {
3035 dest[destIndex++]=*p++;
3036 --length;
3037 }
3038 } else {
3039 /* buffer overflow */
3040 /* keep incrementing the destIndex for preflighting */
3041 destIndex+=length;
3042 }
3043
3044 /* set the next starter */
3045 prevStarter=src;
3046
3047 continue;
3048 }
3049 }
3050
3051 /* append the single code point (c, c2) to the destination buffer */
3052 if((destIndex+length)<=destCapacity) {
3053 if(cc!=0 && cc<prevCC) {
3054 /* (c, c2) is out of order with respect to the preceding text */
3055 UChar *reorderSplit=dest+destIndex;
3056 destIndex+=length;
3057 prevCC=_insertOrdered(dest+reorderStartIndex, reorderSplit, dest+destIndex, c, c2, cc);
3058 } else {
3059 /* just append (c, c2) */
3060 dest[destIndex++]=c;
3061 if(c2!=0) {
3062 dest[destIndex++]=c2;
3063 }
3064 prevCC=cc;
3065 }
3066 } else {
3067 /* buffer overflow */
3068 /* keep incrementing the destIndex for preflighting */
3069 destIndex+=length;
3070 prevCC=cc;
3071 }
3072 }
3073
3074 /* cleanup */
3075 if(buffer!=stackBuffer) {
3076 uprv_free(buffer);
3077 }
3078
3079 return destIndex;
3080}
3081
3082U_CAPI int32_t U_EXPORT2
3083unorm_compose(UChar *dest, int32_t destCapacity,
3084 const UChar *src, int32_t srcLength,
3085 UBool compat, int32_t options,
3086 UErrorCode *pErrorCode) {
3087 const UnicodeSet *nx;
3088 int32_t destIndex;
3089
3090 if(!_haveData(*pErrorCode)) {
3091 return 0;
3092 }
3093
3094 nx=getNX(options, *pErrorCode);
3095 if(U_FAILURE(*pErrorCode)) {
3096 return 0;
3097 }
3098
3099 destIndex=_compose(dest, destCapacity,
3100 src, srcLength,
3101 compat, nx,
3102 pErrorCode);
3103
3104 return u_terminateUChars(dest, destCapacity, destIndex, pErrorCode);
3105}
3106
3107/* normalize() API ---------------------------------------------------------- */
3108
3109/**
3110 * Internal API for normalizing.
3111 * Does not check for bad input.
3112 * Requires _haveData() to be true.
3113 * @internal
3114 */
3115static int32_t
3116unorm_internalNormalize(UChar *dest, int32_t destCapacity,
3117 const UChar *src, int32_t srcLength,
3118 UNormalizationMode mode, const UnicodeSet *nx,
3119 UErrorCode *pErrorCode) {
3120 int32_t destLength;
3121 uint8_t trailCC;
3122
3123 switch(mode) {
3124 case UNORM_NFD:
3125 destLength=_decompose(dest, destCapacity,
3126 src, srcLength,
3127 FALSE, nx, trailCC);
3128 break;
3129 case UNORM_NFKD:
3130 destLength=_decompose(dest, destCapacity,
3131 src, srcLength,
3132 TRUE, nx, trailCC);
3133 break;
3134 case UNORM_NFC:
3135 destLength=_compose(dest, destCapacity,
3136 src, srcLength,
3137 FALSE, nx, pErrorCode);
3138 break;
3139 case UNORM_NFKC:
3140 destLength=_compose(dest, destCapacity,
3141 src, srcLength,
3142 TRUE, nx, pErrorCode);
3143 break;
3144 case UNORM_FCD:
3145 return unorm_makeFCD(dest, destCapacity,
3146 src, srcLength,
3147 nx,
3148 pErrorCode);
3149 case UNORM_NONE:
3150 /* just copy the string */
3151 if(srcLength==-1) {
3152 srcLength=u_strlen(src);
3153 }
3154 if(srcLength>0 && srcLength<=destCapacity) {
3155 uprv_memcpy(dest, src, srcLength*U_SIZEOF_UCHAR);
3156 }
3157 destLength=srcLength;
3158 break;
3159 default:
3160 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3161 return 0;
3162 }
3163
3164 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
3165}
3166
3167/**
3168 * Internal API for normalizing.
3169 * Does not check for bad input.
3170 * @internal
3171 */
3172U_CAPI int32_t U_EXPORT2
3173unorm_internalNormalize(UChar *dest, int32_t destCapacity,
3174 const UChar *src, int32_t srcLength,
3175 UNormalizationMode mode, int32_t options,
3176 UErrorCode *pErrorCode) {
3177 const UnicodeSet *nx;
3178
3179 if(!_haveData(*pErrorCode)) {
3180 return 0;
3181 }
3182
3183 nx=getNX(options, *pErrorCode);
3184 if(U_FAILURE(*pErrorCode)) {
3185 return 0;
3186 }
3187
3188 return unorm_internalNormalize(dest, destCapacity,
3189 src, srcLength,
3190 mode, nx,
3191 pErrorCode);
3192}
3193
3194/** Public API for normalizing. */
3195U_CAPI int32_t U_EXPORT2
3196unorm_normalize(const UChar *src, int32_t srcLength,
3197 UNormalizationMode mode, int32_t options,
3198 UChar *dest, int32_t destCapacity,
3199 UErrorCode *pErrorCode) {
3200 /* check argument values */
3201 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
3202 return 0;
3203 }
3204
3205 if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
3206 src==NULL || srcLength<-1
3207 ) {
3208 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3209 return 0;
3210 }
3211
3212 /* check for overlapping src and destination */
3213 if( dest!=NULL &&
3214 ((src>=dest && src<(dest+destCapacity)) ||
3215 (srcLength>0 && dest>=src && dest<(src+srcLength)))
3216 ) {
3217 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3218 return 0;
3219 }
3220
3221 return unorm_internalNormalize(dest, destCapacity,
3222 src, srcLength,
3223 mode, options,
3224 pErrorCode);
3225}
3226
3227
3228/* iteration functions ------------------------------------------------------ */
3229
3230/*
3231 * These iteration functions are the core implementations of the
3232 * Normalizer class iteration API.
3233 * They read from a UCharIterator into their own buffer
3234 * and normalize into the Normalizer iteration buffer.
3235 * Normalizer itself then iterates over its buffer until that needs to be
3236 * filled again.
3237 */
3238
3239/*
3240 * ### TODO:
3241 * Now that UCharIterator.next/previous return (int32_t)-1 not (UChar)0xffff
3242 * if iteration bounds are reached,
3243 * try to not call hasNext/hasPrevious and instead check for >=0.
3244 */
3245
3246/* backward iteration ------------------------------------------------------- */
3247
3248/*
3249 * read backwards and get norm32
3250 * return 0 if the character is <minC
3251 * if c2!=0 then (c2, c) is a surrogate pair (reversed - c2 is first surrogate but read second!)
3252 */
3253static inline uint32_t
3254_getPrevNorm32(UCharIterator &src, uint32_t minC, uint32_t mask, UChar &c, UChar &c2) {
3255 uint32_t norm32;
3256
3257 /* need src.hasPrevious() */
3258 c=(UChar)src.previous(&src);
3259 c2=0;
3260
3261 /* check for a surrogate before getting norm32 to see if we need to predecrement further */
3262 if(c<minC) {
3263 return 0;
3264 } else if(!UTF_IS_SURROGATE(c)) {
3265 return _getNorm32(c);
3266 } else if(UTF_IS_SURROGATE_FIRST(c) || !src.hasPrevious(&src)) {
3267 /* unpaired surrogate */
3268 return 0;
3269 } else if(UTF_IS_FIRST_SURROGATE(c2=(UChar)src.previous(&src))) {
3270 norm32=_getNorm32(c2);
3271 if((norm32&mask)==0) {
3272 /* all surrogate pairs with this lead surrogate have irrelevant data */
3273 return 0;
3274 } else {
3275 /* norm32 must be a surrogate special */
3276 return _getNorm32FromSurrogatePair(norm32, c);
3277 }
3278 } else {
3279 /* unpaired second surrogate, undo the c2=src.previous() movement */
3280 src.move(&src, 1, UITER_CURRENT);
3281 c2=0;
3282 return 0;
3283 }
3284}
3285
3286/*
3287 * read backwards and check if the character is a previous-iteration boundary
3288 * if c2!=0 then (c2, c) is a surrogate pair (reversed - c2 is first surrogate but read second!)
3289 */
3290typedef UBool
3291IsPrevBoundaryFn(UCharIterator &src, uint32_t minC, uint32_t mask, UChar &c, UChar &c2);
3292
3293/*
3294 * for NF*D:
3295 * read backwards and check if the lead combining class is 0
3296 * if c2!=0 then (c2, c) is a surrogate pair (reversed - c2 is first surrogate but read second!)
3297 */
3298static UBool
3299_isPrevNFDSafe(UCharIterator &src, uint32_t minC, uint32_t ccOrQCMask, UChar &c, UChar &c2) {
3300 return _isNFDSafe(_getPrevNorm32(src, minC, ccOrQCMask, c, c2), ccOrQCMask, ccOrQCMask&_NORM_QC_MASK);
3301}
3302
3303/*
3304 * read backwards and check if the character is (or its decomposition begins with)
3305 * a "true starter" (cc==0 and NF*C_YES)
3306 * if c2!=0 then (c2, c) is a surrogate pair (reversed - c2 is first surrogate but read second!)
3307 */
3308static UBool
3309_isPrevTrueStarter(UCharIterator &src, uint32_t minC, uint32_t ccOrQCMask, UChar &c, UChar &c2) {
3310 uint32_t norm32, decompQCMask;
3311
3312 decompQCMask=(ccOrQCMask<<2)&0xf; /* decomposition quick check mask */
3313 norm32=_getPrevNorm32(src, minC, ccOrQCMask|decompQCMask, c, c2);
3314 return _isTrueStarter(norm32, ccOrQCMask, decompQCMask);
3315}
3316
3317static int32_t
3318_findPreviousIterationBoundary(UCharIterator &src,
3319 IsPrevBoundaryFn *isPrevBoundary, uint32_t minC, uint32_t mask,
3320 UChar *&buffer, int32_t &bufferCapacity,
3321 int32_t &startIndex,
3322 UErrorCode *pErrorCode) {
3323 UChar *stackBuffer;
3324 UChar c, c2;
3325 UBool isBoundary;
3326
3327 /* initialize */
3328 stackBuffer=buffer;
3329 startIndex=bufferCapacity; /* fill the buffer from the end backwards */
3330
3331 while(src.hasPrevious(&src)) {
3332 isBoundary=isPrevBoundary(src, minC, mask, c, c2);
3333
3334 /* always write this character to the front of the buffer */
3335 /* make sure there is enough space in the buffer */
3336 if(startIndex < (c2==0 ? 1 : 2)) {
3337 int32_t bufferLength=bufferCapacity;
3338
3339 if(!u_growBufferFromStatic(stackBuffer, &buffer, &bufferCapacity, 2*bufferCapacity, bufferLength)) {
3340 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
3341 src.move(&src, 0, UITER_START);
3342 return 0;
3343 }
3344
3345 /* move the current buffer contents up */
3346 uprv_memmove(buffer+(bufferCapacity-bufferLength), buffer, bufferLength*U_SIZEOF_UCHAR);
3347 startIndex+=bufferCapacity-bufferLength;
3348 }
3349
3350 buffer[--startIndex]=c;
3351 if(c2!=0) {
3352 buffer[--startIndex]=c2;
3353 }
3354
3355 /* stop if this just-copied character is a boundary */
3356 if(isBoundary) {
3357 break;
3358 }
3359 }
3360
3361 /* return the length of the buffer contents */
3362 return bufferCapacity-startIndex;
3363}
3364
3365U_CAPI int32_t U_EXPORT2
3366unorm_previous(UCharIterator *src,
3367 UChar *dest, int32_t destCapacity,
3368 UNormalizationMode mode, int32_t options,
3369 UBool doNormalize, UBool *pNeededToNormalize,
3370 UErrorCode *pErrorCode) {
3371 UChar stackBuffer[100];
3372 UChar *buffer=NULL;
3373 IsPrevBoundaryFn *isPreviousBoundary=NULL;
3374 uint32_t mask=0;
3375 int32_t startIndex=0, bufferLength=0, bufferCapacity=0, destLength=0;
3376 int32_t c=0, c2=0;
3377 UChar minC=0;
3378
3379 /* check argument values */
3380 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
3381 return 0;
3382 }
3383
3384 if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
3385 src==NULL
3386 ) {
3387 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3388 return 0;
3389 }
3390
3391 if(!_haveData(*pErrorCode)) {
3392 return 0;
3393 }
3394
3395 if(pNeededToNormalize!=NULL) {
3396 *pNeededToNormalize=FALSE;
3397 }
3398
3399 switch(mode) {
3400 case UNORM_NFD:
3401 case UNORM_FCD:
3402 isPreviousBoundary=_isPrevNFDSafe;
3403 minC=_NORM_MIN_WITH_LEAD_CC;
3404 mask=_NORM_CC_MASK|_NORM_QC_NFD;
3405 break;
3406 case UNORM_NFKD:
3407 isPreviousBoundary=_isPrevNFDSafe;
3408 minC=_NORM_MIN_WITH_LEAD_CC;
3409 mask=_NORM_CC_MASK|_NORM_QC_NFKD;
3410 break;
3411 case UNORM_NFC:
3412 isPreviousBoundary=_isPrevTrueStarter;
3413 minC=(UChar)indexes[_NORM_INDEX_MIN_NFC_NO_MAYBE];
3414 mask=_NORM_CC_MASK|_NORM_QC_NFC;
3415 break;
3416 case UNORM_NFKC:
3417 isPreviousBoundary=_isPrevTrueStarter;
3418 minC=(UChar)indexes[_NORM_INDEX_MIN_NFKC_NO_MAYBE];
3419 mask=_NORM_CC_MASK|_NORM_QC_NFKC;
3420 break;
3421 case UNORM_NONE:
3422 destLength=0;
3423 if((c=src->previous(src))>=0) {
3424 destLength=1;
3425 if(UTF_IS_TRAIL(c) && (c2=src->previous(src))>=0) {
3426 if(UTF_IS_LEAD(c2)) {
3427 if(destCapacity>=2) {
3428 dest[1]=(UChar)c; /* trail surrogate */
3429 destLength=2;
3430 }
3431 c=c2; /* lead surrogate to be written below */
3432 } else {
3433 src->move(src, 1, UITER_CURRENT);
3434 }
3435 }
3436
3437 if(destCapacity>0) {
3438 dest[0]=(UChar)c;
3439 }
3440 }
3441 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
3442 default:
3443 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3444 return 0;
3445 }
3446
3447 buffer=stackBuffer;
3448 bufferCapacity=(int32_t)(sizeof(stackBuffer)/U_SIZEOF_UCHAR);
3449 bufferLength=_findPreviousIterationBoundary(*src,
3450 isPreviousBoundary, minC, mask,
3451 buffer, bufferCapacity,
3452 startIndex,
3453 pErrorCode);
3454 if(bufferLength>0) {
3455 if(doNormalize) {
3456 destLength=unorm_internalNormalize(dest, destCapacity,
3457 buffer+startIndex, bufferLength,
3458 mode, options,
3459 pErrorCode);
3460 if(pNeededToNormalize!=0 && U_SUCCESS(*pErrorCode)) {
3461 *pNeededToNormalize=
3462 (UBool)(destLength!=bufferLength ||
3463 0!=uprv_memcmp(dest, buffer+startIndex, destLength*U_SIZEOF_UCHAR));
3464 }
3465 } else {
3466 /* just copy the source characters */
3467 if(destCapacity>0) {
3468 uprv_memcpy(dest, buffer+startIndex, uprv_min(bufferLength, destCapacity)*U_SIZEOF_UCHAR);
3469 }
3470 destLength=u_terminateUChars(dest, destCapacity, bufferLength, pErrorCode);
3471 }
3472 } else {
3473 destLength=u_terminateUChars(dest, destCapacity, 0, pErrorCode);
3474 }
3475
3476 /* cleanup */
3477 if(buffer!=stackBuffer) {
3478 uprv_free(buffer);
3479 }
3480
3481 return destLength;
3482}
3483
3484/* forward iteration -------------------------------------------------------- */
3485
3486/*
3487 * read forward and get norm32
3488 * return 0 if the character is <minC
3489 * if c2!=0 then (c2, c) is a surrogate pair
3490 * always reads complete characters
3491 */
3492static inline uint32_t
3493_getNextNorm32(UCharIterator &src, uint32_t minC, uint32_t mask, UChar &c, UChar &c2) {
3494 uint32_t norm32;
3495
3496 /* need src.hasNext() to be true */
3497 c=(UChar)src.next(&src);
3498 c2=0;
3499
3500 if(c<minC) {
3501 return 0;
3502 }
3503
3504 norm32=_getNorm32(c);
3505 if(UTF_IS_FIRST_SURROGATE(c)) {
3506 if(src.hasNext(&src) && UTF_IS_SECOND_SURROGATE(c2=(UChar)src.current(&src))) {
3507 src.move(&src, 1, UITER_CURRENT); /* skip the c2 surrogate */
3508 if((norm32&mask)==0) {
3509 /* irrelevant data */
3510 return 0;
3511 } else {
3512 /* norm32 must be a surrogate special */
3513 return _getNorm32FromSurrogatePair(norm32, c2);
3514 }
3515 } else {
3516 /* unmatched surrogate */
3517 c2=0;
3518 return 0;
3519 }
3520 }
3521 return norm32;
3522}
3523
3524/*
3525 * read forward and check if the character is a next-iteration boundary
3526 * if c2!=0 then (c, c2) is a surrogate pair
3527 */
3528typedef UBool
3529IsNextBoundaryFn(UCharIterator &src, uint32_t minC, uint32_t mask, UChar &c, UChar &c2);
3530
3531/*
3532 * for NF*D:
3533 * read forward and check if the lead combining class is 0
3534 * if c2!=0 then (c, c2) is a surrogate pair
3535 */
3536static UBool
3537_isNextNFDSafe(UCharIterator &src, uint32_t minC, uint32_t ccOrQCMask, UChar &c, UChar &c2) {
3538 return _isNFDSafe(_getNextNorm32(src, minC, ccOrQCMask, c, c2), ccOrQCMask, ccOrQCMask&_NORM_QC_MASK);
3539}
3540
3541/*
3542 * for NF*C:
3543 * read forward and check if the character is (or its decomposition begins with)
3544 * a "true starter" (cc==0 and NF*C_YES)
3545 * if c2!=0 then (c, c2) is a surrogate pair
3546 */
3547static UBool
3548_isNextTrueStarter(UCharIterator &src, uint32_t minC, uint32_t ccOrQCMask, UChar &c, UChar &c2) {
3549 uint32_t norm32, decompQCMask;
3550
3551 decompQCMask=(ccOrQCMask<<2)&0xf; /* decomposition quick check mask */
3552 norm32=_getNextNorm32(src, minC, ccOrQCMask|decompQCMask, c, c2);
3553 return _isTrueStarter(norm32, ccOrQCMask, decompQCMask);
3554}
3555
3556static int32_t
3557_findNextIterationBoundary(UCharIterator &src,
3558 IsNextBoundaryFn *isNextBoundary, uint32_t minC, uint32_t mask,
3559 UChar *&buffer, int32_t &bufferCapacity,
3560 UErrorCode *pErrorCode) {
3561 UChar *stackBuffer;
3562 int32_t bufferIndex;
3563 UChar c, c2;
3564
3565 if(!src.hasNext(&src)) {
3566 return 0;
3567 }
3568
3569 /* initialize */
3570 stackBuffer=buffer;
3571
3572 /* get one character and ignore its properties */
3573 buffer[0]=c=(UChar)src.next(&src);
3574 bufferIndex=1;
3575 if(UTF_IS_FIRST_SURROGATE(c) && src.hasNext(&src)) {
3576 if(UTF_IS_SECOND_SURROGATE(c2=(UChar)src.next(&src))) {
3577 buffer[bufferIndex++]=c2;
3578 } else {
3579 src.move(&src, -1, UITER_CURRENT); /* back out the non-trail-surrogate */
3580 }
3581 }
3582
3583 /* get all following characters until we see a boundary */
3584 /* checking hasNext() instead of c!=DONE on the off-chance that U+ffff is part of the string */
3585 while(src.hasNext(&src)) {
3586 if(isNextBoundary(src, minC, mask, c, c2)) {
3587 /* back out the latest movement to stop at the boundary */
3588 src.move(&src, c2==0 ? -1 : -2, UITER_CURRENT);
3589 break;
3590 } else {
3591 if(bufferIndex+(c2==0 ? 1 : 2)<=bufferCapacity ||
3592 /* attempt to grow the buffer */
3593 u_growBufferFromStatic(stackBuffer, &buffer, &bufferCapacity,
3594 2*bufferCapacity,
3595 bufferIndex)
3596 ) {
3597 buffer[bufferIndex++]=c;
3598 if(c2!=0) {
3599 buffer[bufferIndex++]=c2;
3600 }
3601 } else {
3602 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
3603 src.move(&src, 0, UITER_LIMIT);
3604 return 0;
3605 }
3606 }
3607 }
3608
3609 /* return the length of the buffer contents */
3610 return bufferIndex;
3611}
3612
3613U_CAPI int32_t U_EXPORT2
3614unorm_next(UCharIterator *src,
3615 UChar *dest, int32_t destCapacity,
3616 UNormalizationMode mode, int32_t options,
3617 UBool doNormalize, UBool *pNeededToNormalize,
3618 UErrorCode *pErrorCode) {
3619 UChar stackBuffer[100];
3620 UChar *buffer;
3621 IsNextBoundaryFn *isNextBoundary;
3622 uint32_t mask;
3623 int32_t bufferLength, bufferCapacity, destLength;
3624 int32_t c, c2;
3625 UChar minC;
3626
3627 /* check argument values */
3628 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
3629 return 0;
3630 }
3631
3632 if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
3633 src==NULL
3634 ) {
3635 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3636 return 0;
3637 }
3638
3639 if(!_haveData(*pErrorCode)) {
3640 return 0;
3641 }
3642
3643 if(pNeededToNormalize!=NULL) {
3644 *pNeededToNormalize=FALSE;
3645 }
3646
3647 switch(mode) {
3648 case UNORM_NFD:
3649 case UNORM_FCD:
3650 isNextBoundary=_isNextNFDSafe;
3651 minC=_NORM_MIN_WITH_LEAD_CC;
3652 mask=_NORM_CC_MASK|_NORM_QC_NFD;
3653 break;
3654 case UNORM_NFKD:
3655 isNextBoundary=_isNextNFDSafe;
3656 minC=_NORM_MIN_WITH_LEAD_CC;
3657 mask=_NORM_CC_MASK|_NORM_QC_NFKD;
3658 break;
3659 case UNORM_NFC:
3660 isNextBoundary=_isNextTrueStarter;
3661 minC=(UChar)indexes[_NORM_INDEX_MIN_NFC_NO_MAYBE];
3662 mask=_NORM_CC_MASK|_NORM_QC_NFC;
3663 break;
3664 case UNORM_NFKC:
3665 isNextBoundary=_isNextTrueStarter;
3666 minC=(UChar)indexes[_NORM_INDEX_MIN_NFKC_NO_MAYBE];
3667 mask=_NORM_CC_MASK|_NORM_QC_NFKC;
3668 break;
3669 case UNORM_NONE:
3670 destLength=0;
3671 if((c=src->next(src))>=0) {
3672 destLength=1;
3673 if(UTF_IS_LEAD(c) && (c2=src->next(src))>=0) {
3674 if(UTF_IS_TRAIL(c2)) {
3675 if(destCapacity>=2) {
3676 dest[1]=(UChar)c2; /* trail surrogate */
3677 destLength=2;
3678 }
3679 /* lead surrogate to be written below */
3680 } else {
3681 src->move(src, -1, UITER_CURRENT);
3682 }
3683 }
3684
3685 if(destCapacity>0) {
3686 dest[0]=(UChar)c;
3687 }
3688 }
3689 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
3690 default:
3691 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3692 return 0;
3693 }
3694
3695 buffer=stackBuffer;
3696 bufferCapacity=(int32_t)(sizeof(stackBuffer)/U_SIZEOF_UCHAR);
3697 bufferLength=_findNextIterationBoundary(*src,
3698 isNextBoundary, minC, mask,
3699 buffer, bufferCapacity,
3700 pErrorCode);
3701 if(bufferLength>0) {
3702 if(doNormalize) {
3703 destLength=unorm_internalNormalize(dest, destCapacity,
3704 buffer, bufferLength,
3705 mode, options,
3706 pErrorCode);
3707 if(pNeededToNormalize!=0 && U_SUCCESS(*pErrorCode)) {
3708 *pNeededToNormalize=
3709 (UBool)(destLength!=bufferLength ||
3710 0!=uprv_memcmp(dest, buffer, destLength*U_SIZEOF_UCHAR));
3711 }
3712 } else {
3713 /* just copy the source characters */
3714 if(destCapacity>0) {
3715 uprv_memcpy(dest, buffer, uprv_min(bufferLength, destCapacity)*U_SIZEOF_UCHAR);
3716 }
3717 destLength=u_terminateUChars(dest, destCapacity, bufferLength, pErrorCode);
3718 }
3719 } else {
3720 destLength=u_terminateUChars(dest, destCapacity, 0, pErrorCode);
3721 }
3722
3723 /* cleanup */
3724 if(buffer!=stackBuffer) {
3725 uprv_free(buffer);
3726 }
3727
3728 return destLength;
3729}
3730
3731/*
3732 * ### TODO: check if NF*D and FCD iteration finds optimal boundaries
3733 * and if not, how hard it would be to improve it.
3734 * For example, see _findSafeFCD().
3735 */
3736
3737/* Concatenation of normalized strings -------------------------------------- */
3738
3739U_CAPI int32_t U_EXPORT2
3740unorm_concatenate(const UChar *left, int32_t leftLength,
3741 const UChar *right, int32_t rightLength,
3742 UChar *dest, int32_t destCapacity,
3743 UNormalizationMode mode, int32_t options,
3744 UErrorCode *pErrorCode) {
3745 UChar stackBuffer[100];
3746 UChar *buffer;
3747 int32_t bufferLength, bufferCapacity;
3748
3749 UCharIterator iter;
3750 int32_t leftBoundary, rightBoundary, destLength;
3751
3752 /* check argument values */
3753 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
3754 return 0;
3755 }
3756
3757 if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
3758 left==NULL || leftLength<-1 ||
3759 right==NULL || rightLength<-1
3760 ) {
3761 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3762 return 0;
3763 }
3764
3765 /* check for overlapping right and destination */
3766 if( dest!=NULL &&
3767 ((right>=dest && right<(dest+destCapacity)) ||
3768 (rightLength>0 && dest>=right && dest<(right+rightLength)))
3769 ) {
3770 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
3771 return 0;
3772 }
3773
3774 /* allow left==dest */
3775
3776 /* set up intermediate buffer */
3777 buffer=stackBuffer;
3778 bufferCapacity=(int32_t)(sizeof(stackBuffer)/U_SIZEOF_UCHAR);
3779
3780 /*
3781 * Input: left[0..leftLength[ + right[0..rightLength[
3782 *
3783 * Find normalization-safe boundaries leftBoundary and rightBoundary
3784 * and copy the end parts together:
3785 * buffer=left[leftBoundary..leftLength[ + right[0..rightBoundary[
3786 *
3787 * dest=left[0..leftBoundary[ +
3788 * normalize(buffer) +
3789 * right[rightBoundary..rightLength[
3790 */
3791
3792 /*
3793 * find a normalization boundary at the end of the left string
3794 * and copy the end part into the buffer
3795 */
3796 uiter_setString(&iter, left, leftLength);
3797 iter.index=leftLength=iter.length; /* end of left string */
3798
3799 bufferLength=unorm_previous(&iter, buffer, bufferCapacity,
3800 mode, options,
3801 FALSE, NULL,
3802 pErrorCode);
3803 leftBoundary=iter.index;
3804 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
3805 *pErrorCode=U_ZERO_ERROR;
3806 if(!u_growBufferFromStatic(stackBuffer, &buffer, &bufferCapacity, 2*bufferLength, 0)) {
3807 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
3808 /* dont need to cleanup here since
3809 * u_growBufferFromStatic frees buffer if(buffer!=stackBuffer)
3810 */
3811 return 0;
3812 }
3813
3814 /* just copy from the left string: we know the boundary already */
3815 uprv_memcpy(buffer, left+leftBoundary, bufferLength*U_SIZEOF_UCHAR);
3816 }
3817
3818 /*
3819 * find a normalization boundary at the beginning of the right string
3820 * and concatenate the beginning part to the buffer
3821 */
3822 uiter_setString(&iter, right, rightLength);
3823 rightLength=iter.length; /* in case it was -1 */
3824
3825 rightBoundary=unorm_next(&iter, buffer+bufferLength, bufferCapacity-bufferLength,
3826 mode, options,
3827 FALSE, NULL,
3828 pErrorCode);
3829 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
3830 *pErrorCode=U_ZERO_ERROR;
3831 if(!u_growBufferFromStatic(stackBuffer, &buffer, &bufferCapacity, bufferLength+rightBoundary, 0)) {
3832 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
3833 /* dont need to cleanup here since
3834 * u_growBufferFromStatic frees buffer if(buffer!=stackBuffer)
3835 */
3836 return 0;
3837 }
3838
3839 /* just copy from the right string: we know the boundary already */
3840 uprv_memcpy(buffer+bufferLength, right, rightBoundary*U_SIZEOF_UCHAR);
3841 }
3842
3843 bufferLength+=rightBoundary;
3844
3845 /* copy left[0..leftBoundary[ to dest */
3846 if(left!=dest && leftBoundary>0 && destCapacity>0) {
3847 uprv_memcpy(dest, left, uprv_min(leftBoundary, destCapacity)*U_SIZEOF_UCHAR);
3848 }
3849 destLength=leftBoundary;
3850
3851 /* concatenate the normalization of the buffer to dest */
3852 if(destCapacity>destLength) {
3853 destLength+=unorm_internalNormalize(dest+destLength, destCapacity-destLength,
3854 buffer, bufferLength,
3855 mode, options,
3856 pErrorCode);
3857 } else {
3858 destLength+=unorm_internalNormalize(NULL, 0,
3859 buffer, bufferLength,
3860 mode, options,
3861 pErrorCode);
3862 }
3863 /*
3864 * only errorCode that is expected is a U_BUFFER_OVERFLOW_ERROR
3865 * so we dont check for the error code here..just let it pass through
3866 */
3867 /* concatenate right[rightBoundary..rightLength[ to dest */
3868 right+=rightBoundary;
3869 rightLength-=rightBoundary;
3870 if(rightLength>0 && destCapacity>destLength) {
3871 uprv_memcpy(dest+destLength, right, uprv_min(rightLength, destCapacity-destLength)*U_SIZEOF_UCHAR);
3872 }
3873 destLength+=rightLength;
3874
3875 /* cleanup */
3876 if(buffer!=stackBuffer) {
3877 uprv_free(buffer);
3878 }
3879
3880 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
3881}
3882
3883/* compare canonically equivalent ------------------------------------------- */
3884
3885#else
3886
3887/*
3888 * Normalization is not built into the ICU library, but case-insensitive
3889 * comparisons are possible using unorm_cmpEquivFold().
3890 * The following simply disables the decomposition part.
3891 */
3892
3893static inline UBool
3894_haveData(UErrorCode &errorCode) {
3895 if(U_SUCCESS(errorCode)) {
3896 errorCode=U_INTERNAL_PROGRAM_ERROR;
3897 }
3898 return FALSE;
3899}
3900
3901static inline const UChar *
3902_decompose(UChar32 /*c*/, UChar /*buffer*/[4], int32_t &/*length*/) {
3903 return NULL;
3904}
3905
3906#endif /* #if !UCONFIG_NO_NORMALIZATION */
3907
3908/*
3909 * Compare two strings for canonical equivalence.
3910 * Further options include case-insensitive comparison and
3911 * code point order (as opposed to code unit order).
3912 *
3913 * In this function, canonical equivalence is optional as well.
3914 * If canonical equivalence is tested, then both strings must fulfill
3915 * the FCD check.
3916 *
3917 * Semantically, this is equivalent to
3918 * strcmp[CodePointOrder](NFD(foldCase(s1)), NFD(foldCase(s2)))
3919 * where code point order, NFD and foldCase are all optional.
3920 *
3921 * String comparisons almost always yield results before processing both strings
3922 * completely.
3923 * They are generally more efficient working incrementally instead of
3924 * performing the sub-processing (strlen, normalization, case-folding)
3925 * on the entire strings first.
3926 *
3927 * It is also unnecessary to not normalize identical characters.
3928 *
3929 * This function works in principle as follows:
3930 *
3931 * loop {
3932 * get one code unit c1 from s1 (-1 if end of source)
3933 * get one code unit c2 from s2 (-1 if end of source)
3934 *
3935 * if(either string finished) {
3936 * return result;
3937 * }
3938 * if(c1==c2) {
3939 * continue;
3940 * }
3941 *
3942 * // c1!=c2
3943 * try to decompose/case-fold c1/c2, and continue if one does;
3944 *
3945 * // still c1!=c2 and neither decomposes/case-folds, return result
3946 * return c1-c2;
3947 * }
3948 *
3949 * When a character decomposes, then the pointer for that source changes to
3950 * the decomposition, pushing the previous pointer onto a stack.
3951 * When the end of the decomposition is reached, then the code unit reader
3952 * pops the previous source from the stack.
3953 * (Same for case-folding.)
3954 *
3955 * This is complicated further by operating on variable-width UTF-16.
3956 * The top part of the loop works on code units, while lookups for decomposition
3957 * and case-folding need code points.
3958 * Code points are assembled after the equality/end-of-source part.
3959 * The source pointer is only advanced beyond all code units when the code point
3960 * actually decomposes/case-folds.
3961 *
3962 * If we were on a trail surrogate unit when assembling a code point,
3963 * and the code point decomposes/case-folds, then the decomposition/folding
3964 * result must be compared with the part of the other string that corresponds to
3965 * this string's lead surrogate.
3966 * Since we only assemble a code point when hitting a trail unit when the
3967 * preceding lead units were identical, we back up the other string by one unit
3968 * in such a case.
3969 *
3970 * The optional code point order comparison at the end works with
3971 * the same fix-up as the other code point order comparison functions.
3972 * See ustring.c and the comment near the end of this function.
3973 *
3974 * Assumption: A decomposition or case-folding result string never contains
3975 * a single surrogate. This is a safe assumption in the Unicode Standard.
3976 * Therefore, we do not need to check for surrogate pairs across
3977 * decomposition/case-folding boundaries.
3978 *
3979 * Further assumptions (see verifications tstnorm.cpp):
3980 * The API function checks for FCD first, while the core function
3981 * first case-folds and then decomposes. This requires that case-folding does not
3982 * un-FCD any strings.
3983 *
3984 * The API function may also NFD the input and turn off decomposition.
3985 * This requires that case-folding does not un-NFD strings either.
3986 *
3987 * TODO If any of the above two assumptions is violated,
3988 * then this entire code must be re-thought.
3989 * If this happens, then a simple solution is to case-fold both strings up front
3990 * and to turn off UNORM_INPUT_IS_FCD.
3991 * We already do this when not both strings are in FCD because makeFCD
3992 * would be a partial NFD before the case folding, which does not work.
3993 * Note that all of this is only a problem when case-folding _and_
3994 * canonical equivalence come together.
3995 *
3996 * This function could be moved to a different source file, at increased cost
3997 * for calling the decomposition access function.
3998 */
3999
4000// stack element for previous-level source/decomposition pointers
4001struct CmpEquivLevel {
4002 const UChar *start, *s, *limit;
4003};
4004typedef struct CmpEquivLevel CmpEquivLevel;
4005
4006// internal function
4007U_CAPI int32_t U_EXPORT2
4008unorm_cmpEquivFold(const UChar *s1, int32_t length1,
4009 const UChar *s2, int32_t length2,
4010 uint32_t options,
4011 UErrorCode *pErrorCode) {
4012 // current-level start/limit - s1/s2 as current
4013 const UChar *start1, *start2, *limit1, *limit2;
4014
4015 // decomposition variables
4016 const UChar *p;
4017 int32_t length;
4018
4019 // stacks of previous-level start/current/limit
4020 CmpEquivLevel stack1[2], stack2[2];
4021
4022 // decomposition buffers for Hangul
4023 UChar decomp1[4], decomp2[4];
4024
4025 // case folding buffers, only use current-level start/limit
4026 UChar fold1[32], fold2[32];
4027
4028 // track which is the current level per string
4029 int32_t level1, level2;
4030
4031 // current code units, and code points for lookups
4032 int32_t c1, c2, cp1, cp2;
4033
4034 // no argument error checking because this itself is not an API
4035
4036 // assume that at least one of the options _COMPARE_EQUIV and U_COMPARE_IGNORE_CASE is set
4037 // otherwise this function must behave exactly as uprv_strCompare()
4038 // not checking for that here makes testing this function easier
4039
4040 // normalization/properties data loaded?
4041 if( ((options&_COMPARE_EQUIV)!=0 && !_haveData(*pErrorCode)) ||
4042 ((options&U_COMPARE_IGNORE_CASE)!=0 && !uprv_haveProperties(pErrorCode))
4043 ) {
4044 return 0;
4045 }
4046
4047 // initialize
4048 start1=s1;
4049 if(length1==-1) {
4050 limit1=NULL;
4051 } else {
4052 limit1=s1+length1;
4053 }
4054
4055 start2=s2;
4056 if(length2==-1) {
4057 limit2=NULL;
4058 } else {
4059 limit2=s2+length2;
4060 }
4061
4062 level1=level2=0;
4063 c1=c2=-1;
4064
4065 // comparison loop
4066 for(;;) {
4067 // here a code unit value of -1 means "get another code unit"
4068 // below it will mean "this source is finished"
4069
4070 if(c1<0) {
4071 // get next code unit from string 1, post-increment
4072 for(;;) {
4073 if(s1==limit1 || ((c1=*s1)==0 && (limit1==NULL || (options&_STRNCMP_STYLE)))) {
4074 if(level1==0) {
4075 c1=-1;
4076 break;
4077 }
4078 } else {
4079 ++s1;
4080 break;
4081 }
4082
4083 // reached end of level buffer, pop one level
4084 do {
4085 --level1;
4086 start1=stack1[level1].start;
4087 } while(start1==NULL);
4088 s1=stack1[level1].s;
4089 limit1=stack1[level1].limit;
4090 }
4091 }
4092
4093 if(c2<0) {
4094 // get next code unit from string 2, post-increment
4095 for(;;) {
4096 if(s2==limit2 || ((c2=*s2)==0 && (limit2==NULL || (options&_STRNCMP_STYLE)))) {
4097 if(level2==0) {
4098 c2=-1;
4099 break;
4100 }
4101 } else {
4102 ++s2;
4103 break;
4104 }
4105
4106 // reached end of level buffer, pop one level
4107 do {
4108 --level2;
4109 start2=stack2[level2].start;
4110 } while(start2==NULL);
4111 s2=stack2[level2].s;
4112 limit2=stack2[level2].limit;
4113 }
4114 }
4115
4116 // compare c1 and c2
4117 // either variable c1, c2 is -1 only if the corresponding string is finished
4118 if(c1==c2) {
4119 if(c1<0) {
4120 return 0; // c1==c2==-1 indicating end of strings
4121 }
4122 c1=c2=-1; // make us fetch new code units
4123 continue;
4124 } else if(c1<0) {
4125 return -1; // string 1 ends before string 2
4126 } else if(c2<0) {
4127 return 1; // string 2 ends before string 1
4128 }
4129 // c1!=c2 && c1>=0 && c2>=0
4130
4131 // get complete code points for c1, c2 for lookups if either is a surrogate
4132 cp1=c1;
4133 if(UTF_IS_SURROGATE(c1)) {
4134 UChar c;
4135
4136 if(UTF_IS_SURROGATE_FIRST(c1)) {
4137 if(s1!=limit1 && UTF_IS_TRAIL(c=*s1)) {
4138 // advance ++s1; only below if cp1 decomposes/case-folds
4139 cp1=UTF16_GET_PAIR_VALUE(c1, c);
4140 }
4141 } else /* isTrail(c1) */ {
4142 if(start1<=(s1-2) && UTF_IS_LEAD(c=*(s1-2))) {
4143 cp1=UTF16_GET_PAIR_VALUE(c, c1);
4144 }
4145 }
4146 }
4147
4148 cp2=c2;
4149 if(UTF_IS_SURROGATE(c2)) {
4150 UChar c;
4151
4152 if(UTF_IS_SURROGATE_FIRST(c2)) {
4153 if(s2!=limit2 && UTF_IS_TRAIL(c=*s2)) {
4154 // advance ++s2; only below if cp2 decomposes/case-folds
4155 cp2=UTF16_GET_PAIR_VALUE(c2, c);
4156 }
4157 } else /* isTrail(c2) */ {
4158 if(start2<=(s2-2) && UTF_IS_LEAD(c=*(s2-2))) {
4159 cp2=UTF16_GET_PAIR_VALUE(c, c2);
4160 }
4161 }
4162 }
4163
4164 // go down one level for each string
4165 // continue with the main loop as soon as there is a real change
4166
4167 if( level1==0 && (options&U_COMPARE_IGNORE_CASE) &&
4168 (length=u_internalFoldCase((UChar32)cp1, fold1, 32, options))>=0
4169 ) {
4170 // cp1 case-folds to fold1[length]
4171 if(UTF_IS_SURROGATE(c1)) {
4172 if(UTF_IS_SURROGATE_FIRST(c1)) {
4173 // advance beyond source surrogate pair if it case-folds
4174 ++s1;
4175 } else /* isTrail(c1) */ {
4176 // we got a supplementary code point when hitting its trail surrogate,
4177 // therefore the lead surrogate must have been the same as in the other string;
4178 // compare this decomposition with the lead surrogate in the other string
4179 // remember that this simulates bulk text replacement:
4180 // the decomposition would replace the entire code point
4181 --s2;
4182 c2=*(s2-1);
4183 }
4184 }
4185
4186 // push current level pointers
4187 stack1[0].start=start1;
4188 stack1[0].s=s1;
4189 stack1[0].limit=limit1;
4190 ++level1;
4191
4192 // set next level pointers to case folding
4193 start1=s1=fold1;
4194 limit1=fold1+length;
4195
4196 // get ready to read from decomposition, continue with loop
4197 c1=-1;
4198 continue;
4199 }
4200
4201 if( level2==0 && (options&U_COMPARE_IGNORE_CASE) &&
4202 (length=u_internalFoldCase((UChar32)cp2, fold2, 32, options))>=0
4203 ) {
4204 // cp2 case-folds to fold2[length]
4205 if(UTF_IS_SURROGATE(c2)) {
4206 if(UTF_IS_SURROGATE_FIRST(c2)) {
4207 // advance beyond source surrogate pair if it case-folds
4208 ++s2;
4209 } else /* isTrail(c2) */ {
4210 // we got a supplementary code point when hitting its trail surrogate,
4211 // therefore the lead surrogate must have been the same as in the other string;
4212 // compare this decomposition with the lead surrogate in the other string
4213 // remember that this simulates bulk text replacement:
4214 // the decomposition would replace the entire code point
4215 --s1;
4216 c1=*(s1-1);
4217 }
4218 }
4219
4220 // push current level pointers
4221 stack2[0].start=start2;
4222 stack2[0].s=s2;
4223 stack2[0].limit=limit2;
4224 ++level2;
4225
4226 // set next level pointers to case folding
4227 start2=s2=fold2;
4228 limit2=fold2+length;
4229
4230 // get ready to read from decomposition, continue with loop
4231 c2=-1;
4232 continue;
4233 }
4234
4235 if( level1<2 && (options&_COMPARE_EQUIV) &&
4236 0!=(p=_decompose((UChar32)cp1, decomp1, length))
4237 ) {
4238 // cp1 decomposes into p[length]
4239 if(UTF_IS_SURROGATE(c1)) {
4240 if(UTF_IS_SURROGATE_FIRST(c1)) {
4241 // advance beyond source surrogate pair if it decomposes
4242 ++s1;
4243 } else /* isTrail(c1) */ {
4244 // we got a supplementary code point when hitting its trail surrogate,
4245 // therefore the lead surrogate must have been the same as in the other string;
4246 // compare this decomposition with the lead surrogate in the other string
4247 // remember that this simulates bulk text replacement:
4248 // the decomposition would replace the entire code point
4249 --s2;
4250 c2=*(s2-1);
4251 }
4252 }
4253
4254 // push current level pointers
4255 stack1[level1].start=start1;
4256 stack1[level1].s=s1;
4257 stack1[level1].limit=limit1;
4258 ++level1;
4259
4260 // set empty intermediate level if skipped
4261 if(level1<2) {
4262 stack1[level1++].start=NULL;
4263 }
4264
4265 // set next level pointers to decomposition
4266 start1=s1=p;
4267 limit1=p+length;
4268
4269 // get ready to read from decomposition, continue with loop
4270 c1=-1;
4271 continue;
4272 }
4273
4274 if( level2<2 && (options&_COMPARE_EQUIV) &&
4275 0!=(p=_decompose((UChar32)cp2, decomp2, length))
4276 ) {
4277 // cp2 decomposes into p[length]
4278 if(UTF_IS_SURROGATE(c2)) {
4279 if(UTF_IS_SURROGATE_FIRST(c2)) {
4280 // advance beyond source surrogate pair if it decomposes
4281 ++s2;
4282 } else /* isTrail(c2) */ {
4283 // we got a supplementary code point when hitting its trail surrogate,
4284 // therefore the lead surrogate must have been the same as in the other string;
4285 // compare this decomposition with the lead surrogate in the other string
4286 // remember that this simulates bulk text replacement:
4287 // the decomposition would replace the entire code point
4288 --s1;
4289 c1=*(s1-1);
4290 }
4291 }
4292
4293 // push current level pointers
4294 stack2[level2].start=start2;
4295 stack2[level2].s=s2;
4296 stack2[level2].limit=limit2;
4297 ++level2;
4298
4299 // set empty intermediate level if skipped
4300 if(level2<2) {
4301 stack2[level2++].start=NULL;
4302 }
4303
4304 // set next level pointers to decomposition
4305 start2=s2=p;
4306 limit2=p+length;
4307
4308 // get ready to read from decomposition, continue with loop
4309 c2=-1;
4310 continue;
4311 }
4312
4313 // no decomposition/case folding, max level for both sides:
4314 // return difference result
4315
4316 // code point order comparison must not just return cp1-cp2
4317 // because when single surrogates are present then the surrogate pairs
4318 // that formed cp1 and cp2 may be from different string indexes
4319
4320 // example: { d800 d800 dc01 } vs. { d800 dc00 }, compare at second code units
4321 // c1=d800 cp1=10001 c2=dc00 cp2=10000
4322 // cp1-cp2>0 but c1-c2<0 and in fact in UTF-32 it is { d800 10001 } < { 10000 }
4323
4324 // therefore, use same fix-up as in ustring.c/uprv_strCompare()
4325 // except: uprv_strCompare() fetches c=*s while this functions fetches c=*s++
4326 // so we have slightly different pointer/start/limit comparisons here
4327
4328 if(c1>=0xd800 && c2>=0xd800 && (options&U_COMPARE_CODE_POINT_ORDER)) {
4329 /* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */
4330 if(
4331 (c1<=0xdbff && s1!=limit1 && UTF_IS_TRAIL(*s1)) ||
4332 (UTF_IS_TRAIL(c1) && start1!=(s1-1) && UTF_IS_LEAD(*(s1-2)))
4333 ) {
4334 /* part of a surrogate pair, leave >=d800 */
4335 } else {
4336 /* BMP code point - may be surrogate code point - make <d800 */
4337 c1-=0x2800;
4338 }
4339
4340 if(
4341 (c2<=0xdbff && s2!=limit2 && UTF_IS_TRAIL(*s2)) ||
4342 (UTF_IS_TRAIL(c2) && start2!=(s2-1) && UTF_IS_LEAD(*(s2-2)))
4343 ) {
4344 /* part of a surrogate pair, leave >=d800 */
4345 } else {
4346 /* BMP code point - may be surrogate code point - make <d800 */
4347 c2-=0x2800;
4348 }
4349 }
4350
4351 return c1-c2;
4352 }
4353}
4354
4355#if !UCONFIG_NO_NORMALIZATION
4356
4357U_CAPI int32_t U_EXPORT2
4358unorm_compare(const UChar *s1, int32_t length1,
4359 const UChar *s2, int32_t length2,
4360 uint32_t options,
4361 UErrorCode *pErrorCode) {
4362 UChar fcd1[300], fcd2[300];
4363 UChar *d1, *d2;
4364 const UnicodeSet *nx;
4365 UNormalizationMode mode;
4366 int32_t result;
4367
4368 /* argument checking */
4369 if(pErrorCode==0 || U_FAILURE(*pErrorCode)) {
4370 return 0;
4371 }
4372 if(s1==0 || length1<-1 || s2==0 || length2<-1) {
4373 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
4374 return 0;
4375 }
4376
4377 if(!_haveData(*pErrorCode)) {
4378 return 0;
4379 }
4380 if(!uprv_haveProperties(pErrorCode)) {
4381 return 0;
4382 }
4383
4384 nx=getNX((int32_t)(options>>UNORM_COMPARE_NORM_OPTIONS_SHIFT), *pErrorCode);
4385 if(U_FAILURE(*pErrorCode)) {
4386 return 0;
4387 }
4388
4389 d1=d2=0;
4390 options|=_COMPARE_EQUIV;
4391 result=0;
4392
4393 /*
4394 * UAX #21 Case Mappings, as fixed for Unicode version 4
4395 * (see Jitterbug 2021), defines a canonical caseless match as
4396 *
4397 * A string X is a canonical caseless match
4398 * for a string Y if and only if
4399 * NFD(toCasefold(NFD(X))) = NFD(toCasefold(NFD(Y)))
4400 *
4401 * For better performance, we check for FCD (or let the caller tell us that
4402 * both strings are in FCD) for the inner normalization.
4403 * BasicNormalizerTest::FindFoldFCDExceptions() makes sure that
4404 * case-folding preserves the FCD-ness of a string.
4405 * The outer normalization is then only performed by unorm_cmpEquivFold()
4406 * when there is a difference.
4407 *
4408 * Exception: When using the Turkic case-folding option, we do perform
4409 * full NFD first. This is because in the Turkic case precomposed characters
4410 * with 0049 capital I or 0069 small i fold differently whether they
4411 * are first decomposed or not, so an FCD check - a check only for
4412 * canonical order - is not sufficient.
4413 */
4414 if(options&U_FOLD_CASE_EXCLUDE_SPECIAL_I) {
4415 mode=UNORM_NFD;
4416 options&=~UNORM_INPUT_IS_FCD;
4417 } else {
4418 mode=UNORM_FCD;
4419 }
4420
4421 if(!(options&UNORM_INPUT_IS_FCD)) {
4422 int32_t _len1, _len2;
4423 UBool isFCD1, isFCD2;
4424
4425 // check if s1 and/or s2 fulfill the FCD conditions
4426 isFCD1= UNORM_YES==_quickCheck(s1, length1, mode, TRUE, nx, pErrorCode);
4427 isFCD2= UNORM_YES==_quickCheck(s2, length2, mode, TRUE, nx, pErrorCode);
4428 if(U_FAILURE(*pErrorCode)) {
4429 return 0;
4430 }
4431
4432 /*
4433 * ICU 2.4 had a further optimization:
4434 * If both strings were not in FCD, then they were both NFD'ed,
4435 * and the _COMPARE_EQUIV option was turned off.
4436 * It is not entirely clear that this is valid with the current
4437 * definition of the canonical caseless match.
4438 * Therefore, ICU 2.6 removes that optimization.
4439 */
4440
4441 if(!isFCD1) {
4442 _len1=unorm_internalNormalize(fcd1, LENGTHOF(fcd1),
4443 s1, length1,
4444 mode, nx,
4445 pErrorCode);
4446 if(*pErrorCode!=U_BUFFER_OVERFLOW_ERROR) {
4447 s1=fcd1;
4448 } else {
4449 d1=(UChar *)uprv_malloc(_len1*U_SIZEOF_UCHAR);
4450 if(d1==0) {
4451 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
4452 goto cleanup;
4453 }
4454
4455 *pErrorCode=U_ZERO_ERROR;
4456 _len1=unorm_internalNormalize(d1, _len1,
4457 s1, length1,
4458 mode, nx,
4459 pErrorCode);
4460 if(U_FAILURE(*pErrorCode)) {
4461 goto cleanup;
4462 }
4463
4464 s1=d1;
4465 }
4466 length1=_len1;
4467 }
4468
4469 if(!isFCD2) {
4470 _len2=unorm_internalNormalize(fcd2, LENGTHOF(fcd2),
4471 s2, length2,
4472 mode, nx,
4473 pErrorCode);
4474 if(*pErrorCode!=U_BUFFER_OVERFLOW_ERROR) {
4475 s2=fcd2;
4476 } else {
4477 d2=(UChar *)uprv_malloc(_len2*U_SIZEOF_UCHAR);
4478 if(d2==0) {
4479 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
4480 goto cleanup;
4481 }
4482
4483 *pErrorCode=U_ZERO_ERROR;
4484 _len2=unorm_internalNormalize(d2, _len2,
4485 s2, length2,
4486 mode, nx,
4487 pErrorCode);
4488 if(U_FAILURE(*pErrorCode)) {
4489 goto cleanup;
4490 }
4491
4492 s2=d2;
4493 }
4494 length2=_len2;
4495 }
4496 }
4497
4498 if(U_SUCCESS(*pErrorCode)) {
4499 result=unorm_cmpEquivFold(s1, length1, s2, length2, options, pErrorCode);
4500 }
4501
4502cleanup:
4503 if(d1!=0) {
4504 uprv_free(d1);
4505 }
4506 if(d2!=0) {
4507 uprv_free(d2);
4508 }
4509
4510 return result;
4511}
4512
4513#endif /* #if !UCONFIG_NO_NORMALIZATION */