regex.h 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 2002-2015, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. **********************************************************************
  6. * file name: regex.h
  7. * encoding: US-ASCII
  8. * indentation:4
  9. *
  10. * created on: 2002oct22
  11. * created by: Andy Heninger
  12. *
  13. * ICU Regular Expressions, API for C++
  14. */
  15. #ifndef REGEX_H
  16. #define REGEX_H
  17. //#define REGEX_DEBUG
  18. /**
  19. * \file
  20. * \brief C++ API: Regular Expressions
  21. *
  22. * <h2>Regular Expression API</h2>
  23. *
  24. * <p>The ICU API for processing regular expressions consists of two classes,
  25. * <code>RegexPattern</code> and <code>RegexMatcher</code>.
  26. * <code>RegexPattern</code> objects represent a pre-processed, or compiled
  27. * regular expression. They are created from a regular expression pattern string,
  28. * and can be used to create <code>RegexMatcher</code> objects for the pattern.</p>
  29. *
  30. * <p>Class <code>RegexMatcher</code> bundles together a regular expression
  31. * pattern and a target string to which the search pattern will be applied.
  32. * <code>RegexMatcher</code> includes API for doing plain find or search
  33. * operations, for search and replace operations, and for obtaining detailed
  34. * information about bounds of a match. </p>
  35. *
  36. * <p>Note that by constructing <code>RegexMatcher</code> objects directly from regular
  37. * expression pattern strings application code can be simplified and the explicit
  38. * need for <code>RegexPattern</code> objects can usually be eliminated.
  39. * </p>
  40. */
  41. #include "unicode/utypes.h"
  42. #if !UCONFIG_NO_REGULAR_EXPRESSIONS
  43. #include "unicode/uobject.h"
  44. #include "unicode/unistr.h"
  45. #include "unicode/utext.h"
  46. #include "unicode/parseerr.h"
  47. #include "unicode/uregex.h"
  48. // Forward Declarations
  49. struct UHashtable;
  50. U_NAMESPACE_BEGIN
  51. struct Regex8BitSet;
  52. class RegexCImpl;
  53. class RegexMatcher;
  54. class RegexPattern;
  55. struct REStackFrame;
  56. class RuleBasedBreakIterator;
  57. class UnicodeSet;
  58. class UVector;
  59. class UVector32;
  60. class UVector64;
  61. /**
  62. * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
  63. * factory methods for creating a RegexPattern object from the source (string) form
  64. * of a regular expression, methods for creating RegexMatchers that allow the pattern
  65. * to be applied to input text, and a few convenience methods for simple common
  66. * uses of regular expressions.
  67. *
  68. * <p>Class RegexPattern is not intended to be subclassed.</p>
  69. *
  70. * @stable ICU 2.4
  71. */
  72. class U_I18N_API RegexPattern U_FINAL : public UObject {
  73. public:
  74. /**
  75. * default constructor. Create a RegexPattern object that refers to no actual
  76. * pattern. Not normally needed; RegexPattern objects are usually
  77. * created using the factory method <code>compile()</code>.
  78. *
  79. * @stable ICU 2.4
  80. */
  81. RegexPattern();
  82. /**
  83. * Copy Constructor. Create a new RegexPattern object that is equivalent
  84. * to the source object.
  85. * @param source the pattern object to be copied.
  86. * @stable ICU 2.4
  87. */
  88. RegexPattern(const RegexPattern &source);
  89. /**
  90. * Destructor. Note that a RegexPattern object must persist so long as any
  91. * RegexMatcher objects that were created from the RegexPattern are active.
  92. * @stable ICU 2.4
  93. */
  94. virtual ~RegexPattern();
  95. /**
  96. * Comparison operator. Two RegexPattern objects are considered equal if they
  97. * were constructed from identical source patterns using the same match flag
  98. * settings.
  99. * @param that a RegexPattern object to compare with "this".
  100. * @return TRUE if the objects are equivalent.
  101. * @stable ICU 2.4
  102. */
  103. UBool operator==(const RegexPattern& that) const;
  104. /**
  105. * Comparison operator. Two RegexPattern objects are considered equal if they
  106. * were constructed from identical source patterns using the same match flag
  107. * settings.
  108. * @param that a RegexPattern object to compare with "this".
  109. * @return TRUE if the objects are different.
  110. * @stable ICU 2.4
  111. */
  112. inline UBool operator!=(const RegexPattern& that) const {return ! operator ==(that);}
  113. /**
  114. * Assignment operator. After assignment, this RegexPattern will behave identically
  115. * to the source object.
  116. * @stable ICU 2.4
  117. */
  118. RegexPattern &operator =(const RegexPattern &source);
  119. /**
  120. * Create an exact copy of this RegexPattern object. Since RegexPattern is not
  121. * intended to be subclassed, <code>clone()</code> and the copy construction are
  122. * equivalent operations.
  123. * @return the copy of this RegexPattern
  124. * @stable ICU 2.4
  125. */
  126. virtual RegexPattern *clone() const;
  127. /**
  128. * Compiles the regular expression in string form into a RegexPattern
  129. * object. These compile methods, rather than the constructors, are the usual
  130. * way that RegexPattern objects are created.
  131. *
  132. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  133. * objects created from the pattern are active. RegexMatchers keep a pointer
  134. * back to their pattern, so premature deletion of the pattern is a
  135. * catastrophic error.</p>
  136. *
  137. * <p>All pattern match mode flags are set to their default values.</p>
  138. *
  139. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  140. * from a pattern string rather than separately compiling the pattern and
  141. * then creating a RegexMatcher object from the pattern.</p>
  142. *
  143. * @param regex The regular expression to be compiled.
  144. * @param pe Receives the position (line and column nubers) of any error
  145. * within the regular expression.)
  146. * @param status A reference to a UErrorCode to receive any errors.
  147. * @return A regexPattern object for the compiled pattern.
  148. *
  149. * @stable ICU 2.4
  150. */
  151. static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
  152. UParseError &pe,
  153. UErrorCode &status);
  154. /**
  155. * Compiles the regular expression in string form into a RegexPattern
  156. * object. These compile methods, rather than the constructors, are the usual
  157. * way that RegexPattern objects are created.
  158. *
  159. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  160. * objects created from the pattern are active. RegexMatchers keep a pointer
  161. * back to their pattern, so premature deletion of the pattern is a
  162. * catastrophic error.</p>
  163. *
  164. * <p>All pattern match mode flags are set to their default values.</p>
  165. *
  166. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  167. * from a pattern string rather than separately compiling the pattern and
  168. * then creating a RegexMatcher object from the pattern.</p>
  169. *
  170. * @param regex The regular expression to be compiled. Note, the text referred
  171. * to by this UText must not be deleted during the lifetime of the
  172. * RegexPattern object or any RegexMatcher object created from it.
  173. * @param pe Receives the position (line and column nubers) of any error
  174. * within the regular expression.)
  175. * @param status A reference to a UErrorCode to receive any errors.
  176. * @return A regexPattern object for the compiled pattern.
  177. *
  178. * @stable ICU 4.6
  179. */
  180. static RegexPattern * U_EXPORT2 compile( UText *regex,
  181. UParseError &pe,
  182. UErrorCode &status);
  183. /**
  184. * Compiles the regular expression in string form into a RegexPattern
  185. * object using the specified match mode flags. These compile methods,
  186. * rather than the constructors, are the usual way that RegexPattern objects
  187. * are created.
  188. *
  189. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  190. * objects created from the pattern are active. RegexMatchers keep a pointer
  191. * back to their pattern, so premature deletion of the pattern is a
  192. * catastrophic error.</p>
  193. *
  194. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  195. * from a pattern string instead of than separately compiling the pattern and
  196. * then creating a RegexMatcher object from the pattern.</p>
  197. *
  198. * @param regex The regular expression to be compiled.
  199. * @param flags The match mode flags to be used.
  200. * @param pe Receives the position (line and column numbers) of any error
  201. * within the regular expression.)
  202. * @param status A reference to a UErrorCode to receive any errors.
  203. * @return A regexPattern object for the compiled pattern.
  204. *
  205. * @stable ICU 2.4
  206. */
  207. static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
  208. uint32_t flags,
  209. UParseError &pe,
  210. UErrorCode &status);
  211. /**
  212. * Compiles the regular expression in string form into a RegexPattern
  213. * object using the specified match mode flags. These compile methods,
  214. * rather than the constructors, are the usual way that RegexPattern objects
  215. * are created.
  216. *
  217. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  218. * objects created from the pattern are active. RegexMatchers keep a pointer
  219. * back to their pattern, so premature deletion of the pattern is a
  220. * catastrophic error.</p>
  221. *
  222. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  223. * from a pattern string instead of than separately compiling the pattern and
  224. * then creating a RegexMatcher object from the pattern.</p>
  225. *
  226. * @param regex The regular expression to be compiled. Note, the text referred
  227. * to by this UText must not be deleted during the lifetime of the
  228. * RegexPattern object or any RegexMatcher object created from it.
  229. * @param flags The match mode flags to be used.
  230. * @param pe Receives the position (line and column numbers) of any error
  231. * within the regular expression.)
  232. * @param status A reference to a UErrorCode to receive any errors.
  233. * @return A regexPattern object for the compiled pattern.
  234. *
  235. * @stable ICU 4.6
  236. */
  237. static RegexPattern * U_EXPORT2 compile( UText *regex,
  238. uint32_t flags,
  239. UParseError &pe,
  240. UErrorCode &status);
  241. /**
  242. * Compiles the regular expression in string form into a RegexPattern
  243. * object using the specified match mode flags. These compile methods,
  244. * rather than the constructors, are the usual way that RegexPattern objects
  245. * are created.
  246. *
  247. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  248. * objects created from the pattern are active. RegexMatchers keep a pointer
  249. * back to their pattern, so premature deletion of the pattern is a
  250. * catastrophic error.</p>
  251. *
  252. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  253. * from a pattern string instead of than separately compiling the pattern and
  254. * then creating a RegexMatcher object from the pattern.</p>
  255. *
  256. * @param regex The regular expression to be compiled.
  257. * @param flags The match mode flags to be used.
  258. * @param status A reference to a UErrorCode to receive any errors.
  259. * @return A regexPattern object for the compiled pattern.
  260. *
  261. * @stable ICU 2.6
  262. */
  263. static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
  264. uint32_t flags,
  265. UErrorCode &status);
  266. /**
  267. * Compiles the regular expression in string form into a RegexPattern
  268. * object using the specified match mode flags. These compile methods,
  269. * rather than the constructors, are the usual way that RegexPattern objects
  270. * are created.
  271. *
  272. * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
  273. * objects created from the pattern are active. RegexMatchers keep a pointer
  274. * back to their pattern, so premature deletion of the pattern is a
  275. * catastrophic error.</p>
  276. *
  277. * <p>Note that it is often more convenient to construct a RegexMatcher directly
  278. * from a pattern string instead of than separately compiling the pattern and
  279. * then creating a RegexMatcher object from the pattern.</p>
  280. *
  281. * @param regex The regular expression to be compiled. Note, the text referred
  282. * to by this UText must not be deleted during the lifetime of the
  283. * RegexPattern object or any RegexMatcher object created from it.
  284. * @param flags The match mode flags to be used.
  285. * @param status A reference to a UErrorCode to receive any errors.
  286. * @return A regexPattern object for the compiled pattern.
  287. *
  288. * @stable ICU 4.6
  289. */
  290. static RegexPattern * U_EXPORT2 compile( UText *regex,
  291. uint32_t flags,
  292. UErrorCode &status);
  293. /**
  294. * Get the match mode flags that were used when compiling this pattern.
  295. * @return the match mode flags
  296. * @stable ICU 2.4
  297. */
  298. virtual uint32_t flags() const;
  299. /**
  300. * Creates a RegexMatcher that will match the given input against this pattern. The
  301. * RegexMatcher can then be used to perform match, find or replace operations
  302. * on the input. Note that a RegexPattern object must not be deleted while
  303. * RegexMatchers created from it still exist and might possibly be used again.
  304. * <p>
  305. * The matcher will retain a reference to the supplied input string, and all regexp
  306. * pattern matching operations happen directly on this original string. It is
  307. * critical that the string not be altered or deleted before use by the regular
  308. * expression operations is complete.
  309. *
  310. * @param input The input string to which the regular expression will be applied.
  311. * @param status A reference to a UErrorCode to receive any errors.
  312. * @return A RegexMatcher object for this pattern and input.
  313. *
  314. * @stable ICU 2.4
  315. */
  316. virtual RegexMatcher *matcher(const UnicodeString &input,
  317. UErrorCode &status) const;
  318. private:
  319. /**
  320. * Cause a compilation error if an application accidentally attempts to
  321. * create a matcher with a (UChar *) string as input rather than
  322. * a UnicodeString. Avoids a dangling reference to a temporary string.
  323. * <p>
  324. * To efficiently work with UChar *strings, wrap the data in a UnicodeString
  325. * using one of the aliasing constructors, such as
  326. * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
  327. * or in a UText, using
  328. * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
  329. *
  330. */
  331. RegexMatcher *matcher(const UChar *input,
  332. UErrorCode &status) const;
  333. public:
  334. /**
  335. * Creates a RegexMatcher that will match against this pattern. The
  336. * RegexMatcher can be used to perform match, find or replace operations.
  337. * Note that a RegexPattern object must not be deleted while
  338. * RegexMatchers created from it still exist and might possibly be used again.
  339. *
  340. * @param status A reference to a UErrorCode to receive any errors.
  341. * @return A RegexMatcher object for this pattern and input.
  342. *
  343. * @stable ICU 2.6
  344. */
  345. virtual RegexMatcher *matcher(UErrorCode &status) const;
  346. /**
  347. * Test whether a string matches a regular expression. This convenience function
  348. * both compiles the regular expression and applies it in a single operation.
  349. * Note that if the same pattern needs to be applied repeatedly, this method will be
  350. * less efficient than creating and reusing a RegexMatcher object.
  351. *
  352. * @param regex The regular expression
  353. * @param input The string data to be matched
  354. * @param pe Receives the position of any syntax errors within the regular expression
  355. * @param status A reference to a UErrorCode to receive any errors.
  356. * @return True if the regular expression exactly matches the full input string.
  357. *
  358. * @stable ICU 2.4
  359. */
  360. static UBool U_EXPORT2 matches(const UnicodeString &regex,
  361. const UnicodeString &input,
  362. UParseError &pe,
  363. UErrorCode &status);
  364. /**
  365. * Test whether a string matches a regular expression. This convenience function
  366. * both compiles the regular expression and applies it in a single operation.
  367. * Note that if the same pattern needs to be applied repeatedly, this method will be
  368. * less efficient than creating and reusing a RegexMatcher object.
  369. *
  370. * @param regex The regular expression
  371. * @param input The string data to be matched
  372. * @param pe Receives the position of any syntax errors within the regular expression
  373. * @param status A reference to a UErrorCode to receive any errors.
  374. * @return True if the regular expression exactly matches the full input string.
  375. *
  376. * @stable ICU 4.6
  377. */
  378. static UBool U_EXPORT2 matches(UText *regex,
  379. UText *input,
  380. UParseError &pe,
  381. UErrorCode &status);
  382. /**
  383. * Returns the regular expression from which this pattern was compiled. This method will work
  384. * even if the pattern was compiled from a UText.
  385. *
  386. * Note: If the pattern was originally compiled from a UText, and that UText was modified,
  387. * the returned string may no longer reflect the RegexPattern object.
  388. * @stable ICU 2.4
  389. */
  390. virtual UnicodeString pattern() const;
  391. /**
  392. * Returns the regular expression from which this pattern was compiled. This method will work
  393. * even if the pattern was compiled from a UnicodeString.
  394. *
  395. * Note: This is the original input, not a clone. If the pattern was originally compiled from a
  396. * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
  397. * object.
  398. *
  399. * @stable ICU 4.6
  400. */
  401. virtual UText *patternText(UErrorCode &status) const;
  402. /**
  403. * Get the group number corresponding to a named capture group.
  404. * The returned number can be used with any function that access
  405. * capture groups by number.
  406. *
  407. * The function returns an error status if the specified name does not
  408. * appear in the pattern.
  409. *
  410. * @param groupName The capture group name.
  411. * @param status A UErrorCode to receive any errors.
  412. *
  413. * @draft ICU 55
  414. */
  415. virtual int32_t groupNumberFromName(const UnicodeString &groupName, UErrorCode &status) const;
  416. /**
  417. * Get the group number corresponding to a named capture group.
  418. * The returned number can be used with any function that access
  419. * capture groups by number.
  420. *
  421. * The function returns an error status if the specified name does not
  422. * appear in the pattern.
  423. *
  424. * @param groupName The capture group name,
  425. * platform invariant characters only.
  426. * @param nameLength The length of the name, or -1 if the name is
  427. * nul-terminated.
  428. * @param status A UErrorCode to receive any errors.
  429. *
  430. * @draft ICU 55
  431. */
  432. virtual int32_t groupNumberFromName(const char *groupName, int32_t nameLength, UErrorCode &status) const;
  433. /**
  434. * Split a string into fields. Somewhat like split() from Perl or Java.
  435. * Pattern matches identify delimiters that separate the input
  436. * into fields. The input data between the delimiters becomes the
  437. * fields themselves.
  438. *
  439. * If the delimiter pattern includes capture groups, the captured text will
  440. * also appear in the destination array of output strings, interspersed
  441. * with the fields. This is similar to Perl, but differs from Java,
  442. * which ignores the presence of capture groups in the pattern.
  443. *
  444. * Trailing empty fields will always be returned, assuming sufficient
  445. * destination capacity. This differs from the default behavior for Java
  446. * and Perl where trailing empty fields are not returned.
  447. *
  448. * The number of strings produced by the split operation is returned.
  449. * This count includes the strings from capture groups in the delimiter pattern.
  450. * This behavior differs from Java, which ignores capture groups.
  451. *
  452. * For the best performance on split() operations,
  453. * <code>RegexMatcher::split</code> is preferable to this function
  454. *
  455. * @param input The string to be split into fields. The field delimiters
  456. * match the pattern (in the "this" object)
  457. * @param dest An array of UnicodeStrings to receive the results of the split.
  458. * This is an array of actual UnicodeString objects, not an
  459. * array of pointers to strings. Local (stack based) arrays can
  460. * work well here.
  461. * @param destCapacity The number of elements in the destination array.
  462. * If the number of fields found is less than destCapacity, the
  463. * extra strings in the destination array are not altered.
  464. * If the number of destination strings is less than the number
  465. * of fields, the trailing part of the input string, including any
  466. * field delimiters, is placed in the last destination string.
  467. * @param status A reference to a UErrorCode to receive any errors.
  468. * @return The number of fields into which the input string was split.
  469. * @stable ICU 2.4
  470. */
  471. virtual int32_t split(const UnicodeString &input,
  472. UnicodeString dest[],
  473. int32_t destCapacity,
  474. UErrorCode &status) const;
  475. /**
  476. * Split a string into fields. Somewhat like split() from Perl or Java.
  477. * Pattern matches identify delimiters that separate the input
  478. * into fields. The input data between the delimiters becomes the
  479. * fields themselves.
  480. *
  481. * If the delimiter pattern includes capture groups, the captured text will
  482. * also appear in the destination array of output strings, interspersed
  483. * with the fields. This is similar to Perl, but differs from Java,
  484. * which ignores the presence of capture groups in the pattern.
  485. *
  486. * Trailing empty fields will always be returned, assuming sufficient
  487. * destination capacity. This differs from the default behavior for Java
  488. * and Perl where trailing empty fields are not returned.
  489. *
  490. * The number of strings produced by the split operation is returned.
  491. * This count includes the strings from capture groups in the delimiter pattern.
  492. * This behavior differs from Java, which ignores capture groups.
  493. *
  494. * For the best performance on split() operations,
  495. * <code>RegexMatcher::split</code> is preferable to this function
  496. *
  497. * @param input The string to be split into fields. The field delimiters
  498. * match the pattern (in the "this" object)
  499. * @param dest An array of mutable UText structs to receive the results of the split.
  500. * If a field is NULL, a new UText is allocated to contain the results for
  501. * that field. This new UText is not guaranteed to be mutable.
  502. * @param destCapacity The number of elements in the destination array.
  503. * If the number of fields found is less than destCapacity, the
  504. * extra strings in the destination array are not altered.
  505. * If the number of destination strings is less than the number
  506. * of fields, the trailing part of the input string, including any
  507. * field delimiters, is placed in the last destination string.
  508. * @param status A reference to a UErrorCode to receive any errors.
  509. * @return The number of destination strings used.
  510. *
  511. * @stable ICU 4.6
  512. */
  513. virtual int32_t split(UText *input,
  514. UText *dest[],
  515. int32_t destCapacity,
  516. UErrorCode &status) const;
  517. /**
  518. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  519. *
  520. * @stable ICU 2.4
  521. */
  522. virtual UClassID getDynamicClassID() const;
  523. /**
  524. * ICU "poor man's RTTI", returns a UClassID for this class.
  525. *
  526. * @stable ICU 2.4
  527. */
  528. static UClassID U_EXPORT2 getStaticClassID();
  529. private:
  530. //
  531. // Implementation Data
  532. //
  533. UText *fPattern; // The original pattern string.
  534. UnicodeString *fPatternString; // The original pattern UncodeString if relevant
  535. uint32_t fFlags; // The flags used when compiling the pattern.
  536. //
  537. UVector64 *fCompiledPat; // The compiled pattern p-code.
  538. UnicodeString fLiteralText; // Any literal string data from the pattern,
  539. // after un-escaping, for use during the match.
  540. UVector *fSets; // Any UnicodeSets referenced from the pattern.
  541. Regex8BitSet *fSets8; // (and fast sets for latin-1 range.)
  542. UErrorCode fDeferredStatus; // status if some prior error has left this
  543. // RegexPattern in an unusable state.
  544. int32_t fMinMatchLen; // Minimum Match Length. All matches will have length
  545. // >= this value. For some patterns, this calculated
  546. // value may be less than the true shortest
  547. // possible match.
  548. int32_t fFrameSize; // Size of a state stack frame in the
  549. // execution engine.
  550. int32_t fDataSize; // The size of the data needed by the pattern that
  551. // does not go on the state stack, but has just
  552. // a single copy per matcher.
  553. UVector32 *fGroupMap; // Map from capture group number to position of
  554. // the group's variables in the matcher stack frame.
  555. UnicodeSet **fStaticSets; // Ptr to static (shared) sets for predefined
  556. // regex character classes, e.g. Word.
  557. Regex8BitSet *fStaticSets8; // Ptr to the static (shared) latin-1 only
  558. // sets for predefined regex classes.
  559. int32_t fStartType; // Info on how a match must start.
  560. int32_t fInitialStringIdx; //
  561. int32_t fInitialStringLen;
  562. UnicodeSet *fInitialChars;
  563. UChar32 fInitialChar;
  564. Regex8BitSet *fInitialChars8;
  565. UBool fNeedsAltInput;
  566. UHashtable *fNamedCaptureMap; // Map from capture group names to numbers.
  567. friend class RegexCompile;
  568. friend class RegexMatcher;
  569. friend class RegexCImpl;
  570. //
  571. // Implementation Methods
  572. //
  573. void init(); // Common initialization, for use by constructors.
  574. void zap(); // Common cleanup
  575. void dumpOp(int32_t index) const;
  576. public:
  577. #ifndef U_HIDE_INTERNAL_API
  578. /**
  579. * Dump a compiled pattern. Internal debug function.
  580. * @internal
  581. */
  582. void dumpPattern() const;
  583. #endif /* U_HIDE_INTERNAL_API */
  584. };
  585. /**
  586. * class RegexMatcher bundles together a regular expression pattern and
  587. * input text to which the expression can be applied. It includes methods
  588. * for testing for matches, and for find and replace operations.
  589. *
  590. * <p>Class RegexMatcher is not intended to be subclassed.</p>
  591. *
  592. * @stable ICU 2.4
  593. */
  594. class U_I18N_API RegexMatcher U_FINAL : public UObject {
  595. public:
  596. /**
  597. * Construct a RegexMatcher for a regular expression.
  598. * This is a convenience method that avoids the need to explicitly create
  599. * a RegexPattern object. Note that if several RegexMatchers need to be
  600. * created for the same expression, it will be more efficient to
  601. * separately create and cache a RegexPattern object, and use
  602. * its matcher() method to create the RegexMatcher objects.
  603. *
  604. * @param regexp The Regular Expression to be compiled.
  605. * @param flags Regular expression options, such as case insensitive matching.
  606. * @see UREGEX_CASE_INSENSITIVE
  607. * @param status Any errors are reported by setting this UErrorCode variable.
  608. * @stable ICU 2.6
  609. */
  610. RegexMatcher(const UnicodeString &regexp, uint32_t flags, UErrorCode &status);
  611. /**
  612. * Construct a RegexMatcher for a regular expression.
  613. * This is a convenience method that avoids the need to explicitly create
  614. * a RegexPattern object. Note that if several RegexMatchers need to be
  615. * created for the same expression, it will be more efficient to
  616. * separately create and cache a RegexPattern object, and use
  617. * its matcher() method to create the RegexMatcher objects.
  618. *
  619. * @param regexp The regular expression to be compiled.
  620. * @param flags Regular expression options, such as case insensitive matching.
  621. * @see UREGEX_CASE_INSENSITIVE
  622. * @param status Any errors are reported by setting this UErrorCode variable.
  623. *
  624. * @stable ICU 4.6
  625. */
  626. RegexMatcher(UText *regexp, uint32_t flags, UErrorCode &status);
  627. /**
  628. * Construct a RegexMatcher for a regular expression.
  629. * This is a convenience method that avoids the need to explicitly create
  630. * a RegexPattern object. Note that if several RegexMatchers need to be
  631. * created for the same expression, it will be more efficient to
  632. * separately create and cache a RegexPattern object, and use
  633. * its matcher() method to create the RegexMatcher objects.
  634. * <p>
  635. * The matcher will retain a reference to the supplied input string, and all regexp
  636. * pattern matching operations happen directly on the original string. It is
  637. * critical that the string not be altered or deleted before use by the regular
  638. * expression operations is complete.
  639. *
  640. * @param regexp The Regular Expression to be compiled.
  641. * @param input The string to match. The matcher retains a reference to the
  642. * caller's string; mo copy is made.
  643. * @param flags Regular expression options, such as case insensitive matching.
  644. * @see UREGEX_CASE_INSENSITIVE
  645. * @param status Any errors are reported by setting this UErrorCode variable.
  646. * @stable ICU 2.6
  647. */
  648. RegexMatcher(const UnicodeString &regexp, const UnicodeString &input,
  649. uint32_t flags, UErrorCode &status);
  650. /**
  651. * Construct a RegexMatcher for a regular expression.
  652. * This is a convenience method that avoids the need to explicitly create
  653. * a RegexPattern object. Note that if several RegexMatchers need to be
  654. * created for the same expression, it will be more efficient to
  655. * separately create and cache a RegexPattern object, and use
  656. * its matcher() method to create the RegexMatcher objects.
  657. * <p>
  658. * The matcher will make a shallow clone of the supplied input text, and all regexp
  659. * pattern matching operations happen on this clone. While read-only operations on
  660. * the supplied text are permitted, it is critical that the underlying string not be
  661. * altered or deleted before use by the regular expression operations is complete.
  662. *
  663. * @param regexp The Regular Expression to be compiled.
  664. * @param input The string to match. The matcher retains a shallow clone of the text.
  665. * @param flags Regular expression options, such as case insensitive matching.
  666. * @see UREGEX_CASE_INSENSITIVE
  667. * @param status Any errors are reported by setting this UErrorCode variable.
  668. *
  669. * @stable ICU 4.6
  670. */
  671. RegexMatcher(UText *regexp, UText *input,
  672. uint32_t flags, UErrorCode &status);
  673. private:
  674. /**
  675. * Cause a compilation error if an application accidentally attempts to
  676. * create a matcher with a (UChar *) string as input rather than
  677. * a UnicodeString. Avoids a dangling reference to a temporary string.
  678. * <p>
  679. * To efficiently work with UChar *strings, wrap the data in a UnicodeString
  680. * using one of the aliasing constructors, such as
  681. * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
  682. * or in a UText, using
  683. * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
  684. *
  685. */
  686. RegexMatcher(const UnicodeString &regexp, const UChar *input,
  687. uint32_t flags, UErrorCode &status);
  688. public:
  689. /**
  690. * Destructor.
  691. *
  692. * @stable ICU 2.4
  693. */
  694. virtual ~RegexMatcher();
  695. /**
  696. * Attempts to match the entire input region against the pattern.
  697. * @param status A reference to a UErrorCode to receive any errors.
  698. * @return TRUE if there is a match
  699. * @stable ICU 2.4
  700. */
  701. virtual UBool matches(UErrorCode &status);
  702. /**
  703. * Resets the matcher, then attempts to match the input beginning
  704. * at the specified startIndex, and extending to the end of the input.
  705. * The input region is reset to include the entire input string.
  706. * A successful match must extend to the end of the input.
  707. * @param startIndex The input string (native) index at which to begin matching.
  708. * @param status A reference to a UErrorCode to receive any errors.
  709. * @return TRUE if there is a match
  710. * @stable ICU 2.8
  711. */
  712. virtual UBool matches(int64_t startIndex, UErrorCode &status);
  713. /**
  714. * Attempts to match the input string, starting from the beginning of the region,
  715. * against the pattern. Like the matches() method, this function
  716. * always starts at the beginning of the input region;
  717. * unlike that function, it does not require that the entire region be matched.
  718. *
  719. * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
  720. * <code>end()</code>, and <code>group()</code> functions.</p>
  721. *
  722. * @param status A reference to a UErrorCode to receive any errors.
  723. * @return TRUE if there is a match at the start of the input string.
  724. * @stable ICU 2.4
  725. */
  726. virtual UBool lookingAt(UErrorCode &status);
  727. /**
  728. * Attempts to match the input string, starting from the specified index, against the pattern.
  729. * The match may be of any length, and is not required to extend to the end
  730. * of the input string. Contrast with match().
  731. *
  732. * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
  733. * <code>end()</code>, and <code>group()</code> functions.</p>
  734. *
  735. * @param startIndex The input string (native) index at which to begin matching.
  736. * @param status A reference to a UErrorCode to receive any errors.
  737. * @return TRUE if there is a match.
  738. * @stable ICU 2.8
  739. */
  740. virtual UBool lookingAt(int64_t startIndex, UErrorCode &status);
  741. /**
  742. * Find the next pattern match in the input string.
  743. * The find begins searching the input at the location following the end of
  744. * the previous match, or at the start of the string if there is no previous match.
  745. * If a match is found, <code>start(), end()</code> and <code>group()</code>
  746. * will provide more information regarding the match.
  747. * <p>Note that if the input string is changed by the application,
  748. * use find(startPos, status) instead of find(), because the saved starting
  749. * position may not be valid with the altered input string.</p>
  750. * @return TRUE if a match is found.
  751. * @stable ICU 2.4
  752. */
  753. virtual UBool find();
  754. /**
  755. * Find the next pattern match in the input string.
  756. * The find begins searching the input at the location following the end of
  757. * the previous match, or at the start of the string if there is no previous match.
  758. * If a match is found, <code>start(), end()</code> and <code>group()</code>
  759. * will provide more information regarding the match.
  760. * <p>Note that if the input string is changed by the application,
  761. * use find(startPos, status) instead of find(), because the saved starting
  762. * position may not be valid with the altered input string.</p>
  763. * @param status A reference to a UErrorCode to receive any errors.
  764. * @return TRUE if a match is found.
  765. * @draft ICU 55
  766. */
  767. virtual UBool find(UErrorCode &status);
  768. /**
  769. * Resets this RegexMatcher and then attempts to find the next substring of the
  770. * input string that matches the pattern, starting at the specified index.
  771. *
  772. * @param start The (native) index in the input string to begin the search.
  773. * @param status A reference to a UErrorCode to receive any errors.
  774. * @return TRUE if a match is found.
  775. * @stable ICU 2.4
  776. */
  777. virtual UBool find(int64_t start, UErrorCode &status);
  778. /**
  779. * Returns a string containing the text matched by the previous match.
  780. * If the pattern can match an empty string, an empty string may be returned.
  781. * @param status A reference to a UErrorCode to receive any errors.
  782. * Possible errors are U_REGEX_INVALID_STATE if no match
  783. * has been attempted or the last match failed.
  784. * @return a string containing the matched input text.
  785. * @stable ICU 2.4
  786. */
  787. virtual UnicodeString group(UErrorCode &status) const;
  788. /**
  789. * Returns a string containing the text captured by the given group
  790. * during the previous match operation. Group(0) is the entire match.
  791. *
  792. * @param groupNum the capture group number
  793. * @param status A reference to a UErrorCode to receive any errors.
  794. * Possible errors are U_REGEX_INVALID_STATE if no match
  795. * has been attempted or the last match failed and
  796. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  797. * @return the captured text
  798. * @stable ICU 2.4
  799. */
  800. virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const;
  801. /**
  802. * Returns the number of capturing groups in this matcher's pattern.
  803. * @return the number of capture groups
  804. * @stable ICU 2.4
  805. */
  806. virtual int32_t groupCount() const;
  807. /**
  808. * Returns a shallow clone of the entire live input string with the UText current native index
  809. * set to the beginning of the requested group.
  810. *
  811. * @param dest The UText into which the input should be cloned, or NULL to create a new UText
  812. * @param group_len A reference to receive the length of the desired capture group
  813. * @param status A reference to a UErrorCode to receive any errors.
  814. * Possible errors are U_REGEX_INVALID_STATE if no match
  815. * has been attempted or the last match failed and
  816. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  817. * @return dest if non-NULL, a shallow copy of the input text otherwise
  818. *
  819. * @stable ICU 4.6
  820. */
  821. virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const;
  822. /**
  823. * Returns a shallow clone of the entire live input string with the UText current native index
  824. * set to the beginning of the requested group.
  825. *
  826. * @param groupNum The capture group number.
  827. * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
  828. * @param group_len A reference to receive the length of the desired capture group
  829. * @param status A reference to a UErrorCode to receive any errors.
  830. * Possible errors are U_REGEX_INVALID_STATE if no match
  831. * has been attempted or the last match failed and
  832. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  833. * @return dest if non-NULL, a shallow copy of the input text otherwise
  834. *
  835. * @stable ICU 4.6
  836. */
  837. virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const;
  838. /**
  839. * Returns the index in the input string of the start of the text matched
  840. * during the previous match operation.
  841. * @param status a reference to a UErrorCode to receive any errors.
  842. * @return The (native) position in the input string of the start of the last match.
  843. * @stable ICU 2.4
  844. */
  845. virtual int32_t start(UErrorCode &status) const;
  846. /**
  847. * Returns the index in the input string of the start of the text matched
  848. * during the previous match operation.
  849. * @param status a reference to a UErrorCode to receive any errors.
  850. * @return The (native) position in the input string of the start of the last match.
  851. * @stable ICU 4.6
  852. */
  853. virtual int64_t start64(UErrorCode &status) const;
  854. /**
  855. * Returns the index in the input string of the start of the text matched by the
  856. * specified capture group during the previous match operation. Return -1 if
  857. * the capture group exists in the pattern, but was not part of the last match.
  858. *
  859. * @param group the capture group number
  860. * @param status A reference to a UErrorCode to receive any errors. Possible
  861. * errors are U_REGEX_INVALID_STATE if no match has been
  862. * attempted or the last match failed, and
  863. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  864. * @return the (native) start position of substring matched by the specified group.
  865. * @stable ICU 2.4
  866. */
  867. virtual int32_t start(int32_t group, UErrorCode &status) const;
  868. /**
  869. * Returns the index in the input string of the start of the text matched by the
  870. * specified capture group during the previous match operation. Return -1 if
  871. * the capture group exists in the pattern, but was not part of the last match.
  872. *
  873. * @param group the capture group number.
  874. * @param status A reference to a UErrorCode to receive any errors. Possible
  875. * errors are U_REGEX_INVALID_STATE if no match has been
  876. * attempted or the last match failed, and
  877. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  878. * @return the (native) start position of substring matched by the specified group.
  879. * @stable ICU 4.6
  880. */
  881. virtual int64_t start64(int32_t group, UErrorCode &status) const;
  882. /**
  883. * Returns the index in the input string of the first character following the
  884. * text matched during the previous match operation.
  885. *
  886. * @param status A reference to a UErrorCode to receive any errors. Possible
  887. * errors are U_REGEX_INVALID_STATE if no match has been
  888. * attempted or the last match failed.
  889. * @return the index of the last character matched, plus one.
  890. * The index value returned is a native index, corresponding to
  891. * code units for the underlying encoding type, for example,
  892. * a byte index for UTF-8.
  893. * @stable ICU 2.4
  894. */
  895. virtual int32_t end(UErrorCode &status) const;
  896. /**
  897. * Returns the index in the input string of the first character following the
  898. * text matched during the previous match operation.
  899. *
  900. * @param status A reference to a UErrorCode to receive any errors. Possible
  901. * errors are U_REGEX_INVALID_STATE if no match has been
  902. * attempted or the last match failed.
  903. * @return the index of the last character matched, plus one.
  904. * The index value returned is a native index, corresponding to
  905. * code units for the underlying encoding type, for example,
  906. * a byte index for UTF-8.
  907. * @stable ICU 4.6
  908. */
  909. virtual int64_t end64(UErrorCode &status) const;
  910. /**
  911. * Returns the index in the input string of the character following the
  912. * text matched by the specified capture group during the previous match operation.
  913. *
  914. * @param group the capture group number
  915. * @param status A reference to a UErrorCode to receive any errors. Possible
  916. * errors are U_REGEX_INVALID_STATE if no match has been
  917. * attempted or the last match failed and
  918. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  919. * @return the index of the first character following the text
  920. * captured by the specified group during the previous match operation.
  921. * Return -1 if the capture group exists in the pattern but was not part of the match.
  922. * The index value returned is a native index, corresponding to
  923. * code units for the underlying encoding type, for example,
  924. * a byte index for UTF8.
  925. * @stable ICU 2.4
  926. */
  927. virtual int32_t end(int32_t group, UErrorCode &status) const;
  928. /**
  929. * Returns the index in the input string of the character following the
  930. * text matched by the specified capture group during the previous match operation.
  931. *
  932. * @param group the capture group number
  933. * @param status A reference to a UErrorCode to receive any errors. Possible
  934. * errors are U_REGEX_INVALID_STATE if no match has been
  935. * attempted or the last match failed and
  936. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  937. * @return the index of the first character following the text
  938. * captured by the specified group during the previous match operation.
  939. * Return -1 if the capture group exists in the pattern but was not part of the match.
  940. * The index value returned is a native index, corresponding to
  941. * code units for the underlying encoding type, for example,
  942. * a byte index for UTF8.
  943. * @stable ICU 4.6
  944. */
  945. virtual int64_t end64(int32_t group, UErrorCode &status) const;
  946. /**
  947. * Resets this matcher. The effect is to remove any memory of previous matches,
  948. * and to cause subsequent find() operations to begin at the beginning of
  949. * the input string.
  950. *
  951. * @return this RegexMatcher.
  952. * @stable ICU 2.4
  953. */
  954. virtual RegexMatcher &reset();
  955. /**
  956. * Resets this matcher, and set the current input position.
  957. * The effect is to remove any memory of previous matches,
  958. * and to cause subsequent find() operations to begin at
  959. * the specified (native) position in the input string.
  960. * <p>
  961. * The matcher's region is reset to its default, which is the entire
  962. * input string.
  963. * <p>
  964. * An alternative to this function is to set a match region
  965. * beginning at the desired index.
  966. *
  967. * @return this RegexMatcher.
  968. * @stable ICU 2.8
  969. */
  970. virtual RegexMatcher &reset(int64_t index, UErrorCode &status);
  971. /**
  972. * Resets this matcher with a new input string. This allows instances of RegexMatcher
  973. * to be reused, which is more efficient than creating a new RegexMatcher for
  974. * each input string to be processed.
  975. * @param input The new string on which subsequent pattern matches will operate.
  976. * The matcher retains a reference to the callers string, and operates
  977. * directly on that. Ownership of the string remains with the caller.
  978. * Because no copy of the string is made, it is essential that the
  979. * caller not delete the string until after regexp operations on it
  980. * are done.
  981. * Note that while a reset on the matcher with an input string that is then
  982. * modified across/during matcher operations may be supported currently for UnicodeString,
  983. * this was not originally intended behavior, and support for this is not guaranteed
  984. * in upcoming versions of ICU.
  985. * @return this RegexMatcher.
  986. * @stable ICU 2.4
  987. */
  988. virtual RegexMatcher &reset(const UnicodeString &input);
  989. /**
  990. * Resets this matcher with a new input string. This allows instances of RegexMatcher
  991. * to be reused, which is more efficient than creating a new RegexMatcher for
  992. * each input string to be processed.
  993. * @param input The new string on which subsequent pattern matches will operate.
  994. * The matcher makes a shallow clone of the given text; ownership of the
  995. * original string remains with the caller. Because no deep copy of the
  996. * text is made, it is essential that the caller not modify the string
  997. * until after regexp operations on it are done.
  998. * @return this RegexMatcher.
  999. *
  1000. * @stable ICU 4.6
  1001. */
  1002. virtual RegexMatcher &reset(UText *input);
  1003. /**
  1004. * Set the subject text string upon which the regular expression is looking for matches
  1005. * without changing any other aspect of the matching state.
  1006. * The new and previous text strings must have the same content.
  1007. *
  1008. * This function is intended for use in environments where ICU is operating on
  1009. * strings that may move around in memory. It provides a mechanism for notifying
  1010. * ICU that the string has been relocated, and providing a new UText to access the
  1011. * string in its new position.
  1012. *
  1013. * Note that the regular expression implementation never copies the underlying text
  1014. * of a string being matched, but always operates directly on the original text
  1015. * provided by the user. Refreshing simply drops the references to the old text
  1016. * and replaces them with references to the new.
  1017. *
  1018. * Caution: this function is normally used only by very specialized,
  1019. * system-level code. One example use case is with garbage collection that moves
  1020. * the text in memory.
  1021. *
  1022. * @param input The new (moved) text string.
  1023. * @param status Receives errors detected by this function.
  1024. *
  1025. * @stable ICU 4.8
  1026. */
  1027. virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status);
  1028. private:
  1029. /**
  1030. * Cause a compilation error if an application accidentally attempts to
  1031. * reset a matcher with a (UChar *) string as input rather than
  1032. * a UnicodeString. Avoids a dangling reference to a temporary string.
  1033. * <p>
  1034. * To efficiently work with UChar *strings, wrap the data in a UnicodeString
  1035. * using one of the aliasing constructors, such as
  1036. * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
  1037. * or in a UText, using
  1038. * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
  1039. *
  1040. */
  1041. RegexMatcher &reset(const UChar *input);
  1042. public:
  1043. /**
  1044. * Returns the input string being matched. Ownership of the string belongs to
  1045. * the matcher; it should not be altered or deleted. This method will work even if the input
  1046. * was originally supplied as a UText.
  1047. * @return the input string
  1048. * @stable ICU 2.4
  1049. */
  1050. virtual const UnicodeString &input() const;
  1051. /**
  1052. * Returns the input string being matched. This is the live input text; it should not be
  1053. * altered or deleted. This method will work even if the input was originally supplied as
  1054. * a UnicodeString.
  1055. * @return the input text
  1056. *
  1057. * @stable ICU 4.6
  1058. */
  1059. virtual UText *inputText() const;
  1060. /**
  1061. * Returns the input string being matched, either by copying it into the provided
  1062. * UText parameter or by returning a shallow clone of the live input. Note that copying
  1063. * the entire input may cause significant performance and memory issues.
  1064. * @param dest The UText into which the input should be copied, or NULL to create a new UText
  1065. * @param status error code
  1066. * @return dest if non-NULL, a shallow copy of the input text otherwise
  1067. *
  1068. * @stable ICU 4.6
  1069. */
  1070. virtual UText *getInput(UText *dest, UErrorCode &status) const;
  1071. /** Sets the limits of this matcher's region.
  1072. * The region is the part of the input string that will be searched to find a match.
  1073. * Invoking this method resets the matcher, and then sets the region to start
  1074. * at the index specified by the start parameter and end at the index specified
  1075. * by the end parameter.
  1076. *
  1077. * Depending on the transparency and anchoring being used (see useTransparentBounds
  1078. * and useAnchoringBounds), certain constructs such as anchors may behave differently
  1079. * at or around the boundaries of the region
  1080. *
  1081. * The function will fail if start is greater than limit, or if either index
  1082. * is less than zero or greater than the length of the string being matched.
  1083. *
  1084. * @param start The (native) index to begin searches at.
  1085. * @param limit The index to end searches at (exclusive).
  1086. * @param status A reference to a UErrorCode to receive any errors.
  1087. * @stable ICU 4.0
  1088. */
  1089. virtual RegexMatcher &region(int64_t start, int64_t limit, UErrorCode &status);
  1090. /**
  1091. * Identical to region(start, limit, status) but also allows a start position without
  1092. * resetting the region state.
  1093. * @param regionStart The region start
  1094. * @param regionLimit the limit of the region
  1095. * @param startIndex The (native) index within the region bounds at which to begin searches.
  1096. * @param status A reference to a UErrorCode to receive any errors.
  1097. * If startIndex is not within the specified region bounds,
  1098. * U_INDEX_OUTOFBOUNDS_ERROR is returned.
  1099. * @stable ICU 4.6
  1100. */
  1101. virtual RegexMatcher &region(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status);
  1102. /**
  1103. * Reports the start index of this matcher's region. The searches this matcher
  1104. * conducts are limited to finding matches within regionStart (inclusive) and
  1105. * regionEnd (exclusive).
  1106. *
  1107. * @return The starting (native) index of this matcher's region.
  1108. * @stable ICU 4.0
  1109. */
  1110. virtual int32_t regionStart() const;
  1111. /**
  1112. * Reports the start index of this matcher's region. The searches this matcher
  1113. * conducts are limited to finding matches within regionStart (inclusive) and
  1114. * regionEnd (exclusive).
  1115. *
  1116. * @return The starting (native) index of this matcher's region.
  1117. * @stable ICU 4.6
  1118. */
  1119. virtual int64_t regionStart64() const;
  1120. /**
  1121. * Reports the end (limit) index (exclusive) of this matcher's region. The searches
  1122. * this matcher conducts are limited to finding matches within regionStart
  1123. * (inclusive) and regionEnd (exclusive).
  1124. *
  1125. * @return The ending point (native) of this matcher's region.
  1126. * @stable ICU 4.0
  1127. */
  1128. virtual int32_t regionEnd() const;
  1129. /**
  1130. * Reports the end (limit) index (exclusive) of this matcher's region. The searches
  1131. * this matcher conducts are limited to finding matches within regionStart
  1132. * (inclusive) and regionEnd (exclusive).
  1133. *
  1134. * @return The ending point (native) of this matcher's region.
  1135. * @stable ICU 4.6
  1136. */
  1137. virtual int64_t regionEnd64() const;
  1138. /**
  1139. * Queries the transparency of region bounds for this matcher.
  1140. * See useTransparentBounds for a description of transparent and opaque bounds.
  1141. * By default, a matcher uses opaque region boundaries.
  1142. *
  1143. * @return TRUE if this matcher is using opaque bounds, false if it is not.
  1144. * @stable ICU 4.0
  1145. */
  1146. virtual UBool hasTransparentBounds() const;
  1147. /**
  1148. * Sets the transparency of region bounds for this matcher.
  1149. * Invoking this function with an argument of true will set this matcher to use transparent bounds.
  1150. * If the boolean argument is false, then opaque bounds will be used.
  1151. *
  1152. * Using transparent bounds, the boundaries of this matcher's region are transparent
  1153. * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
  1154. * see text beyond the boundaries of the region while checking for a match.
  1155. *
  1156. * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
  1157. * lookbehind, and boundary matching constructs.
  1158. *
  1159. * By default, a matcher uses opaque bounds.
  1160. *
  1161. * @param b TRUE for transparent bounds; FALSE for opaque bounds
  1162. * @return This Matcher;
  1163. * @stable ICU 4.0
  1164. **/
  1165. virtual RegexMatcher &useTransparentBounds(UBool b);
  1166. /**
  1167. * Return true if this matcher is using anchoring bounds.
  1168. * By default, matchers use anchoring region bounds.
  1169. *
  1170. * @return TRUE if this matcher is using anchoring bounds.
  1171. * @stable ICU 4.0
  1172. */
  1173. virtual UBool hasAnchoringBounds() const;
  1174. /**
  1175. * Set whether this matcher is using Anchoring Bounds for its region.
  1176. * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
  1177. * and end of the region. Without Anchoring Bounds, anchors will only match at
  1178. * the positions they would in the complete text.
  1179. *
  1180. * Anchoring Bounds are the default for regions.
  1181. *
  1182. * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
  1183. * @return This Matcher
  1184. * @stable ICU 4.0
  1185. */
  1186. virtual RegexMatcher &useAnchoringBounds(UBool b);
  1187. /**
  1188. * Return TRUE if the most recent matching operation attempted to access
  1189. * additional input beyond the available input text.
  1190. * In this case, additional input text could change the results of the match.
  1191. *
  1192. * hitEnd() is defined for both successful and unsuccessful matches.
  1193. * In either case hitEnd() will return TRUE if if the end of the text was
  1194. * reached at any point during the matching process.
  1195. *
  1196. * @return TRUE if the most recent match hit the end of input
  1197. * @stable ICU 4.0
  1198. */
  1199. virtual UBool hitEnd() const;
  1200. /**
  1201. * Return TRUE the most recent match succeeded and additional input could cause
  1202. * it to fail. If this method returns false and a match was found, then more input
  1203. * might change the match but the match won't be lost. If a match was not found,
  1204. * then requireEnd has no meaning.
  1205. *
  1206. * @return TRUE if more input could cause the most recent match to no longer match.
  1207. * @stable ICU 4.0
  1208. */
  1209. virtual UBool requireEnd() const;
  1210. /**
  1211. * Returns the pattern that is interpreted by this matcher.
  1212. * @return the RegexPattern for this RegexMatcher
  1213. * @stable ICU 2.4
  1214. */
  1215. virtual const RegexPattern &pattern() const;
  1216. /**
  1217. * Replaces every substring of the input that matches the pattern
  1218. * with the given replacement string. This is a convenience function that
  1219. * provides a complete find-and-replace-all operation.
  1220. *
  1221. * This method first resets this matcher. It then scans the input string
  1222. * looking for matches of the pattern. Input that is not part of any
  1223. * match is left unchanged; each match is replaced in the result by the
  1224. * replacement string. The replacement string may contain references to
  1225. * capture groups.
  1226. *
  1227. * @param replacement a string containing the replacement text.
  1228. * @param status a reference to a UErrorCode to receive any errors.
  1229. * @return a string containing the results of the find and replace.
  1230. * @stable ICU 2.4
  1231. */
  1232. virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status);
  1233. /**
  1234. * Replaces every substring of the input that matches the pattern
  1235. * with the given replacement string. This is a convenience function that
  1236. * provides a complete find-and-replace-all operation.
  1237. *
  1238. * This method first resets this matcher. It then scans the input string
  1239. * looking for matches of the pattern. Input that is not part of any
  1240. * match is left unchanged; each match is replaced in the result by the
  1241. * replacement string. The replacement string may contain references to
  1242. * capture groups.
  1243. *
  1244. * @param replacement a string containing the replacement text.
  1245. * @param dest a mutable UText in which the results are placed.
  1246. * If NULL, a new UText will be created (which may not be mutable).
  1247. * @param status a reference to a UErrorCode to receive any errors.
  1248. * @return a string containing the results of the find and replace.
  1249. * If a pre-allocated UText was provided, it will always be used and returned.
  1250. *
  1251. * @stable ICU 4.6
  1252. */
  1253. virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status);
  1254. /**
  1255. * Replaces the first substring of the input that matches
  1256. * the pattern with the replacement string. This is a convenience
  1257. * function that provides a complete find-and-replace operation.
  1258. *
  1259. * <p>This function first resets this RegexMatcher. It then scans the input string
  1260. * looking for a match of the pattern. Input that is not part
  1261. * of the match is appended directly to the result string; the match is replaced
  1262. * in the result by the replacement string. The replacement string may contain
  1263. * references to captured groups.</p>
  1264. *
  1265. * <p>The state of the matcher (the position at which a subsequent find()
  1266. * would begin) after completing a replaceFirst() is not specified. The
  1267. * RegexMatcher should be reset before doing additional find() operations.</p>
  1268. *
  1269. * @param replacement a string containing the replacement text.
  1270. * @param status a reference to a UErrorCode to receive any errors.
  1271. * @return a string containing the results of the find and replace.
  1272. * @stable ICU 2.4
  1273. */
  1274. virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status);
  1275. /**
  1276. * Replaces the first substring of the input that matches
  1277. * the pattern with the replacement string. This is a convenience
  1278. * function that provides a complete find-and-replace operation.
  1279. *
  1280. * <p>This function first resets this RegexMatcher. It then scans the input string
  1281. * looking for a match of the pattern. Input that is not part
  1282. * of the match is appended directly to the result string; the match is replaced
  1283. * in the result by the replacement string. The replacement string may contain
  1284. * references to captured groups.</p>
  1285. *
  1286. * <p>The state of the matcher (the position at which a subsequent find()
  1287. * would begin) after completing a replaceFirst() is not specified. The
  1288. * RegexMatcher should be reset before doing additional find() operations.</p>
  1289. *
  1290. * @param replacement a string containing the replacement text.
  1291. * @param dest a mutable UText in which the results are placed.
  1292. * If NULL, a new UText will be created (which may not be mutable).
  1293. * @param status a reference to a UErrorCode to receive any errors.
  1294. * @return a string containing the results of the find and replace.
  1295. * If a pre-allocated UText was provided, it will always be used and returned.
  1296. *
  1297. * @stable ICU 4.6
  1298. */
  1299. virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status);
  1300. /**
  1301. * Implements a replace operation intended to be used as part of an
  1302. * incremental find-and-replace.
  1303. *
  1304. * <p>The input string, starting from the end of the previous replacement and ending at
  1305. * the start of the current match, is appended to the destination string. Then the
  1306. * replacement string is appended to the output string,
  1307. * including handling any substitutions of captured text.</p>
  1308. *
  1309. * <p>For simple, prepackaged, non-incremental find-and-replace
  1310. * operations, see replaceFirst() or replaceAll().</p>
  1311. *
  1312. * @param dest A UnicodeString to which the results of the find-and-replace are appended.
  1313. * @param replacement A UnicodeString that provides the text to be substituted for
  1314. * the input text that matched the regexp pattern. The replacement
  1315. * text may contain references to captured text from the
  1316. * input.
  1317. * @param status A reference to a UErrorCode to receive any errors. Possible
  1318. * errors are U_REGEX_INVALID_STATE if no match has been
  1319. * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
  1320. * if the replacement text specifies a capture group that
  1321. * does not exist in the pattern.
  1322. *
  1323. * @return this RegexMatcher
  1324. * @stable ICU 2.4
  1325. *
  1326. */
  1327. virtual RegexMatcher &appendReplacement(UnicodeString &dest,
  1328. const UnicodeString &replacement, UErrorCode &status);
  1329. /**
  1330. * Implements a replace operation intended to be used as part of an
  1331. * incremental find-and-replace.
  1332. *
  1333. * <p>The input string, starting from the end of the previous replacement and ending at
  1334. * the start of the current match, is appended to the destination string. Then the
  1335. * replacement string is appended to the output string,
  1336. * including handling any substitutions of captured text.</p>
  1337. *
  1338. * <p>For simple, prepackaged, non-incremental find-and-replace
  1339. * operations, see replaceFirst() or replaceAll().</p>
  1340. *
  1341. * @param dest A mutable UText to which the results of the find-and-replace are appended.
  1342. * Must not be NULL.
  1343. * @param replacement A UText that provides the text to be substituted for
  1344. * the input text that matched the regexp pattern. The replacement
  1345. * text may contain references to captured text from the input.
  1346. * @param status A reference to a UErrorCode to receive any errors. Possible
  1347. * errors are U_REGEX_INVALID_STATE if no match has been
  1348. * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
  1349. * if the replacement text specifies a capture group that
  1350. * does not exist in the pattern.
  1351. *
  1352. * @return this RegexMatcher
  1353. *
  1354. * @stable ICU 4.6
  1355. */
  1356. virtual RegexMatcher &appendReplacement(UText *dest,
  1357. UText *replacement, UErrorCode &status);
  1358. /**
  1359. * As the final step in a find-and-replace operation, append the remainder
  1360. * of the input string, starting at the position following the last appendReplacement(),
  1361. * to the destination string. <code>appendTail()</code> is intended to be invoked after one
  1362. * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
  1363. *
  1364. * @param dest A UnicodeString to which the results of the find-and-replace are appended.
  1365. * @return the destination string.
  1366. * @stable ICU 2.4
  1367. */
  1368. virtual UnicodeString &appendTail(UnicodeString &dest);
  1369. /**
  1370. * As the final step in a find-and-replace operation, append the remainder
  1371. * of the input string, starting at the position following the last appendReplacement(),
  1372. * to the destination string. <code>appendTail()</code> is intended to be invoked after one
  1373. * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
  1374. *
  1375. * @param dest A mutable UText to which the results of the find-and-replace are appended.
  1376. * Must not be NULL.
  1377. * @param status error cod
  1378. * @return the destination string.
  1379. *
  1380. * @stable ICU 4.6
  1381. */
  1382. virtual UText *appendTail(UText *dest, UErrorCode &status);
  1383. /**
  1384. * Split a string into fields. Somewhat like split() from Perl.
  1385. * The pattern matches identify delimiters that separate the input
  1386. * into fields. The input data between the matches becomes the
  1387. * fields themselves.
  1388. *
  1389. * @param input The string to be split into fields. The field delimiters
  1390. * match the pattern (in the "this" object). This matcher
  1391. * will be reset to this input string.
  1392. * @param dest An array of UnicodeStrings to receive the results of the split.
  1393. * This is an array of actual UnicodeString objects, not an
  1394. * array of pointers to strings. Local (stack based) arrays can
  1395. * work well here.
  1396. * @param destCapacity The number of elements in the destination array.
  1397. * If the number of fields found is less than destCapacity, the
  1398. * extra strings in the destination array are not altered.
  1399. * If the number of destination strings is less than the number
  1400. * of fields, the trailing part of the input string, including any
  1401. * field delimiters, is placed in the last destination string.
  1402. * @param status A reference to a UErrorCode to receive any errors.
  1403. * @return The number of fields into which the input string was split.
  1404. * @stable ICU 2.6
  1405. */
  1406. virtual int32_t split(const UnicodeString &input,
  1407. UnicodeString dest[],
  1408. int32_t destCapacity,
  1409. UErrorCode &status);
  1410. /**
  1411. * Split a string into fields. Somewhat like split() from Perl.
  1412. * The pattern matches identify delimiters that separate the input
  1413. * into fields. The input data between the matches becomes the
  1414. * fields themselves.
  1415. *
  1416. * @param input The string to be split into fields. The field delimiters
  1417. * match the pattern (in the "this" object). This matcher
  1418. * will be reset to this input string.
  1419. * @param dest An array of mutable UText structs to receive the results of the split.
  1420. * If a field is NULL, a new UText is allocated to contain the results for
  1421. * that field. This new UText is not guaranteed to be mutable.
  1422. * @param destCapacity The number of elements in the destination array.
  1423. * If the number of fields found is less than destCapacity, the
  1424. * extra strings in the destination array are not altered.
  1425. * If the number of destination strings is less than the number
  1426. * of fields, the trailing part of the input string, including any
  1427. * field delimiters, is placed in the last destination string.
  1428. * @param status A reference to a UErrorCode to receive any errors.
  1429. * @return The number of fields into which the input string was split.
  1430. *
  1431. * @stable ICU 4.6
  1432. */
  1433. virtual int32_t split(UText *input,
  1434. UText *dest[],
  1435. int32_t destCapacity,
  1436. UErrorCode &status);
  1437. /**
  1438. * Set a processing time limit for match operations with this Matcher.
  1439. *
  1440. * Some patterns, when matching certain strings, can run in exponential time.
  1441. * For practical purposes, the match operation may appear to be in an
  1442. * infinite loop.
  1443. * When a limit is set a match operation will fail with an error if the
  1444. * limit is exceeded.
  1445. * <p>
  1446. * The units of the limit are steps of the match engine.
  1447. * Correspondence with actual processor time will depend on the speed
  1448. * of the processor and the details of the specific pattern, but will
  1449. * typically be on the order of milliseconds.
  1450. * <p>
  1451. * By default, the matching time is not limited.
  1452. * <p>
  1453. *
  1454. * @param limit The limit value, or 0 for no limit.
  1455. * @param status A reference to a UErrorCode to receive any errors.
  1456. * @stable ICU 4.0
  1457. */
  1458. virtual void setTimeLimit(int32_t limit, UErrorCode &status);
  1459. /**
  1460. * Get the time limit, if any, for match operations made with this Matcher.
  1461. *
  1462. * @return the maximum allowed time for a match, in units of processing steps.
  1463. * @stable ICU 4.0
  1464. */
  1465. virtual int32_t getTimeLimit() const;
  1466. /**
  1467. * Set the amount of heap storage available for use by the match backtracking stack.
  1468. * The matcher is also reset, discarding any results from previous matches.
  1469. * <p>
  1470. * ICU uses a backtracking regular expression engine, with the backtrack stack
  1471. * maintained on the heap. This function sets the limit to the amount of memory
  1472. * that can be used for this purpose. A backtracking stack overflow will
  1473. * result in an error from the match operation that caused it.
  1474. * <p>
  1475. * A limit is desirable because a malicious or poorly designed pattern can use
  1476. * excessive memory, potentially crashing the process. A limit is enabled
  1477. * by default.
  1478. * <p>
  1479. * @param limit The maximum size, in bytes, of the matching backtrack stack.
  1480. * A value of zero means no limit.
  1481. * The limit must be greater or equal to zero.
  1482. *
  1483. * @param status A reference to a UErrorCode to receive any errors.
  1484. *
  1485. * @stable ICU 4.0
  1486. */
  1487. virtual void setStackLimit(int32_t limit, UErrorCode &status);
  1488. /**
  1489. * Get the size of the heap storage available for use by the back tracking stack.
  1490. *
  1491. * @return the maximum backtracking stack size, in bytes, or zero if the
  1492. * stack size is unlimited.
  1493. * @stable ICU 4.0
  1494. */
  1495. virtual int32_t getStackLimit() const;
  1496. /**
  1497. * Set a callback function for use with this Matcher.
  1498. * During matching operations the function will be called periodically,
  1499. * giving the application the opportunity to terminate a long-running
  1500. * match.
  1501. *
  1502. * @param callback A pointer to the user-supplied callback function.
  1503. * @param context User context pointer. The value supplied at the
  1504. * time the callback function is set will be saved
  1505. * and passed to the callback each time that it is called.
  1506. * @param status A reference to a UErrorCode to receive any errors.
  1507. * @stable ICU 4.0
  1508. */
  1509. virtual void setMatchCallback(URegexMatchCallback *callback,
  1510. const void *context,
  1511. UErrorCode &status);
  1512. /**
  1513. * Get the callback function for this URegularExpression.
  1514. *
  1515. * @param callback Out parameter, receives a pointer to the user-supplied
  1516. * callback function.
  1517. * @param context Out parameter, receives the user context pointer that
  1518. * was set when uregex_setMatchCallback() was called.
  1519. * @param status A reference to a UErrorCode to receive any errors.
  1520. * @stable ICU 4.0
  1521. */
  1522. virtual void getMatchCallback(URegexMatchCallback *&callback,
  1523. const void *&context,
  1524. UErrorCode &status);
  1525. /**
  1526. * Set a progress callback function for use with find operations on this Matcher.
  1527. * During find operations, the callback will be invoked after each return from a
  1528. * match attempt, giving the application the opportunity to terminate a long-running
  1529. * find operation.
  1530. *
  1531. * @param callback A pointer to the user-supplied callback function.
  1532. * @param context User context pointer. The value supplied at the
  1533. * time the callback function is set will be saved
  1534. * and passed to the callback each time that it is called.
  1535. * @param status A reference to a UErrorCode to receive any errors.
  1536. * @stable ICU 4.6
  1537. */
  1538. virtual void setFindProgressCallback(URegexFindProgressCallback *callback,
  1539. const void *context,
  1540. UErrorCode &status);
  1541. /**
  1542. * Get the find progress callback function for this URegularExpression.
  1543. *
  1544. * @param callback Out parameter, receives a pointer to the user-supplied
  1545. * callback function.
  1546. * @param context Out parameter, receives the user context pointer that
  1547. * was set when uregex_setFindProgressCallback() was called.
  1548. * @param status A reference to a UErrorCode to receive any errors.
  1549. * @stable ICU 4.6
  1550. */
  1551. virtual void getFindProgressCallback(URegexFindProgressCallback *&callback,
  1552. const void *&context,
  1553. UErrorCode &status);
  1554. #ifndef U_HIDE_INTERNAL_API
  1555. /**
  1556. * setTrace Debug function, enable/disable tracing of the matching engine.
  1557. * For internal ICU development use only. DO NO USE!!!!
  1558. * @internal
  1559. */
  1560. void setTrace(UBool state);
  1561. #endif /* U_HIDE_INTERNAL_API */
  1562. /**
  1563. * ICU "poor man's RTTI", returns a UClassID for this class.
  1564. *
  1565. * @stable ICU 2.2
  1566. */
  1567. static UClassID U_EXPORT2 getStaticClassID();
  1568. /**
  1569. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  1570. *
  1571. * @stable ICU 2.2
  1572. */
  1573. virtual UClassID getDynamicClassID() const;
  1574. private:
  1575. // Constructors and other object boilerplate are private.
  1576. // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
  1577. RegexMatcher(); // default constructor not implemented
  1578. RegexMatcher(const RegexPattern *pat);
  1579. RegexMatcher(const RegexMatcher &other);
  1580. RegexMatcher &operator =(const RegexMatcher &rhs);
  1581. void init(UErrorCode &status); // Common initialization
  1582. void init2(UText *t, UErrorCode &e); // Common initialization, part 2.
  1583. friend class RegexPattern;
  1584. friend class RegexCImpl;
  1585. public:
  1586. #ifndef U_HIDE_INTERNAL_API
  1587. /** @internal */
  1588. void resetPreserveRegion(); // Reset matcher state, but preserve any region.
  1589. #endif /* U_HIDE_INTERNAL_API */
  1590. private:
  1591. //
  1592. // MatchAt This is the internal interface to the match engine itself.
  1593. // Match status comes back in matcher member variables.
  1594. //
  1595. void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status);
  1596. inline void backTrack(int64_t &inputIdx, int32_t &patIdx);
  1597. UBool isWordBoundary(int64_t pos); // perform Perl-like \b test
  1598. UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test
  1599. REStackFrame *resetStack();
  1600. inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status);
  1601. void IncrementTime(UErrorCode &status);
  1602. // Call user find callback function, if set. Return TRUE if operation should be interrupted.
  1603. inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status);
  1604. int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const;
  1605. UBool findUsingChunk(UErrorCode &status);
  1606. void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status);
  1607. UBool isChunkWordBoundary(int32_t pos);
  1608. const RegexPattern *fPattern;
  1609. RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and
  1610. // should delete it when through.
  1611. const UnicodeString *fInput; // The string being matched. Only used for input()
  1612. UText *fInputText; // The text being matched. Is never NULL.
  1613. UText *fAltInputText; // A shallow copy of the text being matched.
  1614. // Only created if the pattern contains backreferences.
  1615. int64_t fInputLength; // Full length of the input text.
  1616. int32_t fFrameSize; // The size of a frame in the backtrack stack.
  1617. int64_t fRegionStart; // Start of the input region, default = 0.
  1618. int64_t fRegionLimit; // End of input region, default to input.length.
  1619. int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $).
  1620. int64_t fAnchorLimit; // See useAnchoringBounds
  1621. int64_t fLookStart; // Region bounds for look-ahead/behind and
  1622. int64_t fLookLimit; // and other boundary tests. See
  1623. // useTransparentBounds
  1624. int64_t fActiveStart; // Currently active bounds for matching.
  1625. int64_t fActiveLimit; // Usually is the same as region, but
  1626. // is changed to fLookStart/Limit when
  1627. // entering look around regions.
  1628. UBool fTransparentBounds; // True if using transparent bounds.
  1629. UBool fAnchoringBounds; // True if using anchoring bounds.
  1630. UBool fMatch; // True if the last attempted match was successful.
  1631. int64_t fMatchStart; // Position of the start of the most recent match
  1632. int64_t fMatchEnd; // First position after the end of the most recent match
  1633. // Zero if no previous match, even when a region
  1634. // is active.
  1635. int64_t fLastMatchEnd; // First position after the end of the previous match,
  1636. // or -1 if there was no previous match.
  1637. int64_t fAppendPosition; // First position after the end of the previous
  1638. // appendReplacement(). As described by the
  1639. // JavaDoc for Java Matcher, where it is called
  1640. // "append position"
  1641. UBool fHitEnd; // True if the last match touched the end of input.
  1642. UBool fRequireEnd; // True if the last match required end-of-input
  1643. // (matched $ or Z)
  1644. UVector64 *fStack;
  1645. REStackFrame *fFrame; // After finding a match, the last active stack frame,
  1646. // which will contain the capture group results.
  1647. // NOT valid while match engine is running.
  1648. int64_t *fData; // Data area for use by the compiled pattern.
  1649. int64_t fSmallData[8]; // Use this for data if it's enough.
  1650. int32_t fTimeLimit; // Max time (in arbitrary steps) to let the
  1651. // match engine run. Zero for unlimited.
  1652. int32_t fTime; // Match time, accumulates while matching.
  1653. int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves.
  1654. // Kept separately from fTime to keep as much
  1655. // code as possible out of the inline
  1656. // StateSave function.
  1657. int32_t fStackLimit; // Maximum memory size to use for the backtrack
  1658. // stack, in bytes. Zero for unlimited.
  1659. URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct.
  1660. // NULL if there is no callback.
  1661. const void *fCallbackContext; // User Context ptr for callback function.
  1662. URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct.
  1663. // NULL if there is no callback.
  1664. const void *fFindProgressCallbackContext; // User Context ptr for callback function.
  1665. UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
  1666. UBool fTraceDebug; // Set true for debug tracing of match engine.
  1667. UErrorCode fDeferredStatus; // Save error state that cannot be immediately
  1668. // reported, or that permanently disables this matcher.
  1669. RuleBasedBreakIterator *fWordBreakItr;
  1670. };
  1671. U_NAMESPACE_END
  1672. #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
  1673. #endif