2005-05-20 17:31:12 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2011-05-26 20:05:39 +00:00
|
|
|
* Copyright (C) 2005-2011, International Business Machines
|
2005-05-20 17:31:12 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
2011-05-26 20:05:39 +00:00
|
|
|
* file name: ucasemap.cpp
|
2005-05-20 17:31:12 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2005may06
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* Case mapping service object and functions using it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/ucasemap.h"
|
2007-07-27 06:37:08 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
#include "unicode/ubrk.h"
|
|
|
|
#include "unicode/utext.h"
|
|
|
|
#endif
|
2005-05-20 17:31:12 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "ucase.h"
|
|
|
|
#include "ustr_imp.h"
|
|
|
|
|
|
|
|
/* UCaseMap service object -------------------------------------------------- */
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI UCaseMap * U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode) {
|
|
|
|
UCaseMap *csm;
|
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
csm=(UCaseMap *)uprv_malloc(sizeof(UCaseMap));
|
|
|
|
if(csm==NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uprv_memset(csm, 0, sizeof(UCaseMap));
|
|
|
|
|
2010-09-21 00:12:49 +00:00
|
|
|
csm->csp=ucase_getSingleton();
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_setLocale(csm, locale, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
uprv_free(csm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
csm->options=options;
|
|
|
|
return csm;
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_close(UCaseMap *csm) {
|
|
|
|
if(csm!=NULL) {
|
2007-07-27 06:37:08 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
ubrk_close(csm->iter);
|
|
|
|
#endif
|
2005-05-20 17:31:12 +00:00
|
|
|
uprv_free(csm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI const char * U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_getLocale(const UCaseMap *csm) {
|
|
|
|
return csm->locale;
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI uint32_t U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_getOptions(const UCaseMap *csm) {
|
|
|
|
return csm->options;
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) {
|
|
|
|
int32_t length;
|
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
length=uloc_getName(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
|
|
|
|
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || length==sizeof(csm->locale)) {
|
|
|
|
*pErrorCode=U_ZERO_ERROR;
|
|
|
|
/* we only really need the language code for case mappings */
|
|
|
|
length=uloc_getLanguage(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
|
|
|
|
}
|
|
|
|
if(length==sizeof(csm->locale)) {
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
csm->locCache=0;
|
|
|
|
if(U_SUCCESS(*pErrorCode)) {
|
|
|
|
ucase_getCaseLocale(csm->locale, &csm->locCache);
|
|
|
|
} else {
|
|
|
|
csm->locale[0]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2011-05-26 20:05:39 +00:00
|
|
|
ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode * /*pErrorCode*/) {
|
2005-05-20 17:31:12 +00:00
|
|
|
csm->options=options;
|
|
|
|
}
|
|
|
|
|
2007-07-27 06:37:08 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI const UBreakIterator * U_EXPORT2
|
2007-07-27 06:37:08 +00:00
|
|
|
ucasemap_getBreakIterator(const UCaseMap *csm) {
|
|
|
|
return csm->iter;
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2011-05-26 20:05:39 +00:00
|
|
|
ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode * /*pErrorCode*/) {
|
2007-07-27 06:37:08 +00:00
|
|
|
ubrk_close(csm->iter);
|
|
|
|
csm->iter=iterToAdopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-05-20 17:31:12 +00:00
|
|
|
/* UTF-8 string case mappings ----------------------------------------------- */
|
|
|
|
|
2007-07-27 06:37:08 +00:00
|
|
|
/* TODO(markus): Move to a new, separate utf8case.c file. */
|
|
|
|
|
2005-05-20 17:31:12 +00:00
|
|
|
/* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */
|
2011-05-26 20:05:39 +00:00
|
|
|
static inline int32_t
|
2005-05-20 17:31:12 +00:00
|
|
|
appendResult(uint8_t *dest, int32_t destIndex, int32_t destCapacity,
|
|
|
|
int32_t result, const UChar *s) {
|
|
|
|
UChar32 c;
|
|
|
|
int32_t length, destLength;
|
|
|
|
UErrorCode errorCode;
|
|
|
|
|
|
|
|
/* decode the result */
|
|
|
|
if(result<0) {
|
|
|
|
/* (not) original code point */
|
|
|
|
c=~result;
|
|
|
|
length=-1;
|
|
|
|
} else if(result<=UCASE_MAX_STRING_LENGTH) {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
length=result;
|
|
|
|
} else {
|
|
|
|
c=result;
|
|
|
|
length=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(destIndex<destCapacity) {
|
|
|
|
/* append the result */
|
|
|
|
if(length<0) {
|
|
|
|
/* code point */
|
|
|
|
UBool isError=FALSE;
|
|
|
|
U8_APPEND(dest, destIndex, destCapacity, c, isError);
|
|
|
|
if(isError) {
|
|
|
|
/* overflow, nothing written */
|
|
|
|
destIndex+=U8_LENGTH(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* string */
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
u_strToUTF8(
|
|
|
|
(char *)(dest+destIndex), destCapacity-destIndex, &destLength,
|
|
|
|
s, length,
|
|
|
|
&errorCode);
|
2007-07-27 06:37:08 +00:00
|
|
|
destIndex+=destLength;
|
2005-05-20 17:31:12 +00:00
|
|
|
/* we might have an overflow, but we know the actual length */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* preflight */
|
|
|
|
if(length<0) {
|
|
|
|
destIndex+=U8_LENGTH(c);
|
|
|
|
} else {
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
u_strToUTF8(
|
|
|
|
NULL, 0, &destLength,
|
|
|
|
s, length,
|
|
|
|
&errorCode);
|
2007-07-27 06:37:08 +00:00
|
|
|
destIndex+=destLength;
|
2005-05-20 17:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return destIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UChar32 U_CALLCONV
|
|
|
|
utf8_caseContextIterator(void *context, int8_t dir) {
|
|
|
|
UCaseContext *csc=(UCaseContext *)context;
|
|
|
|
UChar32 c;
|
|
|
|
|
|
|
|
if(dir<0) {
|
|
|
|
/* reset for backward iteration */
|
|
|
|
csc->index=csc->cpStart;
|
|
|
|
csc->dir=dir;
|
|
|
|
} else if(dir>0) {
|
|
|
|
/* reset for forward iteration */
|
|
|
|
csc->index=csc->cpLimit;
|
|
|
|
csc->dir=dir;
|
|
|
|
} else {
|
|
|
|
/* continue current iteration direction */
|
|
|
|
dir=csc->dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dir<0) {
|
|
|
|
if(csc->start<csc->index) {
|
|
|
|
U8_PREV((const uint8_t *)csc->p, csc->start, csc->index, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(csc->index<csc->limit) {
|
|
|
|
U8_NEXT((const uint8_t *)csc->p, csc->index, csc->limit, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return U_SENTINEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Case-maps [srcStart..srcLimit[ but takes
|
|
|
|
* context [0..srcLength[ into account.
|
|
|
|
*/
|
|
|
|
static int32_t
|
|
|
|
_caseMap(const UCaseMap *csm, UCaseMapFull *map,
|
|
|
|
uint8_t *dest, int32_t destCapacity,
|
|
|
|
const uint8_t *src, UCaseContext *csc,
|
|
|
|
int32_t srcStart, int32_t srcLimit,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const UChar *s;
|
2009-03-02 20:53:54 +00:00
|
|
|
UChar32 c, c2 = 0;
|
2005-05-20 17:31:12 +00:00
|
|
|
int32_t srcIndex, destIndex;
|
|
|
|
int32_t locCache;
|
|
|
|
|
|
|
|
locCache=csm->locCache;
|
|
|
|
|
|
|
|
/* case mapping loop */
|
|
|
|
srcIndex=srcStart;
|
|
|
|
destIndex=0;
|
|
|
|
while(srcIndex<srcLimit) {
|
|
|
|
csc->cpStart=srcIndex;
|
|
|
|
U8_NEXT(src, srcIndex, srcLimit, c);
|
|
|
|
csc->cpLimit=srcIndex;
|
2007-07-27 06:37:08 +00:00
|
|
|
if(c<0) {
|
|
|
|
int32_t i=csc->cpStart;
|
|
|
|
while(destIndex<destCapacity && i<srcIndex) {
|
|
|
|
dest[destIndex++]=src[i++];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2005-05-20 17:31:12 +00:00
|
|
|
c=map(csm->csp, c, utf8_caseContextIterator, csc, &s, csm->locale, &locCache);
|
2007-07-27 06:37:08 +00:00
|
|
|
if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) {
|
|
|
|
/* fast path version of appendResult() for ASCII results */
|
|
|
|
dest[destIndex++]=(uint8_t)c2;
|
|
|
|
} else {
|
|
|
|
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(destIndex>destCapacity) {
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
return destIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal titlecasing function.
|
|
|
|
*/
|
|
|
|
static int32_t
|
|
|
|
_toTitle(UCaseMap *csm,
|
|
|
|
uint8_t *dest, int32_t destCapacity,
|
|
|
|
const uint8_t *src, UCaseContext *csc,
|
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
UText utext=UTEXT_INITIALIZER;
|
|
|
|
const UChar *s;
|
|
|
|
UChar32 c;
|
2009-04-15 01:57:02 +00:00
|
|
|
int32_t prev, titleStart, titleLimit, idx, destIndex, length;
|
2007-07-27 06:37:08 +00:00
|
|
|
UBool isFirstIndex;
|
|
|
|
|
|
|
|
utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(csm->iter==NULL) {
|
|
|
|
csm->iter=ubrk_open(UBRK_WORD, csm->locale,
|
|
|
|
NULL, 0,
|
|
|
|
pErrorCode);
|
|
|
|
}
|
|
|
|
ubrk_setUText(csm->iter, &utext, pErrorCode);
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
utext_close(&utext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up local variables */
|
|
|
|
destIndex=0;
|
|
|
|
prev=0;
|
|
|
|
isFirstIndex=TRUE;
|
|
|
|
|
|
|
|
/* titlecasing loop */
|
|
|
|
while(prev<srcLength) {
|
|
|
|
/* find next index where to titlecase */
|
|
|
|
if(isFirstIndex) {
|
|
|
|
isFirstIndex=FALSE;
|
2009-04-15 01:57:02 +00:00
|
|
|
idx=ubrk_first(csm->iter);
|
2007-07-27 06:37:08 +00:00
|
|
|
} else {
|
2009-04-15 01:57:02 +00:00
|
|
|
idx=ubrk_next(csm->iter);
|
2007-07-27 06:37:08 +00:00
|
|
|
}
|
2009-04-15 01:57:02 +00:00
|
|
|
if(idx==UBRK_DONE || idx>srcLength) {
|
|
|
|
idx=srcLength;
|
2007-07-27 06:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unicode 4 & 5 section 3.13 Default Case Operations:
|
|
|
|
*
|
|
|
|
* R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex
|
|
|
|
* #29, "Text Boundaries." Between each pair of word boundaries, find the first
|
|
|
|
* cased character F. If F exists, map F to default_title(F); then map each
|
|
|
|
* subsequent character C to default_lower(C).
|
|
|
|
*
|
|
|
|
* In this implementation, segment [prev..index[ into 3 parts:
|
|
|
|
* a) uncased characters (copy as-is) [prev..titleStart[
|
|
|
|
* b) first case letter (titlecase) [titleStart..titleLimit[
|
|
|
|
* c) subsequent characters (lowercase) [titleLimit..index[
|
|
|
|
*/
|
2009-04-15 01:57:02 +00:00
|
|
|
if(prev<idx) {
|
2007-07-27 06:37:08 +00:00
|
|
|
/* find and copy uncased characters [prev..titleStart[ */
|
|
|
|
titleStart=titleLimit=prev;
|
2009-04-15 01:57:02 +00:00
|
|
|
U8_NEXT(src, titleLimit, idx, c);
|
2007-07-27 06:37:08 +00:00
|
|
|
if((csm->options&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0 && UCASE_NONE==ucase_getType(csm->csp, c)) {
|
|
|
|
/* Adjust the titlecasing index (titleStart) to the next cased character. */
|
|
|
|
for(;;) {
|
|
|
|
titleStart=titleLimit;
|
2009-04-15 01:57:02 +00:00
|
|
|
if(titleLimit==idx) {
|
2007-07-27 06:37:08 +00:00
|
|
|
/*
|
|
|
|
* only uncased characters in [prev..index[
|
|
|
|
* stop with titleStart==titleLimit==index
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
2009-04-15 01:57:02 +00:00
|
|
|
U8_NEXT(src, titleLimit, idx, c);
|
2007-07-27 06:37:08 +00:00
|
|
|
if(UCASE_NONE!=ucase_getType(csm->csp, c)) {
|
|
|
|
break; /* cased letter at [titleStart..titleLimit[ */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length=titleStart-prev;
|
|
|
|
if(length>0) {
|
|
|
|
if((destIndex+length)<=destCapacity) {
|
|
|
|
uprv_memcpy(dest+destIndex, src+prev, length);
|
|
|
|
}
|
|
|
|
destIndex+=length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(titleStart<titleLimit) {
|
|
|
|
/* titlecase c which is from [titleStart..titleLimit[ */
|
|
|
|
csc->cpStart=titleStart;
|
|
|
|
csc->cpLimit=titleLimit;
|
|
|
|
c=ucase_toFullTitle(csm->csp, c, utf8_caseContextIterator, csc, &s, csm->locale, &csm->locCache);
|
|
|
|
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
|
|
|
|
|
2008-06-13 21:07:07 +00:00
|
|
|
|
|
|
|
/* Special case Dutch IJ titlecasing */
|
2009-04-15 01:57:02 +00:00
|
|
|
if ( titleStart+1 < idx &&
|
2008-06-13 21:07:07 +00:00
|
|
|
ucase_getCaseLocale(csm->locale,&csm->locCache) == UCASE_LOC_DUTCH &&
|
|
|
|
( src[titleStart] == 0x0049 || src[titleStart] == 0x0069 ) &&
|
|
|
|
( src[titleStart+1] == 0x004A || src[titleStart+1] == 0x006A )) {
|
|
|
|
c=0x004A;
|
|
|
|
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
|
|
|
|
titleLimit++;
|
|
|
|
}
|
2007-07-27 06:37:08 +00:00
|
|
|
/* lowercase [titleLimit..index[ */
|
2009-04-15 01:57:02 +00:00
|
|
|
if(titleLimit<idx) {
|
2007-07-27 06:37:08 +00:00
|
|
|
if((csm->options&U_TITLECASE_NO_LOWERCASE)==0) {
|
|
|
|
/* Normal operation: Lowercase the rest of the word. */
|
|
|
|
destIndex+=
|
|
|
|
_caseMap(
|
|
|
|
csm, ucase_toFullLower,
|
|
|
|
dest+destIndex, destCapacity-destIndex,
|
|
|
|
src, csc,
|
2009-04-15 01:57:02 +00:00
|
|
|
titleLimit, idx,
|
2007-07-27 06:37:08 +00:00
|
|
|
pErrorCode);
|
|
|
|
} else {
|
|
|
|
/* Optionally just copy the rest of the word unchanged. */
|
2009-04-15 01:57:02 +00:00
|
|
|
length=idx-titleLimit;
|
2007-07-27 06:37:08 +00:00
|
|
|
if((destIndex+length)<=destCapacity) {
|
|
|
|
uprv_memcpy(dest+destIndex, src+titleLimit, length);
|
|
|
|
}
|
|
|
|
destIndex+=length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-15 01:57:02 +00:00
|
|
|
prev=idx;
|
2007-07-27 06:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(destIndex>destCapacity) {
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
utext_close(&utext);
|
|
|
|
return destIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-08-08 18:39:02 +00:00
|
|
|
static int32_t
|
2007-07-27 06:37:08 +00:00
|
|
|
utf8_foldCase(const UCaseProps *csp,
|
|
|
|
uint8_t *dest, int32_t destCapacity,
|
|
|
|
const uint8_t *src, int32_t srcLength,
|
|
|
|
uint32_t options,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
int32_t srcIndex, destIndex;
|
|
|
|
|
|
|
|
const UChar *s;
|
|
|
|
UChar32 c, c2;
|
|
|
|
int32_t start;
|
|
|
|
|
|
|
|
/* case mapping loop */
|
|
|
|
srcIndex=destIndex=0;
|
|
|
|
while(srcIndex<srcLength) {
|
|
|
|
start=srcIndex;
|
|
|
|
U8_NEXT(src, srcIndex, srcLength, c);
|
|
|
|
if(c<0) {
|
|
|
|
while(destIndex<destCapacity && start<srcIndex) {
|
|
|
|
dest[destIndex++]=src[start++];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c=ucase_toFullFolding(csp, c, &s, options);
|
|
|
|
if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) {
|
|
|
|
/* fast path version of appendResult() for ASCII results */
|
|
|
|
dest[destIndex++]=(uint8_t)c2;
|
|
|
|
} else {
|
|
|
|
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
|
|
|
|
}
|
2005-05-20 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(destIndex>destCapacity) {
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
return destIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implement argument checking and buffer handling
|
|
|
|
* for string case mapping as a common function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* common internal function for public API functions */
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
caseMap(const UCaseMap *csm,
|
|
|
|
uint8_t *dest, int32_t destCapacity,
|
|
|
|
const uint8_t *src, int32_t srcLength,
|
|
|
|
int32_t toWhichCase,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
int32_t destLength;
|
|
|
|
|
|
|
|
/* check argument values */
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( destCapacity<0 ||
|
|
|
|
(dest==NULL && destCapacity>0) ||
|
|
|
|
src==NULL ||
|
|
|
|
srcLength<-1
|
|
|
|
) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the string length */
|
|
|
|
if(srcLength==-1) {
|
2009-11-11 15:47:22 +00:00
|
|
|
srcLength=(int32_t)uprv_strlen((const char *)src);
|
2005-05-20 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for overlapping source and destination */
|
|
|
|
if( dest!=NULL &&
|
|
|
|
((src>=dest && src<(dest+destCapacity)) ||
|
|
|
|
(dest>=src && dest<(src+srcLength)))
|
|
|
|
) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
destLength=0;
|
|
|
|
|
2007-07-27 06:37:08 +00:00
|
|
|
if(toWhichCase==FOLD_CASE) {
|
|
|
|
destLength=utf8_foldCase(csm->csp, dest, destCapacity, src, srcLength,
|
|
|
|
csm->options, pErrorCode);
|
|
|
|
} else {
|
2011-05-26 20:05:39 +00:00
|
|
|
UCaseContext csc=UCASECONTEXT_INITIALIZER;
|
2007-07-27 06:37:08 +00:00
|
|
|
|
|
|
|
csc.p=(void *)src;
|
|
|
|
csc.limit=srcLength;
|
|
|
|
|
|
|
|
if(toWhichCase==TO_LOWER) {
|
|
|
|
destLength=_caseMap(csm, ucase_toFullLower,
|
|
|
|
dest, destCapacity,
|
|
|
|
src, &csc,
|
|
|
|
0, srcLength,
|
|
|
|
pErrorCode);
|
|
|
|
} else if(toWhichCase==TO_UPPER) {
|
|
|
|
destLength=_caseMap(csm, ucase_toFullUpper,
|
|
|
|
dest, destCapacity,
|
|
|
|
src, &csc,
|
|
|
|
0, srcLength,
|
|
|
|
pErrorCode);
|
|
|
|
} else /* if(toWhichCase==TO_TITLE) */ {
|
|
|
|
#if UCONFIG_NO_BREAK_ITERATION
|
|
|
|
*pErrorCode=U_UNSUPPORTED_ERROR;
|
|
|
|
#else
|
|
|
|
/* UCaseMap is actually non-const in toTitle() APIs. */
|
2008-10-03 20:30:55 +00:00
|
|
|
UCaseMap *tmp = (UCaseMap *)csm;
|
|
|
|
destLength=_toTitle(tmp, dest, destCapacity,
|
2007-07-27 06:37:08 +00:00
|
|
|
src, &csc, srcLength,
|
|
|
|
pErrorCode);
|
|
|
|
#endif
|
|
|
|
}
|
2005-05-20 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 19:39:14 +00:00
|
|
|
return u_terminateChars((char *)dest, destCapacity, destLength, pErrorCode);
|
2005-05-20 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* public API functions */
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_utf8ToLower(const UCaseMap *csm,
|
|
|
|
char *dest, int32_t destCapacity,
|
|
|
|
const char *src, int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
return caseMap(csm,
|
|
|
|
(uint8_t *)dest, destCapacity,
|
|
|
|
(const uint8_t *)src, srcLength,
|
|
|
|
TO_LOWER, pErrorCode);
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2005-05-20 17:31:12 +00:00
|
|
|
ucasemap_utf8ToUpper(const UCaseMap *csm,
|
|
|
|
char *dest, int32_t destCapacity,
|
|
|
|
const char *src, int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
return caseMap(csm,
|
|
|
|
(uint8_t *)dest, destCapacity,
|
|
|
|
(const uint8_t *)src, srcLength,
|
|
|
|
TO_UPPER, pErrorCode);
|
|
|
|
}
|
2007-07-27 06:37:08 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2007-07-27 06:37:08 +00:00
|
|
|
ucasemap_utf8ToTitle(UCaseMap *csm,
|
|
|
|
char *dest, int32_t destCapacity,
|
|
|
|
const char *src, int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
return caseMap(csm,
|
|
|
|
(uint8_t *)dest, destCapacity,
|
|
|
|
(const uint8_t *)src, srcLength,
|
|
|
|
TO_TITLE, pErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2007-07-27 06:37:08 +00:00
|
|
|
ucasemap_utf8FoldCase(const UCaseMap *csm,
|
|
|
|
char *dest, int32_t destCapacity,
|
|
|
|
const char *src, int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
return caseMap(csm,
|
|
|
|
(uint8_t *)dest, destCapacity,
|
|
|
|
(const uint8_t *)src, srcLength,
|
|
|
|
FOLD_CASE, pErrorCode);
|
|
|
|
}
|