]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
4388f060 A |
3 | /* |
4 | ******************************************************************************* | |
5 | * Copyright (C) 2011, International Business Machines | |
6 | * Corporation and others. All Rights Reserved. | |
7 | ******************************************************************************* | |
8 | * file name: ucasemap_titlecase_brkiter.cpp | |
f3c0d7a5 | 9 | * encoding: UTF-8 |
4388f060 A |
10 | * tab size: 8 (not used) |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2011jun02 | |
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" | |
f3c0d7a5 | 26 | #include "unicode/casemap.h" |
4388f060 A |
27 | #include "unicode/ucasemap.h" |
28 | #include "cmemory.h" | |
29 | #include "ucase.h" | |
f3c0d7a5 A |
30 | #include "ucasemap_imp.h" |
31 | ||
32 | U_NAMESPACE_BEGIN | |
33 | ||
0f5d89e8 A |
34 | void CaseMap::utf8ToTitle( |
35 | const char *locale, uint32_t options, BreakIterator *iter, | |
36 | StringPiece src, ByteSink &sink, Edits *edits, | |
37 | UErrorCode &errorCode) { | |
38 | if (U_FAILURE(errorCode)) { | |
39 | return; | |
40 | } | |
41 | UText utext = UTEXT_INITIALIZER; | |
42 | utext_openUTF8(&utext, src.data(), src.length(), &errorCode); | |
43 | LocalPointer<BreakIterator> ownedIter; | |
44 | iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode); | |
45 | if (iter == nullptr) { | |
46 | utext_close(&utext); | |
47 | return; | |
48 | } | |
49 | iter->setText(&utext, errorCode); | |
50 | ucasemap_mapUTF8( | |
51 | ustrcase_getCaseLocale(locale), options, iter, | |
52 | src.data(), src.length(), | |
53 | ucasemap_internalUTF8ToTitle, sink, edits, errorCode); | |
54 | utext_close(&utext); | |
55 | } | |
56 | ||
f3c0d7a5 A |
57 | int32_t CaseMap::utf8ToTitle( |
58 | const char *locale, uint32_t options, BreakIterator *iter, | |
59 | const char *src, int32_t srcLength, | |
60 | char *dest, int32_t destCapacity, Edits *edits, | |
61 | UErrorCode &errorCode) { | |
62 | if (U_FAILURE(errorCode)) { | |
63 | return 0; | |
64 | } | |
65 | UText utext=UTEXT_INITIALIZER; | |
66 | utext_openUTF8(&utext, src, srcLength, &errorCode); | |
67 | LocalPointer<BreakIterator> ownedIter; | |
0f5d89e8 | 68 | iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode); |
f3c0d7a5 | 69 | if(iter==NULL) { |
f3c0d7a5 A |
70 | utext_close(&utext); |
71 | return 0; | |
72 | } | |
73 | iter->setText(&utext, errorCode); | |
74 | int32_t length=ucasemap_mapUTF8( | |
75 | ustrcase_getCaseLocale(locale), options, iter, | |
0f5d89e8 A |
76 | dest, destCapacity, |
77 | src, srcLength, | |
f3c0d7a5 A |
78 | ucasemap_internalUTF8ToTitle, edits, errorCode); |
79 | utext_close(&utext); | |
80 | return length; | |
81 | } | |
82 | ||
83 | U_NAMESPACE_END | |
4388f060 A |
84 | |
85 | U_NAMESPACE_USE | |
86 | ||
87 | U_CAPI const UBreakIterator * U_EXPORT2 | |
88 | ucasemap_getBreakIterator(const UCaseMap *csm) { | |
f3c0d7a5 | 89 | return reinterpret_cast<UBreakIterator *>(csm->iter); |
4388f060 A |
90 | } |
91 | ||
92 | U_CAPI void U_EXPORT2 | |
f3c0d7a5 A |
93 | ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode *pErrorCode) { |
94 | if(U_FAILURE(*pErrorCode)) { | |
95 | return; | |
96 | } | |
97 | delete csm->iter; | |
98 | csm->iter=reinterpret_cast<BreakIterator *>(iterToAdopt); | |
4388f060 A |
99 | } |
100 | ||
101 | U_CAPI int32_t U_EXPORT2 | |
102 | ucasemap_utf8ToTitle(UCaseMap *csm, | |
103 | char *dest, int32_t destCapacity, | |
104 | const char *src, int32_t srcLength, | |
105 | UErrorCode *pErrorCode) { | |
f3c0d7a5 | 106 | if (U_FAILURE(*pErrorCode)) { |
4388f060 A |
107 | return 0; |
108 | } | |
f3c0d7a5 A |
109 | UText utext=UTEXT_INITIALIZER; |
110 | utext_openUTF8(&utext, (const char *)src, srcLength, pErrorCode); | |
f3c0d7a5 A |
111 | if (U_FAILURE(*pErrorCode)) { |
112 | return 0; | |
4388f060 | 113 | } |
0f5d89e8 A |
114 | if(csm->iter==NULL) { |
115 | LocalPointer<BreakIterator> ownedIter; | |
116 | BreakIterator *iter = ustrcase_getTitleBreakIterator( | |
117 | nullptr, csm->locale, csm->options, nullptr, ownedIter, *pErrorCode); | |
118 | if (iter == nullptr) { | |
119 | utext_close(&utext); | |
120 | return 0; | |
121 | } | |
122 | csm->iter = ownedIter.orphan(); | |
123 | } | |
f3c0d7a5 A |
124 | csm->iter->setText(&utext, *pErrorCode); |
125 | int32_t length=ucasemap_mapUTF8( | |
126 | csm->caseLocale, csm->options, csm->iter, | |
0f5d89e8 A |
127 | dest, destCapacity, |
128 | src, srcLength, | |
f3c0d7a5 | 129 | ucasemap_internalUTF8ToTitle, NULL, *pErrorCode); |
4388f060 A |
130 | utext_close(&utext); |
131 | return length; | |
132 | } | |
133 | ||
134 | #endif // !UCONFIG_NO_BREAK_ITERATION |