-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegExp_Core.pp
More file actions
344 lines (296 loc) · 10.1 KB
/
RegExp_Core.pp
File metadata and controls
344 lines (296 loc) · 10.1 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
unit RegExp_Core;
{$mode ObjFPC}{$H+}
{$IFOPT D+}
{$DEFINE DEBUG_LOG}
{$ENDIF}
interface
type
PLiteralChar = ^LiteralChar;
LiteralChar = WideChar;
PLiteralCharGroup = ^LiteralCharGroup;
LiteralCharGroup = packed Array of LiteralChar;
TCharsetMode = (cmLiteral, cmRange);
PCharset = ^TCharset;
TCharset = record
case Mode: TCharsetMode of
cmLiteral: ( Literal: LiteralChar );
cmRange: (
StartAt: LiteralChar;
EndAt: LiteralChar;
)
end;
PCharsetArray = ^TCharsetArray;
TCharsetArray = Array of TCharset;
PCharGroup = ^TCharGroup;
TCharGroup = record
MinQuantity: Word;
MaxQuantity: Word;
Negated: Boolean;
Charset: TCharsetArray;
end;
PCharGroupSequence = ^TCharGroupSequence;
TCharGroupSequence = packed Array of TCharGroup;
function ScanLiteral(const Text: PWideChar; const Cursor: LongWord; const AChar: LiteralChar): Boolean;
function ScanGroup(const Text: PWideChar; const Cursor: LongWord; const Group: PLiteralCharGroup): Boolean;
function ScanRange(const Text: PWideChar; const Cursor: LongWord; StartAt, EndAt: LiteralChar): Boolean;
function ScanCharset(const Text: PWideChar; const Cursor: LongWord; const CharSet: TCharset): Boolean;
function ScanCharGroup(const Text: PWideChar; const Cursor: LongWord; const CharGroup: TCharGroup; out Matches: LongWord): Boolean;
function ScanSequence(const Text: PWideChar; const Cursor: LongWord; const Sequence: TCharGroupSequence): Boolean;
function StringToSequence(const AString: PWideChar): TCharGroupSequence;
implementation
uses
SysUtils;
function BoolToStr(Value: Boolean): PChar;
begin
if Value then
Result := 'True'
else
Result := 'False';
end;
{*--------------------------------------------------------------------------*
Scan for a literal character at the current cursor position in text
@param Text text to scan
@param Cursor index position of character to scan
@param AChar single character to compare
@returns True if match
*---------------------------------------------------------------------------*}
function ScanLiteral(const Text: PWideChar; const Cursor: LongWord; const AChar: LiteralChar): Boolean;
begin
{$IFDEF DEBUG_LOG}
WriteLn(Format('ScanLiteral: #$%4.4x (%s)', [ Word(AChar), AChar ]));
{$ENDIF}
Result := Text[Cursor] = AChar;
end;
{*--------------------------------------------------------------------------*
Scan for one of a group of characters at the current cursor position
in text
@param Text text to scan
@param Cursor index position of character to scan
@param Group group of characters co compare
@returns True if any match
*---------------------------------------------------------------------------*}
function ScanGroup(const Text: PWideChar; const Cursor: LongWord; const Group: PLiteralCharGroup): Boolean;
var
I: Integer;
begin
{$IFDEF DEBUG_LOG}
WriteLn('ScanGroup: [');
for I := 0 to Length(LiteralCharGroup(Group)) do
begin
WriteLn(Format(' #$%4.4x (%s)', [
Word(LiteralCharGroup(Group)[I]),
LiteralCharGroup(Group)[I]
]));
end;
WriteLn(']');
{$ENDIF}
Result := False;
for I := 0 to Length(LiteralCharGroup(Group)) do
begin
if Text[Cursor] = LiteralCharGroup(Group)[I] then
begin
Result := True;
break;
end;
end;
end;
{*--------------------------------------------------------------------------*
Scan for a range of characters at the current cursor position in text
@param Text text to scan
@param Cursor index position of character to scan
@param StartAt starting character in range (inclusive)
@param EndAt ending character in range (inclusive)
@returns True if match
*---------------------------------------------------------------------------*}
function ScanRange(const Text: PWideChar; const Cursor: LongWord; StartAt, EndAt: LiteralChar): Boolean;
begin
{$IFDEF DEBUG_LOG}
WriteLn(Format('ScanRange: #$%4.4x (%s)- #$%4.4x (%s)', [
Word(StartAt),
StartAt,
Word(EndAt),
EndAt
]));
{$ENDIF}
Result := (Text[Cursor] >= StartAt) and (Text[Cursor] <= EndAt);
end;
{*--------------------------------------------------------------------------*
Perform a selective scan of characters at the current cursor position
in text based on the type of character set
@param Text text to scan
@param Cursor index position of character to scan
@param CharSet one of literal or range of characters
@returns True if match
*---------------------------------------------------------------------------*}
function ScanCharset(const Text: PWideChar; const Cursor: LongWord; const CharSet: TCharset): Boolean;
begin
case CharSet.Mode of
cmLiteral:
begin
{$IFDEF DEBUG_LOG}
WriteLn('ScanCharset (cmLiteral)');
{$ENDIF}
Result := ScanLiteral(Text, Cursor, CharSet.Literal);
end;
cmRange:
begin
{$IFDEF DEBUG_LOG}
WriteLn('ScanCharset (cmRange)');
{$ENDIF}
Result := ScanRange(Text, Cursor, CharSet.StartAt, CharSet.EndAt);
end;
end;
end;
{*--------------------------------------------------------------------------*
Scan character set group
@param Text text to scan
@param Cursor index position of character to scan
@param CharGroup one of literal or range of characters
@exports Matches number of sequential characters matched
@returns True if match
*---------------------------------------------------------------------------*}
function ScanCharGroup(const Text: PWideChar; const Cursor: LongWord; const CharGroup: TCharGroup; out Matches: LongWord): Boolean;
var
I: LongWord = 0;
J: Word;
Matched: Boolean;
begin
{$IFDEF DEBUG_LOG}
WriteLn(Format('ScanCharGroup: '#10' Min (%d)'#10' Max (%d)'#10' Negated(%s)'#10' %d item(s)', [
CharGroup.MinQuantity,
CharGroup.MaxQuantity,
BoolToStr(CharGroup.Negated),
Length(CharGroup.Charset)
]));
{$ENDIF}
Matches := 0;
while Text[Cursor + I] <> #0 do
begin
// § Not at the end of string
if (
(CharGroup.MaxQuantity > 0) and
(Matches >= CharGroup.MaxQuantity)
) then
//
break;
// § Find match on any charset
Matched := False;
for J := 0 to Length(CharGroup.Charset) - 1 do
begin
if ScanCharset(Text, Cursor + I, CharGroup.Charset[J]) then
begin
Matched := True;
break;
end;
end;
// Increment matches for either condition
// * Negated and not Matched
// * Not negated and matched
if CharGroup.Negated <> Matched then
Inc(Matches)
else
break;
// Increment cursor position
Inc(I);
end;
Result := Matches >= CharGroup.MinQuantity;
end;
{*--------------------------------------------------------------------------*
Scan a sequence of characters to compare starting at the current cursor
position in the text up to the cursor + n, where n is the length of
the character sequence
@param Text text to scan
@param Cursor starting position of characters to scan
@param Sequence sequence of characters to compare
@returns True if match
*---------------------------------------------------------------------------*}
function ScanSequence(const Text: PWideChar; const Cursor: LongWord; const Sequence: TCharGroupSequence): Boolean;
var
I: LongWord = 0;
J: Word = 0;
M: LongWord = 0;
C: WideChar;
P: LongWord;
begin
{$IFDEF DEBUG_LOG}
WriteLn(Format('ScanSequence: %d items', [ Length(Sequence) ]));
{$ENDIF}
while Text[Cursor + I] <> #0 do
begin
// § Not at the end of string
C := Text[Cursor + I];
if J < Length(Sequence) then
begin
// § Sequence not complete
if ScanCharGroup(Text, Cursor + I, Sequence[J], M) then
begin
// § Match found
if J = 0 then
P := I; // Pin the cusor posiion (for backtracking)
Inc(J); // Increment sequence counter
Inc(I, M); // Move cursor up by matching result count
end
else if J = 0 then
// § Sequence not yet started, move cursor up
Inc(I)
else
begin
// § Sequence could not be completed
{$IFDEF DEBUG_LOG}
WriteLn(Format('ScanSequence failed at index %d!', [ J ]));
{$ENDIF}
// Exit(False);
// Backtrack to pinned pos + 1
J := 0;
I := P + 1;
end;
end
else
// § Sequence completed
Exit(True);
end;
Result := J = Length(Sequence);
end;
{*--------------------------------------------------------------------------*
Convert a string to a character sequence
@param AString string to convert
@returns TCharsetSequence
*---------------------------------------------------------------------------*}
function StringToSequence(const AString: PWideChar): TCharGroupSequence;
var
I: Integer;
Groups: Integer = 0;
Charset: PCharset;
CharGroup: PCharGroup;
begin
{$IFDEF DEBUG_LOG}
WriteLn(Format('StringToSequence: (%s)', [ AString]));
{$ENDIF}
Result := nil;
for I := 0 to StrLen(AString) - 1 do
begin
if (I > 0) and (Result[Groups - 1].Charset[0].Literal = AString[I]) then
begin
// dedupe consecutive matching chars
Inc(Result[Groups - 1].MaxQuantity);
end
else
begin
SetLength(Result, Groups + 1);
New(CharGroup);
New(Charset);
Charset^.Mode := cmLiteral;
Charset^.Literal := AString[I];
CharGroup^.MinQuantity := 1;
CharGroup^.MaxQuantity := 1;
CharGroup^.Negated := False;
SetLength(CharGroup^.Charset, 1);
CharGroup^.Charset[0] := Charset^;
Result[Groups] := CharGroup^;
Dispose(Charset);
Dispose(CharGroup);
Inc(Groups);
end;
end;
end;
end.