]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/coleitr.cpp
ICU-8.11.1.tar.gz
[apple/icu.git] / icuSources / i18n / coleitr.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 1996-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 */
7
8 /*
9 * File coleitr.cpp
10 *
11 *
12 *
13 * Created by: Helena Shih
14 *
15 * Modification History:
16 *
17 * Date Name Description
18 *
19 * 6/23/97 helena Adding comments to make code more readable.
20 * 08/03/98 erm Synched with 1.2 version of CollationElementIterator.java
21 * 12/10/99 aliu Ported Thai collation support from Java.
22 * 01/25/01 swquek Modified to a C++ wrapper calling C APIs (ucoliter.h)
23 * 02/19/01 swquek Removed CollationElementsIterator() since it is
24 * private constructor and no calls are made to it
25 */
26
27 #include "unicode/utypes.h"
28
29 #if !UCONFIG_NO_COLLATION
30
31 #include "unicode/coleitr.h"
32 #include "unicode/ustring.h"
33 #include "ucol_imp.h"
34 #include "cmemory.h"
35
36
37 /* Constants --------------------------------------------------------------- */
38
39 U_NAMESPACE_BEGIN
40
41 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CollationElementIterator)
42
43 /* CollationElementIterator public constructor/destructor ------------------ */
44
45 CollationElementIterator::CollationElementIterator(
46 const CollationElementIterator& other)
47 : UObject(other), isDataOwned_(TRUE)
48 {
49 UErrorCode status = U_ZERO_ERROR;
50 m_data_ = ucol_openElements(other.m_data_->iteratordata_.coll, NULL, 0,
51 &status);
52
53 *this = other;
54 }
55
56 CollationElementIterator::~CollationElementIterator()
57 {
58 if (isDataOwned_) {
59 ucol_closeElements(m_data_);
60 }
61 }
62
63 /* CollationElementIterator public methods --------------------------------- */
64
65 int32_t CollationElementIterator::getOffset() const
66 {
67 return ucol_getOffset(m_data_);
68 }
69
70 /**
71 * Get the ordering priority of the next character in the string.
72 * @return the next character's ordering. Returns NULLORDER if an error has
73 * occured or if the end of string has been reached
74 */
75 int32_t CollationElementIterator::next(UErrorCode& status)
76 {
77 return ucol_next(m_data_, &status);
78 }
79
80 UBool CollationElementIterator::operator!=(
81 const CollationElementIterator& other) const
82 {
83 return !(*this == other);
84 }
85
86 UBool CollationElementIterator::operator==(
87 const CollationElementIterator& that) const
88 {
89 if (this == &that) {
90 return TRUE;
91 }
92
93 if (m_data_ == that.m_data_) {
94 return TRUE;
95 }
96
97 // option comparison
98 if (m_data_->iteratordata_.coll != that.m_data_->iteratordata_.coll)
99 {
100 return FALSE;
101 }
102
103 // the constructor and setText always sets a length
104 // and we only compare the string not the contents of the normalization
105 // buffer
106 int thislength = m_data_->iteratordata_.endp -
107 m_data_->iteratordata_.string;
108 int thatlength = that.m_data_->iteratordata_.endp -
109 that.m_data_->iteratordata_.string;
110
111 if (thislength != thatlength) {
112 return FALSE;
113 }
114
115 if (uprv_memcmp(m_data_->iteratordata_.string,
116 that.m_data_->iteratordata_.string,
117 thislength * U_SIZEOF_UCHAR) != 0) {
118 return FALSE;
119 }
120 if (getOffset() != that.getOffset()) {
121 return FALSE;
122 }
123
124 // checking normalization buffer
125 if ((m_data_->iteratordata_.flags & UCOL_ITER_HASLEN) == 0) {
126 if ((m_data_->iteratordata_.flags & UCOL_ITER_HASLEN) != 0) {
127 return FALSE;
128 }
129 // both are in the normalization buffer
130 if (m_data_->iteratordata_.pos
131 - m_data_->iteratordata_.writableBuffer
132 != that.m_data_->iteratordata_.pos
133 - that.m_data_->iteratordata_.writableBuffer) {
134 // not in the same position in the normalization buffer
135 return FALSE;
136 }
137 }
138 else if ((m_data_->iteratordata_.flags & UCOL_ITER_HASLEN) == 0) {
139 return FALSE;
140 }
141 // checking ce position
142 return (m_data_->iteratordata_.CEpos - m_data_->iteratordata_.CEs)
143 == (that.m_data_->iteratordata_.CEpos
144 - that.m_data_->iteratordata_.CEs);
145 }
146
147 /**
148 * Get the ordering priority of the previous collation element in the string.
149 * @param status the error code status.
150 * @return the previous element's ordering. Returns NULLORDER if an error has
151 * occured or if the start of string has been reached.
152 */
153 int32_t CollationElementIterator::previous(UErrorCode& status)
154 {
155 return ucol_previous(m_data_, &status);
156 }
157
158 /**
159 * Resets the cursor to the beginning of the string.
160 */
161 void CollationElementIterator::reset()
162 {
163 ucol_reset(m_data_);
164 }
165
166 void CollationElementIterator::setOffset(int32_t newOffset,
167 UErrorCode& status)
168 {
169 ucol_setOffset(m_data_, newOffset, &status);
170 }
171
172 /**
173 * Sets the source to the new source string.
174 */
175 void CollationElementIterator::setText(const UnicodeString& source,
176 UErrorCode& status)
177 {
178 if (U_FAILURE(status)) {
179 return;
180 }
181
182 int32_t length = source.length();
183 UChar *string = NULL;
184 if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) {
185 uprv_free(m_data_->iteratordata_.string);
186 }
187 m_data_->isWritable = TRUE;
188 if (length > 0) {
189 string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length);
190 /* test for NULL */
191 if (string == NULL) {
192 status = U_MEMORY_ALLOCATION_ERROR;
193 return;
194 }
195 u_memcpy(string, source.getBuffer(), length);
196 }
197 else {
198 string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR);
199 /* test for NULL */
200 if (string == NULL) {
201 status = U_MEMORY_ALLOCATION_ERROR;
202 return;
203 }
204 *string = 0;
205 }
206 uprv_init_collIterate(m_data_->iteratordata_.coll, string, length,
207 &m_data_->iteratordata_);
208
209 m_data_->reset_ = TRUE;
210 }
211
212 // Sets the source to the new character iterator.
213 void CollationElementIterator::setText(CharacterIterator& source,
214 UErrorCode& status)
215 {
216 if (U_FAILURE(status))
217 return;
218
219 int32_t length = source.getLength();
220 UChar *buffer = NULL;
221
222 if (length == 0) {
223 buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR);
224 /* test for NULL */
225 if (buffer == NULL) {
226 status = U_MEMORY_ALLOCATION_ERROR;
227 return;
228 }
229 *buffer = 0;
230 }
231 else {
232 buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length);
233 /* test for NULL */
234 if (buffer == NULL) {
235 status = U_MEMORY_ALLOCATION_ERROR;
236 return;
237 }
238 /*
239 Using this constructor will prevent buffer from being removed when
240 string gets removed
241 */
242 UnicodeString string;
243 source.getText(string);
244 u_memcpy(buffer, string.getBuffer(), length);
245 }
246
247 if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) {
248 uprv_free(m_data_->iteratordata_.string);
249 }
250 m_data_->isWritable = TRUE;
251 uprv_init_collIterate(m_data_->iteratordata_.coll, buffer, length,
252 &m_data_->iteratordata_);
253 m_data_->reset_ = TRUE;
254 }
255
256 int32_t CollationElementIterator::strengthOrder(int32_t order) const
257 {
258 UCollationStrength s = ucol_getStrength(m_data_->iteratordata_.coll);
259 // Mask off the unwanted differences.
260 if (s == UCOL_PRIMARY) {
261 order &= RuleBasedCollator::PRIMARYDIFFERENCEONLY;
262 }
263 else if (s == UCOL_SECONDARY) {
264 order &= RuleBasedCollator::SECONDARYDIFFERENCEONLY;
265 }
266
267 return order;
268 }
269
270 /* CollationElementIterator private constructors/destructors --------------- */
271
272 /**
273 * This is the "real" constructor for this class; it constructs an iterator
274 * over the source text using the specified collator
275 */
276 CollationElementIterator::CollationElementIterator(
277 const UnicodeString& sourceText,
278 const RuleBasedCollator* order,
279 UErrorCode& status)
280 : isDataOwned_(TRUE)
281 {
282 if (U_FAILURE(status)) {
283 return;
284 }
285
286 int32_t length = sourceText.length();
287 UChar *string = NULL;
288
289 if (length > 0) {
290 string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length);
291 /* test for NULL */
292 if (string == NULL) {
293 status = U_MEMORY_ALLOCATION_ERROR;
294 return;
295 }
296 /*
297 Using this constructor will prevent buffer from being removed when
298 string gets removed
299 */
300 u_memcpy(string, sourceText.getBuffer(), length);
301 }
302 else {
303 string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR);
304 /* test for NULL */
305 if (string == NULL) {
306 status = U_MEMORY_ALLOCATION_ERROR;
307 return;
308 }
309 *string = 0;
310 }
311 m_data_ = ucol_openElements(order->ucollator, string, length, &status);
312
313 /* Test for buffer overflows */
314 if (U_FAILURE(status)) {
315 return;
316 }
317 m_data_->isWritable = TRUE;
318 }
319
320 /**
321 * This is the "real" constructor for this class; it constructs an iterator over
322 * the source text using the specified collator
323 */
324 CollationElementIterator::CollationElementIterator(
325 const CharacterIterator& sourceText,
326 const RuleBasedCollator* order,
327 UErrorCode& status)
328 : isDataOwned_(TRUE)
329 {
330 if (U_FAILURE(status))
331 return;
332
333 // **** should I just drop this test? ****
334 /*
335 if ( sourceText.endIndex() != 0 )
336 {
337 // A CollationElementIterator is really a two-layered beast.
338 // Internally it uses a Normalizer to munge the source text into a form
339 // where all "composed" Unicode characters (such as \u00FC) are split into a
340 // normal character and a combining accent character.
341 // Afterward, CollationElementIterator does its own processing to handle
342 // expanding and contracting collation sequences, ignorables, and so on.
343
344 Normalizer::EMode decomp = order->getStrength() == Collator::IDENTICAL
345 ? Normalizer::NO_OP : order->getDecomposition();
346
347 text = new Normalizer(sourceText, decomp);
348 if (text == NULL)
349 status = U_MEMORY_ALLOCATION_ERROR;
350 }
351 */
352 int32_t length = sourceText.getLength();
353 UChar *buffer;
354 if (length > 0) {
355 buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length);
356 /* test for NULL */
357 if (buffer == NULL) {
358 status = U_MEMORY_ALLOCATION_ERROR;
359 return;
360 }
361 /*
362 Using this constructor will prevent buffer from being removed when
363 string gets removed
364 */
365 UnicodeString string(buffer, length, length);
366 ((CharacterIterator &)sourceText).getText(string);
367 const UChar *temp = string.getBuffer();
368 u_memcpy(buffer, temp, length);
369 }
370 else {
371 buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR);
372 /* test for NULL */
373 if (buffer == NULL) {
374 status = U_MEMORY_ALLOCATION_ERROR;
375 return;
376 }
377 *buffer = 0;
378 }
379 m_data_ = ucol_openElements(order->ucollator, buffer, length, &status);
380
381 /* Test for buffer overflows */
382 if (U_FAILURE(status)) {
383 return;
384 }
385 m_data_->isWritable = TRUE;
386 }
387
388 /* CollationElementIterator protected methods ----------------------------- */
389
390 const CollationElementIterator& CollationElementIterator::operator=(
391 const CollationElementIterator& other)
392 {
393 if (this != &other)
394 {
395 UCollationElements *ucolelem = this->m_data_;
396 UCollationElements *otherucolelem = other.m_data_;
397 collIterate *coliter = &(ucolelem->iteratordata_);
398 collIterate *othercoliter = &(otherucolelem->iteratordata_);
399 int length = 0;
400
401 // checking only UCOL_ITER_HASLEN is not enough here as we may be in
402 // the normalization buffer
403 length = othercoliter->endp - othercoliter->string;
404
405 ucolelem->reset_ = otherucolelem->reset_;
406 ucolelem->isWritable = TRUE;
407
408 /* create a duplicate of string */
409 if (length > 0) {
410 coliter->string = (UChar *)uprv_malloc(length * U_SIZEOF_UCHAR);
411 if(coliter->string != NULL) {
412 uprv_memcpy(coliter->string, othercoliter->string,
413 length * U_SIZEOF_UCHAR);
414 } else { // Error: couldn't allocate memory. No copying should be done
415 length = 0;
416 }
417 }
418 else {
419 coliter->string = NULL;
420 }
421
422 /* start and end of string */
423 coliter->endp = coliter->string + length;
424
425 /* handle writable buffer here */
426
427 if (othercoliter->flags & UCOL_ITER_INNORMBUF) {
428 uint32_t wlength = u_strlen(othercoliter->writableBuffer) + 1;
429 if (wlength < coliter->writableBufSize) {
430 uprv_memcpy(coliter->stackWritableBuffer,
431 othercoliter->stackWritableBuffer,
432 wlength * U_SIZEOF_UCHAR);
433 }
434 else {
435 if (coliter->writableBuffer != coliter->stackWritableBuffer) {
436 uprv_free(coliter->writableBuffer);
437 }
438 coliter->writableBuffer = (UChar *)uprv_malloc(
439 wlength * U_SIZEOF_UCHAR);
440 if(coliter->writableBuffer != NULL) {
441 uprv_memcpy(coliter->writableBuffer,
442 othercoliter->writableBuffer,
443 wlength * U_SIZEOF_UCHAR);
444 coliter->writableBufSize = wlength;
445 } else { // Error: couldn't allocate memory for writableBuffer
446 coliter->writableBufSize = 0;
447 }
448 }
449 }
450
451 /* current position */
452 if (othercoliter->pos >= othercoliter->string &&
453 othercoliter->pos <= othercoliter->endp)
454 {
455 coliter->pos = coliter->string +
456 (othercoliter->pos - othercoliter->string);
457 }
458 else {
459 coliter->pos = coliter->writableBuffer +
460 (othercoliter->pos - othercoliter->writableBuffer);
461 }
462
463 /* CE buffer */
464 int32_t CEsize = (int32_t)(othercoliter->CEpos - othercoliter->CEs);
465 if (CEsize > 0) {
466 uprv_memcpy(coliter->CEs, othercoliter->CEs, CEsize);
467 }
468 coliter->toReturn = coliter->CEs +
469 (othercoliter->toReturn - othercoliter->CEs);
470 coliter->CEpos = coliter->CEs + CEsize;
471
472 if (othercoliter->fcdPosition != NULL) {
473 coliter->fcdPosition = coliter->string +
474 (othercoliter->fcdPosition
475 - othercoliter->string);
476 }
477 else {
478 coliter->fcdPosition = NULL;
479 }
480 coliter->flags = othercoliter->flags/*| UCOL_ITER_HASLEN*/;
481 coliter->origFlags = othercoliter->origFlags;
482 coliter->coll = othercoliter->coll;
483 this->isDataOwned_ = TRUE;
484 }
485
486 return *this;
487 }
488
489 U_NAMESPACE_END
490
491 #endif /* #if !UCONFIG_NO_COLLATION */
492
493 /* eof */