1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2005-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: ucasemap.cpp
12 * tab size: 8 (not used)
15 * created on: 2005may06
16 * created by: Markus W. Scherer
18 * Case mapping service object and functions using it.
21 #include "unicode/utypes.h"
22 #include "unicode/brkiter.h"
23 #include "unicode/ubrk.h"
24 #include "unicode/uloc.h"
25 #include "unicode/ustring.h"
26 #include "unicode/ucasemap.h"
27 #if !UCONFIG_NO_BREAK_ITERATION
28 #include "unicode/utext.h"
30 #include "unicode/utf.h"
31 #include "unicode/utf8.h"
32 #include "unicode/utf16.h"
40 /* UCaseMap service object -------------------------------------------------- */
42 U_CAPI UCaseMap * U_EXPORT2
43 ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode) {
46 if(U_FAILURE(*pErrorCode)) {
50 csm=(UCaseMap *)uprv_malloc(sizeof(UCaseMap));
54 uprv_memset(csm, 0, sizeof(UCaseMap));
56 csm->csp=ucase_getSingleton();
57 ucasemap_setLocale(csm, locale, pErrorCode);
58 if(U_FAILURE(*pErrorCode)) {
68 ucasemap_close(UCaseMap *csm) {
70 #if !UCONFIG_NO_BREAK_ITERATION
71 // Do not call ubrk_close() so that we do not depend on all of the BreakIterator code.
72 delete reinterpret_cast<BreakIterator *>(csm->iter);
78 U_CAPI const char * U_EXPORT2
79 ucasemap_getLocale(const UCaseMap *csm) {
83 U_CAPI uint32_t U_EXPORT2
84 ucasemap_getOptions(const UCaseMap *csm) {
89 ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) {
92 if(U_FAILURE(*pErrorCode)) {
96 length=uloc_getName(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
97 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || length==sizeof(csm->locale)) {
98 *pErrorCode=U_ZERO_ERROR;
99 /* we only really need the language code for case mappings */
100 length=uloc_getLanguage(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
102 if(length==sizeof(csm->locale)) {
103 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
106 if(U_SUCCESS(*pErrorCode)) {
107 ucase_getCaseLocale(csm->locale, &csm->locCache);
113 U_CAPI void U_EXPORT2
114 ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode * /*pErrorCode*/) {
115 csm->options=options;
118 /* UTF-8 string case mappings ----------------------------------------------- */
120 /* TODO(markus): Move to a new, separate utf8case.c file. */
122 /* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */
123 static inline int32_t
124 appendResult(uint8_t *dest, int32_t destIndex, int32_t destCapacity,
125 int32_t result, const UChar *s) {
128 UErrorCode errorCode;
130 /* decode the result */
132 /* (not) original code point */
135 } else if(result<=UCASE_MAX_STRING_LENGTH) {
142 if(length>(INT32_MAX-destIndex)) {
143 return -1; // integer overflow
146 if(destIndex<destCapacity) {
147 /* append the result */
151 U8_APPEND(dest, destIndex, destCapacity, c, isError);
153 /* overflow, nothing written */
159 errorCode=U_ZERO_ERROR;
161 (char *)(dest+destIndex), destCapacity-destIndex, &destLength,
164 if(U_FAILURE(errorCode) && errorCode != U_BUFFER_OVERFLOW_ERROR) {
167 if(destLength>(INT32_MAX-destIndex)) {
168 return -1; // integer overflow
170 destIndex+=destLength;
171 /* we might have an overflow, but we know the actual length */
179 errorCode=U_ZERO_ERROR;
181 NULL, 0, &destLength,
184 if(U_FAILURE(errorCode) && errorCode != U_BUFFER_OVERFLOW_ERROR) {
187 if(destLength>(INT32_MAX-destIndex)) {
188 return -1; // integer overflow
190 destIndex+=destLength;
196 static inline int32_t
197 appendUChar(uint8_t *dest, int32_t destIndex, int32_t destCapacity, UChar c) {
198 int32_t length=U8_LENGTH(c);
199 if(length>(INT32_MAX-destIndex)) {
200 return -1; // integer overflow
202 int32_t limit=destIndex+length;
203 if(limit<destCapacity) {
204 U8_APPEND_UNSAFE(dest, destIndex, c);
209 static inline int32_t
210 appendString(uint8_t *dest, int32_t destIndex, int32_t destCapacity,
211 const uint8_t *s, int32_t length) {
213 if(length>(INT32_MAX-destIndex)) {
214 return -1; // integer overflow
216 if((destIndex+length)<=destCapacity) {
217 uprv_memcpy(dest+destIndex, s, length);
224 static UChar32 U_CALLCONV
225 utf8_caseContextIterator(void *context, int8_t dir) {
226 UCaseContext *csc=(UCaseContext *)context;
230 /* reset for backward iteration */
231 csc->index=csc->cpStart;
234 /* reset for forward iteration */
235 csc->index=csc->cpLimit;
238 /* continue current iteration direction */
243 if(csc->start<csc->index) {
244 U8_PREV((const uint8_t *)csc->p, csc->start, csc->index, c);
248 if(csc->index<csc->limit) {
249 U8_NEXT((const uint8_t *)csc->p, csc->index, csc->limit, c);
257 * Case-maps [srcStart..srcLimit[ but takes
258 * context [0..srcLength[ into account.
261 _caseMap(const UCaseMap *csm, UCaseMapFull *map,
262 uint8_t *dest, int32_t destCapacity,
263 const uint8_t *src, UCaseContext *csc,
264 int32_t srcStart, int32_t srcLimit,
265 UErrorCode *pErrorCode) {
266 const UChar *s = NULL;
268 int32_t srcIndex, destIndex;
271 locCache=csm->locCache;
273 /* case mapping loop */
276 while(srcIndex<srcLimit) {
277 csc->cpStart=srcIndex;
278 U8_NEXT(src, srcIndex, srcLimit, c);
279 csc->cpLimit=srcIndex;
282 destIndex=appendString(dest, destIndex, destCapacity, src+csc->cpStart, srcIndex-csc->cpStart);
284 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
289 c=map(csm->csp, c, utf8_caseContextIterator, csc, &s, csm->locale, &locCache);
290 if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) {
291 /* fast path version of appendResult() for ASCII results */
292 dest[destIndex++]=(uint8_t)c2;
294 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
296 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
302 if(destIndex>destCapacity) {
303 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
308 #if !UCONFIG_NO_BREAK_ITERATION
310 U_CFUNC int32_t U_CALLCONV
311 ucasemap_internalUTF8ToTitle(const UCaseMap *csm,
312 uint8_t *dest, int32_t destCapacity,
313 const uint8_t *src, int32_t srcLength,
314 UErrorCode *pErrorCode) {
317 int32_t prev, titleStart, titleLimit, idx, destIndex;
320 if(U_FAILURE(*pErrorCode)) {
324 // Use the C++ abstract base class to minimize dependencies.
325 // TODO: Change UCaseMap.iter to store a BreakIterator directly.
326 BreakIterator *bi=reinterpret_cast<BreakIterator *>(csm->iter);
328 /* set up local variables */
329 int32_t locCache=csm->locCache;
330 UCaseContext csc=UCASECONTEXT_INITIALIZER;
337 /* titlecasing loop */
338 while(prev<srcLength) {
339 /* find next index where to titlecase */
346 if(idx==UBRK_DONE || idx>srcLength) {
351 * Unicode 4 & 5 section 3.13 Default Case Operations:
353 * R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex
354 * #29, "Text Boundaries." Between each pair of word boundaries, find the first
355 * cased character F. If F exists, map F to default_title(F); then map each
356 * subsequent character C to default_lower(C).
358 * In this implementation, segment [prev..index[ into 3 parts:
359 * a) uncased characters (copy as-is) [prev..titleStart[
360 * b) first case letter (titlecase) [titleStart..titleLimit[
361 * c) subsequent characters (lowercase) [titleLimit..index[
364 /* find and copy uncased characters [prev..titleStart[ */
365 titleStart=titleLimit=prev;
366 U8_NEXT(src, titleLimit, idx, c);
367 if((csm->options&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0 && UCASE_NONE==ucase_getType(csm->csp, c)) {
368 /* Adjust the titlecasing index (titleStart) to the next cased character. */
370 titleStart=titleLimit;
371 if(titleLimit==idx) {
373 * only uncased characters in [prev..index[
374 * stop with titleStart==titleLimit==index
378 U8_NEXT(src, titleLimit, idx, c);
379 if(UCASE_NONE!=ucase_getType(csm->csp, c)) {
380 break; /* cased letter at [titleStart..titleLimit[ */
383 destIndex=appendString(dest, destIndex, destCapacity, src+prev, titleStart-prev);
385 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
390 if(titleStart<titleLimit) {
391 /* titlecase c which is from [titleStart..titleLimit[ */
393 csc.cpStart=titleStart;
394 csc.cpLimit=titleLimit;
395 c=ucase_toFullTitle(csm->csp, c, utf8_caseContextIterator, &csc, &s, csm->locale, &locCache);
396 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
399 destIndex=appendString(dest, destIndex, destCapacity, src+titleStart, titleLimit-titleStart);
402 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
406 /* Special case Dutch IJ titlecasing */
407 if (titleStart+1 < idx &&
408 ucase_getCaseLocale(csm->locale, &locCache) == UCASE_LOC_DUTCH &&
409 (src[titleStart] == 0x0049 || src[titleStart] == 0x0069) &&
410 (src[titleStart+1] == 0x004A || src[titleStart+1] == 0x006A)) {
411 destIndex=appendUChar(dest, destIndex, destCapacity, 0x004A);
414 /* lowercase [titleLimit..index[ */
416 if((csm->options&U_TITLECASE_NO_LOWERCASE)==0) {
417 /* Normal operation: Lowercase the rest of the word. */
420 csm, ucase_toFullLower,
421 dest+destIndex, destCapacity-destIndex,
425 if(U_FAILURE(*pErrorCode)) {
429 /* Optionally just copy the rest of the word unchanged. */
430 destIndex=appendString(dest, destIndex, destCapacity, src+titleLimit, idx-titleLimit);
432 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
443 if(destIndex>destCapacity) {
444 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
452 namespace GreekUpper {
454 UBool isFollowedByCasedLetter(const UCaseProps *csp, const uint8_t *s, int32_t i, int32_t length) {
457 U8_NEXT(s, i, length, c);
458 int32_t type = ucase_getTypeOrIgnorable(csp, c);
459 if ((type & UCASE_IGNORABLE) != 0) {
460 // Case-ignorable, continue with the loop.
461 } else if (type != UCASE_NONE) {
462 return TRUE; // Followed by cased letter.
464 return FALSE; // Uncased and not case-ignorable.
467 return FALSE; // Not followed by cased letter.
470 // Keep this consistent with the UTF-16 version in ustrcase.cpp and the Java version in CaseMap.java.
471 int32_t toUpper(const UCaseMap *csm,
472 uint8_t *dest, int32_t destCapacity,
473 const uint8_t *src, int32_t srcLength,
474 UErrorCode *pErrorCode) {
475 int32_t locCache = UCASE_LOC_GREEK;
478 for (int32_t i = 0; i < srcLength;) {
479 int32_t nextIndex = i;
481 U8_NEXT(src, nextIndex, srcLength, c);
482 uint32_t nextState = 0;
483 int32_t type = ucase_getTypeOrIgnorable(csm->csp, c);
484 if ((type & UCASE_IGNORABLE) != 0) {
485 // c is case-ignorable
486 nextState |= (state & AFTER_CASED);
487 } else if (type != UCASE_NONE) {
489 nextState |= AFTER_CASED;
491 uint32_t data = getLetterData(c);
493 uint32_t upper = data & UPPER_MASK;
494 // Add a dialytika to this iota or ypsilon vowel
495 // if we removed a tonos from the previous vowel,
496 // and that previous vowel did not also have (or gain) a dialytika.
497 // Adding one only to the final vowel in a longer sequence
498 // (which does not occur in normal writing) would require lookahead.
499 // Set the same flag as for preserving an existing dialytika.
500 if ((data & HAS_VOWEL) != 0 && (state & AFTER_VOWEL_WITH_ACCENT) != 0 &&
501 (upper == 0x399 || upper == 0x3A5)) {
502 data |= HAS_DIALYTIKA;
504 int32_t numYpogegrammeni = 0; // Map each one to a trailing, spacing, capital iota.
505 if ((data & HAS_YPOGEGRAMMENI) != 0) {
506 numYpogegrammeni = 1;
508 // Skip combining diacritics after this Greek letter.
509 int32_t nextNextIndex = nextIndex;
510 while (nextIndex < srcLength) {
512 U8_NEXT(src, nextNextIndex, srcLength, c2);
513 uint32_t diacriticData = getDiacriticData(c2);
514 if (diacriticData != 0) {
515 data |= diacriticData;
516 if ((diacriticData & HAS_YPOGEGRAMMENI) != 0) {
519 nextIndex = nextNextIndex;
521 break; // not a Greek diacritic
524 if ((data & HAS_VOWEL_AND_ACCENT_AND_DIALYTIKA) == HAS_VOWEL_AND_ACCENT) {
525 nextState |= AFTER_VOWEL_WITH_ACCENT;
527 // Map according to Greek rules.
528 UBool addTonos = FALSE;
529 if (upper == 0x397 &&
530 (data & HAS_ACCENT) != 0 &&
531 numYpogegrammeni == 0 &&
532 (state & AFTER_CASED) == 0 &&
533 !isFollowedByCasedLetter(csm->csp, src, nextIndex, srcLength)) {
534 // Keep disjunctive "or" with (only) a tonos.
535 // We use the same "word boundary" conditions as for the Final_Sigma test.
536 if (i == nextIndex) {
537 upper = 0x389; // Preserve the precomposed form.
541 } else if ((data & HAS_DIALYTIKA) != 0) {
542 // Preserve a vowel with dialytika in precomposed form if it exists.
543 if (upper == 0x399) {
545 data &= ~HAS_EITHER_DIALYTIKA;
546 } else if (upper == 0x3A5) {
548 data &= ~HAS_EITHER_DIALYTIKA;
551 destIndex=appendUChar(dest, destIndex, destCapacity, (UChar)upper);
552 if (destIndex >= 0 && (data & HAS_EITHER_DIALYTIKA) != 0) {
553 destIndex=appendUChar(dest, destIndex, destCapacity, 0x308); // restore or add a dialytika
555 if (destIndex >= 0 && addTonos) {
556 destIndex=appendUChar(dest, destIndex, destCapacity, 0x301);
558 while (destIndex >= 0 && numYpogegrammeni > 0) {
559 destIndex=appendUChar(dest, destIndex, destCapacity, 0x399);
563 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
569 c=ucase_toFullUpper(csm->csp, c, NULL, NULL, &s, csm->locale, &locCache);
570 if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) {
571 /* fast path version of appendResult() for ASCII results */
572 dest[destIndex++]=(uint8_t)c2;
574 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
576 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
582 destIndex=appendString(dest, destIndex, destCapacity, src+i, nextIndex-i);
584 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
592 if(destIndex>destCapacity) {
593 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
598 } // namespace GreekUpper
601 static int32_t U_CALLCONV
602 ucasemap_internalUTF8ToLower(const UCaseMap *csm,
603 uint8_t *dest, int32_t destCapacity,
604 const uint8_t *src, int32_t srcLength,
605 UErrorCode *pErrorCode) {
606 UCaseContext csc=UCASECONTEXT_INITIALIZER;
610 csm, ucase_toFullLower,
612 src, &csc, 0, srcLength,
616 static int32_t U_CALLCONV
617 ucasemap_internalUTF8ToUpper(const UCaseMap *csm,
618 uint8_t *dest, int32_t destCapacity,
619 const uint8_t *src, int32_t srcLength,
620 UErrorCode *pErrorCode) {
621 int32_t locCache = csm->locCache;
622 if (ucase_getCaseLocale(csm->locale, &locCache) == UCASE_LOC_GREEK) {
623 return GreekUpper::toUpper(csm, dest, destCapacity, src, srcLength, pErrorCode);
625 UCaseContext csc=UCASECONTEXT_INITIALIZER;
629 csm, ucase_toFullUpper,
631 src, &csc, 0, srcLength,
636 utf8_foldCase(const UCaseProps *csp,
637 uint8_t *dest, int32_t destCapacity,
638 const uint8_t *src, int32_t srcLength,
640 UErrorCode *pErrorCode) {
641 int32_t srcIndex, destIndex;
647 /* case mapping loop */
648 srcIndex=destIndex=0;
649 while(srcIndex<srcLength) {
651 U8_NEXT(src, srcIndex, srcLength, c);
654 destIndex=appendString(dest, destIndex, destCapacity, src+start, srcIndex-start);
656 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
661 c=ucase_toFullFolding(csp, c, &s, options);
662 if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) {
663 /* fast path version of appendResult() for ASCII results */
664 dest[destIndex++]=(uint8_t)c2;
666 destIndex=appendResult(dest, destIndex, destCapacity, c, s);
668 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
674 if(destIndex>destCapacity) {
675 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
680 static int32_t U_CALLCONV
681 ucasemap_internalUTF8Fold(const UCaseMap *csm,
682 uint8_t *dest, int32_t destCapacity,
683 const uint8_t *src, int32_t srcLength,
684 UErrorCode *pErrorCode) {
685 return utf8_foldCase(csm->csp, dest, destCapacity, src, srcLength, csm->options, pErrorCode);
689 ucasemap_mapUTF8(const UCaseMap *csm,
690 uint8_t *dest, int32_t destCapacity,
691 const uint8_t *src, int32_t srcLength,
692 UTF8CaseMapper *stringCaseMapper,
693 UErrorCode *pErrorCode) {
696 /* check argument values */
697 if(U_FAILURE(*pErrorCode)) {
700 if( destCapacity<0 ||
701 (dest==NULL && destCapacity>0) ||
705 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
709 /* get the string length */
711 srcLength=(int32_t)uprv_strlen((const char *)src);
714 /* check for overlapping source and destination */
716 ((src>=dest && src<(dest+destCapacity)) ||
717 (dest>=src && dest<(src+srcLength)))
719 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
723 destLength=stringCaseMapper(csm, dest, destCapacity, src, srcLength, pErrorCode);
724 return u_terminateChars((char *)dest, destCapacity, destLength, pErrorCode);
727 /* public API functions */
729 U_CAPI int32_t U_EXPORT2
730 ucasemap_utf8ToLower(const UCaseMap *csm,
731 char *dest, int32_t destCapacity,
732 const char *src, int32_t srcLength,
733 UErrorCode *pErrorCode) {
734 return ucasemap_mapUTF8(csm,
735 (uint8_t *)dest, destCapacity,
736 (const uint8_t *)src, srcLength,
737 ucasemap_internalUTF8ToLower, pErrorCode);
740 U_CAPI int32_t U_EXPORT2
741 ucasemap_utf8ToUpper(const UCaseMap *csm,
742 char *dest, int32_t destCapacity,
743 const char *src, int32_t srcLength,
744 UErrorCode *pErrorCode) {
745 return ucasemap_mapUTF8(csm,
746 (uint8_t *)dest, destCapacity,
747 (const uint8_t *)src, srcLength,
748 ucasemap_internalUTF8ToUpper, pErrorCode);
751 U_CAPI int32_t U_EXPORT2
752 ucasemap_utf8FoldCase(const UCaseMap *csm,
753 char *dest, int32_t destCapacity,
754 const char *src, int32_t srcLength,
755 UErrorCode *pErrorCode) {
756 return ucasemap_mapUTF8(csm,
757 (uint8_t *)dest, destCapacity,
758 (const uint8_t *)src, srcLength,
759 ucasemap_internalUTF8Fold, pErrorCode);