Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2005-2011, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: ucasemap.cpp |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2005may06 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | * |
michael@0 | 16 | * Case mapping service object and functions using it. |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include "unicode/utypes.h" |
michael@0 | 20 | #include "unicode/brkiter.h" |
michael@0 | 21 | #include "unicode/ubrk.h" |
michael@0 | 22 | #include "unicode/uloc.h" |
michael@0 | 23 | #include "unicode/ustring.h" |
michael@0 | 24 | #include "unicode/ucasemap.h" |
michael@0 | 25 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 26 | #include "unicode/utext.h" |
michael@0 | 27 | #endif |
michael@0 | 28 | #include "unicode/utf.h" |
michael@0 | 29 | #include "unicode/utf8.h" |
michael@0 | 30 | #include "unicode/utf16.h" |
michael@0 | 31 | #include "cmemory.h" |
michael@0 | 32 | #include "cstring.h" |
michael@0 | 33 | #include "ucase.h" |
michael@0 | 34 | #include "ustr_imp.h" |
michael@0 | 35 | |
michael@0 | 36 | U_NAMESPACE_USE |
michael@0 | 37 | |
michael@0 | 38 | /* UCaseMap service object -------------------------------------------------- */ |
michael@0 | 39 | |
michael@0 | 40 | U_CAPI UCaseMap * U_EXPORT2 |
michael@0 | 41 | ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode) { |
michael@0 | 42 | UCaseMap *csm; |
michael@0 | 43 | |
michael@0 | 44 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 45 | return NULL; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | csm=(UCaseMap *)uprv_malloc(sizeof(UCaseMap)); |
michael@0 | 49 | if(csm==NULL) { |
michael@0 | 50 | return NULL; |
michael@0 | 51 | } |
michael@0 | 52 | uprv_memset(csm, 0, sizeof(UCaseMap)); |
michael@0 | 53 | |
michael@0 | 54 | csm->csp=ucase_getSingleton(); |
michael@0 | 55 | ucasemap_setLocale(csm, locale, pErrorCode); |
michael@0 | 56 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 57 | uprv_free(csm); |
michael@0 | 58 | return NULL; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | csm->options=options; |
michael@0 | 62 | return csm; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | U_CAPI void U_EXPORT2 |
michael@0 | 66 | ucasemap_close(UCaseMap *csm) { |
michael@0 | 67 | if(csm!=NULL) { |
michael@0 | 68 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 69 | // Do not call ubrk_close() so that we do not depend on all of the BreakIterator code. |
michael@0 | 70 | delete reinterpret_cast<BreakIterator *>(csm->iter); |
michael@0 | 71 | #endif |
michael@0 | 72 | uprv_free(csm); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | U_CAPI const char * U_EXPORT2 |
michael@0 | 77 | ucasemap_getLocale(const UCaseMap *csm) { |
michael@0 | 78 | return csm->locale; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | U_CAPI uint32_t U_EXPORT2 |
michael@0 | 82 | ucasemap_getOptions(const UCaseMap *csm) { |
michael@0 | 83 | return csm->options; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | U_CAPI void U_EXPORT2 |
michael@0 | 87 | ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) { |
michael@0 | 88 | int32_t length; |
michael@0 | 89 | |
michael@0 | 90 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 91 | return; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | length=uloc_getName(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode); |
michael@0 | 95 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || length==sizeof(csm->locale)) { |
michael@0 | 96 | *pErrorCode=U_ZERO_ERROR; |
michael@0 | 97 | /* we only really need the language code for case mappings */ |
michael@0 | 98 | length=uloc_getLanguage(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode); |
michael@0 | 99 | } |
michael@0 | 100 | if(length==sizeof(csm->locale)) { |
michael@0 | 101 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 102 | } |
michael@0 | 103 | csm->locCache=0; |
michael@0 | 104 | if(U_SUCCESS(*pErrorCode)) { |
michael@0 | 105 | ucase_getCaseLocale(csm->locale, &csm->locCache); |
michael@0 | 106 | } else { |
michael@0 | 107 | csm->locale[0]=0; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | U_CAPI void U_EXPORT2 |
michael@0 | 112 | ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode * /*pErrorCode*/) { |
michael@0 | 113 | csm->options=options; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /* UTF-8 string case mappings ----------------------------------------------- */ |
michael@0 | 117 | |
michael@0 | 118 | /* TODO(markus): Move to a new, separate utf8case.c file. */ |
michael@0 | 119 | |
michael@0 | 120 | /* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */ |
michael@0 | 121 | static inline int32_t |
michael@0 | 122 | appendResult(uint8_t *dest, int32_t destIndex, int32_t destCapacity, |
michael@0 | 123 | int32_t result, const UChar *s) { |
michael@0 | 124 | UChar32 c; |
michael@0 | 125 | int32_t length, destLength; |
michael@0 | 126 | UErrorCode errorCode; |
michael@0 | 127 | |
michael@0 | 128 | /* decode the result */ |
michael@0 | 129 | if(result<0) { |
michael@0 | 130 | /* (not) original code point */ |
michael@0 | 131 | c=~result; |
michael@0 | 132 | length=-1; |
michael@0 | 133 | } else if(result<=UCASE_MAX_STRING_LENGTH) { |
michael@0 | 134 | c=U_SENTINEL; |
michael@0 | 135 | length=result; |
michael@0 | 136 | } else { |
michael@0 | 137 | c=result; |
michael@0 | 138 | length=-1; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | if(destIndex<destCapacity) { |
michael@0 | 142 | /* append the result */ |
michael@0 | 143 | if(length<0) { |
michael@0 | 144 | /* code point */ |
michael@0 | 145 | UBool isError=FALSE; |
michael@0 | 146 | U8_APPEND(dest, destIndex, destCapacity, c, isError); |
michael@0 | 147 | if(isError) { |
michael@0 | 148 | /* overflow, nothing written */ |
michael@0 | 149 | destIndex+=U8_LENGTH(c); |
michael@0 | 150 | } |
michael@0 | 151 | } else { |
michael@0 | 152 | /* string */ |
michael@0 | 153 | errorCode=U_ZERO_ERROR; |
michael@0 | 154 | u_strToUTF8( |
michael@0 | 155 | (char *)(dest+destIndex), destCapacity-destIndex, &destLength, |
michael@0 | 156 | s, length, |
michael@0 | 157 | &errorCode); |
michael@0 | 158 | destIndex+=destLength; |
michael@0 | 159 | /* we might have an overflow, but we know the actual length */ |
michael@0 | 160 | } |
michael@0 | 161 | } else { |
michael@0 | 162 | /* preflight */ |
michael@0 | 163 | if(length<0) { |
michael@0 | 164 | destIndex+=U8_LENGTH(c); |
michael@0 | 165 | } else { |
michael@0 | 166 | errorCode=U_ZERO_ERROR; |
michael@0 | 167 | u_strToUTF8( |
michael@0 | 168 | NULL, 0, &destLength, |
michael@0 | 169 | s, length, |
michael@0 | 170 | &errorCode); |
michael@0 | 171 | destIndex+=destLength; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | return destIndex; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | static UChar32 U_CALLCONV |
michael@0 | 178 | utf8_caseContextIterator(void *context, int8_t dir) { |
michael@0 | 179 | UCaseContext *csc=(UCaseContext *)context; |
michael@0 | 180 | UChar32 c; |
michael@0 | 181 | |
michael@0 | 182 | if(dir<0) { |
michael@0 | 183 | /* reset for backward iteration */ |
michael@0 | 184 | csc->index=csc->cpStart; |
michael@0 | 185 | csc->dir=dir; |
michael@0 | 186 | } else if(dir>0) { |
michael@0 | 187 | /* reset for forward iteration */ |
michael@0 | 188 | csc->index=csc->cpLimit; |
michael@0 | 189 | csc->dir=dir; |
michael@0 | 190 | } else { |
michael@0 | 191 | /* continue current iteration direction */ |
michael@0 | 192 | dir=csc->dir; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if(dir<0) { |
michael@0 | 196 | if(csc->start<csc->index) { |
michael@0 | 197 | U8_PREV((const uint8_t *)csc->p, csc->start, csc->index, c); |
michael@0 | 198 | return c; |
michael@0 | 199 | } |
michael@0 | 200 | } else { |
michael@0 | 201 | if(csc->index<csc->limit) { |
michael@0 | 202 | U8_NEXT((const uint8_t *)csc->p, csc->index, csc->limit, c); |
michael@0 | 203 | return c; |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | return U_SENTINEL; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | /* |
michael@0 | 210 | * Case-maps [srcStart..srcLimit[ but takes |
michael@0 | 211 | * context [0..srcLength[ into account. |
michael@0 | 212 | */ |
michael@0 | 213 | static int32_t |
michael@0 | 214 | _caseMap(const UCaseMap *csm, UCaseMapFull *map, |
michael@0 | 215 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 216 | const uint8_t *src, UCaseContext *csc, |
michael@0 | 217 | int32_t srcStart, int32_t srcLimit, |
michael@0 | 218 | UErrorCode *pErrorCode) { |
michael@0 | 219 | const UChar *s; |
michael@0 | 220 | UChar32 c, c2 = 0; |
michael@0 | 221 | int32_t srcIndex, destIndex; |
michael@0 | 222 | int32_t locCache; |
michael@0 | 223 | |
michael@0 | 224 | locCache=csm->locCache; |
michael@0 | 225 | |
michael@0 | 226 | /* case mapping loop */ |
michael@0 | 227 | srcIndex=srcStart; |
michael@0 | 228 | destIndex=0; |
michael@0 | 229 | while(srcIndex<srcLimit) { |
michael@0 | 230 | csc->cpStart=srcIndex; |
michael@0 | 231 | U8_NEXT(src, srcIndex, srcLimit, c); |
michael@0 | 232 | csc->cpLimit=srcIndex; |
michael@0 | 233 | if(c<0) { |
michael@0 | 234 | int32_t i=csc->cpStart; |
michael@0 | 235 | while(destIndex<destCapacity && i<srcIndex) { |
michael@0 | 236 | dest[destIndex++]=src[i++]; |
michael@0 | 237 | } |
michael@0 | 238 | continue; |
michael@0 | 239 | } |
michael@0 | 240 | c=map(csm->csp, c, utf8_caseContextIterator, csc, &s, csm->locale, &locCache); |
michael@0 | 241 | if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) { |
michael@0 | 242 | /* fast path version of appendResult() for ASCII results */ |
michael@0 | 243 | dest[destIndex++]=(uint8_t)c2; |
michael@0 | 244 | } else { |
michael@0 | 245 | destIndex=appendResult(dest, destIndex, destCapacity, c, s); |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | if(destIndex>destCapacity) { |
michael@0 | 250 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 251 | } |
michael@0 | 252 | return destIndex; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 256 | |
michael@0 | 257 | U_CFUNC int32_t U_CALLCONV |
michael@0 | 258 | ucasemap_internalUTF8ToTitle(const UCaseMap *csm, |
michael@0 | 259 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 260 | const uint8_t *src, int32_t srcLength, |
michael@0 | 261 | UErrorCode *pErrorCode) { |
michael@0 | 262 | const UChar *s; |
michael@0 | 263 | UChar32 c; |
michael@0 | 264 | int32_t prev, titleStart, titleLimit, idx, destIndex, length; |
michael@0 | 265 | UBool isFirstIndex; |
michael@0 | 266 | |
michael@0 | 267 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 268 | return 0; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | // Use the C++ abstract base class to minimize dependencies. |
michael@0 | 272 | // TODO: Change UCaseMap.iter to store a BreakIterator directly. |
michael@0 | 273 | BreakIterator *bi=reinterpret_cast<BreakIterator *>(csm->iter); |
michael@0 | 274 | |
michael@0 | 275 | /* set up local variables */ |
michael@0 | 276 | int32_t locCache=csm->locCache; |
michael@0 | 277 | UCaseContext csc=UCASECONTEXT_INITIALIZER; |
michael@0 | 278 | csc.p=(void *)src; |
michael@0 | 279 | csc.limit=srcLength; |
michael@0 | 280 | destIndex=0; |
michael@0 | 281 | prev=0; |
michael@0 | 282 | isFirstIndex=TRUE; |
michael@0 | 283 | |
michael@0 | 284 | /* titlecasing loop */ |
michael@0 | 285 | while(prev<srcLength) { |
michael@0 | 286 | /* find next index where to titlecase */ |
michael@0 | 287 | if(isFirstIndex) { |
michael@0 | 288 | isFirstIndex=FALSE; |
michael@0 | 289 | idx=bi->first(); |
michael@0 | 290 | } else { |
michael@0 | 291 | idx=bi->next(); |
michael@0 | 292 | } |
michael@0 | 293 | if(idx==UBRK_DONE || idx>srcLength) { |
michael@0 | 294 | idx=srcLength; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | /* |
michael@0 | 298 | * Unicode 4 & 5 section 3.13 Default Case Operations: |
michael@0 | 299 | * |
michael@0 | 300 | * R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex |
michael@0 | 301 | * #29, "Text Boundaries." Between each pair of word boundaries, find the first |
michael@0 | 302 | * cased character F. If F exists, map F to default_title(F); then map each |
michael@0 | 303 | * subsequent character C to default_lower(C). |
michael@0 | 304 | * |
michael@0 | 305 | * In this implementation, segment [prev..index[ into 3 parts: |
michael@0 | 306 | * a) uncased characters (copy as-is) [prev..titleStart[ |
michael@0 | 307 | * b) first case letter (titlecase) [titleStart..titleLimit[ |
michael@0 | 308 | * c) subsequent characters (lowercase) [titleLimit..index[ |
michael@0 | 309 | */ |
michael@0 | 310 | if(prev<idx) { |
michael@0 | 311 | /* find and copy uncased characters [prev..titleStart[ */ |
michael@0 | 312 | titleStart=titleLimit=prev; |
michael@0 | 313 | U8_NEXT(src, titleLimit, idx, c); |
michael@0 | 314 | if((csm->options&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0 && UCASE_NONE==ucase_getType(csm->csp, c)) { |
michael@0 | 315 | /* Adjust the titlecasing index (titleStart) to the next cased character. */ |
michael@0 | 316 | for(;;) { |
michael@0 | 317 | titleStart=titleLimit; |
michael@0 | 318 | if(titleLimit==idx) { |
michael@0 | 319 | /* |
michael@0 | 320 | * only uncased characters in [prev..index[ |
michael@0 | 321 | * stop with titleStart==titleLimit==index |
michael@0 | 322 | */ |
michael@0 | 323 | break; |
michael@0 | 324 | } |
michael@0 | 325 | U8_NEXT(src, titleLimit, idx, c); |
michael@0 | 326 | if(UCASE_NONE!=ucase_getType(csm->csp, c)) { |
michael@0 | 327 | break; /* cased letter at [titleStart..titleLimit[ */ |
michael@0 | 328 | } |
michael@0 | 329 | } |
michael@0 | 330 | length=titleStart-prev; |
michael@0 | 331 | if(length>0) { |
michael@0 | 332 | if((destIndex+length)<=destCapacity) { |
michael@0 | 333 | uprv_memcpy(dest+destIndex, src+prev, length); |
michael@0 | 334 | } |
michael@0 | 335 | destIndex+=length; |
michael@0 | 336 | } |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | if(titleStart<titleLimit) { |
michael@0 | 340 | /* titlecase c which is from [titleStart..titleLimit[ */ |
michael@0 | 341 | csc.cpStart=titleStart; |
michael@0 | 342 | csc.cpLimit=titleLimit; |
michael@0 | 343 | c=ucase_toFullTitle(csm->csp, c, utf8_caseContextIterator, &csc, &s, csm->locale, &locCache); |
michael@0 | 344 | destIndex=appendResult(dest, destIndex, destCapacity, c, s); |
michael@0 | 345 | |
michael@0 | 346 | /* Special case Dutch IJ titlecasing */ |
michael@0 | 347 | if ( titleStart+1 < idx && |
michael@0 | 348 | ucase_getCaseLocale(csm->locale, &locCache) == UCASE_LOC_DUTCH && |
michael@0 | 349 | ( src[titleStart] == 0x0049 || src[titleStart] == 0x0069 ) && |
michael@0 | 350 | ( src[titleStart+1] == 0x004A || src[titleStart+1] == 0x006A )) { |
michael@0 | 351 | c=0x004A; |
michael@0 | 352 | destIndex=appendResult(dest, destIndex, destCapacity, c, s); |
michael@0 | 353 | titleLimit++; |
michael@0 | 354 | } |
michael@0 | 355 | /* lowercase [titleLimit..index[ */ |
michael@0 | 356 | if(titleLimit<idx) { |
michael@0 | 357 | if((csm->options&U_TITLECASE_NO_LOWERCASE)==0) { |
michael@0 | 358 | /* Normal operation: Lowercase the rest of the word. */ |
michael@0 | 359 | destIndex+= |
michael@0 | 360 | _caseMap( |
michael@0 | 361 | csm, ucase_toFullLower, |
michael@0 | 362 | dest+destIndex, destCapacity-destIndex, |
michael@0 | 363 | src, &csc, |
michael@0 | 364 | titleLimit, idx, |
michael@0 | 365 | pErrorCode); |
michael@0 | 366 | } else { |
michael@0 | 367 | /* Optionally just copy the rest of the word unchanged. */ |
michael@0 | 368 | length=idx-titleLimit; |
michael@0 | 369 | if((destIndex+length)<=destCapacity) { |
michael@0 | 370 | uprv_memcpy(dest+destIndex, src+titleLimit, length); |
michael@0 | 371 | } |
michael@0 | 372 | destIndex+=length; |
michael@0 | 373 | } |
michael@0 | 374 | } |
michael@0 | 375 | } |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | prev=idx; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | if(destIndex>destCapacity) { |
michael@0 | 382 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 383 | } |
michael@0 | 384 | return destIndex; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | #endif |
michael@0 | 388 | |
michael@0 | 389 | static int32_t U_CALLCONV |
michael@0 | 390 | ucasemap_internalUTF8ToLower(const UCaseMap *csm, |
michael@0 | 391 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 392 | const uint8_t *src, int32_t srcLength, |
michael@0 | 393 | UErrorCode *pErrorCode) { |
michael@0 | 394 | UCaseContext csc=UCASECONTEXT_INITIALIZER; |
michael@0 | 395 | csc.p=(void *)src; |
michael@0 | 396 | csc.limit=srcLength; |
michael@0 | 397 | return _caseMap( |
michael@0 | 398 | csm, ucase_toFullLower, |
michael@0 | 399 | dest, destCapacity, |
michael@0 | 400 | src, &csc, 0, srcLength, |
michael@0 | 401 | pErrorCode); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | static int32_t U_CALLCONV |
michael@0 | 405 | ucasemap_internalUTF8ToUpper(const UCaseMap *csm, |
michael@0 | 406 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 407 | const uint8_t *src, int32_t srcLength, |
michael@0 | 408 | UErrorCode *pErrorCode) { |
michael@0 | 409 | UCaseContext csc=UCASECONTEXT_INITIALIZER; |
michael@0 | 410 | csc.p=(void *)src; |
michael@0 | 411 | csc.limit=srcLength; |
michael@0 | 412 | return _caseMap( |
michael@0 | 413 | csm, ucase_toFullUpper, |
michael@0 | 414 | dest, destCapacity, |
michael@0 | 415 | src, &csc, 0, srcLength, |
michael@0 | 416 | pErrorCode); |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | static int32_t |
michael@0 | 420 | utf8_foldCase(const UCaseProps *csp, |
michael@0 | 421 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 422 | const uint8_t *src, int32_t srcLength, |
michael@0 | 423 | uint32_t options, |
michael@0 | 424 | UErrorCode *pErrorCode) { |
michael@0 | 425 | int32_t srcIndex, destIndex; |
michael@0 | 426 | |
michael@0 | 427 | const UChar *s; |
michael@0 | 428 | UChar32 c, c2; |
michael@0 | 429 | int32_t start; |
michael@0 | 430 | |
michael@0 | 431 | /* case mapping loop */ |
michael@0 | 432 | srcIndex=destIndex=0; |
michael@0 | 433 | while(srcIndex<srcLength) { |
michael@0 | 434 | start=srcIndex; |
michael@0 | 435 | U8_NEXT(src, srcIndex, srcLength, c); |
michael@0 | 436 | if(c<0) { |
michael@0 | 437 | while(destIndex<destCapacity && start<srcIndex) { |
michael@0 | 438 | dest[destIndex++]=src[start++]; |
michael@0 | 439 | } |
michael@0 | 440 | continue; |
michael@0 | 441 | } |
michael@0 | 442 | c=ucase_toFullFolding(csp, c, &s, options); |
michael@0 | 443 | if((destIndex<destCapacity) && (c<0 ? (c2=~c)<=0x7f : UCASE_MAX_STRING_LENGTH<c && (c2=c)<=0x7f)) { |
michael@0 | 444 | /* fast path version of appendResult() for ASCII results */ |
michael@0 | 445 | dest[destIndex++]=(uint8_t)c2; |
michael@0 | 446 | } else { |
michael@0 | 447 | destIndex=appendResult(dest, destIndex, destCapacity, c, s); |
michael@0 | 448 | } |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | if(destIndex>destCapacity) { |
michael@0 | 452 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 453 | } |
michael@0 | 454 | return destIndex; |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | static int32_t U_CALLCONV |
michael@0 | 458 | ucasemap_internalUTF8Fold(const UCaseMap *csm, |
michael@0 | 459 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 460 | const uint8_t *src, int32_t srcLength, |
michael@0 | 461 | UErrorCode *pErrorCode) { |
michael@0 | 462 | return utf8_foldCase(csm->csp, dest, destCapacity, src, srcLength, csm->options, pErrorCode); |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | U_CFUNC int32_t |
michael@0 | 466 | ucasemap_mapUTF8(const UCaseMap *csm, |
michael@0 | 467 | uint8_t *dest, int32_t destCapacity, |
michael@0 | 468 | const uint8_t *src, int32_t srcLength, |
michael@0 | 469 | UTF8CaseMapper *stringCaseMapper, |
michael@0 | 470 | UErrorCode *pErrorCode) { |
michael@0 | 471 | int32_t destLength; |
michael@0 | 472 | |
michael@0 | 473 | /* check argument values */ |
michael@0 | 474 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 475 | return 0; |
michael@0 | 476 | } |
michael@0 | 477 | if( destCapacity<0 || |
michael@0 | 478 | (dest==NULL && destCapacity>0) || |
michael@0 | 479 | src==NULL || |
michael@0 | 480 | srcLength<-1 |
michael@0 | 481 | ) { |
michael@0 | 482 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 483 | return 0; |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | /* get the string length */ |
michael@0 | 487 | if(srcLength==-1) { |
michael@0 | 488 | srcLength=(int32_t)uprv_strlen((const char *)src); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | /* check for overlapping source and destination */ |
michael@0 | 492 | if( dest!=NULL && |
michael@0 | 493 | ((src>=dest && src<(dest+destCapacity)) || |
michael@0 | 494 | (dest>=src && dest<(src+srcLength))) |
michael@0 | 495 | ) { |
michael@0 | 496 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 497 | return 0; |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | destLength=stringCaseMapper(csm, dest, destCapacity, src, srcLength, pErrorCode); |
michael@0 | 501 | return u_terminateChars((char *)dest, destCapacity, destLength, pErrorCode); |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | /* public API functions */ |
michael@0 | 505 | |
michael@0 | 506 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 507 | ucasemap_utf8ToLower(const UCaseMap *csm, |
michael@0 | 508 | char *dest, int32_t destCapacity, |
michael@0 | 509 | const char *src, int32_t srcLength, |
michael@0 | 510 | UErrorCode *pErrorCode) { |
michael@0 | 511 | return ucasemap_mapUTF8(csm, |
michael@0 | 512 | (uint8_t *)dest, destCapacity, |
michael@0 | 513 | (const uint8_t *)src, srcLength, |
michael@0 | 514 | ucasemap_internalUTF8ToLower, pErrorCode); |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 518 | ucasemap_utf8ToUpper(const UCaseMap *csm, |
michael@0 | 519 | char *dest, int32_t destCapacity, |
michael@0 | 520 | const char *src, int32_t srcLength, |
michael@0 | 521 | UErrorCode *pErrorCode) { |
michael@0 | 522 | return ucasemap_mapUTF8(csm, |
michael@0 | 523 | (uint8_t *)dest, destCapacity, |
michael@0 | 524 | (const uint8_t *)src, srcLength, |
michael@0 | 525 | ucasemap_internalUTF8ToUpper, pErrorCode); |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 529 | ucasemap_utf8FoldCase(const UCaseMap *csm, |
michael@0 | 530 | char *dest, int32_t destCapacity, |
michael@0 | 531 | const char *src, int32_t srcLength, |
michael@0 | 532 | UErrorCode *pErrorCode) { |
michael@0 | 533 | return ucasemap_mapUTF8(csm, |
michael@0 | 534 | (uint8_t *)dest, destCapacity, |
michael@0 | 535 | (const uint8_t *)src, srcLength, |
michael@0 | 536 | ucasemap_internalUTF8Fold, pErrorCode); |
michael@0 | 537 | } |