#include "unicode/utypes.h"
#include "unicode/uloc.h"
#include "unicode/ustring.h"
#include "unicode/ubrk.h"
#include "cmemory.h"
#include "ucase.h"
#include "unormimp.h"
#include "ustr_imp.h"
static U_INLINE int32_t
appendResult(UChar *dest, int32_t destIndex, int32_t destCapacity,
int32_t result, const UChar *s) {
UChar32 c;
int32_t length;
if(result<0) {
c=~result;
length=-1;
} else if(result<=UCASE_MAX_STRING_LENGTH) {
c=U_SENTINEL;
length=result;
} else {
c=result;
length=-1;
}
if(destIndex<destCapacity) {
if(length<0) {
UBool isError=FALSE;
U16_APPEND(dest, destIndex, destCapacity, c, isError);
if(isError) {
destIndex+=U16_LENGTH(c);
}
} else {
if((destIndex+length)<=destCapacity) {
while(length>0) {
dest[destIndex++]=*s++;
--length;
}
} else {
destIndex+=length;
}
}
} else {
if(length<0) {
destIndex+=U16_LENGTH(c);
} else {
destIndex+=length;
}
}
return destIndex;
}
static UChar32 U_CALLCONV
utf16_caseContextIterator(void *context, int8_t dir) {
UCaseContext *csc=(UCaseContext *)context;
UChar32 c;
if(dir<0) {
csc->index=csc->cpStart;
csc->dir=dir;
} else if(dir>0) {
csc->index=csc->cpLimit;
csc->dir=dir;
} else {
dir=csc->dir;
}
if(dir<0) {
if(csc->start<csc->index) {
U16_PREV((const UChar *)csc->p, csc->start, csc->index, c);
return c;
}
} else {
if(csc->index<csc->limit) {
U16_NEXT((const UChar *)csc->p, csc->index, csc->limit, c);
return c;
}
}
return U_SENTINEL;
}
typedef int32_t U_CALLCONV
UCaseMapFull(const UCaseProps *csp, UChar32 c,
UCaseContextIterator *iter, void *context,
const UChar **pString,
const char *locale, int32_t *locCache);
static int32_t
_caseMap(UCaseProps *csp, UCaseMapFull *map,
UChar *dest, int32_t destCapacity,
const UChar *src, UCaseContext *csc,
int32_t srcStart, int32_t srcLimit,
const char *locale, int32_t *locCache,
UErrorCode *pErrorCode) {
const UChar *s;
UChar32 c;
int32_t srcIndex, destIndex;
srcIndex=srcStart;
destIndex=0;
while(srcIndex<srcLimit) {
csc->cpStart=srcIndex;
U16_NEXT(src, srcIndex, srcLimit, c);
csc->cpLimit=srcIndex;
c=map(csp, c, utf16_caseContextIterator, csc, &s, locale, locCache);
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
}
if(destIndex>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
return destIndex;
}
#if !UCONFIG_NO_BREAK_ITERATION
static int32_t
_toTitle(UCaseProps *csp,
UChar *dest, int32_t destCapacity,
const UChar *src, UCaseContext *csc,
int32_t srcLength,
UBreakIterator *titleIter,
const char *locale, int32_t *locCache,
UErrorCode *pErrorCode) {
const UChar *s;
UChar32 c;
int32_t prev, index, destIndex;
UBool isFirstIndex;
destIndex=0;
prev=0;
isFirstIndex=TRUE;
while(prev<srcLength) {
if(isFirstIndex) {
isFirstIndex=FALSE;
index=ubrk_first(titleIter);
} else {
index=ubrk_next(titleIter);
}
if(index==UBRK_DONE || index>srcLength) {
index=srcLength;
}
if(prev<index) {
destIndex+=
_caseMap(
csp, ucase_toFullLower,
dest+destIndex, destCapacity-destIndex,
src, csc,
prev, index,
locale, locCache,
pErrorCode);
}
if(index>=srcLength) {
break;
}
csc->cpStart=index;
U16_NEXT(src, index, srcLength, c);
csc->cpLimit=index;
c=ucase_toFullTitle(csp, c, utf16_caseContextIterator, csc, &s, locale, locCache);
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
prev=index;
}
if(destIndex>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
return destIndex;
}
U_CFUNC int32_t
ustr_toTitle(UCaseProps *csp,
UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UBreakIterator *titleIter,
const char *locale,
UErrorCode *pErrorCode) {
UCaseContext csc={ NULL };
int32_t locCache;
csc.p=(void *)src;
csc.limit=srcLength;
locCache=0;
return _toTitle(csp,
dest, destCapacity,
src, &csc, srcLength,
titleIter, locale, &locCache, pErrorCode);
}
#endif
U_CFUNC int32_t
ustr_toLower(UCaseProps *csp,
UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
const char *locale,
UErrorCode *pErrorCode) {
UCaseContext csc={ NULL };
int32_t locCache;
csc.p=(void *)src;
csc.limit=srcLength;
locCache=0;
return _caseMap(csp, ucase_toFullLower,
dest, destCapacity,
src, &csc, 0, srcLength,
locale, &locCache, pErrorCode);
}
U_CFUNC int32_t
ustr_toUpper(UCaseProps *csp,
UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
const char *locale,
UErrorCode *pErrorCode) {
UCaseContext csc={ NULL };
int32_t locCache;
csc.p=(void *)src;
csc.limit=srcLength;
locCache=0;
return _caseMap(csp, ucase_toFullUpper,
dest, destCapacity,
src, &csc, 0, srcLength,
locale, &locCache, pErrorCode);
}
U_CFUNC int32_t
ustr_foldCase(UCaseProps *csp,
UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
uint32_t options,
UErrorCode *pErrorCode) {
int32_t srcIndex, destIndex;
const UChar *s;
UChar32 c;
srcIndex=destIndex=0;
while(srcIndex<srcLength) {
U16_NEXT(src, srcIndex, srcLength, c);
c=ucase_toFullFolding(csp, c, &s, options);
destIndex=appendResult(dest, destIndex, destCapacity, c, s);
}
if(destIndex>destCapacity) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
return destIndex;
}
enum {
TO_LOWER,
TO_UPPER,
TO_TITLE,
FOLD_CASE
};
static int32_t
caseMap(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UBreakIterator *titleIter,
const char *locale,
uint32_t options,
int32_t toWhichCase,
UErrorCode *pErrorCode) {
UChar buffer[300];
UChar *temp;
UCaseProps *csp;
int32_t destLength;
UBool ownTitleIter;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if( destCapacity<0 ||
(dest==NULL && destCapacity>0) ||
src==NULL ||
srcLength<-1
) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
csp=ucase_getSingleton(pErrorCode);
if(U_FAILURE(*pErrorCode)) {
return 0;
}
if(srcLength==-1) {
srcLength=u_strlen(src);
}
if( dest!=NULL &&
((src>=dest && src<(dest+destCapacity)) ||
(dest>=src && dest<(src+srcLength)))
) {
if(destCapacity<=(sizeof(buffer)/U_SIZEOF_UCHAR)) {
temp=buffer;
} else {
temp=(UChar *)uprv_malloc(destCapacity*U_SIZEOF_UCHAR);
if(temp==NULL) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
return 0;
}
}
} else {
temp=dest;
}
ownTitleIter=FALSE;
destLength=0;
if(toWhichCase==FOLD_CASE) {
destLength=ustr_foldCase(csp, temp, destCapacity, src, srcLength,
options, pErrorCode);
} else {
UCaseContext csc={ NULL };
int32_t locCache;
csc.p=(void *)src;
csc.limit=srcLength;
locCache=0;
if(locale==NULL) {
locale=uloc_getDefault();
}
if(toWhichCase==TO_LOWER) {
destLength=_caseMap(csp, ucase_toFullLower,
temp, destCapacity,
src, &csc,
0, srcLength,
locale, &locCache, pErrorCode);
} else if(toWhichCase==TO_UPPER) {
destLength=_caseMap(csp, ucase_toFullUpper,
temp, destCapacity,
src, &csc,
0, srcLength,
locale, &locCache, pErrorCode);
} else {
#if UCONFIG_NO_BREAK_ITERATION
*pErrorCode=U_UNSUPPORTED_ERROR;
#else
if(titleIter==NULL) {
titleIter=ubrk_open(UBRK_WORD, locale,
src, srcLength,
pErrorCode);
ownTitleIter=(UBool)U_SUCCESS(*pErrorCode);
}
if(U_SUCCESS(*pErrorCode)) {
destLength=_toTitle(csp, temp, destCapacity,
src, &csc, srcLength,
titleIter, locale, &locCache, pErrorCode);
}
#endif
}
}
if(temp!=dest) {
if(destLength>0) {
int32_t copyLength= destLength<=destCapacity ? destLength : destCapacity;
if(copyLength>0) {
uprv_memmove(dest, temp, copyLength*U_SIZEOF_UCHAR);
}
}
if(temp!=buffer) {
uprv_free(temp);
}
}
#if !UCONFIG_NO_BREAK_ITERATION
if(ownTitleIter) {
ubrk_close(titleIter);
}
#endif
return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
u_strToLower(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
const char *locale,
UErrorCode *pErrorCode) {
return caseMap(dest, destCapacity,
src, srcLength,
NULL, locale, 0,
TO_LOWER, pErrorCode);
}
U_CAPI int32_t U_EXPORT2
u_strToUpper(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
const char *locale,
UErrorCode *pErrorCode) {
return caseMap(dest, destCapacity,
src, srcLength,
NULL, locale, 0,
TO_UPPER, pErrorCode);
}
#if !UCONFIG_NO_BREAK_ITERATION
U_CAPI int32_t U_EXPORT2
u_strToTitle(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UBreakIterator *titleIter,
const char *locale,
UErrorCode *pErrorCode) {
return caseMap(dest, destCapacity,
src, srcLength,
titleIter, locale, 0,
TO_TITLE, pErrorCode);
}
#endif
U_CAPI int32_t U_EXPORT2
u_strFoldCase(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
uint32_t options,
UErrorCode *pErrorCode) {
return caseMap(dest, destCapacity,
src, srcLength,
NULL, NULL, options,
FOLD_CASE, pErrorCode);
}
struct CmpEquivLevel {
const UChar *start, *s, *limit;
};
typedef struct CmpEquivLevel CmpEquivLevel;
U_CFUNC int32_t
u_strcmpFold(const UChar *s1, int32_t length1,
const UChar *s2, int32_t length2,
uint32_t options,
UErrorCode *pErrorCode) {
UCaseProps *csp;
const UChar *start1, *start2, *limit1, *limit2;
const UChar *p;
int32_t length;
CmpEquivLevel stack1[2], stack2[2];
UChar fold1[UCASE_MAX_STRING_LENGTH+1], fold2[UCASE_MAX_STRING_LENGTH+1];
int32_t level1, level2;
UChar32 c1, c2, cp1, cp2;
csp=ucase_getSingleton(pErrorCode);
if(U_FAILURE(*pErrorCode)) {
return 0;
}
start1=s1;
if(length1==-1) {
limit1=NULL;
} else {
limit1=s1+length1;
}
start2=s2;
if(length2==-1) {
limit2=NULL;
} else {
limit2=s2+length2;
}
level1=level2=0;
c1=c2=-1;
for(;;) {
if(c1<0) {
for(;;) {
if(s1==limit1 || ((c1=*s1)==0 && (limit1==NULL || (options&_STRNCMP_STYLE)))) {
if(level1==0) {
c1=-1;
break;
}
} else {
++s1;
break;
}
do {
--level1;
start1=stack1[level1].start;
} while(start1==NULL);
s1=stack1[level1].s;
limit1=stack1[level1].limit;
}
}
if(c2<0) {
for(;;) {
if(s2==limit2 || ((c2=*s2)==0 && (limit2==NULL || (options&_STRNCMP_STYLE)))) {
if(level2==0) {
c2=-1;
break;
}
} else {
++s2;
break;
}
do {
--level2;
start2=stack2[level2].start;
} while(start2==NULL);
s2=stack2[level2].s;
limit2=stack2[level2].limit;
}
}
if(c1==c2) {
if(c1<0) {
return 0;
}
c1=c2=-1;
continue;
} else if(c1<0) {
return -1;
} else if(c2<0) {
return 1;
}
cp1=c1;
if(U_IS_SURROGATE(c1)) {
UChar c;
if(U_IS_SURROGATE_LEAD(c1)) {
if(s1!=limit1 && U16_IS_TRAIL(c=*s1)) {
cp1=U16_GET_SUPPLEMENTARY(c1, c);
}
} else {
if(start1<=(s1-2) && U16_IS_LEAD(c=*(s1-2))) {
cp1=U16_GET_SUPPLEMENTARY(c, c1);
}
}
}
cp2=c2;
if(U_IS_SURROGATE(c2)) {
UChar c;
if(U_IS_SURROGATE_LEAD(c2)) {
if(s2!=limit2 && U16_IS_TRAIL(c=*s2)) {
cp2=U16_GET_SUPPLEMENTARY(c2, c);
}
} else {
if(start2<=(s2-2) && U16_IS_LEAD(c=*(s2-2))) {
cp2=U16_GET_SUPPLEMENTARY(c, c2);
}
}
}
if( level1==0 &&
(length=ucase_toFullFolding(csp, (UChar32)cp1, &p, options))>=0
) {
if(U_IS_SURROGATE(c1)) {
if(U_IS_SURROGATE_LEAD(c1)) {
++s1;
} else {
--s2;
c2=*(s2-1);
}
}
stack1[0].start=start1;
stack1[0].s=s1;
stack1[0].limit=limit1;
++level1;
if(length<=UCASE_MAX_STRING_LENGTH) {
u_memcpy(fold1, p, length);
} else {
int32_t i=0;
U16_APPEND_UNSAFE(fold1, i, length);
length=i;
}
start1=s1=fold1;
limit1=fold1+length;
c1=-1;
continue;
}
if( level2==0 &&
(length=ucase_toFullFolding(csp, (UChar32)cp2, &p, options))>=0
) {
if(U_IS_SURROGATE(c2)) {
if(U_IS_SURROGATE_LEAD(c2)) {
++s2;
} else {
--s1;
c1=*(s1-1);
}
}
stack2[0].start=start2;
stack2[0].s=s2;
stack2[0].limit=limit2;
++level2;
if(length<=UCASE_MAX_STRING_LENGTH) {
u_memcpy(fold2, p, length);
} else {
int32_t i=0;
U16_APPEND_UNSAFE(fold2, i, length);
length=i;
}
start2=s2=fold2;
limit2=fold2+length;
c2=-1;
continue;
}
if(c1>=0xd800 && c2>=0xd800 && (options&U_COMPARE_CODE_POINT_ORDER)) {
if(
(c1<=0xdbff && s1!=limit1 && U16_IS_TRAIL(*s1)) ||
(U16_IS_TRAIL(c1) && start1!=(s1-1) && U16_IS_LEAD(*(s1-2)))
) {
} else {
c1-=0x2800;
}
if(
(c2<=0xdbff && s2!=limit2 && U16_IS_TRAIL(*s2)) ||
(U16_IS_TRAIL(c2) && start2!=(s2-1) && U16_IS_LEAD(*(s2-2)))
) {
} else {
c2-=0x2800;
}
}
return c1-c2;
}
}
U_CAPI int32_t U_EXPORT2
u_strCaseCompare(const UChar *s1, int32_t length1,
const UChar *s2, int32_t length2,
uint32_t options,
UErrorCode *pErrorCode) {
if(pErrorCode==0 || U_FAILURE(*pErrorCode)) {
return 0;
}
if(s1==NULL || length1<-1 || s2==NULL || length2<-1) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return u_strcmpFold(s1, length1, s2, length2,
options|U_COMPARE_IGNORE_CASE,
pErrorCode);
}
U_CAPI int32_t U_EXPORT2
u_strcasecmp(const UChar *s1, const UChar *s2, uint32_t options) {
UErrorCode errorCode=U_ZERO_ERROR;
return u_strcmpFold(s1, -1, s2, -1,
options|U_COMPARE_IGNORE_CASE,
&errorCode);
}
U_CAPI int32_t U_EXPORT2
u_memcasecmp(const UChar *s1, const UChar *s2, int32_t length, uint32_t options) {
UErrorCode errorCode=U_ZERO_ERROR;
return u_strcmpFold(s1, length, s2, length,
options|U_COMPARE_IGNORE_CASE,
&errorCode);
}
U_CAPI int32_t U_EXPORT2
u_strncasecmp(const UChar *s1, const UChar *s2, int32_t n, uint32_t options) {
UErrorCode errorCode=U_ZERO_ERROR;
return u_strcmpFold(s1, n, s2, n,
options|(U_COMPARE_IGNORE_CASE|_STRNCMP_STYLE),
&errorCode);
}