Bug Summary

File:out/../deps/icu-small/source/i18n/ucol.cpp
Warning:line 75, column 12
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ucol.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D U_COMMON_IMPLEMENTATION=1 -D U_I18N_IMPLEMENTATION=1 -D U_IO_IMPLEMENTATION=1 -D U_TOOLUTIL_IMPLEMENTATION=1 -D U_ATTRIBUTE_DEPRECATED= -D _CRT_SECURE_NO_DEPRECATE= -D U_STATIC_IMPLEMENTATION=1 -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/icu-small/source/common -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/tools/toolutil -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-strict-aliasing -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/icu-small/source/i18n/ucol.cpp

../deps/icu-small/source/i18n/ucol.cpp

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

../deps/icu-small/source/i18n/unicode/tblcoll.h

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-2016, International Business Machines Corporation and
6* others. All Rights Reserved.
7******************************************************************************
8*/
9
10/**
11 * \file
12 * \brief C++ API: The RuleBasedCollator class implements the Collator abstract base class.
13 */
14
15/**
16* File tblcoll.h
17*
18* Created by: Helena Shih
19*
20* Modification History:
21*
22* Date Name Description
23* 2/5/97 aliu Added streamIn and streamOut methods. Added
24* constructor which reads RuleBasedCollator object from
25* a binary file. Added writeToFile method which streams
26* RuleBasedCollator out to a binary file. The streamIn
27* and streamOut methods use istream and ostream objects
28* in binary mode.
29* 2/12/97 aliu Modified to use TableCollationData sub-object to
30* hold invariant data.
31* 2/13/97 aliu Moved several methods into this class from Collation.
32* Added a private RuleBasedCollator(Locale&) constructor,
33* to be used by Collator::createDefault(). General
34* clean up.
35* 2/20/97 helena Added clone, operator==, operator!=, operator=, and copy
36* constructor and getDynamicClassID.
37* 3/5/97 aliu Modified constructFromFile() to add parameter
38* specifying whether or not binary loading is to be
39* attempted. This is required for dynamic rule loading.
40* 05/07/97 helena Added memory allocation error detection.
41* 6/17/97 helena Added IDENTICAL strength for compare, changed getRules to
42* use MergeCollation::getPattern.
43* 6/20/97 helena Java class name change.
44* 8/18/97 helena Added internal API documentation.
45* 09/03/97 helena Added createCollationKeyValues().
46* 02/10/98 damiba Added compare with "length" parameter
47* 08/05/98 erm Synched with 1.2 version of RuleBasedCollator.java
48* 04/23/99 stephen Removed EDecompositionMode, merged with
49* Normalizer::EMode
50* 06/14/99 stephen Removed kResourceBundleSuffix
51* 11/02/99 helena Collator performance enhancements. Eliminates the
52* UnicodeString construction and special case for NO_OP.
53* 11/23/99 srl More performance enhancements. Updates to NormalizerIterator
54* internal state management.
55* 12/15/99 aliu Update to support Thai collation. Move NormalizerIterator
56* to implementation file.
57* 01/29/01 synwee Modified into a C++ wrapper which calls C API
58* (ucol.h)
59* 2012-2014 markus Rewritten in C++ again.
60*/
61
62#ifndef TBLCOLL_H
63#define TBLCOLL_H
64
65#include "unicode/utypes.h"
66
67#if U_SHOW_CPLUSPLUS_API1
68
69#if !UCONFIG_NO_COLLATION0
70
71#include "unicode/coll.h"
72#include "unicode/locid.h"
73#include "unicode/uiter.h"
74#include "unicode/ucol.h"
75
76U_NAMESPACE_BEGINnamespace icu_71 {
77
78struct CollationCacheEntry;
79struct CollationData;
80struct CollationSettings;
81struct CollationTailoring;
82/**
83* @stable ICU 2.0
84*/
85class StringSearch;
86/**
87* @stable ICU 2.0
88*/
89class CollationElementIterator;
90class CollationKey;
91class SortKeyByteSink;
92class UnicodeSet;
93class UnicodeString;
94class UVector64;
95
96/**
97 * The RuleBasedCollator class provides the implementation of
98 * Collator, using data-driven tables. The user can create a customized
99 * table-based collation.
100 * <p>
101 * For more information about the collation service see
102 * <a href="https://unicode-org.github.io/icu/userguide/collation">the User Guide</a>.
103 * <p>
104 * Collation service provides correct sorting orders for most locales supported in ICU.
105 * If specific data for a locale is not available, the orders eventually falls back
106 * to the <a href="http://www.unicode.org/reports/tr35/tr35-collation.html#Root_Collation">CLDR root sort order</a>.
107 * <p>
108 * Sort ordering may be customized by providing your own set of rules. For more on
109 * this subject see the <a href="https://unicode-org.github.io/icu/userguide/collation/customization">
110 * Collation Customization</a> section of the User Guide.
111 * <p>
112 * Note, RuleBasedCollator is not to be subclassed.
113 * @see Collator
114 */
115class U_I18N_API RuleBasedCollator U_FINALfinal : public Collator {
116public:
117 /**
118 * RuleBasedCollator constructor. This takes the table rules and builds a
119 * collation table out of them. Please see RuleBasedCollator class
120 * description for more details on the collation rule syntax.
121 * @param rules the collation rules to build the collation table from.
122 * @param status reporting a success or an error.
123 * @stable ICU 2.0
124 */
125 RuleBasedCollator(const UnicodeString& rules, UErrorCode& status);
126
127 /**
128 * RuleBasedCollator constructor. This takes the table rules and builds a
129 * collation table out of them. Please see RuleBasedCollator class
130 * description for more details on the collation rule syntax.
131 * @param rules the collation rules to build the collation table from.
132 * @param collationStrength strength for comparison
133 * @param status reporting a success or an error.
134 * @stable ICU 2.0
135 */
136 RuleBasedCollator(const UnicodeString& rules,
137 ECollationStrength collationStrength,
138 UErrorCode& status);
139
140 /**
141 * RuleBasedCollator constructor. This takes the table rules and builds a
142 * collation table out of them. Please see RuleBasedCollator class
143 * description for more details on the collation rule syntax.
144 * @param rules the collation rules to build the collation table from.
145 * @param decompositionMode the normalisation mode
146 * @param status reporting a success or an error.
147 * @stable ICU 2.0
148 */
149 RuleBasedCollator(const UnicodeString& rules,
150 UColAttributeValue decompositionMode,
151 UErrorCode& status);
152
153 /**
154 * RuleBasedCollator constructor. This takes the table rules and builds a
155 * collation table out of them. Please see RuleBasedCollator class
156 * description for more details on the collation rule syntax.
157 * @param rules the collation rules to build the collation table from.
158 * @param collationStrength strength for comparison
159 * @param decompositionMode the normalisation mode
160 * @param status reporting a success or an error.
161 * @stable ICU 2.0
162 */
163 RuleBasedCollator(const UnicodeString& rules,
164 ECollationStrength collationStrength,
165 UColAttributeValue decompositionMode,
166 UErrorCode& status);
167
168#ifndef U_HIDE_INTERNAL_API
169 /**
170 * TODO: document & propose as public API
171 * @internal
172 */
173 RuleBasedCollator(const UnicodeString &rules,
174 UParseError &parseError, UnicodeString &reason,
175 UErrorCode &errorCode);
176#endif /* U_HIDE_INTERNAL_API */
177
178 /**
179 * Copy constructor.
180 * @param other the RuleBasedCollator object to be copied
181 * @stable ICU 2.0
182 */
183 RuleBasedCollator(const RuleBasedCollator& other);
184
185
186 /** Opens a collator from a collator binary image created using
187 * cloneBinary. Binary image used in instantiation of the
188 * collator remains owned by the user and should stay around for
189 * the lifetime of the collator. The API also takes a base collator
190 * which must be the root collator.
191 * @param bin binary image owned by the user and required through the
192 * lifetime of the collator
193 * @param length size of the image. If negative, the API will try to
194 * figure out the length of the image
195 * @param base Base collator, for lookup of untailored characters.
196 * Must be the root collator, must not be NULL.
197 * The base is required to be present through the lifetime of the collator.
198 * @param status for catching errors
199 * @return newly created collator
200 * @see cloneBinary
201 * @stable ICU 3.4
202 */
203 RuleBasedCollator(const uint8_t *bin, int32_t length,
204 const RuleBasedCollator *base,
205 UErrorCode &status);
206
207 /**
208 * Destructor.
209 * @stable ICU 2.0
210 */
211 virtual ~RuleBasedCollator();
212
213 /**
214 * Assignment operator.
215 * @param other other RuleBasedCollator object to copy from.
216 * @stable ICU 2.0
217 */
218 RuleBasedCollator& operator=(const RuleBasedCollator& other);
219
220 /**
221 * Returns true if argument is the same as this object.
222 * @param other Collator object to be compared.
223 * @return true if arguments is the same as this object.
224 * @stable ICU 2.0
225 */
226 virtual bool operator==(const Collator& other) const override;
227
228 /**
229 * Makes a copy of this object.
230 * @return a copy of this object, owned by the caller
231 * @stable ICU 2.0
232 */
233 virtual RuleBasedCollator* clone() const override;
234
235 /**
236 * Creates a collation element iterator for the source string. The caller of
237 * this method is responsible for the memory management of the return
238 * pointer.
239 * @param source the string over which the CollationElementIterator will
240 * iterate.
241 * @return the collation element iterator of the source string using this as
242 * the based Collator.
243 * @stable ICU 2.2
244 */
245 virtual CollationElementIterator* createCollationElementIterator(
246 const UnicodeString& source) const;
247
248 /**
249 * Creates a collation element iterator for the source. The caller of this
250 * method is responsible for the memory management of the returned pointer.
251 * @param source the CharacterIterator which produces the characters over
252 * which the CollationElementItgerator will iterate.
253 * @return the collation element iterator of the source using this as the
254 * based Collator.
255 * @stable ICU 2.2
256 */
257 virtual CollationElementIterator* createCollationElementIterator(
258 const CharacterIterator& source) const;
259
260 // Make deprecated versions of Collator::compare() visible.
261 using Collator::compare;
262
263 /**
264 * The comparison function compares the character data stored in two
265 * different strings. Returns information about whether a string is less
266 * than, greater than or equal to another string.
267 * @param source the source string to be compared with.
268 * @param target the string that is to be compared with the source string.
269 * @param status possible error code
270 * @return Returns an enum value. UCOL_GREATER if source is greater
271 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
272 * than target
273 * @stable ICU 2.6
274 **/
275 virtual UCollationResult compare(const UnicodeString& source,
276 const UnicodeString& target,
277 UErrorCode &status) const override;
278
279 /**
280 * Does the same thing as compare but limits the comparison to a specified
281 * length
282 * @param source the source string to be compared with.
283 * @param target the string that is to be compared with the source string.
284 * @param length the length the comparison is limited to
285 * @param status possible error code
286 * @return Returns an enum value. UCOL_GREATER if source (up to the specified
287 * length) is greater than target; UCOL_EQUAL if source (up to specified
288 * length) is equal to target; UCOL_LESS if source (up to the specified
289 * length) is less than target.
290 * @stable ICU 2.6
291 */
292 virtual UCollationResult compare(const UnicodeString& source,
293 const UnicodeString& target,
294 int32_t length,
295 UErrorCode &status) const override;
296
297 /**
298 * The comparison function compares the character data stored in two
299 * different string arrays. Returns information about whether a string array
300 * is less than, greater than or equal to another string array.
301 * @param source the source string array to be compared with.
302 * @param sourceLength the length of the source string array. If this value
303 * is equal to -1, the string array is null-terminated.
304 * @param target the string that is to be compared with the source string.
305 * @param targetLength the length of the target string array. If this value
306 * is equal to -1, the string array is null-terminated.
307 * @param status possible error code
308 * @return Returns an enum value. UCOL_GREATER if source is greater
309 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
310 * than target
311 * @stable ICU 2.6
312 */
313 virtual UCollationResult compare(const char16_t* source, int32_t sourceLength,
314 const char16_t* target, int32_t targetLength,
315 UErrorCode &status) const override;
316
317 /**
318 * Compares two strings using the Collator.
319 * Returns whether the first one compares less than/equal to/greater than
320 * the second one.
321 * This version takes UCharIterator input.
322 * @param sIter the first ("source") string iterator
323 * @param tIter the second ("target") string iterator
324 * @param status ICU status
325 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
326 * @stable ICU 4.2
327 */
328 virtual UCollationResult compare(UCharIterator &sIter,
329 UCharIterator &tIter,
330 UErrorCode &status) const override;
331
332 /**
333 * Compares two UTF-8 strings using the Collator.
334 * Returns whether the first one compares less than/equal to/greater than
335 * the second one.
336 * This version takes UTF-8 input.
337 * Note that a StringPiece can be implicitly constructed
338 * from a std::string or a NUL-terminated const char * string.
339 * @param source the first UTF-8 string
340 * @param target the second UTF-8 string
341 * @param status ICU status
342 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
343 * @stable ICU 51
344 */
345 virtual UCollationResult compareUTF8(const StringPiece &source,
346 const StringPiece &target,
347 UErrorCode &status) const override;
348
349 /**
350 * Transforms the string into a series of characters
351 * that can be compared with CollationKey.compare().
352 *
353 * Note that sort keys are often less efficient than simply doing comparison.
354 * For more details, see the ICU User Guide.
355 *
356 * @param source the source string.
357 * @param key the transformed key of the source string.
358 * @param status the error code status.
359 * @return the transformed key.
360 * @see CollationKey
361 * @stable ICU 2.0
362 */
363 virtual CollationKey& getCollationKey(const UnicodeString& source,
364 CollationKey& key,
365 UErrorCode& status) const override;
366
367 /**
368 * Transforms a specified region of the string into a series of characters
369 * that can be compared with CollationKey.compare.
370 *
371 * Note that sort keys are often less efficient than simply doing comparison.
372 * For more details, see the ICU User Guide.
373 *
374 * @param source the source string.
375 * @param sourceLength the length of the source string.
376 * @param key the transformed key of the source string.
377 * @param status the error code status.
378 * @return the transformed key.
379 * @see CollationKey
380 * @stable ICU 2.0
381 */
382 virtual CollationKey& getCollationKey(const char16_t *source,
383 int32_t sourceLength,
384 CollationKey& key,
385 UErrorCode& status) const override;
386
387 /**
388 * Generates the hash code for the rule-based collation object.
389 * @return the hash code.
390 * @stable ICU 2.0
391 */
392 virtual int32_t hashCode() const override;
393
394#ifndef U_FORCE_HIDE_DEPRECATED_API
395 /**
396 * Gets the locale of the Collator
397 * @param type can be either requested, valid or actual locale. For more
398 * information see the definition of ULocDataLocaleType in
399 * uloc.h
400 * @param status the error code status.
401 * @return locale where the collation data lives. If the collator
402 * was instantiated from rules, locale is empty.
403 * @deprecated ICU 2.8 likely to change in ICU 3.0, based on feedback
404 */
405 virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const override;
406#endif // U_FORCE_HIDE_DEPRECATED_API
407
408 /**
409 * Gets the tailoring rules for this collator.
410 * @return the collation tailoring from which this collator was created
411 * @stable ICU 2.0
412 */
413 const UnicodeString& getRules() const;
414
415 /**
416 * Gets the version information for a Collator.
417 * @param info the version # information, the result will be filled in
418 * @stable ICU 2.0
419 */
420 virtual void getVersion(UVersionInfo info) const override;
421
422#ifndef U_HIDE_DEPRECATED_API
423 /**
424 * Returns the maximum length of any expansion sequences that end with the
425 * specified comparison order.
426 *
427 * This is specific to the kind of collation element values and sequences
428 * returned by the CollationElementIterator.
429 * Call CollationElementIterator::getMaxExpansion() instead.
430 *
431 * @param order a collation order returned by CollationElementIterator::previous
432 * or CollationElementIterator::next.
433 * @return maximum size of the expansion sequences ending with the collation
434 * element, or 1 if the collation element does not occur at the end of
435 * any expansion sequence
436 * @see CollationElementIterator#getMaxExpansion
437 * @deprecated ICU 51 Use CollationElementIterator::getMaxExpansion() instead.
438 */
439 int32_t getMaxExpansion(int32_t order) const;
440#endif /* U_HIDE_DEPRECATED_API */
441
442 /**
443 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
444 * method is to implement a simple version of RTTI, since not all C++
445 * compilers support genuine RTTI. Polymorphic operator==() and clone()
446 * methods call this method.
447 * @return The class ID for this object. All objects of a given class have
448 * the same class ID. Objects of other classes have different class
449 * IDs.
450 * @stable ICU 2.0
451 */
452 virtual UClassID getDynamicClassID(void) const override;
453
454 /**
455 * Returns the class ID for this class. This is useful only for comparing to
456 * a return value from getDynamicClassID(). For example:
457 * <pre>
458 * Base* polymorphic_pointer = createPolymorphicObject();
459 * if (polymorphic_pointer->getDynamicClassID() ==
460 * Derived::getStaticClassID()) ...
461 * </pre>
462 * @return The class ID for all objects of this class.
463 * @stable ICU 2.0
464 */
465 static UClassID U_EXPORT2 getStaticClassID(void);
466
467#ifndef U_HIDE_DEPRECATED_API
468 /**
469 * Do not use this method: The caller and the ICU library might use different heaps.
470 * Use cloneBinary() instead which writes to caller-provided memory.
471 *
472 * Returns a binary format of this collator.
473 * @param length Returns the length of the data, in bytes
474 * @param status the error code status.
475 * @return memory, owned by the caller, of size 'length' bytes.
476 * @deprecated ICU 52. Use cloneBinary() instead.
477 */
478 uint8_t *cloneRuleData(int32_t &length, UErrorCode &status) const;
479#endif /* U_HIDE_DEPRECATED_API */
480
481 /** Creates a binary image of a collator. This binary image can be stored and
482 * later used to instantiate a collator using ucol_openBinary.
483 * This API supports preflighting.
484 * @param buffer a fill-in buffer to receive the binary image
485 * @param capacity capacity of the destination buffer
486 * @param status for catching errors
487 * @return size of the image
488 * @see ucol_openBinary
489 * @stable ICU 3.4
490 */
491 int32_t cloneBinary(uint8_t *buffer, int32_t capacity, UErrorCode &status) const;
492
493 /**
494 * Returns current rules. Delta defines whether full rules are returned or
495 * just the tailoring.
496 *
497 * getRules(void) should normally be used instead.
498 * See https://unicode-org.github.io/icu/userguide/collation/customization#building-on-existing-locales
499 * @param delta one of UCOL_TAILORING_ONLY, UCOL_FULL_RULES.
500 * @param buffer UnicodeString to store the result rules
501 * @stable ICU 2.2
502 * @see UCOL_FULL_RULES
503 */
504 void getRules(UColRuleOption delta, UnicodeString &buffer) const;
505
506 /**
507 * Universal attribute setter
508 * @param attr attribute type
509 * @param value attribute value
510 * @param status to indicate whether the operation went on smoothly or there were errors
511 * @stable ICU 2.2
512 */
513 virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
514 UErrorCode &status) override;
515
516 /**
517 * Universal attribute getter.
518 * @param attr attribute type
519 * @param status to indicate whether the operation went on smoothly or there were errors
520 * @return attribute value
521 * @stable ICU 2.2
522 */
523 virtual UColAttributeValue getAttribute(UColAttribute attr,
524 UErrorCode &status) const override;
525
526 /**
527 * Sets the variable top to the top of the specified reordering group.
528 * The variable top determines the highest-sorting character
529 * which is affected by UCOL_ALTERNATE_HANDLING.
530 * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
531 * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
532 * UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
533 * or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
534 * @param errorCode Standard ICU error code. Its input value must
535 * pass the U_SUCCESS() test, or else the function returns
536 * immediately. Check for U_FAILURE() on output or use with
537 * function chaining. (See User Guide for details.)
538 * @return *this
539 * @see getMaxVariable
540 * @stable ICU 53
541 */
542 virtual Collator &setMaxVariable(UColReorderCode group, UErrorCode &errorCode) override;
543
544 /**
545 * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
546 * @return the maximum variable reordering group.
547 * @see setMaxVariable
548 * @stable ICU 53
549 */
550 virtual UColReorderCode getMaxVariable() const override;
551
552#ifndef U_FORCE_HIDE_DEPRECATED_API
553 /**
554 * Sets the variable top to the primary weight of the specified string.
555 *
556 * Beginning with ICU 53, the variable top is pinned to
557 * the top of one of the supported reordering groups,
558 * and it must not be beyond the last of those groups.
559 * See setMaxVariable().
560 * @param varTop one or more (if contraction) char16_ts to which the variable top should be set
561 * @param len length of variable top string. If -1 it is considered to be zero terminated.
562 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
563 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
564 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
565 * the last reordering group supported by setMaxVariable()
566 * @return variable top primary weight
567 * @deprecated ICU 53 Call setMaxVariable() instead.
568 */
569 virtual uint32_t setVariableTop(const char16_t *varTop, int32_t len, UErrorCode &status) override;
570
571 /**
572 * Sets the variable top to the primary weight of the specified string.
573 *
574 * Beginning with ICU 53, the variable top is pinned to
575 * the top of one of the supported reordering groups,
576 * and it must not be beyond the last of those groups.
577 * See setMaxVariable().
578 * @param varTop a UnicodeString size 1 or more (if contraction) of char16_ts to which the variable top should be set
579 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
580 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
581 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
582 * the last reordering group supported by setMaxVariable()
583 * @return variable top primary weight
584 * @deprecated ICU 53 Call setMaxVariable() instead.
585 */
586 virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status) override;
587
588 /**
589 * Sets the variable top to the specified primary weight.
590 *
591 * Beginning with ICU 53, the variable top is pinned to
592 * the top of one of the supported reordering groups,
593 * and it must not be beyond the last of those groups.
594 * See setMaxVariable().
595 * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
596 * @param status error code
597 * @deprecated ICU 53 Call setMaxVariable() instead.
598 */
599 virtual void setVariableTop(uint32_t varTop, UErrorCode &status) override;
600#endif // U_FORCE_HIDE_DEPRECATED_API
601
602 /**
603 * Gets the variable top value of a Collator.
604 * @param status error code (not changed by function). If error code is set, the return value is undefined.
605 * @return the variable top primary weight
606 * @see getMaxVariable
607 * @stable ICU 2.0
608 */
609 virtual uint32_t getVariableTop(UErrorCode &status) const override;
610
611 /**
612 * Get a UnicodeSet that contains all the characters and sequences tailored in
613 * this collator.
614 * @param status error code of the operation
615 * @return a pointer to a UnicodeSet object containing all the
616 * code points and sequences that may sort differently than
617 * in the root collator. The object must be disposed of by using delete
618 * @stable ICU 2.4
619 */
620 virtual UnicodeSet *getTailoredSet(UErrorCode &status) const override;
621
622 /**
623 * Get the sort key as an array of bytes from a UnicodeString.
624 *
625 * Note that sort keys are often less efficient than simply doing comparison.
626 * For more details, see the ICU User Guide.
627 *
628 * @param source string to be processed.
629 * @param result buffer to store result in. If NULL, number of bytes needed
630 * will be returned.
631 * @param resultLength length of the result buffer. If if not enough the
632 * buffer will be filled to capacity.
633 * @return Number of bytes needed for storing the sort key
634 * @stable ICU 2.0
635 */
636 virtual int32_t getSortKey(const UnicodeString& source, uint8_t *result,
637 int32_t resultLength) const override;
638
639 /**
640 * Get the sort key as an array of bytes from a char16_t buffer.
641 *
642 * Note that sort keys are often less efficient than simply doing comparison.
643 * For more details, see the ICU User Guide.
644 *
645 * @param source string to be processed.
646 * @param sourceLength length of string to be processed. If -1, the string
647 * is 0 terminated and length will be decided by the function.
648 * @param result buffer to store result in. If NULL, number of bytes needed
649 * will be returned.
650 * @param resultLength length of the result buffer. If if not enough the
651 * buffer will be filled to capacity.
652 * @return Number of bytes needed for storing the sort key
653 * @stable ICU 2.2
654 */
655 virtual int32_t getSortKey(const char16_t *source, int32_t sourceLength,
656 uint8_t *result, int32_t resultLength) const override;
657
658 /**
659 * Retrieves the reordering codes for this collator.
660 * @param dest The array to fill with the script ordering.
661 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the function
662 * will only return the length of the result without writing any codes (pre-flighting).
663 * @param status A reference to an error code value, which must not indicate
664 * a failure before the function call.
665 * @return The length of the script ordering array.
666 * @see ucol_setReorderCodes
667 * @see Collator#getEquivalentReorderCodes
668 * @see Collator#setReorderCodes
669 * @stable ICU 4.8
670 */
671 virtual int32_t getReorderCodes(int32_t *dest,
672 int32_t destCapacity,
673 UErrorCode& status) const override;
674
675 /**
676 * Sets the ordering of scripts for this collator.
677 * @param reorderCodes An array of script codes in the new order. This can be NULL if the
678 * length is also set to 0. An empty array will clear any reordering codes on the collator.
679 * @param reorderCodesLength The length of reorderCodes.
680 * @param status error code
681 * @see ucol_setReorderCodes
682 * @see Collator#getReorderCodes
683 * @see Collator#getEquivalentReorderCodes
684 * @stable ICU 4.8
685 */
686 virtual void setReorderCodes(const int32_t* reorderCodes,
687 int32_t reorderCodesLength,
688 UErrorCode& status) override;
689
690 /**
691 * Implements ucol_strcollUTF8().
692 * @internal
693 */
694 virtual UCollationResult internalCompareUTF8(
695 const char *left, int32_t leftLength,
696 const char *right, int32_t rightLength,
697 UErrorCode &errorCode) const override;
698
699 /** Get the short definition string for a collator. This internal API harvests the collator's
700 * locale and the attribute set and produces a string that can be used for opening
701 * a collator with the same attributes using the ucol_openFromShortString API.
702 * This string will be normalized.
703 * The structure and the syntax of the string is defined in the "Naming collators"
704 * section of the users guide:
705 * https://unicode-org.github.io/icu/userguide/collation/concepts#collator-naming-scheme
706 * This function supports preflighting.
707 *
708 * This is internal, and intended to be used with delegate converters.
709 *
710 * @param locale a locale that will appear as a collators locale in the resulting
711 * short string definition. If NULL, the locale will be harvested
712 * from the collator.
713 * @param buffer space to hold the resulting string
714 * @param capacity capacity of the buffer
715 * @param status for returning errors. All the preflighting errors are featured
716 * @return length of the resulting string
717 * @see ucol_openFromShortString
718 * @see ucol_normalizeShortDefinitionString
719 * @see ucol_getShortDefinitionString
720 * @internal
721 */
722 virtual int32_t internalGetShortDefinitionString(const char *locale,
723 char *buffer,
724 int32_t capacity,
725 UErrorCode &status) const override;
726
727 /**
728 * Implements ucol_nextSortKeyPart().
729 * @internal
730 */
731 virtual int32_t internalNextSortKeyPart(
732 UCharIterator *iter, uint32_t state[2],
733 uint8_t *dest, int32_t count, UErrorCode &errorCode) const override;
734
735 // Do not enclose the default constructor with #ifndef U_HIDE_INTERNAL_API
736 /**
737 * Only for use in ucol_openRules().
738 * @internal
739 */
740 RuleBasedCollator();
741
742#ifndef U_HIDE_INTERNAL_API
743 /**
744 * Implements ucol_getLocaleByType().
745 * Needed because the lifetime of the locale ID string must match that of the collator.
746 * getLocale() returns a copy of a Locale, with minimal lifetime in a C wrapper.
747 * @internal
748 */
749 const char *internalGetLocaleID(ULocDataLocaleType type, UErrorCode &errorCode) const;
750
751 /**
752 * Implements ucol_getContractionsAndExpansions().
753 * Gets this collator's sets of contraction strings and/or
754 * characters and strings that map to multiple collation elements (expansions).
755 * If addPrefixes is true, then contractions that are expressed as
756 * prefix/pre-context rules are included.
757 * @param contractions if not NULL, the set to hold the contractions
758 * @param expansions if not NULL, the set to hold the expansions
759 * @param addPrefixes include prefix contextual mappings
760 * @param errorCode in/out ICU error code
761 * @internal
762 */
763 void internalGetContractionsAndExpansions(
764 UnicodeSet *contractions, UnicodeSet *expansions,
765 UBool addPrefixes, UErrorCode &errorCode) const;
766
767 /**
768 * Adds the contractions that start with character c to the set.
769 * Ignores prefixes. Used by AlphabeticIndex.
770 * @internal
771 */
772 void internalAddContractions(UChar32 c, UnicodeSet &set, UErrorCode &errorCode) const;
773
774 /**
775 * Implements from-rule constructors, and ucol_openRules().
776 * @internal
777 */
778 void internalBuildTailoring(
779 const UnicodeString &rules,
780 int32_t strength,
781 UColAttributeValue decompositionMode,
782 UParseError *outParseError, UnicodeString *outReason,
783 UErrorCode &errorCode);
784
785 /** @internal */
786 static inline RuleBasedCollator *rbcFromUCollator(UCollator *uc) {
787 return dynamic_cast<RuleBasedCollator *>(fromUCollator(uc));
788 }
789 /** @internal */
790 static inline const RuleBasedCollator *rbcFromUCollator(const UCollator *uc) {
791 return dynamic_cast<const RuleBasedCollator *>(fromUCollator(uc));
4
Passing value via 1st parameter 'uc'
5
Returning pointer
792 }
793
794 /**
795 * Appends the CEs for the string to the vector.
796 * @internal for tests & tools
797 */
798 void internalGetCEs(const UnicodeString &str, UVector64 &ces, UErrorCode &errorCode) const;
799#endif // U_HIDE_INTERNAL_API
800
801protected:
802 /**
803 * Used internally by registration to define the requested and valid locales.
804 * @param requestedLocale the requested locale
805 * @param validLocale the valid locale
806 * @param actualLocale the actual locale
807 * @internal
808 */
809 virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale) override;
810
811private:
812 friend class CollationElementIterator;
813 friend class Collator;
814
815 RuleBasedCollator(const CollationCacheEntry *entry);
816
817 /**
818 * Enumeration of attributes that are relevant for short definition strings
819 * (e.g., ucol_getShortDefinitionString()).
820 * Effectively extends UColAttribute.
821 */
822 enum Attributes {
823 ATTR_VARIABLE_TOP = UCOL_ATTRIBUTE_COUNT,
824 ATTR_LIMIT
825 };
826
827 void adoptTailoring(CollationTailoring *t, UErrorCode &errorCode);
828
829 // Both lengths must be <0 or else both must be >=0.
830 UCollationResult doCompare(const char16_t *left, int32_t leftLength,
831 const char16_t *right, int32_t rightLength,
832 UErrorCode &errorCode) const;
833 UCollationResult doCompare(const uint8_t *left, int32_t leftLength,
834 const uint8_t *right, int32_t rightLength,
835 UErrorCode &errorCode) const;
836
837 void writeSortKey(const char16_t *s, int32_t length,
838 SortKeyByteSink &sink, UErrorCode &errorCode) const;
839
840 void writeIdenticalLevel(const char16_t *s, const char16_t *limit,
841 SortKeyByteSink &sink, UErrorCode &errorCode) const;
842
843 const CollationSettings &getDefaultSettings() const;
844
845 void setAttributeDefault(int32_t attribute) {
846 explicitlySetAttributes &= ~((uint32_t)1 << attribute);
847 }
848 void setAttributeExplicitly(int32_t attribute) {
849 explicitlySetAttributes |= (uint32_t)1 << attribute;
850 }
851 UBool attributeHasBeenSetExplicitly(int32_t attribute) const {
852 // assert(0 <= attribute < ATTR_LIMIT);
853 return (UBool)((explicitlySetAttributes & ((uint32_t)1 << attribute)) != 0);
854 }
855
856 /**
857 * Tests whether a character is "unsafe" for use as a collation starting point.
858 *
859 * @param c code point or code unit
860 * @return true if c is unsafe
861 * @see CollationElementIterator#setOffset(int)
862 */
863 UBool isUnsafe(UChar32 c) const;
864
865 static void U_CALLCONV computeMaxExpansions(const CollationTailoring *t, UErrorCode &errorCode);
866 UBool initMaxExpansions(UErrorCode &errorCode) const;
867
868 void setFastLatinOptions(CollationSettings &ownedSettings) const;
869
870 const CollationData *data;
871 const CollationSettings *settings; // reference-counted
872 const CollationTailoring *tailoring; // alias of cacheEntry->tailoring
873 const CollationCacheEntry *cacheEntry; // reference-counted
874 Locale validLocale;
875 uint32_t explicitlySetAttributes;
876
877 UBool actualLocaleIsSameAsValid;
878};
879
880U_NAMESPACE_END}
881
882#endif // !UCONFIG_NO_COLLATION
883
884#endif /* U_SHOW_CPLUSPLUS_API */
885
886#endif // TBLCOLL_H