2 *******************************************************************************
4 * Copyright (C) 2001-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: unormcmp.cpp
10 * tab size: 8 (not used)
13 * created on: 2004sep13
14 * created by: Markus W. Scherer
16 * unorm_compare() function moved here from unorm.cpp for better modularization.
17 * Depends on both normalization and case folding.
18 * Allows unorm.cpp to not depend on any character properties code.
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_NORMALIZATION
25 #include "unicode/ustring.h"
26 #include "unicode/unorm.h"
27 #include "unicode/uniset.h"
32 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
34 /* compare canonically equivalent ------------------------------------------- */
37 * Compare two strings for canonical equivalence.
38 * Further options include case-insensitive comparison and
39 * code point order (as opposed to code unit order).
41 * In this function, canonical equivalence is optional as well.
42 * If canonical equivalence is tested, then both strings must fulfill
45 * Semantically, this is equivalent to
46 * strcmp[CodePointOrder](NFD(foldCase(s1)), NFD(foldCase(s2)))
47 * where code point order, NFD and foldCase are all optional.
49 * String comparisons almost always yield results before processing both strings
51 * They are generally more efficient working incrementally instead of
52 * performing the sub-processing (strlen, normalization, case-folding)
53 * on the entire strings first.
55 * It is also unnecessary to not normalize identical characters.
57 * This function works in principle as follows:
60 * get one code unit c1 from s1 (-1 if end of source)
61 * get one code unit c2 from s2 (-1 if end of source)
63 * if(either string finished) {
71 * try to decompose/case-fold c1/c2, and continue if one does;
73 * // still c1!=c2 and neither decomposes/case-folds, return result
77 * When a character decomposes, then the pointer for that source changes to
78 * the decomposition, pushing the previous pointer onto a stack.
79 * When the end of the decomposition is reached, then the code unit reader
80 * pops the previous source from the stack.
81 * (Same for case-folding.)
83 * This is complicated further by operating on variable-width UTF-16.
84 * The top part of the loop works on code units, while lookups for decomposition
85 * and case-folding need code points.
86 * Code points are assembled after the equality/end-of-source part.
87 * The source pointer is only advanced beyond all code units when the code point
88 * actually decomposes/case-folds.
90 * If we were on a trail surrogate unit when assembling a code point,
91 * and the code point decomposes/case-folds, then the decomposition/folding
92 * result must be compared with the part of the other string that corresponds to
93 * this string's lead surrogate.
94 * Since we only assemble a code point when hitting a trail unit when the
95 * preceding lead units were identical, we back up the other string by one unit
98 * The optional code point order comparison at the end works with
99 * the same fix-up as the other code point order comparison functions.
100 * See ustring.c and the comment near the end of this function.
102 * Assumption: A decomposition or case-folding result string never contains
103 * a single surrogate. This is a safe assumption in the Unicode Standard.
104 * Therefore, we do not need to check for surrogate pairs across
105 * decomposition/case-folding boundaries.
107 * Further assumptions (see verifications tstnorm.cpp):
108 * The API function checks for FCD first, while the core function
109 * first case-folds and then decomposes. This requires that case-folding does not
110 * un-FCD any strings.
112 * The API function may also NFD the input and turn off decomposition.
113 * This requires that case-folding does not un-NFD strings either.
115 * TODO If any of the above two assumptions is violated,
116 * then this entire code must be re-thought.
117 * If this happens, then a simple solution is to case-fold both strings up front
118 * and to turn off UNORM_INPUT_IS_FCD.
119 * We already do this when not both strings are in FCD because makeFCD
120 * would be a partial NFD before the case folding, which does not work.
121 * Note that all of this is only a problem when case-folding _and_
122 * canonical equivalence come together.
123 * (Comments in unorm_compare() are more up to date than this TODO.)
125 * This function could be moved to a different source file, at increased cost
126 * for calling the decomposition access function.
129 /* stack element for previous-level source/decomposition pointers */
130 struct CmpEquivLevel
{
131 const UChar
*start
, *s
, *limit
;
133 typedef struct CmpEquivLevel CmpEquivLevel
;
135 /* internal function */
137 unorm_cmpEquivFold(const UChar
*s1
, int32_t length1
,
138 const UChar
*s2
, int32_t length2
,
140 UErrorCode
*pErrorCode
) {
143 /* current-level start/limit - s1/s2 as current */
144 const UChar
*start1
, *start2
, *limit1
, *limit2
;
146 /* decomposition and case folding variables */
150 /* stacks of previous-level start/current/limit */
151 CmpEquivLevel stack1
[2], stack2
[2];
153 /* decomposition buffers for Hangul */
154 UChar decomp1
[4], decomp2
[4];
156 /* case folding buffers, only use current-level start/limit */
157 UChar fold1
[UCASE_MAX_STRING_LENGTH
+1], fold2
[UCASE_MAX_STRING_LENGTH
+1];
159 /* track which is the current level per string */
160 int32_t level1
, level2
;
162 /* current code units, and code points for lookups */
163 UChar32 c1
, c2
, cp1
, cp2
;
165 /* no argument error checking because this itself is not an API */
168 * assume that at least one of the options _COMPARE_EQUIV and U_COMPARE_IGNORE_CASE is set
169 * otherwise this function must behave exactly as uprv_strCompare()
170 * not checking for that here makes testing this function easier
173 /* normalization/properties data loaded? */
174 if( ((options
&_COMPARE_EQUIV
)!=0 && !unorm_haveData(pErrorCode
)) ||
175 U_FAILURE(*pErrorCode
)
179 if((options
&U_COMPARE_IGNORE_CASE
)!=0) {
180 csp
=ucase_getSingleton(pErrorCode
);
181 if(U_FAILURE(*pErrorCode
)) {
206 /* comparison loop */
209 * here a code unit value of -1 means "get another code unit"
210 * below it will mean "this source is finished"
214 /* get next code unit from string 1, post-increment */
216 if(s1
==limit1
|| ((c1
=*s1
)==0 && (limit1
==NULL
|| (options
&_STRNCMP_STYLE
)))) {
226 /* reached end of level buffer, pop one level */
229 start1
=stack1
[level1
].start
;
230 } while(start1
==NULL
);
232 limit1
=stack1
[level1
].limit
;
237 /* get next code unit from string 2, post-increment */
239 if(s2
==limit2
|| ((c2
=*s2
)==0 && (limit2
==NULL
|| (options
&_STRNCMP_STYLE
)))) {
249 /* reached end of level buffer, pop one level */
252 start2
=stack2
[level2
].start
;
253 } while(start2
==NULL
);
255 limit2
=stack2
[level2
].limit
;
261 * either variable c1, c2 is -1 only if the corresponding string is finished
265 return 0; /* c1==c2==-1 indicating end of strings */
267 c1
=c2
=-1; /* make us fetch new code units */
270 return -1; /* string 1 ends before string 2 */
272 return 1; /* string 2 ends before string 1 */
274 /* c1!=c2 && c1>=0 && c2>=0 */
276 /* get complete code points for c1, c2 for lookups if either is a surrogate */
278 if(U_IS_SURROGATE(c1
)) {
281 if(U_IS_SURROGATE_LEAD(c1
)) {
282 if(s1
!=limit1
&& U16_IS_TRAIL(c
=*s1
)) {
283 /* advance ++s1; only below if cp1 decomposes/case-folds */
284 cp1
=U16_GET_SUPPLEMENTARY(c1
, c
);
286 } else /* isTrail(c1) */ {
287 if(start1
<=(s1
-2) && U16_IS_LEAD(c
=*(s1
-2))) {
288 cp1
=U16_GET_SUPPLEMENTARY(c
, c1
);
294 if(U_IS_SURROGATE(c2
)) {
297 if(U_IS_SURROGATE_LEAD(c2
)) {
298 if(s2
!=limit2
&& U16_IS_TRAIL(c
=*s2
)) {
299 /* advance ++s2; only below if cp2 decomposes/case-folds */
300 cp2
=U16_GET_SUPPLEMENTARY(c2
, c
);
302 } else /* isTrail(c2) */ {
303 if(start2
<=(s2
-2) && U16_IS_LEAD(c
=*(s2
-2))) {
304 cp2
=U16_GET_SUPPLEMENTARY(c
, c2
);
310 * go down one level for each string
311 * continue with the main loop as soon as there is a real change
314 if( level1
==0 && (options
&U_COMPARE_IGNORE_CASE
) &&
315 (length
=ucase_toFullFolding(csp
, (UChar32
)cp1
, &p
, options
))>=0
317 /* cp1 case-folds to the code point "length" or to p[length] */
318 if(U_IS_SURROGATE(c1
)) {
319 if(U_IS_SURROGATE_LEAD(c1
)) {
320 /* advance beyond source surrogate pair if it case-folds */
322 } else /* isTrail(c1) */ {
324 * we got a supplementary code point when hitting its trail surrogate,
325 * therefore the lead surrogate must have been the same as in the other string;
326 * compare this decomposition with the lead surrogate in the other string
327 * remember that this simulates bulk text replacement:
328 * the decomposition would replace the entire code point
335 /* push current level pointers */
336 stack1
[0].start
=start1
;
338 stack1
[0].limit
=limit1
;
341 /* copy the folding result to fold1[] */
342 if(length
<=UCASE_MAX_STRING_LENGTH
) {
343 u_memcpy(fold1
, p
, length
);
346 U16_APPEND_UNSAFE(fold1
, i
, length
);
350 /* set next level pointers to case folding */
354 /* get ready to read from decomposition, continue with loop */
359 if( level2
==0 && (options
&U_COMPARE_IGNORE_CASE
) &&
360 (length
=ucase_toFullFolding(csp
, (UChar32
)cp2
, &p
, options
))>=0
362 /* cp2 case-folds to the code point "length" or to p[length] */
363 if(U_IS_SURROGATE(c2
)) {
364 if(U_IS_SURROGATE_LEAD(c2
)) {
365 /* advance beyond source surrogate pair if it case-folds */
367 } else /* isTrail(c2) */ {
369 * we got a supplementary code point when hitting its trail surrogate,
370 * therefore the lead surrogate must have been the same as in the other string;
371 * compare this decomposition with the lead surrogate in the other string
372 * remember that this simulates bulk text replacement:
373 * the decomposition would replace the entire code point
380 /* push current level pointers */
381 stack2
[0].start
=start2
;
383 stack2
[0].limit
=limit2
;
386 /* copy the folding result to fold2[] */
387 if(length
<=UCASE_MAX_STRING_LENGTH
) {
388 u_memcpy(fold2
, p
, length
);
391 U16_APPEND_UNSAFE(fold2
, i
, length
);
395 /* set next level pointers to case folding */
399 /* get ready to read from decomposition, continue with loop */
404 if( level1
<2 && (options
&_COMPARE_EQUIV
) &&
405 0!=(p
=unorm_getCanonicalDecomposition((UChar32
)cp1
, decomp1
, &length
))
407 /* cp1 decomposes into p[length] */
408 if(U_IS_SURROGATE(c1
)) {
409 if(U_IS_SURROGATE_LEAD(c1
)) {
410 /* advance beyond source surrogate pair if it decomposes */
412 } else /* isTrail(c1) */ {
414 * we got a supplementary code point when hitting its trail surrogate,
415 * therefore the lead surrogate must have been the same as in the other string;
416 * compare this decomposition with the lead surrogate in the other string
417 * remember that this simulates bulk text replacement:
418 * the decomposition would replace the entire code point
425 /* push current level pointers */
426 stack1
[level1
].start
=start1
;
428 stack1
[level1
].limit
=limit1
;
431 /* set empty intermediate level if skipped */
433 stack1
[level1
++].start
=NULL
;
436 /* set next level pointers to decomposition */
440 /* get ready to read from decomposition, continue with loop */
445 if( level2
<2 && (options
&_COMPARE_EQUIV
) &&
446 0!=(p
=unorm_getCanonicalDecomposition((UChar32
)cp2
, decomp2
, &length
))
448 /* cp2 decomposes into p[length] */
449 if(U_IS_SURROGATE(c2
)) {
450 if(U_IS_SURROGATE_LEAD(c2
)) {
451 /* advance beyond source surrogate pair if it decomposes */
453 } else /* isTrail(c2) */ {
455 * we got a supplementary code point when hitting its trail surrogate,
456 * therefore the lead surrogate must have been the same as in the other string;
457 * compare this decomposition with the lead surrogate in the other string
458 * remember that this simulates bulk text replacement:
459 * the decomposition would replace the entire code point
466 /* push current level pointers */
467 stack2
[level2
].start
=start2
;
469 stack2
[level2
].limit
=limit2
;
472 /* set empty intermediate level if skipped */
474 stack2
[level2
++].start
=NULL
;
477 /* set next level pointers to decomposition */
481 /* get ready to read from decomposition, continue with loop */
487 * no decomposition/case folding, max level for both sides:
488 * return difference result
490 * code point order comparison must not just return cp1-cp2
491 * because when single surrogates are present then the surrogate pairs
492 * that formed cp1 and cp2 may be from different string indexes
494 * example: { d800 d800 dc01 } vs. { d800 dc00 }, compare at second code units
495 * c1=d800 cp1=10001 c2=dc00 cp2=10000
496 * cp1-cp2>0 but c1-c2<0 and in fact in UTF-32 it is { d800 10001 } < { 10000 }
498 * therefore, use same fix-up as in ustring.c/uprv_strCompare()
499 * except: uprv_strCompare() fetches c=*s while this functions fetches c=*s++
500 * so we have slightly different pointer/start/limit comparisons here
503 if(c1
>=0xd800 && c2
>=0xd800 && (options
&U_COMPARE_CODE_POINT_ORDER
)) {
504 /* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */
506 (c1
<=0xdbff && s1
!=limit1
&& U16_IS_TRAIL(*s1
)) ||
507 (U16_IS_TRAIL(c1
) && start1
!=(s1
-1) && U16_IS_LEAD(*(s1
-2)))
509 /* part of a surrogate pair, leave >=d800 */
511 /* BMP code point - may be surrogate code point - make <d800 */
516 (c2
<=0xdbff && s2
!=limit2
&& U16_IS_TRAIL(*s2
)) ||
517 (U16_IS_TRAIL(c2
) && start2
!=(s2
-1) && U16_IS_LEAD(*(s2
-2)))
519 /* part of a surrogate pair, leave >=d800 */
521 /* BMP code point - may be surrogate code point - make <d800 */
530 U_CAPI
int32_t U_EXPORT2
531 unorm_compare(const UChar
*s1
, int32_t length1
,
532 const UChar
*s2
, int32_t length2
,
534 UErrorCode
*pErrorCode
) {
535 UChar fcd1
[300], fcd2
[300];
537 const UnicodeSet
*nx
;
538 UNormalizationMode mode
;
542 /* argument checking */
543 if(pErrorCode
==0 || U_FAILURE(*pErrorCode
)) {
546 if(s1
==0 || length1
<-1 || s2
==0 || length2
<-1) {
547 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
551 if(!unorm_haveData(pErrorCode
)) {
554 if(!uprv_haveProperties(pErrorCode
)) {
558 normOptions
=(int32_t)(options
>>UNORM_COMPARE_NORM_OPTIONS_SHIFT
);
559 nx
=unorm_getNX(normOptions
, pErrorCode
);
560 if(U_FAILURE(*pErrorCode
)) {
565 options
|=_COMPARE_EQUIV
;
569 * UAX #21 Case Mappings, as fixed for Unicode version 4
570 * (see Jitterbug 2021), defines a canonical caseless match as
572 * A string X is a canonical caseless match
573 * for a string Y if and only if
574 * NFD(toCasefold(NFD(X))) = NFD(toCasefold(NFD(Y)))
576 * For better performance, we check for FCD (or let the caller tell us that
577 * both strings are in FCD) for the inner normalization.
578 * BasicNormalizerTest::FindFoldFCDExceptions() makes sure that
579 * case-folding preserves the FCD-ness of a string.
580 * The outer normalization is then only performed by unorm_cmpEquivFold()
581 * when there is a difference.
583 * Exception: When using the Turkic case-folding option, we do perform
584 * full NFD first. This is because in the Turkic case precomposed characters
585 * with 0049 capital I or 0069 small i fold differently whether they
586 * are first decomposed or not, so an FCD check - a check only for
587 * canonical order - is not sufficient.
589 if(options
&U_FOLD_CASE_EXCLUDE_SPECIAL_I
) {
591 options
&=~UNORM_INPUT_IS_FCD
;
596 if(!(options
&UNORM_INPUT_IS_FCD
)) {
597 int32_t _len1
, _len2
;
598 UBool isFCD1
, isFCD2
;
600 // check if s1 and/or s2 fulfill the FCD conditions
601 isFCD1
= UNORM_YES
==unorm_internalQuickCheck(s1
, length1
, mode
, TRUE
, nx
, pErrorCode
);
602 isFCD2
= UNORM_YES
==unorm_internalQuickCheck(s2
, length2
, mode
, TRUE
, nx
, pErrorCode
);
603 if(U_FAILURE(*pErrorCode
)) {
608 * ICU 2.4 had a further optimization:
609 * If both strings were not in FCD, then they were both NFD'ed,
610 * and the _COMPARE_EQUIV option was turned off.
611 * It is not entirely clear that this is valid with the current
612 * definition of the canonical caseless match.
613 * Therefore, ICU 2.6 removes that optimization.
617 _len1
=unorm_internalNormalizeWithNX(fcd1
, LENGTHOF(fcd1
),
619 mode
, normOptions
, nx
,
621 if(*pErrorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
624 d1
=(UChar
*)uprv_malloc(_len1
*U_SIZEOF_UCHAR
);
626 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
630 *pErrorCode
=U_ZERO_ERROR
;
631 _len1
=unorm_internalNormalizeWithNX(d1
, _len1
,
633 mode
, normOptions
, nx
,
635 if(U_FAILURE(*pErrorCode
)) {
645 _len2
=unorm_internalNormalizeWithNX(fcd2
, LENGTHOF(fcd2
),
647 mode
, normOptions
, nx
,
649 if(*pErrorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
652 d2
=(UChar
*)uprv_malloc(_len2
*U_SIZEOF_UCHAR
);
654 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
658 *pErrorCode
=U_ZERO_ERROR
;
659 _len2
=unorm_internalNormalizeWithNX(d2
, _len2
,
661 mode
, normOptions
, nx
,
663 if(U_FAILURE(*pErrorCode
)) {
673 if(U_SUCCESS(*pErrorCode
)) {
674 result
=unorm_cmpEquivFold(s1
, length1
, s2
, length2
, options
, pErrorCode
);
688 #endif /* #if !UCONFIG_NO_NORMALIZATION */