2 *******************************************************************************
4 * Copyright (C) 2002-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2002mar07
14 * created by: Markus W. Scherer
16 * The serialized structure, the array of range limits, is
17 * the same as in UnicodeSet, except that the HIGH value is not stored.
19 * There are functions to efficiently serialize a USet into an array of uint16_t
20 * and functions to use such a serialized form efficiently without
21 * instantiating a new USet.
24 #include "unicode/utypes.h"
25 #include "unicode/uobject.h"
26 #include "unicode/uset.h"
27 #include "unicode/uniset.h"
29 #include "unicode/ustring.h"
30 #include "unicode/parsepos.h"
32 U_CAPI USet
* U_EXPORT2
33 uset_open(UChar32 start
, UChar32 end
) {
34 return (USet
*) new UnicodeSet(start
, end
);
38 uset_close(USet
* set
) {
39 delete (UnicodeSet
*) set
;
44 UChar32 start
, UChar32 end
) {
45 ((UnicodeSet
*) set
)->set(start
, end
);
49 uset_addAll(USet
* set
, const USet
*additionalSet
) {
50 ((UnicodeSet
*) set
)->addAll(*((const UnicodeSet
*)additionalSet
));
54 uset_add(USet
* set
, UChar32 c
) {
55 ((UnicodeSet
*) set
)->add(c
);
59 uset_addRange(USet
* set
, UChar32 start
, UChar32 end
) {
60 ((UnicodeSet
*) set
)->add(start
, end
);
64 uset_addString(USet
* set
, const UChar
* str
, int32_t strLen
) {
65 // WRONG! Do not alias, it will stay aliased, even after
66 // copying. TODO: do we need a copy ctor that unaliases
67 //UnicodeString s(strLen==-1, str, strLen);
68 // We promised -1 for zero terminated
70 strLen
= u_strlen(str
);
72 UnicodeString
s(str
, strLen
);
73 ((UnicodeSet
*) set
)->add(s
);
77 uset_remove(USet
* set
, UChar32 c
) {
78 ((UnicodeSet
*) set
)->remove(c
);
82 uset_removeRange(USet
* set
, UChar32 start
, UChar32 end
) {
83 ((UnicodeSet
*) set
)->remove(start
, end
);
87 uset_removeString(USet
* set
, const UChar
* str
, int32_t strLen
) {
88 UnicodeString
s(strLen
==-1, str
, strLen
);
89 ((UnicodeSet
*) set
)->remove(s
);
93 uset_removeAll(USet
* set
, const USet
* remove
) {
94 ((UnicodeSet
*) set
)->removeAll(*(const UnicodeSet
*)remove
);
98 uset_retain(USet
* set
, UChar32 start
, UChar32 end
) {
99 ((UnicodeSet
*) set
)->retain(start
, end
);
102 U_CAPI
void U_EXPORT2
103 uset_retainAll(USet
* set
, const USet
* retain
) {
104 ((UnicodeSet
*) set
)->retainAll(*(const UnicodeSet
*)retain
);
107 U_CAPI
void U_EXPORT2
108 uset_compact(USet
* set
) {
109 ((UnicodeSet
*) set
)->compact();
112 U_CAPI
void U_EXPORT2
113 uset_complement(USet
* set
) {
114 ((UnicodeSet
*) set
)->complement();
117 U_CAPI
void U_EXPORT2
118 uset_complementAll(USet
* set
, const USet
* complement
) {
119 ((UnicodeSet
*) set
)->complementAll(*(const UnicodeSet
*)complement
);
122 U_CAPI
void U_EXPORT2
123 uset_clear(USet
* set
) {
124 ((UnicodeSet
*) set
)->clear();
127 U_CAPI UBool U_EXPORT2
128 uset_isEmpty(const USet
* set
) {
129 return ((const UnicodeSet
*) set
)->isEmpty();
132 U_CAPI UBool U_EXPORT2
133 uset_contains(const USet
* set
, UChar32 c
) {
134 return ((const UnicodeSet
*) set
)->contains(c
);
137 U_CAPI UBool U_EXPORT2
138 uset_containsRange(const USet
* set
, UChar32 start
, UChar32 end
) {
139 return ((const UnicodeSet
*) set
)->contains(start
, end
);
142 U_CAPI UBool U_EXPORT2
143 uset_containsString(const USet
* set
, const UChar
* str
, int32_t strLen
) {
144 UnicodeString
s(strLen
==-1, str
, strLen
);
145 return ((const UnicodeSet
*) set
)->contains(s
);
148 U_CAPI UBool U_EXPORT2
149 uset_containsAll(const USet
* set1
, const USet
* set2
) {
150 return ((const UnicodeSet
*) set1
)->containsAll(* (const UnicodeSet
*) set2
);
153 U_CAPI UBool U_EXPORT2
154 uset_containsNone(const USet
* set1
, const USet
* set2
) {
155 return ((const UnicodeSet
*) set1
)->containsNone(* (const UnicodeSet
*) set2
);
158 U_CAPI UBool U_EXPORT2
159 uset_containsSome(const USet
* set1
, const USet
* set2
) {
160 return ((const UnicodeSet
*) set1
)->containsSome(* (const UnicodeSet
*) set2
);
163 U_CAPI UBool U_EXPORT2
164 uset_equals(const USet
* set1
, const USet
* set2
) {
165 return *(const UnicodeSet
*)set1
== *(const UnicodeSet
*)set2
;
168 U_CAPI
int32_t U_EXPORT2
169 uset_indexOf(const USet
* set
, UChar32 c
) {
170 return ((UnicodeSet
*) set
)->indexOf(c
);
173 U_CAPI UChar32 U_EXPORT2
174 uset_charAt(const USet
* set
, int32_t index
) {
175 return ((UnicodeSet
*) set
)->charAt(index
);
178 U_CAPI
int32_t U_EXPORT2
179 uset_size(const USet
* set
) {
180 return ((const UnicodeSet
*) set
)->size();
185 * This class only exists to provide access to the UnicodeSet private
186 * USet support API. Declaring a class a friend is more portable than
187 * trying to declare extern "C" functions as friends.
189 class USetAccess
/* not : public UObject because all methods are static */ {
191 /* Try to have the compiler inline these*/
192 inline static int32_t getStringCount(const UnicodeSet
& set
) {
193 return set
.getStringCount();
195 inline static const UnicodeString
* getString(const UnicodeSet
& set
,
197 return set
.getString(i
);
200 /* do not instantiate*/
205 U_CAPI
int32_t U_EXPORT2
206 uset_getItemCount(const USet
* uset
) {
207 const UnicodeSet
& set
= *(const UnicodeSet
*)uset
;
208 return set
.getRangeCount() + USetAccess::getStringCount(set
);
211 U_CAPI
int32_t U_EXPORT2
212 uset_getItem(const USet
* uset
, int32_t itemIndex
,
213 UChar32
* start
, UChar32
* end
,
214 UChar
* str
, int32_t strCapacity
,
216 if (U_FAILURE(*ec
)) return 0;
217 const UnicodeSet
& set
= *(const UnicodeSet
*)uset
;
221 *ec
= U_ILLEGAL_ARGUMENT_ERROR
;
223 } else if (itemIndex
< (rangeCount
= set
.getRangeCount())) {
224 *start
= set
.getRangeStart(itemIndex
);
225 *end
= set
.getRangeEnd(itemIndex
);
228 itemIndex
-= rangeCount
;
229 if (itemIndex
< USetAccess::getStringCount(set
)) {
230 const UnicodeString
* s
= USetAccess::getString(set
, itemIndex
);
231 return s
->extract(str
, strCapacity
, *ec
);
233 *ec
= U_INDEX_OUTOFBOUNDS_ERROR
;
239 //U_CAPI int32_t U_EXPORT2
240 //uset_getRangeCount(const USet* set) {
241 // return ((const UnicodeSet*) set)->getRangeCount();
244 //U_CAPI UBool U_EXPORT2
245 //uset_getRange(const USet* set, int32_t rangeIndex,
246 // UChar32* pStart, UChar32* pEnd) {
247 // if ((uint32_t) rangeIndex >= (uint32_t) uset_getRangeCount(set)) {
250 // const UnicodeSet* us = (const UnicodeSet*) set;
251 // *pStart = us->getRangeStart(rangeIndex);
252 // *pEnd = us->getRangeEnd(rangeIndex);
257 * Serialize a USet into 16-bit units.
258 * Store BMP code points as themselves with one 16-bit unit each.
260 * Important: the code points in the array are in ascending order,
261 * therefore all BMP code points precede all supplementary code points.
263 * Store each supplementary code point in 2 16-bit units,
264 * simply with higher-then-lower 16-bit halfs.
266 * Precede the entire list with the length.
267 * If there are supplementary code points, then set bit 15 in the length
268 * and add the bmpLength between it and the array.
271 * - all BMP: (length=bmpLength) BMP, .., BMP
272 * - some supplementary: (length|0x8000) (bmpLength<length) BMP, .., BMP, supp-high, supp-low, ..
274 U_CAPI
int32_t U_EXPORT2
275 uset_serialize(const USet
* set
, uint16_t* dest
, int32_t destCapacity
, UErrorCode
* ec
) {
276 if (ec
==NULL
|| U_FAILURE(*ec
)) {
280 return ((const UnicodeSet
*) set
)->serialize(dest
, destCapacity
,* ec
);
283 U_CAPI UBool U_EXPORT2
284 uset_getSerializedSet(USerializedSet
* fillSet
, const uint16_t* src
, int32_t srcLength
) {
290 if(src
==NULL
|| srcLength
<=0) {
291 fillSet
->length
=fillSet
->bmpLength
=0;
297 /* there are supplementary values */
299 if(srcLength
<(2+length
)) {
300 fillSet
->length
=fillSet
->bmpLength
=0;
303 fillSet
->bmpLength
=*src
++;
305 /* only BMP values */
306 if(srcLength
<(1+length
)) {
307 fillSet
->length
=fillSet
->bmpLength
=0;
310 fillSet
->bmpLength
=length
;
313 fillSet
->length
=length
;
317 U_CAPI
void U_EXPORT2
318 uset_setSerializedToOne(USerializedSet
* fillSet
, UChar32 c
) {
319 if(fillSet
==NULL
|| (uint32_t)c
>0x10ffff) {
323 fillSet
->array
=fillSet
->staticArray
;
325 fillSet
->bmpLength
=fillSet
->length
=2;
326 fillSet
->staticArray
[0]=(uint16_t)c
;
327 fillSet
->staticArray
[1]=(uint16_t)c
+1;
328 } else if(c
==0xffff) {
329 fillSet
->bmpLength
=1;
331 fillSet
->staticArray
[0]=0xffff;
332 fillSet
->staticArray
[1]=1;
333 fillSet
->staticArray
[2]=0;
334 } else if(c
<0x10ffff) {
335 fillSet
->bmpLength
=0;
337 fillSet
->staticArray
[0]=(uint16_t)(c
>>16);
338 fillSet
->staticArray
[1]=(uint16_t)c
;
340 fillSet
->staticArray
[2]=(uint16_t)(c
>>16);
341 fillSet
->staticArray
[3]=(uint16_t)c
;
342 } else /* c==0x10ffff */ {
343 fillSet
->bmpLength
=0;
345 fillSet
->staticArray
[0]=0x10;
346 fillSet
->staticArray
[1]=0xffff;
350 U_CAPI UBool U_EXPORT2
351 uset_serializedContains(const USerializedSet
* set
, UChar32 c
) {
352 const uint16_t* array
;
354 if(set
==NULL
|| (uint32_t)c
>0x10ffff) {
360 /* find c in the BMP part */
361 int32_t i
, bmpLength
=set
->bmpLength
;
362 for(i
=0; i
<bmpLength
&& (uint16_t)c
>=array
[i
]; ++i
) {}
365 /* find c in the supplementary part */
366 int32_t i
, length
=set
->length
;
367 uint16_t high
=(uint16_t)(c
>>16), low
=(uint16_t)c
;
368 for(i
=set
->bmpLength
;
369 i
<length
&& (high
>array
[i
] || (high
==array
[i
] && low
>=array
[i
+1]));
372 /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
373 return (UBool
)(((i
+set
->bmpLength
)&2)!=0);
377 U_CAPI
int32_t U_EXPORT2
378 uset_getSerializedRangeCount(const USerializedSet
* set
) {
383 return (set
->bmpLength
+(set
->length
-set
->bmpLength
)/2+1)/2;
386 U_CAPI UBool U_EXPORT2
387 uset_getSerializedRange(const USerializedSet
* set
, int32_t rangeIndex
,
388 UChar32
* pStart
, UChar32
* pEnd
) {
389 const uint16_t* array
;
390 int32_t bmpLength
, length
;
392 if(set
==NULL
|| rangeIndex
<0 || pStart
==NULL
|| pEnd
==NULL
) {
398 bmpLength
=set
->bmpLength
;
400 rangeIndex
*=2; /* address start/limit pairs */
401 if(rangeIndex
<bmpLength
) {
402 *pStart
=array
[rangeIndex
++];
403 if(rangeIndex
<bmpLength
) {
404 *pEnd
=array
[rangeIndex
];
405 } else if(rangeIndex
<length
) {
406 *pEnd
=(((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1];
413 rangeIndex
-=bmpLength
;
414 rangeIndex
*=2; /* address pairs of pairs of units */
416 if(rangeIndex
<length
) {
418 *pStart
=(((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1];
420 if(rangeIndex
<length
) {
421 *pEnd
=(((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1];
433 // TODO The old, internal uset.c had an efficient uset_containsOne function.
434 // Returned the one and only code point, or else -1 or something.
435 // Consider adding such a function to both C and C++ UnicodeSet/uset.
436 // See tools/gennorm/store.c for usage, now usetContainsOne there.
438 // TODO Investigate incorporating this code into UnicodeSet to improve
441 // #define USET_GROW_DELTA 20
443 // static U_INLINE int32_t
444 // findChar(const UChar32* array, int32_t length, UChar32 c) {
447 // /* check the last range limit first for more efficient appending */
449 // if(c>=array[length-1]) {
453 // /* do not check the last range limit again in the loop below */
457 // for(i=0; i<length && c>=array[i]; ++i) {}
462 // addRemove(USet* set, UChar32 c, int32_t doRemove) {
463 // int32_t i, length, more;
465 // if(set==NULL || (uint32_t)c>0x10ffff) {
469 // length=set->length;
470 // i=findChar(set->array, length, c);
471 // if((i&1)^doRemove) {
472 // /* c is already in the set */
476 // /* how many more array items do we need? */
477 // if(i<length && (c+1)==set->array[i]) {
478 // /* c is just before the following range, extend that in-place by one */
482 // if(c==set->array[i]) {
483 // /* the previous range collapsed, remove it */
484 // set->length=length-=2;
486 // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
491 // } else if(i>0 && c==set->array[i-1]) {
492 // /* c is just after the previous range, extend that in-place by one */
493 // if(++c<=0x10ffff) {
494 // set->array[i-1]=c;
495 // if(i<length && c==set->array[i]) {
496 // /* the following range collapsed, remove it */
498 // set->length=length-=2;
500 // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
504 // /* extend the previous range (had limit 0x10ffff) to the end of Unicode */
508 // } else if(i==length && c==0x10ffff) {
509 // /* insert one range limit c */
512 // /* insert two range limits c, c+1 */
516 // /* insert <more> range limits */
517 // if(length+more>set->capacity) {
519 // int32_t newCapacity=set->capacity+set->capacity/2+USET_GROW_DELTA;
520 // UChar32* newArray=(UChar32* )uprv_malloc(newCapacity*4);
521 // if(newArray==NULL) {
524 // set->capacity=newCapacity;
525 // uprv_memcpy(newArray, set->array, length*4);
527 // if(set->array!=set->staticBuffer) {
528 // uprv_free(set->array);
530 // set->array=newArray;
534 // uprv_memmove(set->array+i+more, set->array+i, (length-i)*4);
538 // set->array[i+1]=c+1;
540 // set->length+=more;
545 // U_CAPI UBool U_EXPORT2
546 // uset_add(USet* set, UChar32 c) {
547 // return addRemove(set, c, 0);
550 // U_CAPI void U_EXPORT2
551 // uset_remove(USet* set, UChar32 c) {
552 // addRemove(set, c, 1);