|
ICU 73.2
73.2
|
00001 // © 2016 and later: Unicode, Inc. and others. 00002 // License & terms of use: http://www.unicode.org/copyright.html 00003 /* 00004 *************************************************************************** 00005 * Copyright (C) 1999-2016, International Business Machines Corporation 00006 * and others. All Rights Reserved. 00007 *************************************************************************** 00008 * Date Name Description 00009 * 10/20/99 alan Creation. 00010 *************************************************************************** 00011 */ 00012 00013 #ifndef UNICODESET_H 00014 #define UNICODESET_H 00015 00016 #include "unicode/utypes.h" 00017 00018 #if U_SHOW_CPLUSPLUS_API 00019 00020 #include "unicode/ucpmap.h" 00021 #include "unicode/unifilt.h" 00022 #include "unicode/unistr.h" 00023 #include "unicode/uset.h" 00024 00030 U_NAMESPACE_BEGIN 00031 00032 // Forward Declarations. 00033 class BMPSet; 00034 class ParsePosition; 00035 class RBBIRuleScanner; 00036 class SymbolTable; 00037 class UnicodeSetStringSpan; 00038 class UVector; 00039 class RuleCharacterIterator; 00040 00285 class U_COMMON_API UnicodeSet final : public UnicodeFilter { 00286 private: 00291 static constexpr int32_t INITIAL_CAPACITY = 25; 00292 // fFlags constant 00293 static constexpr uint8_t kIsBogus = 1; // This set is bogus (i.e. not valid) 00294 00295 UChar32* list = stackList; // MUST be terminated with HIGH 00296 int32_t capacity = INITIAL_CAPACITY; // capacity of list 00297 int32_t len = 1; // length of list used; 1 <= len <= capacity 00298 uint8_t fFlags = 0; // Bit flag (see constants above) 00299 00300 BMPSet *bmpSet = nullptr; // The set is frozen iff either bmpSet or stringSpan is not nullptr. 00301 UChar32* buffer = nullptr; // internal buffer, may be nullptr 00302 int32_t bufferCapacity = 0; // capacity of buffer 00303 00313 char16_t *pat = nullptr; 00314 int32_t patLen = 0; 00315 00316 UVector* strings = nullptr; // maintained in sorted order 00317 UnicodeSetStringSpan *stringSpan = nullptr; 00318 00324 UChar32 stackList[INITIAL_CAPACITY]; 00325 00326 public: 00336 inline UBool isBogus(void) const; 00337 00354 void setToBogus(); 00355 00356 public: 00357 00358 enum { 00363 MIN_VALUE = 0, 00364 00369 MAX_VALUE = 0x10ffff 00370 }; 00371 00372 //---------------------------------------------------------------- 00373 // Constructors &c 00374 //---------------------------------------------------------------- 00375 00376 public: 00377 00382 UnicodeSet(); 00383 00392 UnicodeSet(UChar32 start, UChar32 end); 00393 00394 #ifndef U_HIDE_INTERNAL_API 00395 00398 enum ESerialization { 00399 kSerialized /* result of serialize() */ 00400 }; 00401 00412 UnicodeSet(const uint16_t buffer[], int32_t bufferLen, 00413 ESerialization serialization, UErrorCode &status); 00414 #endif /* U_HIDE_INTERNAL_API */ 00415 00424 UnicodeSet(const UnicodeString& pattern, 00425 UErrorCode& status); 00426 00427 #ifndef U_HIDE_INTERNAL_API 00428 00442 UnicodeSet(const UnicodeString& pattern, 00443 uint32_t options, 00444 const SymbolTable* symbols, 00445 UErrorCode& status); 00446 #endif /* U_HIDE_INTERNAL_API */ 00447 00463 UnicodeSet(const UnicodeString& pattern, ParsePosition& pos, 00464 uint32_t options, 00465 const SymbolTable* symbols, 00466 UErrorCode& status); 00467 00472 UnicodeSet(const UnicodeSet& o); 00473 00478 virtual ~UnicodeSet(); 00479 00485 UnicodeSet& operator=(const UnicodeSet& o); 00486 00498 virtual bool operator==(const UnicodeSet& o) const; 00499 00505 inline bool operator!=(const UnicodeSet& o) const; 00506 00516 virtual UnicodeSet* clone() const override; 00517 00525 virtual int32_t hashCode(void) const; 00526 00535 inline static UnicodeSet *fromUSet(USet *uset); 00536 00545 inline static const UnicodeSet *fromUSet(const USet *uset); 00546 00554 inline USet *toUSet(); 00555 00556 00564 inline const USet * toUSet() const; 00565 00566 00567 //---------------------------------------------------------------- 00568 // Freezable API 00569 //---------------------------------------------------------------- 00570 00579 inline UBool isFrozen() const; 00580 00594 UnicodeSet *freeze(); 00595 00604 UnicodeSet *cloneAsThawed() const; 00605 00606 //---------------------------------------------------------------- 00607 // Public API 00608 //---------------------------------------------------------------- 00609 00619 UnicodeSet& set(UChar32 start, UChar32 end); 00620 00626 static UBool resemblesPattern(const UnicodeString& pattern, 00627 int32_t pos); 00628 00641 UnicodeSet& applyPattern(const UnicodeString& pattern, 00642 UErrorCode& status); 00643 00644 #ifndef U_HIDE_INTERNAL_API 00645 00663 UnicodeSet& applyPattern(const UnicodeString& pattern, 00664 uint32_t options, 00665 const SymbolTable* symbols, 00666 UErrorCode& status); 00667 #endif /* U_HIDE_INTERNAL_API */ 00668 00702 UnicodeSet& applyPattern(const UnicodeString& pattern, 00703 ParsePosition& pos, 00704 uint32_t options, 00705 const SymbolTable* symbols, 00706 UErrorCode& status); 00707 00721 virtual UnicodeString& toPattern(UnicodeString& result, 00722 UBool escapeUnprintable = false) const override; 00723 00746 UnicodeSet& applyIntPropertyValue(UProperty prop, 00747 int32_t value, 00748 UErrorCode& ec); 00749 00779 UnicodeSet& applyPropertyAlias(const UnicodeString& prop, 00780 const UnicodeString& value, 00781 UErrorCode& ec); 00782 00795 virtual int32_t size(void) const; 00796 00803 virtual UBool isEmpty(void) const; 00804 00809 UBool hasStrings() const; 00810 00818 virtual UBool contains(UChar32 c) const override; 00819 00828 virtual UBool contains(UChar32 start, UChar32 end) const; 00829 00837 UBool contains(const UnicodeString& s) const; 00838 00846 virtual UBool containsAll(const UnicodeSet& c) const; 00847 00855 UBool containsAll(const UnicodeString& s) const; 00856 00865 UBool containsNone(UChar32 start, UChar32 end) const; 00866 00874 UBool containsNone(const UnicodeSet& c) const; 00875 00883 UBool containsNone(const UnicodeString& s) const; 00884 00893 inline UBool containsSome(UChar32 start, UChar32 end) const; 00894 00902 inline UBool containsSome(const UnicodeSet& s) const; 00903 00911 inline UBool containsSome(const UnicodeString& s) const; 00912 00931 int32_t span(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const; 00932 00945 inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const; 00946 00964 int32_t spanBack(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const; 00965 00979 inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const; 00980 00999 int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const; 01000 01018 int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const; 01019 01024 virtual UMatchDegree matches(const Replaceable& text, 01025 int32_t& offset, 01026 int32_t limit, 01027 UBool incremental) override; 01028 01029 private: 01052 static int32_t matchRest(const Replaceable& text, 01053 int32_t start, int32_t limit, 01054 const UnicodeString& s); 01055 01065 int32_t findCodePoint(UChar32 c) const; 01066 01067 public: 01068 01076 virtual void addMatchSetTo(UnicodeSet& toUnionTo) const override; 01077 01086 int32_t indexOf(UChar32 c) const; 01087 01103 UChar32 charAt(int32_t index) const; 01104 01119 virtual UnicodeSet& add(UChar32 start, UChar32 end); 01120 01131 UnicodeSet& add(UChar32 c); 01132 01144 UnicodeSet& add(const UnicodeString& s); 01145 01146 private: 01152 static int32_t getSingleCP(const UnicodeString& s); 01153 01154 void _add(const UnicodeString& s); 01155 01156 public: 01165 UnicodeSet& addAll(const UnicodeString& s); 01166 01174 UnicodeSet& retainAll(const UnicodeString& s); 01175 01183 UnicodeSet& complementAll(const UnicodeString& s); 01184 01192 UnicodeSet& removeAll(const UnicodeString& s); 01193 01202 static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s); 01203 01204 01212 static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s); 01213 01225 virtual UnicodeSet& retain(UChar32 start, UChar32 end); 01226 01227 01236 UnicodeSet& retain(UChar32 c); 01237 01248 UnicodeSet& retain(const UnicodeString &s); 01249 01263 virtual UnicodeSet& remove(UChar32 start, UChar32 end); 01264 01275 UnicodeSet& remove(UChar32 c); 01276 01286 UnicodeSet& remove(const UnicodeString& s); 01287 01300 virtual UnicodeSet& complement(); 01301 01314 virtual UnicodeSet& complement(UChar32 start, UChar32 end); 01315 01326 UnicodeSet& complement(UChar32 c); 01327 01337 UnicodeSet& complement(const UnicodeString& s); 01338 01351 virtual UnicodeSet& addAll(const UnicodeSet& c); 01352 01364 virtual UnicodeSet& retainAll(const UnicodeSet& c); 01365 01377 virtual UnicodeSet& removeAll(const UnicodeSet& c); 01378 01389 virtual UnicodeSet& complementAll(const UnicodeSet& c); 01390 01397 virtual UnicodeSet& clear(void); 01398 01426 UnicodeSet& closeOver(int32_t attribute); 01427 01434 virtual UnicodeSet &removeAllStrings(); 01435 01443 virtual int32_t getRangeCount(void) const; 01444 01452 virtual UChar32 getRangeStart(int32_t index) const; 01453 01461 virtual UChar32 getRangeEnd(int32_t index) const; 01462 01511 int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const; 01512 01519 virtual UnicodeSet& compact(); 01520 01532 static UClassID U_EXPORT2 getStaticClassID(void); 01533 01542 virtual UClassID getDynamicClassID(void) const override; 01543 01544 private: 01545 01546 // Private API for the USet API 01547 01548 friend class USetAccess; 01549 01550 const UnicodeString* getString(int32_t index) const; 01551 01552 //---------------------------------------------------------------- 01553 // RuleBasedTransliterator support 01554 //---------------------------------------------------------------- 01555 01556 private: 01557 01563 virtual UBool matchesIndexValue(uint8_t v) const override; 01564 01565 private: 01566 friend class RBBIRuleScanner; 01567 01568 //---------------------------------------------------------------- 01569 // Implementation: Clone as thawed (see ICU4J Freezable) 01570 //---------------------------------------------------------------- 01571 01572 UnicodeSet(const UnicodeSet& o, UBool /* asThawed */); 01573 UnicodeSet& copyFrom(const UnicodeSet& o, UBool asThawed); 01574 01575 //---------------------------------------------------------------- 01576 // Implementation: Pattern parsing 01577 //---------------------------------------------------------------- 01578 01579 void applyPatternIgnoreSpace(const UnicodeString& pattern, 01580 ParsePosition& pos, 01581 const SymbolTable* symbols, 01582 UErrorCode& status); 01583 01584 void applyPattern(RuleCharacterIterator& chars, 01585 const SymbolTable* symbols, 01586 UnicodeString& rebuiltPat, 01587 uint32_t options, 01588 UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute), 01589 int32_t depth, 01590 UErrorCode& ec); 01591 01592 void closeOverCaseInsensitive(bool simple); 01593 void closeOverAddCaseMappings(); 01594 01595 //---------------------------------------------------------------- 01596 // Implementation: Utility methods 01597 //---------------------------------------------------------------- 01598 01599 static int32_t nextCapacity(int32_t minCapacity); 01600 01601 bool ensureCapacity(int32_t newLen); 01602 01603 bool ensureBufferCapacity(int32_t newLen); 01604 01605 void swapBuffers(void); 01606 01607 UBool allocateStrings(UErrorCode &status); 01608 int32_t stringsSize() const; 01609 UBool stringsContains(const UnicodeString &s) const; 01610 01611 UnicodeString& _toPattern(UnicodeString& result, 01612 UBool escapeUnprintable) const; 01613 01614 UnicodeString& _generatePattern(UnicodeString& result, 01615 UBool escapeUnprintable) const; 01616 01617 static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable); 01618 01619 static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable); 01620 01621 static void _appendToPat(UnicodeString &result, UChar32 start, UChar32 end, 01622 UBool escapeUnprintable); 01623 01624 //---------------------------------------------------------------- 01625 // Implementation: Fundamental operators 01626 //---------------------------------------------------------------- 01627 01628 void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity); 01629 01630 void add(const UChar32* other, int32_t otherLen, int8_t polarity); 01631 01632 void retain(const UChar32* other, int32_t otherLen, int8_t polarity); 01633 01639 static UBool resemblesPropertyPattern(const UnicodeString& pattern, 01640 int32_t pos); 01641 01642 static UBool resemblesPropertyPattern(RuleCharacterIterator& chars, 01643 int32_t iterOpts); 01644 01684 UnicodeSet& applyPropertyPattern(const UnicodeString& pattern, 01685 ParsePosition& ppos, 01686 UErrorCode &ec); 01687 01688 void applyPropertyPattern(RuleCharacterIterator& chars, 01689 UnicodeString& rebuiltPat, 01690 UErrorCode& ec); 01691 01696 typedef UBool (*Filter)(UChar32 codePoint, void* context); 01697 01707 void applyFilter(Filter filter, 01708 void* context, 01709 const UnicodeSet* inclusions, 01710 UErrorCode &status); 01711 01715 void setPattern(const UnicodeString& newPat) { 01716 setPattern(newPat.getBuffer(), newPat.length()); 01717 } 01718 void setPattern(const char16_t *newPat, int32_t newPatLen); 01722 void releasePattern(); 01723 01724 friend class UnicodeSetIterator; 01725 }; 01726 01727 01728 01729 inline bool UnicodeSet::operator!=(const UnicodeSet& o) const { 01730 return !operator==(o); 01731 } 01732 01733 inline UBool UnicodeSet::isFrozen() const { 01734 return (UBool)(bmpSet!=nullptr || stringSpan!=nullptr); 01735 } 01736 01737 inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const { 01738 return !containsNone(start, end); 01739 } 01740 01741 inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const { 01742 return !containsNone(s); 01743 } 01744 01745 inline UBool UnicodeSet::containsSome(const UnicodeString& s) const { 01746 return !containsNone(s); 01747 } 01748 01749 inline UBool UnicodeSet::isBogus() const { 01750 return (UBool)(fFlags & kIsBogus); 01751 } 01752 01753 inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) { 01754 return reinterpret_cast<UnicodeSet *>(uset); 01755 } 01756 01757 inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) { 01758 return reinterpret_cast<const UnicodeSet *>(uset); 01759 } 01760 01761 inline USet *UnicodeSet::toUSet() { 01762 return reinterpret_cast<USet *>(this); 01763 } 01764 01765 inline const USet *UnicodeSet::toUSet() const { 01766 return reinterpret_cast<const USet *>(this); 01767 } 01768 01769 inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const { 01770 int32_t sLength=s.length(); 01771 if(start<0) { 01772 start=0; 01773 } else if(start>sLength) { 01774 start=sLength; 01775 } 01776 return start+span(s.getBuffer()+start, sLength-start, spanCondition); 01777 } 01778 01779 inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const { 01780 int32_t sLength=s.length(); 01781 if(limit<0) { 01782 limit=0; 01783 } else if(limit>sLength) { 01784 limit=sLength; 01785 } 01786 return spanBack(s.getBuffer(), limit, spanCondition); 01787 } 01788 01789 U_NAMESPACE_END 01790 01791 #endif /* U_SHOW_CPLUSPLUS_API */ 01792 01793 #endif
1.7.6.1