uniset.h 70 KB

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