2 *******************************************************************************
4 * Copyright (C) 2002-2011, 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 * There are functions to efficiently serialize a USet into an array of uint16_t
17 * and functions to use such a serialized form efficiently without
18 * instantiating a new USet.
21 #include "unicode/utypes.h"
22 #include "unicode/uobject.h"
23 #include "unicode/uset.h"
24 #include "unicode/uniset.h"
26 #include "unicode/ustring.h"
27 #include "unicode/parsepos.h"
31 U_CAPI USet
* U_EXPORT2
33 return (USet
*) new UnicodeSet();
36 U_CAPI USet
* U_EXPORT2
37 uset_open(UChar32 start
, UChar32 end
) {
38 return (USet
*) new UnicodeSet(start
, end
);
42 uset_close(USet
* set
) {
43 delete (UnicodeSet
*) set
;
46 U_CAPI USet
* U_EXPORT2
47 uset_clone(const USet
*set
) {
48 return (USet
*) (((UnicodeSet
*) set
)->UnicodeSet::clone());
51 U_CAPI UBool U_EXPORT2
52 uset_isFrozen(const USet
*set
) {
53 return ((UnicodeSet
*) set
)->UnicodeSet::isFrozen();
57 uset_freeze(USet
*set
) {
58 ((UnicodeSet
*) set
)->UnicodeSet::freeze();
61 U_CAPI USet
* U_EXPORT2
62 uset_cloneAsThawed(const USet
*set
) {
63 return (USet
*) (((UnicodeSet
*) set
)->UnicodeSet::cloneAsThawed());
68 UChar32 start
, UChar32 end
) {
69 ((UnicodeSet
*) set
)->UnicodeSet::set(start
, end
);
73 uset_addAll(USet
* set
, const USet
*additionalSet
) {
74 ((UnicodeSet
*) set
)->UnicodeSet::addAll(*((const UnicodeSet
*)additionalSet
));
78 uset_add(USet
* set
, UChar32 c
) {
79 ((UnicodeSet
*) set
)->UnicodeSet::add(c
);
83 uset_addRange(USet
* set
, UChar32 start
, UChar32 end
) {
84 ((UnicodeSet
*) set
)->UnicodeSet::add(start
, end
);
88 uset_addString(USet
* set
, const UChar
* str
, int32_t strLen
) {
89 // UnicodeString handles -1 for strLen
90 UnicodeString
s(strLen
<0, str
, strLen
);
91 ((UnicodeSet
*) set
)->UnicodeSet::add(s
);
95 uset_addAllCodePoints(USet
* set
, const UChar
*str
, int32_t strLen
) {
96 // UnicodeString handles -1 for strLen
97 UnicodeString
s(str
, strLen
);
98 ((UnicodeSet
*) set
)->UnicodeSet::addAll(s
);
101 U_CAPI
void U_EXPORT2
102 uset_remove(USet
* set
, UChar32 c
) {
103 ((UnicodeSet
*) set
)->UnicodeSet::remove(c
);
106 U_CAPI
void U_EXPORT2
107 uset_removeRange(USet
* set
, UChar32 start
, UChar32 end
) {
108 ((UnicodeSet
*) set
)->UnicodeSet::remove(start
, end
);
111 U_CAPI
void U_EXPORT2
112 uset_removeString(USet
* set
, const UChar
* str
, int32_t strLen
) {
113 UnicodeString
s(strLen
==-1, str
, strLen
);
114 ((UnicodeSet
*) set
)->UnicodeSet::remove(s
);
117 U_CAPI
void U_EXPORT2
118 uset_removeAll(USet
* set
, const USet
* remove
) {
119 ((UnicodeSet
*) set
)->UnicodeSet::removeAll(*(const UnicodeSet
*)remove
);
122 U_CAPI
void U_EXPORT2
123 uset_retain(USet
* set
, UChar32 start
, UChar32 end
) {
124 ((UnicodeSet
*) set
)->UnicodeSet::retain(start
, end
);
127 U_CAPI
void U_EXPORT2
128 uset_retainAll(USet
* set
, const USet
* retain
) {
129 ((UnicodeSet
*) set
)->UnicodeSet::retainAll(*(const UnicodeSet
*)retain
);
132 U_CAPI
void U_EXPORT2
133 uset_compact(USet
* set
) {
134 ((UnicodeSet
*) set
)->UnicodeSet::compact();
137 U_CAPI
void U_EXPORT2
138 uset_complement(USet
* set
) {
139 ((UnicodeSet
*) set
)->UnicodeSet::complement();
142 U_CAPI
void U_EXPORT2
143 uset_complementAll(USet
* set
, const USet
* complement
) {
144 ((UnicodeSet
*) set
)->UnicodeSet::complementAll(*(const UnicodeSet
*)complement
);
147 U_CAPI
void U_EXPORT2
148 uset_clear(USet
* set
) {
149 ((UnicodeSet
*) set
)->UnicodeSet::clear();
152 U_CAPI
void U_EXPORT2
153 uset_removeAllStrings(USet
* set
) {
154 ((UnicodeSet
*) set
)->UnicodeSet::removeAllStrings();
157 U_CAPI UBool U_EXPORT2
158 uset_isEmpty(const USet
* set
) {
159 return ((const UnicodeSet
*) set
)->UnicodeSet::isEmpty();
162 U_CAPI UBool U_EXPORT2
163 uset_contains(const USet
* set
, UChar32 c
) {
164 return ((const UnicodeSet
*) set
)->UnicodeSet::contains(c
);
167 U_CAPI UBool U_EXPORT2
168 uset_containsRange(const USet
* set
, UChar32 start
, UChar32 end
) {
169 return ((const UnicodeSet
*) set
)->UnicodeSet::contains(start
, end
);
172 U_CAPI UBool U_EXPORT2
173 uset_containsString(const USet
* set
, const UChar
* str
, int32_t strLen
) {
174 UnicodeString
s(strLen
==-1, str
, strLen
);
175 return ((const UnicodeSet
*) set
)->UnicodeSet::contains(s
);
178 U_CAPI UBool U_EXPORT2
179 uset_containsAll(const USet
* set1
, const USet
* set2
) {
180 return ((const UnicodeSet
*) set1
)->UnicodeSet::containsAll(* (const UnicodeSet
*) set2
);
183 U_CAPI UBool U_EXPORT2
184 uset_containsAllCodePoints(const USet
* set
, const UChar
*str
, int32_t strLen
) {
185 // Create a string alias, since nothing is being added to the set.
186 UnicodeString
s(strLen
==-1, str
, strLen
);
187 return ((const UnicodeSet
*) set
)->UnicodeSet::containsAll(s
);
190 U_CAPI UBool U_EXPORT2
191 uset_containsNone(const USet
* set1
, const USet
* set2
) {
192 return ((const UnicodeSet
*) set1
)->UnicodeSet::containsNone(* (const UnicodeSet
*) set2
);
195 U_CAPI UBool U_EXPORT2
196 uset_containsSome(const USet
* set1
, const USet
* set2
) {
197 return ((const UnicodeSet
*) set1
)->UnicodeSet::containsSome(* (const UnicodeSet
*) set2
);
200 U_CAPI
int32_t U_EXPORT2
201 uset_span(const USet
*set
, const UChar
*s
, int32_t length
, USetSpanCondition spanCondition
) {
202 return ((UnicodeSet
*) set
)->UnicodeSet::span(s
, length
, spanCondition
);
205 U_CAPI
int32_t U_EXPORT2
206 uset_spanBack(const USet
*set
, const UChar
*s
, int32_t length
, USetSpanCondition spanCondition
) {
207 return ((UnicodeSet
*) set
)->UnicodeSet::spanBack(s
, length
, spanCondition
);
210 U_CAPI
int32_t U_EXPORT2
211 uset_spanUTF8(const USet
*set
, const char *s
, int32_t length
, USetSpanCondition spanCondition
) {
212 return ((UnicodeSet
*) set
)->UnicodeSet::spanUTF8(s
, length
, spanCondition
);
215 U_CAPI
int32_t U_EXPORT2
216 uset_spanBackUTF8(const USet
*set
, const char *s
, int32_t length
, USetSpanCondition spanCondition
) {
217 return ((UnicodeSet
*) set
)->UnicodeSet::spanBackUTF8(s
, length
, spanCondition
);
220 U_CAPI UBool U_EXPORT2
221 uset_equals(const USet
* set1
, const USet
* set2
) {
222 return *(const UnicodeSet
*)set1
== *(const UnicodeSet
*)set2
;
225 U_CAPI
int32_t U_EXPORT2
226 uset_indexOf(const USet
* set
, UChar32 c
) {
227 return ((UnicodeSet
*) set
)->UnicodeSet::indexOf(c
);
230 U_CAPI UChar32 U_EXPORT2
231 uset_charAt(const USet
* set
, int32_t index
) {
232 return ((UnicodeSet
*) set
)->UnicodeSet::charAt(index
);
235 U_CAPI
int32_t U_EXPORT2
236 uset_size(const USet
* set
) {
237 return ((const UnicodeSet
*) set
)->UnicodeSet::size();
242 * This class only exists to provide access to the UnicodeSet private
243 * USet support API. Declaring a class a friend is more portable than
244 * trying to declare extern "C" functions as friends.
246 class USetAccess
/* not : public UObject because all methods are static */ {
248 /* Try to have the compiler inline these*/
249 inline static int32_t getStringCount(const UnicodeSet
& set
) {
250 return set
.getStringCount();
252 inline static const UnicodeString
* getString(const UnicodeSet
& set
,
254 return set
.getString(i
);
257 /* do not instantiate*/
262 U_CAPI
int32_t U_EXPORT2
263 uset_getItemCount(const USet
* uset
) {
264 const UnicodeSet
& set
= *(const UnicodeSet
*)uset
;
265 return set
.getRangeCount() + USetAccess::getStringCount(set
);
268 U_CAPI
int32_t U_EXPORT2
269 uset_getItem(const USet
* uset
, int32_t itemIndex
,
270 UChar32
* start
, UChar32
* end
,
271 UChar
* str
, int32_t strCapacity
,
273 if (U_FAILURE(*ec
)) return 0;
274 const UnicodeSet
& set
= *(const UnicodeSet
*)uset
;
278 *ec
= U_ILLEGAL_ARGUMENT_ERROR
;
280 } else if (itemIndex
< (rangeCount
= set
.getRangeCount())) {
281 *start
= set
.getRangeStart(itemIndex
);
282 *end
= set
.getRangeEnd(itemIndex
);
285 itemIndex
-= rangeCount
;
286 if (itemIndex
< USetAccess::getStringCount(set
)) {
287 const UnicodeString
* s
= USetAccess::getString(set
, itemIndex
);
288 return s
->extract(str
, strCapacity
, *ec
);
290 *ec
= U_INDEX_OUTOFBOUNDS_ERROR
;
296 //U_CAPI int32_t U_EXPORT2
297 //uset_getRangeCount(const USet* set) {
298 // return ((const UnicodeSet*) set)->getRangeCount();
301 //U_CAPI UBool U_EXPORT2
302 //uset_getRange(const USet* set, int32_t rangeIndex,
303 // UChar32* pStart, UChar32* pEnd) {
304 // if ((uint32_t) rangeIndex >= (uint32_t) uset_getRangeCount(set)) {
307 // const UnicodeSet* us = (const UnicodeSet*) set;
308 // *pStart = us->getRangeStart(rangeIndex);
309 // *pEnd = us->getRangeEnd(rangeIndex);
314 * Serialize a USet into 16-bit units.
315 * Store BMP code points as themselves with one 16-bit unit each.
317 * Important: the code points in the array are in ascending order,
318 * therefore all BMP code points precede all supplementary code points.
320 * Store each supplementary code point in 2 16-bit units,
321 * simply with higher-then-lower 16-bit halfs.
323 * Precede the entire list with the length.
324 * If there are supplementary code points, then set bit 15 in the length
325 * and add the bmpLength between it and the array.
328 * - all BMP: (length=bmpLength) BMP, .., BMP
329 * - some supplementary: (length|0x8000) (bmpLength<length) BMP, .., BMP, supp-high, supp-low, ..
331 U_CAPI
int32_t U_EXPORT2
332 uset_serialize(const USet
* set
, uint16_t* dest
, int32_t destCapacity
, UErrorCode
* ec
) {
333 if (ec
==NULL
|| U_FAILURE(*ec
)) {
337 return ((const UnicodeSet
*) set
)->UnicodeSet::serialize(dest
, destCapacity
,* ec
);
340 U_CAPI UBool U_EXPORT2
341 uset_getSerializedSet(USerializedSet
* fillSet
, const uint16_t* src
, int32_t srcLength
) {
347 if(src
==NULL
|| srcLength
<=0) {
348 fillSet
->length
=fillSet
->bmpLength
=0;
354 /* there are supplementary values */
356 if(srcLength
<(2+length
)) {
357 fillSet
->length
=fillSet
->bmpLength
=0;
360 fillSet
->bmpLength
=*src
++;
362 /* only BMP values */
363 if(srcLength
<(1+length
)) {
364 fillSet
->length
=fillSet
->bmpLength
=0;
367 fillSet
->bmpLength
=length
;
370 fillSet
->length
=length
;
374 U_CAPI
void U_EXPORT2
375 uset_setSerializedToOne(USerializedSet
* fillSet
, UChar32 c
) {
376 if(fillSet
==NULL
|| (uint32_t)c
>0x10ffff) {
380 fillSet
->array
=fillSet
->staticArray
;
382 fillSet
->bmpLength
=fillSet
->length
=2;
383 fillSet
->staticArray
[0]=(uint16_t)c
;
384 fillSet
->staticArray
[1]=(uint16_t)c
+1;
385 } else if(c
==0xffff) {
386 fillSet
->bmpLength
=1;
388 fillSet
->staticArray
[0]=0xffff;
389 fillSet
->staticArray
[1]=1;
390 fillSet
->staticArray
[2]=0;
391 } else if(c
<0x10ffff) {
392 fillSet
->bmpLength
=0;
394 fillSet
->staticArray
[0]=(uint16_t)(c
>>16);
395 fillSet
->staticArray
[1]=(uint16_t)c
;
397 fillSet
->staticArray
[2]=(uint16_t)(c
>>16);
398 fillSet
->staticArray
[3]=(uint16_t)c
;
399 } else /* c==0x10ffff */ {
400 fillSet
->bmpLength
=0;
402 fillSet
->staticArray
[0]=0x10;
403 fillSet
->staticArray
[1]=0xffff;
407 U_CAPI UBool U_EXPORT2
408 uset_serializedContains(const USerializedSet
* set
, UChar32 c
) {
409 const uint16_t* array
;
411 if(set
==NULL
|| (uint32_t)c
>0x10ffff) {
417 /* find c in the BMP part */
419 int32_t hi
= set
->bmpLength
-1;
422 } else if (c
< array
[hi
]) {
424 int32_t i
= (lo
+ hi
) >> 1;
427 } else if (c
< array
[i
]) {
436 return (UBool
)(hi
&1);
438 /* find c in the supplementary part */
439 uint16_t high
=(uint16_t)(c
>>16), low
=(uint16_t)c
;
440 int32_t base
= set
->bmpLength
;
442 int32_t hi
= set
->length
- 2 - base
;
443 if (high
< array
[base
] || (high
==array
[base
] && low
<array
[base
+1])) {
445 } else if (high
< array
[base
+hi
] || (high
==array
[base
+hi
] && low
<array
[base
+hi
+1])) {
447 int32_t i
= ((lo
+ hi
) >> 1) & ~1; // Guarantee even result
448 int32_t iabs
= i
+ base
;
451 } else if (high
< array
[iabs
] || (high
==array
[iabs
] && low
<array
[iabs
+1])) {
460 /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
461 return (UBool
)(((hi
+(base
<<1))&2)!=0);
465 U_CAPI
int32_t U_EXPORT2
466 uset_getSerializedRangeCount(const USerializedSet
* set
) {
471 return (set
->bmpLength
+(set
->length
-set
->bmpLength
)/2+1)/2;
474 U_CAPI UBool U_EXPORT2
475 uset_getSerializedRange(const USerializedSet
* set
, int32_t rangeIndex
,
476 UChar32
* pStart
, UChar32
* pEnd
) {
477 const uint16_t* array
;
478 int32_t bmpLength
, length
;
480 if(set
==NULL
|| rangeIndex
<0 || pStart
==NULL
|| pEnd
==NULL
) {
486 bmpLength
=set
->bmpLength
;
488 rangeIndex
*=2; /* address start/limit pairs */
489 if(rangeIndex
<bmpLength
) {
490 *pStart
=array
[rangeIndex
++];
491 if(rangeIndex
<bmpLength
) {
492 *pEnd
=array
[rangeIndex
]-1;
493 } else if(rangeIndex
<length
) {
494 *pEnd
=((((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1])-1;
500 rangeIndex
-=bmpLength
;
501 rangeIndex
*=2; /* address pairs of pairs of units */
503 if(rangeIndex
<length
) {
505 *pStart
=(((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1];
507 if(rangeIndex
<length
) {
508 *pEnd
=((((int32_t)array
[rangeIndex
])<<16)|array
[rangeIndex
+1])-1;
519 // TODO The old, internal uset.c had an efficient uset_containsOne function.
520 // Returned the one and only code point, or else -1 or something.
521 // Consider adding such a function to both C and C++ UnicodeSet/uset.
522 // See tools/gennorm/store.c for usage, now usetContainsOne there.
524 // TODO Investigate incorporating this code into UnicodeSet to improve
527 // #define USET_GROW_DELTA 20
530 // findChar(const UChar32* array, int32_t length, UChar32 c) {
533 // /* check the last range limit first for more efficient appending */
535 // if(c>=array[length-1]) {
539 // /* do not check the last range limit again in the loop below */
543 // for(i=0; i<length && c>=array[i]; ++i) {}
548 // addRemove(USet* set, UChar32 c, int32_t doRemove) {
549 // int32_t i, length, more;
551 // if(set==NULL || (uint32_t)c>0x10ffff) {
555 // length=set->length;
556 // i=findChar(set->array, length, c);
557 // if((i&1)^doRemove) {
558 // /* c is already in the set */
562 // /* how many more array items do we need? */
563 // if(i<length && (c+1)==set->array[i]) {
564 // /* c is just before the following range, extend that in-place by one */
568 // if(c==set->array[i]) {
569 // /* the previous range collapsed, remove it */
570 // set->length=length-=2;
572 // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
577 // } else if(i>0 && c==set->array[i-1]) {
578 // /* c is just after the previous range, extend that in-place by one */
579 // if(++c<=0x10ffff) {
580 // set->array[i-1]=c;
581 // if(i<length && c==set->array[i]) {
582 // /* the following range collapsed, remove it */
584 // set->length=length-=2;
586 // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
590 // /* extend the previous range (had limit 0x10ffff) to the end of Unicode */
594 // } else if(i==length && c==0x10ffff) {
595 // /* insert one range limit c */
598 // /* insert two range limits c, c+1 */
602 // /* insert <more> range limits */
603 // if(length+more>set->capacity) {
605 // int32_t newCapacity=set->capacity+set->capacity/2+USET_GROW_DELTA;
606 // UChar32* newArray=(UChar32* )uprv_malloc(newCapacity*4);
607 // if(newArray==NULL) {
610 // set->capacity=newCapacity;
611 // uprv_memcpy(newArray, set->array, length*4);
613 // if(set->array!=set->staticBuffer) {
614 // uprv_free(set->array);
616 // set->array=newArray;
620 // uprv_memmove(set->array+i+more, set->array+i, (length-i)*4);
624 // set->array[i+1]=c+1;
626 // set->length+=more;
631 // U_CAPI UBool U_EXPORT2
632 // uset_add(USet* set, UChar32 c) {
633 // return addRemove(set, c, 0);
636 // U_CAPI void U_EXPORT2
637 // uset_remove(USet* set, UChar32 c) {
638 // addRemove(set, c, 1);