2 *******************************************************************************
4 * Copyright (C) 2001-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ustrcase.c
10 * tab size: 8 (not used)
13 * created on: 2002feb20
14 * created by: Markus W. Scherer
16 * Implementation file for string casing C API functions.
17 * Uses functions from uchar.c for basic functionality that requires access
18 * to the Unicode Character Database (uprops.dat).
21 #include "unicode/utypes.h"
22 #include "unicode/uloc.h"
23 #include "unicode/ustring.h"
24 #include "unicode/ucasemap.h"
25 #include "unicode/ubrk.h"
30 /* string casing ------------------------------------------------------------ */
32 /* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */
33 static U_INLINE int32_t
34 appendResult(UChar *dest, int32_t destIndex, int32_t destCapacity,
35 int32_t result, const UChar *s) {
39 /* decode the result */
41 /* (not) original code point */
44 } else if(result<=UCASE_MAX_STRING_LENGTH) {
52 if(destIndex<destCapacity) {
53 /* append the result */
57 U16_APPEND(dest, destIndex, destCapacity, c, isError);
59 /* overflow, nothing written */
60 destIndex+=U16_LENGTH(c);
64 if((destIndex+length)<=destCapacity) {
66 dest[destIndex++]=*s++;
77 destIndex+=U16_LENGTH(c);
85 static UChar32 U_CALLCONV
86 utf16_caseContextIterator(void *context, int8_t dir) {
87 UCaseContext *csc=(UCaseContext *)context;
91 /* reset for backward iteration */
92 csc->index=csc->cpStart;
95 /* reset for forward iteration */
96 csc->index=csc->cpLimit;
99 /* continue current iteration direction */
104 if(csc->start<csc->index) {
105 U16_PREV((const UChar *)csc->p, csc->start, csc->index, c);
109 if(csc->index<csc->limit) {
110 U16_NEXT((const UChar *)csc->p, csc->index, csc->limit, c);
118 * Case-maps [srcStart..srcLimit[ but takes
119 * context [0..srcLength[ into account.
122 _caseMap(const UCaseMap *csm, UCaseMapFull *map,
123 UChar *dest, int32_t destCapacity,
124 const UChar *src, UCaseContext *csc,
125 int32_t srcStart, int32_t srcLimit,
126 UErrorCode *pErrorCode) {
129 int32_t srcIndex, destIndex;
132 locCache=csm->locCache;
134 /* case mapping loop */
137 while(srcIndex<srcLimit) {
138 csc->cpStart=srcIndex;
139 U16_NEXT(src, srcIndex, srcLimit, c);
140 csc->cpLimit=srcIndex;
141 c=map(csm->csp, c, utf16_caseContextIterator, csc, &s, csm->locale, &locCache);
142 if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0xffff : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0xffff)) {
143 /* fast path version of appendResult() for BMP results */
144 dest[destIndex++]=(UChar)c2;
146 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
150 if(destIndex>destCapacity) {
151 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
157 setTempCaseMapLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) {
159 * We could call ucasemap_setLocale(), but here we really only care about
160 * the initial language subtag, we need not return the real string via
161 * ucasemap_getLocale(), and we don't care about only getting "x" from
162 * "x-some-thing" etc.
164 * We ignore locales with a longer-than-3 initial subtag.
166 * We also do not fill in the locCache because it is rarely used,
167 * and not worth setting unless we reuse it for many case mapping operations.
168 * (That's why UCaseMap was created.)
173 /* the internal functions require locale!=NULL */
175 locale=uloc_getDefault();
177 for(i=0; i<4 && (c=locale[i])!=0 && c!='-' && c!='_'; ++i) {
181 csm->locale[i]=0; /* Up to 3 non-separator characters. */
183 csm->locale[0]=0; /* Longer-than-3 initial subtag: Ignore. */
188 * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
189 * Do this fast because it is called with every function call.
192 setTempCaseMap(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) {
194 csm->csp=ucase_getSingleton();
196 if(locale!=NULL && locale[0]==0) {
199 setTempCaseMapLocale(csm, locale, pErrorCode);
203 #if !UCONFIG_NO_BREAK_ITERATION
206 * Internal titlecasing function.
209 _toTitle(UCaseMap *csm,
210 UChar *dest, int32_t destCapacity,
211 const UChar *src, UCaseContext *csc,
213 UErrorCode *pErrorCode) {
216 int32_t prev, titleStart, titleLimit, idx, destIndex, length;
219 if(csm->iter!=NULL) {
220 ubrk_setText(csm->iter, src, srcLength, pErrorCode);
222 csm->iter=ubrk_open(UBRK_WORD, csm->locale,
226 if(U_FAILURE(*pErrorCode)) {
230 /* set up local variables */
235 /* titlecasing loop */
236 while(prev<srcLength) {
237 /* find next index where to titlecase */
240 idx=ubrk_first(csm->iter);
242 idx=ubrk_next(csm->iter);
244 if(idx==UBRK_DONE || idx>srcLength) {
249 * Unicode 4 & 5 section 3.13 Default Case Operations:
251 * R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex
252 * #29, "Text Boundaries." Between each pair of word boundaries, find the first
253 * cased character F. If F exists, map F to default_title(F); then map each
254 * subsequent character C to default_lower(C).
256 * In this implementation, segment [prev..index[ into 3 parts:
257 * a) uncased characters (copy as-is) [prev..titleStart[
258 * b) first case letter (titlecase) [titleStart..titleLimit[
259 * c) subsequent characters (lowercase) [titleLimit..index[
262 /* find and copy uncased characters [prev..titleStart[ */
263 titleStart=titleLimit=prev;
264 U16_NEXT(src, titleLimit, idx, c);
265 if((csm->options&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0 && UCASE_NONE==ucase_getType(csm->csp, c)) {
266 /* Adjust the titlecasing index (titleStart) to the next cased character. */
268 titleStart=titleLimit;
269 if(titleLimit==idx) {
271 * only uncased characters in [prev..index[
272 * stop with titleStart==titleLimit==index
276 U16_NEXT(src, titleLimit, idx, c);
277 if(UCASE_NONE!=ucase_getType(csm->csp, c)) {
278 break; /* cased letter at [titleStart..titleLimit[ */
281 length=titleStart-prev;
283 if((destIndex+length)<=destCapacity) {
284 uprv_memcpy(dest+destIndex, src+prev, length*U_SIZEOF_UCHAR);
290 if(titleStart<titleLimit) {
291 /* titlecase c which is from [titleStart..titleLimit[ */
292 csc->cpStart=titleStart;
293 csc->cpLimit=titleLimit;
294 c=ucase_toFullTitle(csm->csp, c, utf16_caseContextIterator, csc, &s, csm->locale, &csm->locCache);
295 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
297 /* Special case Dutch IJ titlecasing */
298 if ( titleStart+1 < idx &&
299 ucase_getCaseLocale(csm->locale,&csm->locCache) == UCASE_LOC_DUTCH &&
300 ( src[titleStart] == (UChar32) 0x0049 || src[titleStart] == (UChar32) 0x0069 ) &&
301 ( src[titleStart+1] == (UChar32) 0x004A || src[titleStart+1] == (UChar32) 0x006A )) {
303 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
307 /* lowercase [titleLimit..index[ */
309 if((csm->options&U_TITLECASE_NO_LOWERCASE)==0) {
310 /* Normal operation: Lowercase the rest of the word. */
313 csm, ucase_toFullLower,
314 dest+destIndex, destCapacity-destIndex,
319 /* Optionally just copy the rest of the word unchanged. */
320 length=idx-titleLimit;
321 if((destIndex+length)<=destCapacity) {
322 uprv_memcpy(dest+destIndex, src+titleLimit, length*U_SIZEOF_UCHAR);
333 if(destIndex>destCapacity) {
334 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
341 /* functions available in the common library (for unistr_case.cpp) */
344 ustr_toLower(const UCaseProps *csp,
345 UChar *dest, int32_t destCapacity,
346 const UChar *src, int32_t srcLength,
348 UErrorCode *pErrorCode) {
349 UCaseMap csm={ NULL };
350 UCaseContext csc={ NULL };
353 setTempCaseMap(&csm, locale, pErrorCode);
357 return _caseMap(&csm, ucase_toFullLower,
359 src, &csc, 0, srcLength,
364 ustr_toUpper(const UCaseProps *csp,
365 UChar *dest, int32_t destCapacity,
366 const UChar *src, int32_t srcLength,
368 UErrorCode *pErrorCode) {
369 UCaseMap csm={ NULL };
370 UCaseContext csc={ NULL };
373 setTempCaseMap(&csm, locale, pErrorCode);
377 return _caseMap(&csm, ucase_toFullUpper,
379 src, &csc, 0, srcLength,
383 #if !UCONFIG_NO_BREAK_ITERATION
386 ustr_toTitle(const UCaseProps *csp,
387 UChar *dest, int32_t destCapacity,
388 const UChar *src, int32_t srcLength,
389 UBreakIterator *titleIter,
390 const char *locale, uint32_t options,
391 UErrorCode *pErrorCode) {
392 UCaseMap csm={ NULL };
393 UCaseContext csc={ NULL };
399 setTempCaseMap(&csm, locale, pErrorCode);
403 length=_toTitle(&csm,
405 src, &csc, srcLength,
407 if(titleIter==NULL && csm.iter!=NULL) {
408 ubrk_close(csm.iter);
416 ustr_foldCase(const UCaseProps *csp,
417 UChar *dest, int32_t destCapacity,
418 const UChar *src, int32_t srcLength,
420 UErrorCode *pErrorCode) {
421 int32_t srcIndex, destIndex;
426 /* case mapping loop */
427 srcIndex=destIndex=0;
428 while(srcIndex<srcLength) {
429 U16_NEXT(src, srcIndex, srcLength, c);
430 c=ucase_toFullFolding(csp, c, &s, options);
431 if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0xffff : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0xffff)) {
432 /* fast path version of appendResult() for BMP results */
433 dest[destIndex++]=(UChar)c2;
435 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
439 if(destIndex>destCapacity) {
440 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
446 * Implement argument checking and buffer handling
447 * for string case mapping as a common function.
450 /* common internal function for public API functions */
453 caseMap(const UCaseMap *csm,
454 UChar *dest, int32_t destCapacity,
455 const UChar *src, int32_t srcLength,
457 UErrorCode *pErrorCode) {
463 /* check argument values */
464 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
467 if( destCapacity<0 ||
468 (dest==NULL && destCapacity>0) ||
472 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
476 /* get the string length */
478 srcLength=u_strlen(src);
481 /* check for overlapping source and destination */
483 ((src>=dest && src<(dest+destCapacity)) ||
484 (dest>=src && dest<(src+srcLength)))
486 /* overlap: provide a temporary destination buffer and later copy the result */
487 if(destCapacity<=(sizeof(buffer)/U_SIZEOF_UCHAR)) {
488 /* the stack buffer is large enough */
491 /* allocate a buffer */
492 temp=(UChar *)uprv_malloc(destCapacity*U_SIZEOF_UCHAR);
494 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
504 if(toWhichCase==FOLD_CASE) {
505 destLength=ustr_foldCase(csm->csp, temp, destCapacity, src, srcLength,
506 csm->options, pErrorCode);
508 UCaseContext csc={ NULL };
513 if(toWhichCase==TO_LOWER) {
514 destLength=_caseMap(csm, ucase_toFullLower,
519 } else if(toWhichCase==TO_UPPER) {
520 destLength=_caseMap(csm, ucase_toFullUpper,
525 } else /* if(toWhichCase==TO_TITLE) */ {
526 #if UCONFIG_NO_BREAK_ITERATION
527 *pErrorCode=U_UNSUPPORTED_ERROR;
529 /* UCaseMap is actually non-const in toTitle() APIs. */
530 destLength=_toTitle((UCaseMap *)csm, temp, destCapacity,
531 src, &csc, srcLength,
537 /* copy the result string to the destination buffer */
539 int32_t copyLength= destLength<=destCapacity ? destLength : destCapacity;
541 uprv_memmove(dest, temp, copyLength*U_SIZEOF_UCHAR);
549 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
552 /* public API functions */
554 U_CAPI int32_t U_EXPORT2
555 u_strToLower(UChar *dest, int32_t destCapacity,
556 const UChar *src, int32_t srcLength,
558 UErrorCode *pErrorCode) {
559 UCaseMap csm={ NULL };
560 setTempCaseMap(&csm, locale, pErrorCode);
564 TO_LOWER, pErrorCode);
567 U_CAPI int32_t U_EXPORT2
568 u_strToUpper(UChar *dest, int32_t destCapacity,
569 const UChar *src, int32_t srcLength,
571 UErrorCode *pErrorCode) {
572 UCaseMap csm={ NULL };
573 setTempCaseMap(&csm, locale, pErrorCode);
577 TO_UPPER, pErrorCode);
580 #if !UCONFIG_NO_BREAK_ITERATION
582 U_CAPI int32_t U_EXPORT2
583 u_strToTitle(UChar *dest, int32_t destCapacity,
584 const UChar *src, int32_t srcLength,
585 UBreakIterator *titleIter,
587 UErrorCode *pErrorCode) {
588 UCaseMap csm={ NULL };
592 setTempCaseMap(&csm, locale, pErrorCode);
596 TO_TITLE, pErrorCode);
597 if(titleIter==NULL && csm.iter!=NULL) {
598 ubrk_close(csm.iter);
603 U_CAPI int32_t U_EXPORT2
604 ucasemap_toTitle(UCaseMap *csm,
605 UChar *dest, int32_t destCapacity,
606 const UChar *src, int32_t srcLength,
607 UErrorCode *pErrorCode) {
611 TO_TITLE, pErrorCode);
616 U_CAPI int32_t U_EXPORT2
617 u_strFoldCase(UChar *dest, int32_t destCapacity,
618 const UChar *src, int32_t srcLength,
620 UErrorCode *pErrorCode) {
621 UCaseMap csm={ NULL };
622 csm.csp=ucase_getSingleton();
627 FOLD_CASE, pErrorCode);
630 /* case-insensitive string comparisons -------------------------------------- */
633 * This function is a copy of unorm_cmpEquivFold() minus the parts for
634 * canonical equivalence.
635 * Keep the functions in sync, and see there for how this works.
636 * The duplication is for modularization:
637 * It makes caseless (but not canonical caseless) matches independent of
638 * the normalization code.
641 /* stack element for previous-level source/decomposition pointers */
642 struct CmpEquivLevel {
643 const UChar *start, *s, *limit;
645 typedef struct CmpEquivLevel CmpEquivLevel;
647 /* internal function */
649 u_strcmpFold(const UChar *s1, int32_t length1,
650 const UChar *s2, int32_t length2,
652 UErrorCode *pErrorCode) {
653 const UCaseProps *csp;
655 /* current-level start/limit - s1/s2 as current */
656 const UChar *start1, *start2, *limit1, *limit2;
658 /* case folding variables */
662 /* stacks of previous-level start/current/limit */
663 CmpEquivLevel stack1[2], stack2[2];
665 /* case folding buffers, only use current-level start/limit */
666 UChar fold1[UCASE_MAX_STRING_LENGTH+1], fold2[UCASE_MAX_STRING_LENGTH+1];
668 /* track which is the current level per string */
669 int32_t level1, level2;
671 /* current code units, and code points for lookups */
672 UChar32 c1, c2, cp1, cp2;
674 /* no argument error checking because this itself is not an API */
677 * assume that at least the option U_COMPARE_IGNORE_CASE is set
678 * otherwise this function would have to behave exactly as uprv_strCompare()
680 csp=ucase_getSingleton();
681 if(U_FAILURE(*pErrorCode)) {
703 /* comparison loop */
706 * here a code unit value of -1 means "get another code unit"
707 * below it will mean "this source is finished"
711 /* get next code unit from string 1, post-increment */
713 if(s1==limit1 || ((c1=*s1)==0 && (limit1==NULL || (options&_STRNCMP_STYLE)))) {
723 /* reached end of level buffer, pop one level */
726 start1=stack1[level1].start;
727 } while(start1==NULL);
729 limit1=stack1[level1].limit;
734 /* get next code unit from string 2, post-increment */
736 if(s2==limit2 || ((c2=*s2)==0 && (limit2==NULL || (options&_STRNCMP_STYLE)))) {
746 /* reached end of level buffer, pop one level */
749 start2=stack2[level2].start;
750 } while(start2==NULL);
752 limit2=stack2[level2].limit;
758 * either variable c1, c2 is -1 only if the corresponding string is finished
762 return 0; /* c1==c2==-1 indicating end of strings */
764 c1=c2=-1; /* make us fetch new code units */
767 return -1; /* string 1 ends before string 2 */
769 return 1; /* string 2 ends before string 1 */
771 /* c1!=c2 && c1>=0 && c2>=0 */
773 /* get complete code points for c1, c2 for lookups if either is a surrogate */
775 if(U_IS_SURROGATE(c1)) {
778 if(U_IS_SURROGATE_LEAD(c1)) {
779 if(s1!=limit1 && U16_IS_TRAIL(c=*s1)) {
780 /* advance ++s1; only below if cp1 decomposes/case-folds */
781 cp1=U16_GET_SUPPLEMENTARY(c1, c);
783 } else /* isTrail(c1) */ {
784 if(start1<=(s1-2) && U16_IS_LEAD(c=*(s1-2))) {
785 cp1=U16_GET_SUPPLEMENTARY(c, c1);
791 if(U_IS_SURROGATE(c2)) {
794 if(U_IS_SURROGATE_LEAD(c2)) {
795 if(s2!=limit2 && U16_IS_TRAIL(c=*s2)) {
796 /* advance ++s2; only below if cp2 decomposes/case-folds */
797 cp2=U16_GET_SUPPLEMENTARY(c2, c);
799 } else /* isTrail(c2) */ {
800 if(start2<=(s2-2) && U16_IS_LEAD(c=*(s2-2))) {
801 cp2=U16_GET_SUPPLEMENTARY(c, c2);
807 * go down one level for each string
808 * continue with the main loop as soon as there is a real change
812 (length=ucase_toFullFolding(csp, (UChar32)cp1, &p, options))>=0
814 /* cp1 case-folds to the code point "length" or to p[length] */
815 if(U_IS_SURROGATE(c1)) {
816 if(U_IS_SURROGATE_LEAD(c1)) {
817 /* advance beyond source surrogate pair if it case-folds */
819 } else /* isTrail(c1) */ {
821 * we got a supplementary code point when hitting its trail surrogate,
822 * therefore the lead surrogate must have been the same as in the other string;
823 * compare this decomposition with the lead surrogate in the other string
824 * remember that this simulates bulk text replacement:
825 * the decomposition would replace the entire code point
832 /* push current level pointers */
833 stack1[0].start=start1;
835 stack1[0].limit=limit1;
838 /* copy the folding result to fold1[] */
839 if(length<=UCASE_MAX_STRING_LENGTH) {
840 u_memcpy(fold1, p, length);
843 U16_APPEND_UNSAFE(fold1, i, length);
847 /* set next level pointers to case folding */
851 /* get ready to read from decomposition, continue with loop */
857 (length=ucase_toFullFolding(csp, (UChar32)cp2, &p, options))>=0
859 /* cp2 case-folds to the code point "length" or to p[length] */
860 if(U_IS_SURROGATE(c2)) {
861 if(U_IS_SURROGATE_LEAD(c2)) {
862 /* advance beyond source surrogate pair if it case-folds */
864 } else /* isTrail(c2) */ {
866 * we got a supplementary code point when hitting its trail surrogate,
867 * therefore the lead surrogate must have been the same as in the other string;
868 * compare this decomposition with the lead surrogate in the other string
869 * remember that this simulates bulk text replacement:
870 * the decomposition would replace the entire code point
877 /* push current level pointers */
878 stack2[0].start=start2;
880 stack2[0].limit=limit2;
883 /* copy the folding result to fold2[] */
884 if(length<=UCASE_MAX_STRING_LENGTH) {
885 u_memcpy(fold2, p, length);
888 U16_APPEND_UNSAFE(fold2, i, length);
892 /* set next level pointers to case folding */
896 /* get ready to read from decomposition, continue with loop */
902 * no decomposition/case folding, max level for both sides:
903 * return difference result
905 * code point order comparison must not just return cp1-cp2
906 * because when single surrogates are present then the surrogate pairs
907 * that formed cp1 and cp2 may be from different string indexes
909 * example: { d800 d800 dc01 } vs. { d800 dc00 }, compare at second code units
910 * c1=d800 cp1=10001 c2=dc00 cp2=10000
911 * cp1-cp2>0 but c1-c2<0 and in fact in UTF-32 it is { d800 10001 } < { 10000 }
913 * therefore, use same fix-up as in ustring.c/uprv_strCompare()
914 * except: uprv_strCompare() fetches c=*s while this functions fetches c=*s++
915 * so we have slightly different pointer/start/limit comparisons here
918 if(c1>=0xd800 && c2>=0xd800 && (options&U_COMPARE_CODE_POINT_ORDER)) {
919 /* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */
921 (c1<=0xdbff && s1!=limit1 && U16_IS_TRAIL(*s1)) ||
922 (U16_IS_TRAIL(c1) && start1!=(s1-1) && U16_IS_LEAD(*(s1-2)))
924 /* part of a surrogate pair, leave >=d800 */
926 /* BMP code point - may be surrogate code point - make <d800 */
931 (c2<=0xdbff && s2!=limit2 && U16_IS_TRAIL(*s2)) ||
932 (U16_IS_TRAIL(c2) && start2!=(s2-1) && U16_IS_LEAD(*(s2-2)))
934 /* part of a surrogate pair, leave >=d800 */
936 /* BMP code point - may be surrogate code point - make <d800 */
945 /* public API functions */
947 U_CAPI int32_t U_EXPORT2
948 u_strCaseCompare(const UChar *s1, int32_t length1,
949 const UChar *s2, int32_t length2,
951 UErrorCode *pErrorCode) {
952 /* argument checking */
953 if(pErrorCode==0 || U_FAILURE(*pErrorCode)) {
956 if(s1==NULL || length1<-1 || s2==NULL || length2<-1) {
957 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
960 return u_strcmpFold(s1, length1, s2, length2,
961 options|U_COMPARE_IGNORE_CASE,
965 U_CAPI int32_t U_EXPORT2
966 u_strcasecmp(const UChar *s1, const UChar *s2, uint32_t options) {
967 UErrorCode errorCode=U_ZERO_ERROR;
968 return u_strcmpFold(s1, -1, s2, -1,
969 options|U_COMPARE_IGNORE_CASE,
973 U_CAPI int32_t U_EXPORT2
974 u_memcasecmp(const UChar *s1, const UChar *s2, int32_t length, uint32_t options) {
975 UErrorCode errorCode=U_ZERO_ERROR;
976 return u_strcmpFold(s1, length, s2, length,
977 options|U_COMPARE_IGNORE_CASE,
981 U_CAPI int32_t U_EXPORT2
982 u_strncasecmp(const UChar *s1, const UChar *s2, int32_t n, uint32_t options) {
983 UErrorCode errorCode=U_ZERO_ERROR;
984 return u_strcmpFold(s1, n, s2, n,
985 options|(U_COMPARE_IGNORE_CASE|_STRNCMP_STYLE),