]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/ucoleitr.cpp
ICU-6.2.15.tar.gz
[apple/icu.git] / icuSources / i18n / ucoleitr.cpp
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3* Copyright (C) 2001-2003, International Business Machines
4* Corporation and others. All Rights Reserved.
5******************************************************************************
6*
7* File ucoleitr.cpp
8*
9* Modification History:
10*
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******************************************************************************/
15
16#include "unicode/utypes.h"
17
18#if !UCONFIG_NO_COLLATION
19
20#include "unicode/ucoleitr.h"
21#include "unicode/ustring.h"
22#include "unicode/sortkey.h"
23#include "ucol_imp.h"
24#include "cmemory.h"
25
26U_NAMESPACE_USE
27
28#define BUFFER_LENGTH 100
29
30typedef struct collIterate collIterator;
31
32/* public methods ---------------------------------------------------- */
33
34/**
35* Since this is going to be deprecated, I'll leave it as it is
36*/
37U_CAPI int32_t U_EXPORT2
38ucol_keyHashCode(const uint8_t *key,
39 int32_t length)
40{
41
42 CollationKey newKey(key, length);
43 return newKey.hashCode();
44}
45
46
47U_CAPI UCollationElements* U_EXPORT2
48ucol_openElements(const UCollator *coll,
49 const UChar *text,
50 int32_t textLength,
51 UErrorCode *status)
52{
53 UCollationElements *result;
54
55 if (U_FAILURE(*status)) {
56 return NULL;
57 }
58
59 result = (UCollationElements *)uprv_malloc(sizeof(UCollationElements));
60 /* test for NULL */
61 if (result == NULL) {
62 *status = U_MEMORY_ALLOCATION_ERROR;
63 return NULL;
64 }
65
66 result->reset_ = TRUE;
67 result->isWritable = FALSE;
68
69 if (text == NULL) {
70 textLength = 0;
71 }
72 uprv_init_collIterate(coll, text, textLength, &result->iteratordata_);
73
74 return result;
75}
76
77U_CAPI void U_EXPORT2
78ucol_closeElements(UCollationElements *elems)
79{
80 collIterate *ci = &elems->iteratordata_;
81 if (ci->writableBuffer != ci->stackWritableBuffer) {
82 uprv_free(ci->writableBuffer);
83 }
84 if (elems->isWritable && elems->iteratordata_.string != NULL)
85 {
86 uprv_free(elems->iteratordata_.string);
87 }
88 uprv_free(elems);
89}
90
91U_CAPI void U_EXPORT2
92ucol_reset(UCollationElements *elems)
93{
94 collIterate *ci = &(elems->iteratordata_);
95 elems->reset_ = TRUE;
96 ci->pos = ci->string;
97 if ((ci->flags & UCOL_ITER_HASLEN) == 0 || ci->endp == NULL) {
98 ci->endp = ci->string + u_strlen(ci->string);
99 }
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;
104 }
105
106 if (ci->stackWritableBuffer != ci->writableBuffer) {
107 uprv_free(ci->writableBuffer);
108 ci->writableBuffer = ci->stackWritableBuffer;
109 ci->writableBufSize = UCOL_WRITABLE_BUFFER_SIZE;
110 }
111 ci->fcdPosition = NULL;
112}
113
114U_CAPI int32_t U_EXPORT2
115ucol_next(UCollationElements *elems,
116 UErrorCode *status)
117{
118 uint32_t result;
119 if (U_FAILURE(*status)) {
120 return UCOL_NULLORDER;
121 }
122
123 elems->reset_ = FALSE;
124
125 result = ucol_getNextCE(elems->iteratordata_.coll, &elems->iteratordata_,
126 status);
127
128 if (result == UCOL_NO_MORE_CES) {
129 result = UCOL_NULLORDER;
130 }
131 return result;
132}
133
134U_CAPI int32_t U_EXPORT2
135ucol_previous(UCollationElements *elems,
136 UErrorCode *status)
137{
138 if(U_FAILURE(*status)) {
139 return UCOL_NULLORDER;
140 }
141 else
142 {
143 uint32_t result;
144
145 if (elems->reset_ &&
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;
151 }
152 elems->iteratordata_.pos = elems->iteratordata_.endp;
153 elems->iteratordata_.fcdPosition = elems->iteratordata_.endp;
154 }
155
156 elems->reset_ = FALSE;
157
158 result = ucol_getPrevCE(elems->iteratordata_.coll, &(elems->iteratordata_),
159 status);
160
161 if (result == UCOL_NO_MORE_CES) {
162 result = UCOL_NULLORDER;
163 }
164
165 return result;
166 }
167}
168
169U_CAPI int32_t U_EXPORT2
170ucol_getMaxExpansion(const UCollationElements *elems,
171 int32_t order)
172{
173 uint8_t result;
174 UCOL_GETMAXEXPANSION(elems->iteratordata_.coll, (uint32_t)order, result);
175 return result;
176}
177
178U_CAPI void U_EXPORT2
179ucol_setText( UCollationElements *elems,
180 const UChar *text,
181 int32_t textLength,
182 UErrorCode *status)
183{
184 if (U_FAILURE(*status)) {
185 return;
186 }
187
188 if (elems->isWritable && elems->iteratordata_.string != NULL)
189 {
190 uprv_free(elems->iteratordata_.string);
191 }
192
193 if (text == NULL) {
194 textLength = 0;
195 }
196
197 elems->isWritable = FALSE;
198 uprv_init_collIterate(elems->iteratordata_.coll, text, textLength,
199 &elems->iteratordata_);
200
201 elems->reset_ = TRUE;
202}
203
204U_CAPI int32_t U_EXPORT2
205ucol_getOffset(const UCollationElements *elems)
206{
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) {
215 return 0;
216 }
217 return (int32_t)(ci->fcdPosition - ci->string);
218 }
219 else {
220 return (int32_t)(ci->pos - ci->string);
221 }
222}
223
224U_CAPI void U_EXPORT2
225ucol_setOffset(UCollationElements *elems,
226 int32_t offset,
227 UErrorCode *status)
228{
229 if (U_FAILURE(*status)) {
230 return;
231 }
232
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;
240 }
241 if ((ci->flags & UCOL_ITER_HASLEN) == 0) {
242 ci->endp = ci->string + u_strlen(ci->string);
243 ci->flags |= UCOL_ITER_HASLEN;
244 }
245 ci->fcdPosition = NULL;
374ca955 246 elems->reset_ = FALSE;
b75a7d8f
A
247}
248
249U_CAPI int32_t U_EXPORT2
250ucol_primaryOrder (int32_t order)
251{
252 order &= UCOL_PRIMARYMASK;
253 return (order >> UCOL_PRIMARYORDERSHIFT);
254}
255
256U_CAPI int32_t U_EXPORT2
257ucol_secondaryOrder (int32_t order)
258{
259 order &= UCOL_SECONDARYMASK;
260 return (order >> UCOL_SECONDARYORDERSHIFT);
261}
262
263U_CAPI int32_t U_EXPORT2
264ucol_tertiaryOrder (int32_t order)
265{
266 return (order & UCOL_TERTIARYMASK);
267}
268
269#endif /* #if !UCONFIG_NO_COLLATION */