1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2001-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 05/24/01 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/uchar.h"
18 #include "unicode/uniset.h"
19 #include "unicode/ustring.h"
20 #include "unicode/utf16.h"
28 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TitlecaseTransliterator
)
30 TitlecaseTransliterator::TitlecaseTransliterator() :
31 CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), NULL
)
33 // Need to look back 2 characters in the case of "can't"
34 setMaximumContextLength(2);
40 TitlecaseTransliterator::~TitlecaseTransliterator() {
46 TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator
& o
) :
47 CaseMapTransliterator(o
)
52 * Assignment operator.
54 /*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
55 const TitlecaseTransliterator& o) {
56 CaseMapTransliterator::operator=(o);
63 Transliterator
* TitlecaseTransliterator::clone(void) const {
64 return new TitlecaseTransliterator(*this);
68 * Implements {@link Transliterator#handleTransliterate}.
70 void TitlecaseTransliterator::handleTransliterate(
71 Replaceable
& text
, UTransPosition
& offsets
,
72 UBool isIncremental
) const
74 // TODO reimplement, see ustrcase.c
75 // using a real word break iterator
76 // instead of just looking for a transition between cased and uncased characters
77 // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
78 // needs to take isIncremental into account because case mappings are context-sensitive
79 // also detect when lowercasing function did not finish because of context
81 if (offsets
.start
>= offsets
.limit
) {
85 // case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
88 // Our mode; we are either converting letter toTitle or
92 // Determine if there is a preceding context of cased case-ignorable*,
93 // in which case we want to start in toLower mode. If the
94 // prior context is anything else (including empty) then start
98 for (start
= offsets
.start
- 1; start
>= offsets
.contextStart
; start
-= U16_LENGTH(c
)) {
99 c
= text
.char32At(start
);
100 type
=ucase_getTypeOrIgnorable(c
);
101 if(type
>0) { // cased
104 } else if(type
==0) { // uncased but not ignorable
107 // else (type<0) case-ignorable: continue
110 // Convert things after a cased character toLower; things
111 // after an uncased, non-case-ignorable character toTitle. Case-ignorable
112 // characters are copied directly and do not change the mode.
114 uprv_memset(&csc
, 0, sizeof(csc
));
116 csc
.start
= offsets
.contextStart
;
117 csc
.limit
= offsets
.contextLimit
;
121 int32_t textPos
, delta
, result
;
123 for(textPos
=offsets
.start
; textPos
<offsets
.limit
;) {
125 c
=text
.char32At(textPos
);
126 csc
.cpLimit
=textPos
+=U16_LENGTH(c
);
128 type
=ucase_getTypeOrIgnorable(c
);
129 if(type
>=0) { // not case-ignorable
131 result
=ucase_toFullTitle(c
, utrans_rep_caseContextIterator
, &csc
, &s
, UCASE_LOC_ROOT
);
133 result
=ucase_toFullLower(c
, utrans_rep_caseContextIterator
, &csc
, &s
, UCASE_LOC_ROOT
);
135 doTitle
= (UBool
)(type
==0); // doTitle=isUncased
137 if(csc
.b1
&& isIncremental
) {
138 // fMap() tried to look beyond the context limit
139 // wait for more input
140 offsets
.start
=csc
.cpStart
;
145 // replace the current code point with its full case mapping result
146 // see UCASE_MAX_STRING_LENGTH
147 if(result
<=UCASE_MAX_STRING_LENGTH
) {
149 tmp
.setTo(FALSE
, s
, result
);
150 delta
=result
-U16_LENGTH(c
);
154 delta
=tmp
.length()-U16_LENGTH(c
);
156 text
.handleReplaceBetween(csc
.cpStart
, textPos
, tmp
);
159 csc
.limit
=offsets
.contextLimit
+=delta
;
160 offsets
.limit
+=delta
;
165 offsets
.start
=textPos
;
170 #endif /* #if !UCONFIG_NO_TRANSLITERATION */