1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2002-2011, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  uset.cpp
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2002mar07
16 *   created by: Markus W. Scherer
17 *
18 *   There are functions to efficiently serialize a USet into an array of uint16_t
19 *   and functions to use such a serialized form efficiently without
20 *   instantiating a new USet.
21 */
22 
23 #include "unicode/utypes.h"
24 #include "unicode/uobject.h"
25 #include "unicode/uset.h"
26 #include "unicode/uniset.h"
27 #include "cmemory.h"
28 #include "unicode/ustring.h"
29 #include "unicode/parsepos.h"
30 
31 U_NAMESPACE_USE
32 
33 U_CAPI USet* U_EXPORT2
uset_openEmpty()34 uset_openEmpty() {
35     return (USet*) new UnicodeSet();
36 }
37 
38 U_CAPI USet* U_EXPORT2
uset_open(UChar32 start,UChar32 end)39 uset_open(UChar32 start, UChar32 end) {
40     return (USet*) new UnicodeSet(start, end);
41 }
42 
43 U_CAPI void U_EXPORT2
uset_close(USet * set)44 uset_close(USet* set) {
45     delete (UnicodeSet*) set;
46 }
47 
48 U_CAPI USet * U_EXPORT2
uset_clone(const USet * set)49 uset_clone(const USet *set) {
50     return (USet*) (((UnicodeSet*) set)->UnicodeSet::clone());
51 }
52 
53 U_CAPI UBool U_EXPORT2
uset_isFrozen(const USet * set)54 uset_isFrozen(const USet *set) {
55     return ((UnicodeSet*) set)->UnicodeSet::isFrozen();
56 }
57 
58 U_CAPI void U_EXPORT2
uset_freeze(USet * set)59 uset_freeze(USet *set) {
60     ((UnicodeSet*) set)->UnicodeSet::freeze();
61 }
62 
63 U_CAPI USet * U_EXPORT2
uset_cloneAsThawed(const USet * set)64 uset_cloneAsThawed(const USet *set) {
65     return (USet*) (((UnicodeSet*) set)->UnicodeSet::cloneAsThawed());
66 }
67 
68 U_CAPI void U_EXPORT2
uset_set(USet * set,UChar32 start,UChar32 end)69 uset_set(USet* set,
70      UChar32 start, UChar32 end) {
71     ((UnicodeSet*) set)->UnicodeSet::set(start, end);
72 }
73 
74 U_CAPI void U_EXPORT2
uset_addAll(USet * set,const USet * additionalSet)75 uset_addAll(USet* set, const USet *additionalSet) {
76     ((UnicodeSet*) set)->UnicodeSet::addAll(*((const UnicodeSet*)additionalSet));
77 }
78 
79 U_CAPI void U_EXPORT2
uset_add(USet * set,UChar32 c)80 uset_add(USet* set, UChar32 c) {
81     ((UnicodeSet*) set)->UnicodeSet::add(c);
82 }
83 
84 U_CAPI void U_EXPORT2
uset_addRange(USet * set,UChar32 start,UChar32 end)85 uset_addRange(USet* set, UChar32 start, UChar32 end) {
86     ((UnicodeSet*) set)->UnicodeSet::add(start, end);
87 }
88 
89 U_CAPI void U_EXPORT2
uset_addString(USet * set,const UChar * str,int32_t strLen)90 uset_addString(USet* set, const UChar* str, int32_t strLen) {
91     // UnicodeString handles -1 for strLen
92     UnicodeString s(strLen<0, str, strLen);
93     ((UnicodeSet*) set)->UnicodeSet::add(s);
94 }
95 
96 U_CAPI void U_EXPORT2
uset_addAllCodePoints(USet * set,const UChar * str,int32_t strLen)97 uset_addAllCodePoints(USet* set, const UChar *str, int32_t strLen) {
98     // UnicodeString handles -1 for strLen
99     UnicodeString s(str, strLen);
100     ((UnicodeSet*) set)->UnicodeSet::addAll(s);
101 }
102 
103 U_CAPI void U_EXPORT2
uset_remove(USet * set,UChar32 c)104 uset_remove(USet* set, UChar32 c) {
105     ((UnicodeSet*) set)->UnicodeSet::remove(c);
106 }
107 
108 U_CAPI void U_EXPORT2
uset_removeRange(USet * set,UChar32 start,UChar32 end)109 uset_removeRange(USet* set, UChar32 start, UChar32 end) {
110     ((UnicodeSet*) set)->UnicodeSet::remove(start, end);
111 }
112 
113 U_CAPI void U_EXPORT2
uset_removeString(USet * set,const UChar * str,int32_t strLen)114 uset_removeString(USet* set, const UChar* str, int32_t strLen) {
115     UnicodeString s(strLen==-1, str, strLen);
116     ((UnicodeSet*) set)->UnicodeSet::remove(s);
117 }
118 
119 U_CAPI void U_EXPORT2
uset_removeAllCodePoints(USet * set,const UChar * str,int32_t length)120 uset_removeAllCodePoints(USet *set, const UChar *str, int32_t length) {
121     UnicodeString s(length==-1, str, length);
122     ((UnicodeSet*) set)->UnicodeSet::removeAll(s);
123 }
124 
125 U_CAPI void U_EXPORT2
uset_removeAll(USet * set,const USet * remove)126 uset_removeAll(USet* set, const USet* remove) {
127     ((UnicodeSet*) set)->UnicodeSet::removeAll(*(const UnicodeSet*)remove);
128 }
129 
130 U_CAPI void U_EXPORT2
uset_retain(USet * set,UChar32 start,UChar32 end)131 uset_retain(USet* set, UChar32 start, UChar32 end) {
132     ((UnicodeSet*) set)->UnicodeSet::retain(start, end);
133 }
134 
135 U_CAPI void U_EXPORT2
uset_retainString(USet * set,const UChar * str,int32_t length)136 uset_retainString(USet *set, const UChar *str, int32_t length) {
137     UnicodeString s(length==-1, str, length);
138     ((UnicodeSet*) set)->UnicodeSet::retain(s);
139 }
140 
141 U_CAPI void U_EXPORT2
uset_retainAllCodePoints(USet * set,const UChar * str,int32_t length)142 uset_retainAllCodePoints(USet *set, const UChar *str, int32_t length) {
143     UnicodeString s(length==-1, str, length);
144     ((UnicodeSet*) set)->UnicodeSet::retainAll(s);
145 }
146 
147 U_CAPI void U_EXPORT2
uset_retainAll(USet * set,const USet * retain)148 uset_retainAll(USet* set, const USet* retain) {
149     ((UnicodeSet*) set)->UnicodeSet::retainAll(*(const UnicodeSet*)retain);
150 }
151 
152 U_CAPI void U_EXPORT2
uset_compact(USet * set)153 uset_compact(USet* set) {
154     ((UnicodeSet*) set)->UnicodeSet::compact();
155 }
156 
157 U_CAPI void U_EXPORT2
uset_complement(USet * set)158 uset_complement(USet* set) {
159     ((UnicodeSet*) set)->UnicodeSet::complement();
160 }
161 
162 U_CAPI void U_EXPORT2
uset_complementRange(USet * set,UChar32 start,UChar32 end)163 uset_complementRange(USet *set, UChar32 start, UChar32 end) {
164     ((UnicodeSet*) set)->UnicodeSet::complement(start, end);
165 }
166 
167 U_CAPI void U_EXPORT2
uset_complementString(USet * set,const UChar * str,int32_t length)168 uset_complementString(USet *set, const UChar *str, int32_t length) {
169     UnicodeString s(length==-1, str, length);
170     ((UnicodeSet*) set)->UnicodeSet::complement(s);
171 }
172 
173 U_CAPI void U_EXPORT2
uset_complementAllCodePoints(USet * set,const UChar * str,int32_t length)174 uset_complementAllCodePoints(USet *set, const UChar *str, int32_t length) {
175     UnicodeString s(length==-1, str, length);
176     ((UnicodeSet*) set)->UnicodeSet::complementAll(s);
177 }
178 
179 U_CAPI void U_EXPORT2
uset_complementAll(USet * set,const USet * complement)180 uset_complementAll(USet* set, const USet* complement) {
181     ((UnicodeSet*) set)->UnicodeSet::complementAll(*(const UnicodeSet*)complement);
182 }
183 
184 U_CAPI void U_EXPORT2
uset_clear(USet * set)185 uset_clear(USet* set) {
186     ((UnicodeSet*) set)->UnicodeSet::clear();
187 }
188 
189 U_CAPI void U_EXPORT2
uset_removeAllStrings(USet * set)190 uset_removeAllStrings(USet* set) {
191     ((UnicodeSet*) set)->UnicodeSet::removeAllStrings();
192 }
193 
194 U_CAPI UBool U_EXPORT2
uset_isEmpty(const USet * set)195 uset_isEmpty(const USet* set) {
196     return ((const UnicodeSet*) set)->UnicodeSet::isEmpty();
197 }
198 
199 U_CAPI UBool U_EXPORT2
uset_contains(const USet * set,UChar32 c)200 uset_contains(const USet* set, UChar32 c) {
201     return ((const UnicodeSet*) set)->UnicodeSet::contains(c);
202 }
203 
204 U_CAPI UBool U_EXPORT2
uset_containsRange(const USet * set,UChar32 start,UChar32 end)205 uset_containsRange(const USet* set, UChar32 start, UChar32 end) {
206     return ((const UnicodeSet*) set)->UnicodeSet::contains(start, end);
207 }
208 
209 U_CAPI UBool U_EXPORT2
uset_containsString(const USet * set,const UChar * str,int32_t strLen)210 uset_containsString(const USet* set, const UChar* str, int32_t strLen) {
211     UnicodeString s(strLen==-1, str, strLen);
212     return ((const UnicodeSet*) set)->UnicodeSet::contains(s);
213 }
214 
215 U_CAPI UBool U_EXPORT2
uset_containsAll(const USet * set1,const USet * set2)216 uset_containsAll(const USet* set1, const USet* set2) {
217     return ((const UnicodeSet*) set1)->UnicodeSet::containsAll(* (const UnicodeSet*) set2);
218 }
219 
220 U_CAPI UBool U_EXPORT2
uset_containsAllCodePoints(const USet * set,const UChar * str,int32_t strLen)221 uset_containsAllCodePoints(const USet* set, const UChar *str, int32_t strLen) {
222     // Create a string alias, since nothing is being added to the set.
223     UnicodeString s(strLen==-1, str, strLen);
224     return ((const UnicodeSet*) set)->UnicodeSet::containsAll(s);
225 }
226 
227 U_CAPI UBool U_EXPORT2
uset_containsNone(const USet * set1,const USet * set2)228 uset_containsNone(const USet* set1, const USet* set2) {
229     return ((const UnicodeSet*) set1)->UnicodeSet::containsNone(* (const UnicodeSet*) set2);
230 }
231 
232 U_CAPI UBool U_EXPORT2
uset_containsSome(const USet * set1,const USet * set2)233 uset_containsSome(const USet* set1, const USet* set2) {
234     return ((const UnicodeSet*) set1)->UnicodeSet::containsSome(* (const UnicodeSet*) set2);
235 }
236 
237 U_CAPI int32_t U_EXPORT2
uset_span(const USet * set,const UChar * s,int32_t length,USetSpanCondition spanCondition)238 uset_span(const USet *set, const UChar *s, int32_t length, USetSpanCondition spanCondition) {
239     return ((UnicodeSet*) set)->UnicodeSet::span(s, length, spanCondition);
240 }
241 
242 U_CAPI int32_t U_EXPORT2
uset_spanBack(const USet * set,const UChar * s,int32_t length,USetSpanCondition spanCondition)243 uset_spanBack(const USet *set, const UChar *s, int32_t length, USetSpanCondition spanCondition) {
244     return ((UnicodeSet*) set)->UnicodeSet::spanBack(s, length, spanCondition);
245 }
246 
247 U_CAPI int32_t U_EXPORT2
uset_spanUTF8(const USet * set,const char * s,int32_t length,USetSpanCondition spanCondition)248 uset_spanUTF8(const USet *set, const char *s, int32_t length, USetSpanCondition spanCondition) {
249     return ((UnicodeSet*) set)->UnicodeSet::spanUTF8(s, length, spanCondition);
250 }
251 
252 U_CAPI int32_t U_EXPORT2
uset_spanBackUTF8(const USet * set,const char * s,int32_t length,USetSpanCondition spanCondition)253 uset_spanBackUTF8(const USet *set, const char *s, int32_t length, USetSpanCondition spanCondition) {
254     return ((UnicodeSet*) set)->UnicodeSet::spanBackUTF8(s, length, spanCondition);
255 }
256 
257 U_CAPI UBool U_EXPORT2
uset_equals(const USet * set1,const USet * set2)258 uset_equals(const USet* set1, const USet* set2) {
259     return *(const UnicodeSet*)set1 == *(const UnicodeSet*)set2;
260 }
261 
262 U_CAPI int32_t U_EXPORT2
uset_indexOf(const USet * set,UChar32 c)263 uset_indexOf(const USet* set, UChar32 c) {
264     return ((UnicodeSet*) set)->UnicodeSet::indexOf(c);
265 }
266 
267 U_CAPI UChar32 U_EXPORT2
uset_charAt(const USet * set,int32_t index)268 uset_charAt(const USet* set, int32_t index) {
269     return ((UnicodeSet*) set)->UnicodeSet::charAt(index);
270 }
271 
272 U_CAPI int32_t U_EXPORT2
uset_size(const USet * set)273 uset_size(const USet* set) {
274     return ((const UnicodeSet*) set)->UnicodeSet::size();
275 }
276 
277 U_NAMESPACE_BEGIN
278 /**
279  * This class only exists to provide access to the UnicodeSet private
280  * USet support API.  Declaring a class a friend is more portable than
281  * trying to declare extern "C" functions as friends.
282  */
283 class USetAccess /* not : public UObject because all methods are static */ {
284 public:
285     /* Try to have the compiler inline these*/
getStringCount(const UnicodeSet & set)286     inline static int32_t getStringCount(const UnicodeSet& set) {
287         return set.stringsSize();
288     }
getString(const UnicodeSet & set,int32_t i)289     inline static const UnicodeString* getString(const UnicodeSet& set,
290                                                  int32_t i) {
291         return set.getString(i);
292     }
293 private:
294     /* do not instantiate*/
295     USetAccess();
296 };
297 U_NAMESPACE_END
298 
299 U_CAPI int32_t U_EXPORT2
uset_getItemCount(const USet * uset)300 uset_getItemCount(const USet* uset) {
301     const UnicodeSet& set = *(const UnicodeSet*)uset;
302     return set.getRangeCount() + USetAccess::getStringCount(set);
303 }
304 
305 U_CAPI int32_t U_EXPORT2
uset_getItem(const USet * uset,int32_t itemIndex,UChar32 * start,UChar32 * end,UChar * str,int32_t strCapacity,UErrorCode * ec)306 uset_getItem(const USet* uset, int32_t itemIndex,
307              UChar32* start, UChar32* end,
308              UChar* str, int32_t strCapacity,
309              UErrorCode* ec) {
310     if (U_FAILURE(*ec)) return 0;
311     const UnicodeSet& set = *(const UnicodeSet*)uset;
312     int32_t rangeCount;
313 
314     if (itemIndex < 0) {
315         *ec = U_ILLEGAL_ARGUMENT_ERROR;
316         return -1;
317     } else if (itemIndex < (rangeCount = set.getRangeCount())) {
318         *start = set.getRangeStart(itemIndex);
319         *end = set.getRangeEnd(itemIndex);
320         return 0;
321     } else {
322         itemIndex -= rangeCount;
323         if (itemIndex < USetAccess::getStringCount(set)) {
324             const UnicodeString* s = USetAccess::getString(set, itemIndex);
325             return s->extract(str, strCapacity, *ec);
326         } else {
327             *ec = U_INDEX_OUTOFBOUNDS_ERROR;
328             return -1;
329         }
330     }
331 }
332 
333 //U_CAPI int32_t U_EXPORT2
334 //uset_getRangeCount(const USet* set) {
335 //    return ((const UnicodeSet*) set)->getRangeCount();
336 //}
337 //
338 //U_CAPI UBool U_EXPORT2
339 //uset_getRange(const USet* set, int32_t rangeIndex,
340 //              UChar32* pStart, UChar32* pEnd) {
341 //    if ((uint32_t) rangeIndex >= (uint32_t) uset_getRangeCount(set)) {
342 //        return FALSE;
343 //    }
344 //    const UnicodeSet* us = (const UnicodeSet*) set;
345 //    *pStart = us->getRangeStart(rangeIndex);
346 //    *pEnd = us->getRangeEnd(rangeIndex);
347 //    return TRUE;
348 //}
349 
350 /*
351  * Serialize a USet into 16-bit units.
352  * Store BMP code points as themselves with one 16-bit unit each.
353  *
354  * Important: the code points in the array are in ascending order,
355  * therefore all BMP code points precede all supplementary code points.
356  *
357  * Store each supplementary code point in 2 16-bit units,
358  * simply with higher-then-lower 16-bit halfs.
359  *
360  * Precede the entire list with the length.
361  * If there are supplementary code points, then set bit 15 in the length
362  * and add the bmpLength between it and the array.
363  *
364  * In other words:
365  * - all BMP:            (length=bmpLength) BMP, .., BMP
366  * - some supplementary: (length|0x8000) (bmpLength<length) BMP, .., BMP, supp-high, supp-low, ..
367  */
368 U_CAPI int32_t U_EXPORT2
uset_serialize(const USet * set,uint16_t * dest,int32_t destCapacity,UErrorCode * ec)369 uset_serialize(const USet* set, uint16_t* dest, int32_t destCapacity, UErrorCode* ec) {
370     if (ec==NULL || U_FAILURE(*ec)) {
371         return 0;
372     }
373 
374     return ((const UnicodeSet*) set)->UnicodeSet::serialize(dest, destCapacity,* ec);
375 }
376 
377 U_CAPI UBool U_EXPORT2
uset_getSerializedSet(USerializedSet * fillSet,const uint16_t * src,int32_t srcLength)378 uset_getSerializedSet(USerializedSet* fillSet, const uint16_t* src, int32_t srcLength) {
379     int32_t length;
380 
381     if(fillSet==NULL) {
382         return FALSE;
383     }
384     if(src==NULL || srcLength<=0) {
385         fillSet->length=fillSet->bmpLength=0;
386         return FALSE;
387     }
388 
389     length=*src++;
390     if(length&0x8000) {
391         /* there are supplementary values */
392         length&=0x7fff;
393         if(srcLength<(2+length)) {
394             fillSet->length=fillSet->bmpLength=0;
395             return FALSE;
396         }
397         fillSet->bmpLength=*src++;
398     } else {
399         /* only BMP values */
400         if(srcLength<(1+length)) {
401             fillSet->length=fillSet->bmpLength=0;
402             return FALSE;
403         }
404         fillSet->bmpLength=length;
405     }
406     fillSet->array=src;
407     fillSet->length=length;
408     return TRUE;
409 }
410 
411 U_CAPI void U_EXPORT2
uset_setSerializedToOne(USerializedSet * fillSet,UChar32 c)412 uset_setSerializedToOne(USerializedSet* fillSet, UChar32 c) {
413     if(fillSet==NULL || (uint32_t)c>0x10ffff) {
414         return;
415     }
416 
417     fillSet->array=fillSet->staticArray;
418     if(c<0xffff) {
419         fillSet->bmpLength=fillSet->length=2;
420         fillSet->staticArray[0]=(uint16_t)c;
421         fillSet->staticArray[1]=(uint16_t)c+1;
422     } else if(c==0xffff) {
423         fillSet->bmpLength=1;
424         fillSet->length=3;
425         fillSet->staticArray[0]=0xffff;
426         fillSet->staticArray[1]=1;
427         fillSet->staticArray[2]=0;
428     } else if(c<0x10ffff) {
429         fillSet->bmpLength=0;
430         fillSet->length=4;
431         fillSet->staticArray[0]=(uint16_t)(c>>16);
432         fillSet->staticArray[1]=(uint16_t)c;
433         ++c;
434         fillSet->staticArray[2]=(uint16_t)(c>>16);
435         fillSet->staticArray[3]=(uint16_t)c;
436     } else /* c==0x10ffff */ {
437         fillSet->bmpLength=0;
438         fillSet->length=2;
439         fillSet->staticArray[0]=0x10;
440         fillSet->staticArray[1]=0xffff;
441     }
442 }
443 
444 U_CAPI UBool U_EXPORT2
uset_serializedContains(const USerializedSet * set,UChar32 c)445 uset_serializedContains(const USerializedSet* set, UChar32 c) {
446     const uint16_t* array;
447 
448     if(set==NULL || (uint32_t)c>0x10ffff) {
449         return FALSE;
450     }
451 
452     array=set->array;
453     if(c<=0xffff) {
454         /* find c in the BMP part */
455         int32_t lo = 0;
456         int32_t hi = set->bmpLength-1;
457         if (c < array[0]) {
458             hi = 0;
459         } else if (c < array[hi]) {
460             for(;;) {
461                 int32_t i = (lo + hi) >> 1;
462                 if (i == lo) {
463                     break;  // Done!
464                 } else if (c < array[i]) {
465                     hi = i;
466                 } else {
467                     lo = i;
468                 }
469             }
470         } else {
471             hi += 1;
472         }
473         return (UBool)(hi&1);
474     } else {
475         /* find c in the supplementary part */
476         uint16_t high=(uint16_t)(c>>16), low=(uint16_t)c;
477         int32_t base = set->bmpLength;
478         int32_t lo = 0;
479         int32_t hi = set->length - 2 - base;
480         if (high < array[base] || (high==array[base] && low<array[base+1])) {
481             hi = 0;
482         } else if (high < array[base+hi] || (high==array[base+hi] && low<array[base+hi+1])) {
483             for (;;) {
484                 int32_t i = ((lo + hi) >> 1) & ~1;  // Guarantee even result
485                 int32_t iabs = i + base;
486                 if (i == lo) {
487                     break;  // Done!
488                 } else if (high < array[iabs] || (high==array[iabs] && low<array[iabs+1])) {
489                     hi = i;
490                 } else {
491                     lo = i;
492                 }
493             }
494         } else {
495             hi += 2;
496         }
497         /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
498         return (UBool)(((hi+(base<<1))&2)!=0);
499     }
500 }
501 
502 U_CAPI int32_t U_EXPORT2
uset_getSerializedRangeCount(const USerializedSet * set)503 uset_getSerializedRangeCount(const USerializedSet* set) {
504     if(set==NULL) {
505         return 0;
506     }
507 
508     return (set->bmpLength+(set->length-set->bmpLength)/2+1)/2;
509 }
510 
511 U_CAPI UBool U_EXPORT2
uset_getSerializedRange(const USerializedSet * set,int32_t rangeIndex,UChar32 * pStart,UChar32 * pEnd)512 uset_getSerializedRange(const USerializedSet* set, int32_t rangeIndex,
513                         UChar32* pStart, UChar32* pEnd) {
514     const uint16_t* array;
515     int32_t bmpLength, length;
516 
517     if(set==NULL || rangeIndex<0 || pStart==NULL || pEnd==NULL) {
518         return FALSE;
519     }
520 
521     array=set->array;
522     length=set->length;
523     bmpLength=set->bmpLength;
524 
525     rangeIndex*=2; /* address start/limit pairs */
526     if(rangeIndex<bmpLength) {
527         *pStart=array[rangeIndex++];
528         if(rangeIndex<bmpLength) {
529             *pEnd=array[rangeIndex]-1;
530         } else if(rangeIndex<length) {
531             *pEnd=((((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1])-1;
532         } else {
533             *pEnd=0x10ffff;
534         }
535         return TRUE;
536     } else {
537         rangeIndex-=bmpLength;
538         rangeIndex*=2; /* address pairs of pairs of units */
539         length-=bmpLength;
540         if(rangeIndex<length) {
541             array+=bmpLength;
542             *pStart=(((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1];
543             rangeIndex+=2;
544             if(rangeIndex<length) {
545                 *pEnd=((((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1])-1;
546             } else {
547                 *pEnd=0x10ffff;
548             }
549             return TRUE;
550         } else {
551             return FALSE;
552         }
553     }
554 }
555 
556 // TODO The old, internal uset.c had an efficient uset_containsOne function.
557 // Returned the one and only code point, or else -1 or something.
558 // Consider adding such a function to both C and C++ UnicodeSet/uset.
559 // See tools/gennorm/store.c for usage, now usetContainsOne there.
560 
561 // TODO Investigate incorporating this code into UnicodeSet to improve
562 // efficiency.
563 // ---
564 // #define USET_GROW_DELTA 20
565 //
566 // static int32_t
567 // findChar(const UChar32* array, int32_t length, UChar32 c) {
568 //     int32_t i;
569 //
570 //     /* check the last range limit first for more efficient appending */
571 //     if(length>0) {
572 //         if(c>=array[length-1]) {
573 //             return length;
574 //         }
575 //
576 //         /* do not check the last range limit again in the loop below */
577 //         --length;
578 //     }
579 //
580 //     for(i=0; i<length && c>=array[i]; ++i) {}
581 //     return i;
582 // }
583 //
584 // static UBool
585 // addRemove(USet* set, UChar32 c, int32_t doRemove) {
586 //     int32_t i, length, more;
587 //
588 //     if(set==NULL || (uint32_t)c>0x10ffff) {
589 //         return FALSE;
590 //     }
591 //
592 //     length=set->length;
593 //     i=findChar(set->array, length, c);
594 //     if((i&1)^doRemove) {
595 //         /* c is already in the set */
596 //         return TRUE;
597 //     }
598 //
599 //     /* how many more array items do we need? */
600 //     if(i<length && (c+1)==set->array[i]) {
601 //         /* c is just before the following range, extend that in-place by one */
602 //         set->array[i]=c;
603 //         if(i>0) {
604 //             --i;
605 //             if(c==set->array[i]) {
606 //                 /* the previous range collapsed, remove it */
607 //                 set->length=length-=2;
608 //                 if(i<length) {
609 //                     uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
610 //                 }
611 //             }
612 //         }
613 //         return TRUE;
614 //     } else if(i>0 && c==set->array[i-1]) {
615 //         /* c is just after the previous range, extend that in-place by one */
616 //         if(++c<=0x10ffff) {
617 //             set->array[i-1]=c;
618 //             if(i<length && c==set->array[i]) {
619 //                 /* the following range collapsed, remove it */
620 //                 --i;
621 //                 set->length=length-=2;
622 //                 if(i<length) {
623 //                     uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
624 //                 }
625 //             }
626 //         } else {
627 //             /* extend the previous range (had limit 0x10ffff) to the end of Unicode */
628 //             set->length=i-1;
629 //         }
630 //         return TRUE;
631 //     } else if(i==length && c==0x10ffff) {
632 //         /* insert one range limit c */
633 //         more=1;
634 //     } else {
635 //         /* insert two range limits c, c+1 */
636 //         more=2;
637 //     }
638 //
639 //     /* insert <more> range limits */
640 //     if(length+more>set->capacity) {
641 //         /* reallocate */
642 //         int32_t newCapacity=set->capacity+set->capacity/2+USET_GROW_DELTA;
643 //         UChar32* newArray=(UChar32* )uprv_malloc(newCapacity*4);
644 //         if(newArray==NULL) {
645 //             return FALSE;
646 //         }
647 //         set->capacity=newCapacity;
648 //         uprv_memcpy(newArray, set->array, length*4);
649 //
650 //         if(set->array!=set->staticBuffer) {
651 //             uprv_free(set->array);
652 //         }
653 //         set->array=newArray;
654 //     }
655 //
656 //     if(i<length) {
657 //         uprv_memmove(set->array+i+more, set->array+i, (length-i)*4);
658 //     }
659 //     set->array[i]=c;
660 //     if(more==2) {
661 //         set->array[i+1]=c+1;
662 //     }
663 //     set->length+=more;
664 //
665 //     return TRUE;
666 // }
667 //
668 // U_CAPI UBool U_EXPORT2
669 // uset_add(USet* set, UChar32 c) {
670 //     return addRemove(set, c, 0);
671 // }
672 //
673 // U_CAPI void U_EXPORT2
674 // uset_remove(USet* set, UChar32 c) {
675 //     addRemove(set, c, 1);
676 // }
677