/vendor/pcre/pcregexp.pas

http://github.com/feyeleanor/RubyGoLightly · Pascal · 820 lines · 592 code · 71 blank · 157 comment · 24 complexity · 4043d7ec2eb2151cda15d815a1123cd6 MD5 · raw file

  1. {
  2. pcRegExp - Perl compatible regular expressions for Virtual Pascal
  3. (c) 2001 Peter S. Voronov aka Chem O'Dun <petervrn@yahoo.com>
  4. Based on PCRE library interface unit for Virtual Pascal.
  5. (c) 2001 Alexander Tokarev <dwalin@dwalin.ru>
  6. The current PCRE version is: 3.7
  7. This software must be distributed as Freeware.
  8. The PCRE library is written by: Philip Hazel <ph10@cam.ac.uk>
  9. Copyright (c) 1997-2004 University of Cambridge
  10. AngelsHolocaust 4-11-04 updated to use version v5.0
  11. (INFO: this is regex-directed, NFA)
  12. AH: 9-11-04 - pcre_free: removed var, pcre already gives the ptr, now
  13. everything works as it should (no more crashes)
  14. -> removed CheckRegExp because pcre handles errors perfectly
  15. 10-11-04 - added pcError (errorhandling), pcInit
  16. 13-11-04 - removed the ErrorPos = 0 check -> always print erroroffset
  17. 17-10-05 - support for \1-\9 backreferences in TpcRegExp.GetReplStr
  18. 17-02-06 - added RunTimeOptions: caller can set options while searching
  19. 19-02-06 - added SearchOfs(): let PCRE use the complete string and offset
  20. into the string itself
  21. 20-12-06 - support for version 7.0
  22. 27.08.08 - support for v7.7
  23. }
  24. {$H+} {$DEFINE PCRE_3_7} {$DEFINE PCRE_5_0} {$DEFINE PCRE_7_0} {$DEFINE PCRE_7_7}
  25. Unit pcregexp;
  26. Interface
  27. uses objects;
  28. Type
  29. PpcRegExp = ^TpcRegExp;
  30. // TpcRegExp = object
  31. TpcRegExp = object(TObject)
  32. MatchesCount: integer;
  33. RegExpC, RegExpExt : Pointer;
  34. Matches:Pointer;
  35. RegExp: shortstring;
  36. SourceLen: integer;
  37. PartialMatch : boolean;
  38. Error : boolean;
  39. ErrorMsg : Pchar;
  40. ErrorPos : integer;
  41. RunTimeOptions: Integer; // options which can be set by the caller
  42. constructor Init(const ARegExp : shortstring; AOptions : integer; ALocale : Pointer);
  43. function Search(AStr: Pchar; ALen : longint) : boolean; virtual;
  44. function SearchNext( AStr: Pchar; ALen : longint) : boolean; virtual;
  45. function SearchOfs ( AStr: Pchar; ALen, AOfs : longint) : boolean; virtual;
  46. function MatchSub(ANom: integer; var Pos, Len : longint) : boolean; virtual;
  47. function MatchFull(var Pos, Len : longint) : boolean; virtual;
  48. function GetSubStr(ANom: integer; AStr: Pchar) : string; virtual;
  49. function GetFullStr(AStr: Pchar) : string; virtual;
  50. function GetReplStr(AStr: Pchar; const ARepl: string) : string; virtual;
  51. function GetPreSubStr(AStr: Pchar) : string; virtual;
  52. function GetPostSubStr(AStr: Pchar) : string; virtual;
  53. function ErrorStr : string; virtual;
  54. destructor Done; virtual;
  55. end;
  56. function pcGrepMatch(WildCard, aStr: string; AOptions:integer; ALocale : Pointer): Boolean;
  57. function pcGrepSub(WildCard, aStr, aRepl: string; AOptions:integer; ALocale : Pointer): string;
  58. function pcFastGrepMatch(WildCard, aStr: string): Boolean;
  59. function pcFastGrepSub(WildCard, aStr, aRepl: string): string;
  60. {$IFDEF PCRE_5_0}
  61. function pcGetVersion : pchar;
  62. {$ENDIF}
  63. function pcError (var pRegExp : Pointer) : Boolean;
  64. function pcInit (const Pattern: Shortstring; CaseSens: Boolean) : Pointer;
  65. Const { Options }
  66. PCRE_CASELESS = $0001;
  67. PCRE_MULTILINE = $0002;
  68. PCRE_DOTALL = $0004;
  69. PCRE_EXTENDED = $0008;
  70. PCRE_ANCHORED = $0010;
  71. PCRE_DOLLAR_ENDONLY = $0020;
  72. PCRE_EXTRA = $0040;
  73. PCRE_NOTBOL = $0080;
  74. PCRE_NOTEOL = $0100;
  75. PCRE_UNGREEDY = $0200;
  76. PCRE_NOTEMPTY = $0400;
  77. {$IFDEF PCRE_5_0}
  78. PCRE_UTF8 = $0800;
  79. PCRE_NO_AUTO_CAPTURE = $1000;
  80. PCRE_NO_UTF8_CHECK = $2000;
  81. PCRE_AUTO_CALLOUT = $4000;
  82. PCRE_PARTIAL = $8000;
  83. {$ENDIF}
  84. {$IFDEF PCRE_7_0}
  85. PCRE_DFA_SHORTEST = $00010000;
  86. PCRE_DFA_RESTART = $00020000;
  87. PCRE_FIRSTLINE = $00040000;
  88. PCRE_DUPNAMES = $00080000;
  89. PCRE_NEWLINE_CR = $00100000;
  90. PCRE_NEWLINE_LF = $00200000;
  91. PCRE_NEWLINE_CRLF = $00300000;
  92. PCRE_NEWLINE_ANY = $00400000;
  93. PCRE_NEWLINE_ANYCRLF = $00500000;
  94. PCRE_NEWLINE_BITS = PCRE_NEWLINE_CR or PCRE_NEWLINE_LF or PCRE_NEWLINE_ANY;
  95. {$ENDIF}
  96. {$IFDEF PCRE_7_7}
  97. PCRE_BSR_ANYCRLF = $00800000;
  98. PCRE_BSR_UNICODE = $01000000;
  99. PCRE_JAVASCRIPT_COMPAT= $02000000;
  100. {$ENDIF}
  101. PCRE_COMPILE_ALLOWED_OPTIONS = PCRE_ANCHORED + PCRE_AUTO_CALLOUT + PCRE_CASELESS +
  102. PCRE_DOLLAR_ENDONLY + PCRE_DOTALL + PCRE_EXTENDED +
  103. PCRE_EXTRA + PCRE_MULTILINE + PCRE_NO_AUTO_CAPTURE +
  104. PCRE_UNGREEDY + PCRE_UTF8 + PCRE_NO_UTF8_CHECK
  105. {$IFDEF PCRE_7_0}
  106. + PCRE_DUPNAMES + PCRE_FIRSTLINE + PCRE_NEWLINE_BITS
  107. {$ENDIF}
  108. {$IFDEF PCRE_7_7}
  109. + PCRE_BSR_ANYCRLF + PCRE_BSR_UNICODE + PCRE_JAVASCRIPT_COMPAT
  110. {$ENDIF}
  111. ;
  112. PCRE_EXEC_ALLOWED_OPTIONS = PCRE_ANCHORED + PCRE_NOTBOL + PCRE_NOTEOL +
  113. PCRE_NOTEMPTY + PCRE_NO_UTF8_CHECK + PCRE_PARTIAL
  114. {$IFDEF PCRE_7_0}
  115. + PCRE_NEWLINE_BITS
  116. {$ENDIF}
  117. {$IFDEF PCRE_7_7}
  118. + PCRE_BSR_ANYCRLF + PCRE_BSR_UNICODE
  119. {$ENDIF}
  120. ;
  121. {$IFDEF PCRE_7_0}
  122. PCRE_DFA_EXEC_ALLOWED_OPTIONS = PCRE_ANCHORED + PCRE_NOTBOL + PCRE_NOTEOL +
  123. PCRE_NOTEMPTY + PCRE_NO_UTF8_CHECK + PCRE_PARTIAL +
  124. PCRE_DFA_SHORTEST + PCRE_DFA_RESTART +
  125. PCRE_NEWLINE_BITS
  126. {$IFDEF PCRE_7_7}
  127. + PCRE_BSR_ANYCRLF + PCRE_BSR_UNICODE
  128. {$ENDIF}
  129. ;
  130. {$ENDIF}
  131. { Exec-time and get/set-time error codes }
  132. PCRE_ERROR_NOMATCH = -1;
  133. PCRE_ERROR_NULL = -2;
  134. PCRE_ERROR_BADOPTION = -3;
  135. PCRE_ERROR_BADMAGIC = -4;
  136. PCRE_ERROR_UNKNOWN_MODE = -5;
  137. PCRE_ERROR_NOMEMORY = -6;
  138. PCRE_ERROR_NOSUBSTRING = -7;
  139. {$IFDEF PCRE_5_0}
  140. PCRE_ERROR_MATCHLIMIT = -8;
  141. PCRE_ERROR_CALLOUT = -9; { Never used by PCRE itself }
  142. PCRE_ERROR_BADUTF8 = -10;
  143. PCRE_ERROR_BADUTF8_OFFSET = -11;
  144. PCRE_ERROR_PARTIAL = -12;
  145. PCRE_ERROR_BADPARTIAL = -13;
  146. PCRE_ERROR_INTERNAL = -14;
  147. PCRE_ERROR_BADCOUNT = -15;
  148. {$ENDIF}
  149. {$IFDEF PCRE_7_0}
  150. PCRE_ERROR_DFA_UITEM = -16;
  151. PCRE_ERROR_DFA_UCOND = -17;
  152. PCRE_ERROR_DFA_UMLIMIT = -18;
  153. PCRE_ERROR_DFA_WSSIZE = -19;
  154. PCRE_ERROR_DFA_RECURSE = -20;
  155. PCRE_ERROR_RECURSIONLIMIT = -21;
  156. PCRE_ERROR_NULLWSLIMIT = -22;
  157. PCRE_ERROR_BADNEWLINE = -23;
  158. {$ENDIF}
  159. { Request types for pcre_fullinfo() }
  160. PCRE_INFO_OPTIONS = 0;
  161. PCRE_INFO_SIZE = 1;
  162. PCRE_INFO_CAPTURECOUNT = 2;
  163. PCRE_INFO_BACKREFMAX = 3;
  164. PCRE_INFO_FIRSTBYTE = 4;
  165. PCRE_INFO_FIRSTCHAR = 4; { For backwards compatibility }
  166. PCRE_INFO_FIRSTTABLE = 5;
  167. {$IFDEF PCRE_5_0}
  168. PCRE_INFO_LASTLITERAL = 6;
  169. PCRE_INFO_NAMEENTRYSIZE = 7;
  170. PCRE_INFO_NAMECOUNT = 8;
  171. PCRE_INFO_NAMETABLE = 9;
  172. PCRE_INFO_STUDYSIZE = 10;
  173. PCRE_INFO_DEFAULT_TABLES = 11;
  174. {$ENDIF PCRE_5_0}
  175. {$IFDEF PCRE_7_7}
  176. PCRE_INFO_OKPARTIAL = 12;
  177. PCRE_INFO_JCHANGED = 13;
  178. PCRE_INFO_HASCRORLF = 14;
  179. {$ENDIF}
  180. { Request types for pcre_config() }
  181. {$IFDEF PCRE_5_0}
  182. PCRE_CONFIG_UTF8 = 0;
  183. PCRE_CONFIG_NEWLINE = 1;
  184. PCRE_CONFIG_LINK_SIZE = 2;
  185. PCRE_CONFIG_POSIX_MALLOC_THRESHOLD = 3;
  186. PCRE_CONFIG_MATCH_LIMIT = 4;
  187. PCRE_CONFIG_STACKRECURSE = 5;
  188. PCRE_CONFIG_UNICODE_PROPERTIES = 6;
  189. {$ENDIF PCRE_5_0}
  190. {$IFDEF PCRE_7_0}
  191. PCRE_CONFIG_MATCH_LIMIT_RECURSION = 7;
  192. {$ENDIF}
  193. {$IFDEF PCRE_7_7}
  194. PCRE_CONFIG_BSR = 8;
  195. {$ENDIF}
  196. { Bit flags for the pcre_extra structure }
  197. {$IFDEF PCRE_5_0}
  198. PCRE_EXTRA_STUDY_DATA = $0001;
  199. PCRE_EXTRA_MATCH_LIMIT = $0002;
  200. PCRE_EXTRA_CALLOUT_DATA = $0004;
  201. PCRE_EXTRA_TABLES = $0008;
  202. {$ENDIF PCRE_5_0}
  203. {$IFDEF PCRE_7_0}
  204. PCRE_EXTRA_MATCH_LIMIT_RECURSION = $0010;
  205. {$ENDIF}
  206. Const
  207. // DefaultOptions : integer = 0;
  208. DefaultLocaleTable : pointer = nil;
  209. {$IFDEF PCRE_5_0}
  210. { The structure for passing additional data to pcre_exec(). This is defined in
  211. such as way as to be extensible. Always add new fields at the end, in order to
  212. remain compatible. }
  213. type ppcre_extra = ^tpcre_extra;
  214. tpcre_extra = record
  215. flags : longint; { Bits for which fields are set }
  216. study_data : pointer; { Opaque data from pcre_study() }
  217. match_limit : longint; { Maximum number of calls to match() }
  218. callout_data : pointer; { Data passed back in callouts }
  219. tables : pointer; { Pointer to character tables }
  220. match_limit_recursion: longint; { Max recursive calls to match() }
  221. end;
  222. type ppcre_callout_block = ^pcre_callout_block;
  223. pcre_callout_block = record
  224. version,
  225. (* ------------------------ Version 0 ------------------------------- *)
  226. callout_number : integer;
  227. offset_vector : pointer;
  228. subject : pchar;
  229. subject_length, start_match, current_position, capture_top,
  230. capture_last : integer;
  231. callout_data : pointer;
  232. (* ------------------- Added for Version 1 -------------------------- *)
  233. pattern_position, next_item_length : integer;
  234. end;
  235. {$ENDIF PCRE_5_0}
  236. {$OrgName+}
  237. {$IFDEF VIRTUALPASCAL} {&Cdecl+} {$ENDIF VIRTUALPASCAL}
  238. { local replacement of external pcre memory management functions }
  239. function pcre_malloc( size : integer ) : pointer;
  240. procedure pcre_free( {var} p : pointer );
  241. {$IFDEF PCRE_5_0}
  242. const pcre_stack_malloc: function ( size : integer ): pointer = pcre_malloc;
  243. pcre_stack_free: procedure ( {var} p : pointer ) = pcre_free;
  244. function pcre_callout(var p : ppcre_callout_block) : integer;
  245. {$ENDIF PCRE_5_0}
  246. {$IFDEF VIRTUALPASCAL} {&Cdecl-} {$ENDIF VIRTUALPASCAL}
  247. Implementation
  248. Uses strings, collect, messages, dnapp, commands, advance0, stringsx
  249. {$IFDEF VIRTUALPASCAL} ,vpsyslow {$ENDIF VIRTUALPASCAL};
  250. Const
  251. MAGIC_NUMBER = $50435245; { 'PCRE' }
  252. MAX_MATCHES = 90; { changed in 3.5 version; should be divisible by 3, was 64}
  253. Type
  254. PMatchArray = ^TMatchArray;
  255. TMatchArray = array[0..( MAX_MATCHES * 3 )] of integer;
  256. PRegExpCollection = ^TRegExpCollection;
  257. TRegExpCollection = object(TSortedCollection)
  258. MaxRegExp : integer;
  259. SearchRegExp : shortstring;
  260. CompareModeInsert : boolean;
  261. constructor Init(AMaxRegExp:integer);
  262. procedure FreeItem(P: Pointer); virtual;
  263. function Compare(P1, P2: Pointer): Integer; virtual;
  264. function Find(ARegExp:shortstring;var P: PpcRegExp):boolean; virtual;
  265. function CheckNew(ARegExp:shortstring):PpcRegExp;virtual;
  266. end;
  267. Var
  268. PRegExpCache : PRegExpCollection;
  269. {$IFDEF VIRTUALPASCAL} {&Cdecl+} {$ENDIF VIRTUALPASCAL}
  270. { imported original pcre functions }
  271. function pcre_compile( const pattern : PChar; options : integer;
  272. var errorptr : PChar; var erroroffset : integer;
  273. const tables : PChar ) : pointer {pcre}; external;
  274. {$IFDEF PCRE_7_0}
  275. function pcre_compile2( const pattern : PChar; options : integer;
  276. var errorcodeptr : Integer;
  277. var errorptr : PChar; var erroroffset : integer;
  278. const tables : PChar ) : pointer {pcre}; external;
  279. {$ENDIF}
  280. {$IFDEF PCRE_5_0}
  281. function pcre_config( what : integer; where : pointer) : integer; external;
  282. function pcre_copy_named_substring( const code : pointer {pcre};
  283. const subject : pchar;
  284. var ovector : integer;
  285. stringcount : integer;
  286. const stringname : pchar;
  287. var buffer : pchar;
  288. size : integer) : integer; external;
  289. function pcre_copy_substring( const subject : pchar; var ovector : integer;
  290. stringcount, stringnumber : integer;
  291. var buffer : pchar; size : integer )
  292. : integer; external;
  293. function pcre_exec( const argument_re : pointer {pcre};
  294. const extra_data : pointer {pcre_extra};
  295. {$ELSE}
  296. function pcre_exec( const external_re : pointer;
  297. const external_extra : pointer;
  298. {$ENDIF}
  299. const subject : PChar;
  300. length, start_offset, options : integer;
  301. offsets : pointer;
  302. offsetcount : integer ) : integer; external;
  303. {$IFDEF PCRE_7_0}
  304. function pcre_dfa_exec( const argument_re : pointer {pcre};
  305. const extra_data : pointer {pcre_extra};
  306. const subject : pchar;
  307. length, start_offset, options : integer;
  308. offsets : pointer;
  309. offsetcount : integer;
  310. workspace : pointer;
  311. wscount : integer ) : integer; external;
  312. {$ENDIF}
  313. {$IFDEF PCRE_5_0}
  314. procedure pcre_free_substring( const p : pchar ); external;
  315. procedure pcre_free_substring_list( var p : pchar ); external;
  316. function pcre_fullinfo( const argument_re : pointer {pcre};
  317. const extra_data : pointer {pcre_extra};
  318. what : integer;
  319. where : pointer ) : integer; external;
  320. function pcre_get_named_substring( const code : pointer {pcre};
  321. const subject : pchar;
  322. var ovector : integer;
  323. stringcount : integer;
  324. const stringname : pchar;
  325. var stringptr : pchar ) : integer; external;
  326. function pcre_get_stringnumber( const code : pointer {pcre};
  327. const stringname : pchar ) : integer; external;
  328. function pcre_get_stringtable_entries( const code : pointer {pcre};
  329. const stringname : pchar;
  330. var firstptr,
  331. lastptr : pchar ) : integer; external;
  332. function pcre_get_substring( const subject : pchar; var ovector : integer;
  333. stringcount, stringnumber : integer;
  334. var stringptr : pchar ) : integer; external;
  335. function pcre_get_substring_list( const subject : pchar; var ovector : integer;
  336. stringcount : integer;
  337. listptr : pointer {const char ***listptr}) : integer; external;
  338. function pcre_info( const argument_re : pointer {pcre};
  339. var optptr : integer;
  340. var first_byte : integer ) : integer; external;
  341. function pcre_maketables : pchar; external;
  342. {$ENDIF}
  343. {$IFDEF PCRE_7_0}
  344. function pcre_refcount( const argument_re : pointer {pcre};
  345. adjust : integer ) : pchar; external;
  346. {$ENDIF}
  347. function pcre_study( const external_re : pointer {pcre};
  348. options : integer;
  349. var errorptr : PChar ) : pointer {pcre_extra}; external;
  350. {$IFDEF PCRE_5_0}
  351. function pcre_version : pchar; external;
  352. {$ENDIF}
  353. function pcre_malloc( size : integer ) : pointer;
  354. begin
  355. GetMem( result, size );
  356. end;
  357. procedure pcre_free( {var} p : pointer );
  358. begin
  359. if (p <> nil) then
  360. FreeMem( p, 0 );
  361. {@p := nil;}
  362. end;
  363. {$IFDEF PCRE_5_0}
  364. (* Called from PCRE as a result of the (?C) item. We print out where we are in
  365. the match. Yield zero unless more callouts than the fail count, or the callout
  366. data is not zero. *)
  367. function pcre_callout;
  368. begin
  369. end;
  370. {$ENDIF}
  371. {$IFDEF VIRTUALPASCAL} {&Cdecl-} {$ENDIF VIRTUALPASCAL}
  372. // Always include the newest version of the library
  373. {$IFDEF PCRE_7_7}
  374. {$L pcre77.lib}
  375. {$ELSE}
  376. {$IFDEF PCRE_7_0}
  377. {$L pcre70.lib}
  378. {$ELSE}
  379. {$IFDEF PCRE_5_0}
  380. {$L pcre50.lib}
  381. {$ELSE}
  382. {$IFDEF PCRE_3_7}
  383. {$L pcre37.lib}
  384. {$ENDIF PCRE_3_7}
  385. {$ENDIF PCRE_5_0}
  386. {$ENDIF PCRE_7_0}
  387. {$ENDIF PCRE_7_7}
  388. {TpcRegExp}
  389. constructor TpcRegExp.Init(const ARegExp:shortstring; AOptions:integer; ALocale : Pointer);
  390. var
  391. pRegExp : PChar;
  392. begin
  393. RegExp:=ARegExp;
  394. RegExpC:=nil;
  395. RegExpExt:=nil;
  396. Matches:=nil;
  397. MatchesCount:=0;
  398. Error:=true;
  399. ErrorMsg:=nil;
  400. ErrorPos:=0;
  401. RunTimeOptions := 0;
  402. if length(RegExp) < 255 then
  403. begin
  404. RegExp[length(RegExp)+1]:=#0;
  405. pRegExp:=@RegExp[1];
  406. end
  407. else
  408. begin
  409. GetMem(pRegExp,length(RegExp)+1);
  410. pRegExp:=strpcopy(pRegExp,RegExp);
  411. end;
  412. RegExpC := pcre_compile( pRegExp,
  413. AOptions and PCRE_COMPILE_ALLOWED_OPTIONS,
  414. ErrorMsg, ErrorPos, ALocale);
  415. if length(RegExp) = 255 then
  416. StrDispose(pRegExp);
  417. if RegExpC = nil then
  418. exit;
  419. ErrorMsg:=nil;
  420. RegExpExt := pcre_study( RegExpC, 0, ErrorMsg );
  421. if (RegExpExt = nil) and (ErrorMsg <> nil) then
  422. begin
  423. pcre_free(RegExpC);
  424. exit;
  425. end;
  426. GetMem(Matches,SizeOf(TMatchArray));
  427. Error:=false;
  428. end;
  429. destructor TpcRegExp.Done;
  430. begin
  431. if RegExpC <> nil then
  432. pcre_free(RegExpC);
  433. if RegExpExt <> nil then
  434. pcre_free(RegExpExt);
  435. if Matches <> nil then
  436. FreeMem(Matches,SizeOf(TMatchArray));
  437. end;
  438. function TpcRegExp.SearchNext( AStr: Pchar; ALen : longint ) : boolean;
  439. var Options: Integer;
  440. begin // must handle PCRE_ERROR_PARTIAL here
  441. Options := (RunTimeOptions or startup.MiscMultiData.cfgRegEx.DefaultOptions) and
  442. PCRE_EXEC_ALLOWED_OPTIONS;
  443. if MatchesCount > 0 then
  444. MatchesCount:=pcre_exec( RegExpC, RegExpExt, AStr, ALen, PMatchArray(Matches)^[1],
  445. Options, Matches, MAX_MATCHES ) else
  446. MatchesCount:=pcre_exec( RegExpC, RegExpExt, AStr, ALen, 0,
  447. Options, Matches, MAX_MATCHES );
  448. { if MatchesCount = 0 then
  449. MatchesCount := MatchesCount div 3;}
  450. PartialMatch := MatchesCount = PCRE_ERROR_PARTIAL;
  451. SearchNext := MatchesCount > 0;
  452. end;
  453. function TpcRegExp.Search( AStr: Pchar; ALen : longint):boolean;
  454. begin
  455. MatchesCount:=0;
  456. Search:=SearchNext(AStr,ALen);
  457. SourceLen:=ALen;
  458. end;
  459. function TpcRegExp.SearchOfs( AStr: Pchar; ALen, AOfs: longint ) : boolean;
  460. var Options: Integer;
  461. begin
  462. MatchesCount:=0;
  463. Options := (RunTimeOptions or startup.MiscMultiData.cfgRegEx.DefaultOptions) and
  464. PCRE_EXEC_ALLOWED_OPTIONS;
  465. MatchesCount:=pcre_exec( RegExpC, RegExpExt, AStr, ALen, AOfs,
  466. Options, Matches, MAX_MATCHES );
  467. PartialMatch := MatchesCount = PCRE_ERROR_PARTIAL;
  468. SearchOfs := MatchesCount > 0;
  469. SourceLen := ALen-AOfs;
  470. end;
  471. function TpcRegExp.MatchSub(ANom:integer; var Pos,Len:longint):boolean;
  472. begin
  473. if (MatchesCount > 0) and (ANom <= (MatchesCount-1)) then
  474. begin
  475. ANom:=ANom*2;
  476. Pos:=PMatchArray(Matches)^[ANom];
  477. Len:=PMatchArray(Matches)^[ANom+1]-Pos;
  478. MatchSub:=true;
  479. end
  480. else
  481. MatchSub:=false;
  482. end;
  483. function TpcRegExp.MatchFull(var Pos,Len:longint):boolean;
  484. begin
  485. MatchFull:=MatchSub(0,Pos,Len);
  486. end;
  487. function TpcRegExp.GetSubStr(ANom: integer; AStr: Pchar):string;
  488. var
  489. s: ansistring;
  490. pos,len: longint;
  491. begin
  492. s:='';
  493. if MatchSub(ANom, pos, len) then
  494. begin
  495. setlength(s, len);
  496. Move(AStr[pos], s[1], len);
  497. end;
  498. GetSubStr:=s;
  499. end;
  500. function TpcRegExp.GetPreSubStr(AStr: Pchar):string;
  501. var
  502. s: ansistring;
  503. l: longint;
  504. begin
  505. s:='';
  506. if (MatchesCount > 0) then
  507. begin
  508. l:=PMatchArray(Matches)^[0]-1;
  509. if l > 0 then
  510. begin
  511. setlength(s,l);
  512. Move(AStr[1],s[1],l);
  513. end;
  514. end;
  515. GetPreSubStr:=s;
  516. end;
  517. function TpcRegExp.GetPostSubStr(AStr: Pchar):string;
  518. var
  519. s: ansistring;
  520. l: longint;
  521. ANom: integer;
  522. begin
  523. s:='';
  524. if (MatchesCount > 0) then
  525. begin
  526. ANom:=(MatchesCount-1){*2} shl 1;
  527. l:=SourceLen-PMatchArray(Matches)^[ANom+1]+1;
  528. if l > 0 then
  529. begin
  530. setlength(s,l);
  531. Move(AStr[PMatchArray(Matches)^[ANom+1]],s[1],l);
  532. end;
  533. end;
  534. GetPostSubStr:=s;
  535. end;
  536. function TpcRegExp.GetFullStr(AStr: Pchar):string;
  537. var
  538. s: ansistring;
  539. l: longint;
  540. begin
  541. GetFullStr:=GetSubStr(0,AStr);
  542. end;
  543. function TpcRegExp.GetReplStr(AStr: Pchar; const ARepl: string):string;
  544. var
  545. s: ansistring;
  546. l,i,lasti: longint;
  547. begin
  548. l:=length(ARepl);
  549. i:=1;
  550. lasti:=1;
  551. s:='';
  552. while i <= l do
  553. begin
  554. case ARepl[i] of
  555. '\' :
  556. begin
  557. if i < l then
  558. begin
  559. s:=s+copy(ARepl,lasti,i-lasti){+ARepl[i+1]};
  560. {AH 17-10-05 support for POSIX \1-\9 backreferences}
  561. case ARepl[i+1] of
  562. '0' : s:=s+GetFullStr(AStr);
  563. '1'..'9' : s:=s+GetSubStr(ord(ARepl[i+1])-ord('0'),AStr);
  564. else s:=s+ARepl[i+1]; // copy the escaped character
  565. end;
  566. end;
  567. inc(i);
  568. lasti:=i+1;
  569. end;
  570. '$' :
  571. begin
  572. if i < l then
  573. begin
  574. s:=s+copy(ARepl,lasti,i-lasti);
  575. case ARepl[i+1] of
  576. '&' : s:=s+GetFullStr(AStr);
  577. '1'..'9' : s:=s+GetSubStr(ord(ARepl[i+1])-ord('0'),AStr);
  578. '`' : s:=s+GetPreSubStr(AStr);
  579. #39 : s:=s+GetPostSubStr(AStr);
  580. end;
  581. end;
  582. inc(i);
  583. lasti:=i+1;
  584. end;
  585. end;
  586. inc(i);
  587. end;
  588. if lasti <= {AH 25-10-2004 added =, else l==1 won't work} l then
  589. s:=s+copy(ARepl,lasti,l-lasti+1);
  590. GetReplStr:=s;
  591. end;
  592. function TpcRegExp.ErrorStr:string;
  593. begin
  594. ErrorStr:=StrPas(ErrorMsg);
  595. end;
  596. {TRegExpCollection}
  597. constructor TRegExpCollection.Init(AMaxRegExp: integer);
  598. begin
  599. Inherited Init(1,1);
  600. MaxRegExp:=AMaxRegExp;
  601. CompareModeInsert:=true;
  602. end;
  603. procedure TRegExpCollection.FreeItem(P: Pointer);
  604. begin
  605. if P <> nil then
  606. begin
  607. Dispose(PpcRegExp(P),Done);
  608. end;
  609. end;
  610. function TRegExpCollection.Compare(P1, P2: Pointer): Integer;
  611. //var
  612. // l,l1,l2,i : byte;
  613. //// wPos: pchar;
  614. begin
  615. if CompareModeInsert then
  616. begin
  617. // l1:=length(PpcRegExp(P1)^.RegExp);
  618. // l2:=length(PpcRegExp(P2)^.RegExp);
  619. // if l1 > l2 then l:=l2 else
  620. // l:=l1;
  621. // for i:=1 to l do
  622. // if PpcRegExp(P1).RegExp[i] <> PpcRegExp(P2).RegExp[i] then break;
  623. // if i <=l then
  624. // Compare:=ord(PpcRegExp(P1).RegExp[i])-ord(PpcRegExp(P2).RegExp[i]) else
  625. // Compare:=l1-l2;
  626. Compare := stringsx.PasStrCmp(PpcRegExp(P1).RegExp, PpcRegExp(P2).RegExp, False);
  627. end
  628. else
  629. begin
  630. // l1:=length(PpcRegExp(P1)^.RegExp);
  631. // l2:=length(SearchRegExp);
  632. // if l1 > l2 then l:=l2 else
  633. // l:=l1;
  634. // for i:=1 to l do
  635. // if PpcRegExp(P1).RegExp[i] <> SearchRegExp[i] then
  636. // begin
  637. // Compare:=ord(PpcRegExp(P1).RegExp[i])-ord(SearchRegExp[i]);
  638. // break;
  639. // end;
  640. // if i > l then Compare:=l1-l2;
  641. Compare := stringsx.PasStrCmp(PpcRegExp(P1).RegExp, SearchRegExp, False);
  642. end;
  643. end;
  644. function TRegExpCollection.Find(ARegExp:shortstring;var P: PpcRegExp):boolean;
  645. var I : integer;
  646. begin
  647. CompareModeInsert:=false;
  648. SearchRegExp:=ARegExp;
  649. if Search(nil,I) then
  650. begin
  651. P:=PpcRegExp(At(I));
  652. Find:=true;
  653. end
  654. else
  655. begin
  656. P:=nil;
  657. Find:=false;
  658. end;
  659. CompareModeInsert:=true;
  660. end;
  661. function TRegExpCollection.CheckNew(ARegExp:shortstring):PpcRegExp;
  662. var
  663. P : PpcRegExp;
  664. begin
  665. if not Find(ARegExp,P) then
  666. begin
  667. if Count = MaxRegExp then
  668. AtFree(0);
  669. P:=New(ppcRegExp,Init(ARegExp,PCRE_CASELESS,nil));
  670. Insert(P);
  671. end;
  672. CheckNew:=P;
  673. end;
  674. function pcGrepMatch(WildCard, aStr: string; AOptions:integer; ALocale : Pointer): Boolean;
  675. var
  676. PpcRE:PpcRegExp;
  677. begin
  678. PpcRE:=New(ppcRegExp,Init(WildCard,AOptions,Alocale));
  679. pcGrepMatch:=PpcRE^.Search(pchar(AStr),Length(AStr));
  680. Dispose(PpcRE,Done);
  681. end;
  682. function pcGrepSub(WildCard, aStr, aRepl: string; AOptions:integer; ALocale : Pointer): string;
  683. var
  684. PpcRE:PpcRegExp;
  685. begin
  686. PpcRE:=New(ppcRegExp,Init(WildCard,AOptions,Alocale));
  687. if PpcRE^.Search(pchar(AStr),Length(AStr)) then
  688. pcGrepSub:=PpcRE^.GetReplStr(pchar(AStr),ARepl)
  689. else
  690. pcGrepSub:='';
  691. Dispose(PpcRE,Done);
  692. end;
  693. function pcFastGrepMatch(WildCard, aStr: string): Boolean;
  694. var
  695. PpcRE:PpcRegExp;
  696. begin
  697. PpcRE:=PRegExpCache^.CheckNew(WildCard);
  698. pcFastGrepMatch:=PpcRE^.Search(pchar(AStr),Length(AStr));
  699. end;
  700. function pcFastGrepSub(WildCard, aStr, aRepl: string): string;
  701. var
  702. PpcRE:PpcRegExp;
  703. begin
  704. PpcRE:=PRegExpCache^.CheckNew(WildCard);
  705. if PpcRE^.Search(pchar(AStr),Length(AStr)) then
  706. pcFastGrepSub:=PpcRE^.GetReplStr(pchar(AStr),ARepl)
  707. else
  708. pcFastGrepSub:='';
  709. end;
  710. {$IFDEF PCRE_5_0}
  711. function pcGetVersion : pchar; assembler; {$FRAME-}{$USES none}
  712. asm
  713. call pcre_version
  714. end;
  715. {$ENDIF PCRE_5_0}
  716. function pcError;
  717. var P: ppcRegExp absolute pRegExp;
  718. begin
  719. Result := (P = nil) or P^.Error;
  720. If Result and (P <> nil) then
  721. begin
  722. { if P^.ErrorPos = 0 then
  723. MessageBox(GetString(erRegExpCompile)+'"'+P^.ErrorStr+'"', nil,mfConfirmation+mfOkButton)
  724. else}
  725. MessageBox(GetString(erRegExpCompile)+'"'+P^.ErrorStr+'"'+GetString(erRegExpCompPos),
  726. @P^.ErrorPos,mfConfirmation+mfOkButton);
  727. Dispose(P, Done);
  728. P:=nil;
  729. end;
  730. end;
  731. function pcInit;
  732. var Options : Integer;
  733. begin
  734. If CaseSens then Options := 0 else Options := PCRE_CASELESS;
  735. Result := New( PpcRegExp, Init( Pattern,
  736. {DefaultOptions}
  737. startup.MiscMultiData.cfgRegEx.DefaultOptions or Options,
  738. DefaultLocaleTable) );
  739. end;
  740. Initialization
  741. PRegExpCache:=New(PRegExpCollection,Init(64));
  742. Finalization
  743. Dispose(PRegExpCache,Done);
  744. End.