2 **********************************************************************
3 * Copyright (C) 2001-2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 05/24/01 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/uchar.h"
16 #include "unicode/uniset.h"
17 #include "unicode/ustring.h"
25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TitlecaseTransliterator
)
27 TitlecaseTransliterator::TitlecaseTransliterator() :
28 CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), NULL
)
30 // Need to look back 2 characters in the case of "can't"
31 setMaximumContextLength(2);
37 TitlecaseTransliterator::~TitlecaseTransliterator() {
43 TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator
& o
) :
44 CaseMapTransliterator(o
)
49 * Assignment operator.
51 /*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
52 const TitlecaseTransliterator& o) {
53 CaseMapTransliterator::operator=(o);
60 Transliterator
* TitlecaseTransliterator::clone(void) const {
61 return new TitlecaseTransliterator(*this);
65 * Implements {@link Transliterator#handleTransliterate}.
67 void TitlecaseTransliterator::handleTransliterate(
68 Replaceable
& text
, UTransPosition
& offsets
,
69 UBool isIncremental
) const
71 // TODO reimplement, see ustrcase.c
72 // using a real word break iterator
73 // instead of just looking for a transition between cased and uncased characters
74 // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
75 // needs to take isIncremental into account because case mappings are context-sensitive
76 // also detect when lowercasing function did not finish because of context
78 if (offsets
.start
>= offsets
.limit
) {
82 // case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
85 // Our mode; we are either converting letter toTitle or
89 // Determine if there is a preceding context of cased case-ignorable*,
90 // in which case we want to start in toLower mode. If the
91 // prior context is anything else (including empty) then start
95 for (start
= offsets
.start
- 1; start
>= offsets
.contextStart
; start
-= U16_LENGTH(c
)) {
96 c
= text
.char32At(start
);
97 type
=ucase_getTypeOrIgnorable(fCsp
, c
);
101 } else if(type
==0) { // uncased but not ignorable
104 // else (type<0) case-ignorable: continue
107 // Convert things after a cased character toLower; things
108 // after an uncased, non-case-ignorable character toTitle. Case-ignorable
109 // characters are copied directly and do not change the mode.
111 uprv_memset(&csc
, 0, sizeof(csc
));
113 csc
.start
= offsets
.contextStart
;
114 csc
.limit
= offsets
.contextLimit
;
118 int32_t textPos
, delta
, result
, locCache
=0;
120 for(textPos
=offsets
.start
; textPos
<offsets
.limit
;) {
122 c
=text
.char32At(textPos
);
123 csc
.cpLimit
=textPos
+=U16_LENGTH(c
);
125 type
=ucase_getTypeOrIgnorable(fCsp
, c
);
126 if(type
>=0) { // not case-ignorable
128 result
=ucase_toFullTitle(fCsp
, c
, utrans_rep_caseContextIterator
, &csc
, &s
, "", &locCache
);
130 result
=ucase_toFullLower(fCsp
, c
, utrans_rep_caseContextIterator
, &csc
, &s
, "", &locCache
);
132 doTitle
= (UBool
)(type
==0); // doTitle=isUncased
134 if(csc
.b1
&& isIncremental
) {
135 // fMap() tried to look beyond the context limit
136 // wait for more input
137 offsets
.start
=csc
.cpStart
;
142 // replace the current code point with its full case mapping result
143 // see UCASE_MAX_STRING_LENGTH
144 if(result
<=UCASE_MAX_STRING_LENGTH
) {
146 tmp
.setTo(FALSE
, s
, result
);
147 delta
=result
-U16_LENGTH(c
);
151 delta
=tmp
.length()-U16_LENGTH(c
);
153 text
.handleReplaceBetween(csc
.cpStart
, textPos
, tmp
);
156 csc
.limit
=offsets
.contextLimit
+=delta
;
157 offsets
.limit
+=delta
;
162 offsets
.start
=textPos
;
167 #endif /* #if !UCONFIG_NO_TRANSLITERATION */