]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustr_titlecase_brkiter.cpp
ICU-59152.0.1.tar.gz
[apple/icu.git] / icuSources / common / ustr_titlecase_brkiter.cpp
1 // © 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: UTF-8
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/casemap.h"
26 #include "unicode/localpointer.h"
27 #include "unicode/ubrk.h"
28 #include "unicode/ucasemap.h"
29 #include "cmemory.h"
30 #include "ucase.h"
31 #include "ucasemap_imp.h"
32
33 U_NAMESPACE_USE
34
35 /* functions available in the common library (for unistr_case.cpp) */
36
37 /* public API functions */
38
39 U_CAPI int32_t U_EXPORT2
40 u_strToTitle(UChar *dest, int32_t destCapacity,
41 const UChar *src, int32_t srcLength,
42 UBreakIterator *titleIter,
43 const char *locale,
44 UErrorCode *pErrorCode) {
45 LocalPointer<BreakIterator> ownedIter;
46 BreakIterator *iter;
47 if(titleIter!=NULL) {
48 iter=reinterpret_cast<BreakIterator *>(titleIter);
49 } else {
50 iter=BreakIterator::createWordInstance(Locale(locale), *pErrorCode);
51 ownedIter.adoptInstead(iter);
52 }
53 if(U_FAILURE(*pErrorCode)) {
54 return 0;
55 }
56 UnicodeString s(srcLength<0, src, srcLength);
57 iter->setText(s);
58 return ustrcase_mapWithOverlap(
59 ustrcase_getCaseLocale(locale), 0, iter,
60 dest, destCapacity,
61 src, srcLength,
62 ustrcase_internalToTitle, *pErrorCode);
63 }
64
65 U_NAMESPACE_BEGIN
66
67 int32_t CaseMap::toTitle(
68 const char *locale, uint32_t options, BreakIterator *iter,
69 const UChar *src, int32_t srcLength,
70 UChar *dest, int32_t destCapacity, Edits *edits,
71 UErrorCode &errorCode) {
72 LocalPointer<BreakIterator> ownedIter;
73 if(iter==NULL) {
74 iter=BreakIterator::createWordInstance(Locale(locale), errorCode);
75 ownedIter.adoptInstead(iter);
76 }
77 if(U_FAILURE(errorCode)) {
78 return 0;
79 }
80 UnicodeString s(srcLength<0, src, srcLength);
81 iter->setText(s);
82 return ustrcase_map(
83 ustrcase_getCaseLocale(locale), options, iter,
84 dest, destCapacity,
85 src, srcLength,
86 ustrcase_internalToTitle, edits, errorCode);
87 }
88
89 U_NAMESPACE_END
90
91 U_CAPI int32_t U_EXPORT2
92 ucasemap_toTitle(UCaseMap *csm,
93 UChar *dest, int32_t destCapacity,
94 const UChar *src, int32_t srcLength,
95 UErrorCode *pErrorCode) {
96 if (U_FAILURE(*pErrorCode)) {
97 return 0;
98 }
99 if (csm->iter == NULL) {
100 csm->iter = BreakIterator::createWordInstance(Locale(csm->locale), *pErrorCode);
101 }
102 if (U_FAILURE(*pErrorCode)) {
103 return 0;
104 }
105 UnicodeString s(srcLength<0, src, srcLength);
106 csm->iter->setText(s);
107 return ustrcase_map(
108 csm->caseLocale, csm->options, csm->iter,
109 dest, destCapacity,
110 src, srcLength,
111 ustrcase_internalToTitle, NULL, *pErrorCode);
112 }
113
114 #endif // !UCONFIG_NO_BREAK_ITERATION