2 *******************************************************************************
4 * Copyright (C) 2001-2004, 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 Locale
&loc
, const UnicodeString
&id
, UCaseMapFull
*map
) :
88 Transliterator(id
, 0),
89 fLoc(loc
), fLocName(NULL
),
93 UErrorCode errorCode
= U_ZERO_ERROR
;
94 fCsp
= ucase_getSingleton(&errorCode
); // expect to get NULL if failure
95 fLocName
=fLoc
.getName();
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 fLoc(o
.fLoc
), fLocName(NULL
), fCsp(o
.fCsp
), fMap(o
.fMap
)
114 fLocName
=fLoc
.getName();
118 * Assignment operator.
120 CaseMapTransliterator
& CaseMapTransliterator::operator=(const CaseMapTransliterator
& o
) {
121 Transliterator::operator=(o
);
123 fLocName
= fLoc
.getName();
130 * Transliterator API.
132 Transliterator
* CaseMapTransliterator::clone(void) const {
133 return new CaseMapTransliterator(*this);
137 * Implements {@link Transliterator#handleTransliterate}.
139 void CaseMapTransliterator::handleTransliterate(Replaceable
& text
,
140 UTransPosition
& offsets
,
141 UBool isIncremental
) const
143 if (offsets
.start
>= offsets
.limit
) {
148 uprv_memset(&csc
, 0, sizeof(csc
));
150 csc
.start
= offsets
.contextStart
;
151 csc
.limit
= offsets
.contextLimit
;
156 int32_t textPos
, delta
, result
, locCache
=0;
158 for(textPos
=offsets
.start
; textPos
<offsets
.limit
;) {
160 c
=text
.char32At(textPos
);
161 csc
.cpLimit
=textPos
+=U16_LENGTH(c
);
163 result
=fMap(fCsp
, c
, utrans_rep_caseContextIterator
, &csc
, &s
, fLocName
, &locCache
);
165 if(csc
.b1
&& isIncremental
) {
166 // fMap() tried to look beyond the context limit
167 // wait for more input
172 // replace the current code point with its full case mapping result
173 // see UCASE_MAX_STRING_LENGTH
174 if(result
<=UCASE_MAX_STRING_LENGTH
) {
176 tmp
.setTo(FALSE
, s
, result
);
177 delta
=result
-U16_LENGTH(c
);
181 delta
=tmp
.length()-U16_LENGTH(c
);
183 text
.handleReplaceBetween(csc
.cpStart
, textPos
, tmp
);
186 csc
.limit
=offsets
.contextLimit
+=delta
;
187 offsets
.limit
+=delta
;
191 offsets
.start
=textPos
;
196 #endif /* #if !UCONFIG_NO_TRANSLITERATION */