]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/casetrn.cpp
ICU-8.11.1.tar.gz
[apple/icu.git] / icuSources / i18n / casetrn.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2001-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: casetrn.cpp
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2004sep03
14 * created by: Markus W. Scherer
15 *
16 * Implementation class for lower-/upper-/title-casing transliterators.
17 */
18
19 #include "unicode/utypes.h"
20
21 #if !UCONFIG_NO_TRANSLITERATION
22
23 #include "unicode/uchar.h"
24 #include "unicode/ustring.h"
25 #include "tolowtrn.h"
26 #include "ucase.h"
27 #include "cpputils.h"
28
29 /* case context iterator using a Replaceable */
30 U_CFUNC UChar32 U_CALLCONV
31 utrans_rep_caseContextIterator(void *context, int8_t dir)
32 {
33 UCaseContext *csc=(UCaseContext *)context;
34 Replaceable *rep=(Replaceable *)csc->p;
35 UChar32 c;
36
37 if(dir<0) {
38 /* reset for backward iteration */
39 csc->index=csc->cpStart;
40 csc->dir=dir;
41 } else if(dir>0) {
42 /* reset for forward iteration */
43 csc->index=csc->cpLimit;
44 csc->dir=dir;
45 } else {
46 /* continue current iteration direction */
47 dir=csc->dir;
48 }
49
50 // automatically adjust start and limit if the Replaceable disagrees
51 // with the original values
52 if(dir<0) {
53 if(csc->start<csc->index) {
54 c=rep->char32At(csc->index-1);
55 if(c<0) {
56 csc->start=csc->index;
57 } else {
58 csc->index-=U16_LENGTH(c);
59 return c;
60 }
61 }
62 } else {
63 // detect, and store in csc->b1, if we hit the limit
64 if(csc->index<csc->limit) {
65 c=rep->char32At(csc->index);
66 if(c<0) {
67 csc->limit=csc->index;
68 csc->b1=TRUE;
69 } else {
70 csc->index+=U16_LENGTH(c);
71 return c;
72 }
73 } else {
74 csc->b1=TRUE;
75 }
76 }
77 return U_SENTINEL;
78 }
79
80 U_NAMESPACE_BEGIN
81
82 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CaseMapTransliterator)
83
84 /**
85 * Constructs a transliterator.
86 */
87 CaseMapTransliterator::CaseMapTransliterator(const UnicodeString &id, UCaseMapFull *map) :
88 Transliterator(id, 0),
89 fCsp(NULL),
90 fMap(map)
91 {
92 UErrorCode errorCode = U_ZERO_ERROR;
93 fCsp = ucase_getSingleton(&errorCode); // expect to get NULL if failure
94
95 // TODO test incremental mode with context-sensitive text (e.g. greek sigma)
96 // TODO need to call setMaximumContextLength()?!
97 }
98
99 /**
100 * Destructor.
101 */
102 CaseMapTransliterator::~CaseMapTransliterator() {
103 }
104
105 /**
106 * Copy constructor.
107 */
108 CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator& o) :
109 Transliterator(o),
110 fCsp(o.fCsp), fMap(o.fMap)
111 {
112 }
113
114 /**
115 * Assignment operator.
116 */
117 CaseMapTransliterator& CaseMapTransliterator::operator=(const CaseMapTransliterator& o) {
118 Transliterator::operator=(o);
119 fCsp = o.fCsp;
120 fMap = o.fMap;
121 return *this;
122 }
123
124 /**
125 * Transliterator API.
126 */
127 Transliterator* CaseMapTransliterator::clone(void) const {
128 return new CaseMapTransliterator(*this);
129 }
130
131 /**
132 * Implements {@link Transliterator#handleTransliterate}.
133 */
134 void CaseMapTransliterator::handleTransliterate(Replaceable& text,
135 UTransPosition& offsets,
136 UBool isIncremental) const
137 {
138 if (offsets.start >= offsets.limit) {
139 return;
140 }
141
142 UCaseContext csc;
143 uprv_memset(&csc, 0, sizeof(csc));
144 csc.p = &text;
145 csc.start = offsets.contextStart;
146 csc.limit = offsets.contextLimit;
147
148 UnicodeString tmp;
149 const UChar *s;
150 UChar32 c;
151 int32_t textPos, delta, result, locCache=0;
152
153 for(textPos=offsets.start; textPos<offsets.limit;) {
154 csc.cpStart=textPos;
155 c=text.char32At(textPos);
156 csc.cpLimit=textPos+=U16_LENGTH(c);
157
158 result=fMap(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
159
160 if(csc.b1 && isIncremental) {
161 // fMap() tried to look beyond the context limit
162 // wait for more input
163 offsets.start=csc.cpStart;
164 return;
165 }
166
167 if(result>=0) {
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) {
171 // string s[result]
172 tmp.setTo(FALSE, s, result);
173 delta=result-U16_LENGTH(c);
174 } else {
175 // single code point
176 tmp.setTo(result);
177 delta=tmp.length()-U16_LENGTH(c);
178 }
179 text.handleReplaceBetween(csc.cpStart, textPos, tmp);
180 if(delta!=0) {
181 textPos+=delta;
182 csc.limit=offsets.contextLimit+=delta;
183 offsets.limit+=delta;
184 }
185 }
186 }
187 offsets.start=textPos;
188 }
189
190 U_NAMESPACE_END
191
192 #endif /* #if !UCONFIG_NO_TRANSLITERATION */