2 *******************************************************************************
4 * Copyright (C) 2001-2008, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: casetrn.cpp
10 * tab size: 8 (not used)
13 * created on: 2004sep03
14 * created by: Markus W. Scherer
16 * Implementation class for lower-/upper-/title-casing transliterators.
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_TRANSLITERATION
23 #include "unicode/uchar.h"
24 #include "unicode/ustring.h"
29 /* case context iterator using a Replaceable */
30 U_CFUNC UChar32 U_CALLCONV
31 utrans_rep_caseContextIterator(void *context
, int8_t dir
)
35 UCaseContext
*csc
=(UCaseContext
*)context
;
36 Replaceable
*rep
=(Replaceable
*)csc
->p
;
40 /* reset for backward iteration */
41 csc
->index
=csc
->cpStart
;
44 /* reset for forward iteration */
45 csc
->index
=csc
->cpLimit
;
48 /* continue current iteration direction */
52 // automatically adjust start and limit if the Replaceable disagrees
53 // with the original values
55 if(csc
->start
<csc
->index
) {
56 c
=rep
->char32At(csc
->index
-1);
58 csc
->start
=csc
->index
;
60 csc
->index
-=U16_LENGTH(c
);
65 // detect, and store in csc->b1, if we hit the limit
66 if(csc
->index
<csc
->limit
) {
67 c
=rep
->char32At(csc
->index
);
69 csc
->limit
=csc
->index
;
72 csc
->index
+=U16_LENGTH(c
);
84 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(CaseMapTransliterator
)
87 * Constructs a transliterator.
89 CaseMapTransliterator::CaseMapTransliterator(const UnicodeString
&id
, UCaseMapFull
*map
) :
90 Transliterator(id
, 0),
94 UErrorCode errorCode
= U_ZERO_ERROR
;
95 fCsp
= ucase_getSingleton(&errorCode
); // expect to get NULL if failure
97 // TODO test incremental mode with context-sensitive text (e.g. greek sigma)
98 // TODO need to call setMaximumContextLength()?!
104 CaseMapTransliterator::~CaseMapTransliterator() {
110 CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator
& o
) :
112 fCsp(o
.fCsp
), fMap(o
.fMap
)
117 * Assignment operator.
119 /*CaseMapTransliterator& CaseMapTransliterator::operator=(const CaseMapTransliterator& o) {
120 Transliterator::operator=(o);
127 * Transliterator API.
129 /*Transliterator* CaseMapTransliterator::clone(void) const {
130 return new CaseMapTransliterator(*this);
134 * Implements {@link Transliterator#handleTransliterate}.
136 void CaseMapTransliterator::handleTransliterate(Replaceable
& text
,
137 UTransPosition
& offsets
,
138 UBool isIncremental
) const
140 if (offsets
.start
>= offsets
.limit
) {
145 uprv_memset(&csc
, 0, sizeof(csc
));
147 csc
.start
= offsets
.contextStart
;
148 csc
.limit
= offsets
.contextLimit
;
153 int32_t textPos
, delta
, result
, locCache
=0;
155 for(textPos
=offsets
.start
; textPos
<offsets
.limit
;) {
157 c
=text
.char32At(textPos
);
158 csc
.cpLimit
=textPos
+=U16_LENGTH(c
);
160 result
=fMap(fCsp
, c
, utrans_rep_caseContextIterator
, &csc
, &s
, "", &locCache
);
162 if(csc
.b1
&& isIncremental
) {
163 // fMap() tried to look beyond the context limit
164 // wait for more input
165 offsets
.start
=csc
.cpStart
;
170 // replace the current code point with its full case mapping result
171 // see UCASE_MAX_STRING_LENGTH
172 if(result
<=UCASE_MAX_STRING_LENGTH
) {
174 tmp
.setTo(FALSE
, s
, result
);
175 delta
=result
-U16_LENGTH(c
);
179 delta
=tmp
.length()-U16_LENGTH(c
);
181 text
.handleReplaceBetween(csc
.cpStart
, textPos
, tmp
);
184 csc
.limit
=offsets
.contextLimit
+=delta
;
185 offsets
.limit
+=delta
;
189 offsets
.start
=textPos
;
194 #endif /* #if !UCONFIG_NO_TRANSLITERATION */