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