uniset.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /*
  2. ***************************************************************************
  3. * Copyright (C) 1999-2016, International Business Machines Corporation
  4. * and others. All Rights Reserved.
  5. ***************************************************************************
  6. * Date Name Description
  7. * 10/20/99 alan Creation.
  8. ***************************************************************************
  9. */
  10. #ifndef UNICODESET_H
  11. #define UNICODESET_H
  12. #include "unicode/unifilt.h"
  13. #include "unicode/unistr.h"
  14. #include "unicode/uset.h"
  15. /**
  16. * \file
  17. * \brief C++ API: Unicode Set
  18. */
  19. U_NAMESPACE_BEGIN
  20. // Forward Declarations.
  21. void U_CALLCONV UnicodeSet_initInclusion(int32_t src, UErrorCode &status); /**< @internal */
  22. class BMPSet;
  23. class ParsePosition;
  24. class RBBIRuleScanner;
  25. class SymbolTable;
  26. class UnicodeSetStringSpan;
  27. class UVector;
  28. class RuleCharacterIterator;
  29. /**
  30. * A mutable set of Unicode characters and multicharacter strings. Objects of this class
  31. * represent <em>character classes</em> used in regular expressions.
  32. * A character specifies a subset of Unicode code points. Legal
  33. * code points are U+0000 to U+10FFFF, inclusive.
  34. *
  35. * <p>The UnicodeSet class is not designed to be subclassed.
  36. *
  37. * <p><code>UnicodeSet</code> supports two APIs. The first is the
  38. * <em>operand</em> API that allows the caller to modify the value of
  39. * a <code>UnicodeSet</code> object. It conforms to Java 2's
  40. * <code>java.util.Set</code> interface, although
  41. * <code>UnicodeSet</code> does not actually implement that
  42. * interface. All methods of <code>Set</code> are supported, with the
  43. * modification that they take a character range or single character
  44. * instead of an <code>Object</code>, and they take a
  45. * <code>UnicodeSet</code> instead of a <code>Collection</code>. The
  46. * operand API may be thought of in terms of boolean logic: a boolean
  47. * OR is implemented by <code>add</code>, a boolean AND is implemented
  48. * by <code>retain</code>, a boolean XOR is implemented by
  49. * <code>complement</code> taking an argument, and a boolean NOT is
  50. * implemented by <code>complement</code> with no argument. In terms
  51. * of traditional set theory function names, <code>add</code> is a
  52. * union, <code>retain</code> is an intersection, <code>remove</code>
  53. * is an asymmetric difference, and <code>complement</code> with no
  54. * argument is a set complement with respect to the superset range
  55. * <code>MIN_VALUE-MAX_VALUE</code>
  56. *
  57. * <p>The second API is the
  58. * <code>applyPattern()</code>/<code>toPattern()</code> API from the
  59. * <code>java.text.Format</code>-derived classes. Unlike the
  60. * methods that add characters, add categories, and control the logic
  61. * of the set, the method <code>applyPattern()</code> sets all
  62. * attributes of a <code>UnicodeSet</code> at once, based on a
  63. * string pattern.
  64. *
  65. * <p><b>Pattern syntax</b></p>
  66. *
  67. * Patterns are accepted by the constructors and the
  68. * <code>applyPattern()</code> methods and returned by the
  69. * <code>toPattern()</code> method. These patterns follow a syntax
  70. * similar to that employed by version 8 regular expression character
  71. * classes. Here are some simple examples:
  72. *
  73. * \htmlonly<blockquote>\endhtmlonly
  74. * <table>
  75. * <tr align="top">
  76. * <td nowrap valign="top" align="left"><code>[]</code></td>
  77. * <td valign="top">No characters</td>
  78. * </tr><tr align="top">
  79. * <td nowrap valign="top" align="left"><code>[a]</code></td>
  80. * <td valign="top">The character 'a'</td>
  81. * </tr><tr align="top">
  82. * <td nowrap valign="top" align="left"><code>[ae]</code></td>
  83. * <td valign="top">The characters 'a' and 'e'</td>
  84. * </tr>
  85. * <tr>
  86. * <td nowrap valign="top" align="left"><code>[a-e]</code></td>
  87. * <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code
  88. * point order</td>
  89. * </tr>
  90. * <tr>
  91. * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>
  92. * <td valign="top">The character U+4E01</td>
  93. * </tr>
  94. * <tr>
  95. * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>
  96. * <td valign="top">The character 'a' and the multicharacter strings &quot;ab&quot; and
  97. * &quot;ac&quot;</td>
  98. * </tr>
  99. * <tr>
  100. * <td nowrap valign="top" align="left"><code>[\\p{Lu}]</code></td>
  101. * <td valign="top">All characters in the general category Uppercase Letter</td>
  102. * </tr>
  103. * </table>
  104. * \htmlonly</blockquote>\endhtmlonly
  105. *
  106. * Any character may be preceded by a backslash in order to remove any special
  107. * meaning. White space characters, as defined by UCharacter.isWhitespace(), are
  108. * ignored, unless they are escaped.
  109. *
  110. * <p>Property patterns specify a set of characters having a certain
  111. * property as defined by the Unicode standard. Both the POSIX-like
  112. * "[:Lu:]" and the Perl-like syntax "\\p{Lu}" are recognized. For a
  113. * complete list of supported property patterns, see the User's Guide
  114. * for UnicodeSet at
  115. * <a href="http://icu-project.org/userguide/unicodeSet.html">
  116. * http://icu-project.org/userguide/unicodeSet.html</a>.
  117. * Actual determination of property data is defined by the underlying
  118. * Unicode database as implemented by UCharacter.
  119. *
  120. * <p>Patterns specify individual characters, ranges of characters, and
  121. * Unicode property sets. When elements are concatenated, they
  122. * specify their union. To complement a set, place a '^' immediately
  123. * after the opening '['. Property patterns are inverted by modifying
  124. * their delimiters; "[:^foo]" and "\\P{foo}". In any other location,
  125. * '^' has no special meaning.
  126. *
  127. * <p>Ranges are indicated by placing two a '-' between two
  128. * characters, as in "a-z". This specifies the range of all
  129. * characters from the left to the right, in Unicode order. If the
  130. * left character is greater than or equal to the
  131. * right character it is a syntax error. If a '-' occurs as the first
  132. * character after the opening '[' or '[^', or if it occurs as the
  133. * last character before the closing ']', then it is taken as a
  134. * literal. Thus "[a\-b]", "[-ab]", and "[ab-]" all indicate the same
  135. * set of three characters, 'a', 'b', and '-'.
  136. *
  137. * <p>Sets may be intersected using the '&' operator or the asymmetric
  138. * set difference may be taken using the '-' operator, for example,
  139. * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters
  140. * with values less than 4096. Operators ('&' and '|') have equal
  141. * precedence and bind left-to-right. Thus
  142. * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to
  143. * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]". This only really matters for
  144. * difference; intersection is commutative.
  145. *
  146. * <table>
  147. * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a'
  148. * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a'
  149. * through 'z' and all letters in between, in Unicode order
  150. * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing
  151. * all characters but 'a' through 'z',
  152. * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF
  153. * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code>
  154. * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em>
  155. * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code>
  156. * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em>
  157. * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code>
  158. * <td>The asymmetric difference of sets specified by <em>pat1</em> and
  159. * <em>pat2</em>
  160. * <tr valign=top><td nowrap><code>[:Lu:] or \\p{Lu}</code>
  161. * <td>The set of characters having the specified
  162. * Unicode property; in
  163. * this case, Unicode uppercase letters
  164. * <tr valign=top><td nowrap><code>[:^Lu:] or \\P{Lu}</code>
  165. * <td>The set of characters <em>not</em> having the given
  166. * Unicode property
  167. * </table>
  168. *
  169. * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>
  170. *
  171. * <p><b>Formal syntax</b></p>
  172. *
  173. * \htmlonly<blockquote>\endhtmlonly
  174. * <table>
  175. * <tr align="top">
  176. * <td nowrap valign="top" align="right"><code>pattern :=&nbsp; </code></td>
  177. * <td valign="top"><code>('[' '^'? item* ']') |
  178. * property</code></td>
  179. * </tr>
  180. * <tr align="top">
  181. * <td nowrap valign="top" align="right"><code>item :=&nbsp; </code></td>
  182. * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>
  183. * </code></td>
  184. * </tr>
  185. * <tr align="top">
  186. * <td nowrap valign="top" align="right"><code>pattern-expr :=&nbsp; </code></td>
  187. * <td valign="top"><code>pattern | pattern-expr pattern |
  188. * pattern-expr op pattern<br>
  189. * </code></td>
  190. * </tr>
  191. * <tr align="top">
  192. * <td nowrap valign="top" align="right"><code>op :=&nbsp; </code></td>
  193. * <td valign="top"><code>'&amp;' | '-'<br>
  194. * </code></td>
  195. * </tr>
  196. * <tr align="top">
  197. * <td nowrap valign="top" align="right"><code>special :=&nbsp; </code></td>
  198. * <td valign="top"><code>'[' | ']' | '-'<br>
  199. * </code></td>
  200. * </tr>
  201. * <tr align="top">
  202. * <td nowrap valign="top" align="right"><code>char :=&nbsp; </code></td>
  203. * <td valign="top"><em>any character that is not</em><code> special<br>
  204. * | ('\' </code><em>any character</em><code>)<br>
  205. * | ('\\u' hex hex hex hex)<br>
  206. * </code></td>
  207. * </tr>
  208. * <tr align="top">
  209. * <td nowrap valign="top" align="right"><code>hex :=&nbsp; </code></td>
  210. * <td valign="top"><em>any character for which
  211. * </em><code>Character.digit(c, 16)</code><em>
  212. * returns a non-negative result</em></td>
  213. * </tr>
  214. * <tr>
  215. * <td nowrap valign="top" align="right"><code>property :=&nbsp; </code></td>
  216. * <td valign="top"><em>a Unicode property set pattern</em></td>
  217. * </tr>
  218. * </table>
  219. * <br>
  220. * <table border="1">
  221. * <tr>
  222. * <td>Legend: <table>
  223. * <tr>
  224. * <td nowrap valign="top"><code>a := b</code></td>
  225. * <td width="20" valign="top">&nbsp; </td>
  226. * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>
  227. * </tr>
  228. * <tr>
  229. * <td nowrap valign="top"><code>a?</code></td>
  230. * <td valign="top"></td>
  231. * <td valign="top">zero or one instance of <code>a</code><br>
  232. * </td>
  233. * </tr>
  234. * <tr>
  235. * <td nowrap valign="top"><code>a*</code></td>
  236. * <td valign="top"></td>
  237. * <td valign="top">one or more instances of <code>a</code><br>
  238. * </td>
  239. * </tr>
  240. * <tr>
  241. * <td nowrap valign="top"><code>a | b</code></td>
  242. * <td valign="top"></td>
  243. * <td valign="top">either <code>a</code> or <code>b</code><br>
  244. * </td>
  245. * </tr>
  246. * <tr>
  247. * <td nowrap valign="top"><code>'a'</code></td>
  248. * <td valign="top"></td>
  249. * <td valign="top">the literal string between the quotes </td>
  250. * </tr>
  251. * </table>
  252. * </td>
  253. * </tr>
  254. * </table>
  255. * \htmlonly</blockquote>\endhtmlonly
  256. *
  257. * <p>Note:
  258. * - Most UnicodeSet methods do not take a UErrorCode parameter because
  259. * there are usually very few opportunities for failure other than a shortage
  260. * of memory, error codes in low-level C++ string methods would be inconvenient,
  261. * and the error code as the last parameter (ICU convention) would prevent
  262. * the use of default parameter values.
  263. * Instead, such methods set the UnicodeSet into a "bogus" state
  264. * (see isBogus()) if an error occurs.
  265. *
  266. * @author Alan Liu
  267. * @stable ICU 2.0
  268. */
  269. class U_COMMON_API UnicodeSet U_FINAL : public UnicodeFilter {
  270. int32_t len; // length of list used; 0 <= len <= capacity
  271. int32_t capacity; // capacity of list
  272. UChar32* list; // MUST be terminated with HIGH
  273. BMPSet *bmpSet; // The set is frozen iff either bmpSet or stringSpan is not NULL.
  274. UChar32* buffer; // internal buffer, may be NULL
  275. int32_t bufferCapacity; // capacity of buffer
  276. int32_t patLen;
  277. /**
  278. * The pattern representation of this set. This may not be the
  279. * most economical pattern. It is the pattern supplied to
  280. * applyPattern(), with variables substituted and whitespace
  281. * removed. For sets constructed without applyPattern(), or
  282. * modified using the non-pattern API, this string will be empty,
  283. * indicating that toPattern() must generate a pattern
  284. * representation from the inversion list.
  285. */
  286. UChar *pat;
  287. UVector* strings; // maintained in sorted order
  288. UnicodeSetStringSpan *stringSpan;
  289. private:
  290. enum { // constants
  291. kIsBogus = 1 // This set is bogus (i.e. not valid)
  292. };
  293. uint8_t fFlags; // Bit flag (see constants above)
  294. public:
  295. /**
  296. * Determine if this object contains a valid set.
  297. * A bogus set has no value. It is different from an empty set.
  298. * It can be used to indicate that no set value is available.
  299. *
  300. * @return TRUE if the set is bogus/invalid, FALSE otherwise
  301. * @see setToBogus()
  302. * @stable ICU 4.0
  303. */
  304. inline UBool isBogus(void) const;
  305. /**
  306. * Make this UnicodeSet object invalid.
  307. * The string will test TRUE with isBogus().
  308. *
  309. * A bogus set has no value. It is different from an empty set.
  310. * It can be used to indicate that no set value is available.
  311. *
  312. * This utility function is used throughout the UnicodeSet
  313. * implementation to indicate that a UnicodeSet operation failed,
  314. * and may be used in other functions,
  315. * especially but not exclusively when such functions do not
  316. * take a UErrorCode for simplicity.
  317. *
  318. * @see isBogus()
  319. * @stable ICU 4.0
  320. */
  321. void setToBogus();
  322. public:
  323. enum {
  324. /**
  325. * Minimum value that can be stored in a UnicodeSet.
  326. * @stable ICU 2.4
  327. */
  328. MIN_VALUE = 0,
  329. /**
  330. * Maximum value that can be stored in a UnicodeSet.
  331. * @stable ICU 2.4
  332. */
  333. MAX_VALUE = 0x10ffff
  334. };
  335. //----------------------------------------------------------------
  336. // Constructors &c
  337. //----------------------------------------------------------------
  338. public:
  339. /**
  340. * Constructs an empty set.
  341. * @stable ICU 2.0
  342. */
  343. UnicodeSet();
  344. /**
  345. * Constructs a set containing the given range. If <code>end >
  346. * start</code> then an empty set is created.
  347. *
  348. * @param start first character, inclusive, of range
  349. * @param end last character, inclusive, of range
  350. * @stable ICU 2.4
  351. */
  352. UnicodeSet(UChar32 start, UChar32 end);
  353. #ifndef U_HIDE_INTERNAL_API
  354. /**
  355. * @internal
  356. */
  357. enum ESerialization {
  358. kSerialized /* result of serialize() */
  359. };
  360. /**
  361. * Constructs a set from the output of serialize().
  362. *
  363. * @param buffer the 16 bit array
  364. * @param bufferLen the original length returned from serialize()
  365. * @param serialization the value 'kSerialized'
  366. * @param status error code
  367. *
  368. * @internal
  369. */
  370. UnicodeSet(const uint16_t buffer[], int32_t bufferLen,
  371. ESerialization serialization, UErrorCode &status);
  372. #endif /* U_HIDE_INTERNAL_API */
  373. /**
  374. * Constructs a set from the given pattern. See the class
  375. * description for the syntax of the pattern language.
  376. * @param pattern a string specifying what characters are in the set
  377. * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
  378. * contains a syntax error.
  379. * @stable ICU 2.0
  380. */
  381. UnicodeSet(const UnicodeString& pattern,
  382. UErrorCode& status);
  383. #ifndef U_HIDE_INTERNAL_API
  384. /**
  385. * Constructs a set from the given pattern. See the class
  386. * description for the syntax of the pattern language.
  387. * @param pattern a string specifying what characters are in the set
  388. * @param options bitmask for options to apply to the pattern.
  389. * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
  390. * @param symbols a symbol table mapping variable names to values
  391. * and stand-in characters to UnicodeSets; may be NULL
  392. * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
  393. * contains a syntax error.
  394. * @internal
  395. */
  396. UnicodeSet(const UnicodeString& pattern,
  397. uint32_t options,
  398. const SymbolTable* symbols,
  399. UErrorCode& status);
  400. #endif /* U_HIDE_INTERNAL_API */
  401. /**
  402. * Constructs a set from the given pattern. See the class description
  403. * for the syntax of the pattern language.
  404. * @param pattern a string specifying what characters are in the set
  405. * @param pos on input, the position in pattern at which to start parsing.
  406. * On output, the position after the last character parsed.
  407. * @param options bitmask for options to apply to the pattern.
  408. * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
  409. * @param symbols a symbol table mapping variable names to values
  410. * and stand-in characters to UnicodeSets; may be NULL
  411. * @param status input-output error code
  412. * @stable ICU 2.8
  413. */
  414. UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
  415. uint32_t options,
  416. const SymbolTable* symbols,
  417. UErrorCode& status);
  418. /**
  419. * Constructs a set that is identical to the given UnicodeSet.
  420. * @stable ICU 2.0
  421. */
  422. UnicodeSet(const UnicodeSet& o);
  423. /**
  424. * Destructs the set.
  425. * @stable ICU 2.0
  426. */
  427. virtual ~UnicodeSet();
  428. /**
  429. * Assigns this object to be a copy of another.
  430. * A frozen set will not be modified.
  431. * @stable ICU 2.0
  432. */
  433. UnicodeSet& operator=(const UnicodeSet& o);
  434. /**
  435. * Compares the specified object with this set for equality. Returns
  436. * <tt>true</tt> if the two sets
  437. * have the same size, and every member of the specified set is
  438. * contained in this set (or equivalently, every member of this set is
  439. * contained in the specified set).
  440. *
  441. * @param o set to be compared for equality with this set.
  442. * @return <tt>true</tt> if the specified set is equal to this set.
  443. * @stable ICU 2.0
  444. */
  445. virtual UBool operator==(const UnicodeSet& o) const;
  446. /**
  447. * Compares the specified object with this set for equality. Returns
  448. * <tt>true</tt> if the specified set is not equal to this set.
  449. * @stable ICU 2.0
  450. */
  451. UBool operator!=(const UnicodeSet& o) const;
  452. /**
  453. * Returns a copy of this object. All UnicodeFunctor objects have
  454. * to support cloning in order to allow classes using
  455. * UnicodeFunctors, such as Transliterator, to implement cloning.
  456. * If this set is frozen, then the clone will be frozen as well.
  457. * Use cloneAsThawed() for a mutable clone of a frozen set.
  458. * @see cloneAsThawed
  459. * @stable ICU 2.0
  460. */
  461. virtual UnicodeFunctor* clone() const;
  462. /**
  463. * Returns the hash code value for this set.
  464. *
  465. * @return the hash code value for this set.
  466. * @see Object#hashCode()
  467. * @stable ICU 2.0
  468. */
  469. virtual int32_t hashCode(void) const;
  470. /**
  471. * Get a UnicodeSet pointer from a USet
  472. *
  473. * @param uset a USet (the ICU plain C type for UnicodeSet)
  474. * @return the corresponding UnicodeSet pointer.
  475. *
  476. * @stable ICU 4.2
  477. */
  478. inline static UnicodeSet *fromUSet(USet *uset);
  479. /**
  480. * Get a UnicodeSet pointer from a const USet
  481. *
  482. * @param uset a const USet (the ICU plain C type for UnicodeSet)
  483. * @return the corresponding UnicodeSet pointer.
  484. *
  485. * @stable ICU 4.2
  486. */
  487. inline static const UnicodeSet *fromUSet(const USet *uset);
  488. /**
  489. * Produce a USet * pointer for this UnicodeSet.
  490. * USet is the plain C type for UnicodeSet
  491. *
  492. * @return a USet pointer for this UnicodeSet
  493. * @stable ICU 4.2
  494. */
  495. inline USet *toUSet();
  496. /**
  497. * Produce a const USet * pointer for this UnicodeSet.
  498. * USet is the plain C type for UnicodeSet
  499. *
  500. * @return a const USet pointer for this UnicodeSet
  501. * @stable ICU 4.2
  502. */
  503. inline const USet * toUSet() const;
  504. //----------------------------------------------------------------
  505. // Freezable API
  506. //----------------------------------------------------------------
  507. /**
  508. * Determines whether the set has been frozen (made immutable) or not.
  509. * See the ICU4J Freezable interface for details.
  510. * @return TRUE/FALSE for whether the set has been frozen
  511. * @see freeze
  512. * @see cloneAsThawed
  513. * @stable ICU 3.8
  514. */
  515. inline UBool isFrozen() const;
  516. /**
  517. * Freeze the set (make it immutable).
  518. * Once frozen, it cannot be unfrozen and is therefore thread-safe
  519. * until it is deleted.
  520. * See the ICU4J Freezable interface for details.
  521. * Freezing the set may also make some operations faster, for example
  522. * contains() and span().
  523. * A frozen set will not be modified. (It remains frozen.)
  524. * @return this set.
  525. * @see isFrozen
  526. * @see cloneAsThawed
  527. * @stable ICU 3.8
  528. */
  529. UnicodeFunctor *freeze();
  530. /**
  531. * Clone the set and make the clone mutable.
  532. * See the ICU4J Freezable interface for details.
  533. * @return the mutable clone
  534. * @see freeze
  535. * @see isFrozen
  536. * @stable ICU 3.8
  537. */
  538. UnicodeFunctor *cloneAsThawed() const;
  539. //----------------------------------------------------------------
  540. // Public API
  541. //----------------------------------------------------------------
  542. /**
  543. * Make this object represent the range <code>start - end</code>.
  544. * If <code>end > start</code> then this object is set to an
  545. * an empty range.
  546. * A frozen set will not be modified.
  547. *
  548. * @param start first character in the set, inclusive
  549. * @param end last character in the set, inclusive
  550. * @stable ICU 2.4
  551. */
  552. UnicodeSet& set(UChar32 start, UChar32 end);
  553. /**
  554. * Return true if the given position, in the given pattern, appears
  555. * to be the start of a UnicodeSet pattern.
  556. * @stable ICU 2.4
  557. */
  558. static UBool resemblesPattern(const UnicodeString& pattern,
  559. int32_t pos);
  560. /**
  561. * Modifies this set to represent the set specified by the given
  562. * pattern, ignoring Unicode Pattern_White_Space characters.
  563. * See the class description for the syntax of the pattern language.
  564. * A frozen set will not be modified.
  565. * @param pattern a string specifying what characters are in the set
  566. * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
  567. * contains a syntax error.
  568. * <em> Empties the set passed before applying the pattern.</em>
  569. * @return a reference to this
  570. * @stable ICU 2.0
  571. */
  572. UnicodeSet& applyPattern(const UnicodeString& pattern,
  573. UErrorCode& status);
  574. #ifndef U_HIDE_INTERNAL_API
  575. /**
  576. * Modifies this set to represent the set specified by the given
  577. * pattern, optionally ignoring Unicode Pattern_White_Space characters.
  578. * See the class description for the syntax of the pattern language.
  579. * A frozen set will not be modified.
  580. * @param pattern a string specifying what characters are in the set
  581. * @param options bitmask for options to apply to the pattern.
  582. * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
  583. * @param symbols a symbol table mapping variable names to
  584. * values and stand-ins to UnicodeSets; may be NULL
  585. * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
  586. * contains a syntax error.
  587. *<em> Empties the set passed before applying the pattern.</em>
  588. * @return a reference to this
  589. * @internal
  590. */
  591. UnicodeSet& applyPattern(const UnicodeString& pattern,
  592. uint32_t options,
  593. const SymbolTable* symbols,
  594. UErrorCode& status);
  595. #endif /* U_HIDE_INTERNAL_API */
  596. /**
  597. * Parses the given pattern, starting at the given position. The
  598. * character at pattern.charAt(pos.getIndex()) must be '[', or the
  599. * parse fails. Parsing continues until the corresponding closing
  600. * ']'. If a syntax error is encountered between the opening and
  601. * closing brace, the parse fails. Upon return from a successful
  602. * parse, the ParsePosition is updated to point to the character
  603. * following the closing ']', and a StringBuffer containing a
  604. * pairs list for the parsed pattern is returned. This method calls
  605. * itself recursively to parse embedded subpatterns.
  606. *<em> Empties the set passed before applying the pattern.</em>
  607. * A frozen set will not be modified.
  608. *
  609. * @param pattern the string containing the pattern to be parsed.
  610. * The portion of the string from pos.getIndex(), which must be a
  611. * '[', to the corresponding closing ']', is parsed.
  612. * @param pos upon entry, the position at which to being parsing.
  613. * The character at pattern.charAt(pos.getIndex()) must be a '['.
  614. * Upon return from a successful parse, pos.getIndex() is either
  615. * the character after the closing ']' of the parsed pattern, or
  616. * pattern.length() if the closing ']' is the last character of
  617. * the pattern string.
  618. * @param options bitmask for options to apply to the pattern.
  619. * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
  620. * @param symbols a symbol table mapping variable names to
  621. * values and stand-ins to UnicodeSets; may be NULL
  622. * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
  623. * contains a syntax error.
  624. * @return a reference to this
  625. * @stable ICU 2.8
  626. */
  627. UnicodeSet& applyPattern(const UnicodeString& pattern,
  628. ParsePosition& pos,
  629. uint32_t options,
  630. const SymbolTable* symbols,
  631. UErrorCode& status);
  632. /**
  633. * Returns a string representation of this set. If the result of
  634. * calling this function is passed to a UnicodeSet constructor, it
  635. * will produce another set that is equal to this one.
  636. * A frozen set will not be modified.
  637. * @param result the string to receive the rules. Previous
  638. * contents will be deleted.
  639. * @param escapeUnprintable if TRUE then convert unprintable
  640. * character to their hex escape representations, \\uxxxx or
  641. * \\Uxxxxxxxx. Unprintable characters are those other than
  642. * U+000A, U+0020..U+007E.
  643. * @stable ICU 2.0
  644. */
  645. virtual UnicodeString& toPattern(UnicodeString& result,
  646. UBool escapeUnprintable = FALSE) const;
  647. /**
  648. * Modifies this set to contain those code points which have the given value
  649. * for the given binary or enumerated property, as returned by
  650. * u_getIntPropertyValue. Prior contents of this set are lost.
  651. * A frozen set will not be modified.
  652. *
  653. * @param prop a property in the range UCHAR_BIN_START..UCHAR_BIN_LIMIT-1
  654. * or UCHAR_INT_START..UCHAR_INT_LIMIT-1
  655. * or UCHAR_MASK_START..UCHAR_MASK_LIMIT-1.
  656. *
  657. * @param value a value in the range u_getIntPropertyMinValue(prop)..
  658. * u_getIntPropertyMaxValue(prop), with one exception. If prop is
  659. * UCHAR_GENERAL_CATEGORY_MASK, then value should not be a UCharCategory, but
  660. * rather a mask value produced by U_GET_GC_MASK(). This allows grouped
  661. * categories such as [:L:] to be represented.
  662. *
  663. * @param ec error code input/output parameter
  664. *
  665. * @return a reference to this set
  666. *
  667. * @stable ICU 2.4
  668. */
  669. UnicodeSet& applyIntPropertyValue(UProperty prop,
  670. int32_t value,
  671. UErrorCode& ec);
  672. /**
  673. * Modifies this set to contain those code points which have the
  674. * given value for the given property. Prior contents of this
  675. * set are lost.
  676. * A frozen set will not be modified.
  677. *
  678. * @param prop a property alias, either short or long. The name is matched
  679. * loosely. See PropertyAliases.txt for names and a description of loose
  680. * matching. If the value string is empty, then this string is interpreted
  681. * as either a General_Category value alias, a Script value alias, a binary
  682. * property alias, or a special ID. Special IDs are matched loosely and
  683. * correspond to the following sets:
  684. *
  685. * "ANY" = [\\u0000-\\U0010FFFF],
  686. * "ASCII" = [\\u0000-\\u007F],
  687. * "Assigned" = [:^Cn:].
  688. *
  689. * @param value a value alias, either short or long. The name is matched
  690. * loosely. See PropertyValueAliases.txt for names and a description of
  691. * loose matching. In addition to aliases listed, numeric values and
  692. * canonical combining classes may be expressed numerically, e.g., ("nv",
  693. * "0.5") or ("ccc", "220"). The value string may also be empty.
  694. *
  695. * @param ec error code input/output parameter
  696. *
  697. * @return a reference to this set
  698. *
  699. * @stable ICU 2.4
  700. */
  701. UnicodeSet& applyPropertyAlias(const UnicodeString& prop,
  702. const UnicodeString& value,
  703. UErrorCode& ec);
  704. /**
  705. * Returns the number of elements in this set (its cardinality).
  706. * Note than the elements of a set may include both individual
  707. * codepoints and strings.
  708. *
  709. * @return the number of elements in this set (its cardinality).
  710. * @stable ICU 2.0
  711. */
  712. virtual int32_t size(void) const;
  713. /**
  714. * Returns <tt>true</tt> if this set contains no elements.
  715. *
  716. * @return <tt>true</tt> if this set contains no elements.
  717. * @stable ICU 2.0
  718. */
  719. virtual UBool isEmpty(void) const;
  720. /**
  721. * Returns true if this set contains the given character.
  722. * This function works faster with a frozen set.
  723. * @param c character to be checked for containment
  724. * @return true if the test condition is met
  725. * @stable ICU 2.0
  726. */
  727. virtual UBool contains(UChar32 c) const;
  728. /**
  729. * Returns true if this set contains every character
  730. * of the given range.
  731. * @param start first character, inclusive, of the range
  732. * @param end last character, inclusive, of the range
  733. * @return true if the test condition is met
  734. * @stable ICU 2.0
  735. */
  736. virtual UBool contains(UChar32 start, UChar32 end) const;
  737. /**
  738. * Returns <tt>true</tt> if this set contains the given
  739. * multicharacter string.
  740. * @param s string to be checked for containment
  741. * @return <tt>true</tt> if this set contains the specified string
  742. * @stable ICU 2.4
  743. */
  744. UBool contains(const UnicodeString& s) const;
  745. /**
  746. * Returns true if this set contains all the characters and strings
  747. * of the given set.
  748. * @param c set to be checked for containment
  749. * @return true if the test condition is met
  750. * @stable ICU 2.4
  751. */
  752. virtual UBool containsAll(const UnicodeSet& c) const;
  753. /**
  754. * Returns true if this set contains all the characters
  755. * of the given string.
  756. * @param s string containing characters to be checked for containment
  757. * @return true if the test condition is met
  758. * @stable ICU 2.4
  759. */
  760. UBool containsAll(const UnicodeString& s) const;
  761. /**
  762. * Returns true if this set contains none of the characters
  763. * of the given range.
  764. * @param start first character, inclusive, of the range
  765. * @param end last character, inclusive, of the range
  766. * @return true if the test condition is met
  767. * @stable ICU 2.4
  768. */
  769. UBool containsNone(UChar32 start, UChar32 end) const;
  770. /**
  771. * Returns true if this set contains none of the characters and strings
  772. * of the given set.
  773. * @param c set to be checked for containment
  774. * @return true if the test condition is met
  775. * @stable ICU 2.4
  776. */
  777. UBool containsNone(const UnicodeSet& c) const;
  778. /**
  779. * Returns true if this set contains none of the characters
  780. * of the given string.
  781. * @param s string containing characters to be checked for containment
  782. * @return true if the test condition is met
  783. * @stable ICU 2.4
  784. */
  785. UBool containsNone(const UnicodeString& s) const;
  786. /**
  787. * Returns true if this set contains one or more of the characters
  788. * in the given range.
  789. * @param start first character, inclusive, of the range
  790. * @param end last character, inclusive, of the range
  791. * @return true if the condition is met
  792. * @stable ICU 2.4
  793. */
  794. inline UBool containsSome(UChar32 start, UChar32 end) const;
  795. /**
  796. * Returns true if this set contains one or more of the characters
  797. * and strings of the given set.
  798. * @param s The set to be checked for containment
  799. * @return true if the condition is met
  800. * @stable ICU 2.4
  801. */
  802. inline UBool containsSome(const UnicodeSet& s) const;
  803. /**
  804. * Returns true if this set contains one or more of the characters
  805. * of the given string.
  806. * @param s string containing characters to be checked for containment
  807. * @return true if the condition is met
  808. * @stable ICU 2.4
  809. */
  810. inline UBool containsSome(const UnicodeString& s) const;
  811. /**
  812. * Returns the length of the initial substring of the input string which
  813. * consists only of characters and strings that are contained in this set
  814. * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE),
  815. * or only of characters and strings that are not contained
  816. * in this set (USET_SPAN_NOT_CONTAINED).
  817. * See USetSpanCondition for details.
  818. * Similar to the strspn() C library function.
  819. * Unpaired surrogates are treated according to contains() of their surrogate code points.
  820. * This function works faster with a frozen set and with a non-negative string length argument.
  821. * @param s start of the string
  822. * @param length of the string; can be -1 for NUL-terminated
  823. * @param spanCondition specifies the containment condition
  824. * @return the length of the initial substring according to the spanCondition;
  825. * 0 if the start of the string does not fit the spanCondition
  826. * @stable ICU 3.8
  827. * @see USetSpanCondition
  828. */
  829. int32_t span(const UChar *s, int32_t length, USetSpanCondition spanCondition) const;
  830. /**
  831. * Returns the end of the substring of the input string according to the USetSpanCondition.
  832. * Same as <code>start+span(s.getBuffer()+start, s.length()-start, spanCondition)</code>
  833. * after pinning start to 0<=start<=s.length().
  834. * @param s the string
  835. * @param start the start index in the string for the span operation
  836. * @param spanCondition specifies the containment condition
  837. * @return the exclusive end of the substring according to the spanCondition;
  838. * the substring s.tempSubStringBetween(start, end) fulfills the spanCondition
  839. * @stable ICU 4.4
  840. * @see USetSpanCondition
  841. */
  842. inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const;
  843. /**
  844. * Returns the start of the trailing substring of the input string which
  845. * consists only of characters and strings that are contained in this set
  846. * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE),
  847. * or only of characters and strings that are not contained
  848. * in this set (USET_SPAN_NOT_CONTAINED).
  849. * See USetSpanCondition for details.
  850. * Unpaired surrogates are treated according to contains() of their surrogate code points.
  851. * This function works faster with a frozen set and with a non-negative string length argument.
  852. * @param s start of the string
  853. * @param length of the string; can be -1 for NUL-terminated
  854. * @param spanCondition specifies the containment condition
  855. * @return the start of the trailing substring according to the spanCondition;
  856. * the string length if the end of the string does not fit the spanCondition
  857. * @stable ICU 3.8
  858. * @see USetSpanCondition
  859. */
  860. int32_t spanBack(const UChar *s, int32_t length, USetSpanCondition spanCondition) const;
  861. /**
  862. * Returns the start of the substring of the input string according to the USetSpanCondition.
  863. * Same as <code>spanBack(s.getBuffer(), limit, spanCondition)</code>
  864. * after pinning limit to 0<=end<=s.length().
  865. * @param s the string
  866. * @param limit the exclusive-end index in the string for the span operation
  867. * (use s.length() or INT32_MAX for spanning back from the end of the string)
  868. * @param spanCondition specifies the containment condition
  869. * @return the start of the substring according to the spanCondition;
  870. * the substring s.tempSubStringBetween(start, limit) fulfills the spanCondition
  871. * @stable ICU 4.4
  872. * @see USetSpanCondition
  873. */
  874. inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const;
  875. /**
  876. * Returns the length of the initial substring of the input string which
  877. * consists only of characters and strings that are contained in this set
  878. * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE),
  879. * or only of characters and strings that are not contained
  880. * in this set (USET_SPAN_NOT_CONTAINED).
  881. * See USetSpanCondition for details.
  882. * Similar to the strspn() C library function.
  883. * Malformed byte sequences are treated according to contains(0xfffd).
  884. * This function works faster with a frozen set and with a non-negative string length argument.
  885. * @param s start of the string (UTF-8)
  886. * @param length of the string; can be -1 for NUL-terminated
  887. * @param spanCondition specifies the containment condition
  888. * @return the length of the initial substring according to the spanCondition;
  889. * 0 if the start of the string does not fit the spanCondition
  890. * @stable ICU 3.8
  891. * @see USetSpanCondition
  892. */
  893. int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
  894. /**
  895. * Returns the start of the trailing substring of the input string which
  896. * consists only of characters and strings that are contained in this set
  897. * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE),
  898. * or only of characters and strings that are not contained
  899. * in this set (USET_SPAN_NOT_CONTAINED).
  900. * See USetSpanCondition for details.
  901. * Malformed byte sequences are treated according to contains(0xfffd).
  902. * This function works faster with a frozen set and with a non-negative string length argument.
  903. * @param s start of the string (UTF-8)
  904. * @param length of the string; can be -1 for NUL-terminated
  905. * @param spanCondition specifies the containment condition
  906. * @return the start of the trailing substring according to the spanCondition;
  907. * the string length if the end of the string does not fit the spanCondition
  908. * @stable ICU 3.8
  909. * @see USetSpanCondition
  910. */
  911. int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const;
  912. /**
  913. * Implement UnicodeMatcher::matches()
  914. * @stable ICU 2.4
  915. */
  916. virtual UMatchDegree matches(const Replaceable& text,
  917. int32_t& offset,
  918. int32_t limit,
  919. UBool incremental);
  920. private:
  921. /**
  922. * Returns the longest match for s in text at the given position.
  923. * If limit > start then match forward from start+1 to limit
  924. * matching all characters except s.charAt(0). If limit < start,
  925. * go backward starting from start-1 matching all characters
  926. * except s.charAt(s.length()-1). This method assumes that the
  927. * first character, text.charAt(start), matches s, so it does not
  928. * check it.
  929. * @param text the text to match
  930. * @param start the first character to match. In the forward
  931. * direction, text.charAt(start) is matched against s.charAt(0).
  932. * In the reverse direction, it is matched against
  933. * s.charAt(s.length()-1).
  934. * @param limit the limit offset for matching, either last+1 in
  935. * the forward direction, or last-1 in the reverse direction,
  936. * where last is the index of the last character to match.
  937. * @param s
  938. * @return If part of s matches up to the limit, return |limit -
  939. * start|. If all of s matches before reaching the limit, return
  940. * s.length(). If there is a mismatch between s and text, return
  941. * 0
  942. */
  943. static int32_t matchRest(const Replaceable& text,
  944. int32_t start, int32_t limit,
  945. const UnicodeString& s);
  946. /**
  947. * Returns the smallest value i such that c < list[i]. Caller
  948. * must ensure that c is a legal value or this method will enter
  949. * an infinite loop. This method performs a binary search.
  950. * @param c a character in the range MIN_VALUE..MAX_VALUE
  951. * inclusive
  952. * @return the smallest integer i in the range 0..len-1,
  953. * inclusive, such that c < list[i]
  954. */
  955. int32_t findCodePoint(UChar32 c) const;
  956. public:
  957. /**
  958. * Implementation of UnicodeMatcher API. Union the set of all
  959. * characters that may be matched by this object into the given
  960. * set.
  961. * @param toUnionTo the set into which to union the source characters
  962. * @stable ICU 2.4
  963. */
  964. virtual void addMatchSetTo(UnicodeSet& toUnionTo) const;
  965. /**
  966. * Returns the index of the given character within this set, where
  967. * the set is ordered by ascending code point. If the character
  968. * is not in this set, return -1. The inverse of this method is
  969. * <code>charAt()</code>.
  970. * @return an index from 0..size()-1, or -1
  971. * @stable ICU 2.4
  972. */
  973. int32_t indexOf(UChar32 c) const;
  974. /**
  975. * Returns the character at the given index within this set, where
  976. * the set is ordered by ascending code point. If the index is
  977. * out of range, return (UChar32)-1. The inverse of this method is
  978. * <code>indexOf()</code>.
  979. * @param index an index from 0..size()-1
  980. * @return the character at the given index, or (UChar32)-1.
  981. * @stable ICU 2.4
  982. */
  983. UChar32 charAt(int32_t index) const;
  984. /**
  985. * Adds the specified range to this set if it is not already
  986. * present. If this set already contains the specified range,
  987. * the call leaves this set unchanged. If <code>end > start</code>
  988. * then an empty range is added, leaving the set unchanged.
  989. * This is equivalent to a boolean logic OR, or a set UNION.
  990. * A frozen set will not be modified.
  991. *
  992. * @param start first character, inclusive, of range to be added
  993. * to this set.
  994. * @param end last character, inclusive, of range to be added
  995. * to this set.
  996. * @stable ICU 2.0
  997. */
  998. virtual UnicodeSet& add(UChar32 start, UChar32 end);
  999. /**
  1000. * Adds the specified character to this set if it is not already
  1001. * present. If this set already contains the specified character,
  1002. * the call leaves this set unchanged.
  1003. * A frozen set will not be modified.
  1004. * @stable ICU 2.0
  1005. */
  1006. UnicodeSet& add(UChar32 c);
  1007. /**
  1008. * Adds the specified multicharacter to this set if it is not already
  1009. * present. If this set already contains the multicharacter,
  1010. * the call leaves this set unchanged.
  1011. * Thus "ch" => {"ch"}
  1012. * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
  1013. * A frozen set will not be modified.
  1014. * @param s the source string
  1015. * @return this object, for chaining
  1016. * @stable ICU 2.4
  1017. */
  1018. UnicodeSet& add(const UnicodeString& s);
  1019. private:
  1020. /**
  1021. * @return a code point IF the string consists of a single one.
  1022. * otherwise returns -1.
  1023. * @param s string to test
  1024. */
  1025. static int32_t getSingleCP(const UnicodeString& s);
  1026. void _add(const UnicodeString& s);
  1027. public:
  1028. /**
  1029. * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
  1030. * If this set already any particular character, it has no effect on that character.
  1031. * A frozen set will not be modified.
  1032. * @param s the source string
  1033. * @return this object, for chaining
  1034. * @stable ICU 2.4
  1035. */
  1036. UnicodeSet& addAll(const UnicodeString& s);
  1037. /**
  1038. * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}
  1039. * If this set already any particular character, it has no effect on that character.
  1040. * A frozen set will not be modified.
  1041. * @param s the source string
  1042. * @return this object, for chaining
  1043. * @stable ICU 2.4
  1044. */
  1045. UnicodeSet& retainAll(const UnicodeString& s);
  1046. /**
  1047. * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}
  1048. * If this set already any particular character, it has no effect on that character.
  1049. * A frozen set will not be modified.
  1050. * @param s the source string
  1051. * @return this object, for chaining
  1052. * @stable ICU 2.4
  1053. */
  1054. UnicodeSet& complementAll(const UnicodeString& s);
  1055. /**
  1056. * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}
  1057. * If this set already any particular character, it has no effect on that character.
  1058. * A frozen set will not be modified.
  1059. * @param s the source string
  1060. * @return this object, for chaining
  1061. * @stable ICU 2.4
  1062. */
  1063. UnicodeSet& removeAll(const UnicodeString& s);
  1064. /**
  1065. * Makes a set from a multicharacter string. Thus "ch" => {"ch"}
  1066. * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
  1067. * @param s the source string
  1068. * @return a newly created set containing the given string.
  1069. * The caller owns the return object and is responsible for deleting it.
  1070. * @stable ICU 2.4
  1071. */
  1072. static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s);
  1073. /**
  1074. * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}
  1075. * @param s the source string
  1076. * @return a newly created set containing the given characters
  1077. * The caller owns the return object and is responsible for deleting it.
  1078. * @stable ICU 2.4
  1079. */
  1080. static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s);
  1081. /**
  1082. * Retain only the elements in this set that are contained in the
  1083. * specified range. If <code>end > start</code> then an empty range is
  1084. * retained, leaving the set empty. This is equivalent to
  1085. * a boolean logic AND, or a set INTERSECTION.
  1086. * A frozen set will not be modified.
  1087. *
  1088. * @param start first character, inclusive, of range to be retained
  1089. * to this set.
  1090. * @param end last character, inclusive, of range to be retained
  1091. * to this set.
  1092. * @stable ICU 2.0
  1093. */
  1094. virtual UnicodeSet& retain(UChar32 start, UChar32 end);
  1095. /**
  1096. * Retain the specified character from this set if it is present.
  1097. * A frozen set will not be modified.
  1098. * @stable ICU 2.0
  1099. */
  1100. UnicodeSet& retain(UChar32 c);
  1101. /**
  1102. * Removes the specified range from this set if it is present.
  1103. * The set will not contain the specified range once the call
  1104. * returns. If <code>end > start</code> then an empty range is
  1105. * removed, leaving the set unchanged.
  1106. * A frozen set will not be modified.
  1107. *
  1108. * @param start first character, inclusive, of range to be removed
  1109. * from this set.
  1110. * @param end last character, inclusive, of range to be removed
  1111. * from this set.
  1112. * @stable ICU 2.0
  1113. */
  1114. virtual UnicodeSet& remove(UChar32 start, UChar32 end);
  1115. /**
  1116. * Removes the specified character from this set if it is present.
  1117. * The set will not contain the specified range once the call
  1118. * returns.
  1119. * A frozen set will not be modified.
  1120. * @stable ICU 2.0
  1121. */
  1122. UnicodeSet& remove(UChar32 c);
  1123. /**
  1124. * Removes the specified string from this set if it is present.
  1125. * The set will not contain the specified character once the call
  1126. * returns.
  1127. * A frozen set will not be modified.
  1128. * @param s the source string
  1129. * @return this object, for chaining
  1130. * @stable ICU 2.4
  1131. */
  1132. UnicodeSet& remove(const UnicodeString& s);
  1133. /**
  1134. * Inverts this set. This operation modifies this set so that
  1135. * its value is its complement. This is equivalent to
  1136. * <code>complement(MIN_VALUE, MAX_VALUE)</code>.
  1137. * A frozen set will not be modified.
  1138. * @stable ICU 2.0
  1139. */
  1140. virtual UnicodeSet& complement(void);
  1141. /**
  1142. * Complements the specified range in this set. Any character in
  1143. * the range will be removed if it is in this set, or will be
  1144. * added if it is not in this set. If <code>end > start</code>
  1145. * then an empty range is complemented, leaving the set unchanged.
  1146. * This is equivalent to a boolean logic XOR.
  1147. * A frozen set will not be modified.
  1148. *
  1149. * @param start first character, inclusive, of range to be removed
  1150. * from this set.
  1151. * @param end last character, inclusive, of range to be removed
  1152. * from this set.
  1153. * @stable ICU 2.0
  1154. */
  1155. virtual UnicodeSet& complement(UChar32 start, UChar32 end);
  1156. /**
  1157. * Complements the specified character in this set. The character
  1158. * will be removed if it is in this set, or will be added if it is
  1159. * not in this set.
  1160. * A frozen set will not be modified.
  1161. * @stable ICU 2.0
  1162. */
  1163. UnicodeSet& complement(UChar32 c);
  1164. /**
  1165. * Complement the specified string in this set.
  1166. * The set will not contain the specified string once the call
  1167. * returns.
  1168. * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
  1169. * A frozen set will not be modified.
  1170. * @param s the string to complement
  1171. * @return this object, for chaining
  1172. * @stable ICU 2.4
  1173. */
  1174. UnicodeSet& complement(const UnicodeString& s);
  1175. /**
  1176. * Adds all of the elements in the specified set to this set if
  1177. * they're not already present. This operation effectively
  1178. * modifies this set so that its value is the <i>union</i> of the two
  1179. * sets. The behavior of this operation is unspecified if the specified
  1180. * collection is modified while the operation is in progress.
  1181. * A frozen set will not be modified.
  1182. *
  1183. * @param c set whose elements are to be added to this set.
  1184. * @see #add(UChar32, UChar32)
  1185. * @stable ICU 2.0
  1186. */
  1187. virtual UnicodeSet& addAll(const UnicodeSet& c);
  1188. /**
  1189. * Retains only the elements in this set that are contained in the
  1190. * specified set. In other words, removes from this set all of
  1191. * its elements that are not contained in the specified set. This
  1192. * operation effectively modifies this set so that its value is
  1193. * the <i>intersection</i> of the two sets.
  1194. * A frozen set will not be modified.
  1195. *
  1196. * @param c set that defines which elements this set will retain.
  1197. * @stable ICU 2.0
  1198. */
  1199. virtual UnicodeSet& retainAll(const UnicodeSet& c);
  1200. /**
  1201. * Removes from this set all of its elements that are contained in the
  1202. * specified set. This operation effectively modifies this
  1203. * set so that its value is the <i>asymmetric set difference</i> of
  1204. * the two sets.
  1205. * A frozen set will not be modified.
  1206. *
  1207. * @param c set that defines which elements will be removed from
  1208. * this set.
  1209. * @stable ICU 2.0
  1210. */
  1211. virtual UnicodeSet& removeAll(const UnicodeSet& c);
  1212. /**
  1213. * Complements in this set all elements contained in the specified
  1214. * set. Any character in the other set will be removed if it is
  1215. * in this set, or will be added if it is not in this set.
  1216. * A frozen set will not be modified.
  1217. *
  1218. * @param c set that defines which elements will be xor'ed from
  1219. * this set.
  1220. * @stable ICU 2.4
  1221. */
  1222. virtual UnicodeSet& complementAll(const UnicodeSet& c);
  1223. /**
  1224. * Removes all of the elements from this set. This set will be
  1225. * empty after this call returns.
  1226. * A frozen set will not be modified.
  1227. * @stable ICU 2.0
  1228. */
  1229. virtual UnicodeSet& clear(void);
  1230. /**
  1231. * Close this set over the given attribute. For the attribute
  1232. * USET_CASE, the result is to modify this set so that:
  1233. *
  1234. * 1. For each character or string 'a' in this set, all strings or
  1235. * characters 'b' such that foldCase(a) == foldCase(b) are added
  1236. * to this set.
  1237. *
  1238. * 2. For each string 'e' in the resulting set, if e !=
  1239. * foldCase(e), 'e' will be removed.
  1240. *
  1241. * Example: [aq\\u00DF{Bc}{bC}{Fi}] => [aAqQ\\u00DF\\uFB01{ss}{bc}{fi}]
  1242. *
  1243. * (Here foldCase(x) refers to the operation u_strFoldCase, and a
  1244. * == b denotes that the contents are the same, not pointer
  1245. * comparison.)
  1246. *
  1247. * A frozen set will not be modified.
  1248. *
  1249. * @param attribute bitmask for attributes to close over.
  1250. * Currently only the USET_CASE bit is supported. Any undefined bits
  1251. * are ignored.
  1252. * @return a reference to this set.
  1253. * @stable ICU 4.2
  1254. */
  1255. UnicodeSet& closeOver(int32_t attribute);
  1256. /**
  1257. * Remove all strings from this set.
  1258. *
  1259. * @return a reference to this set.
  1260. * @stable ICU 4.2
  1261. */
  1262. virtual UnicodeSet &removeAllStrings();
  1263. /**
  1264. * Iteration method that returns the number of ranges contained in
  1265. * this set.
  1266. * @see #getRangeStart
  1267. * @see #getRangeEnd
  1268. * @stable ICU 2.4
  1269. */
  1270. virtual int32_t getRangeCount(void) const;
  1271. /**
  1272. * Iteration method that returns the first character in the
  1273. * specified range of this set.
  1274. * @see #getRangeCount
  1275. * @see #getRangeEnd
  1276. * @stable ICU 2.4
  1277. */
  1278. virtual UChar32 getRangeStart(int32_t index) const;
  1279. /**
  1280. * Iteration method that returns the last character in the
  1281. * specified range of this set.
  1282. * @see #getRangeStart
  1283. * @see #getRangeEnd
  1284. * @stable ICU 2.4
  1285. */
  1286. virtual UChar32 getRangeEnd(int32_t index) const;
  1287. /**
  1288. * Serializes this set into an array of 16-bit integers. Serialization
  1289. * (currently) only records the characters in the set; multicharacter
  1290. * strings are ignored.
  1291. *
  1292. * The array has following format (each line is one 16-bit
  1293. * integer):
  1294. *
  1295. * length = (n+2*m) | (m!=0?0x8000:0)
  1296. * bmpLength = n; present if m!=0
  1297. * bmp[0]
  1298. * bmp[1]
  1299. * ...
  1300. * bmp[n-1]
  1301. * supp-high[0]
  1302. * supp-low[0]
  1303. * supp-high[1]
  1304. * supp-low[1]
  1305. * ...
  1306. * supp-high[m-1]
  1307. * supp-low[m-1]
  1308. *
  1309. * The array starts with a header. After the header are n bmp
  1310. * code points, then m supplementary code points. Either n or m
  1311. * or both may be zero. n+2*m is always <= 0x7FFF.
  1312. *
  1313. * If there are no supplementary characters (if m==0) then the
  1314. * header is one 16-bit integer, 'length', with value n.
  1315. *
  1316. * If there are supplementary characters (if m!=0) then the header
  1317. * is two 16-bit integers. The first, 'length', has value
  1318. * (n+2*m)|0x8000. The second, 'bmpLength', has value n.
  1319. *
  1320. * After the header the code points are stored in ascending order.
  1321. * Supplementary code points are stored as most significant 16
  1322. * bits followed by least significant 16 bits.
  1323. *
  1324. * @param dest pointer to buffer of destCapacity 16-bit integers.
  1325. * May be NULL only if destCapacity is zero.
  1326. * @param destCapacity size of dest, or zero. Must not be negative.
  1327. * @param ec error code. Will be set to U_INDEX_OUTOFBOUNDS_ERROR
  1328. * if n+2*m > 0x7FFF. Will be set to U_BUFFER_OVERFLOW_ERROR if
  1329. * n+2*m+(m!=0?2:1) > destCapacity.
  1330. * @return the total length of the serialized format, including
  1331. * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other
  1332. * than U_BUFFER_OVERFLOW_ERROR.
  1333. * @stable ICU 2.4
  1334. */
  1335. int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const;
  1336. /**
  1337. * Reallocate this objects internal structures to take up the least
  1338. * possible space, without changing this object's value.
  1339. * A frozen set will not be modified.
  1340. * @stable ICU 2.4
  1341. */
  1342. virtual UnicodeSet& compact();
  1343. /**
  1344. * Return the class ID for this class. This is useful only for
  1345. * comparing to a return value from getDynamicClassID(). For example:
  1346. * <pre>
  1347. * . Base* polymorphic_pointer = createPolymorphicObject();
  1348. * . if (polymorphic_pointer->getDynamicClassID() ==
  1349. * . Derived::getStaticClassID()) ...
  1350. * </pre>
  1351. * @return The class ID for all objects of this class.
  1352. * @stable ICU 2.0
  1353. */
  1354. static UClassID U_EXPORT2 getStaticClassID(void);
  1355. /**
  1356. * Implement UnicodeFunctor API.
  1357. *
  1358. * @return The class ID for this object. All objects of a given
  1359. * class have the same class ID. Objects of other classes have
  1360. * different class IDs.
  1361. * @stable ICU 2.4
  1362. */
  1363. virtual UClassID getDynamicClassID(void) const;
  1364. private:
  1365. // Private API for the USet API
  1366. friend class USetAccess;
  1367. int32_t getStringCount() const;
  1368. const UnicodeString* getString(int32_t index) const;
  1369. //----------------------------------------------------------------
  1370. // RuleBasedTransliterator support
  1371. //----------------------------------------------------------------
  1372. private:
  1373. /**
  1374. * Returns <tt>true</tt> if this set contains any character whose low byte
  1375. * is the given value. This is used by <tt>RuleBasedTransliterator</tt> for
  1376. * indexing.
  1377. */
  1378. virtual UBool matchesIndexValue(uint8_t v) const;
  1379. private:
  1380. friend class RBBIRuleScanner;
  1381. //----------------------------------------------------------------
  1382. // Implementation: Clone as thawed (see ICU4J Freezable)
  1383. //----------------------------------------------------------------
  1384. UnicodeSet(const UnicodeSet& o, UBool /* asThawed */);
  1385. //----------------------------------------------------------------
  1386. // Implementation: Pattern parsing
  1387. //----------------------------------------------------------------
  1388. void applyPatternIgnoreSpace(const UnicodeString& pattern,
  1389. ParsePosition& pos,
  1390. const SymbolTable* symbols,
  1391. UErrorCode& status);
  1392. void applyPattern(RuleCharacterIterator& chars,
  1393. const SymbolTable* symbols,
  1394. UnicodeString& rebuiltPat,
  1395. uint32_t options,
  1396. UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute),
  1397. UErrorCode& ec);
  1398. //----------------------------------------------------------------
  1399. // Implementation: Utility methods
  1400. //----------------------------------------------------------------
  1401. void ensureCapacity(int32_t newLen, UErrorCode& ec);
  1402. void ensureBufferCapacity(int32_t newLen, UErrorCode& ec);
  1403. void swapBuffers(void);
  1404. UBool allocateStrings(UErrorCode &status);
  1405. UnicodeString& _toPattern(UnicodeString& result,
  1406. UBool escapeUnprintable) const;
  1407. UnicodeString& _generatePattern(UnicodeString& result,
  1408. UBool escapeUnprintable) const;
  1409. static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable);
  1410. static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable);
  1411. //----------------------------------------------------------------
  1412. // Implementation: Fundamental operators
  1413. //----------------------------------------------------------------
  1414. void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity);
  1415. void add(const UChar32* other, int32_t otherLen, int8_t polarity);
  1416. void retain(const UChar32* other, int32_t otherLen, int8_t polarity);
  1417. /**
  1418. * Return true if the given position, in the given pattern, appears
  1419. * to be the start of a property set pattern [:foo:], \\p{foo}, or
  1420. * \\P{foo}, or \\N{name}.
  1421. */
  1422. static UBool resemblesPropertyPattern(const UnicodeString& pattern,
  1423. int32_t pos);
  1424. static UBool resemblesPropertyPattern(RuleCharacterIterator& chars,
  1425. int32_t iterOpts);
  1426. /**
  1427. * Parse the given property pattern at the given parse position
  1428. * and set this UnicodeSet to the result.
  1429. *
  1430. * The original design document is out of date, but still useful.
  1431. * Ignore the property and value names:
  1432. * http://source.icu-project.org/repos/icu/icuhtml/trunk/design/unicodeset_properties.html
  1433. *
  1434. * Recognized syntax:
  1435. *
  1436. * [:foo:] [:^foo:] - white space not allowed within "[:" or ":]"
  1437. * \\p{foo} \\P{foo} - white space not allowed within "\\p" or "\\P"
  1438. * \\N{name} - white space not allowed within "\\N"
  1439. *
  1440. * Other than the above restrictions, Unicode Pattern_White_Space characters are ignored.
  1441. * Case is ignored except in "\\p" and "\\P" and "\\N". In 'name' leading
  1442. * and trailing space is deleted, and internal runs of whitespace
  1443. * are collapsed to a single space.
  1444. *
  1445. * We support binary properties, enumerated properties, and the
  1446. * following non-enumerated properties:
  1447. *
  1448. * Numeric_Value
  1449. * Name
  1450. * Unicode_1_Name
  1451. *
  1452. * @param pattern the pattern string
  1453. * @param ppos on entry, the position at which to begin parsing.
  1454. * This should be one of the locations marked '^':
  1455. *
  1456. * [:blah:] \\p{blah} \\P{blah} \\N{name}
  1457. * ^ % ^ % ^ % ^ %
  1458. *
  1459. * On return, the position after the last character parsed, that is,
  1460. * the locations marked '%'. If the parse fails, ppos is returned
  1461. * unchanged.
  1462. * @param ec status
  1463. * @return a reference to this.
  1464. */
  1465. UnicodeSet& applyPropertyPattern(const UnicodeString& pattern,
  1466. ParsePosition& ppos,
  1467. UErrorCode &ec);
  1468. void applyPropertyPattern(RuleCharacterIterator& chars,
  1469. UnicodeString& rebuiltPat,
  1470. UErrorCode& ec);
  1471. friend void U_CALLCONV UnicodeSet_initInclusion(int32_t src, UErrorCode &status);
  1472. static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status);
  1473. /**
  1474. * A filter that returns TRUE if the given code point should be
  1475. * included in the UnicodeSet being constructed.
  1476. */
  1477. typedef UBool (*Filter)(UChar32 codePoint, void* context);
  1478. /**
  1479. * Given a filter, set this UnicodeSet to the code points
  1480. * contained by that filter. The filter MUST be
  1481. * property-conformant. That is, if it returns value v for one
  1482. * code point, then it must return v for all affiliated code
  1483. * points, as defined by the inclusions list. See
  1484. * getInclusions().
  1485. * src is a UPropertySource value.
  1486. */
  1487. void applyFilter(Filter filter,
  1488. void* context,
  1489. int32_t src,
  1490. UErrorCode &status);
  1491. /**
  1492. * Set the new pattern to cache.
  1493. */
  1494. void setPattern(const UnicodeString& newPat);
  1495. /**
  1496. * Release existing cached pattern.
  1497. */
  1498. void releasePattern();
  1499. friend class UnicodeSetIterator;
  1500. };
  1501. inline UBool UnicodeSet::operator!=(const UnicodeSet& o) const {
  1502. return !operator==(o);
  1503. }
  1504. inline UBool UnicodeSet::isFrozen() const {
  1505. return (UBool)(bmpSet!=NULL || stringSpan!=NULL);
  1506. }
  1507. inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const {
  1508. return !containsNone(start, end);
  1509. }
  1510. inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const {
  1511. return !containsNone(s);
  1512. }
  1513. inline UBool UnicodeSet::containsSome(const UnicodeString& s) const {
  1514. return !containsNone(s);
  1515. }
  1516. inline UBool UnicodeSet::isBogus() const {
  1517. return (UBool)(fFlags & kIsBogus);
  1518. }
  1519. inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) {
  1520. return reinterpret_cast<UnicodeSet *>(uset);
  1521. }
  1522. inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) {
  1523. return reinterpret_cast<const UnicodeSet *>(uset);
  1524. }
  1525. inline USet *UnicodeSet::toUSet() {
  1526. return reinterpret_cast<USet *>(this);
  1527. }
  1528. inline const USet *UnicodeSet::toUSet() const {
  1529. return reinterpret_cast<const USet *>(this);
  1530. }
  1531. inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const {
  1532. int32_t sLength=s.length();
  1533. if(start<0) {
  1534. start=0;
  1535. } else if(start>sLength) {
  1536. start=sLength;
  1537. }
  1538. return start+span(s.getBuffer()+start, sLength-start, spanCondition);
  1539. }
  1540. inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const {
  1541. int32_t sLength=s.length();
  1542. if(limit<0) {
  1543. limit=0;
  1544. } else if(limit>sLength) {
  1545. limit=sLength;
  1546. }
  1547. return spanBack(s.getBuffer(), limit, spanCondition);
  1548. }
  1549. U_NAMESPACE_END
  1550. #endif