oldregexpr.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. {
  2. This file is part of the Free Pascal packages library.
  3. Copyright (c) 2008 by Joost van der Sluis, member of the
  4. Free Pascal development team
  5. Compatibility unit for the old regexpr unit.
  6. Renaming to OldRegExpr after insertion of the newer
  7. RegExpr unit by Andrey V. Sorokin in 2011-08.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit OldRegExpr;
  15. {$mode objfpc}{$H+}
  16. interface
  17. uses
  18. Regex;
  19. type
  20. tregexprflag = (
  21. ref_singleline,
  22. {** This indicates that a start of line is either the
  23. start of the pattern or a linebreak. }
  24. ref_multiline,
  25. {** The match will be done in a case-insensitive way
  26. according to US-ASCII character set. }
  27. ref_caseinsensitive);
  28. tregexprflags = set of tregexprflag;
  29. TRegExprEngine = TRegexEngine;
  30. function GenerateRegExprEngine(regexpr : pchar;flags : tregexprflags;var RegExprEngine: TRegExprEngine): boolean;
  31. function GenerateRegExprEngine(regexpr : pchar;flags : tregexprflags): TREGExprEngine;
  32. procedure DestroyRegExprEngine(var regexpr : TRegExprEngine);
  33. function RegExprPos(RegExprEngine : TRegExprEngine;p : pchar;var index,len : integer) : boolean;
  34. function RegExprReplaceAll(RegExprEngine : TRegExprEngine;const src,newstr : ansistring;var dest : ansistring) : sizeint;
  35. function RegExprEscapeStr (const S : string) : string;
  36. implementation
  37. function GenerateRegExprEngine(regexpr: pchar; flags: tregexprflags;
  38. var RegExprEngine: TRegExprEngine): boolean;
  39. var ErrorPos : Integer;
  40. ErrorCode : TRegexError;
  41. begin
  42. RegExprEngine := TRegExprEngine.Create(regexpr);
  43. if ref_multiline in flags then RegExprEngine.MultiLine:=True;
  44. if ref_caseinsensitive in flags then RegExprEngine.IgnoreCase:=True;
  45. Result := RegExprEngine.Parse(ErrorPos,ErrorCode);
  46. end;
  47. function GenerateRegExprEngine(regexpr: pchar; flags: tregexprflags
  48. ): TREGExprEngine;
  49. var r: TRegExprEngine;
  50. begin
  51. GenerateRegExprEngine(regexpr,flags,r);
  52. GenerateRegExprEngine:=r;
  53. end;
  54. procedure DestroyRegExprEngine(var regexpr: TRegExprEngine);
  55. begin
  56. if regexpr <> nil then
  57. regexpr.Free;
  58. regexpr := nil;
  59. end;
  60. function RegExprPos(RegExprEngine: TRegExprEngine; p: pchar; var index,
  61. len: integer): boolean;
  62. begin
  63. Len := 1;
  64. Result := RegExprEngine.MatchString(p,index,len);
  65. Len := Len - index;
  66. Dec(Index);
  67. if not Result then
  68. begin
  69. index := -1;
  70. len := 0;
  71. end;
  72. end;
  73. function RegExprReplaceAll(RegExprEngine: TRegExprEngine; const src,
  74. newstr: ansistring; var dest: ansistring): sizeint;
  75. begin
  76. result := RegExprEngine.ReplaceAllString(src,newstr,Dest);
  77. end;
  78. function RegExprEscapeStr(const S: string): string;
  79. var
  80. i, len : integer;
  81. s1: string;
  82. begin
  83. RegExprEscapeStr:= '';
  84. s1:='';
  85. if (S = '') then
  86. exit;
  87. len := Length (S);
  88. for i := 1 to len do
  89. begin
  90. if (S [i] in ['(','|', '.', '*', '?', '^', '$', '-', '[', '{', '}', ']', ')', '\']) then
  91. begin
  92. s1 := s1 + '\';
  93. end;
  94. s1 := s1 + S[i];
  95. end;
  96. RegExprEscapeStr:=s1;
  97. end;
  98. end.