2 **********************************************************************
3 * Copyright (C) 2001-2011, 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"
18 #include "unicode/utf16.h"
26 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TitlecaseTransliterator)
28 TitlecaseTransliterator::TitlecaseTransliterator() :
29 CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), NULL)
31 // Need to look back 2 characters in the case of "can't"
32 setMaximumContextLength(2);
38 TitlecaseTransliterator::~TitlecaseTransliterator() {
44 TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator& o) :
45 CaseMapTransliterator(o)
50 * Assignment operator.
52 /*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
53 const TitlecaseTransliterator& o) {
54 CaseMapTransliterator::operator=(o);
61 Transliterator* TitlecaseTransliterator::clone(void) const {
62 return new TitlecaseTransliterator(*this);
66 * Implements {@link Transliterator#handleTransliterate}.
68 void TitlecaseTransliterator::handleTransliterate(
69 Replaceable& text, UTransPosition& offsets,
70 UBool isIncremental) const
72 // TODO reimplement, see ustrcase.c
73 // using a real word break iterator
74 // instead of just looking for a transition between cased and uncased characters
75 // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
76 // needs to take isIncremental into account because case mappings are context-sensitive
77 // also detect when lowercasing function did not finish because of context
79 if (offsets.start >= offsets.limit) {
83 // case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
86 // Our mode; we are either converting letter toTitle or
90 // Determine if there is a preceding context of cased case-ignorable*,
91 // in which case we want to start in toLower mode. If the
92 // prior context is anything else (including empty) then start
96 for (start = offsets.start - 1; start >= offsets.contextStart; start -= U16_LENGTH(c)) {
97 c = text.char32At(start);
98 type=ucase_getTypeOrIgnorable(fCsp, c);
102 } else if(type==0) { // uncased but not ignorable
105 // else (type<0) case-ignorable: continue
108 // Convert things after a cased character toLower; things
109 // after an uncased, non-case-ignorable character toTitle. Case-ignorable
110 // characters are copied directly and do not change the mode.
112 uprv_memset(&csc, 0, sizeof(csc));
114 csc.start = offsets.contextStart;
115 csc.limit = offsets.contextLimit;
119 int32_t textPos, delta, result, locCache=0;
121 for(textPos=offsets.start; textPos<offsets.limit;) {
123 c=text.char32At(textPos);
124 csc.cpLimit=textPos+=U16_LENGTH(c);
126 type=ucase_getTypeOrIgnorable(fCsp, c);
127 if(type>=0) { // not case-ignorable
129 result=ucase_toFullTitle(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
131 result=ucase_toFullLower(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
133 doTitle = (UBool)(type==0); // doTitle=isUncased
135 if(csc.b1 && isIncremental) {
136 // fMap() tried to look beyond the context limit
137 // wait for more input
138 offsets.start=csc.cpStart;
143 // replace the current code point with its full case mapping result
144 // see UCASE_MAX_STRING_LENGTH
145 if(result<=UCASE_MAX_STRING_LENGTH) {
147 tmp.setTo(FALSE, s, result);
148 delta=result-U16_LENGTH(c);
152 delta=tmp.length()-U16_LENGTH(c);
154 text.handleReplaceBetween(csc.cpStart, textPos, tmp);
157 csc.limit=offsets.contextLimit+=delta;
158 offsets.limit+=delta;
163 offsets.start=textPos;
168 #endif /* #if !UCONFIG_NO_TRANSLITERATION */