ICU 70.1  70.1
uniset.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ***************************************************************************
5 * Copyright (C) 1999-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***************************************************************************
8 * Date Name Description
9 * 10/20/99 alan Creation.
10 ***************************************************************************
11 */
12 
13 #ifndef UNICODESET_H
14 #define UNICODESET_H
15 
16 #include "unicode/utypes.h"
17 
18 #if U_SHOW_CPLUSPLUS_API
19 
20 #include "unicode/ucpmap.h"
21 #include "unicode/unifilt.h"
22 #include "unicode/unistr.h"
23 #include "unicode/uset.h"
24 
30 U_NAMESPACE_BEGIN
31 
32 // Forward Declarations.
33 class BMPSet;
34 class ParsePosition;
35 class RBBIRuleScanner;
36 class SymbolTable;
37 class UnicodeSetStringSpan;
38 class UVector;
39 class RuleCharacterIterator;
40 
286 private:
291  static constexpr int32_t INITIAL_CAPACITY = 25;
292  // fFlags constant
293  static constexpr uint8_t kIsBogus = 1; // This set is bogus (i.e. not valid)
294 
295  UChar32* list = stackList; // MUST be terminated with HIGH
296  int32_t capacity = INITIAL_CAPACITY; // capacity of list
297  int32_t len = 1; // length of list used; 1 <= len <= capacity
298  uint8_t fFlags = 0; // Bit flag (see constants above)
299 
300  BMPSet *bmpSet = nullptr; // The set is frozen iff either bmpSet or stringSpan is not NULL.
301  UChar32* buffer = nullptr; // internal buffer, may be NULL
302  int32_t bufferCapacity = 0; // capacity of buffer
303 
313  char16_t *pat = nullptr;
314  int32_t patLen = 0;
315 
316  UVector* strings = nullptr; // maintained in sorted order
317  UnicodeSetStringSpan *stringSpan = nullptr;
318 
324  UChar32 stackList[INITIAL_CAPACITY];
325 
326 public:
336  inline UBool isBogus(void) const;
337 
354  void setToBogus();
355 
356 public:
357 
358  enum {
363  MIN_VALUE = 0,
364 
369  MAX_VALUE = 0x10ffff
370  };
371 
372  //----------------------------------------------------------------
373  // Constructors &c
374  //----------------------------------------------------------------
375 
376 public:
377 
382  UnicodeSet();
383 
392  UnicodeSet(UChar32 start, UChar32 end);
393 
394 #ifndef U_HIDE_INTERNAL_API
395 
399  kSerialized /* result of serialize() */
400  };
401 
412  UnicodeSet(const uint16_t buffer[], int32_t bufferLen,
413  ESerialization serialization, UErrorCode &status);
414 #endif /* U_HIDE_INTERNAL_API */
415 
424  UnicodeSet(const UnicodeString& pattern,
425  UErrorCode& status);
426 
427 #ifndef U_HIDE_INTERNAL_API
428 
440  UnicodeSet(const UnicodeString& pattern,
441  uint32_t options,
442  const SymbolTable* symbols,
443  UErrorCode& status);
444 #endif /* U_HIDE_INTERNAL_API */
445 
459  UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
460  uint32_t options,
461  const SymbolTable* symbols,
462  UErrorCode& status);
463 
468  UnicodeSet(const UnicodeSet& o);
469 
474  virtual ~UnicodeSet();
475 
481  UnicodeSet& operator=(const UnicodeSet& o);
482 
494  virtual bool operator==(const UnicodeSet& o) const;
495 
501  inline bool operator!=(const UnicodeSet& o) const;
502 
512  virtual UnicodeSet* clone() const override;
513 
521  virtual int32_t hashCode(void) const;
522 
531  inline static UnicodeSet *fromUSet(USet *uset);
532 
541  inline static const UnicodeSet *fromUSet(const USet *uset);
542 
550  inline USet *toUSet();
551 
552 
560  inline const USet * toUSet() const;
561 
562 
563  //----------------------------------------------------------------
564  // Freezable API
565  //----------------------------------------------------------------
566 
575  inline UBool isFrozen() const;
576 
590  UnicodeSet *freeze();
591 
600  UnicodeSet *cloneAsThawed() const;
601 
602  //----------------------------------------------------------------
603  // Public API
604  //----------------------------------------------------------------
605 
615  UnicodeSet& set(UChar32 start, UChar32 end);
616 
622  static UBool resemblesPattern(const UnicodeString& pattern,
623  int32_t pos);
624 
637  UnicodeSet& applyPattern(const UnicodeString& pattern,
638  UErrorCode& status);
639 
640 #ifndef U_HIDE_INTERNAL_API
641 
657  UnicodeSet& applyPattern(const UnicodeString& pattern,
658  uint32_t options,
659  const SymbolTable* symbols,
660  UErrorCode& status);
661 #endif /* U_HIDE_INTERNAL_API */
662 
694  UnicodeSet& applyPattern(const UnicodeString& pattern,
695  ParsePosition& pos,
696  uint32_t options,
697  const SymbolTable* symbols,
698  UErrorCode& status);
699 
713  virtual UnicodeString& toPattern(UnicodeString& result,
714  UBool escapeUnprintable = false) const override;
715 
738  UnicodeSet& applyIntPropertyValue(UProperty prop,
739  int32_t value,
740  UErrorCode& ec);
741 
771  UnicodeSet& applyPropertyAlias(const UnicodeString& prop,
772  const UnicodeString& value,
773  UErrorCode& ec);
774 
787  virtual int32_t size(void) const;
788 
795  virtual UBool isEmpty(void) const;
796 
797 #ifndef U_HIDE_DRAFT_API
798 
802  UBool hasStrings() const;
803 #endif // U_HIDE_DRAFT_API
804 
812  virtual UBool contains(UChar32 c) const override;
813 
822  virtual UBool contains(UChar32 start, UChar32 end) const;
823 
831  UBool contains(const UnicodeString& s) const;
832 
840  virtual UBool containsAll(const UnicodeSet& c) const;
841 
849  UBool containsAll(const UnicodeString& s) const;
850 
859  UBool containsNone(UChar32 start, UChar32 end) const;
860 
868  UBool containsNone(const UnicodeSet& c) const;
869 
877  UBool containsNone(const UnicodeString& s) const;
878 
887  inline UBool containsSome(UChar32 start, UChar32 end) const;
888 
896  inline UBool containsSome(const UnicodeSet& s) const;
897 
905  inline UBool containsSome(const UnicodeString& s) const;
906 
925  int32_t span(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
926 
939  inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const;
940 
958  int32_t spanBack(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
959 
973  inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const;
974 
993  int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
994 
1012  int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
1013 
1018  virtual UMatchDegree matches(const Replaceable& text,
1019  int32_t& offset,
1020  int32_t limit,
1021  UBool incremental) override;
1022 
1023 private:
1046  static int32_t matchRest(const Replaceable& text,
1047  int32_t start, int32_t limit,
1048  const UnicodeString& s);
1049 
1059  int32_t findCodePoint(UChar32 c) const;
1060 
1061 public:
1062 
1070  virtual void addMatchSetTo(UnicodeSet& toUnionTo) const override;
1071 
1080  int32_t indexOf(UChar32 c) const;
1081 
1097  UChar32 charAt(int32_t index) const;
1098 
1113  virtual UnicodeSet& add(UChar32 start, UChar32 end);
1114 
1125  UnicodeSet& add(UChar32 c);
1126 
1138  UnicodeSet& add(const UnicodeString& s);
1139 
1140  private:
1146  static int32_t getSingleCP(const UnicodeString& s);
1147 
1148  void _add(const UnicodeString& s);
1149 
1150  public:
1159  UnicodeSet& addAll(const UnicodeString& s);
1160 
1168  UnicodeSet& retainAll(const UnicodeString& s);
1169 
1177  UnicodeSet& complementAll(const UnicodeString& s);
1178 
1186  UnicodeSet& removeAll(const UnicodeString& s);
1187 
1196  static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s);
1197 
1198 
1206  static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s);
1207 
1219  virtual UnicodeSet& retain(UChar32 start, UChar32 end);
1220 
1221 
1230  UnicodeSet& retain(UChar32 c);
1231 
1232 #ifndef U_HIDE_DRAFT_API
1233 
1243  UnicodeSet& retain(const UnicodeString &s);
1244 #endif // U_HIDE_DRAFT_API
1245 
1259  virtual UnicodeSet& remove(UChar32 start, UChar32 end);
1260 
1271  UnicodeSet& remove(UChar32 c);
1272 
1282  UnicodeSet& remove(const UnicodeString& s);
1283 
1296  virtual UnicodeSet& complement();
1297 
1310  virtual UnicodeSet& complement(UChar32 start, UChar32 end);
1311 
1322  UnicodeSet& complement(UChar32 c);
1323 
1333  UnicodeSet& complement(const UnicodeString& s);
1334 
1347  virtual UnicodeSet& addAll(const UnicodeSet& c);
1348 
1360  virtual UnicodeSet& retainAll(const UnicodeSet& c);
1361 
1373  virtual UnicodeSet& removeAll(const UnicodeSet& c);
1374 
1385  virtual UnicodeSet& complementAll(const UnicodeSet& c);
1386 
1393  virtual UnicodeSet& clear(void);
1394 
1420  UnicodeSet& closeOver(int32_t attribute);
1421 
1428  virtual UnicodeSet &removeAllStrings();
1429 
1437  virtual int32_t getRangeCount(void) const;
1438 
1446  virtual UChar32 getRangeStart(int32_t index) const;
1447 
1455  virtual UChar32 getRangeEnd(int32_t index) const;
1456 
1505  int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const;
1506 
1513  virtual UnicodeSet& compact();
1514 
1526  static UClassID U_EXPORT2 getStaticClassID(void);
1527 
1536  virtual UClassID getDynamicClassID(void) const override;
1537 
1538 private:
1539 
1540  // Private API for the USet API
1541 
1542  friend class USetAccess;
1543 
1544  const UnicodeString* getString(int32_t index) const;
1545 
1546  //----------------------------------------------------------------
1547  // RuleBasedTransliterator support
1548  //----------------------------------------------------------------
1549 
1550 private:
1551 
1557  virtual UBool matchesIndexValue(uint8_t v) const override;
1558 
1559 private:
1560  friend class RBBIRuleScanner;
1561 
1562  //----------------------------------------------------------------
1563  // Implementation: Clone as thawed (see ICU4J Freezable)
1564  //----------------------------------------------------------------
1565 
1566  UnicodeSet(const UnicodeSet& o, UBool /* asThawed */);
1567  UnicodeSet& copyFrom(const UnicodeSet& o, UBool asThawed);
1568 
1569  //----------------------------------------------------------------
1570  // Implementation: Pattern parsing
1571  //----------------------------------------------------------------
1572 
1573  void applyPatternIgnoreSpace(const UnicodeString& pattern,
1574  ParsePosition& pos,
1575  const SymbolTable* symbols,
1576  UErrorCode& status);
1577 
1578  void applyPattern(RuleCharacterIterator& chars,
1579  const SymbolTable* symbols,
1580  UnicodeString& rebuiltPat,
1581  uint32_t options,
1582  UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute),
1583  int32_t depth,
1584  UErrorCode& ec);
1585 
1586  //----------------------------------------------------------------
1587  // Implementation: Utility methods
1588  //----------------------------------------------------------------
1589 
1590  static int32_t nextCapacity(int32_t minCapacity);
1591 
1592  bool ensureCapacity(int32_t newLen);
1593 
1594  bool ensureBufferCapacity(int32_t newLen);
1595 
1596  void swapBuffers(void);
1597 
1598  UBool allocateStrings(UErrorCode &status);
1599  int32_t stringsSize() const;
1600  UBool stringsContains(const UnicodeString &s) const;
1601 
1602  UnicodeString& _toPattern(UnicodeString& result,
1603  UBool escapeUnprintable) const;
1604 
1605  UnicodeString& _generatePattern(UnicodeString& result,
1606  UBool escapeUnprintable) const;
1607 
1608  static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable);
1609 
1610  static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable);
1611 
1612  static void _appendToPat(UnicodeString &result, UChar32 start, UChar32 end,
1613  UBool escapeUnprintable);
1614 
1615  //----------------------------------------------------------------
1616  // Implementation: Fundamental operators
1617  //----------------------------------------------------------------
1618 
1619  void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity);
1620 
1621  void add(const UChar32* other, int32_t otherLen, int8_t polarity);
1622 
1623  void retain(const UChar32* other, int32_t otherLen, int8_t polarity);
1624 
1630  static UBool resemblesPropertyPattern(const UnicodeString& pattern,
1631  int32_t pos);
1632 
1633  static UBool resemblesPropertyPattern(RuleCharacterIterator& chars,
1634  int32_t iterOpts);
1635 
1675  UnicodeSet& applyPropertyPattern(const UnicodeString& pattern,
1676  ParsePosition& ppos,
1677  UErrorCode &ec);
1678 
1679  void applyPropertyPattern(RuleCharacterIterator& chars,
1680  UnicodeString& rebuiltPat,
1681  UErrorCode& ec);
1682 
1683  static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status);
1684 
1689  typedef UBool (*Filter)(UChar32 codePoint, void* context);
1690 
1700  void applyFilter(Filter filter,
1701  void* context,
1702  const UnicodeSet* inclusions,
1703  UErrorCode &status);
1704 
1705  // UCPMap is now stable ICU 63
1706  void applyIntPropertyValue(const UCPMap *map,
1707  UCPMapValueFilter *filter, const void *context,
1708  UErrorCode &errorCode);
1709 
1713  void setPattern(const UnicodeString& newPat) {
1714  setPattern(newPat.getBuffer(), newPat.length());
1715  }
1716  void setPattern(const char16_t *newPat, int32_t newPatLen);
1720  void releasePattern();
1721 
1722  friend class UnicodeSetIterator;
1723 };
1724 
1725 
1726 
1727 inline bool UnicodeSet::operator!=(const UnicodeSet& o) const {
1728  return !operator==(o);
1729 }
1730 
1731 inline UBool UnicodeSet::isFrozen() const {
1732  return (UBool)(bmpSet!=NULL || stringSpan!=NULL);
1733 }
1734 
1735 inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const {
1736  return !containsNone(start, end);
1737 }
1738 
1739 inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const {
1740  return !containsNone(s);
1741 }
1742 
1743 inline UBool UnicodeSet::containsSome(const UnicodeString& s) const {
1744  return !containsNone(s);
1745 }
1746 
1747 inline UBool UnicodeSet::isBogus() const {
1748  return (UBool)(fFlags & kIsBogus);
1749 }
1750 
1751 inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) {
1752  return reinterpret_cast<UnicodeSet *>(uset);
1753 }
1754 
1755 inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) {
1756  return reinterpret_cast<const UnicodeSet *>(uset);
1757 }
1758 
1759 inline USet *UnicodeSet::toUSet() {
1760  return reinterpret_cast<USet *>(this);
1761 }
1762 
1763 inline const USet *UnicodeSet::toUSet() const {
1764  return reinterpret_cast<const USet *>(this);
1765 }
1766 
1767 inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const {
1768  int32_t sLength=s.length();
1769  if(start<0) {
1770  start=0;
1771  } else if(start>sLength) {
1772  start=sLength;
1773  }
1774  return start+span(s.getBuffer()+start, sLength-start, spanCondition);
1775 }
1776 
1777 inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const {
1778  int32_t sLength=s.length();
1779  if(limit<0) {
1780  limit=0;
1781  } else if(limit>sLength) {
1782  limit=sLength;
1783  }
1784  return spanBack(s.getBuffer(), limit, spanCondition);
1785 }
1786 
1787 U_NAMESPACE_END
1788 
1789 #endif /* U_SHOW_CPLUSPLUS_API */
1790 
1791 #endif
U_FINAL
#define U_FINAL
Definition: umachine.h:141
icu::UnicodeString::getBuffer
char16_t * getBuffer(int32_t minCapacity)
Get a read/write pointer to the internal buffer.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::UnicodeSet
A mutable set of Unicode characters and multicharacter strings.
Definition: uniset.h:285
icu::UnicodeSet::ESerialization
ESerialization
Definition: uniset.h:398
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
icu::Replaceable
Replaceable is an abstract base class representing a string of characters that supports the replaceme...
Definition: rep.h:77
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::UnicodeMatcher::matchesIndexValue
virtual UBool matchesIndexValue(uint8_t v) const =0
Returns true if this matcher will match a character c, where c & 0xFF == v, at offset,...
U_COMMON_API
#define U_COMMON_API
Definition: utypes.h:300
UProperty
UProperty
Selection constants for Unicode properties.
Definition: uchar.h:195
icu::UnicodeFilter::matches
virtual UMatchDegree matches(const Replaceable &text, int32_t &offset, int32_t limit, UBool incremental) override
Implement UnicodeMatcher API.
icu::SymbolTable
An interface that defines both lookup protocol and parsing of symbolic names.
Definition: symtable.h:59
USetSpanCondition
USetSpanCondition
Argument values for whether span() and similar functions continue while the current character is cont...
Definition: uset.h:159
icu::UnicodeString::length
int32_t length(void) const
Return the length of the UnicodeString object.
Definition: unistr.h:3890
NULL
#define NULL
Definition: utypes.h:188
icu::UnicodeFilter::getStaticClassID
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
icu::UnicodeFilter::clone
virtual UnicodeFilter * clone() const override=0
Clones this object polymorphically.
unifilt.h
C++ API: Unicode Filter.
icu::UnicodeFunctor::getDynamicClassID
virtual UClassID getDynamicClassID(void) const override=0
Returns a unique class ID polymorphically.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:467
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
UCPMapValueFilter
uint32_t UCPMapValueFilter(const void *context, uint32_t value)
Callback function type: Modifies a map value.
Definition: ucpmap.h:114
UCPMap
struct UCPMap UCPMap
Abstract map from Unicode code points (U+0000..U+10FFFF) to integer values.
Definition: ucpmap.h:31
icu::UMatchDegree
UMatchDegree
Constants returned by UnicodeMatcher::matches() indicating the degree of match.
Definition: unimatch.h:33
icu::UnicodeFilter::contains
virtual UBool contains(UChar32 c) const =0
Returns true for characters that are in the selected subset.
ucpmap.h
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
uset.h
C API: Unicode Set.
icu::UnicodeFilter
UnicodeFilter defines a protocol for selecting a subset of the full range (U+0000 to U+10FFFF) of Uni...
Definition: unifilt.h:65
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
icu::UnicodeMatcher::toPattern
virtual UnicodeString & toPattern(UnicodeString &result, UBool escapeUnprintable=false) const =0
Returns a string representation of this matcher.
USet
struct USet USet
USet is the C API type corresponding to C++ class UnicodeSet.
Definition: uset.h:50
icu::UnicodeMatcher::addMatchSetTo
virtual void addMatchSetTo(UnicodeSet &toUnionTo) const =0
Union the set of all characters that may be matched by this object into the given set.
unistr.h
C++ API: Unicode String.
INITIAL_CAPACITY
#define INITIAL_CAPACITY
The initial size of an array if it is unspecified.
Definition: RunArrays.h:32