-
Notifications
You must be signed in to change notification settings - Fork 3
/
StringLists.pas
302 lines (257 loc) · 8.27 KB
/
StringLists.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
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
{
Copyright (c) Peter Karpov 2010 - 2018.
Usage of the works is permitted provided that this instrument is retained with
the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
}
{$IFDEF FPC} {$MODE DELPHI} {$ENDIF}
unit StringLists; ///////////////////////////////////////////////////////////////////
{
>> Version: 1.2
>> Description
Basic string list implementation.
>> Author
Peter Karpov
Email : [email protected]
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> ToDo
- Use hashing if fast search is required
>> Changelog
1.2 : 2019.10.02 * Parse procedure
1.1 : 2018.09.09 ~ FreePascal compatibility
1.0 : 2014.09.25 ~ Sectionized
+ ExtractWords procedure
+ LoadLines procedure
0.2 : 2013.10.21 + SaveStringList function
0.1 : 2013.05.23 + Find, AddUnique, Parse procedures
0.0 : 2011.03.04 + Initial version
Notation: + added, - removed, * fixed, ~ changed
}
interface ///////////////////////////////////////////////////////////////////////////
uses
StringUtils;
type
TStringList =
record
N : Integer;
_ : array of AnsiString;
end;
{-----------------------<< Basic operations >>--------------------------------------}
// Initialize StringList
procedure InitStringList(
var StringList : TStringList);
// Add S to StringList
procedure AddToStringList(
var StringList : TStringList;
const S : AnsiString);
// Add S to StringList if S is not empty
procedure AddIfNotEmpty(
var StringList : TStringList;
const S : AnsiString);
// Add S to StringList if it is not there yet. O(StringList.N) time.
procedure AddUnique(
var StringList : TStringList;
const S : AnsiString);
// Return Index of S in StringList. If S is not found, Index is left unchanged and
// Fail is returned. O(StringList.N) time.
function Find(
var Index : Integer;
const StringList : TStringList;
const S : AnsiString
) : Boolean;
{-----------------------<< Splitting Strings >>-------------------------------------}
// Parse substrings of S delimited by Delim into StringList
procedure Parse(
var StringList : TStringList;
const S, Delim : AnsiString);
// Make list of Words from a string S. Words are defined as consecutive sequences of
// characters in WordChars. Transform to lower case if LowCase is True.
procedure ExtractWords(
var Words : TStringList;
const S : AnsiString;
LowCase : Boolean;
WordChars : CharSet = AlphabetChars);
{-----------------------<< Reading and Writing >>-----------------------------------}
// Save StringList to specified Path, return operation status
function SaveStringList(
const StringList : TStringList;
const Path : AnsiString
) : Boolean;
// Load file at Path line by line as a StringList
procedure LoadLines(
var StringList : TStringList;
const Path : AnsiString);
// Display StringList in console terminal
procedure DisplayStringList(
const StringList : TStringList);
implementation //////////////////////////////////////////////////////////////////////
uses
InvSys;
{-----------------------<< Basic operations >>--------------------------------------}
// Initialize StringList
procedure InitStringList(
var StringList : TStringList);
begin
StringList.N := 0;
SetLength(StringList._, 0);
end;
// Add S to StringList
procedure AddToStringList(
var StringList : TStringList;
const S : AnsiString);
begin
Inc(StringList.N);
if StringList.N > Length(StringList._) then
SetLength(StringList._, 2 * StringList.N);
StringList._[StringList.N - 1] := S;
end;
// Add S to StringList if S is not empty
procedure AddIfNotEmpty(
var StringList : TStringList;
const S : AnsiString);
begin
if S <> '' then
AddToStringList(StringList, S);
end;
// Return Index of S in StringList. If S is not found, Index is left unchanged and
// Fail is returned. O(StringList.N) time.
function Find(
var Index : Integer;
const StringList : TStringList;
const S : AnsiString
) : Boolean;
var
i : Integer;
begin
Result := Fail;
for i := 0 to StringList.N - 1 do
if StringList._[i] = S then
begin
Index := i;
Result := Success;
{<---}break;
end;
end;
// Add S to StringList if it is not there yet. O(StringList.N) time.
procedure AddUnique(
var StringList : TStringList;
const S : AnsiString);
var
Index : Integer;
Found : Boolean;
begin
Found := Find(Index, StringList, S);
if not Found then
AddToStringList(StringList, S);
end;
{-----------------------<< Splitting Strings >>-------------------------------------}
// Parse substrings of S delimited by Delim into StringList
procedure Parse(
var StringList : TStringList;
const S, Delim : AnsiString);
var
i, j : Integer;
Found : Boolean;
SubS : AnsiString;
begin
InitStringList(StringList);
i := 1;
repeat
j := 1 + Length(S);
Found := GetPos(j, S, Delim, {Start:} i);
SubS := CopyRange(S, i, j - 1);
AddToStringList(StringList, SubS);
i := j + Length(Delim);
until not Found;
end;
// Make list of Words from a string S. Words are defined as consecutive sequences of
// characters in WordChars. Transform to lower case if LowCase is True.
procedure ExtractWords(
var Words : TStringList;
const S : AnsiString;
LowCase : Boolean;
WordChars : CharSet = AlphabetChars);
var
i1, i2, Len : Integer;
WorkS : AnsiString;
begin
// Transform to lower case if required
if LowCase then
WorkS := LowerCase(S) else
WorkS := S;
// Add words one by one
InitStringList(Words);
Len := Length(WorkS);
i1 := 0;
repeat
// Find next alphabetic character
repeat
Inc(i1);
until (i1 = Len) or (WorkS[i1] in WordChars);
if WorkS[i1] in WordChars then
begin
// Seek end of word
i2 := i1;
while (i2 < Len) and (WorkS[i2] in WordChars) do
Inc(i2);
if not (WorkS[i2] in WordChars) then
Dec(i2);
// Add to word list
AddToStringList(Words, CopyRange(WorkS, i1, i2));
i1 := i2 + 1;
if i1 >= Len then
{<------}break;
end
else
{<---}break;
until False;
end;
{-----------------------<< Reading and Writing >>-----------------------------------}
// Load file at Path line by line as a StringList
procedure LoadLines(
var StringList : TStringList;
const Path : AnsiString);
var
TempS : AnsiString;
InFile : Text;
begin
InitStringList(StringList);
Assign(InFile, Path);
Reset(InFile);
repeat
ReadLn(InFile, TempS);
AddToStringList(StringList, TempS);
until EoF(InFile);
Close(InFile);
end;
// Save StringList to specified Path, return operation status
function SaveStringList(
const StringList : TStringList;
const Path : AnsiString
) : Boolean;
var
FileOut : Text;
i : Integer;
begin
if OpenWrite(FileOut, Path) then
begin
for i := 0 to StringList.N - 1 do
WriteLn(FileOut, StringList._[i]);
Close(FileOut);
Result := Success;
end
else
Result := Fail;
end;
// Display StringList in console terminal
procedure DisplayStringList(
const StringList : TStringList);
var
i : LongInt;
begin
for i := 0 to StringList.N - 1 do
WriteLn(StringList._[i]);
end;
end.