regex.h 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 2002-2016, 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. * @stable 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. * @stable 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. * @stable 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. * A zero length string is returned both for capture groups that did not
  793. * participate in the match and for actual zero length matches.
  794. * To distinguish between these two cases use the function start(),
  795. * which returns -1 for non-participating groups.
  796. *
  797. * @param groupNum the capture group number
  798. * @param status A reference to a UErrorCode to receive any errors.
  799. * Possible errors are U_REGEX_INVALID_STATE if no match
  800. * has been attempted or the last match failed and
  801. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  802. * @return the captured text
  803. * @stable ICU 2.4
  804. */
  805. virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const;
  806. /**
  807. * Returns the number of capturing groups in this matcher's pattern.
  808. * @return the number of capture groups
  809. * @stable ICU 2.4
  810. */
  811. virtual int32_t groupCount() const;
  812. /**
  813. * Returns a shallow clone of the entire live input string with the UText current native index
  814. * set to the beginning of the requested group.
  815. *
  816. * @param dest The UText into which the input should be cloned, or NULL to create a new UText
  817. * @param group_len A reference to receive the length of the desired capture group
  818. * @param status A reference to a UErrorCode to receive any errors.
  819. * Possible errors are U_REGEX_INVALID_STATE if no match
  820. * has been attempted or the last match failed and
  821. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  822. * @return dest if non-NULL, a shallow copy of the input text otherwise
  823. *
  824. * @stable ICU 4.6
  825. */
  826. virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const;
  827. /**
  828. * Returns a shallow clone of the entire live input string with the UText current native index
  829. * set to the beginning of the requested group.
  830. *
  831. * A group length of zero is returned both for capture groups that did not
  832. * participate in the match and for actual zero length matches.
  833. * To distinguish between these two cases use the function start(),
  834. * which returns -1 for non-participating groups.
  835. *
  836. * @param groupNum The capture group number.
  837. * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
  838. * @param group_len A reference to receive the length of the desired capture group
  839. * @param status A reference to a UErrorCode to receive any errors.
  840. * Possible errors are U_REGEX_INVALID_STATE if no match
  841. * has been attempted or the last match failed and
  842. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  843. * @return dest if non-NULL, a shallow copy of the input text otherwise
  844. *
  845. * @stable ICU 4.6
  846. */
  847. virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const;
  848. /**
  849. * Returns the index in the input string of the start of the text matched
  850. * during the previous match operation.
  851. * @param status a reference to a UErrorCode to receive any errors.
  852. * @return The (native) position in the input string of the start of the last match.
  853. * @stable ICU 2.4
  854. */
  855. virtual int32_t start(UErrorCode &status) const;
  856. /**
  857. * Returns the index in the input string of the start of the text matched
  858. * during the previous match operation.
  859. * @param status a reference to a UErrorCode to receive any errors.
  860. * @return The (native) position in the input string of the start of the last match.
  861. * @stable ICU 4.6
  862. */
  863. virtual int64_t start64(UErrorCode &status) const;
  864. /**
  865. * Returns the index in the input string of the start of the text matched by the
  866. * specified capture group during the previous match operation. Return -1 if
  867. * the capture group exists in the pattern, but was not part of the last match.
  868. *
  869. * @param group the capture group number
  870. * @param status A reference to a UErrorCode to receive any errors. Possible
  871. * errors are U_REGEX_INVALID_STATE if no match has been
  872. * attempted or the last match failed, and
  873. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  874. * @return the (native) start position of substring matched by the specified group.
  875. * @stable ICU 2.4
  876. */
  877. virtual int32_t start(int32_t group, UErrorCode &status) const;
  878. /**
  879. * Returns the index in the input string of the start of the text matched by the
  880. * specified capture group during the previous match operation. Return -1 if
  881. * the capture group exists in the pattern, but was not part of the last match.
  882. *
  883. * @param group the capture group number.
  884. * @param status A reference to a UErrorCode to receive any errors. Possible
  885. * errors are U_REGEX_INVALID_STATE if no match has been
  886. * attempted or the last match failed, and
  887. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
  888. * @return the (native) start position of substring matched by the specified group.
  889. * @stable ICU 4.6
  890. */
  891. virtual int64_t start64(int32_t group, UErrorCode &status) const;
  892. /**
  893. * Returns the index in the input string of the first character following the
  894. * text matched during the previous match operation.
  895. *
  896. * @param status A reference to a UErrorCode to receive any errors. Possible
  897. * errors are U_REGEX_INVALID_STATE if no match has been
  898. * attempted or the last match failed.
  899. * @return the index of the last character matched, plus one.
  900. * The index value returned is a native index, corresponding to
  901. * code units for the underlying encoding type, for example,
  902. * a byte index for UTF-8.
  903. * @stable ICU 2.4
  904. */
  905. virtual int32_t end(UErrorCode &status) const;
  906. /**
  907. * Returns the index in the input string of the first character following the
  908. * text matched during the previous match operation.
  909. *
  910. * @param status A reference to a UErrorCode to receive any errors. Possible
  911. * errors are U_REGEX_INVALID_STATE if no match has been
  912. * attempted or the last match failed.
  913. * @return the index of the last character matched, plus one.
  914. * The index value returned is a native index, corresponding to
  915. * code units for the underlying encoding type, for example,
  916. * a byte index for UTF-8.
  917. * @stable ICU 4.6
  918. */
  919. virtual int64_t end64(UErrorCode &status) const;
  920. /**
  921. * Returns the index in the input string of the character following the
  922. * text matched by the specified capture group during the previous match operation.
  923. *
  924. * @param group the capture group number
  925. * @param status A reference to a UErrorCode to receive any errors. Possible
  926. * errors are U_REGEX_INVALID_STATE if no match has been
  927. * attempted or the last match failed and
  928. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  929. * @return the index of the first character following the text
  930. * captured by the specified group during the previous match operation.
  931. * Return -1 if the capture group exists in the pattern but was not part of the match.
  932. * The index value returned is a native index, corresponding to
  933. * code units for the underlying encoding type, for example,
  934. * a byte index for UTF8.
  935. * @stable ICU 2.4
  936. */
  937. virtual int32_t end(int32_t group, UErrorCode &status) const;
  938. /**
  939. * Returns the index in the input string of the character following the
  940. * text matched by the specified capture group during the previous match operation.
  941. *
  942. * @param group the capture group number
  943. * @param status A reference to a UErrorCode to receive any errors. Possible
  944. * errors are U_REGEX_INVALID_STATE if no match has been
  945. * attempted or the last match failed and
  946. * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
  947. * @return the index of the first character following the text
  948. * captured by the specified group during the previous match operation.
  949. * Return -1 if the capture group exists in the pattern but was not part of the match.
  950. * The index value returned is a native index, corresponding to
  951. * code units for the underlying encoding type, for example,
  952. * a byte index for UTF8.
  953. * @stable ICU 4.6
  954. */
  955. virtual int64_t end64(int32_t group, UErrorCode &status) const;
  956. /**
  957. * Resets this matcher. The effect is to remove any memory of previous matches,
  958. * and to cause subsequent find() operations to begin at the beginning of
  959. * the input string.
  960. *
  961. * @return this RegexMatcher.
  962. * @stable ICU 2.4
  963. */
  964. virtual RegexMatcher &reset();
  965. /**
  966. * Resets this matcher, and set the current input position.
  967. * The effect is to remove any memory of previous matches,
  968. * and to cause subsequent find() operations to begin at
  969. * the specified (native) position in the input string.
  970. * <p>
  971. * The matcher's region is reset to its default, which is the entire
  972. * input string.
  973. * <p>
  974. * An alternative to this function is to set a match region
  975. * beginning at the desired index.
  976. *
  977. * @return this RegexMatcher.
  978. * @stable ICU 2.8
  979. */
  980. virtual RegexMatcher &reset(int64_t index, UErrorCode &status);
  981. /**
  982. * Resets this matcher with a new input string. This allows instances of RegexMatcher
  983. * to be reused, which is more efficient than creating a new RegexMatcher for
  984. * each input string to be processed.
  985. * @param input The new string on which subsequent pattern matches will operate.
  986. * The matcher retains a reference to the callers string, and operates
  987. * directly on that. Ownership of the string remains with the caller.
  988. * Because no copy of the string is made, it is essential that the
  989. * caller not delete the string until after regexp operations on it
  990. * are done.
  991. * Note that while a reset on the matcher with an input string that is then
  992. * modified across/during matcher operations may be supported currently for UnicodeString,
  993. * this was not originally intended behavior, and support for this is not guaranteed
  994. * in upcoming versions of ICU.
  995. * @return this RegexMatcher.
  996. * @stable ICU 2.4
  997. */
  998. virtual RegexMatcher &reset(const UnicodeString &input);
  999. /**
  1000. * Resets this matcher with a new input string. This allows instances of RegexMatcher
  1001. * to be reused, which is more efficient than creating a new RegexMatcher for
  1002. * each input string to be processed.
  1003. * @param input The new string on which subsequent pattern matches will operate.
  1004. * The matcher makes a shallow clone of the given text; ownership of the
  1005. * original string remains with the caller. Because no deep copy of the
  1006. * text is made, it is essential that the caller not modify the string
  1007. * until after regexp operations on it are done.
  1008. * @return this RegexMatcher.
  1009. *
  1010. * @stable ICU 4.6
  1011. */
  1012. virtual RegexMatcher &reset(UText *input);
  1013. /**
  1014. * Set the subject text string upon which the regular expression is looking for matches
  1015. * without changing any other aspect of the matching state.
  1016. * The new and previous text strings must have the same content.
  1017. *
  1018. * This function is intended for use in environments where ICU is operating on
  1019. * strings that may move around in memory. It provides a mechanism for notifying
  1020. * ICU that the string has been relocated, and providing a new UText to access the
  1021. * string in its new position.
  1022. *
  1023. * Note that the regular expression implementation never copies the underlying text
  1024. * of a string being matched, but always operates directly on the original text
  1025. * provided by the user. Refreshing simply drops the references to the old text
  1026. * and replaces them with references to the new.
  1027. *
  1028. * Caution: this function is normally used only by very specialized,
  1029. * system-level code. One example use case is with garbage collection that moves
  1030. * the text in memory.
  1031. *
  1032. * @param input The new (moved) text string.
  1033. * @param status Receives errors detected by this function.
  1034. *
  1035. * @stable ICU 4.8
  1036. */
  1037. virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status);
  1038. private:
  1039. /**
  1040. * Cause a compilation error if an application accidentally attempts to
  1041. * reset a matcher with a (UChar *) string as input rather than
  1042. * a UnicodeString. Avoids a dangling reference to a temporary string.
  1043. * <p>
  1044. * To efficiently work with UChar *strings, wrap the data in a UnicodeString
  1045. * using one of the aliasing constructors, such as
  1046. * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
  1047. * or in a UText, using
  1048. * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
  1049. *
  1050. */
  1051. RegexMatcher &reset(const UChar *input);
  1052. public:
  1053. /**
  1054. * Returns the input string being matched. Ownership of the string belongs to
  1055. * the matcher; it should not be altered or deleted. This method will work even if the input
  1056. * was originally supplied as a UText.
  1057. * @return the input string
  1058. * @stable ICU 2.4
  1059. */
  1060. virtual const UnicodeString &input() const;
  1061. /**
  1062. * Returns the input string being matched. This is the live input text; it should not be
  1063. * altered or deleted. This method will work even if the input was originally supplied as
  1064. * a UnicodeString.
  1065. * @return the input text
  1066. *
  1067. * @stable ICU 4.6
  1068. */
  1069. virtual UText *inputText() const;
  1070. /**
  1071. * Returns the input string being matched, either by copying it into the provided
  1072. * UText parameter or by returning a shallow clone of the live input. Note that copying
  1073. * the entire input may cause significant performance and memory issues.
  1074. * @param dest The UText into which the input should be copied, or NULL to create a new UText
  1075. * @param status error code
  1076. * @return dest if non-NULL, a shallow copy of the input text otherwise
  1077. *
  1078. * @stable ICU 4.6
  1079. */
  1080. virtual UText *getInput(UText *dest, UErrorCode &status) const;
  1081. /** Sets the limits of this matcher's region.
  1082. * The region is the part of the input string that will be searched to find a match.
  1083. * Invoking this method resets the matcher, and then sets the region to start
  1084. * at the index specified by the start parameter and end at the index specified
  1085. * by the end parameter.
  1086. *
  1087. * Depending on the transparency and anchoring being used (see useTransparentBounds
  1088. * and useAnchoringBounds), certain constructs such as anchors may behave differently
  1089. * at or around the boundaries of the region
  1090. *
  1091. * The function will fail if start is greater than limit, or if either index
  1092. * is less than zero or greater than the length of the string being matched.
  1093. *
  1094. * @param start The (native) index to begin searches at.
  1095. * @param limit The index to end searches at (exclusive).
  1096. * @param status A reference to a UErrorCode to receive any errors.
  1097. * @stable ICU 4.0
  1098. */
  1099. virtual RegexMatcher &region(int64_t start, int64_t limit, UErrorCode &status);
  1100. /**
  1101. * Identical to region(start, limit, status) but also allows a start position without
  1102. * resetting the region state.
  1103. * @param regionStart The region start
  1104. * @param regionLimit the limit of the region
  1105. * @param startIndex The (native) index within the region bounds at which to begin searches.
  1106. * @param status A reference to a UErrorCode to receive any errors.
  1107. * If startIndex is not within the specified region bounds,
  1108. * U_INDEX_OUTOFBOUNDS_ERROR is returned.
  1109. * @stable ICU 4.6
  1110. */
  1111. virtual RegexMatcher &region(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status);
  1112. /**
  1113. * Reports the start index of this matcher's region. The searches this matcher
  1114. * conducts are limited to finding matches within regionStart (inclusive) and
  1115. * regionEnd (exclusive).
  1116. *
  1117. * @return The starting (native) index of this matcher's region.
  1118. * @stable ICU 4.0
  1119. */
  1120. virtual int32_t regionStart() const;
  1121. /**
  1122. * Reports the start index of this matcher's region. The searches this matcher
  1123. * conducts are limited to finding matches within regionStart (inclusive) and
  1124. * regionEnd (exclusive).
  1125. *
  1126. * @return The starting (native) index of this matcher's region.
  1127. * @stable ICU 4.6
  1128. */
  1129. virtual int64_t regionStart64() const;
  1130. /**
  1131. * Reports the end (limit) index (exclusive) of this matcher's region. The searches
  1132. * this matcher conducts are limited to finding matches within regionStart
  1133. * (inclusive) and regionEnd (exclusive).
  1134. *
  1135. * @return The ending point (native) of this matcher's region.
  1136. * @stable ICU 4.0
  1137. */
  1138. virtual int32_t regionEnd() const;
  1139. /**
  1140. * Reports the end (limit) index (exclusive) of this matcher's region. The searches
  1141. * this matcher conducts are limited to finding matches within regionStart
  1142. * (inclusive) and regionEnd (exclusive).
  1143. *
  1144. * @return The ending point (native) of this matcher's region.
  1145. * @stable ICU 4.6
  1146. */
  1147. virtual int64_t regionEnd64() const;
  1148. /**
  1149. * Queries the transparency of region bounds for this matcher.
  1150. * See useTransparentBounds for a description of transparent and opaque bounds.
  1151. * By default, a matcher uses opaque region boundaries.
  1152. *
  1153. * @return TRUE if this matcher is using opaque bounds, false if it is not.
  1154. * @stable ICU 4.0
  1155. */
  1156. virtual UBool hasTransparentBounds() const;
  1157. /**
  1158. * Sets the transparency of region bounds for this matcher.
  1159. * Invoking this function with an argument of true will set this matcher to use transparent bounds.
  1160. * If the boolean argument is false, then opaque bounds will be used.
  1161. *
  1162. * Using transparent bounds, the boundaries of this matcher's region are transparent
  1163. * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
  1164. * see text beyond the boundaries of the region while checking for a match.
  1165. *
  1166. * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
  1167. * lookbehind, and boundary matching constructs.
  1168. *
  1169. * By default, a matcher uses opaque bounds.
  1170. *
  1171. * @param b TRUE for transparent bounds; FALSE for opaque bounds
  1172. * @return This Matcher;
  1173. * @stable ICU 4.0
  1174. **/
  1175. virtual RegexMatcher &useTransparentBounds(UBool b);
  1176. /**
  1177. * Return true if this matcher is using anchoring bounds.
  1178. * By default, matchers use anchoring region bounds.
  1179. *
  1180. * @return TRUE if this matcher is using anchoring bounds.
  1181. * @stable ICU 4.0
  1182. */
  1183. virtual UBool hasAnchoringBounds() const;
  1184. /**
  1185. * Set whether this matcher is using Anchoring Bounds for its region.
  1186. * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
  1187. * and end of the region. Without Anchoring Bounds, anchors will only match at
  1188. * the positions they would in the complete text.
  1189. *
  1190. * Anchoring Bounds are the default for regions.
  1191. *
  1192. * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
  1193. * @return This Matcher
  1194. * @stable ICU 4.0
  1195. */
  1196. virtual RegexMatcher &useAnchoringBounds(UBool b);
  1197. /**
  1198. * Return TRUE if the most recent matching operation attempted to access
  1199. * additional input beyond the available input text.
  1200. * In this case, additional input text could change the results of the match.
  1201. *
  1202. * hitEnd() is defined for both successful and unsuccessful matches.
  1203. * In either case hitEnd() will return TRUE if if the end of the text was
  1204. * reached at any point during the matching process.
  1205. *
  1206. * @return TRUE if the most recent match hit the end of input
  1207. * @stable ICU 4.0
  1208. */
  1209. virtual UBool hitEnd() const;
  1210. /**
  1211. * Return TRUE the most recent match succeeded and additional input could cause
  1212. * it to fail. If this method returns false and a match was found, then more input
  1213. * might change the match but the match won't be lost. If a match was not found,
  1214. * then requireEnd has no meaning.
  1215. *
  1216. * @return TRUE if more input could cause the most recent match to no longer match.
  1217. * @stable ICU 4.0
  1218. */
  1219. virtual UBool requireEnd() const;
  1220. /**
  1221. * Returns the pattern that is interpreted by this matcher.
  1222. * @return the RegexPattern for this RegexMatcher
  1223. * @stable ICU 2.4
  1224. */
  1225. virtual const RegexPattern &pattern() const;
  1226. /**
  1227. * Replaces every substring of the input that matches the pattern
  1228. * with the given replacement string. This is a convenience function that
  1229. * provides a complete find-and-replace-all operation.
  1230. *
  1231. * This method first resets this matcher. It then scans the input string
  1232. * looking for matches of the pattern. Input that is not part of any
  1233. * match is left unchanged; each match is replaced in the result by the
  1234. * replacement string. The replacement string may contain references to
  1235. * capture groups.
  1236. *
  1237. * @param replacement a string containing the replacement text.
  1238. * @param status a reference to a UErrorCode to receive any errors.
  1239. * @return a string containing the results of the find and replace.
  1240. * @stable ICU 2.4
  1241. */
  1242. virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status);
  1243. /**
  1244. * Replaces every substring of the input that matches the pattern
  1245. * with the given replacement string. This is a convenience function that
  1246. * provides a complete find-and-replace-all operation.
  1247. *
  1248. * This method first resets this matcher. It then scans the input string
  1249. * looking for matches of the pattern. Input that is not part of any
  1250. * match is left unchanged; each match is replaced in the result by the
  1251. * replacement string. The replacement string may contain references to
  1252. * capture groups.
  1253. *
  1254. * @param replacement a string containing the replacement text.
  1255. * @param dest a mutable UText in which the results are placed.
  1256. * If NULL, a new UText will be created (which may not be mutable).
  1257. * @param status a reference to a UErrorCode to receive any errors.
  1258. * @return a string containing the results of the find and replace.
  1259. * If a pre-allocated UText was provided, it will always be used and returned.
  1260. *
  1261. * @stable ICU 4.6
  1262. */
  1263. virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status);
  1264. /**
  1265. * Replaces the first substring of the input that matches
  1266. * the pattern with the replacement string. This is a convenience
  1267. * function that provides a complete find-and-replace operation.
  1268. *
  1269. * <p>This function first resets this RegexMatcher. It then scans the input string
  1270. * looking for a match of the pattern. Input that is not part
  1271. * of the match is appended directly to the result string; the match is replaced
  1272. * in the result by the replacement string. The replacement string may contain
  1273. * references to captured groups.</p>
  1274. *
  1275. * <p>The state of the matcher (the position at which a subsequent find()
  1276. * would begin) after completing a replaceFirst() is not specified. The
  1277. * RegexMatcher should be reset before doing additional find() operations.</p>
  1278. *
  1279. * @param replacement a string containing the replacement text.
  1280. * @param status a reference to a UErrorCode to receive any errors.
  1281. * @return a string containing the results of the find and replace.
  1282. * @stable ICU 2.4
  1283. */
  1284. virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status);
  1285. /**
  1286. * Replaces the first substring of the input that matches
  1287. * the pattern with the replacement string. This is a convenience
  1288. * function that provides a complete find-and-replace operation.
  1289. *
  1290. * <p>This function first resets this RegexMatcher. It then scans the input string
  1291. * looking for a match of the pattern. Input that is not part
  1292. * of the match is appended directly to the result string; the match is replaced
  1293. * in the result by the replacement string. The replacement string may contain
  1294. * references to captured groups.</p>
  1295. *
  1296. * <p>The state of the matcher (the position at which a subsequent find()
  1297. * would begin) after completing a replaceFirst() is not specified. The
  1298. * RegexMatcher should be reset before doing additional find() operations.</p>
  1299. *
  1300. * @param replacement a string containing the replacement text.
  1301. * @param dest a mutable UText in which the results are placed.
  1302. * If NULL, a new UText will be created (which may not be mutable).
  1303. * @param status a reference to a UErrorCode to receive any errors.
  1304. * @return a string containing the results of the find and replace.
  1305. * If a pre-allocated UText was provided, it will always be used and returned.
  1306. *
  1307. * @stable ICU 4.6
  1308. */
  1309. virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status);
  1310. /**
  1311. * Implements a replace operation intended to be used as part of an
  1312. * incremental find-and-replace.
  1313. *
  1314. * <p>The input string, starting from the end of the previous replacement and ending at
  1315. * the start of the current match, is appended to the destination string. Then the
  1316. * replacement string is appended to the output string,
  1317. * including handling any substitutions of captured text.</p>
  1318. *
  1319. * <p>For simple, prepackaged, non-incremental find-and-replace
  1320. * operations, see replaceFirst() or replaceAll().</p>
  1321. *
  1322. * @param dest A UnicodeString to which the results of the find-and-replace are appended.
  1323. * @param replacement A UnicodeString that provides the text to be substituted for
  1324. * the input text that matched the regexp pattern. The replacement
  1325. * text may contain references to captured text from the
  1326. * input.
  1327. * @param status A reference to a UErrorCode to receive any errors. Possible
  1328. * errors are U_REGEX_INVALID_STATE if no match has been
  1329. * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
  1330. * if the replacement text specifies a capture group that
  1331. * does not exist in the pattern.
  1332. *
  1333. * @return this RegexMatcher
  1334. * @stable ICU 2.4
  1335. *
  1336. */
  1337. virtual RegexMatcher &appendReplacement(UnicodeString &dest,
  1338. const UnicodeString &replacement, UErrorCode &status);
  1339. /**
  1340. * Implements a replace operation intended to be used as part of an
  1341. * incremental find-and-replace.
  1342. *
  1343. * <p>The input string, starting from the end of the previous replacement and ending at
  1344. * the start of the current match, is appended to the destination string. Then the
  1345. * replacement string is appended to the output string,
  1346. * including handling any substitutions of captured text.</p>
  1347. *
  1348. * <p>For simple, prepackaged, non-incremental find-and-replace
  1349. * operations, see replaceFirst() or replaceAll().</p>
  1350. *
  1351. * @param dest A mutable UText to which the results of the find-and-replace are appended.
  1352. * Must not be NULL.
  1353. * @param replacement A UText that provides the text to be substituted for
  1354. * the input text that matched the regexp pattern. The replacement
  1355. * text may contain references to captured text from the input.
  1356. * @param status A reference to a UErrorCode to receive any errors. Possible
  1357. * errors are U_REGEX_INVALID_STATE if no match has been
  1358. * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
  1359. * if the replacement text specifies a capture group that
  1360. * does not exist in the pattern.
  1361. *
  1362. * @return this RegexMatcher
  1363. *
  1364. * @stable ICU 4.6
  1365. */
  1366. virtual RegexMatcher &appendReplacement(UText *dest,
  1367. UText *replacement, UErrorCode &status);
  1368. /**
  1369. * As the final step in a find-and-replace operation, append the remainder
  1370. * of the input string, starting at the position following the last appendReplacement(),
  1371. * to the destination string. <code>appendTail()</code> is intended to be invoked after one
  1372. * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
  1373. *
  1374. * @param dest A UnicodeString to which the results of the find-and-replace are appended.
  1375. * @return the destination string.
  1376. * @stable ICU 2.4
  1377. */
  1378. virtual UnicodeString &appendTail(UnicodeString &dest);
  1379. /**
  1380. * As the final step in a find-and-replace operation, append the remainder
  1381. * of the input string, starting at the position following the last appendReplacement(),
  1382. * to the destination string. <code>appendTail()</code> is intended to be invoked after one
  1383. * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
  1384. *
  1385. * @param dest A mutable UText to which the results of the find-and-replace are appended.
  1386. * Must not be NULL.
  1387. * @param status error cod
  1388. * @return the destination string.
  1389. *
  1390. * @stable ICU 4.6
  1391. */
  1392. virtual UText *appendTail(UText *dest, UErrorCode &status);
  1393. /**
  1394. * Split a string into fields. Somewhat like split() from Perl.
  1395. * The pattern matches identify delimiters that separate the input
  1396. * into fields. The input data between the matches becomes the
  1397. * fields themselves.
  1398. *
  1399. * @param input The string to be split into fields. The field delimiters
  1400. * match the pattern (in the "this" object). This matcher
  1401. * will be reset to this input string.
  1402. * @param dest An array of UnicodeStrings to receive the results of the split.
  1403. * This is an array of actual UnicodeString objects, not an
  1404. * array of pointers to strings. Local (stack based) arrays can
  1405. * work well here.
  1406. * @param destCapacity The number of elements in the destination array.
  1407. * If the number of fields found is less than destCapacity, the
  1408. * extra strings in the destination array are not altered.
  1409. * If the number of destination strings is less than the number
  1410. * of fields, the trailing part of the input string, including any
  1411. * field delimiters, is placed in the last destination string.
  1412. * @param status A reference to a UErrorCode to receive any errors.
  1413. * @return The number of fields into which the input string was split.
  1414. * @stable ICU 2.6
  1415. */
  1416. virtual int32_t split(const UnicodeString &input,
  1417. UnicodeString dest[],
  1418. int32_t destCapacity,
  1419. UErrorCode &status);
  1420. /**
  1421. * Split a string into fields. Somewhat like split() from Perl.
  1422. * The pattern matches identify delimiters that separate the input
  1423. * into fields. The input data between the matches becomes the
  1424. * fields themselves.
  1425. *
  1426. * @param input The string to be split into fields. The field delimiters
  1427. * match the pattern (in the "this" object). This matcher
  1428. * will be reset to this input string.
  1429. * @param dest An array of mutable UText structs to receive the results of the split.
  1430. * If a field is NULL, a new UText is allocated to contain the results for
  1431. * that field. This new UText is not guaranteed to be mutable.
  1432. * @param destCapacity The number of elements in the destination array.
  1433. * If the number of fields found is less than destCapacity, the
  1434. * extra strings in the destination array are not altered.
  1435. * If the number of destination strings is less than the number
  1436. * of fields, the trailing part of the input string, including any
  1437. * field delimiters, is placed in the last destination string.
  1438. * @param status A reference to a UErrorCode to receive any errors.
  1439. * @return The number of fields into which the input string was split.
  1440. *
  1441. * @stable ICU 4.6
  1442. */
  1443. virtual int32_t split(UText *input,
  1444. UText *dest[],
  1445. int32_t destCapacity,
  1446. UErrorCode &status);
  1447. /**
  1448. * Set a processing time limit for match operations with this Matcher.
  1449. *
  1450. * Some patterns, when matching certain strings, can run in exponential time.
  1451. * For practical purposes, the match operation may appear to be in an
  1452. * infinite loop.
  1453. * When a limit is set a match operation will fail with an error if the
  1454. * limit is exceeded.
  1455. * <p>
  1456. * The units of the limit are steps of the match engine.
  1457. * Correspondence with actual processor time will depend on the speed
  1458. * of the processor and the details of the specific pattern, but will
  1459. * typically be on the order of milliseconds.
  1460. * <p>
  1461. * By default, the matching time is not limited.
  1462. * <p>
  1463. *
  1464. * @param limit The limit value, or 0 for no limit.
  1465. * @param status A reference to a UErrorCode to receive any errors.
  1466. * @stable ICU 4.0
  1467. */
  1468. virtual void setTimeLimit(int32_t limit, UErrorCode &status);
  1469. /**
  1470. * Get the time limit, if any, for match operations made with this Matcher.
  1471. *
  1472. * @return the maximum allowed time for a match, in units of processing steps.
  1473. * @stable ICU 4.0
  1474. */
  1475. virtual int32_t getTimeLimit() const;
  1476. /**
  1477. * Set the amount of heap storage available for use by the match backtracking stack.
  1478. * The matcher is also reset, discarding any results from previous matches.
  1479. * <p>
  1480. * ICU uses a backtracking regular expression engine, with the backtrack stack
  1481. * maintained on the heap. This function sets the limit to the amount of memory
  1482. * that can be used for this purpose. A backtracking stack overflow will
  1483. * result in an error from the match operation that caused it.
  1484. * <p>
  1485. * A limit is desirable because a malicious or poorly designed pattern can use
  1486. * excessive memory, potentially crashing the process. A limit is enabled
  1487. * by default.
  1488. * <p>
  1489. * @param limit The maximum size, in bytes, of the matching backtrack stack.
  1490. * A value of zero means no limit.
  1491. * The limit must be greater or equal to zero.
  1492. *
  1493. * @param status A reference to a UErrorCode to receive any errors.
  1494. *
  1495. * @stable ICU 4.0
  1496. */
  1497. virtual void setStackLimit(int32_t limit, UErrorCode &status);
  1498. /**
  1499. * Get the size of the heap storage available for use by the back tracking stack.
  1500. *
  1501. * @return the maximum backtracking stack size, in bytes, or zero if the
  1502. * stack size is unlimited.
  1503. * @stable ICU 4.0
  1504. */
  1505. virtual int32_t getStackLimit() const;
  1506. /**
  1507. * Set a callback function for use with this Matcher.
  1508. * During matching operations the function will be called periodically,
  1509. * giving the application the opportunity to terminate a long-running
  1510. * match.
  1511. *
  1512. * @param callback A pointer to the user-supplied callback function.
  1513. * @param context User context pointer. The value supplied at the
  1514. * time the callback function is set will be saved
  1515. * and passed to the callback each time that it is called.
  1516. * @param status A reference to a UErrorCode to receive any errors.
  1517. * @stable ICU 4.0
  1518. */
  1519. virtual void setMatchCallback(URegexMatchCallback *callback,
  1520. const void *context,
  1521. UErrorCode &status);
  1522. /**
  1523. * Get the callback function for this URegularExpression.
  1524. *
  1525. * @param callback Out parameter, receives a pointer to the user-supplied
  1526. * callback function.
  1527. * @param context Out parameter, receives the user context pointer that
  1528. * was set when uregex_setMatchCallback() was called.
  1529. * @param status A reference to a UErrorCode to receive any errors.
  1530. * @stable ICU 4.0
  1531. */
  1532. virtual void getMatchCallback(URegexMatchCallback *&callback,
  1533. const void *&context,
  1534. UErrorCode &status);
  1535. /**
  1536. * Set a progress callback function for use with find operations on this Matcher.
  1537. * During find operations, the callback will be invoked after each return from a
  1538. * match attempt, giving the application the opportunity to terminate a long-running
  1539. * find operation.
  1540. *
  1541. * @param callback A pointer to the user-supplied callback function.
  1542. * @param context User context pointer. The value supplied at the
  1543. * time the callback function is set will be saved
  1544. * and passed to the callback each time that it is called.
  1545. * @param status A reference to a UErrorCode to receive any errors.
  1546. * @stable ICU 4.6
  1547. */
  1548. virtual void setFindProgressCallback(URegexFindProgressCallback *callback,
  1549. const void *context,
  1550. UErrorCode &status);
  1551. /**
  1552. * Get the find progress callback function for this URegularExpression.
  1553. *
  1554. * @param callback Out parameter, receives a pointer to the user-supplied
  1555. * callback function.
  1556. * @param context Out parameter, receives the user context pointer that
  1557. * was set when uregex_setFindProgressCallback() was called.
  1558. * @param status A reference to a UErrorCode to receive any errors.
  1559. * @stable ICU 4.6
  1560. */
  1561. virtual void getFindProgressCallback(URegexFindProgressCallback *&callback,
  1562. const void *&context,
  1563. UErrorCode &status);
  1564. #ifndef U_HIDE_INTERNAL_API
  1565. /**
  1566. * setTrace Debug function, enable/disable tracing of the matching engine.
  1567. * For internal ICU development use only. DO NO USE!!!!
  1568. * @internal
  1569. */
  1570. void setTrace(UBool state);
  1571. #endif /* U_HIDE_INTERNAL_API */
  1572. /**
  1573. * ICU "poor man's RTTI", returns a UClassID for this class.
  1574. *
  1575. * @stable ICU 2.2
  1576. */
  1577. static UClassID U_EXPORT2 getStaticClassID();
  1578. /**
  1579. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  1580. *
  1581. * @stable ICU 2.2
  1582. */
  1583. virtual UClassID getDynamicClassID() const;
  1584. private:
  1585. // Constructors and other object boilerplate are private.
  1586. // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
  1587. RegexMatcher(); // default constructor not implemented
  1588. RegexMatcher(const RegexPattern *pat);
  1589. RegexMatcher(const RegexMatcher &other);
  1590. RegexMatcher &operator =(const RegexMatcher &rhs);
  1591. void init(UErrorCode &status); // Common initialization
  1592. void init2(UText *t, UErrorCode &e); // Common initialization, part 2.
  1593. friend class RegexPattern;
  1594. friend class RegexCImpl;
  1595. public:
  1596. #ifndef U_HIDE_INTERNAL_API
  1597. /** @internal */
  1598. void resetPreserveRegion(); // Reset matcher state, but preserve any region.
  1599. #endif /* U_HIDE_INTERNAL_API */
  1600. private:
  1601. //
  1602. // MatchAt This is the internal interface to the match engine itself.
  1603. // Match status comes back in matcher member variables.
  1604. //
  1605. void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status);
  1606. inline void backTrack(int64_t &inputIdx, int32_t &patIdx);
  1607. UBool isWordBoundary(int64_t pos); // perform Perl-like \b test
  1608. UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test
  1609. REStackFrame *resetStack();
  1610. inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status);
  1611. void IncrementTime(UErrorCode &status);
  1612. // Call user find callback function, if set. Return TRUE if operation should be interrupted.
  1613. inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status);
  1614. int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const;
  1615. UBool findUsingChunk(UErrorCode &status);
  1616. void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status);
  1617. UBool isChunkWordBoundary(int32_t pos);
  1618. const RegexPattern *fPattern;
  1619. RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and
  1620. // should delete it when through.
  1621. const UnicodeString *fInput; // The string being matched. Only used for input()
  1622. UText *fInputText; // The text being matched. Is never NULL.
  1623. UText *fAltInputText; // A shallow copy of the text being matched.
  1624. // Only created if the pattern contains backreferences.
  1625. int64_t fInputLength; // Full length of the input text.
  1626. int32_t fFrameSize; // The size of a frame in the backtrack stack.
  1627. int64_t fRegionStart; // Start of the input region, default = 0.
  1628. int64_t fRegionLimit; // End of input region, default to input.length.
  1629. int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $).
  1630. int64_t fAnchorLimit; // See useAnchoringBounds
  1631. int64_t fLookStart; // Region bounds for look-ahead/behind and
  1632. int64_t fLookLimit; // and other boundary tests. See
  1633. // useTransparentBounds
  1634. int64_t fActiveStart; // Currently active bounds for matching.
  1635. int64_t fActiveLimit; // Usually is the same as region, but
  1636. // is changed to fLookStart/Limit when
  1637. // entering look around regions.
  1638. UBool fTransparentBounds; // True if using transparent bounds.
  1639. UBool fAnchoringBounds; // True if using anchoring bounds.
  1640. UBool fMatch; // True if the last attempted match was successful.
  1641. int64_t fMatchStart; // Position of the start of the most recent match
  1642. int64_t fMatchEnd; // First position after the end of the most recent match
  1643. // Zero if no previous match, even when a region
  1644. // is active.
  1645. int64_t fLastMatchEnd; // First position after the end of the previous match,
  1646. // or -1 if there was no previous match.
  1647. int64_t fAppendPosition; // First position after the end of the previous
  1648. // appendReplacement(). As described by the
  1649. // JavaDoc for Java Matcher, where it is called
  1650. // "append position"
  1651. UBool fHitEnd; // True if the last match touched the end of input.
  1652. UBool fRequireEnd; // True if the last match required end-of-input
  1653. // (matched $ or Z)
  1654. UVector64 *fStack;
  1655. REStackFrame *fFrame; // After finding a match, the last active stack frame,
  1656. // which will contain the capture group results.
  1657. // NOT valid while match engine is running.
  1658. int64_t *fData; // Data area for use by the compiled pattern.
  1659. int64_t fSmallData[8]; // Use this for data if it's enough.
  1660. int32_t fTimeLimit; // Max time (in arbitrary steps) to let the
  1661. // match engine run. Zero for unlimited.
  1662. int32_t fTime; // Match time, accumulates while matching.
  1663. int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves.
  1664. // Kept separately from fTime to keep as much
  1665. // code as possible out of the inline
  1666. // StateSave function.
  1667. int32_t fStackLimit; // Maximum memory size to use for the backtrack
  1668. // stack, in bytes. Zero for unlimited.
  1669. URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct.
  1670. // NULL if there is no callback.
  1671. const void *fCallbackContext; // User Context ptr for callback function.
  1672. URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct.
  1673. // NULL if there is no callback.
  1674. const void *fFindProgressCallbackContext; // User Context ptr for callback function.
  1675. UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
  1676. UBool fTraceDebug; // Set true for debug tracing of match engine.
  1677. UErrorCode fDeferredStatus; // Save error state that cannot be immediately
  1678. // reported, or that permanently disables this matcher.
  1679. RuleBasedBreakIterator *fWordBreakItr;
  1680. };
  1681. U_NAMESPACE_END
  1682. #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
  1683. #endif