uniset.h 64 KB

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