2 *******************************************************************************
4 * Copyright (C) 2009-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: normalizer2impl.cpp
10 * tab size: 8 (not used)
13 * created on: 2009nov22
14 * created by: Markus W. Scherer
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_NORMALIZATION
21 #include "unicode/normalizer2.h"
22 #include "unicode/udata.h"
23 #include "unicode/ustring.h"
24 #include "unicode/utf16.h"
27 #include "normalizer2impl.h"
36 // ReorderingBuffer -------------------------------------------------------- ***
38 UBool
ReorderingBuffer::init(int32_t destCapacity
, UErrorCode
&errorCode
) {
39 int32_t length
=str
.length();
40 start
=str
.getBuffer(destCapacity
);
42 // getBuffer() already did str.setToBogus()
43 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
47 remainingCapacity
=str
.getCapacity()-length
;
54 // Set reorderStart after the last code point with cc<=1 if there is one.
56 while(previousCC()>1) {}
58 reorderStart
=codePointLimit
;
63 UBool
ReorderingBuffer::equals(const UChar
*otherStart
, const UChar
*otherLimit
) const {
64 int32_t length
=(int32_t)(limit
-start
);
66 length
==(int32_t)(otherLimit
-otherStart
) &&
67 0==u_memcmp(start
, otherStart
, length
);
70 UBool
ReorderingBuffer::appendSupplementary(UChar32 c
, uint8_t cc
, UErrorCode
&errorCode
) {
71 if(remainingCapacity
<2 && !resize(2, errorCode
)) {
74 if(lastCC
<=cc
|| cc
==0) {
76 limit
[1]=U16_TRAIL(c
);
89 UBool
ReorderingBuffer::append(const UChar
*s
, int32_t length
,
90 uint8_t leadCC
, uint8_t trailCC
,
91 UErrorCode
&errorCode
) {
95 if(remainingCapacity
<length
&& !resize(length
, errorCode
)) {
98 remainingCapacity
-=length
;
99 if(lastCC
<=leadCC
|| leadCC
==0) {
101 reorderStart
=limit
+length
;
102 } else if(leadCC
<=1) {
103 reorderStart
=limit
+1; // Ok if not a code point boundary.
105 const UChar
*sLimit
=s
+length
;
106 do { *limit
++=*s
++; } while(s
!=sLimit
);
111 U16_NEXT(s
, i
, length
, c
);
112 insert(c
, leadCC
); // insert first code point
114 U16_NEXT(s
, i
, length
, c
);
116 // s must be in NFD, otherwise we need to use getCC().
117 leadCC
=Normalizer2Impl::getCCFromYesOrMaybe(impl
.getNorm16(c
));
121 append(c
, leadCC
, errorCode
);
127 UBool
ReorderingBuffer::appendZeroCC(UChar32 c
, UErrorCode
&errorCode
) {
128 int32_t cpLength
=U16_LENGTH(c
);
129 if(remainingCapacity
<cpLength
&& !resize(cpLength
, errorCode
)) {
132 remainingCapacity
-=cpLength
;
136 limit
[0]=U16_LEAD(c
);
137 limit
[1]=U16_TRAIL(c
);
145 UBool
ReorderingBuffer::appendZeroCC(const UChar
*s
, const UChar
*sLimit
, UErrorCode
&errorCode
) {
149 int32_t length
=(int32_t)(sLimit
-s
);
150 if(remainingCapacity
<length
&& !resize(length
, errorCode
)) {
153 u_memcpy(limit
, s
, length
);
155 remainingCapacity
-=length
;
161 void ReorderingBuffer::remove() {
162 reorderStart
=limit
=start
;
163 remainingCapacity
=str
.getCapacity();
167 void ReorderingBuffer::removeSuffix(int32_t suffixLength
) {
168 if(suffixLength
<(limit
-start
)) {
170 remainingCapacity
+=suffixLength
;
173 remainingCapacity
=str
.getCapacity();
179 UBool
ReorderingBuffer::resize(int32_t appendLength
, UErrorCode
&errorCode
) {
180 int32_t reorderStartIndex
=(int32_t)(reorderStart
-start
);
181 int32_t length
=(int32_t)(limit
-start
);
182 str
.releaseBuffer(length
);
183 int32_t newCapacity
=length
+appendLength
;
184 int32_t doubleCapacity
=2*str
.getCapacity();
185 if(newCapacity
<doubleCapacity
) {
186 newCapacity
=doubleCapacity
;
188 if(newCapacity
<256) {
191 start
=str
.getBuffer(newCapacity
);
193 // getBuffer() already did str.setToBogus()
194 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
197 reorderStart
=start
+reorderStartIndex
;
199 remainingCapacity
=str
.getCapacity()-length
;
203 void ReorderingBuffer::skipPrevious() {
204 codePointLimit
=codePointStart
;
205 UChar c
=*--codePointStart
;
206 if(U16_IS_TRAIL(c
) && start
<codePointStart
&& U16_IS_LEAD(*(codePointStart
-1))) {
211 uint8_t ReorderingBuffer::previousCC() {
212 codePointLimit
=codePointStart
;
213 if(reorderStart
>=codePointStart
) {
216 UChar32 c
=*--codePointStart
;
217 if(c
<Normalizer2Impl::MIN_CCC_LCCC_CP
) {
222 if(U16_IS_TRAIL(c
) && start
<codePointStart
&& U16_IS_LEAD(c2
=*(codePointStart
-1))) {
224 c
=U16_GET_SUPPLEMENTARY(c2
, c
);
226 return Normalizer2Impl::getCCFromYesOrMaybe(impl
.getNorm16(c
));
229 // Inserts c somewhere before the last character.
230 // Requires 0<cc<lastCC which implies reorderStart<limit.
231 void ReorderingBuffer::insert(UChar32 c
, uint8_t cc
) {
232 for(setIterator(), skipPrevious(); previousCC()>cc
;) {}
233 // insert c at codePointLimit, after the character with prevCC<=cc
235 UChar
*r
=limit
+=U16_LENGTH(c
);
238 } while(codePointLimit
!=q
);
239 writeCodePoint(q
, c
);
245 // Normalizer2Impl --------------------------------------------------------- ***
247 struct CanonIterData
: public UMemory
{
248 CanonIterData(UErrorCode
&errorCode
);
250 void addToStartSet(UChar32 origin
, UChar32 decompLead
, UErrorCode
&errorCode
);
252 UVector canonStartSets
; // contains UnicodeSet *
255 Normalizer2Impl::~Normalizer2Impl() {
257 utrie2_close(normTrie
);
258 delete (CanonIterData
*)canonIterDataSingleton
.fInstance
;
262 Normalizer2Impl::isAcceptable(void *context
,
263 const char * /* type */, const char * /*name*/,
264 const UDataInfo
*pInfo
) {
267 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
268 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
269 pInfo
->dataFormat
[0]==0x4e && /* dataFormat="Nrm2" */
270 pInfo
->dataFormat
[1]==0x72 &&
271 pInfo
->dataFormat
[2]==0x6d &&
272 pInfo
->dataFormat
[3]==0x32 &&
273 pInfo
->formatVersion
[0]==2
275 Normalizer2Impl
*me
=(Normalizer2Impl
*)context
;
276 uprv_memcpy(me
->dataVersion
, pInfo
->dataVersion
, 4);
284 Normalizer2Impl::load(const char *packageName
, const char *name
, UErrorCode
&errorCode
) {
285 if(U_FAILURE(errorCode
)) {
288 memory
=udata_openChoice(packageName
, "nrm", name
, isAcceptable
, this, &errorCode
);
289 if(U_FAILURE(errorCode
)) {
292 const uint8_t *inBytes
=(const uint8_t *)udata_getMemory(memory
);
293 const int32_t *inIndexes
=(const int32_t *)inBytes
;
294 int32_t indexesLength
=inIndexes
[IX_NORM_TRIE_OFFSET
]/4;
295 if(indexesLength
<=IX_MIN_MAYBE_YES
) {
296 errorCode
=U_INVALID_FORMAT_ERROR
; // Not enough indexes.
300 minDecompNoCP
=inIndexes
[IX_MIN_DECOMP_NO_CP
];
301 minCompNoMaybeCP
=inIndexes
[IX_MIN_COMP_NO_MAYBE_CP
];
303 minYesNo
=inIndexes
[IX_MIN_YES_NO
];
304 minYesNoMappingsOnly
=inIndexes
[IX_MIN_YES_NO_MAPPINGS_ONLY
];
305 minNoNo
=inIndexes
[IX_MIN_NO_NO
];
306 limitNoNo
=inIndexes
[IX_LIMIT_NO_NO
];
307 minMaybeYes
=inIndexes
[IX_MIN_MAYBE_YES
];
309 int32_t offset
=inIndexes
[IX_NORM_TRIE_OFFSET
];
310 int32_t nextOffset
=inIndexes
[IX_EXTRA_DATA_OFFSET
];
311 normTrie
=utrie2_openFromSerialized(UTRIE2_16_VALUE_BITS
,
312 inBytes
+offset
, nextOffset
-offset
, NULL
,
314 if(U_FAILURE(errorCode
)) {
319 nextOffset
=inIndexes
[IX_SMALL_FCD_OFFSET
];
320 maybeYesCompositions
=(const uint16_t *)(inBytes
+offset
);
321 extraData
=maybeYesCompositions
+(MIN_NORMAL_MAYBE_YES
-minMaybeYes
);
323 // smallFCD: new in formatVersion 2
325 smallFCD
=inBytes
+offset
;
328 // gennorm2 enforces lccc=0 for c<MIN_CCC_LCCC_CP=U+0300.
330 for(UChar c
=0; c
<0x180; bits
>>=1) {
332 bits
=smallFCD
[c
>>8]; // one byte per 0x100 code points
335 for(int i
=0; i
<0x20; ++i
, ++c
) {
336 tccc180
[c
]=(uint8_t)getFCD16FromNormData(c
);
339 uprv_memset(tccc180
+c
, 0, 0x20);
345 uint8_t Normalizer2Impl::getTrailCCFromCompYesAndZeroCC(const UChar
*cpStart
, const UChar
*cpLimit
) const {
347 if(cpStart
==(cpLimit
-1)) {
350 c
=U16_GET_SUPPLEMENTARY(cpStart
[0], cpStart
[1]);
352 uint16_t prevNorm16
=getNorm16(c
);
353 if(prevNorm16
<=minYesNo
) {
354 return 0; // yesYes and Hangul LV/LVT have ccc=tccc=0
356 return (uint8_t)(*getMapping(prevNorm16
)>>8); // tccc from yesNo
362 static UBool U_CALLCONV
363 enumPropertyStartsRange(const void *context
, UChar32 start
, UChar32
/*end*/, uint32_t /*value*/) {
364 /* add the start code point to the USet */
365 const USetAdder
*sa
=(const USetAdder
*)context
;
366 sa
->add(sa
->set
, start
);
370 static uint32_t U_CALLCONV
371 segmentStarterMapper(const void * /*context*/, uint32_t value
) {
372 return value
&CANON_NOT_SEGMENT_STARTER
;
378 Normalizer2Impl::addPropertyStarts(const USetAdder
*sa
, UErrorCode
& /*errorCode*/) const {
379 /* add the start code point of each same-value range of each trie */
380 utrie2_enum(normTrie
, NULL
, enumPropertyStartsRange
, sa
);
382 /* add Hangul LV syllables and LV+1 because of skippables */
383 for(UChar c
=Hangul::HANGUL_BASE
; c
<Hangul::HANGUL_LIMIT
; c
+=Hangul::JAMO_T_COUNT
) {
385 sa
->add(sa
->set
, c
+1);
387 sa
->add(sa
->set
, Hangul::HANGUL_LIMIT
); /* add Hangul+1 to continue with other properties */
391 Normalizer2Impl::addCanonIterPropertyStarts(const USetAdder
*sa
, UErrorCode
&errorCode
) const {
392 /* add the start code point of each same-value range of the canonical iterator data trie */
393 if(ensureCanonIterData(errorCode
)) {
394 // currently only used for the SEGMENT_STARTER property
395 utrie2_enum(((CanonIterData
*)canonIterDataSingleton
.fInstance
)->trie
,
396 segmentStarterMapper
, enumPropertyStartsRange
, sa
);
401 Normalizer2Impl::copyLowPrefixFromNulTerminated(const UChar
*src
,
402 UChar32 minNeedDataCP
,
403 ReorderingBuffer
*buffer
,
404 UErrorCode
&errorCode
) const {
405 // Make some effort to support NUL-terminated strings reasonably.
406 // Take the part of the fast quick check loop that does not look up
407 // data and check the first part of the string.
408 // After this prefix, determine the string length to simplify the rest
410 const UChar
*prevSrc
=src
;
412 while((c
=*src
++)<minNeedDataCP
&& c
!=0) {}
413 // Back out the last character for full processing.
417 buffer
->appendZeroCC(prevSrc
, src
, errorCode
);
423 // Dual functionality:
424 // buffer!=NULL: normalize
425 // buffer==NULL: isNormalized/spanQuickCheckYes
427 Normalizer2Impl::decompose(const UChar
*src
, const UChar
*limit
,
428 ReorderingBuffer
*buffer
,
429 UErrorCode
&errorCode
) const {
430 UChar32 minNoCP
=minDecompNoCP
;
432 src
=copyLowPrefixFromNulTerminated(src
, minNoCP
, buffer
, errorCode
);
433 if(U_FAILURE(errorCode
)) {
436 limit
=u_strchr(src
, 0);
439 const UChar
*prevSrc
;
443 // only for quick check
444 const UChar
*prevBoundary
=src
;
448 // count code units below the minimum or with irrelevant data for the quick check
449 for(prevSrc
=src
; src
!=limit
;) {
450 if( (c
=*src
)<minNoCP
||
451 isMostDecompYesAndZeroCC(norm16
=UTRIE2_GET16_FROM_U16_SINGLE_LEAD(normTrie
, c
))
454 } else if(!U16_IS_SURROGATE(c
)) {
458 if(U16_IS_SURROGATE_LEAD(c
)) {
459 if((src
+1)!=limit
&& U16_IS_TRAIL(c2
=src
[1])) {
460 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
462 } else /* trail surrogate */ {
463 if(prevSrc
<src
&& U16_IS_LEAD(c2
=*(src
-1))) {
465 c
=U16_GET_SUPPLEMENTARY(c2
, c
);
468 if(isMostDecompYesAndZeroCC(norm16
=getNorm16(c
))) {
475 // copy these code units all at once
478 if(!buffer
->appendZeroCC(prevSrc
, src
, errorCode
)) {
490 // Check one above-minimum, relevant code point.
493 if(!decompose(c
, norm16
, *buffer
, errorCode
)) {
497 if(isDecompYes(norm16
)) {
498 uint8_t cc
=getCCFromYesOrMaybe(norm16
);
499 if(prevCC
<=cc
|| cc
==0) {
507 return prevBoundary
; // "no" or cc out of order
513 // Decompose a short piece of text which is likely to contain characters that
514 // fail the quick check loop and/or where the quick check loop's overhead
515 // is unlikely to be amortized.
516 // Called by the compose() and makeFCD() implementations.
517 UBool
Normalizer2Impl::decomposeShort(const UChar
*src
, const UChar
*limit
,
518 ReorderingBuffer
&buffer
,
519 UErrorCode
&errorCode
) const {
523 UTRIE2_U16_NEXT16(normTrie
, src
, limit
, c
, norm16
);
524 if(!decompose(c
, norm16
, buffer
, errorCode
)) {
531 UBool
Normalizer2Impl::decompose(UChar32 c
, uint16_t norm16
,
532 ReorderingBuffer
&buffer
,
533 UErrorCode
&errorCode
) const {
534 // Only loops for 1:1 algorithmic mappings.
536 // get the decomposition and the lead and trail cc's
537 if(isDecompYes(norm16
)) {
538 // c does not decompose
539 return buffer
.append(c
, getCCFromYesOrMaybe(norm16
), errorCode
);
540 } else if(isHangul(norm16
)) {
541 // Hangul syllable: decompose algorithmically
543 return buffer
.appendZeroCC(jamos
, jamos
+Hangul::decompose(c
, jamos
), errorCode
);
544 } else if(isDecompNoAlgorithmic(norm16
)) {
545 c
=mapAlgorithmic(c
, norm16
);
548 // c decomposes, get everything from the variable-length extra data
549 const uint16_t *mapping
=getMapping(norm16
);
550 uint16_t firstUnit
=*mapping
;
551 int32_t length
=firstUnit
&MAPPING_LENGTH_MASK
;
552 uint8_t leadCC
, trailCC
;
553 trailCC
=(uint8_t)(firstUnit
>>8);
554 if(firstUnit
&MAPPING_HAS_CCC_LCCC_WORD
) {
555 leadCC
=(uint8_t)(*(mapping
-1)>>8);
559 return buffer
.append((const UChar
*)mapping
+1, length
, leadCC
, trailCC
, errorCode
);
565 Normalizer2Impl::getDecomposition(UChar32 c
, UChar buffer
[4], int32_t &length
) const {
566 const UChar
*decomp
=NULL
;
569 if(c
<minDecompNoCP
|| isDecompYes(norm16
=getNorm16(c
))) {
570 // c does not decompose
572 } else if(isHangul(norm16
)) {
573 // Hangul syllable: decompose algorithmically
574 length
=Hangul::decompose(c
, buffer
);
576 } else if(isDecompNoAlgorithmic(norm16
)) {
577 c
=mapAlgorithmic(c
, norm16
);
580 U16_APPEND_UNSAFE(buffer
, length
, c
);
582 // c decomposes, get everything from the variable-length extra data
583 const uint16_t *mapping
=getMapping(norm16
);
584 length
=*mapping
&MAPPING_LENGTH_MASK
;
585 return (const UChar
*)mapping
+1;
590 // The capacity of the buffer must be 30=MAPPING_LENGTH_MASK-1
591 // so that a raw mapping fits that consists of one unit ("rm0")
592 // plus all but the first two code units of the normal mapping.
593 // The maximum length of a normal mapping is 31=MAPPING_LENGTH_MASK.
595 Normalizer2Impl::getRawDecomposition(UChar32 c
, UChar buffer
[30], int32_t &length
) const {
596 // We do not loop in this method because an algorithmic mapping itself
597 // becomes a final result rather than having to be decomposed recursively.
599 if(c
<minDecompNoCP
|| isDecompYes(norm16
=getNorm16(c
))) {
600 // c does not decompose
602 } else if(isHangul(norm16
)) {
603 // Hangul syllable: decompose algorithmically
604 Hangul::getRawDecomposition(c
, buffer
);
607 } else if(isDecompNoAlgorithmic(norm16
)) {
608 c
=mapAlgorithmic(c
, norm16
);
610 U16_APPEND_UNSAFE(buffer
, length
, c
);
613 // c decomposes, get everything from the variable-length extra data
614 const uint16_t *mapping
=getMapping(norm16
);
615 uint16_t firstUnit
=*mapping
;
616 int32_t mLength
=firstUnit
&MAPPING_LENGTH_MASK
; // length of normal mapping
617 if(firstUnit
&MAPPING_HAS_RAW_MAPPING
) {
618 // Read the raw mapping from before the firstUnit and before the optional ccc/lccc word.
619 // Bit 7=MAPPING_HAS_CCC_LCCC_WORD
620 const uint16_t *rawMapping
=mapping
-((firstUnit
>>7)&1)-1;
621 uint16_t rm0
=*rawMapping
;
622 if(rm0
<=MAPPING_LENGTH_MASK
) {
624 return (const UChar
*)rawMapping
-rm0
;
626 // Copy the normal mapping and replace its first two code units with rm0.
627 buffer
[0]=(UChar
)rm0
;
628 u_memcpy(buffer
+1, (const UChar
*)mapping
+1+2, mLength
-2);
634 return (const UChar
*)mapping
+1;
639 void Normalizer2Impl::decomposeAndAppend(const UChar
*src
, const UChar
*limit
,
641 UnicodeString
&safeMiddle
,
642 ReorderingBuffer
&buffer
,
643 UErrorCode
&errorCode
) const {
644 buffer
.copyReorderableSuffixTo(safeMiddle
);
646 decompose(src
, limit
, &buffer
, errorCode
);
649 // Just merge the strings at the boundary.
650 ForwardUTrie2StringIterator
iter(normTrie
, src
, limit
);
651 uint8_t firstCC
, prevCC
, cc
;
652 firstCC
=prevCC
=cc
=getCC(iter
.next16());
655 cc
=getCC(iter
.next16());
657 if(limit
==NULL
) { // appendZeroCC() needs limit!=NULL
658 limit
=u_strchr(iter
.codePointStart
, 0);
661 if (buffer
.append(src
, (int32_t)(iter
.codePointStart
-src
), firstCC
, prevCC
, errorCode
)) {
662 buffer
.appendZeroCC(iter
.codePointStart
, limit
, errorCode
);
666 // Note: hasDecompBoundary() could be implemented as aliases to
667 // hasFCDBoundaryBefore() and hasFCDBoundaryAfter()
668 // at the cost of building the FCD trie for a decomposition normalizer.
669 UBool
Normalizer2Impl::hasDecompBoundary(UChar32 c
, UBool before
) const {
671 if(c
<minDecompNoCP
) {
674 uint16_t norm16
=getNorm16(c
);
675 if(isHangul(norm16
) || isDecompYesAndZeroCC(norm16
)) {
677 } else if(norm16
>MIN_NORMAL_MAYBE_YES
) {
678 return FALSE
; // ccc!=0
679 } else if(isDecompNoAlgorithmic(norm16
)) {
680 c
=mapAlgorithmic(c
, norm16
);
682 // c decomposes, get everything from the variable-length extra data
683 const uint16_t *mapping
=getMapping(norm16
);
684 uint16_t firstUnit
=*mapping
;
685 if((firstUnit
&MAPPING_LENGTH_MASK
)==0) {
689 // decomp after-boundary: same as hasFCDBoundaryAfter(),
690 // fcd16<=1 || trailCC==0
691 if(firstUnit
>0x1ff) {
692 return FALSE
; // trailCC>1
694 if(firstUnit
<=0xff) {
695 return TRUE
; // trailCC==0
697 // if(trailCC==1) test leadCC==0, same as checking for before-boundary
699 // TRUE if leadCC==0 (hasFCDBoundaryBefore())
700 return (firstUnit
&MAPPING_HAS_CCC_LCCC_WORD
)==0 || (*(mapping
-1)&0xff00)==0;
706 * Finds the recomposition result for
707 * a forward-combining "lead" character,
708 * specified with a pointer to its compositions list,
709 * and a backward-combining "trail" character.
711 * If the lead and trail characters combine, then this function returns
712 * the following "compositeAndFwd" value:
713 * Bits 21..1 composite character
714 * Bit 0 set if the composite is a forward-combining starter
715 * otherwise it returns -1.
717 * The compositions list has (trail, compositeAndFwd) pair entries,
718 * encoded as either pairs or triples of 16-bit units.
719 * The last entry has the high bit of its first unit set.
721 * The list is sorted by ascending trail characters (there are no duplicates).
722 * A linear search is used.
724 * See normalizer2impl.h for a more detailed description
725 * of the compositions list format.
727 int32_t Normalizer2Impl::combine(const uint16_t *list
, UChar32 trail
) {
728 uint16_t key1
, firstUnit
;
729 if(trail
<COMP_1_TRAIL_LIMIT
) {
730 // trail character is 0..33FF
731 // result entry may have 2 or 3 units
732 key1
=(uint16_t)(trail
<<1);
733 while(key1
>(firstUnit
=*list
)) {
734 list
+=2+(firstUnit
&COMP_1_TRIPLE
);
736 if(key1
==(firstUnit
&COMP_1_TRAIL_MASK
)) {
737 if(firstUnit
&COMP_1_TRIPLE
) {
738 return ((int32_t)list
[1]<<16)|list
[2];
744 // trail character is 3400..10FFFF
745 // result entry has 3 units
746 key1
=(uint16_t)(COMP_1_TRAIL_LIMIT
+
747 (((trail
>>COMP_1_TRAIL_SHIFT
))&
749 uint16_t key2
=(uint16_t)(trail
<<COMP_2_TRAIL_SHIFT
);
752 if(key1
>(firstUnit
=*list
)) {
753 list
+=2+(firstUnit
&COMP_1_TRIPLE
);
754 } else if(key1
==(firstUnit
&COMP_1_TRAIL_MASK
)) {
755 if(key2
>(secondUnit
=list
[1])) {
756 if(firstUnit
&COMP_1_LAST_TUPLE
) {
761 } else if(key2
==(secondUnit
&COMP_2_TRAIL_MASK
)) {
762 return ((int32_t)(secondUnit
&~COMP_2_TRAIL_MASK
)<<16)|list
[2];
775 * @param list some character's compositions list
776 * @param set recursively receives the composites from these compositions
778 void Normalizer2Impl::addComposites(const uint16_t *list
, UnicodeSet
&set
) const {
780 int32_t compositeAndFwd
;
783 if((firstUnit
&COMP_1_TRIPLE
)==0) {
784 compositeAndFwd
=list
[1];
787 compositeAndFwd
=(((int32_t)list
[1]&~COMP_2_TRAIL_MASK
)<<16)|list
[2];
790 UChar32 composite
=compositeAndFwd
>>1;
791 if((compositeAndFwd
&1)!=0) {
792 addComposites(getCompositionsListForComposite(getNorm16(composite
)), set
);
795 } while((firstUnit
&COMP_1_LAST_TUPLE
)==0);
799 * Recomposes the buffer text starting at recomposeStartIndex
800 * (which is in NFD - decomposed and canonically ordered),
801 * and truncates the buffer contents.
803 * Note that recomposition never lengthens the text:
804 * Any character consists of either one or two code units;
805 * a composition may contain at most one more code unit than the original starter,
806 * while the combining mark that is removed has at least one code unit.
808 void Normalizer2Impl::recompose(ReorderingBuffer
&buffer
, int32_t recomposeStartIndex
,
809 UBool onlyContiguous
) const {
810 UChar
*p
=buffer
.getStart()+recomposeStartIndex
;
811 UChar
*limit
=buffer
.getLimit();
816 UChar
*starter
, *pRemove
, *q
, *r
;
817 const uint16_t *compositionsList
;
818 UChar32 c
, compositeAndFwd
;
821 UBool starterIsSupplementary
;
823 // Some of the following variables are not used until we have a forward-combining starter
824 // and are only initialized now to avoid compiler warnings.
825 compositionsList
=NULL
; // used as indicator for whether we have a forward-combining starter
827 starterIsSupplementary
=FALSE
;
831 UTRIE2_U16_NEXT16(normTrie
, p
, limit
, c
, norm16
);
832 cc
=getCCFromYesOrMaybe(norm16
);
833 if( // this character combines backward and
835 // we have seen a starter that combines forward and
836 compositionsList
!=NULL
&&
837 // the backward-combining character is not blocked
838 (prevCC
<cc
|| prevCC
==0)
840 if(isJamoVT(norm16
)) {
841 // c is a Jamo V/T, see if we can compose it with the previous character.
842 if(c
<Hangul::JAMO_T_BASE
) {
843 // c is a Jamo Vowel, compose with previous Jamo L and following Jamo T.
844 UChar prev
=(UChar
)(*starter
-Hangul::JAMO_L_BASE
);
845 if(prev
<Hangul::JAMO_L_COUNT
) {
847 UChar syllable
=(UChar
)
848 (Hangul::HANGUL_BASE
+
849 (prev
*Hangul::JAMO_V_COUNT
+(c
-Hangul::JAMO_V_BASE
))*
850 Hangul::JAMO_T_COUNT
);
852 if(p
!=limit
&& (t
=(UChar
)(*p
-Hangul::JAMO_T_BASE
))<Hangul::JAMO_T_COUNT
) {
854 syllable
+=t
; // The next character was a Jamo T.
857 // remove the Jamo V/T
868 * No "else" for Jamo T:
869 * Since the input is in NFD, there are no Hangul LV syllables that
870 * a Jamo T could combine with.
871 * All Jamo Ts are combined above when handling Jamo Vs.
876 compositionsList
=NULL
;
878 } else if((compositeAndFwd
=combine(compositionsList
, c
))>=0) {
879 // The starter and the combining mark (c) do combine.
880 UChar32 composite
=compositeAndFwd
>>1;
882 // Replace the starter with the composite, remove the combining mark.
883 pRemove
=p
-U16_LENGTH(c
); // pRemove & p: start & limit of the combining mark
884 if(starterIsSupplementary
) {
885 if(U_IS_SUPPLEMENTARY(composite
)) {
886 // both are supplementary
887 starter
[0]=U16_LEAD(composite
);
888 starter
[1]=U16_TRAIL(composite
);
890 *starter
=(UChar
)composite
;
891 // The composite is shorter than the starter,
892 // move the intermediate characters forward one.
893 starterIsSupplementary
=FALSE
;
901 } else if(U_IS_SUPPLEMENTARY(composite
)) {
902 // The composite is longer than the starter,
903 // move the intermediate characters back one.
904 starterIsSupplementary
=TRUE
;
905 ++starter
; // temporarily increment for the loop boundary
911 *starter
=U16_TRAIL(composite
);
912 *--starter
=U16_LEAD(composite
); // undo the temporary increment
914 // both are on the BMP
915 *starter
=(UChar
)composite
;
918 /* remove the combining mark by moving the following text over it */
928 // Keep prevCC because we removed the combining mark.
933 // Is the composite a starter that combines forward?
934 if(compositeAndFwd
&1) {
936 getCompositionsListForComposite(getNorm16(composite
));
938 compositionsList
=NULL
;
941 // We combined; continue with looking for compositions.
946 // no combination this time
952 // If c did not combine, then check if it is a starter.
954 // Found a new starter.
955 if((compositionsList
=getCompositionsListForDecompYes(norm16
))!=NULL
) {
956 // It may combine with something, prepare for it.
958 starterIsSupplementary
=FALSE
;
961 starterIsSupplementary
=TRUE
;
965 } else if(onlyContiguous
) {
966 // FCC: no discontiguous compositions; any intervening character blocks.
967 compositionsList
=NULL
;
970 buffer
.setReorderingLimit(limit
);
974 Normalizer2Impl::composePair(UChar32 a
, UChar32 b
) const {
975 uint16_t norm16
=getNorm16(a
); // maps an out-of-range 'a' to inert norm16=0
976 const uint16_t *list
;
977 if(isInert(norm16
)) {
979 } else if(norm16
<minYesNoMappingsOnly
) {
980 if(isJamoL(norm16
)) {
981 b
-=Hangul::JAMO_V_BASE
;
982 if(0<=b
&& b
<Hangul::JAMO_V_COUNT
) {
984 (Hangul::HANGUL_BASE
+
985 ((a
-Hangul::JAMO_L_BASE
)*Hangul::JAMO_V_COUNT
+b
)*
986 Hangul::JAMO_T_COUNT
);
990 } else if(isHangul(norm16
)) {
991 b
-=Hangul::JAMO_T_BASE
;
992 if(Hangul::isHangulWithoutJamoT(a
) && 0<b
&& b
<Hangul::JAMO_T_COUNT
) { // not b==0!
998 // 'a' has a compositions list in extraData
999 list
=extraData
+norm16
;
1000 if(norm16
>minYesNo
) { // composite 'a' has both mapping & compositions list
1001 list
+= // mapping pointer
1002 1+ // +1 to skip the first unit with the mapping lenth
1003 (*list
&MAPPING_LENGTH_MASK
); // + mapping length
1006 } else if(norm16
<minMaybeYes
|| MIN_NORMAL_MAYBE_YES
<=norm16
) {
1009 list
=maybeYesCompositions
+norm16
-minMaybeYes
;
1011 if(b
<0 || 0x10ffff<b
) { // combine(list, b) requires a valid code point b
1014 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
1015 return combine(list
, b
)>>1;
1017 int32_t compositeAndFwd
=combine(list
, b
);
1018 return compositeAndFwd
>=0 ? compositeAndFwd
>>1 : U_SENTINEL
;
1022 // Very similar to composeQuickCheck(): Make the same changes in both places if relevant.
1023 // doCompose: normalize
1024 // !doCompose: isNormalized (buffer must be empty and initialized)
1026 Normalizer2Impl::compose(const UChar
*src
, const UChar
*limit
,
1027 UBool onlyContiguous
,
1029 ReorderingBuffer
&buffer
,
1030 UErrorCode
&errorCode
) const {
1032 * prevBoundary points to the last character before the current one
1033 * that has a composition boundary before it with ccc==0 and quick check "yes".
1034 * Keeping track of prevBoundary saves us looking for a composition boundary
1035 * when we find a "no" or "maybe".
1037 * When we back out from prevSrc back to prevBoundary,
1038 * then we also remove those same characters (which had been simply copied
1039 * or canonically-order-inserted) from the ReorderingBuffer.
1040 * Therefore, at all times, the [prevBoundary..prevSrc[ source units
1041 * must correspond 1:1 to destination units at the end of the destination buffer.
1043 const UChar
*prevBoundary
=src
;
1044 UChar32 minNoMaybeCP
=minCompNoMaybeCP
;
1046 src
=copyLowPrefixFromNulTerminated(src
, minNoMaybeCP
,
1047 doCompose
? &buffer
: NULL
,
1049 if(U_FAILURE(errorCode
)) {
1052 if(prevBoundary
<src
) {
1053 // Set prevBoundary to the last character in the prefix.
1056 limit
=u_strchr(src
, 0);
1059 const UChar
*prevSrc
;
1063 // only for isNormalized
1067 // count code units below the minimum or with irrelevant data for the quick check
1068 for(prevSrc
=src
; src
!=limit
;) {
1069 if( (c
=*src
)<minNoMaybeCP
||
1070 isCompYesAndZeroCC(norm16
=UTRIE2_GET16_FROM_U16_SINGLE_LEAD(normTrie
, c
))
1073 } else if(!U16_IS_SURROGATE(c
)) {
1077 if(U16_IS_SURROGATE_LEAD(c
)) {
1078 if((src
+1)!=limit
&& U16_IS_TRAIL(c2
=src
[1])) {
1079 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1081 } else /* trail surrogate */ {
1082 if(prevSrc
<src
&& U16_IS_LEAD(c2
=*(src
-1))) {
1084 c
=U16_GET_SUPPLEMENTARY(c2
, c
);
1087 if(isCompYesAndZeroCC(norm16
=getNorm16(c
))) {
1094 // copy these code units all at once
1097 if(!buffer
.appendZeroCC(prevSrc
, src
, errorCode
)) {
1106 // Set prevBoundary to the last character in the quick check loop.
1108 if( U16_IS_TRAIL(*prevBoundary
) && prevSrc
<prevBoundary
&&
1109 U16_IS_LEAD(*(prevBoundary
-1))
1113 // The start of the current character (c).
1115 } else if(src
==limit
) {
1121 * isCompYesAndZeroCC(norm16) is false, that is, norm16>=minNoNo.
1122 * c is either a "noNo" (has a mapping) or a "maybeYes" (combines backward)
1124 * Check for Jamo V/T, then for regular characters.
1125 * c is not a Hangul syllable or Jamo L because those have "yes" properties.
1127 if(isJamoVT(norm16
) && prevBoundary
!=prevSrc
) {
1128 UChar prev
=*(prevSrc
-1);
1129 UBool needToDecompose
=FALSE
;
1130 if(c
<Hangul::JAMO_T_BASE
) {
1131 // c is a Jamo Vowel, compose with previous Jamo L and following Jamo T.
1132 prev
=(UChar
)(prev
-Hangul::JAMO_L_BASE
);
1133 if(prev
<Hangul::JAMO_L_COUNT
) {
1137 UChar syllable
=(UChar
)
1138 (Hangul::HANGUL_BASE
+
1139 (prev
*Hangul::JAMO_V_COUNT
+(c
-Hangul::JAMO_V_BASE
))*
1140 Hangul::JAMO_T_COUNT
);
1142 if(src
!=limit
&& (t
=(UChar
)(*src
-Hangul::JAMO_T_BASE
))<Hangul::JAMO_T_COUNT
) {
1144 syllable
+=t
; // The next character was a Jamo T.
1146 buffer
.setLastChar(syllable
);
1149 // If we see L+V+x where x!=T then we drop to the slow path,
1150 // decompose and recompose.
1151 // This is to deal with NFKC finding normal L and V but a
1152 // compatibility variant of a T. We need to either fully compose that
1153 // combination here (which would complicate the code and may not work
1154 // with strange custom data) or use the slow path -- or else our replacing
1155 // two input characters (L+V) with one output character (LV syllable)
1156 // would violate the invariant that [prevBoundary..prevSrc[ has the same
1157 // length as what we appended to the buffer since prevBoundary.
1158 needToDecompose
=TRUE
;
1160 } else if(Hangul::isHangulWithoutJamoT(prev
)) {
1161 // c is a Jamo Trailing consonant,
1162 // compose with previous Hangul LV that does not contain a Jamo T.
1166 buffer
.setLastChar((UChar
)(prev
+c
-Hangul::JAMO_T_BASE
));
1170 if(!needToDecompose
) {
1171 // The Jamo V/T did not compose into a Hangul syllable.
1173 if(!buffer
.appendBMP((UChar
)c
, 0, errorCode
)) {
1183 * Source buffer pointers:
1185 * all done quick check current char not yet
1186 * "yes" but (c) processed
1189 * [-------------[-------------[-------------[-------------[
1191 * orig. src prevBoundary prevSrc src limit
1194 * Destination buffer pointers inside the ReorderingBuffer:
1196 * all done might take not filled yet
1199 * [-------------[-------------[-------------[
1201 * start reorderStart limit |
1204 if(norm16
>=MIN_YES_YES_WITH_CC
) {
1205 uint8_t cc
=(uint8_t)norm16
; // cc!=0
1206 if( onlyContiguous
&& // FCC
1207 (doCompose
? buffer
.getLastCC() : prevCC
)==0 &&
1208 prevBoundary
<prevSrc
&&
1209 // buffer.getLastCC()==0 && prevBoundary<prevSrc tell us that
1210 // [prevBoundary..prevSrc[ (which is exactly one character under these conditions)
1211 // passed the quick check "yes && ccc==0" test.
1212 // Check whether the last character was a "yesYes" or a "yesNo".
1213 // If a "yesNo", then we get its trailing ccc from its
1214 // mapping and check for canonical order.
1215 // All other cases are ok.
1216 getTrailCCFromCompYesAndZeroCC(prevBoundary
, prevSrc
)>cc
1218 // Fails FCD test, need to decompose and contiguously recompose.
1222 } else if(doCompose
) {
1223 if(!buffer
.append(c
, cc
, errorCode
)) {
1227 } else if(prevCC
<=cc
) {
1233 } else if(!doCompose
&& !isMaybeOrNonZeroCC(norm16
)) {
1238 * Find appropriate boundaries around this character,
1239 * decompose the source text from between the boundaries,
1242 * We may need to remove the last few characters from the ReorderingBuffer
1243 * to account for source text that was copied or appended
1244 * but needs to take part in the recomposition.
1248 * Find the last composition boundary in [prevBoundary..src[.
1249 * It is either the decomposition of the current character (at prevSrc),
1252 if(hasCompBoundaryBefore(c
, norm16
)) {
1253 prevBoundary
=prevSrc
;
1254 } else if(doCompose
) {
1255 buffer
.removeSuffix((int32_t)(prevSrc
-prevBoundary
));
1258 // Find the next composition boundary in [src..limit[ -
1259 // modifies src to point to the next starter.
1260 src
=(UChar
*)findNextCompBoundary(src
, limit
);
1262 // Decompose [prevBoundary..src[ into the buffer and then recompose that part of it.
1263 int32_t recomposeStartIndex
=buffer
.length();
1264 if(!decomposeShort(prevBoundary
, src
, buffer
, errorCode
)) {
1267 recompose(buffer
, recomposeStartIndex
, onlyContiguous
);
1269 if(!buffer
.equals(prevBoundary
, src
)) {
1276 // Move to the next starter. We never need to look back before this point again.
1282 // Very similar to compose(): Make the same changes in both places if relevant.
1283 // pQCResult==NULL: spanQuickCheckYes
1284 // pQCResult!=NULL: quickCheck (*pQCResult must be UNORM_YES)
1286 Normalizer2Impl::composeQuickCheck(const UChar
*src
, const UChar
*limit
,
1287 UBool onlyContiguous
,
1288 UNormalizationCheckResult
*pQCResult
) const {
1290 * prevBoundary points to the last character before the current one
1291 * that has a composition boundary before it with ccc==0 and quick check "yes".
1293 const UChar
*prevBoundary
=src
;
1294 UChar32 minNoMaybeCP
=minCompNoMaybeCP
;
1296 UErrorCode errorCode
=U_ZERO_ERROR
;
1297 src
=copyLowPrefixFromNulTerminated(src
, minNoMaybeCP
, NULL
, errorCode
);
1298 if(prevBoundary
<src
) {
1299 // Set prevBoundary to the last character in the prefix.
1302 limit
=u_strchr(src
, 0);
1305 const UChar
*prevSrc
;
1311 // count code units below the minimum or with irrelevant data for the quick check
1312 for(prevSrc
=src
;;) {
1316 if( (c
=*src
)<minNoMaybeCP
||
1317 isCompYesAndZeroCC(norm16
=UTRIE2_GET16_FROM_U16_SINGLE_LEAD(normTrie
, c
))
1320 } else if(!U16_IS_SURROGATE(c
)) {
1324 if(U16_IS_SURROGATE_LEAD(c
)) {
1325 if((src
+1)!=limit
&& U16_IS_TRAIL(c2
=src
[1])) {
1326 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1328 } else /* trail surrogate */ {
1329 if(prevSrc
<src
&& U16_IS_LEAD(c2
=*(src
-1))) {
1331 c
=U16_GET_SUPPLEMENTARY(c2
, c
);
1334 if(isCompYesAndZeroCC(norm16
=getNorm16(c
))) {
1342 // Set prevBoundary to the last character in the quick check loop.
1344 if( U16_IS_TRAIL(*prevBoundary
) && prevSrc
<prevBoundary
&&
1345 U16_IS_LEAD(*(prevBoundary
-1))
1350 // The start of the current character (c).
1356 * isCompYesAndZeroCC(norm16) is false, that is, norm16>=minNoNo.
1357 * c is either a "noNo" (has a mapping) or a "maybeYes" (combines backward)
1360 if(isMaybeOrNonZeroCC(norm16
)) {
1361 uint8_t cc
=getCCFromYesOrMaybe(norm16
);
1362 if( onlyContiguous
&& // FCC
1365 prevBoundary
<prevSrc
&&
1366 // prevCC==0 && prevBoundary<prevSrc tell us that
1367 // [prevBoundary..prevSrc[ (which is exactly one character under these conditions)
1368 // passed the quick check "yes && ccc==0" test.
1369 // Check whether the last character was a "yesYes" or a "yesNo".
1370 // If a "yesNo", then we get its trailing ccc from its
1371 // mapping and check for canonical order.
1372 // All other cases are ok.
1373 getTrailCCFromCompYesAndZeroCC(prevBoundary
, prevSrc
)>cc
1376 } else if(prevCC
<=cc
|| cc
==0) {
1378 if(norm16
<MIN_YES_YES_WITH_CC
) {
1379 if(pQCResult
!=NULL
) {
1380 *pQCResult
=UNORM_MAYBE
;
1382 return prevBoundary
;
1388 if(pQCResult
!=NULL
) {
1389 *pQCResult
=UNORM_NO
;
1391 return prevBoundary
;
1395 void Normalizer2Impl::composeAndAppend(const UChar
*src
, const UChar
*limit
,
1397 UBool onlyContiguous
,
1398 UnicodeString
&safeMiddle
,
1399 ReorderingBuffer
&buffer
,
1400 UErrorCode
&errorCode
) const {
1401 if(!buffer
.isEmpty()) {
1402 const UChar
*firstStarterInSrc
=findNextCompBoundary(src
, limit
);
1403 if(src
!=firstStarterInSrc
) {
1404 const UChar
*lastStarterInDest
=findPreviousCompBoundary(buffer
.getStart(),
1406 int32_t destSuffixLength
=(int32_t)(buffer
.getLimit()-lastStarterInDest
);
1407 UnicodeString
middle(lastStarterInDest
, destSuffixLength
);
1408 buffer
.removeSuffix(destSuffixLength
);
1410 middle
.append(src
, (int32_t)(firstStarterInSrc
-src
));
1411 const UChar
*middleStart
=middle
.getBuffer();
1412 compose(middleStart
, middleStart
+middle
.length(), onlyContiguous
,
1413 TRUE
, buffer
, errorCode
);
1414 if(U_FAILURE(errorCode
)) {
1417 src
=firstStarterInSrc
;
1421 compose(src
, limit
, onlyContiguous
, TRUE
, buffer
, errorCode
);
1423 if(limit
==NULL
) { // appendZeroCC() needs limit!=NULL
1424 limit
=u_strchr(src
, 0);
1426 buffer
.appendZeroCC(src
, limit
, errorCode
);
1431 * Does c have a composition boundary before it?
1432 * True if its decomposition begins with a character that has
1433 * ccc=0 && NFC_QC=Yes (isCompYesAndZeroCC()).
1434 * As a shortcut, this is true if c itself has ccc=0 && NFC_QC=Yes
1435 * (isCompYesAndZeroCC()) so we need not decompose.
1437 UBool
Normalizer2Impl::hasCompBoundaryBefore(UChar32 c
, uint16_t norm16
) const {
1439 if(isCompYesAndZeroCC(norm16
)) {
1441 } else if(isMaybeOrNonZeroCC(norm16
)) {
1443 } else if(isDecompNoAlgorithmic(norm16
)) {
1444 c
=mapAlgorithmic(c
, norm16
);
1445 norm16
=getNorm16(c
);
1447 // c decomposes, get everything from the variable-length extra data
1448 const uint16_t *mapping
=getMapping(norm16
);
1449 uint16_t firstUnit
=*mapping
;
1450 if((firstUnit
&MAPPING_LENGTH_MASK
)==0) {
1453 if((firstUnit
&MAPPING_HAS_CCC_LCCC_WORD
) && (*(mapping
-1)&0xff00)) {
1454 return FALSE
; // non-zero leadCC
1456 int32_t i
=1; // skip over the firstUnit
1458 U16_NEXT_UNSAFE(mapping
, i
, c
);
1459 return isCompYesAndZeroCC(getNorm16(c
));
1464 UBool
Normalizer2Impl::hasCompBoundaryAfter(UChar32 c
, UBool onlyContiguous
, UBool testInert
) const {
1466 uint16_t norm16
=getNorm16(c
);
1467 if(isInert(norm16
)) {
1469 } else if(norm16
<=minYesNo
) {
1470 // Hangul: norm16==minYesNo
1471 // Hangul LVT has a boundary after it.
1472 // Hangul LV and non-inert yesYes characters combine forward.
1473 return isHangul(norm16
) && !Hangul::isHangulWithoutJamoT((UChar
)c
);
1474 } else if(norm16
>= (testInert
? minNoNo
: minMaybeYes
)) {
1476 } else if(isDecompNoAlgorithmic(norm16
)) {
1477 c
=mapAlgorithmic(c
, norm16
);
1479 // c decomposes, get everything from the variable-length extra data.
1480 // If testInert, then c must be a yesNo character which has lccc=0,
1481 // otherwise it could be a noNo.
1482 const uint16_t *mapping
=getMapping(norm16
);
1483 uint16_t firstUnit
=*mapping
;
1485 // not MAPPING_NO_COMP_BOUNDARY_AFTER
1487 // c is not deleted, and
1488 // it and its decomposition do not combine forward, and it has a starter)
1489 // and if FCC then trailCC<=1
1491 (firstUnit
&MAPPING_NO_COMP_BOUNDARY_AFTER
)==0 &&
1492 (!onlyContiguous
|| firstUnit
<=0x1ff);
1497 const UChar
*Normalizer2Impl::findPreviousCompBoundary(const UChar
*start
, const UChar
*p
) const {
1498 BackwardUTrie2StringIterator
iter(normTrie
, start
, p
);
1501 norm16
=iter
.previous16();
1502 } while(!hasCompBoundaryBefore(iter
.codePoint
, norm16
));
1503 // We could also test hasCompBoundaryAfter() and return iter.codePointLimit,
1504 // but that's probably not worth the extra cost.
1505 return iter
.codePointStart
;
1508 const UChar
*Normalizer2Impl::findNextCompBoundary(const UChar
*p
, const UChar
*limit
) const {
1509 ForwardUTrie2StringIterator
iter(normTrie
, p
, limit
);
1512 norm16
=iter
.next16();
1513 } while(!hasCompBoundaryBefore(iter
.codePoint
, norm16
));
1514 return iter
.codePointStart
;
1517 // Note: normalizer2impl.cpp r30982 (2011-nov-27)
1518 // still had getFCDTrie() which built and cached an FCD trie.
1519 // That provided faster access to FCD data than getFCD16FromNormData()
1520 // but required synchronization and consumed some 10kB of heap memory
1521 // in any process that uses FCD (e.g., via collation).
1522 // tccc180[] and smallFCD[] are intended to help with any loss of performance,
1523 // at least for Latin & CJK.
1525 // Gets the FCD value from the regular normalization data.
1526 uint16_t Normalizer2Impl::getFCD16FromNormData(UChar32 c
) const {
1527 // Only loops for 1:1 algorithmic mappings.
1529 uint16_t norm16
=getNorm16(c
);
1530 if(norm16
<=minYesNo
) {
1531 // no decomposition or Hangul syllable, all zeros
1533 } else if(norm16
>=MIN_NORMAL_MAYBE_YES
) {
1536 return norm16
|(norm16
<<8);
1537 } else if(norm16
>=minMaybeYes
) {
1539 } else if(isDecompNoAlgorithmic(norm16
)) {
1540 c
=mapAlgorithmic(c
, norm16
);
1542 // c decomposes, get everything from the variable-length extra data
1543 const uint16_t *mapping
=getMapping(norm16
);
1544 uint16_t firstUnit
=*mapping
;
1545 if((firstUnit
&MAPPING_LENGTH_MASK
)==0) {
1546 // A character that is deleted (maps to an empty string) must
1547 // get the worst-case lccc and tccc values because arbitrary
1548 // characters on both sides will become adjacent.
1551 norm16
=firstUnit
>>8; // tccc
1552 if(firstUnit
&MAPPING_HAS_CCC_LCCC_WORD
) {
1553 norm16
|=*(mapping
-1)&0xff00; // lccc
1561 // Dual functionality:
1562 // buffer!=NULL: normalize
1563 // buffer==NULL: isNormalized/quickCheck/spanQuickCheckYes
1565 Normalizer2Impl::makeFCD(const UChar
*src
, const UChar
*limit
,
1566 ReorderingBuffer
*buffer
,
1567 UErrorCode
&errorCode
) const {
1568 // Tracks the last FCD-safe boundary, before lccc=0 or after properly-ordered tccc<=1.
1569 // Similar to the prevBoundary in the compose() implementation.
1570 const UChar
*prevBoundary
=src
;
1571 int32_t prevFCD16
=0;
1573 src
=copyLowPrefixFromNulTerminated(src
, MIN_CCC_LCCC_CP
, buffer
, errorCode
);
1574 if(U_FAILURE(errorCode
)) {
1577 if(prevBoundary
<src
) {
1579 // We know that the previous character's lccc==0.
1580 // Fetching the fcd16 value was deferred for this below-U+0300 code point.
1581 prevFCD16
=getFCD16(*(src
-1));
1586 limit
=u_strchr(src
, 0);
1589 // Note: In this function we use buffer->appendZeroCC() because we track
1590 // the lead and trail combining classes here, rather than leaving it to
1591 // the ReorderingBuffer.
1592 // The exception is the call to decomposeShort() which uses the buffer
1593 // in the normal way.
1595 const UChar
*prevSrc
;
1600 // count code units with lccc==0
1601 for(prevSrc
=src
; src
!=limit
;) {
1602 if((c
=*src
)<MIN_CCC_LCCC_CP
) {
1605 } else if(!singleLeadMightHaveNonZeroFCD16(c
)) {
1609 if(U16_IS_SURROGATE(c
)) {
1611 if(U16_IS_SURROGATE_LEAD(c
)) {
1612 if((src
+1)!=limit
&& U16_IS_TRAIL(c2
=src
[1])) {
1613 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1615 } else /* trail surrogate */ {
1616 if(prevSrc
<src
&& U16_IS_LEAD(c2
=*(src
-1))) {
1618 c
=U16_GET_SUPPLEMENTARY(c2
, c
);
1622 if((fcd16
=getFCD16FromNormData(c
))<=0xff) {
1630 // copy these code units all at once
1632 if(buffer
!=NULL
&& !buffer
->appendZeroCC(prevSrc
, src
, errorCode
)) {
1639 // We know that the previous character's lccc==0.
1641 // Fetching the fcd16 value was deferred for this below-U+0300 code point.
1642 UChar32 prev
=~prevFCD16
;
1643 prevFCD16
= prev
<0x180 ? tccc180
[prev
] : getFCD16FromNormData(prev
);
1648 const UChar
*p
=src
-1;
1649 if(U16_IS_TRAIL(*p
) && prevSrc
<p
&& U16_IS_LEAD(*(p
-1))) {
1651 // Need to fetch the previous character's FCD value because
1652 // prevFCD16 was just for the trail surrogate code point.
1653 prevFCD16
=getFCD16FromNormData(U16_GET_SUPPLEMENTARY(p
[0], p
[1]));
1654 // Still known to have lccc==0 because its lead surrogate unit had lccc==0.
1660 // The start of the current character (c).
1662 } else if(src
==limit
) {
1667 // The current character (c) at [prevSrc..src[ has a non-zero lead combining class.
1668 // Check for proper order, and decompose locally if necessary.
1669 if((prevFCD16
&0xff)<=(fcd16
>>8)) {
1670 // proper order: prev tccc <= current lccc
1671 if((fcd16
&0xff)<=1) {
1674 if(buffer
!=NULL
&& !buffer
->appendZeroCC(c
, errorCode
)) {
1679 } else if(buffer
==NULL
) {
1680 return prevBoundary
; // quick check "no"
1683 * Back out the part of the source that we copied or appended
1684 * already but is now going to be decomposed.
1685 * prevSrc is set to after what was copied/appended.
1687 buffer
->removeSuffix((int32_t)(prevSrc
-prevBoundary
));
1689 * Find the part of the source that needs to be decomposed,
1690 * up to the next safe boundary.
1692 src
=findNextFCDBoundary(src
, limit
);
1694 * The source text does not fulfill the conditions for FCD.
1695 * Decompose and reorder a limited piece of the text.
1697 if(!decomposeShort(prevBoundary
, src
, *buffer
, errorCode
)) {
1707 void Normalizer2Impl::makeFCDAndAppend(const UChar
*src
, const UChar
*limit
,
1709 UnicodeString
&safeMiddle
,
1710 ReorderingBuffer
&buffer
,
1711 UErrorCode
&errorCode
) const {
1712 if(!buffer
.isEmpty()) {
1713 const UChar
*firstBoundaryInSrc
=findNextFCDBoundary(src
, limit
);
1714 if(src
!=firstBoundaryInSrc
) {
1715 const UChar
*lastBoundaryInDest
=findPreviousFCDBoundary(buffer
.getStart(),
1717 int32_t destSuffixLength
=(int32_t)(buffer
.getLimit()-lastBoundaryInDest
);
1718 UnicodeString
middle(lastBoundaryInDest
, destSuffixLength
);
1719 buffer
.removeSuffix(destSuffixLength
);
1721 middle
.append(src
, (int32_t)(firstBoundaryInSrc
-src
));
1722 const UChar
*middleStart
=middle
.getBuffer();
1723 makeFCD(middleStart
, middleStart
+middle
.length(), &buffer
, errorCode
);
1724 if(U_FAILURE(errorCode
)) {
1727 src
=firstBoundaryInSrc
;
1731 makeFCD(src
, limit
, &buffer
, errorCode
);
1733 if(limit
==NULL
) { // appendZeroCC() needs limit!=NULL
1734 limit
=u_strchr(src
, 0);
1736 buffer
.appendZeroCC(src
, limit
, errorCode
);
1740 const UChar
*Normalizer2Impl::findPreviousFCDBoundary(const UChar
*start
, const UChar
*p
) const {
1741 while(start
<p
&& previousFCD16(start
, p
)>0xff) {}
1745 const UChar
*Normalizer2Impl::findNextFCDBoundary(const UChar
*p
, const UChar
*limit
) const {
1747 const UChar
*codePointStart
=p
;
1748 if(nextFCD16(p
, limit
)<=0xff) {
1749 return codePointStart
;
1755 // CanonicalIterator data -------------------------------------------------- ***
1757 CanonIterData::CanonIterData(UErrorCode
&errorCode
) :
1758 trie(utrie2_open(0, 0, &errorCode
)),
1759 canonStartSets(uprv_deleteUObject
, NULL
, errorCode
) {}
1761 CanonIterData::~CanonIterData() {
1765 void CanonIterData::addToStartSet(UChar32 origin
, UChar32 decompLead
, UErrorCode
&errorCode
) {
1766 uint32_t canonValue
=utrie2_get32(trie
, decompLead
);
1767 if((canonValue
&(CANON_HAS_SET
|CANON_VALUE_MASK
))==0 && origin
!=0) {
1768 // origin is the first character whose decomposition starts with
1769 // the character for which we are setting the value.
1770 utrie2_set32(trie
, decompLead
, canonValue
|origin
, &errorCode
);
1772 // origin is not the first character, or it is U+0000.
1774 if((canonValue
&CANON_HAS_SET
)==0) {
1777 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
1780 UChar32 firstOrigin
=(UChar32
)(canonValue
&CANON_VALUE_MASK
);
1781 canonValue
=(canonValue
&~CANON_VALUE_MASK
)|CANON_HAS_SET
|(uint32_t)canonStartSets
.size();
1782 utrie2_set32(trie
, decompLead
, canonValue
, &errorCode
);
1783 canonStartSets
.addElement(set
, errorCode
);
1784 if(firstOrigin
!=0) {
1785 set
->add(firstOrigin
);
1788 set
=(UnicodeSet
*)canonStartSets
[(int32_t)(canonValue
&CANON_VALUE_MASK
)];
1794 class CanonIterDataSingleton
{
1796 CanonIterDataSingleton(SimpleSingleton
&s
, Normalizer2Impl
&ni
, UErrorCode
&ec
) :
1797 singleton(s
), impl(ni
), errorCode(ec
) {}
1798 CanonIterData
*getInstance(UErrorCode
&errorCode
) {
1800 CanonIterData
*instance
=
1801 (CanonIterData
*)singleton
.getInstance(createInstance
, this, duplicate
, errorCode
);
1802 delete (CanonIterData
*)duplicate
;
1805 static void *createInstance(const void *context
, UErrorCode
&errorCode
);
1806 UBool
rangeHandler(UChar32 start
, UChar32 end
, uint32_t value
) {
1808 impl
.makeCanonIterDataFromNorm16(start
, end
, (uint16_t)value
, *newData
, errorCode
);
1810 return U_SUCCESS(errorCode
);
1814 SimpleSingleton
&singleton
;
1815 Normalizer2Impl
&impl
;
1816 CanonIterData
*newData
;
1817 UErrorCode
&errorCode
;
1822 // Call Normalizer2Impl::makeCanonIterDataFromNorm16() for a range of same-norm16 characters.
1823 static UBool U_CALLCONV
1824 enumCIDRangeHandler(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
1825 return ((CanonIterDataSingleton
*)context
)->rangeHandler(start
, end
, value
);
1830 void *CanonIterDataSingleton::createInstance(const void *context
, UErrorCode
&errorCode
) {
1831 CanonIterDataSingleton
*me
=(CanonIterDataSingleton
*)context
;
1832 me
->newData
=new CanonIterData(errorCode
);
1833 if(me
->newData
==NULL
) {
1834 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
1837 if(U_SUCCESS(errorCode
)) {
1838 utrie2_enum(me
->impl
.getNormTrie(), NULL
, enumCIDRangeHandler
, me
);
1839 utrie2_freeze(me
->newData
->trie
, UTRIE2_32_VALUE_BITS
, &errorCode
);
1840 if(U_SUCCESS(errorCode
)) {
1848 void Normalizer2Impl::makeCanonIterDataFromNorm16(UChar32 start
, UChar32 end
, uint16_t norm16
,
1849 CanonIterData
&newData
,
1850 UErrorCode
&errorCode
) const {
1851 if(norm16
==0 || (minYesNo
<=norm16
&& norm16
<minNoNo
)) {
1852 // Inert, or 2-way mapping (including Hangul syllable).
1853 // We do not write a canonStartSet for any yesNo character.
1854 // Composites from 2-way mappings are added at runtime from the
1855 // starter's compositions list, and the other characters in
1856 // 2-way mappings get CANON_NOT_SEGMENT_STARTER set because they are
1857 // "maybe" characters.
1860 for(UChar32 c
=start
; c
<=end
; ++c
) {
1861 uint32_t oldValue
=utrie2_get32(newData
.trie
, c
);
1862 uint32_t newValue
=oldValue
;
1863 if(norm16
>=minMaybeYes
) {
1864 // not a segment starter if it occurs in a decomposition or has cc!=0
1865 newValue
|=CANON_NOT_SEGMENT_STARTER
;
1866 if(norm16
<MIN_NORMAL_MAYBE_YES
) {
1867 newValue
|=CANON_HAS_COMPOSITIONS
;
1869 } else if(norm16
<minYesNo
) {
1870 newValue
|=CANON_HAS_COMPOSITIONS
;
1872 // c has a one-way decomposition
1874 uint16_t norm16_2
=norm16
;
1875 while(limitNoNo
<=norm16_2
&& norm16_2
<minMaybeYes
) {
1876 c2
=mapAlgorithmic(c2
, norm16_2
);
1877 norm16_2
=getNorm16(c2
);
1879 if(minYesNo
<=norm16_2
&& norm16_2
<limitNoNo
) {
1880 // c decomposes, get everything from the variable-length extra data
1881 const uint16_t *mapping
=getMapping(norm16_2
);
1882 uint16_t firstUnit
=*mapping
;
1883 int32_t length
=firstUnit
&MAPPING_LENGTH_MASK
;
1884 if((firstUnit
&MAPPING_HAS_CCC_LCCC_WORD
)!=0) {
1885 if(c
==c2
&& (*(mapping
-1)&0xff)!=0) {
1886 newValue
|=CANON_NOT_SEGMENT_STARTER
; // original c has cc!=0
1889 // Skip empty mappings (no characters in the decomposition).
1891 ++mapping
; // skip over the firstUnit
1892 // add c to first code point's start set
1894 U16_NEXT_UNSAFE(mapping
, i
, c2
);
1895 newData
.addToStartSet(c
, c2
, errorCode
);
1896 // Set CANON_NOT_SEGMENT_STARTER for each remaining code point of a
1897 // one-way mapping. A 2-way mapping is possible here after
1898 // intermediate algorithmic mapping.
1899 if(norm16_2
>=minNoNo
) {
1901 U16_NEXT_UNSAFE(mapping
, i
, c2
);
1902 uint32_t c2Value
=utrie2_get32(newData
.trie
, c2
);
1903 if((c2Value
&CANON_NOT_SEGMENT_STARTER
)==0) {
1904 utrie2_set32(newData
.trie
, c2
, c2Value
|CANON_NOT_SEGMENT_STARTER
,
1911 // c decomposed to c2 algorithmically; c has cc==0
1912 newData
.addToStartSet(c
, c2
, errorCode
);
1915 if(newValue
!=oldValue
) {
1916 utrie2_set32(newData
.trie
, c
, newValue
, &errorCode
);
1921 UBool
Normalizer2Impl::ensureCanonIterData(UErrorCode
&errorCode
) const {
1922 // Logically const: Synchronized instantiation.
1923 Normalizer2Impl
*me
=const_cast<Normalizer2Impl
*>(this);
1924 CanonIterDataSingleton(me
->canonIterDataSingleton
, *me
, errorCode
).getInstance(errorCode
);
1925 return U_SUCCESS(errorCode
);
1928 int32_t Normalizer2Impl::getCanonValue(UChar32 c
) const {
1929 return (int32_t)utrie2_get32(((CanonIterData
*)canonIterDataSingleton
.fInstance
)->trie
, c
);
1932 const UnicodeSet
&Normalizer2Impl::getCanonStartSet(int32_t n
) const {
1933 return *(const UnicodeSet
*)(
1934 ((CanonIterData
*)canonIterDataSingleton
.fInstance
)->canonStartSets
[n
]);
1937 UBool
Normalizer2Impl::isCanonSegmentStarter(UChar32 c
) const {
1938 return getCanonValue(c
)>=0;
1941 UBool
Normalizer2Impl::getCanonStartSet(UChar32 c
, UnicodeSet
&set
) const {
1942 int32_t canonValue
=getCanonValue(c
)&~CANON_NOT_SEGMENT_STARTER
;
1947 int32_t value
=canonValue
&CANON_VALUE_MASK
;
1948 if((canonValue
&CANON_HAS_SET
)!=0) {
1949 set
.addAll(getCanonStartSet(value
));
1950 } else if(value
!=0) {
1953 if((canonValue
&CANON_HAS_COMPOSITIONS
)!=0) {
1954 uint16_t norm16
=getNorm16(c
);
1955 if(norm16
==JAMO_L
) {
1957 (UChar32
)(Hangul::HANGUL_BASE
+(c
-Hangul::JAMO_L_BASE
)*Hangul::JAMO_VT_COUNT
);
1958 set
.add(syllable
, syllable
+Hangul::JAMO_VT_COUNT
-1);
1960 addComposites(getCompositionsList(norm16
), set
);
1968 // Normalizer2 data swapping ----------------------------------------------- ***
1972 U_CAPI
int32_t U_EXPORT2
1973 unorm2_swap(const UDataSwapper
*ds
,
1974 const void *inData
, int32_t length
, void *outData
,
1975 UErrorCode
*pErrorCode
) {
1976 const UDataInfo
*pInfo
;
1979 const uint8_t *inBytes
;
1982 const int32_t *inIndexes
;
1983 int32_t indexes
[Normalizer2Impl::IX_MIN_MAYBE_YES
+1];
1985 int32_t i
, offset
, nextOffset
, size
;
1987 /* udata_swapDataHeader checks the arguments */
1988 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1989 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1993 /* check data format and format version */
1994 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1996 pInfo
->dataFormat
[0]==0x4e && /* dataFormat="Nrm2" */
1997 pInfo
->dataFormat
[1]==0x72 &&
1998 pInfo
->dataFormat
[2]==0x6d &&
1999 pInfo
->dataFormat
[3]==0x32 &&
2000 (pInfo
->formatVersion
[0]==1 || pInfo
->formatVersion
[0]==2)
2002 udata_printError(ds
, "unorm2_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as Normalizer2 data\n",
2003 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
2004 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
2005 pInfo
->formatVersion
[0]);
2006 *pErrorCode
=U_UNSUPPORTED_ERROR
;
2010 inBytes
=(const uint8_t *)inData
+headerSize
;
2011 outBytes
=(uint8_t *)outData
+headerSize
;
2013 inIndexes
=(const int32_t *)inBytes
;
2017 if(length
<(int32_t)sizeof(indexes
)) {
2018 udata_printError(ds
, "unorm2_swap(): too few bytes (%d after header) for Normalizer2 data\n",
2020 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2025 /* read the first few indexes */
2026 for(i
=0; i
<=Normalizer2Impl::IX_MIN_MAYBE_YES
; ++i
) {
2027 indexes
[i
]=udata_readInt32(ds
, inIndexes
[i
]);
2030 /* get the total length of the data */
2031 size
=indexes
[Normalizer2Impl::IX_TOTAL_SIZE
];
2035 udata_printError(ds
, "unorm2_swap(): too few bytes (%d after header) for all of Normalizer2 data\n",
2037 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2041 /* copy the data for inaccessible bytes */
2042 if(inBytes
!=outBytes
) {
2043 uprv_memcpy(outBytes
, inBytes
, size
);
2048 /* swap the int32_t indexes[] */
2049 nextOffset
=indexes
[Normalizer2Impl::IX_NORM_TRIE_OFFSET
];
2050 ds
->swapArray32(ds
, inBytes
, nextOffset
-offset
, outBytes
, pErrorCode
);
2053 /* swap the UTrie2 */
2054 nextOffset
=indexes
[Normalizer2Impl::IX_EXTRA_DATA_OFFSET
];
2055 utrie2_swap(ds
, inBytes
+offset
, nextOffset
-offset
, outBytes
+offset
, pErrorCode
);
2058 /* swap the uint16_t extraData[] */
2059 nextOffset
=indexes
[Normalizer2Impl::IX_SMALL_FCD_OFFSET
];
2060 ds
->swapArray16(ds
, inBytes
+offset
, nextOffset
-offset
, outBytes
+offset
, pErrorCode
);
2063 /* no need to swap the uint8_t smallFCD[] (new in formatVersion 2) */
2064 nextOffset
=indexes
[Normalizer2Impl::IX_SMALL_FCD_OFFSET
+1];
2067 U_ASSERT(offset
==size
);
2070 return headerSize
+size
;
2073 #endif // !UCONFIG_NO_NORMALIZATION