1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 1997-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
12 * \brief C++ API: Collation Element Iterator.
18 * Created by: Helena Shih
20 * Modification History:
22 * Date Name Description
24 * 8/18/97 helena Added internal API documentation.
25 * 08/03/98 erm Synched with 1.2 version CollationElementIterator.java
26 * 12/10/99 aliu Ported Thai collation support from Java.
27 * 01/25/01 swquek Modified into a C++ wrapper calling C APIs (ucoliter.h)
28 * 02/19/01 swquek Removed CollationElementsIterator() since it is
29 * private constructor and no calls are made to it
30 * 2012-2014 markus Rewritten in C++ again.
36 #include "unicode/utypes.h"
38 #if !UCONFIG_NO_COLLATION
40 #include "unicode/unistr.h"
41 #include "unicode/uobject.h"
43 struct UCollationElements
;
46 #if U_SHOW_CPLUSPLUS_API
51 class CharacterIterator
;
52 class CollationIterator
;
53 class RuleBasedCollator
;
58 * The CollationElementIterator class is used as an iterator to walk through
59 * each character of an international string. Use the iterator to return the
60 * ordering priority of the positioned character. The ordering priority of a
61 * character, which we refer to as a key, defines how a character is collated in
62 * the given collation object.
63 * For example, consider the following in Slovak and in traditional Spanish collation:
65 * "ca" -> the first key is key('c') and second key is key('a').
66 * "cha" -> the first key is key('ch') and second key is key('a').</pre>
67 * And in German phonebook collation,
68 * <pre> \htmlonly "æb"-> the first key is key('a'), the second key is key('e'), and
69 * the third key is key('b'). \endhtmlonly </pre>
70 * The key of a character, is an integer composed of primary order(short),
71 * secondary order(char), and tertiary order(char). Java strictly defines the
72 * size and signedness of its primitive data types. Therefore, the static
73 * functions primaryOrder(), secondaryOrder(), and tertiaryOrder() return
74 * int32_t to ensure the correctness of the key value.
75 * <p>Example of the iterator usage: (without error checking)
78 * void CollationElementIterator_Example()
80 * UnicodeString str = "This is a test";
81 * UErrorCode success = U_ZERO_ERROR;
82 * RuleBasedCollator* rbc =
83 * (RuleBasedCollator*) RuleBasedCollator::createInstance(success);
84 * CollationElementIterator* c =
85 * rbc->createCollationElementIterator( str );
86 * int32_t order = c->next(success);
88 * order = c->previous(success);
95 * The method next() returns the collation order of the next character based on
96 * the comparison level of the collator. The method previous() returns the
97 * collation order of the previous character based on the comparison level of
98 * the collator. The Collation Element Iterator moves only in one direction
99 * between calls to reset(), setOffset(), or setText(). That is, next()
100 * and previous() can not be inter-used. Whenever previous() is to be called after
101 * next() or vice versa, reset(), setOffset() or setText() has to be called first
102 * to reset the status, shifting pointers to either the end or the start of
103 * the string (reset() or setText()), or the specified position (setOffset()).
104 * Hence at the next call of next() or previous(), the first or last collation order,
105 * or collation order at the spefcifieid position will be returned. If a change of
106 * direction is done without one of these calls, the result is undefined.
108 * The result of a forward iterate (next()) and reversed result of the backward
109 * iterate (previous()) on the same string are equivalent, if collation orders
110 * with the value 0 are ignored.
111 * Character based on the comparison level of the collator. A collation order
112 * consists of primary order, secondary order and tertiary order. The data
113 * type of the collation order is <strong>int32_t</strong>.
115 * Note, CollationElementIterator should not be subclassed.
117 * @see RuleBasedCollator
118 * @version 1.8 Jan 16 2001
120 class U_I18N_API CollationElementIterator U_FINAL
: public UObject
{
123 // CollationElementIterator public data member ------------------------------
127 * NULLORDER indicates that an error has occured while processing
130 NULLORDER
= (int32_t)0xffffffff
133 // CollationElementIterator public constructor/destructor -------------------
138 * @param other the object to be copied from
141 CollationElementIterator(const CollationElementIterator
& other
);
147 virtual ~CollationElementIterator();
149 // CollationElementIterator public methods ----------------------------------
152 * Returns true if "other" is the same as "this"
154 * @param other the object to be compared
155 * @return true if "other" is the same as "this"
158 UBool
operator==(const CollationElementIterator
& other
) const;
161 * Returns true if "other" is not the same as "this".
163 * @param other the object to be compared
164 * @return true if "other" is not the same as "this"
167 UBool
operator!=(const CollationElementIterator
& other
) const;
170 * Resets the cursor to the beginning of the string.
176 * Gets the ordering priority of the next character in the string.
177 * @param status the error code status.
178 * @return the next character's ordering. otherwise returns NULLORDER if an
179 * error has occured or if the end of string has been reached
182 int32_t next(UErrorCode
& status
);
185 * Get the ordering priority of the previous collation element in the string.
186 * @param status the error code status.
187 * @return the previous element's ordering. otherwise returns NULLORDER if an
188 * error has occured or if the start of string has been reached
191 int32_t previous(UErrorCode
& status
);
194 * Gets the primary order of a collation order.
195 * @param order the collation order
196 * @return the primary order of a collation order.
199 static inline int32_t primaryOrder(int32_t order
);
202 * Gets the secondary order of a collation order.
203 * @param order the collation order
204 * @return the secondary order of a collation order.
207 static inline int32_t secondaryOrder(int32_t order
);
210 * Gets the tertiary order of a collation order.
211 * @param order the collation order
212 * @return the tertiary order of a collation order.
215 static inline int32_t tertiaryOrder(int32_t order
);
218 * Return the maximum length of any expansion sequences that end with the
219 * specified comparison order.
220 * @param order a collation order returned by previous or next.
221 * @return maximum size of the expansion sequences ending with the collation
222 * element or 1 if collation element does not occur at the end of any
226 int32_t getMaxExpansion(int32_t order
) const;
229 * Gets the comparison order in the desired strength. Ignore the other
231 * @param order The order value
234 int32_t strengthOrder(int32_t order
) const;
237 * Sets the source string.
238 * @param str the source string.
239 * @param status the error code status.
242 void setText(const UnicodeString
& str
, UErrorCode
& status
);
245 * Sets the source string.
246 * @param str the source character iterator.
247 * @param status the error code status.
250 void setText(CharacterIterator
& str
, UErrorCode
& status
);
253 * Checks if a comparison order is ignorable.
254 * @param order the collation order.
255 * @return TRUE if a character is ignorable, FALSE otherwise.
258 static inline UBool
isIgnorable(int32_t order
);
261 * Gets the offset of the currently processed character in the source string.
262 * @return the offset of the character.
265 int32_t getOffset(void) const;
268 * Sets the offset of the currently processed character in the source string.
269 * @param newOffset the new offset.
270 * @param status the error code status.
271 * @return the offset of the character.
274 void setOffset(int32_t newOffset
, UErrorCode
& status
);
277 * ICU "poor man's RTTI", returns a UClassID for the actual class.
281 virtual UClassID
getDynamicClassID() const;
284 * ICU "poor man's RTTI", returns a UClassID for this class.
288 static UClassID U_EXPORT2
getStaticClassID();
290 #ifndef U_HIDE_INTERNAL_API
292 static inline CollationElementIterator
*fromUCollationElements(UCollationElements
*uc
) {
293 return reinterpret_cast<CollationElementIterator
*>(uc
);
296 static inline const CollationElementIterator
*fromUCollationElements(const UCollationElements
*uc
) {
297 return reinterpret_cast<const CollationElementIterator
*>(uc
);
300 inline UCollationElements
*toUCollationElements() {
301 return reinterpret_cast<UCollationElements
*>(this);
304 inline const UCollationElements
*toUCollationElements() const {
305 return reinterpret_cast<const UCollationElements
*>(this);
307 #endif // U_HIDE_INTERNAL_API
310 friend class RuleBasedCollator
;
311 friend class UCollationPCE
;
314 * CollationElementIterator constructor. This takes the source string and the
315 * collation object. The cursor will walk thru the source string based on the
316 * predefined collation rules. If the source string is empty, NULLORDER will
317 * be returned on the calls to next().
318 * @param sourceText the source string.
319 * @param order the collation object.
320 * @param status the error code status.
322 CollationElementIterator(const UnicodeString
& sourceText
,
323 const RuleBasedCollator
* order
, UErrorCode
& status
);
324 // Note: The constructors should take settings & tailoring, not a collator,
325 // to avoid circular dependencies.
326 // However, for operator==() we would need to be able to compare tailoring data for equality
327 // without making CollationData or CollationTailoring depend on TailoredSet.
328 // (See the implementation of RuleBasedCollator::operator==().)
329 // That might require creating an intermediate class that would be used
330 // by both CollationElementIterator and RuleBasedCollator
331 // but only contain the part of RBC== related to data and rules.
334 * CollationElementIterator constructor. This takes the source string and the
335 * collation object. The cursor will walk thru the source string based on the
336 * predefined collation rules. If the source string is empty, NULLORDER will
337 * be returned on the calls to next().
338 * @param sourceText the source string.
339 * @param order the collation object.
340 * @param status the error code status.
342 CollationElementIterator(const CharacterIterator
& sourceText
,
343 const RuleBasedCollator
* order
, UErrorCode
& status
);
346 * Assignment operator
348 * @param other the object to be copied
350 const CollationElementIterator
&
351 operator=(const CollationElementIterator
& other
);
353 CollationElementIterator(); // default constructor not implemented
355 /** Normalizes dir_=1 (just after setOffset()) to dir_=0 (just after reset()). */
356 inline int8_t normalizeDir() const { return dir_
== 1 ? 0 : dir_
; }
358 static UHashtable
*computeMaxExpansions(const CollationData
*data
, UErrorCode
&errorCode
);
360 static int32_t getMaxExpansion(const UHashtable
*maxExpansions
, int32_t order
);
362 // CollationElementIterator private data members ----------------------------
364 CollationIterator
*iter_
; // owned
365 const RuleBasedCollator
*rbc_
; // aliased
368 * <0: backwards; 0: just after reset() (previous() begins from end);
369 * 1: just after setOffset(); >1: forward
373 * Stores offsets from expansions and from unsafe-backwards iteration,
374 * so that getOffset() returns intermediate offsets for the CEs
375 * that are consistent with forward iteration.
379 UnicodeString string_
;
382 // CollationElementIterator inline method definitions --------------------------
384 inline int32_t CollationElementIterator::primaryOrder(int32_t order
)
386 return (order
>> 16) & 0xffff;
389 inline int32_t CollationElementIterator::secondaryOrder(int32_t order
)
391 return (order
>> 8) & 0xff;
394 inline int32_t CollationElementIterator::tertiaryOrder(int32_t order
)
399 inline UBool
CollationElementIterator::isIgnorable(int32_t order
)
401 return (order
& 0xffff0000) == 0;
405 #endif // U_SHOW_CPLUSPLUS_API
407 #endif /* #if !UCONFIG_NO_COLLATION */