-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextMan.pas
More file actions
151 lines (133 loc) · 4.37 KB
/
TextMan.pas
File metadata and controls
151 lines (133 loc) · 4.37 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
unit Textman;
{
DESCRIPTION: Text manager provides high-level functions-wrappers over AText
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
}
(***) interface (***)
uses Windows, SysUtils, Utils, Crypto, StrLib, CFiles, Files, ATexts;
const
(* FindStr *)
IGNORE_CASE = TRUE;
type
TCharPreprocessFunc = function (c: char): char;
function FindStr (Str: string; IgnoreCase: boolean; Text: ATexts.AText): boolean;
procedure ReplaceStr (Str: string; ReplWith: string; IgnoreCase: boolean; Text: ATexts.AText);
function LoadFromFile (const FilePath: string; {n} Settings: Utils.TCloneable; Text: ATexts.AText): boolean;
function SaveToFile (const FilePath: string; Text: ATexts.AText): boolean;
(***) implementation (***)
function FindStr (Str: string; IgnoreCase: boolean; Text: ATexts.AText): boolean;
var
StrLen: integer;
StrHash: integer;
MatchHash: integer;
HashInd: integer;
StartPos: integer;
c: char;
i: integer;
function IsMatch: boolean;
begin
Text.GotoPos(StartPos);
result := TRUE;
i := 1;
while result and (i <= StrLen) do begin
Text.GetCurrChar(c);
if IgnoreCase then begin
c := StrLib.CharToLower(c);
end;
result := c = Str[i];
Text.GotoNextPos;
Inc(i);
end;
end; // .function IsMatch
begin
{!} Assert(Text <> nil);
StrLen := Length(Str);
result := FALSE;
if (Text.Len - Text.Pos + 1) >= StrLen then begin
if IgnoreCase then begin
Str := SysUtils.AnsiLowerCase(Str);
end;
StartPos := Text.Pos;
StrHash := 0;
HashInd := 0;
for i:=1 to StrLen do begin
c := Str[i];
if IgnoreCase then begin
c := StrLib.CharToLower(c);
end;
Inc(Crypto.TInt32(StrHash)[HashInd], Crypto.ByteRedirTable[ORD(c)]);
HashInd := (HashInd + 1) and 3;
end;
MatchHash := 0;
HashInd := 0;
for i:=1 to StrLen do begin
Text.GetCurrChar(c);
if IgnoreCase then begin
c := StrLib.CharToLower(c);
end;
Inc(Crypto.TInt32(MatchHash)[HashInd], Crypto.ByteRedirTable[ORD(c)]);
HashInd := (HashInd + 1) and 3;
Text.GotoNextPos;
end;
result := (MatchHash = StrHash) and IsMatch;
HashInd := (StrLen - 1) and 3;
while (not result) and (not Text.TextEnd) do begin
Text.GotoPos(StartPos);
Text.GetCurrChar(c);
if IgnoreCase then begin
c := StrLib.CharToLower(c);
end;
Dec(Crypto.TInt32(MatchHash)[0], Crypto.ByteRedirTable[ORD(c)]);
MatchHash := ((MatchHash and $FF) shl 24) or (MatchHash shr 8);
Text.GotoPos(StartPos + StrLen);
Text.GetCurrChar(c);
if IgnoreCase then begin
c := StrLib.CharToLower(c);
end;
Inc(Crypto.TInt32(MatchHash)[HashInd], Crypto.ByteRedirTable[ORD(c)]);
Inc(StartPos);
result := (MatchHash = StrHash) and IsMatch;
Text.GotoNextPos;
end; // .while
if result then begin
Text.GotoPos(StartPos);
end;
end; // .if
end; // .function FindStr
procedure ReplaceStr (Str: string; ReplWith: string; IgnoreCase: boolean; Text: ATexts.AText);
begin
{!} Assert(Text <> nil);
while FindStr(Str, IgnoreCase, Text) do begin
Text.Replace(Length(Str), ReplWith);
end;
end;
function LoadFromFile (const FilePath: string; {n} Settings: Utils.TCloneable; Text: ATexts.AText): boolean;
var
{O} DataFile: Files.TFile;
FileContents: string;
begin
{!} Assert(Text <> nil);
DataFile := Files.TFile.Create;
// * * * * * //
result := DataFile.Open(FilePath, CFiles.MODE_READ) and DataFile.ReadAllToStr(FileContents);
if result then begin
Text.Connect(FileContents, TObject(Settings));
end;
SysUtils.FreeAndNil(DataFile);
end; // .function LoadFromFile
function SaveToFile (const FilePath: string; Text: ATexts.AText): boolean;
var
{O} DataFile: Files.TFile;
SavedPos: integer;
begin
{!} Assert(Text <> nil);
DataFile := Files.TFile.Create;
// * * * * * //
SavedPos := Text.Pos;
Text.GotoPos(1);
Windows.DeleteFile(pchar(FilePath));
result := DataFile.CreateNew(FilePath) and DataFile.WriteStr(Text.GetStr(Text.Len));
Text.GotoPos(SavedPos);
SysUtils.FreeAndNil(DataFile);
end; // .function SaveToFile
end.