-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.pas
81 lines (72 loc) · 1.64 KB
/
Token.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
unit Token;
interface
function GetToken(aString, SepChar: string; TokenNum: Byte): string;
function GetFirstToken(Line: String): String;
function GetNextToken: String;
function GetRemainingTokens: String;
implementation
var
TokenString: String;
function GetToken(aString, SepChar: string; TokenNum: Byte): string;
var
Token: string;
StrLen: Integer;
Num: Integer;
EndofToken: Integer;
begin
StrLen := Length(aString);
Num := 1;
EndofToken := StrLen;
while ((Num <= TokenNum) and (EndofToken <> 0)) do
begin
EndofToken := Pos(SepChar, aString);
if EndofToken <> 0 then
begin
Token := Copy(aString, 1, EndofToken - 1);
Delete(aString, 1, EndofToken);
Inc(Num);
end
else
Token := aString;
end;
if Num >= TokenNum then
Result := Token
else
Result := '';
end;
function GetFirstToken(Line: String): String;
begin
TokenString := Line;
Result := GetNextToken;
end;
function GetNextToken: String;
var
i, j: Integer;
begin
Result := '';
if TokenString <> '' then
begin
i := 1;
while TokenString[i] in [#9, #10, #13, ' '] do
Inc(i);
j := 0;
while not(TokenString[i + j] in [#0, #9, #10, #13, ' ']) do
Inc(j);
Result := Copy(TokenString, i, j);
TokenString := Copy(TokenString, i + j, High(Integer));
end;
end;
function GetRemainingTokens: String;
var
i: Integer;
begin
Result := '';
if TokenString <> '' then
begin
i := 1;
while TokenString[i] in [#9, #10, #13, ' '] do
Inc(i);
Result := Copy(TokenString, i, High(Integer));
end;
end;
end.