-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.RegEx.pas
101 lines (82 loc) · 3.4 KB
/
Utils.RegEx.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{******************************************************************************}
{ }
{ Delphi cross platform socket library }
{ }
{ Copyright (c) 2017 WiNDDRiVER([email protected]) }
{ }
{ Homepage: https://github.com/winddriver/Delphi-Cross-Socket }
{ }
{******************************************************************************}
unit Utils.RegEx;
interface
uses
System.RegularExpressions;
type
TMatchEvaluatorProc = reference to function(const AMatch: TMatch): string;
IScopeEvaluator = interface
function GetMatchEvaluator: TMatchEvaluator;
property MatchEvaluator: TMatchEvaluator read GetMatchEvaluator;
end;
TScopeEvaluator = class(TInterfacedObject, IScopeEvaluator)
private
FMatchEvaluatorProc: TMatchEvaluatorProc;
function MatchEvaluator(const Match: TMatch): string;
function GetMatchEvaluator: TMatchEvaluator;
public
constructor Create(AMatchEvaluatorProc: TMatchEvaluatorProc);
end;
TRegExHelper = record helper for TRegEx
function Replace(const Input: string; Evaluator: TMatchEvaluatorProc): string; overload;
function Replace(const Input: string; Evaluator: TMatchEvaluatorProc; Count: Integer): string; overload;
class function Replace(const Input, Pattern: string; Evaluator: TMatchEvaluatorProc): string; overload; static;
class function Replace(const Input, Pattern: string; Evaluator: TMatchEvaluatorProc; Options: TRegExOptions): string; overload; static;
end;
implementation
{ TScopeEvaluator }
constructor TScopeEvaluator.Create(AMatchEvaluatorProc: TMatchEvaluatorProc);
begin
FMatchEvaluatorProc := AMatchEvaluatorProc;
end;
function TScopeEvaluator.GetMatchEvaluator: TMatchEvaluator;
begin
Result := Self.MatchEvaluator;
end;
function TScopeEvaluator.MatchEvaluator(const Match: TMatch): string;
begin
if Assigned(FMatchEvaluatorProc) then
Result := FMatchEvaluatorProc(Match);
end;
{ TRegExHelper }
function TRegExHelper.Replace(const Input: string;
Evaluator: TMatchEvaluatorProc; Count: Integer): string;
var
LScopeEvaluator: IScopeEvaluator;
begin
LScopeEvaluator := TScopeEvaluator.Create(Evaluator);
Result := Self.Replace(Input, LScopeEvaluator.MatchEvaluator, Count);
end;
function TRegExHelper.Replace(const Input: string;
Evaluator: TMatchEvaluatorProc): string;
var
LScopeEvaluator: IScopeEvaluator;
begin
LScopeEvaluator := TScopeEvaluator.Create(Evaluator);
Result := Self.Replace(Input, LScopeEvaluator.MatchEvaluator);
end;
class function TRegExHelper.Replace(const Input, Pattern: string;
Evaluator: TMatchEvaluatorProc; Options: TRegExOptions): string;
var
LScopeEvaluator: IScopeEvaluator;
begin
LScopeEvaluator := TScopeEvaluator.Create(Evaluator);
Result := Replace(Input, Pattern, LScopeEvaluator.MatchEvaluator, Options);
end;
class function TRegExHelper.Replace(const Input, Pattern: string;
Evaluator: TMatchEvaluatorProc): string;
var
LScopeEvaluator: IScopeEvaluator;
begin
LScopeEvaluator := TScopeEvaluator.Create(Evaluator);
Result := Replace(Input, Pattern, LScopeEvaluator.MatchEvaluator);
end;
end.