1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2001-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/15/2001 synwee Modified all methods to process its own function
15 * instead of calling the equivalent c++ api (coleitr.h)
16 *******************************************************************************/
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_COLLATION
26 * This indicates an error has occured during processing or if no more CEs is
30 #define UCOL_NULLORDER ((int32_t)0xFFFFFFFF)
32 #ifndef U_HIDE_INTERNAL_API
34 * DO NOT USE, INTERNAL CONSTANT THAT WAS REMOVED AND THEN
35 * TEMPORARILY RESTORED TO PREVENT THE BUILD FROM BREAKING.
36 * This indicates an error has occured during processing or there are no more CEs
41 #define UCOL_PROCESSED_NULLORDER ((int64_t)U_INT64_MAX)
42 #endif /* U_HIDE_INTERNAL_API */
44 #include "unicode/ucol.h"
47 * The UCollationElements struct.
48 * For usage in C programs.
51 typedef struct UCollationElements UCollationElements
;
55 * \brief C API: UCollationElements
57 * The UCollationElements API is used as an iterator to walk through each
58 * character of an international string. Use the iterator to return the
59 * ordering priority of the positioned character. The ordering priority of a
60 * character, which we refer to as a key, defines how a character is collated
61 * in the given collation object.
62 * For example, consider the following in Slovak and in traditional Spanish collation:
64 * . "ca" -> the first key is key('c') and second key is key('a').
65 * . "cha" -> the first key is key('ch') and second key is key('a').
67 * And in German phonebook collation,
69 * . "<ae ligature>b"-> the first key is key('a'), the second key is key('e'), and
70 * . the third key is key('b').
72 * <p>Example of the iterator usage: (without error checking)
74 * . void CollationElementIterator_Example()
77 * . t_int32 order, primaryOrder;
78 * . UCollationElements *c;
79 * . UCollatorOld *coll;
80 * . UErrorCode success = U_ZERO_ERROR;
81 * . s=(UChar*)malloc(sizeof(UChar) * (strlen("This is a test")+1) );
82 * . u_uastrcpy(s, "This is a test");
83 * . coll = ucol_open(NULL, &success);
84 * . c = ucol_openElements(coll, str, u_strlen(str), &status);
85 * . order = ucol_next(c, &success);
87 * . order = ucol_prev(c, &success);
90 * . ucol_closeElements(c);
94 * ucol_next() returns the collation order of the next.
95 * ucol_prev() returns the collation order of the previous character.
96 * The Collation Element Iterator moves only in one direction between calls to
97 * ucol_reset. That is, ucol_next() and ucol_prev can not be inter-used.
98 * Whenever ucol_prev is to be called after ucol_next() or vice versa,
99 * ucol_reset has to be called first to reset the status, shifting pointers to
100 * either the end or the start of the string. Hence at the next call of
101 * ucol_prev or ucol_next, the first or last collation order will be returned.
102 * If a change of direction is done without a ucol_reset, the result is
104 * The result of a forward iterate (ucol_next) and reversed result of the
105 * backward iterate (ucol_prev) on the same string are equivalent, if
106 * collation orders with the value 0 are ignored.
107 * Character based on the comparison level of the collator. A collation order
108 * consists of primary order, secondary order and tertiary order. The data
109 * type of the collation order is <strong>int32_t</strong>.
115 * Open the collation elements for a string.
117 * @param coll The collator containing the desired collation rules.
118 * @param text The text to iterate over.
119 * @param textLength The number of characters in text, or -1 if null-terminated
120 * @param status A pointer to a UErrorCode to receive any errors.
121 * @return a struct containing collation element information
124 U_STABLE UCollationElements
* U_EXPORT2
125 ucol_openElements(const UCollator
*coll
,
132 * get a hash code for a key... Not very useful!
133 * @param key the given key.
134 * @param length the size of the key array.
135 * @return the hash code.
138 U_STABLE
int32_t U_EXPORT2
139 ucol_keyHashCode(const uint8_t* key
, int32_t length
);
142 * Close a UCollationElements.
143 * Once closed, a UCollationElements may no longer be used.
144 * @param elems The UCollationElements to close.
147 U_STABLE
void U_EXPORT2
148 ucol_closeElements(UCollationElements
*elems
);
151 * Reset the collation elements to their initial state.
152 * This will move the 'cursor' to the beginning of the text.
153 * Property settings for collation will be reset to the current status.
154 * @param elems The UCollationElements to reset.
159 U_STABLE
void U_EXPORT2
160 ucol_reset(UCollationElements
*elems
);
163 * Get the ordering priority of the next collation element in the text.
164 * A single character may contain more than one collation element.
165 * @param elems The UCollationElements containing the text.
166 * @param status A pointer to a UErrorCode to receive any errors.
167 * @return The next collation elements ordering, otherwise returns NULLORDER
168 * if an error has occured or if the end of string has been reached
171 U_STABLE
int32_t U_EXPORT2
172 ucol_next(UCollationElements
*elems
, UErrorCode
*status
);
175 * Get the ordering priority of the previous collation element in the text.
176 * A single character may contain more than one collation element.
177 * Note that internally a stack is used to store buffered collation elements.
178 * @param elems The UCollationElements containing the text.
179 * @param status A pointer to a UErrorCode to receive any errors. Noteably
180 * a U_BUFFER_OVERFLOW_ERROR is returned if the internal stack
181 * buffer has been exhausted.
182 * @return The previous collation elements ordering, otherwise returns
183 * NULLORDER if an error has occured or if the start of string has
187 U_STABLE
int32_t U_EXPORT2
188 ucol_previous(UCollationElements
*elems
, UErrorCode
*status
);
190 #ifndef U_HIDE_INTERNAL_API
192 * DO NOT USE, INTERNAL FUNCTION THAT WAS REMOVED AND THEN
193 * TEMPORARILY RESTORED TO PREVENT THE BUILD FROM BREAKING.
194 * Get the processed ordering priority of the next collation element in the text.
195 * A single character may contain more than one collation element.
197 * @param elems The UCollationElements containing the text.
198 * @param ixLow a pointer to an int32_t to receive the iterator index before fetching the CE.
199 * @param ixHigh a pointer to an int32_t to receive the iterator index after fetching the CE.
200 * @param status A pointer to an UErrorCode to receive any errors.
201 * @return The next collation elements ordering, otherwise returns UCOL_PROCESSED_NULLORDER
202 * if an error has occured or if the end of string has been reached
206 U_INTERNAL
int64_t U_EXPORT2
207 ucol_nextProcessed(UCollationElements
*elems
, int32_t *ixLow
, int32_t *ixHigh
, UErrorCode
*status
);
210 * DO NOT USE, INTERNAL FUNCTION THAT WAS REMOVED AND THEN
211 * TEMPORARILY RESTORED TO PREVENT THE BUILD FROM BREAKING.
212 * Get the processed ordering priority of the previous collation element in the text.
213 * A single character may contain more than one collation element.
214 * Note that internally a stack is used to store buffered collation elements.
215 * It is very rare that the stack will overflow, however if such a case is
216 * encountered, the problem can be solved by increasing the size
217 * UCOL_EXPAND_CE_BUFFER_SIZE in ucol_imp.h.
219 * @param elems The UCollationElements containing the text.
220 * @param ixLow A pointer to an int32_t to receive the iterator index after fetching the CE
221 * @param ixHigh A pointer to an int32_t to receiver the iterator index before fetching the CE
222 * @param status A pointer to an UErrorCode to receive any errors. Noteably
223 * a U_BUFFER_OVERFLOW_ERROR is returned if the internal stack
224 * buffer has been exhausted.
225 * @return The previous collation elements ordering, otherwise returns
226 * UCOL_PROCESSED_NULLORDER if an error has occured or if the start of
227 * string has been reached.
231 U_INTERNAL
int64_t U_EXPORT2
232 ucol_previousProcessed(UCollationElements
*elems
, int32_t *ixLow
, int32_t *ixHigh
, UErrorCode
*status
);
233 #endif /* U_HIDE_INTERNAL_API */
236 * Get the maximum length of any expansion sequences that end with the
237 * specified comparison order.
238 * This is useful for .... ?
239 * @param elems The UCollationElements containing the text.
240 * @param order A collation order returned by previous or next.
241 * @return maximum size of the expansion sequences ending with the collation
242 * element or 1 if collation element does not occur at the end of any
246 U_STABLE
int32_t U_EXPORT2
247 ucol_getMaxExpansion(const UCollationElements
*elems
, int32_t order
);
250 * Set the text containing the collation elements.
251 * Property settings for collation will remain the same.
252 * In order to reset the iterator to the current collation property settings,
253 * the API reset() has to be called.
254 * @param elems The UCollationElements to set.
255 * @param text The source text containing the collation elements.
256 * @param textLength The length of text, or -1 if null-terminated.
257 * @param status A pointer to a UErrorCode to receive any errors.
261 U_STABLE
void U_EXPORT2
262 ucol_setText( UCollationElements
*elems
,
268 * Get the offset of the current source character.
269 * This is an offset into the text of the character containing the current
270 * collation elements.
271 * @param elems The UCollationElements to query.
272 * @return The offset of the current source character.
273 * @see ucol_setOffset
276 U_STABLE
int32_t U_EXPORT2
277 ucol_getOffset(const UCollationElements
*elems
);
280 * Set the offset of the current source character.
281 * This is an offset into the text of the character to be processed.
282 * Property settings for collation will remain the same.
283 * In order to reset the iterator to the current collation property settings,
284 * the API reset() has to be called.
285 * @param elems The UCollationElements to set.
286 * @param offset The desired character offset.
287 * @param status A pointer to a UErrorCode to receive any errors.
288 * @see ucol_getOffset
291 U_STABLE
void U_EXPORT2
292 ucol_setOffset(UCollationElements
*elems
,
297 * Get the primary order of a collation order.
298 * @param order the collation order
299 * @return the primary order of a collation order.
302 U_STABLE
int32_t U_EXPORT2
303 ucol_primaryOrder (int32_t order
);
306 * Get the secondary order of a collation order.
307 * @param order the collation order
308 * @return the secondary order of a collation order.
311 U_STABLE
int32_t U_EXPORT2
312 ucol_secondaryOrder (int32_t order
);
315 * Get the tertiary order of a collation order.
316 * @param order the collation order
317 * @return the tertiary order of a collation order.
320 U_STABLE
int32_t U_EXPORT2
321 ucol_tertiaryOrder (int32_t order
);
323 #endif /* #if !UCONFIG_NO_COLLATION */