1/*
2*******************************************************************************
3*   Copyright (C) 2011, International Business Machines
4*   Corporation and others.  All Rights Reserved.
5*******************************************************************************
6*   file name:  unistr_titlecase_brkiter.cpp
7*   encoding:   US-ASCII
8*   tab size:   8 (not used)
9*   indentation:2
10*
11*   created on: 2011may30
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/unistr.h"
25#include "unicode/ustring.h"
26#include "cmemory.h"
27#include "ustr_imp.h"
28
29static int32_t U_CALLCONV
30unistr_case_internalToTitle(const UCaseMap *csm,
31                            UChar *dest, int32_t destCapacity,
32                            const UChar *src, int32_t srcLength,
33                            UErrorCode *pErrorCode) {
34  ubrk_setText(csm->iter, src, srcLength, pErrorCode);
35  return ustrcase_internalToTitle(csm, dest, destCapacity, src, srcLength, pErrorCode);
36}
37
38/*
39 * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
40 * Do this fast because it is called with every function call.
41 */
42static inline void
43setTempCaseMap(UCaseMap *csm, const char *locale) {
44    if(csm->csp==NULL) {
45        csm->csp=ucase_getSingleton();
46    }
47    if(locale!=NULL && locale[0]==0) {
48        csm->locale[0]=0;
49    } else {
50        ustrcase_setTempCaseMapLocale(csm, locale);
51    }
52}
53
54U_NAMESPACE_BEGIN
55
56UnicodeString &
57UnicodeString::toTitle(BreakIterator *titleIter) {
58  return toTitle(titleIter, Locale::getDefault(), 0);
59}
60
61UnicodeString &
62UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) {
63  return toTitle(titleIter, locale, 0);
64}
65
66UnicodeString &
67UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options) {
68  UCaseMap csm=UCASEMAP_INITIALIZER;
69  csm.options=options;
70  setTempCaseMap(&csm, locale.getName());
71  BreakIterator *bi=titleIter;
72  if(bi==NULL) {
73    UErrorCode errorCode=U_ZERO_ERROR;
74    bi=BreakIterator::createWordInstance(locale, errorCode);
75    if(U_FAILURE(errorCode)) {
76      setToBogus();
77      return *this;
78    }
79  }
80  csm.iter=reinterpret_cast<UBreakIterator *>(bi);
81  caseMap(&csm, unistr_case_internalToTitle);
82  if(titleIter==NULL) {
83    delete bi;
84  }
85  return *this;
86}
87
88U_NAMESPACE_END
89
90#endif  // !UCONFIG_NO_BREAK_ITERATION
91