-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextScan.pas
More file actions
358 lines (299 loc) · 10.4 KB
/
TextScan.pas
File metadata and controls
358 lines (299 loc) · 10.4 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
unit TextScan;
{
DESCRIPTION: Provides high-level access to solid text in string
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
}
(***) interface (***)
uses Math, Utils;
type
(* IMPORT *)
TCharSet = Utils.TCharSet;
TTextScanner = class
(***) protected (***)
fTextBuf: string;
fPos: integer; // Start from 1
fLineN: integer; // Start from 1
fEndOfLineMarker: char;
fLineStartPos: integer; // Position of EndOfLineMarker; LinePos = Pos - LineStartPos
fTextBufLen: integer;
fEndOfText: boolean;
(***) public (***)
property TextBuf: string read fTextBuf;
property Pos: integer read fPos; // Start from 1
property LineN: integer read fLineN; // Start from 1
property EndOfLineMarker: char read fEndOfLineMarker;
property LineStartPos: integer read fLineStartPos; // Position of EndOfLineMarker; LinePos = Pos - LineStartPos
property TextBufLen: integer read fTextBufLen;
property EndOfText: boolean read fEndOfText;
property Eot: boolean read fEndOfText;
constructor Create;
function IsValidPos (CheckPos: integer): boolean;
(* Returns #0 for EndOfText *)
function c: char;
function GetCurrChar (out Res: char): boolean;
function ReadChar (out Res: char): boolean;
function GetCharAtPos (TargetPos: integer; out Res: char): boolean;
function GetCharAtRelPos (RelPos: integer; out Res: char): boolean;
function CharAtPos (TargetPos: integer): char;
function CharAtPosRel (RelPos: integer): char;
function GetSubstrAtPos (TargetPos, SubstrLen: integer): string;
function GetSubstrAtRelPos (RelPos, SubstrLen: integer): string;
function GotoNextChar: boolean;
function GotoPrevChar: boolean;
function GotoPos (TargetPos: integer): boolean;
function GotoRelPos (RelPos: integer): boolean;
function GotoNextLine: boolean;
function GotoPrevLine: boolean;
function GotoLine (TargetLine: integer): boolean;
function GotoRelLine (RelLineN: integer): boolean;
function PosToLine (TargetPos: integer; out LineN, LinePos: integer): boolean;
function SkipChars (Ch: char): boolean;
function SkipCharset (const Charset: TCharSet): boolean;
function FindChar (Ch: char): boolean;
function FindCharset (const Charset: TCharSet): boolean;
function ReadToken (const TokenCharset: TCharSet; out Token: string): boolean;
function ReadTokenTillDelim (const DelimCharset: TCharSet; out Token: string): boolean;
procedure Connect (const TextBuf: string; EndOfLineMarker: char);
property Chars[CharPos: integer]: char read CharAtPos; default;
property CharsRel[RelCharPos: integer]: char read CharAtPosRel;
end; // .class TTextScanner
(***) implementation (***)
constructor TTextScanner.Create;
const
EMPTY_TEXT = '';
END_OF_LINE_MARKER = #10;
begin
Self.Connect(EMPTY_TEXT, END_OF_LINE_MARKER);
end; // .constructor TTextScanner.Create
function TTextScanner.IsValidPos (CheckPos: integer): boolean;
begin
result := Math.InRange(CheckPos, 1, Self.TextBufLen + 1);
end;
function TTextScanner.c: char;
begin
if Self.EndOfText then begin
result := #0;
end else begin
result := Self.TextBuf[Self.Pos];
end;
end;
function TTextScanner.GetCurrChar (out Res: char): boolean;
begin
result := not Self.EndOfText;
if result then begin
Res := Self.TextBuf[Self.Pos];
end;
end;
function TTextScanner.ReadChar (out Res: char): boolean;
begin
result := Self.GetCurrChar(Res);
Self.GotoNextChar;
end;
function TTextScanner.GetCharAtPos (TargetPos: integer; out Res: char): boolean;
begin
result := Self.IsValidPos(TargetPos) and (TargetPos <= Self.TextBufLen);
if result then begin
Res := Self.TextBuf[TargetPos];
end;
end;
function TTextScanner.GetCharAtRelPos (RelPos: integer; out Res: char): boolean;
begin
result := Self.GetCharAtPos(Self.Pos + RelPos, Res);
end;
function TTextScanner.CharAtPos (TargetPos: integer): char;
begin
if not GetCharAtPos(TargetPos, result) then begin
result := #0;
end;
end;
function TTextScanner.CharAtPosRel (RelPos: integer): char;
begin
result := CharAtPos(Self.Pos + RelPos);
end;
function TTextScanner.GetSubstrAtPos (TargetPos, SubstrLen: integer): string;
var
StartPos: integer;
EndPos: integer;
begin
{!} Assert(SubstrLen >= 0);
StartPos := TargetPos;
if StartPos < 1 then begin
StartPos := 1;
SubstrLen := Math.Max(0, SubstrLen + TargetPos - 1);
end else if StartPos > Self.TextBufLen + 1 then begin
StartPos := Self.TextBufLen + 1;
end;
EndPos := Math.EnsureRange(TargetPos + SubstrLen, 1, Self.TextBufLen + 1);
result := Copy(Self.TextBuf, StartPos, EndPos - StartPos);
end; // .function TTextScanner.GetSubstrAtPos
function TTextScanner.GetSubstrAtRelPos (RelPos, SubstrLen: integer): string;
begin
result := Self.GetSubstrAtPos(Self.Pos + RelPos, SubstrLen);
end;
function TTextScanner.GotoNextChar: boolean;
begin
result := not Self.EndOfText;
if result then begin
if Self.TextBuf[Self.Pos] = Self.EndOfLineMarker then begin
Self.fLineStartPos := Self.Pos;
Inc(Self.fLineN);
end;
Inc(Self.fPos);
if Self.Pos > Self.TextBufLen then begin
Self.fEndOfText := TRUE;
result := FALSE;
end;
end; // .if
end; // .function TTextScanner.GotoNextChar
function TTextScanner.GotoPrevChar: boolean;
var
i: integer;
begin
result := Self.Pos > 1;
if result then begin
Dec(Self.fPos);
if Self.TextBuf[Self.Pos] = Self.EndOfLineMarker then begin
Dec(Self.fLineN);
i := Self.Pos - 1;
while (i >= 1) and (Self.TextBuf[i] <> Self.EndOfLineMarker) do begin
Dec(i);
end;
Self.fLineStartPos := i;
end;
Self.fEndOfText := FALSE;
end; // .if
end; // .function TTextScanner.GotoPrevChar
function TTextScanner.GotoPos (TargetPos: integer): boolean;
var
NumSteps: integer;
i: integer;
begin
result := Self.IsValidPos(TargetPos);
if result then begin
NumSteps := ABS(TargetPos - Self.Pos);
if TargetPos >= Self.Pos then begin
for i:=1 to NumSteps do begin
Self.GotoNextChar;
end;
end else begin
for i:=1 to NumSteps do begin
Self.GotoPrevChar;
end;
end;
end; // .if
end; // .function TTextScanner.GotoPos
function TTextScanner.GotoRelPos (RelPos: integer): boolean;
begin
result := Self.GotoPos(Self.Pos + RelPos);
end;
function TTextScanner.GotoNextLine: boolean;
var
OrigLineN: integer;
begin
OrigLineN := Self.LineN;
while (Self.LineN = OrigLineN) and Self.GotoNextChar do begin end;
result := Self.LineN > OrigLineN;
end;
function TTextScanner.GotoPrevLine: boolean;
var
OrigLineN: integer;
begin
OrigLineN := Self.LineN;
while (Self.LineN = OrigLineN) and Self.GotoPrevChar do begin end;
result := Self.LineN < OrigLineN;
if result then begin
Self.GotoPos(Self.LineStartPos + 1);
end;
end; // .function TTextScanner.GotoPrevLine
function TTextScanner.GotoLine (TargetLine: integer): boolean;
begin
if TargetLine > Self.LineN then begin
while (Self.LineN <> TargetLine) and Self.GotoNextLine do begin end;
end else begin
while (Self.LineN <> TargetLine) and Self.GotoPrevLine do begin end;
end;
result := Self.LineN = TargetLine;
end;
function TTextScanner.GotoRelLine (RelLineN: integer): boolean;
begin
result := Self.GotoLine(Self.LineN + RelLineN);
end;
function TTextScanner.PosToLine (TargetPos: integer; out LineN, LinePos: integer): boolean;
var
CurrPos: integer;
begin
CurrPos := Self.Pos;
result := Self.GotoPos(TargetPos);
if result then begin
LineN := Self.LineN;
LinePos := Self.Pos - Self.LineStartPos;
Self.GotoPos(CurrPos);
end;
end; // .function TTextScanner.PosToLine
function TTextScanner.SkipChars (Ch: char): boolean;
begin
result := not Self.EndOfText;
if result then begin
while (Self.TextBuf[Self.Pos] = Ch) and Self.GotoNextChar do begin end;
result := not Self.EndOfText;
end;
end;
function TTextScanner.SkipCharset (const Charset: TCharSet): boolean;
begin
result := not Self.EndOfText;
if result then begin
while (Self.TextBuf[Self.Pos] in Charset) and Self.GotoNextChar do begin end;
result := not Self.EndOfText;
end;
end;
function TTextScanner.FindChar (Ch: char): boolean;
begin
result := not Self.EndOfText;
if result then begin
while (Self.TextBuf[Self.Pos] <> Ch) and Self.GotoNextChar do begin end;
result := not Self.EndOfText;
end;
end;
function TTextScanner.FindCharset (const Charset: TCharSet): boolean;
begin
result := not Self.EndOfText;
if result then begin
while not (Self.TextBuf[Self.Pos] in Charset) and Self.GotoNextChar do begin end;
result := not Self.EndOfText;
end;
end;
function TTextScanner.ReadToken (const TokenCharset: TCharSet; out Token: string): boolean;
var
StartPos: integer;
begin
result := not Self.EndOfText;
if result then begin
StartPos := Self.Pos;
Self.SkipCharset(TokenCharset);
Token := Copy(Self.TextBuf, StartPos, Self.Pos - StartPos);
end;
end; // .function TTextScanner.ReadToken
function TTextScanner.ReadTokenTillDelim (const DelimCharset: TCharSet; out Token: string): boolean;
var
StartPos: integer;
begin
result := not Self.EndOfText;
if result then begin
StartPos := Self.Pos;
Self.FindCharset(DelimCharset);
Token := Copy(Self.TextBuf, StartPos, Self.Pos - StartPos);
end;
end; // .function TTextScanner.ReadTokenTillDelim
procedure TTextScanner.Connect (const TextBuf: string; EndOfLineMarker: char);
const
MIN_STR_POS = 1;
begin
Self.fTextBuf := TextBuf;
Self.fTextBufLen := Length(TextBuf);
Self.fPos := MIN_STR_POS;
Self.fLineN := 1;
Self.fLineStartPos := MIN_STR_POS - 1;
Self.fEndOfLineMarker := EndOfLineMarker;
Self.fEndOfText := Self.TextBufLen = 0;
end; // .procedure Connect
end.