2 ******************************************************************************
3 * Copyright (C) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
13 * Created by: Helena Shih
15 * Modification History:
17 * Date Name Description
19 * 8/18/97 helena Added internal API documentation.
20 * 08/03/98 erm Synched with 1.2 version CollationElementIterator.java
21 * 12/10/99 aliu Ported Thai collation support from Java.
22 * 01/25/01 swquek Modified into 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
30 #include "unicode/utypes.h"
32 #if !UCONFIG_NO_COLLATION
34 #include "unicode/uobject.h"
35 #include "unicode/tblcoll.h"
36 #include "unicode/ucoleitr.h"
39 * The UCollationElements struct.
40 * For usage in C programs.
43 typedef struct UCollationElements UCollationElements
;
48 * The CollationElementIterator class is used as an iterator to walk through
49 * each character of an international string. Use the iterator to return the
50 * ordering priority of the positioned character. The ordering priority of a
51 * character, which we refer to as a key, defines how a character is collated in
52 * the given collation object.
53 * For example, consider the following in Spanish:
56 * "ca" -> the first key is key('c') and second key is key('a').
57 * "cha" -> the first key is key('ch') and second key is key('a').
63 * "æb"-> the first key is key('a'), the second key is key('e'), and
64 * the third key is key('b').
67 * The key of a character, is an integer composed of primary order(short),
68 * secondary order(char), and tertiary order(char). Java strictly defines the
69 * size and signedness of its primitive data types. Therefore, the static
70 * functions primaryOrder(), secondaryOrder(), and tertiaryOrder() return
71 * int32_t to ensure the correctness of the key value.
72 * <p>Example of the iterator usage: (without error checking)
75 * void CollationElementIterator_Example()
77 * UnicodeString str = "This is a test";
78 * UErrorCode success = U_ZERO_ERROR;
79 * RuleBasedCollator* rbc =
80 * (RuleBasedCollator*) RuleBasedCollator::createInstance(success);
81 * CollationElementIterator* c =
82 * rbc->createCollationElementIterator( str );
83 * int32_t order = c->next(success);
85 * order = c->previous(success);
92 * CollationElementIterator::next returns the collation order of the next
93 * character based on the comparison level of the collator.
94 * CollationElementIterator::previous returns the collation order of the
95 * previous character based on the comparison level of the collator.
96 * The Collation Element Iterator moves only in one direction between calls to
97 * CollationElementIterator::reset. That is, CollationElementIterator::next()
98 * and CollationElementIterator::previous can not be inter-used. Whenever
99 * CollationElementIterator::previous is to be called after
100 * CollationElementIterator::next() or vice versa,
101 * CollationElementIterator::reset has to be called first to reset the status,
102 * shifting pointers to either the end or the start of the string. Hence at the
103 * next call of CollationElementIterator::previous or
104 * CollationElementIterator::next(), the first or last collation order will be
106 * If a change of direction is done without a CollationElementIterator::reset(),
107 * the result is undefined.
108 * The result of a forward iterate (CollationElementIterator::next) and
109 * reversed result of the backward iterate (CollationElementIterator::previous)
110 * on the same string are equivalent, if collation orders with the value
111 * UCOL_IGNORABLE are ignored.
112 * Character based on the comparison level of the collator. A collation order
113 * consists of primary order, secondary order and tertiary order. The data
114 * type of the collation order is <strong>t_int32</strong>.
116 * Note, CollationElementIterator should not be subclassed.
118 * @see RuleBasedCollator
119 * @version 1.8 Jan 16 2001
121 class U_I18N_API CollationElementIterator
: public UObject
{
124 // CollationElementIterator public data member ------------------------------
127 * NULLORDER indicates that an error has occured while processing
130 static int32_t const NULLORDER
;
132 // CollationElementIterator public constructor/destructor -------------------
137 * @param other the object to be copied from
140 CollationElementIterator(const CollationElementIterator
& other
);
146 virtual ~CollationElementIterator();
148 // CollationElementIterator public methods ----------------------------------
151 * Returns true if "other" is the same as "this"
153 * @param other the object to be compared
154 * @return true if "other" is the same as "this"
157 UBool
operator==(const CollationElementIterator
& other
) const;
160 * Returns true if "other" is not the same as "this".
162 * @param other the object to be compared
163 * @return true if "other" is not the same as "this"
166 UBool
operator!=(const CollationElementIterator
& other
) const;
169 * Resets the cursor to the beginning of the string.
175 * Gets the ordering priority of the next character in the string.
176 * @param status the error code status.
177 * @return the next character's ordering. otherwise returns NULLORDER if an
178 * error has occured or if the end of string has been reached
181 int32_t next(UErrorCode
& status
);
184 * Get the ordering priority of the previous collation element in the string.
185 * @param status the error code status.
186 * @return the previous element's ordering. otherwise returns NULLORDER if an
187 * error has occured or if the start of string has been reached
190 int32_t previous(UErrorCode
& status
);
193 * Gets the primary order of a collation order.
194 * @param order the collation order
195 * @return the primary order of a collation order.
198 static inline int32_t primaryOrder(int32_t order
);
201 * Gets the secondary order of a collation order.
202 * @param order the collation order
203 * @return the secondary order of a collation order.
206 static inline int32_t secondaryOrder(int32_t order
);
209 * Gets the tertiary order of a collation order.
210 * @param order the collation order
211 * @return the tertiary order of a collation order.
214 static inline int32_t tertiaryOrder(int32_t order
);
217 * Return the maximum length of any expansion sequences that end with the
218 * specified comparison order.
219 * @param order a collation order returned by previous or next.
220 * @return maximum size of the expansion sequences ending with the collation
221 * element or 1 if collation element does not occur at the end of any
225 int32_t getMaxExpansion(int32_t order
) const;
228 * Gets the comparison order in the desired strength. Ignore the other
230 * @param order The order value
233 int32_t strengthOrder(int32_t order
) const;
236 * Sets the source string.
237 * @param str the source string.
238 * @param status the error code status.
241 void setText(const UnicodeString
& str
, UErrorCode
& status
);
244 * Sets the source string.
245 * @param str the source character iterator.
246 * @param status the error code status.
249 void setText(CharacterIterator
& str
, UErrorCode
& status
);
252 * Checks if a comparison order is ignorable.
253 * @param order the collation order.
254 * @return TRUE if a character is ignorable, FALSE otherwise.
257 static inline UBool
isIgnorable(int32_t order
);
260 * Gets the offset of the currently processed character in the source string.
261 * @return the offset of the character.
264 int32_t getOffset(void) const;
267 * Sets the offset of the currently processed character in the source string.
268 * @param newOffset the new offset.
269 * @param status the error code status.
270 * @return the offset of the character.
273 void setOffset(int32_t newOffset
, UErrorCode
& status
);
276 * ICU "poor man's RTTI", returns a UClassID for the actual class.
280 virtual UClassID
getDynamicClassID() const;
283 * ICU "poor man's RTTI", returns a UClassID for this class.
287 static UClassID U_EXPORT2
getStaticClassID();
291 // CollationElementIterator protected constructors --------------------------
295 friend class RuleBasedCollator
;
298 * CollationElementIterator constructor. This takes the source string and the
299 * collation object. The cursor will walk thru the source string based on the
300 * predefined collation rules. If the source string is empty, NULLORDER will
301 * be returned on the calls to next().
302 * @param sourceText the source string.
303 * @param order the collation object.
304 * @param status the error code status.
307 CollationElementIterator(const UnicodeString
& sourceText
,
308 const RuleBasedCollator
* order
, UErrorCode
& status
);
311 * CollationElementIterator constructor. This takes the source string and the
312 * collation object. The cursor will walk thru the source string based on the
313 * predefined collation rules. If the source string is empty, NULLORDER will
314 * be returned on the calls to next().
315 * @param sourceText the source string.
316 * @param order the collation object.
317 * @param status the error code status.
320 CollationElementIterator(const CharacterIterator
& sourceText
,
321 const RuleBasedCollator
* order
, UErrorCode
& status
);
323 // CollationElementIterator protected methods -------------------------------
326 * Assignment operator
328 * @param other the object to be copied
331 const CollationElementIterator
&
332 operator=(const CollationElementIterator
& other
);
335 CollationElementIterator(); // default constructor not implemented
337 // CollationElementIterator private data members ----------------------------
340 * Data wrapper for collation elements
342 UCollationElements
*m_data_
;
345 * Indicates if m_data_ belongs to this object.
351 // CollationElementIterator inline method defination --------------------------
354 * Get the primary order of a collation order.
355 * @param order the collation order
356 * @return the primary order of a collation order.
358 inline int32_t CollationElementIterator::primaryOrder(int32_t order
)
360 order
&= RuleBasedCollator::PRIMARYORDERMASK
;
361 return (order
>> RuleBasedCollator::PRIMARYORDERSHIFT
);
365 * Get the secondary order of a collation order.
366 * @param order the collation order
367 * @return the secondary order of a collation order.
369 inline int32_t CollationElementIterator::secondaryOrder(int32_t order
)
371 order
= order
& RuleBasedCollator::SECONDARYORDERMASK
;
372 return (order
>> RuleBasedCollator::SECONDARYORDERSHIFT
);
376 * Get the tertiary order of a collation order.
377 * @param order the collation order
378 * @return the tertiary order of a collation order.
380 inline int32_t CollationElementIterator::tertiaryOrder(int32_t order
)
382 return (order
&= RuleBasedCollator::TERTIARYORDERMASK
);
385 inline int32_t CollationElementIterator::getMaxExpansion(int32_t order
) const
387 return ucol_getMaxExpansion(m_data_
, (uint32_t)order
);
390 inline UBool
CollationElementIterator::isIgnorable(int32_t order
)
392 return (primaryOrder(order
) == RuleBasedCollator::PRIMIGNORABLE
);
397 #endif /* #if !UCONFIG_NO_COLLATION */