2 ******************************************************************************
3 * Copyright (C) 2001-2003, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/15/2001 synwee Modified all methods to process its own function
13 * instead of calling the equivalent c++ api (coleitr.h)
14 ******************************************************************************/
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_COLLATION
20 #include "unicode/ucoleitr.h"
21 #include "unicode/ustring.h"
22 #include "unicode/sortkey.h"
28 #define BUFFER_LENGTH 100
30 typedef struct collIterate collIterator
;
32 /* public methods ---------------------------------------------------- */
35 * Since this is going to be deprecated, I'll leave it as it is
37 U_CAPI
int32_t U_EXPORT2
38 ucol_keyHashCode(const uint8_t *key
,
42 CollationKey
newKey(key
, length
);
43 return newKey
.hashCode();
47 U_CAPI UCollationElements
* U_EXPORT2
48 ucol_openElements(const UCollator
*coll
,
53 UCollationElements
*result
;
55 if (U_FAILURE(*status
)) {
59 result
= (UCollationElements
*)uprv_malloc(sizeof(UCollationElements
));
62 *status
= U_MEMORY_ALLOCATION_ERROR
;
66 result
->reset_
= TRUE
;
67 result
->isWritable
= FALSE
;
72 uprv_init_collIterate(coll
, text
, textLength
, &result
->iteratordata_
);
78 ucol_closeElements(UCollationElements
*elems
)
80 collIterate
*ci
= &elems
->iteratordata_
;
81 if (ci
->writableBuffer
!= ci
->stackWritableBuffer
) {
82 uprv_free(ci
->writableBuffer
);
84 if (elems
->isWritable
&& elems
->iteratordata_
.string
!= NULL
)
86 uprv_free(elems
->iteratordata_
.string
);
92 ucol_reset(UCollationElements
*elems
)
94 collIterate
*ci
= &(elems
->iteratordata_
);
97 if ((ci
->flags
& UCOL_ITER_HASLEN
) == 0 || ci
->endp
== NULL
) {
98 ci
->endp
= ci
->string
+ u_strlen(ci
->string
);
100 ci
->CEpos
= ci
->toReturn
= ci
->CEs
;
101 ci
->flags
= UCOL_ITER_HASLEN
;
102 if (ci
->coll
->normalizationMode
== UCOL_ON
) {
103 ci
->flags
|= UCOL_ITER_NORM
;
106 if (ci
->stackWritableBuffer
!= ci
->writableBuffer
) {
107 uprv_free(ci
->writableBuffer
);
108 ci
->writableBuffer
= ci
->stackWritableBuffer
;
109 ci
->writableBufSize
= UCOL_WRITABLE_BUFFER_SIZE
;
111 ci
->fcdPosition
= NULL
;
114 U_CAPI
int32_t U_EXPORT2
115 ucol_next(UCollationElements
*elems
,
119 if (U_FAILURE(*status
)) {
120 return UCOL_NULLORDER
;
123 elems
->reset_
= FALSE
;
125 result
= ucol_getNextCE(elems
->iteratordata_
.coll
, &elems
->iteratordata_
,
128 if (result
== UCOL_NO_MORE_CES
) {
129 result
= UCOL_NULLORDER
;
134 U_CAPI
int32_t U_EXPORT2
135 ucol_previous(UCollationElements
*elems
,
138 if(U_FAILURE(*status
)) {
139 return UCOL_NULLORDER
;
146 (elems
->iteratordata_
.pos
== elems
->iteratordata_
.string
)) {
147 if (elems
->iteratordata_
.endp
== NULL
) {
148 elems
->iteratordata_
.endp
= elems
->iteratordata_
.string
+
149 u_strlen(elems
->iteratordata_
.string
);
150 elems
->iteratordata_
.flags
|= UCOL_ITER_HASLEN
;
152 elems
->iteratordata_
.pos
= elems
->iteratordata_
.endp
;
153 elems
->iteratordata_
.fcdPosition
= elems
->iteratordata_
.endp
;
156 elems
->reset_
= FALSE
;
158 result
= ucol_getPrevCE(elems
->iteratordata_
.coll
, &(elems
->iteratordata_
),
161 if (result
== UCOL_NO_MORE_CES
) {
162 result
= UCOL_NULLORDER
;
169 U_CAPI
int32_t U_EXPORT2
170 ucol_getMaxExpansion(const UCollationElements
*elems
,
174 UCOL_GETMAXEXPANSION(elems
->iteratordata_
.coll
, (uint32_t)order
, result
);
178 U_CAPI
void U_EXPORT2
179 ucol_setText( UCollationElements
*elems
,
184 if (U_FAILURE(*status
)) {
188 if (elems
->isWritable
&& elems
->iteratordata_
.string
!= NULL
)
190 uprv_free(elems
->iteratordata_
.string
);
197 elems
->isWritable
= FALSE
;
198 uprv_init_collIterate(elems
->iteratordata_
.coll
, text
, textLength
,
199 &elems
->iteratordata_
);
201 elems
->reset_
= TRUE
;
204 U_CAPI
int32_t U_EXPORT2
205 ucol_getOffset(const UCollationElements
*elems
)
207 const collIterate
*ci
= &(elems
->iteratordata_
);
208 // while processing characters in normalization buffer getOffset will
209 // return the next non-normalized character.
210 // should be inline with the old implementation since the old codes uses
211 // nextDecomp in normalizer which also decomposes the string till the
212 // first base character is found.
213 if (ci
->flags
& UCOL_ITER_INNORMBUF
) {
214 if (ci
->fcdPosition
== NULL
) {
217 return (int32_t)(ci
->fcdPosition
- ci
->string
);
220 return (int32_t)(ci
->pos
- ci
->string
);
224 U_CAPI
void U_EXPORT2
225 ucol_setOffset(UCollationElements
*elems
,
229 if (U_FAILURE(*status
)) {
233 // this methods will clean up any use of the writable buffer and points to
234 // the original string
235 collIterate
*ci
= &(elems
->iteratordata_
);
236 ci
->pos
= ci
->string
+ offset
;
237 ci
->CEpos
= ci
->toReturn
= ci
->CEs
;
238 if (ci
->flags
& UCOL_ITER_INNORMBUF
) {
239 ci
->flags
= ci
->origFlags
;
241 if ((ci
->flags
& UCOL_ITER_HASLEN
) == 0) {
242 ci
->endp
= ci
->string
+ u_strlen(ci
->string
);
243 ci
->flags
|= UCOL_ITER_HASLEN
;
245 ci
->fcdPosition
= NULL
;
246 elems
->reset_
= FALSE
;
249 U_CAPI
int32_t U_EXPORT2
250 ucol_primaryOrder (int32_t order
)
252 order
&= UCOL_PRIMARYMASK
;
253 return (order
>> UCOL_PRIMARYORDERSHIFT
);
256 U_CAPI
int32_t U_EXPORT2
257 ucol_secondaryOrder (int32_t order
)
259 order
&= UCOL_SECONDARYMASK
;
260 return (order
>> UCOL_SECONDARYORDERSHIFT
);
263 U_CAPI
int32_t U_EXPORT2
264 ucol_tertiaryOrder (int32_t order
)
266 return (order
& UCOL_TERTIARYMASK
);
269 #endif /* #if !UCONFIG_NO_COLLATION */