/src/icu/source/i18n/ucol.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * Copyright (C) 1996-2015, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | * file name: ucol.cpp |
9 | | * encoding: UTF-8 |
10 | | * tab size: 8 (not used) |
11 | | * indentation:4 |
12 | | * |
13 | | * Modification history |
14 | | * Date Name Comments |
15 | | * 1996-1999 various members of ICU team maintained C API for collation framework |
16 | | * 02/16/2001 synwee Added internal method getPrevSpecialCE |
17 | | * 03/01/2001 synwee Added maxexpansion functionality. |
18 | | * 03/16/2001 weiv Collation framework is rewritten in C and made UCA compliant |
19 | | * 2012-2014 markus Rewritten in C++ again. |
20 | | */ |
21 | | |
22 | | #include "unicode/utypes.h" |
23 | | |
24 | | #if !UCONFIG_NO_COLLATION |
25 | | |
26 | | #include "unicode/coll.h" |
27 | | #include "unicode/tblcoll.h" |
28 | | #include "unicode/bytestream.h" |
29 | | #include "unicode/coleitr.h" |
30 | | #include "unicode/ucoleitr.h" |
31 | | #include "unicode/ustring.h" |
32 | | #include "cmemory.h" |
33 | | #include "collation.h" |
34 | | #include "cstring.h" |
35 | | #include "putilimp.h" |
36 | | #include "uassert.h" |
37 | | #include "utracimp.h" |
38 | | |
39 | | U_NAMESPACE_USE |
40 | | |
41 | | U_CAPI UCollator* U_EXPORT2 |
42 | | ucol_openBinary(const uint8_t *bin, int32_t length, |
43 | | const UCollator *base, |
44 | | UErrorCode *status) |
45 | 0 | { |
46 | 0 | if(U_FAILURE(*status)) { return NULL; } |
47 | 0 | RuleBasedCollator *coll = new RuleBasedCollator( |
48 | 0 | bin, length, |
49 | 0 | RuleBasedCollator::rbcFromUCollator(base), |
50 | 0 | *status); |
51 | 0 | if(coll == NULL) { |
52 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
53 | 0 | return NULL; |
54 | 0 | } |
55 | 0 | if(U_FAILURE(*status)) { |
56 | 0 | delete coll; |
57 | 0 | return NULL; |
58 | 0 | } |
59 | 0 | return coll->toUCollator(); |
60 | 0 | } |
61 | | |
62 | | U_CAPI int32_t U_EXPORT2 |
63 | | ucol_cloneBinary(const UCollator *coll, |
64 | | uint8_t *buffer, int32_t capacity, |
65 | | UErrorCode *status) |
66 | 0 | { |
67 | 0 | if(U_FAILURE(*status)) { |
68 | 0 | return 0; |
69 | 0 | } |
70 | 0 | const RuleBasedCollator *rbc = RuleBasedCollator::rbcFromUCollator(coll); |
71 | 0 | if(rbc == NULL && coll != NULL) { |
72 | 0 | *status = U_UNSUPPORTED_ERROR; |
73 | 0 | return 0; |
74 | 0 | } |
75 | 0 | return rbc->cloneBinary(buffer, capacity, *status); |
76 | 0 | } |
77 | | |
78 | | U_CAPI UCollator* U_EXPORT2 |
79 | | ucol_safeClone(const UCollator *coll, void * /*stackBuffer*/, int32_t * pBufferSize, UErrorCode *status) |
80 | 0 | { |
81 | 0 | if (status == NULL || U_FAILURE(*status)){ |
82 | 0 | return NULL; |
83 | 0 | } |
84 | 0 | if (coll == NULL) { |
85 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 0 | if (pBufferSize != NULL) { |
89 | 0 | int32_t inputSize = *pBufferSize; |
90 | 0 | *pBufferSize = 1; |
91 | 0 | if (inputSize == 0) { |
92 | 0 | return NULL; // preflighting for deprecated functionality |
93 | 0 | } |
94 | 0 | } |
95 | 0 | Collator *newColl = Collator::fromUCollator(coll)->clone(); |
96 | 0 | if (newColl == NULL) { |
97 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
98 | 0 | return nullptr; |
99 | 0 | } else { |
100 | 0 | *status = U_SAFECLONE_ALLOCATED_WARNING; |
101 | 0 | } |
102 | 0 | return newColl->toUCollator(); |
103 | 0 | } |
104 | | |
105 | | U_CAPI void U_EXPORT2 |
106 | | ucol_close(UCollator *coll) |
107 | 0 | { |
108 | 0 | UTRACE_ENTRY_OC(UTRACE_UCOL_CLOSE); |
109 | 0 | UTRACE_DATA1(UTRACE_INFO, "coll = %p", coll); |
110 | 0 | if(coll != NULL) { |
111 | 0 | delete Collator::fromUCollator(coll); |
112 | 0 | } |
113 | 0 | UTRACE_EXIT(); |
114 | 0 | } |
115 | | |
116 | | U_CAPI int32_t U_EXPORT2 |
117 | | ucol_mergeSortkeys(const uint8_t *src1, int32_t src1Length, |
118 | | const uint8_t *src2, int32_t src2Length, |
119 | 0 | uint8_t *dest, int32_t destCapacity) { |
120 | | /* check arguments */ |
121 | 0 | if( src1==NULL || src1Length<-1 || src1Length==0 || (src1Length>0 && src1[src1Length-1]!=0) || |
122 | 0 | src2==NULL || src2Length<-1 || src2Length==0 || (src2Length>0 && src2[src2Length-1]!=0) || |
123 | 0 | destCapacity<0 || (destCapacity>0 && dest==NULL) |
124 | 0 | ) { |
125 | | /* error, attempt to write a zero byte and return 0 */ |
126 | 0 | if(dest!=NULL && destCapacity>0) { |
127 | 0 | *dest=0; |
128 | 0 | } |
129 | 0 | return 0; |
130 | 0 | } |
131 | | |
132 | | /* check lengths and capacity */ |
133 | 0 | if(src1Length<0) { |
134 | 0 | src1Length=(int32_t)uprv_strlen((const char *)src1)+1; |
135 | 0 | } |
136 | 0 | if(src2Length<0) { |
137 | 0 | src2Length=(int32_t)uprv_strlen((const char *)src2)+1; |
138 | 0 | } |
139 | |
|
140 | 0 | int32_t destLength=src1Length+src2Length; |
141 | 0 | if(destLength>destCapacity) { |
142 | | /* the merged sort key does not fit into the destination */ |
143 | 0 | return destLength; |
144 | 0 | } |
145 | | |
146 | | /* merge the sort keys with the same number of levels */ |
147 | 0 | uint8_t *p=dest; |
148 | 0 | for(;;) { |
149 | | /* copy level from src1 not including 00 or 01 */ |
150 | 0 | uint8_t b; |
151 | 0 | while((b=*src1)>=2) { |
152 | 0 | ++src1; |
153 | 0 | *p++=b; |
154 | 0 | } |
155 | | |
156 | | /* add a 02 merge separator */ |
157 | 0 | *p++=2; |
158 | | |
159 | | /* copy level from src2 not including 00 or 01 */ |
160 | 0 | while((b=*src2)>=2) { |
161 | 0 | ++src2; |
162 | 0 | *p++=b; |
163 | 0 | } |
164 | | |
165 | | /* if both sort keys have another level, then add a 01 level separator and continue */ |
166 | 0 | if(*src1==1 && *src2==1) { |
167 | 0 | ++src1; |
168 | 0 | ++src2; |
169 | 0 | *p++=1; |
170 | 0 | } else { |
171 | 0 | break; |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * here, at least one sort key is finished now, but the other one |
177 | | * might have some contents left from containing more levels; |
178 | | * that contents is just appended to the result |
179 | | */ |
180 | 0 | if(*src1!=0) { |
181 | | /* src1 is not finished, therefore *src2==0, and src1 is appended */ |
182 | 0 | src2=src1; |
183 | 0 | } |
184 | | /* append src2, "the other, unfinished sort key" */ |
185 | 0 | while((*p++=*src2++)!=0) {} |
186 | | |
187 | | /* the actual length might be less than destLength if either sort key contained illegally embedded zero bytes */ |
188 | 0 | return (int32_t)(p-dest); |
189 | 0 | } |
190 | | |
191 | | U_CAPI int32_t U_EXPORT2 |
192 | | ucol_getSortKey(const UCollator *coll, |
193 | | const UChar *source, |
194 | | int32_t sourceLength, |
195 | | uint8_t *result, |
196 | | int32_t resultLength) |
197 | 0 | { |
198 | 0 | UTRACE_ENTRY(UTRACE_UCOL_GET_SORTKEY); |
199 | 0 | if (UTRACE_LEVEL(UTRACE_VERBOSE)) { |
200 | 0 | UTRACE_DATA3(UTRACE_VERBOSE, "coll=%p, source string = %vh ", coll, source, |
201 | 0 | ((sourceLength==-1 && source!=NULL) ? u_strlen(source) : sourceLength)); |
202 | 0 | } |
203 | |
|
204 | 0 | int32_t keySize = Collator::fromUCollator(coll)-> |
205 | 0 | getSortKey(source, sourceLength, result, resultLength); |
206 | |
|
207 | 0 | UTRACE_DATA2(UTRACE_VERBOSE, "Sort Key = %vb", result, keySize); |
208 | 0 | UTRACE_EXIT_VALUE(keySize); |
209 | 0 | return keySize; |
210 | 0 | } |
211 | | |
212 | | U_CAPI int32_t U_EXPORT2 |
213 | | ucol_nextSortKeyPart(const UCollator *coll, |
214 | | UCharIterator *iter, |
215 | | uint32_t state[2], |
216 | | uint8_t *dest, int32_t count, |
217 | | UErrorCode *status) |
218 | 0 | { |
219 | | /* error checking */ |
220 | 0 | if(status==NULL || U_FAILURE(*status)) { |
221 | 0 | return 0; |
222 | 0 | } |
223 | 0 | UTRACE_ENTRY(UTRACE_UCOL_NEXTSORTKEYPART); |
224 | 0 | UTRACE_DATA6(UTRACE_VERBOSE, "coll=%p, iter=%p, state=%d %d, dest=%p, count=%d", |
225 | 0 | coll, iter, state[0], state[1], dest, count); |
226 | |
|
227 | 0 | int32_t i = Collator::fromUCollator(coll)-> |
228 | 0 | internalNextSortKeyPart(iter, state, dest, count, *status); |
229 | | |
230 | | // Return number of meaningful sortkey bytes. |
231 | 0 | UTRACE_DATA4(UTRACE_VERBOSE, "dest = %vb, state=%d %d", |
232 | 0 | dest,i, state[0], state[1]); |
233 | 0 | UTRACE_EXIT_VALUE_STATUS(i, *status); |
234 | 0 | return i; |
235 | 0 | } |
236 | | |
237 | | /** |
238 | | * Produce a bound for a given sortkey and a number of levels. |
239 | | */ |
240 | | U_CAPI int32_t U_EXPORT2 |
241 | | ucol_getBound(const uint8_t *source, |
242 | | int32_t sourceLength, |
243 | | UColBoundMode boundType, |
244 | | uint32_t noOfLevels, |
245 | | uint8_t *result, |
246 | | int32_t resultLength, |
247 | | UErrorCode *status) |
248 | 0 | { |
249 | | // consistency checks |
250 | 0 | if(status == NULL || U_FAILURE(*status)) { |
251 | 0 | return 0; |
252 | 0 | } |
253 | 0 | if(source == NULL) { |
254 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | 0 | int32_t sourceIndex = 0; |
259 | | // Scan the string until we skip enough of the key OR reach the end of the key |
260 | 0 | do { |
261 | 0 | sourceIndex++; |
262 | 0 | if(source[sourceIndex] == Collation::LEVEL_SEPARATOR_BYTE) { |
263 | 0 | noOfLevels--; |
264 | 0 | } |
265 | 0 | } while (noOfLevels > 0 |
266 | 0 | && (source[sourceIndex] != 0 || sourceIndex < sourceLength)); |
267 | |
|
268 | 0 | if((source[sourceIndex] == 0 || sourceIndex == sourceLength) |
269 | 0 | && noOfLevels > 0) { |
270 | 0 | *status = U_SORT_KEY_TOO_SHORT_WARNING; |
271 | 0 | } |
272 | | |
273 | | |
274 | | // READ ME: this code assumes that the values for boundType |
275 | | // enum will not changes. They are set so that the enum value |
276 | | // corresponds to the number of extra bytes each bound type |
277 | | // needs. |
278 | 0 | if(result != NULL && resultLength >= sourceIndex+boundType) { |
279 | 0 | uprv_memcpy(result, source, sourceIndex); |
280 | 0 | switch(boundType) { |
281 | | // Lower bound just gets terminated. No extra bytes |
282 | 0 | case UCOL_BOUND_LOWER: // = 0 |
283 | 0 | break; |
284 | | // Upper bound needs one extra byte |
285 | 0 | case UCOL_BOUND_UPPER: // = 1 |
286 | 0 | result[sourceIndex++] = 2; |
287 | 0 | break; |
288 | | // Upper long bound needs two extra bytes |
289 | 0 | case UCOL_BOUND_UPPER_LONG: // = 2 |
290 | 0 | result[sourceIndex++] = 0xFF; |
291 | 0 | result[sourceIndex++] = 0xFF; |
292 | 0 | break; |
293 | 0 | default: |
294 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
295 | 0 | return 0; |
296 | 0 | } |
297 | 0 | result[sourceIndex++] = 0; |
298 | |
|
299 | 0 | return sourceIndex; |
300 | 0 | } else { |
301 | 0 | return sourceIndex+boundType+1; |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | | U_CAPI void U_EXPORT2 |
306 | 0 | ucol_setMaxVariable(UCollator *coll, UColReorderCode group, UErrorCode *pErrorCode) { |
307 | 0 | if(U_FAILURE(*pErrorCode)) { return; } |
308 | 0 | Collator::fromUCollator(coll)->setMaxVariable(group, *pErrorCode); |
309 | 0 | } |
310 | | |
311 | | U_CAPI UColReorderCode U_EXPORT2 |
312 | 0 | ucol_getMaxVariable(const UCollator *coll) { |
313 | 0 | return Collator::fromUCollator(coll)->getMaxVariable(); |
314 | 0 | } |
315 | | |
316 | | U_CAPI uint32_t U_EXPORT2 |
317 | 0 | ucol_setVariableTop(UCollator *coll, const UChar *varTop, int32_t len, UErrorCode *status) { |
318 | 0 | if(U_FAILURE(*status) || coll == NULL) { |
319 | 0 | return 0; |
320 | 0 | } |
321 | 0 | return Collator::fromUCollator(coll)->setVariableTop(varTop, len, *status); |
322 | 0 | } |
323 | | |
324 | 0 | U_CAPI uint32_t U_EXPORT2 ucol_getVariableTop(const UCollator *coll, UErrorCode *status) { |
325 | 0 | if(U_FAILURE(*status) || coll == NULL) { |
326 | 0 | return 0; |
327 | 0 | } |
328 | 0 | return Collator::fromUCollator(coll)->getVariableTop(*status); |
329 | 0 | } |
330 | | |
331 | | U_CAPI void U_EXPORT2 |
332 | 0 | ucol_restoreVariableTop(UCollator *coll, const uint32_t varTop, UErrorCode *status) { |
333 | 0 | if(U_FAILURE(*status) || coll == NULL) { |
334 | 0 | return; |
335 | 0 | } |
336 | 0 | Collator::fromUCollator(coll)->setVariableTop(varTop, *status); |
337 | 0 | } |
338 | | |
339 | | U_CAPI void U_EXPORT2 |
340 | 0 | ucol_setAttribute(UCollator *coll, UColAttribute attr, UColAttributeValue value, UErrorCode *status) { |
341 | 0 | if(U_FAILURE(*status) || coll == NULL) { |
342 | 0 | return; |
343 | 0 | } |
344 | | |
345 | 0 | Collator::fromUCollator(coll)->setAttribute(attr, value, *status); |
346 | 0 | } |
347 | | |
348 | | U_CAPI UColAttributeValue U_EXPORT2 |
349 | 0 | ucol_getAttribute(const UCollator *coll, UColAttribute attr, UErrorCode *status) { |
350 | 0 | if(U_FAILURE(*status) || coll == NULL) { |
351 | 0 | return UCOL_DEFAULT; |
352 | 0 | } |
353 | | |
354 | 0 | return Collator::fromUCollator(coll)->getAttribute(attr, *status); |
355 | 0 | } |
356 | | |
357 | | U_CAPI void U_EXPORT2 |
358 | | ucol_setStrength( UCollator *coll, |
359 | | UCollationStrength strength) |
360 | 0 | { |
361 | 0 | UErrorCode status = U_ZERO_ERROR; |
362 | 0 | ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status); |
363 | 0 | } |
364 | | |
365 | | U_CAPI UCollationStrength U_EXPORT2 |
366 | | ucol_getStrength(const UCollator *coll) |
367 | 0 | { |
368 | 0 | UErrorCode status = U_ZERO_ERROR; |
369 | 0 | return ucol_getAttribute(coll, UCOL_STRENGTH, &status); |
370 | 0 | } |
371 | | |
372 | | U_CAPI int32_t U_EXPORT2 |
373 | | ucol_getReorderCodes(const UCollator *coll, |
374 | | int32_t *dest, |
375 | | int32_t destCapacity, |
376 | 0 | UErrorCode *status) { |
377 | 0 | if (U_FAILURE(*status)) { |
378 | 0 | return 0; |
379 | 0 | } |
380 | | |
381 | 0 | return Collator::fromUCollator(coll)->getReorderCodes(dest, destCapacity, *status); |
382 | 0 | } |
383 | | |
384 | | U_CAPI void U_EXPORT2 |
385 | | ucol_setReorderCodes(UCollator* coll, |
386 | | const int32_t* reorderCodes, |
387 | | int32_t reorderCodesLength, |
388 | 0 | UErrorCode *status) { |
389 | 0 | if (U_FAILURE(*status)) { |
390 | 0 | return; |
391 | 0 | } |
392 | | |
393 | 0 | Collator::fromUCollator(coll)->setReorderCodes(reorderCodes, reorderCodesLength, *status); |
394 | 0 | } |
395 | | |
396 | | U_CAPI int32_t U_EXPORT2 |
397 | | ucol_getEquivalentReorderCodes(int32_t reorderCode, |
398 | | int32_t* dest, |
399 | | int32_t destCapacity, |
400 | 0 | UErrorCode *pErrorCode) { |
401 | 0 | return Collator::getEquivalentReorderCodes(reorderCode, dest, destCapacity, *pErrorCode); |
402 | 0 | } |
403 | | |
404 | | U_CAPI void U_EXPORT2 |
405 | | ucol_getVersion(const UCollator* coll, |
406 | | UVersionInfo versionInfo) |
407 | 0 | { |
408 | 0 | Collator::fromUCollator(coll)->getVersion(versionInfo); |
409 | 0 | } |
410 | | |
411 | | U_CAPI UCollationResult U_EXPORT2 |
412 | | ucol_strcollIter( const UCollator *coll, |
413 | | UCharIterator *sIter, |
414 | | UCharIterator *tIter, |
415 | | UErrorCode *status) |
416 | 0 | { |
417 | 0 | if(!status || U_FAILURE(*status)) { |
418 | 0 | return UCOL_EQUAL; |
419 | 0 | } |
420 | | |
421 | 0 | UTRACE_ENTRY(UTRACE_UCOL_STRCOLLITER); |
422 | 0 | UTRACE_DATA3(UTRACE_VERBOSE, "coll=%p, sIter=%p, tIter=%p", coll, sIter, tIter); |
423 | |
|
424 | 0 | if(sIter == NULL || tIter == NULL || coll == NULL) { |
425 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
426 | 0 | UTRACE_EXIT_VALUE_STATUS(UCOL_EQUAL, *status); |
427 | 0 | return UCOL_EQUAL; |
428 | 0 | } |
429 | | |
430 | 0 | UCollationResult result = Collator::fromUCollator(coll)->compare(*sIter, *tIter, *status); |
431 | |
|
432 | 0 | UTRACE_EXIT_VALUE_STATUS(result, *status); |
433 | 0 | return result; |
434 | 0 | } |
435 | | |
436 | | |
437 | | /* */ |
438 | | /* ucol_strcoll Main public API string comparison function */ |
439 | | /* */ |
440 | | U_CAPI UCollationResult U_EXPORT2 |
441 | | ucol_strcoll( const UCollator *coll, |
442 | | const UChar *source, |
443 | | int32_t sourceLength, |
444 | | const UChar *target, |
445 | | int32_t targetLength) |
446 | 0 | { |
447 | 0 | UTRACE_ENTRY(UTRACE_UCOL_STRCOLL); |
448 | 0 | if (UTRACE_LEVEL(UTRACE_VERBOSE)) { |
449 | 0 | UTRACE_DATA3(UTRACE_VERBOSE, "coll=%p, source=%p, target=%p", coll, source, target); |
450 | 0 | UTRACE_DATA2(UTRACE_VERBOSE, "source string = %vh ", source, sourceLength); |
451 | 0 | UTRACE_DATA2(UTRACE_VERBOSE, "target string = %vh ", target, targetLength); |
452 | 0 | } |
453 | |
|
454 | 0 | UErrorCode status = U_ZERO_ERROR; |
455 | 0 | UCollationResult returnVal = Collator::fromUCollator(coll)-> |
456 | 0 | compare(source, sourceLength, target, targetLength, status); |
457 | 0 | UTRACE_EXIT_VALUE_STATUS(returnVal, status); |
458 | 0 | return returnVal; |
459 | 0 | } |
460 | | |
461 | | U_CAPI UCollationResult U_EXPORT2 |
462 | | ucol_strcollUTF8( |
463 | | const UCollator *coll, |
464 | | const char *source, |
465 | | int32_t sourceLength, |
466 | | const char *target, |
467 | | int32_t targetLength, |
468 | | UErrorCode *status) |
469 | 0 | { |
470 | 0 | UTRACE_ENTRY(UTRACE_UCOL_STRCOLLUTF8); |
471 | 0 | if (UTRACE_LEVEL(UTRACE_VERBOSE)) { |
472 | 0 | UTRACE_DATA3(UTRACE_VERBOSE, "coll=%p, source=%p, target=%p", coll, source, target); |
473 | 0 | UTRACE_DATA2(UTRACE_VERBOSE, "source string = %vb ", source, sourceLength); |
474 | 0 | UTRACE_DATA2(UTRACE_VERBOSE, "target string = %vb ", target, targetLength); |
475 | 0 | } |
476 | |
|
477 | 0 | if (U_FAILURE(*status)) { |
478 | | /* do nothing */ |
479 | 0 | UTRACE_EXIT_VALUE_STATUS(UCOL_EQUAL, *status); |
480 | 0 | return UCOL_EQUAL; |
481 | 0 | } |
482 | | |
483 | 0 | UCollationResult returnVal = Collator::fromUCollator(coll)->internalCompareUTF8( |
484 | 0 | source, sourceLength, target, targetLength, *status); |
485 | 0 | UTRACE_EXIT_VALUE_STATUS(returnVal, *status); |
486 | 0 | return returnVal; |
487 | 0 | } |
488 | | |
489 | | |
490 | | /* convenience function for comparing strings */ |
491 | | U_CAPI UBool U_EXPORT2 |
492 | | ucol_greater( const UCollator *coll, |
493 | | const UChar *source, |
494 | | int32_t sourceLength, |
495 | | const UChar *target, |
496 | | int32_t targetLength) |
497 | 0 | { |
498 | 0 | return (ucol_strcoll(coll, source, sourceLength, target, targetLength) |
499 | 0 | == UCOL_GREATER); |
500 | 0 | } |
501 | | |
502 | | /* convenience function for comparing strings */ |
503 | | U_CAPI UBool U_EXPORT2 |
504 | | ucol_greaterOrEqual( const UCollator *coll, |
505 | | const UChar *source, |
506 | | int32_t sourceLength, |
507 | | const UChar *target, |
508 | | int32_t targetLength) |
509 | 0 | { |
510 | 0 | return (ucol_strcoll(coll, source, sourceLength, target, targetLength) |
511 | 0 | != UCOL_LESS); |
512 | 0 | } |
513 | | |
514 | | /* convenience function for comparing strings */ |
515 | | U_CAPI UBool U_EXPORT2 |
516 | | ucol_equal( const UCollator *coll, |
517 | | const UChar *source, |
518 | | int32_t sourceLength, |
519 | | const UChar *target, |
520 | | int32_t targetLength) |
521 | 0 | { |
522 | 0 | return (ucol_strcoll(coll, source, sourceLength, target, targetLength) |
523 | 0 | == UCOL_EQUAL); |
524 | 0 | } |
525 | | |
526 | | U_CAPI void U_EXPORT2 |
527 | 0 | ucol_getUCAVersion(const UCollator* coll, UVersionInfo info) { |
528 | 0 | const Collator *c = Collator::fromUCollator(coll); |
529 | 0 | if(c != NULL) { |
530 | 0 | UVersionInfo v; |
531 | 0 | c->getVersion(v); |
532 | | // Note: This is tied to how the current implementation encodes the UCA version |
533 | | // in the overall getVersion(). |
534 | | // Alternatively, we could load the root collator and get at lower-level data from there. |
535 | | // Either way, it will reflect the input collator's UCA version only |
536 | | // if it is a known implementation. |
537 | | // It would be cleaner to make this a virtual Collator method. |
538 | 0 | info[0] = v[1] >> 3; |
539 | 0 | info[1] = v[1] & 7; |
540 | 0 | info[2] = v[2] >> 6; |
541 | 0 | info[3] = 0; |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | | U_CAPI const UChar * U_EXPORT2 |
546 | 0 | ucol_getRules(const UCollator *coll, int32_t *length) { |
547 | 0 | const RuleBasedCollator *rbc = RuleBasedCollator::rbcFromUCollator(coll); |
548 | | // OK to crash if coll==NULL: We do not want to check "this" pointers. |
549 | 0 | if(rbc != NULL || coll == NULL) { |
550 | 0 | const UnicodeString &rules = rbc->getRules(); |
551 | 0 | U_ASSERT(rules.getBuffer()[rules.length()] == 0); |
552 | 0 | *length = rules.length(); |
553 | 0 | return rules.getBuffer(); |
554 | 0 | } |
555 | 0 | static const UChar _NUL = 0; |
556 | 0 | *length = 0; |
557 | 0 | return &_NUL; |
558 | 0 | } |
559 | | |
560 | | U_CAPI int32_t U_EXPORT2 |
561 | 0 | ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) { |
562 | 0 | UnicodeString rules; |
563 | 0 | const RuleBasedCollator *rbc = RuleBasedCollator::rbcFromUCollator(coll); |
564 | 0 | if(rbc != NULL || coll == NULL) { |
565 | 0 | rbc->getRules(delta, rules); |
566 | 0 | } |
567 | 0 | if(buffer != NULL && bufferLen > 0) { |
568 | 0 | UErrorCode errorCode = U_ZERO_ERROR; |
569 | 0 | return rules.extract(buffer, bufferLen, errorCode); |
570 | 0 | } else { |
571 | 0 | return rules.length(); |
572 | 0 | } |
573 | 0 | } |
574 | | |
575 | | U_CAPI const char * U_EXPORT2 |
576 | 0 | ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { |
577 | 0 | return ucol_getLocaleByType(coll, type, status); |
578 | 0 | } |
579 | | |
580 | | U_CAPI const char * U_EXPORT2 |
581 | 0 | ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { |
582 | 0 | if(U_FAILURE(*status)) { |
583 | 0 | return NULL; |
584 | 0 | } |
585 | 0 | UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE); |
586 | 0 | UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll); |
587 | |
|
588 | 0 | const char *result; |
589 | 0 | const RuleBasedCollator *rbc = RuleBasedCollator::rbcFromUCollator(coll); |
590 | 0 | if(rbc == NULL && coll != NULL) { |
591 | 0 | *status = U_UNSUPPORTED_ERROR; |
592 | 0 | result = NULL; |
593 | 0 | } else { |
594 | 0 | result = rbc->internalGetLocaleID(type, *status); |
595 | 0 | } |
596 | |
|
597 | 0 | UTRACE_DATA1(UTRACE_INFO, "result = %s", result); |
598 | 0 | UTRACE_EXIT_STATUS(*status); |
599 | 0 | return result; |
600 | 0 | } |
601 | | |
602 | | U_CAPI USet * U_EXPORT2 |
603 | 0 | ucol_getTailoredSet(const UCollator *coll, UErrorCode *status) { |
604 | 0 | if(U_FAILURE(*status)) { |
605 | 0 | return NULL; |
606 | 0 | } |
607 | 0 | UnicodeSet *set = Collator::fromUCollator(coll)->getTailoredSet(*status); |
608 | 0 | if(U_FAILURE(*status)) { |
609 | 0 | delete set; |
610 | 0 | return NULL; |
611 | 0 | } |
612 | 0 | return set->toUSet(); |
613 | 0 | } |
614 | | |
615 | | U_CAPI UBool U_EXPORT2 |
616 | 0 | ucol_equals(const UCollator *source, const UCollator *target) { |
617 | 0 | return source == target || |
618 | 0 | (*Collator::fromUCollator(source)) == (*Collator::fromUCollator(target)); |
619 | 0 | } |
620 | | |
621 | | #endif /* #if !UCONFIG_NO_COLLATION */ |