xref: /aosp_15_r20/external/icu/libandroidicu/include/unicode/ucoleitr.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker *   Copyright (C) 2001-2014, International Business Machines
6*0e209d39SAndroid Build Coastguard Worker *   Corporation and others.  All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker * File ucoleitr.h
10*0e209d39SAndroid Build Coastguard Worker *
11*0e209d39SAndroid Build Coastguard Worker * Modification History:
12*0e209d39SAndroid Build Coastguard Worker *
13*0e209d39SAndroid Build Coastguard Worker * Date        Name        Description
14*0e209d39SAndroid Build Coastguard Worker * 02/15/2001  synwee      Modified all methods to process its own function
15*0e209d39SAndroid Build Coastguard Worker *                         instead of calling the equivalent c++ api (coleitr.h)
16*0e209d39SAndroid Build Coastguard Worker *******************************************************************************/
17*0e209d39SAndroid Build Coastguard Worker 
18*0e209d39SAndroid Build Coastguard Worker #ifndef UCOLEITR_H
19*0e209d39SAndroid Build Coastguard Worker #define UCOLEITR_H
20*0e209d39SAndroid Build Coastguard Worker 
21*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
22*0e209d39SAndroid Build Coastguard Worker 
23*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_COLLATION
24*0e209d39SAndroid Build Coastguard Worker 
25*0e209d39SAndroid Build Coastguard Worker /**
26*0e209d39SAndroid Build Coastguard Worker  * This indicates an error has occurred during processing or if no more CEs is
27*0e209d39SAndroid Build Coastguard Worker  * to be returned.
28*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
29*0e209d39SAndroid Build Coastguard Worker  */
30*0e209d39SAndroid Build Coastguard Worker #define UCOL_NULLORDER        ((int32_t)0xFFFFFFFF)
31*0e209d39SAndroid Build Coastguard Worker 
32*0e209d39SAndroid Build Coastguard Worker #include "unicode/ucol.h"
33*0e209d39SAndroid Build Coastguard Worker 
34*0e209d39SAndroid Build Coastguard Worker /**
35*0e209d39SAndroid Build Coastguard Worker  * The UCollationElements struct.
36*0e209d39SAndroid Build Coastguard Worker  * For usage in C programs.
37*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
38*0e209d39SAndroid Build Coastguard Worker  */
39*0e209d39SAndroid Build Coastguard Worker typedef struct UCollationElements UCollationElements;
40*0e209d39SAndroid Build Coastguard Worker 
41*0e209d39SAndroid Build Coastguard Worker /**
42*0e209d39SAndroid Build Coastguard Worker  * \file
43*0e209d39SAndroid Build Coastguard Worker  * \brief C API: UCollationElements
44*0e209d39SAndroid Build Coastguard Worker  *
45*0e209d39SAndroid Build Coastguard Worker  * The UCollationElements API is used as an iterator to walk through each
46*0e209d39SAndroid Build Coastguard Worker  * character of an international string. Use the iterator to return the
47*0e209d39SAndroid Build Coastguard Worker  * ordering priority of the positioned character. The ordering priority of a
48*0e209d39SAndroid Build Coastguard Worker  * character, which we refer to as a key, defines how a character is collated
49*0e209d39SAndroid Build Coastguard Worker  * in the given collation object.
50*0e209d39SAndroid Build Coastguard Worker  * For example, consider the following in Slovak and in traditional Spanish collation:
51*0e209d39SAndroid Build Coastguard Worker  * <pre>
52*0e209d39SAndroid Build Coastguard Worker  * .       "ca" -> the first key is key('c') and second key is key('a').
53*0e209d39SAndroid Build Coastguard Worker  * .       "cha" -> the first key is key('ch') and second key is key('a').
54*0e209d39SAndroid Build Coastguard Worker  * </pre>
55*0e209d39SAndroid Build Coastguard Worker  * And in German phonebook collation,
56*0e209d39SAndroid Build Coastguard Worker  * <pre>
57*0e209d39SAndroid Build Coastguard Worker  * .       "<ae ligature>b"-> the first key is key('a'), the second key is key('e'), and
58*0e209d39SAndroid Build Coastguard Worker  * .       the third key is key('b').
59*0e209d39SAndroid Build Coastguard Worker  * </pre>
60*0e209d39SAndroid Build Coastguard Worker  * <p>Example of the iterator usage: (without error checking)
61*0e209d39SAndroid Build Coastguard Worker  * <pre>
62*0e209d39SAndroid Build Coastguard Worker  * .  void CollationElementIterator_Example()
63*0e209d39SAndroid Build Coastguard Worker  * .  {
64*0e209d39SAndroid Build Coastguard Worker  * .      UChar *s;
65*0e209d39SAndroid Build Coastguard Worker  * .      t_int32 order, primaryOrder;
66*0e209d39SAndroid Build Coastguard Worker  * .      UCollationElements *c;
67*0e209d39SAndroid Build Coastguard Worker  * .      UCollatorOld *coll;
68*0e209d39SAndroid Build Coastguard Worker  * .      UErrorCode success = U_ZERO_ERROR;
69*0e209d39SAndroid Build Coastguard Worker  * .      str=(UChar*)malloc(sizeof(UChar) * (strlen("This is a test")+1) );
70*0e209d39SAndroid Build Coastguard Worker  * .      u_uastrcpy(str, "This is a test");
71*0e209d39SAndroid Build Coastguard Worker  * .      coll = ucol_open(NULL, &success);
72*0e209d39SAndroid Build Coastguard Worker  * .      c = ucol_openElements(coll, str, u_strlen(str), &status);
73*0e209d39SAndroid Build Coastguard Worker  * .      order = ucol_next(c, &success);
74*0e209d39SAndroid Build Coastguard Worker  * .      ucol_reset(c);
75*0e209d39SAndroid Build Coastguard Worker  * .      order = ucol_prev(c, &success);
76*0e209d39SAndroid Build Coastguard Worker  * .      free(str);
77*0e209d39SAndroid Build Coastguard Worker  * .      ucol_close(coll);
78*0e209d39SAndroid Build Coastguard Worker  * .      ucol_closeElements(c);
79*0e209d39SAndroid Build Coastguard Worker  * .  }
80*0e209d39SAndroid Build Coastguard Worker  * </pre>
81*0e209d39SAndroid Build Coastguard Worker  * <p>
82*0e209d39SAndroid Build Coastguard Worker  * ucol_next() returns the collation order of the next.
83*0e209d39SAndroid Build Coastguard Worker  * ucol_prev() returns the collation order of the previous character.
84*0e209d39SAndroid Build Coastguard Worker  * The Collation Element Iterator moves only in one direction between calls to
85*0e209d39SAndroid Build Coastguard Worker  * ucol_reset. That is, ucol_next() and ucol_prev can not be inter-used.
86*0e209d39SAndroid Build Coastguard Worker  * Whenever ucol_prev is to be called after ucol_next() or vice versa,
87*0e209d39SAndroid Build Coastguard Worker  * ucol_reset has to be called first to reset the status, shifting pointers to
88*0e209d39SAndroid Build Coastguard Worker  * either the end or the start of the string. Hence at the next call of
89*0e209d39SAndroid Build Coastguard Worker  * ucol_prev or ucol_next, the first or last collation order will be returned.
90*0e209d39SAndroid Build Coastguard Worker  * If a change of direction is done without a ucol_reset, the result is
91*0e209d39SAndroid Build Coastguard Worker  * undefined.
92*0e209d39SAndroid Build Coastguard Worker  * The result of a forward iterate (ucol_next) and reversed result of the
93*0e209d39SAndroid Build Coastguard Worker  * backward iterate (ucol_prev) on the same string are equivalent, if
94*0e209d39SAndroid Build Coastguard Worker  * collation orders with the value 0 are ignored.
95*0e209d39SAndroid Build Coastguard Worker  * Character based on the comparison level of the collator.  A collation order
96*0e209d39SAndroid Build Coastguard Worker  * consists of primary order, secondary order and tertiary order.  The data
97*0e209d39SAndroid Build Coastguard Worker  * type of the collation order is <strong>int32_t</strong>.
98*0e209d39SAndroid Build Coastguard Worker  *
99*0e209d39SAndroid Build Coastguard Worker  * @see UCollator
100*0e209d39SAndroid Build Coastguard Worker  */
101*0e209d39SAndroid Build Coastguard Worker 
102*0e209d39SAndroid Build Coastguard Worker /**
103*0e209d39SAndroid Build Coastguard Worker  * Open the collation elements for a string.
104*0e209d39SAndroid Build Coastguard Worker  *
105*0e209d39SAndroid Build Coastguard Worker  * The UCollationElements retains a pointer to the supplied text.
106*0e209d39SAndroid Build Coastguard Worker  * The caller must not modify or delete the text while the UCollationElements
107*0e209d39SAndroid Build Coastguard Worker  * object is used to iterate over this text.
108*0e209d39SAndroid Build Coastguard Worker  *
109*0e209d39SAndroid Build Coastguard Worker  * @param coll The collator containing the desired collation rules.
110*0e209d39SAndroid Build Coastguard Worker  * @param text The text to iterate over.
111*0e209d39SAndroid Build Coastguard Worker  * @param textLength The number of characters in text, or -1 if null-terminated
112*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
113*0e209d39SAndroid Build Coastguard Worker  * @return a struct containing collation element information
114*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
115*0e209d39SAndroid Build Coastguard Worker  */
116*0e209d39SAndroid Build Coastguard Worker U_CAPI UCollationElements* U_EXPORT2
117*0e209d39SAndroid Build Coastguard Worker ucol_openElements(const UCollator  *coll,
118*0e209d39SAndroid Build Coastguard Worker                   const UChar      *text,
119*0e209d39SAndroid Build Coastguard Worker                         int32_t    textLength,
120*0e209d39SAndroid Build Coastguard Worker                         UErrorCode *status);
121*0e209d39SAndroid Build Coastguard Worker 
122*0e209d39SAndroid Build Coastguard Worker /**
123*0e209d39SAndroid Build Coastguard Worker  * get a hash code for a key... Not very useful!
124*0e209d39SAndroid Build Coastguard Worker  * @param key    the given key.
125*0e209d39SAndroid Build Coastguard Worker  * @param length the size of the key array.
126*0e209d39SAndroid Build Coastguard Worker  * @return       the hash code.
127*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
128*0e209d39SAndroid Build Coastguard Worker  */
129*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
130*0e209d39SAndroid Build Coastguard Worker ucol_keyHashCode(const uint8_t* key, int32_t length);
131*0e209d39SAndroid Build Coastguard Worker 
132*0e209d39SAndroid Build Coastguard Worker /**
133*0e209d39SAndroid Build Coastguard Worker  * Close a UCollationElements.
134*0e209d39SAndroid Build Coastguard Worker  * Once closed, a UCollationElements may no longer be used.
135*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements to close.
136*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
137*0e209d39SAndroid Build Coastguard Worker  */
138*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
139*0e209d39SAndroid Build Coastguard Worker ucol_closeElements(UCollationElements *elems);
140*0e209d39SAndroid Build Coastguard Worker 
141*0e209d39SAndroid Build Coastguard Worker /**
142*0e209d39SAndroid Build Coastguard Worker  * Reset the collation elements to their initial state.
143*0e209d39SAndroid Build Coastguard Worker  * This will move the 'cursor' to the beginning of the text.
144*0e209d39SAndroid Build Coastguard Worker  * Property settings for collation will be reset to the current status.
145*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements to reset.
146*0e209d39SAndroid Build Coastguard Worker  * @see ucol_next
147*0e209d39SAndroid Build Coastguard Worker  * @see ucol_previous
148*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
149*0e209d39SAndroid Build Coastguard Worker  */
150*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
151*0e209d39SAndroid Build Coastguard Worker ucol_reset(UCollationElements *elems);
152*0e209d39SAndroid Build Coastguard Worker 
153*0e209d39SAndroid Build Coastguard Worker /**
154*0e209d39SAndroid Build Coastguard Worker  * Get the ordering priority of the next collation element in the text.
155*0e209d39SAndroid Build Coastguard Worker  * A single character may contain more than one collation element.
156*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements containing the text.
157*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
158*0e209d39SAndroid Build Coastguard Worker  * @return The next collation elements ordering, otherwise returns UCOL_NULLORDER
159*0e209d39SAndroid Build Coastguard Worker  *         if an error has occurred or if the end of string has been reached
160*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
161*0e209d39SAndroid Build Coastguard Worker  */
162*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
163*0e209d39SAndroid Build Coastguard Worker ucol_next(UCollationElements *elems, UErrorCode *status);
164*0e209d39SAndroid Build Coastguard Worker 
165*0e209d39SAndroid Build Coastguard Worker /**
166*0e209d39SAndroid Build Coastguard Worker  * Get the ordering priority of the previous collation element in the text.
167*0e209d39SAndroid Build Coastguard Worker  * A single character may contain more than one collation element.
168*0e209d39SAndroid Build Coastguard Worker  * Note that internally a stack is used to store buffered collation elements.
169*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements containing the text.
170*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors. Notably
171*0e209d39SAndroid Build Coastguard Worker  *               a U_BUFFER_OVERFLOW_ERROR is returned if the internal stack
172*0e209d39SAndroid Build Coastguard Worker  *               buffer has been exhausted.
173*0e209d39SAndroid Build Coastguard Worker  * @return The previous collation elements ordering, otherwise returns
174*0e209d39SAndroid Build Coastguard Worker  *         UCOL_NULLORDER if an error has occurred or if the start of string has
175*0e209d39SAndroid Build Coastguard Worker  *         been reached.
176*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
177*0e209d39SAndroid Build Coastguard Worker  */
178*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
179*0e209d39SAndroid Build Coastguard Worker ucol_previous(UCollationElements *elems, UErrorCode *status);
180*0e209d39SAndroid Build Coastguard Worker 
181*0e209d39SAndroid Build Coastguard Worker /**
182*0e209d39SAndroid Build Coastguard Worker  * Get the maximum length of any expansion sequences that end with the
183*0e209d39SAndroid Build Coastguard Worker  * specified comparison order.
184*0e209d39SAndroid Build Coastguard Worker  * This is useful for .... ?
185*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements containing the text.
186*0e209d39SAndroid Build Coastguard Worker  * @param order A collation order returned by previous or next.
187*0e209d39SAndroid Build Coastguard Worker  * @return maximum size of the expansion sequences ending with the collation
188*0e209d39SAndroid Build Coastguard Worker  *         element or 1 if collation element does not occur at the end of any
189*0e209d39SAndroid Build Coastguard Worker  *         expansion sequence
190*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
191*0e209d39SAndroid Build Coastguard Worker  */
192*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
193*0e209d39SAndroid Build Coastguard Worker ucol_getMaxExpansion(const UCollationElements *elems, int32_t order);
194*0e209d39SAndroid Build Coastguard Worker 
195*0e209d39SAndroid Build Coastguard Worker /**
196*0e209d39SAndroid Build Coastguard Worker  * Set the text containing the collation elements.
197*0e209d39SAndroid Build Coastguard Worker  * Property settings for collation will remain the same.
198*0e209d39SAndroid Build Coastguard Worker  * In order to reset the iterator to the current collation property settings,
199*0e209d39SAndroid Build Coastguard Worker  * the API reset() has to be called.
200*0e209d39SAndroid Build Coastguard Worker  *
201*0e209d39SAndroid Build Coastguard Worker  * The UCollationElements retains a pointer to the supplied text.
202*0e209d39SAndroid Build Coastguard Worker  * The caller must not modify or delete the text while the UCollationElements
203*0e209d39SAndroid Build Coastguard Worker  * object is used to iterate over this text.
204*0e209d39SAndroid Build Coastguard Worker  *
205*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements to set.
206*0e209d39SAndroid Build Coastguard Worker  * @param text The source text containing the collation elements.
207*0e209d39SAndroid Build Coastguard Worker  * @param textLength The length of text, or -1 if null-terminated.
208*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
209*0e209d39SAndroid Build Coastguard Worker  * @see ucol_getText
210*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
211*0e209d39SAndroid Build Coastguard Worker  */
212*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
213*0e209d39SAndroid Build Coastguard Worker ucol_setText(      UCollationElements *elems,
214*0e209d39SAndroid Build Coastguard Worker              const UChar              *text,
215*0e209d39SAndroid Build Coastguard Worker                    int32_t            textLength,
216*0e209d39SAndroid Build Coastguard Worker                    UErrorCode         *status);
217*0e209d39SAndroid Build Coastguard Worker 
218*0e209d39SAndroid Build Coastguard Worker /**
219*0e209d39SAndroid Build Coastguard Worker  * Get the offset of the current source character.
220*0e209d39SAndroid Build Coastguard Worker  * This is an offset into the text of the character containing the current
221*0e209d39SAndroid Build Coastguard Worker  * collation elements.
222*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements to query.
223*0e209d39SAndroid Build Coastguard Worker  * @return The offset of the current source character.
224*0e209d39SAndroid Build Coastguard Worker  * @see ucol_setOffset
225*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
226*0e209d39SAndroid Build Coastguard Worker  */
227*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
228*0e209d39SAndroid Build Coastguard Worker ucol_getOffset(const UCollationElements *elems);
229*0e209d39SAndroid Build Coastguard Worker 
230*0e209d39SAndroid Build Coastguard Worker /**
231*0e209d39SAndroid Build Coastguard Worker  * Set the offset of the current source character.
232*0e209d39SAndroid Build Coastguard Worker  * This is an offset into the text of the character to be processed.
233*0e209d39SAndroid Build Coastguard Worker  * Property settings for collation will remain the same.
234*0e209d39SAndroid Build Coastguard Worker  * In order to reset the iterator to the current collation property settings,
235*0e209d39SAndroid Build Coastguard Worker  * the API reset() has to be called.
236*0e209d39SAndroid Build Coastguard Worker  * @param elems The UCollationElements to set.
237*0e209d39SAndroid Build Coastguard Worker  * @param offset The desired character offset.
238*0e209d39SAndroid Build Coastguard Worker  * @param status A pointer to a UErrorCode to receive any errors.
239*0e209d39SAndroid Build Coastguard Worker  * @see ucol_getOffset
240*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 2.0
241*0e209d39SAndroid Build Coastguard Worker  */
242*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
243*0e209d39SAndroid Build Coastguard Worker ucol_setOffset(UCollationElements *elems,
244*0e209d39SAndroid Build Coastguard Worker                int32_t             offset,
245*0e209d39SAndroid Build Coastguard Worker                UErrorCode         *status);
246*0e209d39SAndroid Build Coastguard Worker 
247*0e209d39SAndroid Build Coastguard Worker /**
248*0e209d39SAndroid Build Coastguard Worker * Get the primary order of a collation order.
249*0e209d39SAndroid Build Coastguard Worker * @param order the collation order
250*0e209d39SAndroid Build Coastguard Worker * @return the primary order of a collation order.
251*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.6
252*0e209d39SAndroid Build Coastguard Worker */
253*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
254*0e209d39SAndroid Build Coastguard Worker ucol_primaryOrder (int32_t order);
255*0e209d39SAndroid Build Coastguard Worker 
256*0e209d39SAndroid Build Coastguard Worker /**
257*0e209d39SAndroid Build Coastguard Worker * Get the secondary order of a collation order.
258*0e209d39SAndroid Build Coastguard Worker * @param order the collation order
259*0e209d39SAndroid Build Coastguard Worker * @return the secondary order of a collation order.
260*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.6
261*0e209d39SAndroid Build Coastguard Worker */
262*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
263*0e209d39SAndroid Build Coastguard Worker ucol_secondaryOrder (int32_t order);
264*0e209d39SAndroid Build Coastguard Worker 
265*0e209d39SAndroid Build Coastguard Worker /**
266*0e209d39SAndroid Build Coastguard Worker * Get the tertiary order of a collation order.
267*0e209d39SAndroid Build Coastguard Worker * @param order the collation order
268*0e209d39SAndroid Build Coastguard Worker * @return the tertiary order of a collation order.
269*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.6
270*0e209d39SAndroid Build Coastguard Worker */
271*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
272*0e209d39SAndroid Build Coastguard Worker ucol_tertiaryOrder (int32_t order);
273*0e209d39SAndroid Build Coastguard Worker 
274*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_COLLATION */
275*0e209d39SAndroid Build Coastguard Worker 
276*0e209d39SAndroid Build Coastguard Worker #endif
277