/src/icu/icu4c/source/common/ucasemap_titlecase_brkiter.cpp
Line | Count | Source |
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: ucasemap_titlecase_brkiter.cpp |
9 | | * encoding: UTF-8 |
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" |
26 | | #include "unicode/casemap.h" |
27 | | #include "unicode/ucasemap.h" |
28 | | #include "cmemory.h" |
29 | | #include "ucase.h" |
30 | | #include "ucasemap_imp.h" |
31 | | |
32 | | U_NAMESPACE_BEGIN |
33 | | |
34 | | void CaseMap::utf8ToTitle( |
35 | | const char *locale, uint32_t options, BreakIterator *iter, |
36 | | StringPiece src, ByteSink &sink, Edits *edits, |
37 | 0 | UErrorCode &errorCode) { |
38 | 0 | if (U_FAILURE(errorCode)) { |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | UText utext = UTEXT_INITIALIZER; |
42 | 0 | utext_openUTF8(&utext, src.data(), src.length(), &errorCode); |
43 | 0 | LocalPointer<BreakIterator> ownedIter; |
44 | 0 | iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode); |
45 | 0 | if (iter == nullptr) { |
46 | 0 | utext_close(&utext); |
47 | 0 | return; |
48 | 0 | } |
49 | 0 | iter->setText(&utext, errorCode); |
50 | 0 | ucasemap_mapUTF8( |
51 | 0 | ustrcase_getCaseLocale(locale), options, iter, |
52 | 0 | src.data(), src.length(), |
53 | 0 | ucasemap_internalUTF8ToTitle, sink, edits, errorCode); |
54 | 0 | utext_close(&utext); |
55 | 0 | } |
56 | | |
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 | 0 | UErrorCode &errorCode) { |
62 | 0 | if (U_FAILURE(errorCode)) { |
63 | 0 | return 0; |
64 | 0 | } |
65 | 0 | UText utext=UTEXT_INITIALIZER; |
66 | 0 | utext_openUTF8(&utext, src, srcLength, &errorCode); |
67 | 0 | LocalPointer<BreakIterator> ownedIter; |
68 | 0 | iter = ustrcase_getTitleBreakIterator(nullptr, locale, options, iter, ownedIter, errorCode); |
69 | 0 | if(iter==nullptr) { |
70 | 0 | utext_close(&utext); |
71 | 0 | return 0; |
72 | 0 | } |
73 | 0 | iter->setText(&utext, errorCode); |
74 | 0 | int32_t length=ucasemap_mapUTF8( |
75 | 0 | ustrcase_getCaseLocale(locale), options, iter, |
76 | 0 | dest, destCapacity, |
77 | 0 | src, srcLength, |
78 | 0 | ucasemap_internalUTF8ToTitle, edits, errorCode); |
79 | 0 | utext_close(&utext); |
80 | 0 | return length; |
81 | 0 | } |
82 | | |
83 | | U_NAMESPACE_END |
84 | | |
85 | | U_NAMESPACE_USE |
86 | | |
87 | | U_CAPI const UBreakIterator * U_EXPORT2 |
88 | 0 | ucasemap_getBreakIterator(const UCaseMap *csm) { |
89 | 0 | return reinterpret_cast<UBreakIterator *>(csm->iter); |
90 | 0 | } |
91 | | |
92 | | U_CAPI void U_EXPORT2 |
93 | 0 | ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode *pErrorCode) { |
94 | 0 | if(U_FAILURE(*pErrorCode)) { |
95 | 0 | return; |
96 | 0 | } |
97 | 0 | delete csm->iter; |
98 | 0 | csm->iter=reinterpret_cast<BreakIterator *>(iterToAdopt); |
99 | 0 | } |
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 | 4.56k | UErrorCode *pErrorCode) { |
106 | 4.56k | if (U_FAILURE(*pErrorCode)) { |
107 | 0 | return 0; |
108 | 0 | } |
109 | 4.56k | UText utext=UTEXT_INITIALIZER; |
110 | 4.56k | utext_openUTF8(&utext, src, srcLength, pErrorCode); |
111 | 4.56k | if (U_FAILURE(*pErrorCode)) { |
112 | 0 | return 0; |
113 | 0 | } |
114 | 4.56k | if(csm->iter==nullptr) { |
115 | 4.56k | LocalPointer<BreakIterator> ownedIter; |
116 | 4.56k | BreakIterator *iter = ustrcase_getTitleBreakIterator( |
117 | 4.56k | nullptr, csm->locale, csm->options, nullptr, ownedIter, *pErrorCode); |
118 | 4.56k | if (iter == nullptr) { |
119 | 7 | utext_close(&utext); |
120 | 7 | return 0; |
121 | 7 | } |
122 | 4.55k | csm->iter = ownedIter.orphan(); |
123 | 4.55k | } |
124 | 4.55k | csm->iter->setText(&utext, *pErrorCode); |
125 | 4.55k | int32_t length=ucasemap_mapUTF8( |
126 | 4.55k | csm->caseLocale, csm->options, csm->iter, |
127 | 4.55k | dest, destCapacity, |
128 | 4.55k | src, srcLength, |
129 | 4.55k | ucasemap_internalUTF8ToTitle, nullptr, *pErrorCode); |
130 | 4.55k | utext_close(&utext); |
131 | 4.55k | return length; |
132 | 4.56k | } |
133 | | |
134 | | #endif // !UCONFIG_NO_BREAK_ITERATION |