1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *   Copyright (C) 2011, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 *   file name:  ustr_titlecase_brkiter.cpp
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2011may30
14 *   created by: Markus W. Scherer
15 *
16 *   Titlecasing functions that are based on BreakIterator
17 *   were moved here to break dependency cycles among parts of the common library.
18 */
19 
20 #include "unicode/utypes.h"
21 
22 #if !UCONFIG_NO_BREAK_ITERATION
23 
24 #include "unicode/brkiter.h"
25 #include "unicode/ubrk.h"
26 #include "unicode/ucasemap.h"
27 #include "cmemory.h"
28 #include "ucase.h"
29 #include "ustr_imp.h"
30 
31 /* functions available in the common library (for unistr_case.cpp) */
32 
33 /*
34  * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
35  * Do this fast because it is called with every function call.
36  * Duplicate of the same function in ustrcase.cpp, to keep it inline.
37  */
38 static inline void
39 setTempCaseMap(UCaseMap *csm, const char *locale) {
40     if(csm->csp==NULL) {
41         csm->csp=ucase_getSingleton();
42     }
43     if(locale!=NULL && locale[0]==0) {
44         csm->locale[0]=0;
45     } else {
46         ustrcase_setTempCaseMapLocale(csm, locale);
47     }
48 }
49 
50 /* public API functions */
51 
52 U_CAPI int32_t U_EXPORT2
53 u_strToTitle(UChar *dest, int32_t destCapacity,
54              const UChar *src, int32_t srcLength,
55              UBreakIterator *titleIter,
56              const char *locale,
57              UErrorCode *pErrorCode) {
58     UCaseMap csm=UCASEMAP_INITIALIZER;
59     setTempCaseMap(&csm, locale);
60     if(titleIter!=NULL) {
61         ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode);
62     } else {
63         csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode);
64     }
65     int32_t length=ustrcase_map(
66         &csm,
67         dest, destCapacity,
68         src, srcLength,
69         ustrcase_internalToTitle, pErrorCode);
70     if(titleIter==NULL && csm.iter!=NULL) {
71         ubrk_close(csm.iter);
72     }
73     return length;
74 }
75 
76 U_CAPI int32_t U_EXPORT2
77 ucasemap_toTitle(UCaseMap *csm,
78                  UChar *dest, int32_t destCapacity,
79                  const UChar *src, int32_t srcLength,
80                  UErrorCode *pErrorCode) {
81     if(csm->iter!=NULL) {
82         ubrk_setText(csm->iter, src, srcLength, pErrorCode);
83     } else {
84         csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode);
85     }
86     return ustrcase_map(
87         csm,
88         dest, destCapacity,
89         src, srcLength,
90         ustrcase_internalToTitle, pErrorCode);
91 }
92 
93 #endif  // !UCONFIG_NO_BREAK_ITERATION
94