1 /*
2 *******************************************************************************
3 *   Copyright (C) 2011, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 *   file name:  ucasemap_titlecase_brkiter.cpp
7 *   encoding:   US-ASCII
8 *   tab size:   8 (not used)
9 *   indentation:4
10 *
11 *   created on: 2011jun02
12 *   created by: Markus W. Scherer
13 *
14 *   Titlecasing functions that are based on BreakIterator
15 *   were moved here to break dependency cycles among parts of the common library.
16 */
17 
18 #include "unicode/utypes.h"
19 
20 #if !UCONFIG_NO_BREAK_ITERATION
21 
22 #include "unicode/brkiter.h"
23 #include "unicode/ubrk.h"
24 #include "unicode/ucasemap.h"
25 #include "cmemory.h"
26 #include "ucase.h"
27 #include "ustr_imp.h"
28 
29 U_NAMESPACE_USE
30 
31 U_CAPI const UBreakIterator * U_EXPORT2
ucasemap_getBreakIterator(const UCaseMap * csm)32 ucasemap_getBreakIterator(const UCaseMap *csm) {
33     return csm->iter;
34 }
35 
36 U_CAPI void U_EXPORT2
ucasemap_setBreakIterator(UCaseMap * csm,UBreakIterator * iterToAdopt,UErrorCode *)37 ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode * /*pErrorCode*/) {
38     // Do not call ubrk_close() so that we do not depend on all of the BreakIterator code.
39     delete reinterpret_cast<BreakIterator *>(csm->iter);
40     csm->iter=iterToAdopt;
41 }
42 
43 U_CAPI int32_t U_EXPORT2
ucasemap_utf8ToTitle(UCaseMap * csm,char * dest,int32_t destCapacity,const char * src,int32_t srcLength,UErrorCode * pErrorCode)44 ucasemap_utf8ToTitle(UCaseMap *csm,
45                      char *dest, int32_t destCapacity,
46                      const char *src, int32_t srcLength,
47                      UErrorCode *pErrorCode) {
48     UText utext=UTEXT_INITIALIZER;
49     utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode);
50     if(U_FAILURE(*pErrorCode)) {
51         return 0;
52     }
53     if(csm->iter==NULL) {
54         csm->iter=ubrk_open(UBRK_WORD, csm->locale,
55                             NULL, 0,
56                             pErrorCode);
57     }
58     ubrk_setUText(csm->iter, &utext, pErrorCode);
59     int32_t length=ucasemap_mapUTF8(csm,
60                    (uint8_t *)dest, destCapacity,
61                    (const uint8_t *)src, srcLength,
62                    ucasemap_internalUTF8ToTitle, pErrorCode);
63     utext_close(&utext);
64     return length;
65 }
66 
67 #endif  // !UCONFIG_NO_BREAK_ITERATION
68