michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2005-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: ucasemap.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2005may06 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Case mapping service object and functions using it. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/brkiter.h" michael@0: #include "unicode/ubrk.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/ucasemap.h" michael@0: #if !UCONFIG_NO_BREAK_ITERATION michael@0: #include "unicode/utext.h" michael@0: #endif michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf8.h" michael@0: #include "unicode/utf16.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "ucase.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: /* UCaseMap service object -------------------------------------------------- */ michael@0: michael@0: U_CAPI UCaseMap * U_EXPORT2 michael@0: ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode) { michael@0: UCaseMap *csm; michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: csm=(UCaseMap *)uprv_malloc(sizeof(UCaseMap)); michael@0: if(csm==NULL) { michael@0: return NULL; michael@0: } michael@0: uprv_memset(csm, 0, sizeof(UCaseMap)); michael@0: michael@0: csm->csp=ucase_getSingleton(); michael@0: ucasemap_setLocale(csm, locale, pErrorCode); michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: uprv_free(csm); michael@0: return NULL; michael@0: } michael@0: michael@0: csm->options=options; michael@0: return csm; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucasemap_close(UCaseMap *csm) { michael@0: if(csm!=NULL) { michael@0: #if !UCONFIG_NO_BREAK_ITERATION michael@0: // Do not call ubrk_close() so that we do not depend on all of the BreakIterator code. michael@0: delete reinterpret_cast(csm->iter); michael@0: #endif michael@0: uprv_free(csm); michael@0: } michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucasemap_getLocale(const UCaseMap *csm) { michael@0: return csm->locale; michael@0: } michael@0: michael@0: U_CAPI uint32_t U_EXPORT2 michael@0: ucasemap_getOptions(const UCaseMap *csm) { michael@0: return csm->options; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) { michael@0: int32_t length; michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: michael@0: length=uloc_getName(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode); michael@0: if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || length==sizeof(csm->locale)) { michael@0: *pErrorCode=U_ZERO_ERROR; michael@0: /* we only really need the language code for case mappings */ michael@0: length=uloc_getLanguage(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode); michael@0: } michael@0: if(length==sizeof(csm->locale)) { michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: csm->locCache=0; michael@0: if(U_SUCCESS(*pErrorCode)) { michael@0: ucase_getCaseLocale(csm->locale, &csm->locCache); michael@0: } else { michael@0: csm->locale[0]=0; michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode * /*pErrorCode*/) { michael@0: csm->options=options; michael@0: } michael@0: michael@0: /* UTF-8 string case mappings ----------------------------------------------- */ michael@0: michael@0: /* TODO(markus): Move to a new, separate utf8case.c file. */ michael@0: michael@0: /* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */ michael@0: static inline int32_t michael@0: appendResult(uint8_t *dest, int32_t destIndex, int32_t destCapacity, michael@0: int32_t result, const UChar *s) { michael@0: UChar32 c; michael@0: int32_t length, destLength; michael@0: UErrorCode errorCode; michael@0: michael@0: /* decode the result */ michael@0: if(result<0) { michael@0: /* (not) original code point */ michael@0: c=~result; michael@0: length=-1; michael@0: } else if(result<=UCASE_MAX_STRING_LENGTH) { michael@0: c=U_SENTINEL; michael@0: length=result; michael@0: } else { michael@0: c=result; michael@0: length=-1; michael@0: } michael@0: michael@0: if(destIndexindex=csc->cpStart; michael@0: csc->dir=dir; michael@0: } else if(dir>0) { michael@0: /* reset for forward iteration */ michael@0: csc->index=csc->cpLimit; michael@0: csc->dir=dir; michael@0: } else { michael@0: /* continue current iteration direction */ michael@0: dir=csc->dir; michael@0: } michael@0: michael@0: if(dir<0) { michael@0: if(csc->startindex) { michael@0: U8_PREV((const uint8_t *)csc->p, csc->start, csc->index, c); michael@0: return c; michael@0: } michael@0: } else { michael@0: if(csc->indexlimit) { michael@0: U8_NEXT((const uint8_t *)csc->p, csc->index, csc->limit, c); michael@0: return c; michael@0: } michael@0: } michael@0: return U_SENTINEL; michael@0: } michael@0: michael@0: /* michael@0: * Case-maps [srcStart..srcLimit[ but takes michael@0: * context [0..srcLength[ into account. michael@0: */ michael@0: static int32_t michael@0: _caseMap(const UCaseMap *csm, UCaseMapFull *map, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, UCaseContext *csc, michael@0: int32_t srcStart, int32_t srcLimit, michael@0: UErrorCode *pErrorCode) { michael@0: const UChar *s; michael@0: UChar32 c, c2 = 0; michael@0: int32_t srcIndex, destIndex; michael@0: int32_t locCache; michael@0: michael@0: locCache=csm->locCache; michael@0: michael@0: /* case mapping loop */ michael@0: srcIndex=srcStart; michael@0: destIndex=0; michael@0: while(srcIndexcpStart=srcIndex; michael@0: U8_NEXT(src, srcIndex, srcLimit, c); michael@0: csc->cpLimit=srcIndex; michael@0: if(c<0) { michael@0: int32_t i=csc->cpStart; michael@0: while(destIndexcsp, c, utf8_caseContextIterator, csc, &s, csm->locale, &locCache); michael@0: if((destIndexdestCapacity) { michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: return destIndex; michael@0: } michael@0: michael@0: #if !UCONFIG_NO_BREAK_ITERATION michael@0: michael@0: U_CFUNC int32_t U_CALLCONV michael@0: ucasemap_internalUTF8ToTitle(const UCaseMap *csm, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: const UChar *s; michael@0: UChar32 c; michael@0: int32_t prev, titleStart, titleLimit, idx, destIndex, length; michael@0: UBool isFirstIndex; michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: // Use the C++ abstract base class to minimize dependencies. michael@0: // TODO: Change UCaseMap.iter to store a BreakIterator directly. michael@0: BreakIterator *bi=reinterpret_cast(csm->iter); michael@0: michael@0: /* set up local variables */ michael@0: int32_t locCache=csm->locCache; michael@0: UCaseContext csc=UCASECONTEXT_INITIALIZER; michael@0: csc.p=(void *)src; michael@0: csc.limit=srcLength; michael@0: destIndex=0; michael@0: prev=0; michael@0: isFirstIndex=TRUE; michael@0: michael@0: /* titlecasing loop */ michael@0: while(prevfirst(); michael@0: } else { michael@0: idx=bi->next(); michael@0: } michael@0: if(idx==UBRK_DONE || idx>srcLength) { michael@0: idx=srcLength; michael@0: } michael@0: michael@0: /* michael@0: * Unicode 4 & 5 section 3.13 Default Case Operations: michael@0: * michael@0: * R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex michael@0: * #29, "Text Boundaries." Between each pair of word boundaries, find the first michael@0: * cased character F. If F exists, map F to default_title(F); then map each michael@0: * subsequent character C to default_lower(C). michael@0: * michael@0: * In this implementation, segment [prev..index[ into 3 parts: michael@0: * a) uncased characters (copy as-is) [prev..titleStart[ michael@0: * b) first case letter (titlecase) [titleStart..titleLimit[ michael@0: * c) subsequent characters (lowercase) [titleLimit..index[ michael@0: */ michael@0: if(prevoptions&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0 && UCASE_NONE==ucase_getType(csm->csp, c)) { michael@0: /* Adjust the titlecasing index (titleStart) to the next cased character. */ michael@0: for(;;) { michael@0: titleStart=titleLimit; michael@0: if(titleLimit==idx) { michael@0: /* michael@0: * only uncased characters in [prev..index[ michael@0: * stop with titleStart==titleLimit==index michael@0: */ michael@0: break; michael@0: } michael@0: U8_NEXT(src, titleLimit, idx, c); michael@0: if(UCASE_NONE!=ucase_getType(csm->csp, c)) { michael@0: break; /* cased letter at [titleStart..titleLimit[ */ michael@0: } michael@0: } michael@0: length=titleStart-prev; michael@0: if(length>0) { michael@0: if((destIndex+length)<=destCapacity) { michael@0: uprv_memcpy(dest+destIndex, src+prev, length); michael@0: } michael@0: destIndex+=length; michael@0: } michael@0: } michael@0: michael@0: if(titleStartcsp, c, utf8_caseContextIterator, &csc, &s, csm->locale, &locCache); michael@0: destIndex=appendResult(dest, destIndex, destCapacity, c, s); michael@0: michael@0: /* Special case Dutch IJ titlecasing */ michael@0: if ( titleStart+1 < idx && michael@0: ucase_getCaseLocale(csm->locale, &locCache) == UCASE_LOC_DUTCH && michael@0: ( src[titleStart] == 0x0049 || src[titleStart] == 0x0069 ) && michael@0: ( src[titleStart+1] == 0x004A || src[titleStart+1] == 0x006A )) { michael@0: c=0x004A; michael@0: destIndex=appendResult(dest, destIndex, destCapacity, c, s); michael@0: titleLimit++; michael@0: } michael@0: /* lowercase [titleLimit..index[ */ michael@0: if(titleLimitoptions&U_TITLECASE_NO_LOWERCASE)==0) { michael@0: /* Normal operation: Lowercase the rest of the word. */ michael@0: destIndex+= michael@0: _caseMap( michael@0: csm, ucase_toFullLower, michael@0: dest+destIndex, destCapacity-destIndex, michael@0: src, &csc, michael@0: titleLimit, idx, michael@0: pErrorCode); michael@0: } else { michael@0: /* Optionally just copy the rest of the word unchanged. */ michael@0: length=idx-titleLimit; michael@0: if((destIndex+length)<=destCapacity) { michael@0: uprv_memcpy(dest+destIndex, src+titleLimit, length); michael@0: } michael@0: destIndex+=length; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: prev=idx; michael@0: } michael@0: michael@0: if(destIndex>destCapacity) { michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: return destIndex; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: static int32_t U_CALLCONV michael@0: ucasemap_internalUTF8ToLower(const UCaseMap *csm, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: UCaseContext csc=UCASECONTEXT_INITIALIZER; michael@0: csc.p=(void *)src; michael@0: csc.limit=srcLength; michael@0: return _caseMap( michael@0: csm, ucase_toFullLower, michael@0: dest, destCapacity, michael@0: src, &csc, 0, srcLength, michael@0: pErrorCode); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: ucasemap_internalUTF8ToUpper(const UCaseMap *csm, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: UCaseContext csc=UCASECONTEXT_INITIALIZER; michael@0: csc.p=(void *)src; michael@0: csc.limit=srcLength; michael@0: return _caseMap( michael@0: csm, ucase_toFullUpper, michael@0: dest, destCapacity, michael@0: src, &csc, 0, srcLength, michael@0: pErrorCode); michael@0: } michael@0: michael@0: static int32_t michael@0: utf8_foldCase(const UCaseProps *csp, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: uint32_t options, michael@0: UErrorCode *pErrorCode) { michael@0: int32_t srcIndex, destIndex; michael@0: michael@0: const UChar *s; michael@0: UChar32 c, c2; michael@0: int32_t start; michael@0: michael@0: /* case mapping loop */ michael@0: srcIndex=destIndex=0; michael@0: while(srcIndexdestCapacity) { michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: return destIndex; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: ucasemap_internalUTF8Fold(const UCaseMap *csm, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: return utf8_foldCase(csm->csp, dest, destCapacity, src, srcLength, csm->options, pErrorCode); michael@0: } michael@0: michael@0: U_CFUNC int32_t michael@0: ucasemap_mapUTF8(const UCaseMap *csm, michael@0: uint8_t *dest, int32_t destCapacity, michael@0: const uint8_t *src, int32_t srcLength, michael@0: UTF8CaseMapper *stringCaseMapper, michael@0: UErrorCode *pErrorCode) { michael@0: int32_t destLength; michael@0: michael@0: /* check argument values */ michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if( destCapacity<0 || michael@0: (dest==NULL && destCapacity>0) || michael@0: src==NULL || michael@0: srcLength<-1 michael@0: ) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: /* get the string length */ michael@0: if(srcLength==-1) { michael@0: srcLength=(int32_t)uprv_strlen((const char *)src); michael@0: } michael@0: michael@0: /* check for overlapping source and destination */ michael@0: if( dest!=NULL && michael@0: ((src>=dest && src<(dest+destCapacity)) || michael@0: (dest>=src && dest<(src+srcLength))) michael@0: ) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: destLength=stringCaseMapper(csm, dest, destCapacity, src, srcLength, pErrorCode); michael@0: return u_terminateChars((char *)dest, destCapacity, destLength, pErrorCode); michael@0: } michael@0: michael@0: /* public API functions */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucasemap_utf8ToLower(const UCaseMap *csm, michael@0: char *dest, int32_t destCapacity, michael@0: const char *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: return ucasemap_mapUTF8(csm, michael@0: (uint8_t *)dest, destCapacity, michael@0: (const uint8_t *)src, srcLength, michael@0: ucasemap_internalUTF8ToLower, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucasemap_utf8ToUpper(const UCaseMap *csm, michael@0: char *dest, int32_t destCapacity, michael@0: const char *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: return ucasemap_mapUTF8(csm, michael@0: (uint8_t *)dest, destCapacity, michael@0: (const uint8_t *)src, srcLength, michael@0: ucasemap_internalUTF8ToUpper, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucasemap_utf8FoldCase(const UCaseMap *csm, michael@0: char *dest, int32_t destCapacity, michael@0: const char *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: return ucasemap_mapUTF8(csm, michael@0: (uint8_t *)dest, destCapacity, michael@0: (const uint8_t *)src, srcLength, michael@0: ucasemap_internalUTF8Fold, pErrorCode); michael@0: }