// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * * Copyright (C) 2002-2011, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* * file name: uset.cpp * encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * * created on: 2002mar07 * created by: Markus W. Scherer * * There are functions to efficiently serialize a USet into an array of uint16_t * and functions to use such a serialized form efficiently without * instantiating a new USet. */ #include "unicode/utypes.h" #include "unicode/uobject.h" #include "unicode/uset.h" #include "unicode/uniset.h" #include "cmemory.h" #include "unicode/ustring.h" #include "unicode/parsepos.h" U_NAMESPACE_USE U_CAPI USet* U_EXPORT2 uset_openEmpty() { … } U_CAPI USet* U_EXPORT2 uset_open(UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_close(USet* set) { … } U_CAPI USet * U_EXPORT2 uset_clone(const USet *set) { … } U_CAPI UBool U_EXPORT2 uset_isFrozen(const USet *set) { … } U_CAPI void U_EXPORT2 uset_freeze(USet *set) { … } U_CAPI USet * U_EXPORT2 uset_cloneAsThawed(const USet *set) { … } U_CAPI void U_EXPORT2 uset_set(USet* set, UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_addAll(USet* set, const USet *additionalSet) { … } U_CAPI void U_EXPORT2 uset_add(USet* set, UChar32 c) { … } U_CAPI void U_EXPORT2 uset_addRange(USet* set, UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_addString(USet* set, const char16_t* str, int32_t strLen) { … } U_CAPI void U_EXPORT2 uset_addAllCodePoints(USet* set, const char16_t *str, int32_t strLen) { … } U_CAPI void U_EXPORT2 uset_remove(USet* set, UChar32 c) { … } U_CAPI void U_EXPORT2 uset_removeRange(USet* set, UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_removeString(USet* set, const char16_t* str, int32_t strLen) { … } U_CAPI void U_EXPORT2 uset_removeAllCodePoints(USet *set, const char16_t *str, int32_t length) { … } U_CAPI void U_EXPORT2 uset_removeAll(USet* set, const USet* remove) { … } U_CAPI void U_EXPORT2 uset_retain(USet* set, UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_retainString(USet *set, const char16_t *str, int32_t length) { … } U_CAPI void U_EXPORT2 uset_retainAllCodePoints(USet *set, const char16_t *str, int32_t length) { … } U_CAPI void U_EXPORT2 uset_retainAll(USet* set, const USet* retain) { … } U_CAPI void U_EXPORT2 uset_compact(USet* set) { … } U_CAPI void U_EXPORT2 uset_complement(USet* set) { … } U_CAPI void U_EXPORT2 uset_complementRange(USet *set, UChar32 start, UChar32 end) { … } U_CAPI void U_EXPORT2 uset_complementString(USet *set, const char16_t *str, int32_t length) { … } U_CAPI void U_EXPORT2 uset_complementAllCodePoints(USet *set, const char16_t *str, int32_t length) { … } U_CAPI void U_EXPORT2 uset_complementAll(USet* set, const USet* complement) { … } U_CAPI void U_EXPORT2 uset_clear(USet* set) { … } U_CAPI void U_EXPORT2 uset_removeAllStrings(USet* set) { … } U_CAPI UBool U_EXPORT2 uset_isEmpty(const USet* set) { … } U_CAPI UBool U_EXPORT2 uset_hasStrings(const USet* set) { … } U_CAPI UBool U_EXPORT2 uset_contains(const USet* set, UChar32 c) { … } U_CAPI UBool U_EXPORT2 uset_containsRange(const USet* set, UChar32 start, UChar32 end) { … } U_CAPI UBool U_EXPORT2 uset_containsString(const USet* set, const char16_t* str, int32_t strLen) { … } U_CAPI UBool U_EXPORT2 uset_containsAll(const USet* set1, const USet* set2) { … } U_CAPI UBool U_EXPORT2 uset_containsAllCodePoints(const USet* set, const char16_t *str, int32_t strLen) { … } U_CAPI UBool U_EXPORT2 uset_containsNone(const USet* set1, const USet* set2) { … } U_CAPI UBool U_EXPORT2 uset_containsSome(const USet* set1, const USet* set2) { … } U_CAPI int32_t U_EXPORT2 uset_span(const USet *set, const char16_t *s, int32_t length, USetSpanCondition spanCondition) { … } U_CAPI int32_t U_EXPORT2 uset_spanBack(const USet *set, const char16_t *s, int32_t length, USetSpanCondition spanCondition) { … } U_CAPI int32_t U_EXPORT2 uset_spanUTF8(const USet *set, const char *s, int32_t length, USetSpanCondition spanCondition) { … } U_CAPI int32_t U_EXPORT2 uset_spanBackUTF8(const USet *set, const char *s, int32_t length, USetSpanCondition spanCondition) { … } U_CAPI UBool U_EXPORT2 uset_equals(const USet* set1, const USet* set2) { … } U_CAPI int32_t U_EXPORT2 uset_indexOf(const USet* set, UChar32 c) { … } U_CAPI UChar32 U_EXPORT2 uset_charAt(const USet* set, int32_t index) { … } U_CAPI int32_t U_EXPORT2 uset_size(const USet* set) { … } U_NAMESPACE_BEGIN /** * This class only exists to provide access to the UnicodeSet private * USet support API. Declaring a class a friend is more portable than * trying to declare extern "C" functions as friends. */ class USetAccess /* not : public UObject because all methods are static */ { … }; U_NAMESPACE_END U_CAPI int32_t U_EXPORT2 uset_getRangeCount(const USet *set) { … } U_CAPI int32_t U_EXPORT2 uset_getItemCount(const USet* uset) { … } U_CAPI int32_t U_EXPORT2 uset_getItem(const USet* uset, int32_t itemIndex, UChar32* start, UChar32* end, char16_t* str, int32_t strCapacity, UErrorCode* ec) { … } //U_CAPI UBool U_EXPORT2 //uset_getRange(const USet* set, int32_t rangeIndex, // UChar32* pStart, UChar32* pEnd) { // if ((uint32_t) rangeIndex >= (uint32_t) uset_getRangeCount(set)) { // return false; // } // const UnicodeSet* us = (const UnicodeSet*) set; // *pStart = us->getRangeStart(rangeIndex); // *pEnd = us->getRangeEnd(rangeIndex); // return true; //} /* * Serialize a USet into 16-bit units. * Store BMP code points as themselves with one 16-bit unit each. * * Important: the code points in the array are in ascending order, * therefore all BMP code points precede all supplementary code points. * * Store each supplementary code point in 2 16-bit units, * simply with higher-then-lower 16-bit halves. * * Precede the entire list with the length. * If there are supplementary code points, then set bit 15 in the length * and add the bmpLength between it and the array. * * In other words: * - all BMP: (length=bmpLength) BMP, .., BMP * - some supplementary: (length|0x8000) (bmpLength<length) BMP, .., BMP, supp-high, supp-low, .. */ U_CAPI int32_t U_EXPORT2 uset_serialize(const USet* set, uint16_t* dest, int32_t destCapacity, UErrorCode* ec) { … } U_CAPI UBool U_EXPORT2 uset_getSerializedSet(USerializedSet* fillSet, const uint16_t* src, int32_t srcLength) { … } U_CAPI void U_EXPORT2 uset_setSerializedToOne(USerializedSet* fillSet, UChar32 c) { … } U_CAPI UBool U_EXPORT2 uset_serializedContains(const USerializedSet* set, UChar32 c) { … } U_CAPI int32_t U_EXPORT2 uset_getSerializedRangeCount(const USerializedSet* set) { … } U_CAPI UBool U_EXPORT2 uset_getSerializedRange(const USerializedSet* set, int32_t rangeIndex, UChar32* pStart, UChar32* pEnd) { … } // TODO The old, internal uset.c had an efficient uset_containsOne function. // Returned the one and only code point, or else -1 or something. // Consider adding such a function to both C and C++ UnicodeSet/uset. // See tools/gennorm/store.c for usage, now usetContainsOne there. // TODO Investigate incorporating this code into UnicodeSet to improve // efficiency. // --- // #define USET_GROW_DELTA 20 // // static int32_t // findChar(const UChar32* array, int32_t length, UChar32 c) { // int32_t i; // // /* check the last range limit first for more efficient appending */ // if(length>0) { // if(c>=array[length-1]) { // return length; // } // // /* do not check the last range limit again in the loop below */ // --length; // } // // for(i=0; i<length && c>=array[i]; ++i) {} // return i; // } // // static UBool // addRemove(USet* set, UChar32 c, int32_t doRemove) { // int32_t i, length, more; // // if(set==nullptr || (uint32_t)c>0x10ffff) { // return false; // } // // length=set->length; // i=findChar(set->array, length, c); // if((i&1)^doRemove) { // /* c is already in the set */ // return true; // } // // /* how many more array items do we need? */ // if(i<length && (c+1)==set->array[i]) { // /* c is just before the following range, extend that in-place by one */ // set->array[i]=c; // if(i>0) { // --i; // if(c==set->array[i]) { // /* the previous range collapsed, remove it */ // set->length=length-=2; // if(i<length) { // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4); // } // } // } // return true; // } else if(i>0 && c==set->array[i-1]) { // /* c is just after the previous range, extend that in-place by one */ // if(++c<=0x10ffff) { // set->array[i-1]=c; // if(i<length && c==set->array[i]) { // /* the following range collapsed, remove it */ // --i; // set->length=length-=2; // if(i<length) { // uprv_memmove(set->array+i, set->array+i+2, (length-i)*4); // } // } // } else { // /* extend the previous range (had limit 0x10ffff) to the end of Unicode */ // set->length=i-1; // } // return true; // } else if(i==length && c==0x10ffff) { // /* insert one range limit c */ // more=1; // } else { // /* insert two range limits c, c+1 */ // more=2; // } // // /* insert <more> range limits */ // if(length+more>set->capacity) { // /* reallocate */ // int32_t newCapacity=set->capacity+set->capacity/2+USET_GROW_DELTA; // UChar32* newArray=(UChar32* )uprv_malloc(newCapacity*4); // if(newArray==nullptr) { // return false; // } // set->capacity=newCapacity; // uprv_memcpy(newArray, set->array, length*4); // // if(set->array!=set->staticBuffer) { // uprv_free(set->array); // } // set->array=newArray; // } // // if(i<length) { // uprv_memmove(set->array+i+more, set->array+i, (length-i)*4); // } // set->array[i]=c; // if(more==2) { // set->array[i+1]=c+1; // } // set->length+=more; // // return true; // } // // U_CAPI UBool U_EXPORT2 // uset_add(USet* set, UChar32 c) { // return addRemove(set, c, 0); // } // // U_CAPI void U_EXPORT2 // uset_remove(USet* set, UChar32 c) { // addRemove(set, c, 1); // }