1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2001-2011, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: casetrn.cpp
12 * tab size: 8 (not used)
15 * created on: 2004sep03
16 * created by: Markus W. Scherer
18 * Implementation class for lower-/upper-/title-casing transliterators.
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_TRANSLITERATION
25 #include "unicode/uchar.h"
26 #include "unicode/ustring.h"
27 #include "unicode/utf.h"
28 #include "unicode/utf16.h"
33 /* case context iterator using a Replaceable */
34 U_CFUNC UChar32 U_CALLCONV
35 utrans_rep_caseContextIterator(void *context, int8_t dir)
39 UCaseContext *csc=(UCaseContext *)context;
40 Replaceable *rep=(Replaceable *)csc->p;
44 /* reset for backward iteration */
45 csc->index=csc->cpStart;
48 /* reset for forward iteration */
49 csc->index=csc->cpLimit;
52 /* continue current iteration direction */
56 // automatically adjust start and limit if the Replaceable disagrees
57 // with the original values
59 if(csc->start<csc->index) {
60 c=rep->char32At(csc->index-1);
62 csc->start=csc->index;
64 csc->index-=U16_LENGTH(c);
69 // detect, and store in csc->b1, if we hit the limit
70 if(csc->index<csc->limit) {
71 c=rep->char32At(csc->index);
73 csc->limit=csc->index;
76 csc->index+=U16_LENGTH(c);
88 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(CaseMapTransliterator)
91 * Constructs a transliterator.
93 CaseMapTransliterator::CaseMapTransliterator(const UnicodeString &id, UCaseMapFull *map) :
94 Transliterator(id, 0),
95 fCsp(ucase_getSingleton()),
98 // TODO test incremental mode with context-sensitive text (e.g. greek sigma)
99 // TODO need to call setMaximumContextLength()?!
105 CaseMapTransliterator::~CaseMapTransliterator() {
111 CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator& o) :
113 fCsp(o.fCsp), fMap(o.fMap)
118 * Assignment operator.
120 /*CaseMapTransliterator& CaseMapTransliterator::operator=(const CaseMapTransliterator& o) {
121 Transliterator::operator=(o);
128 * Transliterator API.
130 /*Transliterator* CaseMapTransliterator::clone(void) const {
131 return new CaseMapTransliterator(*this);
135 * Implements {@link Transliterator#handleTransliterate}.
137 void CaseMapTransliterator::handleTransliterate(Replaceable& text,
138 UTransPosition& offsets,
139 UBool isIncremental) const
141 if (offsets.start >= offsets.limit) {
146 uprv_memset(&csc, 0, sizeof(csc));
148 csc.start = offsets.contextStart;
149 csc.limit = offsets.contextLimit;
154 int32_t textPos, delta, result, locCache=0;
156 for(textPos=offsets.start; textPos<offsets.limit;) {
158 c=text.char32At(textPos);
159 csc.cpLimit=textPos+=U16_LENGTH(c);
161 result=fMap(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
163 if(csc.b1 && isIncremental) {
164 // fMap() tried to look beyond the context limit
165 // wait for more input
166 offsets.start=csc.cpStart;
171 // replace the current code point with its full case mapping result
172 // see UCASE_MAX_STRING_LENGTH
173 if(result<=UCASE_MAX_STRING_LENGTH) {
175 tmp.setTo(FALSE, s, result);
176 delta=result-U16_LENGTH(c);
180 delta=tmp.length()-U16_LENGTH(c);
182 text.handleReplaceBetween(csc.cpStart, textPos, tmp);
185 csc.limit=offsets.contextLimit+=delta;
186 offsets.limit+=delta;
190 offsets.start=textPos;
195 #endif /* #if !UCONFIG_NO_TRANSLITERATION */