ICU 52.1  52.1
uniset.h
Go to the documentation of this file.
1 /*
2 ***************************************************************************
3 * Copyright (C) 1999-2013, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***************************************************************************
6 * Date Name Description
7 * 10/20/99 alan Creation.
8 ***************************************************************************
9 */
10 
11 #ifndef UNICODESET_H
12 #define UNICODESET_H
13 
14 #include "unicode/unifilt.h"
15 #include "unicode/unistr.h"
16 #include "unicode/uset.h"
17 
24 
25 // Forward Declarations.
26 void UnicodeSet_initInclusion(int32_t src, UErrorCode &status);
28 class BMPSet;
29 class ParsePosition;
30 class RBBIRuleScanner;
31 class SymbolTable;
32 class UnicodeSetStringSpan;
33 class UVector;
34 class RuleCharacterIterator;
35 
277 
278  int32_t len; // length of list used; 0 <= len <= capacity
279  int32_t capacity; // capacity of list
280  UChar32* list; // MUST be terminated with HIGH
281  BMPSet *bmpSet; // The set is frozen iff either bmpSet or stringSpan is not NULL.
282  UChar32* buffer; // internal buffer, may be NULL
283  int32_t bufferCapacity; // capacity of buffer
284  int32_t patLen;
285 
295  UChar *pat;
296  UVector* strings; // maintained in sorted order
297  UnicodeSetStringSpan *stringSpan;
298 
299 private:
300  enum { // constants
301  kIsBogus = 1 // This set is bogus (i.e. not valid)
302  };
303  uint8_t fFlags; // Bit flag (see constants above)
304 public:
314  inline UBool isBogus(void) const;
315 
332  void setToBogus();
333 
334 public:
335 
336  enum {
341  MIN_VALUE = 0,
342 
347  MAX_VALUE = 0x10ffff
348  };
349 
350  //----------------------------------------------------------------
351  // Constructors &c
352  //----------------------------------------------------------------
353 
354 public:
355 
360  UnicodeSet();
361 
370  UnicodeSet(UChar32 start, UChar32 end);
371 
380  UnicodeSet(const UnicodeString& pattern,
381  UErrorCode& status);
382 
383 #ifndef U_HIDE_INTERNAL_API
384 
396  UnicodeSet(const UnicodeString& pattern,
397  uint32_t options,
398  const SymbolTable* symbols,
399  UErrorCode& status);
400 #endif /* U_HIDE_INTERNAL_API */
401 
415  UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
416  uint32_t options,
417  const SymbolTable* symbols,
418  UErrorCode& status);
419 
424  UnicodeSet(const UnicodeSet& o);
425 
430  virtual ~UnicodeSet();
431 
437  UnicodeSet& operator=(const UnicodeSet& o);
438 
450  virtual UBool operator==(const UnicodeSet& o) const;
451 
457  UBool operator!=(const UnicodeSet& o) const;
458 
468  virtual UnicodeFunctor* clone() const;
469 
477  virtual int32_t hashCode(void) const;
478 
487  inline static UnicodeSet *fromUSet(USet *uset);
488 
497  inline static const UnicodeSet *fromUSet(const USet *uset);
498 
506  inline USet *toUSet();
507 
508 
516  inline const USet * toUSet() const;
517 
518 
519  //----------------------------------------------------------------
520  // Freezable API
521  //----------------------------------------------------------------
522 
531  inline UBool isFrozen() const;
532 
546  UnicodeFunctor *freeze();
547 
556  UnicodeFunctor *cloneAsThawed() const;
557 
558  //----------------------------------------------------------------
559  // Public API
560  //----------------------------------------------------------------
561 
572  UnicodeSet& set(UChar32 start, UChar32 end);
573 
579  static UBool resemblesPattern(const UnicodeString& pattern,
580  int32_t pos);
581 
594  UnicodeSet& applyPattern(const UnicodeString& pattern,
595  UErrorCode& status);
596 
597 #ifndef U_HIDE_INTERNAL_API
598 
614  UnicodeSet& applyPattern(const UnicodeString& pattern,
615  uint32_t options,
616  const SymbolTable* symbols,
617  UErrorCode& status);
618 #endif /* U_HIDE_INTERNAL_API */
619 
651  UnicodeSet& applyPattern(const UnicodeString& pattern,
652  ParsePosition& pos,
653  uint32_t options,
654  const SymbolTable* symbols,
655  UErrorCode& status);
656 
670  virtual UnicodeString& toPattern(UnicodeString& result,
671  UBool escapeUnprintable = FALSE) const;
672 
695  UnicodeSet& applyIntPropertyValue(UProperty prop,
696  int32_t value,
697  UErrorCode& ec);
698 
728  UnicodeSet& applyPropertyAlias(const UnicodeString& prop,
729  const UnicodeString& value,
730  UErrorCode& ec);
731 
740  virtual int32_t size(void) const;
741 
748  virtual UBool isEmpty(void) const;
749 
757  virtual UBool contains(UChar32 c) const;
758 
767  virtual UBool contains(UChar32 start, UChar32 end) const;
768 
776  UBool contains(const UnicodeString& s) const;
777 
785  virtual UBool containsAll(const UnicodeSet& c) const;
786 
794  UBool containsAll(const UnicodeString& s) const;
795 
804  UBool containsNone(UChar32 start, UChar32 end) const;
805 
813  UBool containsNone(const UnicodeSet& c) const;
814 
822  UBool containsNone(const UnicodeString& s) const;
823 
832  inline UBool containsSome(UChar32 start, UChar32 end) const;
833 
841  inline UBool containsSome(const UnicodeSet& s) const;
842 
850  inline UBool containsSome(const UnicodeString& s) const;
851 
870  int32_t span(const UChar *s, int32_t length, USetSpanCondition spanCondition) const;
871 
884  inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const;
885 
903  int32_t spanBack(const UChar *s, int32_t length, USetSpanCondition spanCondition) const;
904 
918  inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const;
919 
938  int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
939 
957  int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
958 
963  virtual UMatchDegree matches(const Replaceable& text,
964  int32_t& offset,
965  int32_t limit,
966  UBool incremental);
967 
968 private:
991  static int32_t matchRest(const Replaceable& text,
992  int32_t start, int32_t limit,
993  const UnicodeString& s);
994 
1004  int32_t findCodePoint(UChar32 c) const;
1005 
1006 public:
1007 
1015  virtual void addMatchSetTo(UnicodeSet& toUnionTo) const;
1016 
1025  int32_t indexOf(UChar32 c) const;
1026 
1036  UChar32 charAt(int32_t index) const;
1037 
1052  virtual UnicodeSet& add(UChar32 start, UChar32 end);
1053 
1061  UnicodeSet& add(UChar32 c);
1062 
1074  UnicodeSet& add(const UnicodeString& s);
1075 
1076  private:
1082  static int32_t getSingleCP(const UnicodeString& s);
1083 
1084  void _add(const UnicodeString& s);
1085 
1086  public:
1095  UnicodeSet& addAll(const UnicodeString& s);
1096 
1105  UnicodeSet& retainAll(const UnicodeString& s);
1106 
1115  UnicodeSet& complementAll(const UnicodeString& s);
1116 
1125  UnicodeSet& removeAll(const UnicodeString& s);
1126 
1135  static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s);
1136 
1137 
1145  static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s);
1146 
1160  virtual UnicodeSet& retain(UChar32 start, UChar32 end);
1161 
1162 
1168  UnicodeSet& retain(UChar32 c);
1169 
1183  virtual UnicodeSet& remove(UChar32 start, UChar32 end);
1184 
1192  UnicodeSet& remove(UChar32 c);
1193 
1203  UnicodeSet& remove(const UnicodeString& s);
1204 
1212  virtual UnicodeSet& complement(void);
1213 
1228  virtual UnicodeSet& complement(UChar32 start, UChar32 end);
1229 
1237  UnicodeSet& complement(UChar32 c);
1238 
1249  UnicodeSet& complement(const UnicodeString& s);
1250 
1263  virtual UnicodeSet& addAll(const UnicodeSet& c);
1264 
1276  virtual UnicodeSet& retainAll(const UnicodeSet& c);
1277 
1289  virtual UnicodeSet& removeAll(const UnicodeSet& c);
1290 
1301  virtual UnicodeSet& complementAll(const UnicodeSet& c);
1302 
1309  virtual UnicodeSet& clear(void);
1310 
1336  UnicodeSet& closeOver(int32_t attribute);
1337 
1344  virtual UnicodeSet &removeAllStrings();
1345 
1353  virtual int32_t getRangeCount(void) const;
1354 
1362  virtual UChar32 getRangeStart(int32_t index) const;
1363 
1371  virtual UChar32 getRangeEnd(int32_t index) const;
1372 
1421  int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const;
1422 
1429  virtual UnicodeSet& compact();
1430 
1442  static UClassID U_EXPORT2 getStaticClassID(void);
1443 
1452  virtual UClassID getDynamicClassID(void) const;
1453 
1454 private:
1455 
1456  // Private API for the USet API
1457 
1458  friend class USetAccess;
1459 
1460  int32_t getStringCount() const;
1461 
1462  const UnicodeString* getString(int32_t index) const;
1463 
1464  //----------------------------------------------------------------
1465  // RuleBasedTransliterator support
1466  //----------------------------------------------------------------
1467 
1468 private:
1469 
1475  virtual UBool matchesIndexValue(uint8_t v) const;
1476 
1477 private:
1478  friend class RBBIRuleScanner;
1479 
1480  //----------------------------------------------------------------
1481  // Implementation: Clone as thawed (see ICU4J Freezable)
1482  //----------------------------------------------------------------
1483 
1484  UnicodeSet(const UnicodeSet& o, UBool /* asThawed */);
1485 
1486  //----------------------------------------------------------------
1487  // Implementation: Pattern parsing
1488  //----------------------------------------------------------------
1489 
1490  void applyPatternIgnoreSpace(const UnicodeString& pattern,
1491  ParsePosition& pos,
1492  const SymbolTable* symbols,
1493  UErrorCode& status);
1494 
1495  void applyPattern(RuleCharacterIterator& chars,
1496  const SymbolTable* symbols,
1497  UnicodeString& rebuiltPat,
1498  uint32_t options,
1499  UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute),
1500  UErrorCode& ec);
1501 
1502  //----------------------------------------------------------------
1503  // Implementation: Utility methods
1504  //----------------------------------------------------------------
1505 
1506  void ensureCapacity(int32_t newLen, UErrorCode& ec);
1507 
1508  void ensureBufferCapacity(int32_t newLen, UErrorCode& ec);
1509 
1510  void swapBuffers(void);
1511 
1512  UBool allocateStrings(UErrorCode &status);
1513 
1514  UnicodeString& _toPattern(UnicodeString& result,
1515  UBool escapeUnprintable) const;
1516 
1517  UnicodeString& _generatePattern(UnicodeString& result,
1518  UBool escapeUnprintable) const;
1519 
1520  static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable);
1521 
1522  static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable);
1523 
1524  //----------------------------------------------------------------
1525  // Implementation: Fundamental operators
1526  //----------------------------------------------------------------
1527 
1528  void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity);
1529 
1530  void add(const UChar32* other, int32_t otherLen, int8_t polarity);
1531 
1532  void retain(const UChar32* other, int32_t otherLen, int8_t polarity);
1533 
1539  static UBool resemblesPropertyPattern(const UnicodeString& pattern,
1540  int32_t pos);
1541 
1542  static UBool resemblesPropertyPattern(RuleCharacterIterator& chars,
1543  int32_t iterOpts);
1544 
1584  UnicodeSet& applyPropertyPattern(const UnicodeString& pattern,
1585  ParsePosition& ppos,
1586  UErrorCode &ec);
1587 
1588  void applyPropertyPattern(RuleCharacterIterator& chars,
1589  UnicodeString& rebuiltPat,
1590  UErrorCode& ec);
1591 
1592  friend void UnicodeSet_initInclusion(int32_t src, UErrorCode &status);
1593  static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status);
1594 
1599  typedef UBool (*Filter)(UChar32 codePoint, void* context);
1600 
1610  void applyFilter(Filter filter,
1611  void* context,
1612  int32_t src,
1613  UErrorCode &status);
1614 
1618  void setPattern(const UnicodeString& newPat);
1622  void releasePattern();
1623 
1624  friend class UnicodeSetIterator;
1625 };
1626 
1627 
1628 
1629 inline UBool UnicodeSet::operator!=(const UnicodeSet& o) const {
1630  return !operator==(o);
1631 }
1632 
1633 inline UBool UnicodeSet::isFrozen() const {
1634  return (UBool)(bmpSet!=NULL || stringSpan!=NULL);
1635 }
1636 
1637 inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const {
1638  return !containsNone(start, end);
1639 }
1640 
1641 inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const {
1642  return !containsNone(s);
1643 }
1644 
1645 inline UBool UnicodeSet::containsSome(const UnicodeString& s) const {
1646  return !containsNone(s);
1647 }
1648 
1649 inline UBool UnicodeSet::isBogus() const {
1650  return (UBool)(fFlags & kIsBogus);
1651 }
1652 
1653 inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) {
1654  return reinterpret_cast<UnicodeSet *>(uset);
1655 }
1656 
1657 inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) {
1658  return reinterpret_cast<const UnicodeSet *>(uset);
1659 }
1660 
1661 inline USet *UnicodeSet::toUSet() {
1662  return reinterpret_cast<USet *>(this);
1663 }
1664 
1665 inline const USet *UnicodeSet::toUSet() const {
1666  return reinterpret_cast<const USet *>(this);
1667 }
1668 
1669 inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const {
1670  int32_t sLength=s.length();
1671  if(start<0) {
1672  start=0;
1673  } else if(start>sLength) {
1674  start=sLength;
1675  }
1676  return start+span(s.getBuffer()+start, sLength-start, spanCondition);
1677 }
1678 
1679 inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const {
1680  int32_t sLength=s.length();
1681  if(limit<0) {
1682  limit=0;
1683  } else if(limit>sLength) {
1684  limit=sLength;
1685  }
1686  return spanBack(s.getBuffer(), limit, spanCondition);
1687 }
1688 
1690 
1691 #endif