2 *******************************************************************************
4 * Copyright (C) 2001-2005, 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
)
33 UCaseContext
*csc
=(UCaseContext
*)context
;
34 Replaceable
*rep
=(Replaceable
*)csc
->p
;
38 /* reset for backward iteration */
39 csc
->index
=csc
->cpStart
;
42 /* reset for forward iteration */
43 csc
->index
=csc
->cpLimit
;
46 /* continue current iteration direction */
50 // automatically adjust start and limit if the Replaceable disagrees
51 // with the original values
53 if(csc
->start
<csc
->index
) {
54 c
=rep
->char32At(csc
->index
-1);
56 csc
->start
=csc
->index
;
58 csc
->index
-=U16_LENGTH(c
);
63 // detect, and store in csc->b1, if we hit the limit
64 if(csc
->index
<csc
->limit
) {
65 c
=rep
->char32At(csc
->index
);
67 csc
->limit
=csc
->index
;
70 csc
->index
+=U16_LENGTH(c
);
82 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CaseMapTransliterator
)
85 * Constructs a transliterator.
87 CaseMapTransliterator::CaseMapTransliterator(const UnicodeString
&id
, UCaseMapFull
*map
) :
88 Transliterator(id
, 0),
92 UErrorCode errorCode
= U_ZERO_ERROR
;
93 fCsp
= ucase_getSingleton(&errorCode
); // expect to get NULL if failure
95 // TODO test incremental mode with context-sensitive text (e.g. greek sigma)
96 // TODO need to call setMaximumContextLength()?!
102 CaseMapTransliterator::~CaseMapTransliterator() {
108 CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator
& o
) :
110 fCsp(o
.fCsp
), fMap(o
.fMap
)
115 * Assignment operator.
117 CaseMapTransliterator
& CaseMapTransliterator::operator=(const CaseMapTransliterator
& o
) {
118 Transliterator::operator=(o
);
125 * Transliterator API.
127 Transliterator
* CaseMapTransliterator::clone(void) const {
128 return new CaseMapTransliterator(*this);
132 * Implements {@link Transliterator#handleTransliterate}.
134 void CaseMapTransliterator::handleTransliterate(Replaceable
& text
,
135 UTransPosition
& offsets
,
136 UBool isIncremental
) const
138 if (offsets
.start
>= offsets
.limit
) {
143 uprv_memset(&csc
, 0, sizeof(csc
));
145 csc
.start
= offsets
.contextStart
;
146 csc
.limit
= offsets
.contextLimit
;
151 int32_t textPos
, delta
, result
, locCache
=0;
153 for(textPos
=offsets
.start
; textPos
<offsets
.limit
;) {
155 c
=text
.char32At(textPos
);
156 csc
.cpLimit
=textPos
+=U16_LENGTH(c
);
158 result
=fMap(fCsp
, c
, utrans_rep_caseContextIterator
, &csc
, &s
, "", &locCache
);
160 if(csc
.b1
&& isIncremental
) {
161 // fMap() tried to look beyond the context limit
162 // wait for more input
163 offsets
.start
=csc
.cpStart
;
168 // replace the current code point with its full case mapping result
169 // see UCASE_MAX_STRING_LENGTH
170 if(result
<=UCASE_MAX_STRING_LENGTH
) {
172 tmp
.setTo(FALSE
, s
, result
);
173 delta
=result
-U16_LENGTH(c
);
177 delta
=tmp
.length()-U16_LENGTH(c
);
179 text
.handleReplaceBetween(csc
.cpStart
, textPos
, tmp
);
182 csc
.limit
=offsets
.contextLimit
+=delta
;
183 offsets
.limit
+=delta
;
187 offsets
.start
=textPos
;
192 #endif /* #if !UCONFIG_NO_TRANSLITERATION */