-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathStringScanner.pas
More file actions
129 lines (106 loc) · 3.74 KB
/
StringScanner.pas
File metadata and controls
129 lines (106 loc) · 3.74 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
unit StringScanner;
{
Inno Setup
Copyright (C) 1997-2026 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
TStringScanner
}
interface
uses
SysUtils;
type
EStringScannerError = class(Exception);
TStringScanner = record
strict private
FStr: String;
FPosition: Integer;
function GetReachedEnd: Boolean;
function GetRemainingCount: Integer;
public
class function Create(const AString: String): TStringScanner; static;
function Consume(const C: Char): Boolean; overload;
function Consume(const S: String): Boolean; overload;
function ConsumeMulti(const AAllowedChars: TSysCharSet;
const AAllowAllCharsAboveFF: Boolean = False; const AMinChars: Integer = 1;
const AMaxChars: Integer = MaxInt): Integer;
function ConsumeMultiToString(const AAllowedChars: TSysCharSet;
var ACapturedString: String; const AAllowAllCharsAboveFF: Boolean = False;
const AMinChars: Integer = 1; const AMaxChars: Integer = MaxInt): Integer;
property ReachedEnd: Boolean read GetReachedEnd;
property RemainingCount: Integer read GetRemainingCount;
property Str: String read FStr;
end;
implementation
{$ZEROBASEDSTRINGS OFF}
{ TStringScanner }
class function TStringScanner.Create(const AString: String): TStringScanner;
begin
Result.FPosition := 1;
Result.FStr := AString;
end;
function TStringScanner.Consume(const C: Char): Boolean;
begin
Result := (GetRemainingCount > 0) and (FStr[FPosition] = C);
if Result then
Inc(FPosition);
end;
function TStringScanner.Consume(const S: String): Boolean;
begin
const SLen = Length(S);
if SLen > GetRemainingCount then
Exit(False);
for var I := 0 to SLen-1 do
if FStr[FPosition + I] <> S[I+1] then
Exit(False);
Inc(FPosition, SLen);
Result := True;
end;
function TStringScanner.ConsumeMulti(const AAllowedChars: TSysCharSet;
const AAllowAllCharsAboveFF: Boolean = False; const AMinChars: Integer = 1;
const AMaxChars: Integer = MaxInt): Integer;
begin
{ AMinChars may be 0; it functions the same as 1 }
if (AMinChars < 0) or (AMinChars > AMaxChars) then
raise EStringScannerError.Create('TStringScanner.ConsumeMulti: Invalid parameter');
const Remain = GetRemainingCount;
if Remain < AMinChars then
Exit(0);
Result := 0;
while (Result < AMaxChars) and (Result < Remain) and
(CharInSet(FStr[FPosition + Result], AAllowedChars) or
(AAllowAllCharsAboveFF and (Ord(FStr[FPosition + Result]) > $FF))) do
Inc(Result);
if Result < AMinChars then
Result := 0
else
Inc(FPosition, Result);
end;
function TStringScanner.ConsumeMultiToString(const AAllowedChars: TSysCharSet;
var ACapturedString: String; const AAllowAllCharsAboveFF: Boolean = False;
const AMinChars: Integer = 1; const AMaxChars: Integer = MaxInt): Integer;
begin
const StartPos = FPosition;
Result := ConsumeMulti(AAllowedChars, AAllowAllCharsAboveFF, AMinChars, AMaxChars);
if Result > 0 then
ACapturedString := Copy(FStr, StartPos, Result)
else
ACapturedString := '';
end;
function TStringScanner.GetReachedEnd: Boolean;
begin
Result := (GetRemainingCount = 0);
end;
function TStringScanner.GetRemainingCount: Integer;
begin
{ The "<= 0" check exists to protect against OOB reads in case someone calls
into an instance that was never properly initialized (via Create).
Inside TStringScanner, FStr[FPosition] must not be accessed unless
GetRemainingCount is called first and returns a nonzero value. }
const Len = Length(FStr);
if (FPosition <= 0) or (FPosition > Len) then
Result := 0
else
Result := Len - FPosition + 1;
end;
end.