ICU 76.1  76.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 
285 class U_COMMON_API UnicodeSet final : public UnicodeFilter {
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 nullptr.
301  UChar32* buffer = nullptr; // internal buffer, may be nullptr
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() 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 
442  UnicodeSet(const UnicodeString& pattern,
443  uint32_t options,
444  const SymbolTable* symbols,
445  UErrorCode& status);
446 #endif /* U_HIDE_INTERNAL_API */
447 
463  UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
464  uint32_t options,
465  const SymbolTable* symbols,
466  UErrorCode& status);
467 
472  UnicodeSet(const UnicodeSet& o);
473 
478  virtual ~UnicodeSet();
479 
485  UnicodeSet& operator=(const UnicodeSet& o);
486 
498  virtual bool operator==(const UnicodeSet& o) const;
499 
505  inline bool operator!=(const UnicodeSet& o) const;
506 
516  virtual UnicodeSet* clone() const override;
517 
525  virtual int32_t hashCode() const;
526 
535  inline static UnicodeSet *fromUSet(USet *uset);
536 
545  inline static const UnicodeSet *fromUSet(const USet *uset);
546 
554  inline USet *toUSet();
555 
556 
564  inline const USet * toUSet() const;
565 
566 
567  //----------------------------------------------------------------
568  // Freezable API
569  //----------------------------------------------------------------
570 
579  inline UBool isFrozen() const;
580 
594  UnicodeSet *freeze();
595 
604  UnicodeSet *cloneAsThawed() const;
605 
606  //----------------------------------------------------------------
607  // Public API
608  //----------------------------------------------------------------
609 
619  UnicodeSet& set(UChar32 start, UChar32 end);
620 
626  static UBool resemblesPattern(const UnicodeString& pattern,
627  int32_t pos);
628 
641  UnicodeSet& applyPattern(const UnicodeString& pattern,
642  UErrorCode& status);
643 
644 #ifndef U_HIDE_INTERNAL_API
645 
663  UnicodeSet& applyPattern(const UnicodeString& pattern,
664  uint32_t options,
665  const SymbolTable* symbols,
666  UErrorCode& status);
667 #endif /* U_HIDE_INTERNAL_API */
668 
702  UnicodeSet& applyPattern(const UnicodeString& pattern,
703  ParsePosition& pos,
704  uint32_t options,
705  const SymbolTable* symbols,
706  UErrorCode& status);
707 
721  virtual UnicodeString& toPattern(UnicodeString& result,
722  UBool escapeUnprintable = false) const override;
723 
746  UnicodeSet& applyIntPropertyValue(UProperty prop,
747  int32_t value,
748  UErrorCode& ec);
749 
779  UnicodeSet& applyPropertyAlias(const UnicodeString& prop,
780  const UnicodeString& value,
781  UErrorCode& ec);
782 
795  virtual int32_t size() const;
796 
803  virtual UBool isEmpty() const;
804 
809  UBool hasStrings() const;
810 
818  virtual UBool contains(UChar32 c) const override;
819 
828  virtual UBool contains(UChar32 start, UChar32 end) const;
829 
837  UBool contains(const UnicodeString& s) const;
838 
846  virtual UBool containsAll(const UnicodeSet& c) const;
847 
855  UBool containsAll(const UnicodeString& s) const;
856 
865  UBool containsNone(UChar32 start, UChar32 end) const;
866 
874  UBool containsNone(const UnicodeSet& c) const;
875 
883  UBool containsNone(const UnicodeString& s) const;
884 
893  inline UBool containsSome(UChar32 start, UChar32 end) const;
894 
902  inline UBool containsSome(const UnicodeSet& s) const;
903 
911  inline UBool containsSome(const UnicodeString& s) const;
912 
931  int32_t span(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
932 
945  inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const;
946 
964  int32_t spanBack(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
965 
979  inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const;
980 
999  int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
1000 
1018  int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
1019 
1024  virtual UMatchDegree matches(const Replaceable& text,
1025  int32_t& offset,
1026  int32_t limit,
1027  UBool incremental) override;
1028 
1029 private:
1052  static int32_t matchRest(const Replaceable& text,
1053  int32_t start, int32_t limit,
1054  const UnicodeString& s);
1055 
1065  int32_t findCodePoint(UChar32 c) const;
1066 
1067 public:
1068 
1076  virtual void addMatchSetTo(UnicodeSet& toUnionTo) const override;
1077 
1086  int32_t indexOf(UChar32 c) const;
1087 
1103  UChar32 charAt(int32_t index) const;
1104 
1105 #ifndef U_HIDE_DRAFT_API
1106 
1123  inline U_HEADER_NESTED_NAMESPACE::USetCodePoints codePoints() const {
1124  return U_HEADER_NESTED_NAMESPACE::USetCodePoints(toUSet());
1125  }
1126 
1149  inline U_HEADER_NESTED_NAMESPACE::USetRanges ranges() const {
1150  return U_HEADER_NESTED_NAMESPACE::USetRanges(toUSet());
1151  }
1152 
1173  inline U_HEADER_NESTED_NAMESPACE::USetStrings strings() const {
1174  return U_HEADER_NESTED_NAMESPACE::USetStrings(toUSet());
1175  }
1176 
1200  inline U_HEADER_NESTED_NAMESPACE::USetElementIterator begin() const {
1201  return U_HEADER_NESTED_NAMESPACE::USetElements(toUSet()).begin();
1202  }
1203 
1212  inline U_HEADER_NESTED_NAMESPACE::USetElementIterator end() const {
1213  return U_HEADER_NESTED_NAMESPACE::USetElements(toUSet()).end();
1214  }
1215 #endif // U_HIDE_DRAFT_API
1216 
1231  virtual UnicodeSet& add(UChar32 start, UChar32 end);
1232 
1243  UnicodeSet& add(UChar32 c);
1244 
1256  UnicodeSet& add(const UnicodeString& s);
1257 
1258  private:
1264  static int32_t getSingleCP(const UnicodeString& s);
1265 
1266  void _add(const UnicodeString& s);
1267 
1268  public:
1277  UnicodeSet& addAll(const UnicodeString& s);
1278 
1286  UnicodeSet& retainAll(const UnicodeString& s);
1287 
1295  UnicodeSet& complementAll(const UnicodeString& s);
1296 
1304  UnicodeSet& removeAll(const UnicodeString& s);
1305 
1314  static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s);
1315 
1316 
1324  static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s);
1325 
1337  virtual UnicodeSet& retain(UChar32 start, UChar32 end);
1338 
1339 
1348  UnicodeSet& retain(UChar32 c);
1349 
1360  UnicodeSet& retain(const UnicodeString &s);
1361 
1375  virtual UnicodeSet& remove(UChar32 start, UChar32 end);
1376 
1387  UnicodeSet& remove(UChar32 c);
1388 
1398  UnicodeSet& remove(const UnicodeString& s);
1399 
1412  virtual UnicodeSet& complement();
1413 
1426  virtual UnicodeSet& complement(UChar32 start, UChar32 end);
1427 
1438  UnicodeSet& complement(UChar32 c);
1439 
1449  UnicodeSet& complement(const UnicodeString& s);
1450 
1463  virtual UnicodeSet& addAll(const UnicodeSet& c);
1464 
1476  virtual UnicodeSet& retainAll(const UnicodeSet& c);
1477 
1489  virtual UnicodeSet& removeAll(const UnicodeSet& c);
1490 
1501  virtual UnicodeSet& complementAll(const UnicodeSet& c);
1502 
1509  virtual UnicodeSet& clear();
1510 
1538  UnicodeSet& closeOver(int32_t attribute);
1539 
1546  virtual UnicodeSet &removeAllStrings();
1547 
1555  virtual int32_t getRangeCount() const;
1556 
1564  virtual UChar32 getRangeStart(int32_t index) const;
1565 
1573  virtual UChar32 getRangeEnd(int32_t index) const;
1574 
1623  int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const;
1624 
1631  virtual UnicodeSet& compact();
1632 
1644  static UClassID U_EXPORT2 getStaticClassID();
1645 
1654  virtual UClassID getDynamicClassID() const override;
1655 
1656  private:
1657 
1658  // Private API for the USet API
1659 
1660  friend class USetAccess;
1661 
1662  const UnicodeString* getString(int32_t index) const;
1663 
1664  //----------------------------------------------------------------
1665  // RuleBasedTransliterator support
1666  //----------------------------------------------------------------
1667 
1668 private:
1669 
1675  virtual UBool matchesIndexValue(uint8_t v) const override;
1676 
1677 private:
1678  friend class RBBIRuleScanner;
1679 
1680  //----------------------------------------------------------------
1681  // Implementation: Clone as thawed (see ICU4J Freezable)
1682  //----------------------------------------------------------------
1683 
1684  UnicodeSet(const UnicodeSet& o, UBool /* asThawed */);
1685  UnicodeSet& copyFrom(const UnicodeSet& o, UBool asThawed);
1686 
1687  //----------------------------------------------------------------
1688  // Implementation: Pattern parsing
1689  //----------------------------------------------------------------
1690 
1691  void applyPatternIgnoreSpace(const UnicodeString& pattern,
1692  ParsePosition& pos,
1693  const SymbolTable* symbols,
1694  UErrorCode& status);
1695 
1696  void applyPattern(RuleCharacterIterator& chars,
1697  const SymbolTable* symbols,
1698  UnicodeString& rebuiltPat,
1699  uint32_t options,
1700  UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute),
1701  int32_t depth,
1702  UErrorCode& ec);
1703 
1704  void closeOverCaseInsensitive(bool simple);
1705  void closeOverAddCaseMappings();
1706 
1707  //----------------------------------------------------------------
1708  // Implementation: Utility methods
1709  //----------------------------------------------------------------
1710 
1711  static int32_t nextCapacity(int32_t minCapacity);
1712 
1713  bool ensureCapacity(int32_t newLen);
1714 
1715  bool ensureBufferCapacity(int32_t newLen);
1716 
1717  void swapBuffers();
1718 
1719  UBool allocateStrings(UErrorCode &status);
1720  int32_t stringsSize() const;
1721  UBool stringsContains(const UnicodeString &s) const;
1722 
1723  UnicodeString& _toPattern(UnicodeString& result,
1724  UBool escapeUnprintable) const;
1725 
1726  UnicodeString& _generatePattern(UnicodeString& result,
1727  UBool escapeUnprintable) const;
1728 
1729  static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable);
1730 
1731  static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable);
1732 
1733  static void _appendToPat(UnicodeString &result, UChar32 start, UChar32 end,
1734  UBool escapeUnprintable);
1735 
1736  //----------------------------------------------------------------
1737  // Implementation: Fundamental operators
1738  //----------------------------------------------------------------
1739 
1740  void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity);
1741 
1742  void add(const UChar32* other, int32_t otherLen, int8_t polarity);
1743 
1744  void retain(const UChar32* other, int32_t otherLen, int8_t polarity);
1745 
1751  static UBool resemblesPropertyPattern(const UnicodeString& pattern,
1752  int32_t pos);
1753 
1754  static UBool resemblesPropertyPattern(RuleCharacterIterator& chars,
1755  int32_t iterOpts);
1756 
1796  UnicodeSet& applyPropertyPattern(const UnicodeString& pattern,
1797  ParsePosition& ppos,
1798  UErrorCode &ec);
1799 
1800  void applyPropertyPattern(RuleCharacterIterator& chars,
1801  UnicodeString& rebuiltPat,
1802  UErrorCode& ec);
1803 
1808  typedef UBool (*Filter)(UChar32 codePoint, void* context);
1809 
1819  void applyFilter(Filter filter,
1820  void* context,
1821  const UnicodeSet* inclusions,
1822  UErrorCode &status);
1823 
1827  void setPattern(const UnicodeString& newPat) {
1828  setPattern(newPat.getBuffer(), newPat.length());
1829  }
1830  void setPattern(const char16_t *newPat, int32_t newPatLen);
1834  void releasePattern();
1835 
1836  friend class UnicodeSetIterator;
1837 };
1838 
1839 
1840 
1841 inline bool UnicodeSet::operator!=(const UnicodeSet& o) const {
1842  return !operator==(o);
1843 }
1844 
1845 inline UBool UnicodeSet::isFrozen() const {
1846  return bmpSet != nullptr || stringSpan != nullptr;
1847 }
1848 
1849 inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const {
1850  return !containsNone(start, end);
1851 }
1852 
1853 inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const {
1854  return !containsNone(s);
1855 }
1856 
1857 inline UBool UnicodeSet::containsSome(const UnicodeString& s) const {
1858  return !containsNone(s);
1859 }
1860 
1861 inline UBool UnicodeSet::isBogus() const {
1862  return fFlags & kIsBogus;
1863 }
1864 
1865 inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) {
1866  return reinterpret_cast<UnicodeSet *>(uset);
1867 }
1868 
1869 inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) {
1870  return reinterpret_cast<const UnicodeSet *>(uset);
1871 }
1872 
1873 inline USet *UnicodeSet::toUSet() {
1874  return reinterpret_cast<USet *>(this);
1875 }
1876 
1877 inline const USet *UnicodeSet::toUSet() const {
1878  return reinterpret_cast<const USet *>(this);
1879 }
1880 
1881 inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const {
1882  int32_t sLength=s.length();
1883  if(start<0) {
1884  start=0;
1885  } else if(start>sLength) {
1886  start=sLength;
1887  }
1888  return start+span(s.getBuffer()+start, sLength-start, spanCondition);
1889 }
1890 
1891 inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const {
1892  int32_t sLength=s.length();
1893  if(limit<0) {
1894  limit=0;
1895  } else if(limit>sLength) {
1896  limit=sLength;
1897  }
1898  return spanBack(s.getBuffer(), limit, spanCondition);
1899 }
1900 
1901 U_NAMESPACE_END
1902 
1903 #endif /* U_SHOW_CPLUSPLUS_API */
1904 
1905 #endif
#define INITIAL_CAPACITY
The initial size of an array if it is unspecified.
Definition: RunArrays.h:32
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:346
virtual UMatchDegree matches(const Replaceable &text, int32_t &offset, int32_t limit, UBool incremental) override
Implement UnicodeMatcher API.
static UClassID getStaticClassID()
ICU "poor man&#39;s RTTI", returns a UClassID for this class.
int32_t length() const
Return the length of the UnicodeString object.
Definition: unistr.h:4214
UMatchDegree
Constants returned by UnicodeMatcher::matches() indicating the degree of match.
Definition: unimatch.h:33
C++ API: Unicode String.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
C API: This file defines an abstract map from Unicode code points to integer values.
virtual UBool matchesIndexValue(uint8_t v) const =0
Returns true if this matcher will match a character c, where c & 0xFF == v, at offset, in the forward direction (with limit > offset).
C API: Unicode Set.
An interface that defines both lookup protocol and parsing of symbolic names.
Definition: symtable.h:59
virtual UnicodeString & toPattern(UnicodeString &result, UBool escapeUnprintable=false) const =0
Returns a string representation of this matcher.
Replaceable is an abstract base class representing a string of characters that supports the replaceme...
Definition: rep.h:77
UnicodeFilter defines a protocol for selecting a subset of the full range (U+0000 to U+10FFFF) of Uni...
Definition: unifilt.h:65
U_HEADER_NESTED_NAMESPACE::USetCodePoints codePoints() const
Returns a C++ "range" for iterating over the code points of this set.
Definition: uniset.h:1123
U_HEADER_NESTED_NAMESPACE::USetRanges ranges() const
Returns a C++ "range" for iterating over the code point ranges of this set.
Definition: uniset.h:1149
virtual void addMatchSetTo(UnicodeSet &toUnionTo) const =0
Union the set of all characters that may be matched by this object into the given set...
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:427
A mutable set of Unicode characters and multicharacter strings.
Definition: uniset.h:285
virtual UClassID getDynamicClassID() const override=0
Returns a unique class ID polymorphically.
USetSpanCondition
Argument values for whether span() and similar functions continue while the current character is cont...
Definition: uset.h:185
UProperty
Selection constants for Unicode properties.
Definition: uchar.h:196
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
struct USet USet
USet is the C API type corresponding to C++ class UnicodeSet.
Definition: uset.h:53
U_HEADER_NESTED_NAMESPACE::USetElementIterator end() const
Definition: uniset.h:1212
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
char16_t * getBuffer(int32_t minCapacity)
Get a read/write pointer to the internal buffer.
virtual UnicodeFilter * clone() const override=0
Clones this object polymorphically.
Basic definitions for ICU, for both C and C++ APIs.
U_HEADER_NESTED_NAMESPACE::USetElementIterator begin() const
Returns a C++ iterator for iterating over all of the elements of this set.
Definition: uniset.h:1200
virtual UBool contains(UChar32 c) const =0
Returns true for characters that are in the selected subset.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:315
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
C++ API: Unicode Filter.
U_HEADER_NESTED_NAMESPACE::USetStrings strings() const
Returns a C++ "range" for iterating over the empty and multi-character strings of this set...
Definition: uniset.h:1173
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247