uniset.h 65 KB

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