-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonstrutils.pas
448 lines (409 loc) · 11.9 KB
/
commonstrutils.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
unit CommonStrUtils;
{ Common routines to work with strings.
Copyright (C) 2018 Mohammadreza Bahrami m.audio91@gmail.com
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Character;
function IsEmptyStr(const AStr: String): Boolean;
function Escape(const S: String; const ToEscape: String = ' ';
const EscapeTo: String = '?'): String;
function DeEscape(const S: String; const ToDeEscape: String = '?';
const DeEscapeTo: String = ' '): String;
function QuoteAndEscape(const S: String; const ToEscape: String = ' ';
const EscapeTo: String = '?'; QuoteChar: Char = '"'): String;
function PrependEscapeChars(const S, Chars: String; EscChar: Char): String;
function NoChainedSpaces(const S: String): String;
function NowToString: String;
function CountAWord(const AWord, AStr: String): Integer;
function StrForceLength(const AStr: String; ALen: Integer; APadChar: Char;
PadFromRight: Boolean = False): String;
function StringIsNumber(const AStr: String): Boolean; inline;
function CharsAreDigit(const AStr: String; CharIndexes: array of Integer;
ZeroBasedIndexes: Boolean = False): Boolean;
function CharsEqual(const AStr: String; AChar: Char; CharIndexes:
array of Integer): Boolean;
function NextNonDigit(const AStr: String; AIndex: Integer): Integer;
function PrevNonDigit(const AStr: String; AIndex: Integer): Integer;
procedure RemoveDupsInArray(var AArray: TStringArray);
procedure NilIfEmptyArray(var AArray: TStringArray);
function StringListToArray(var SL: TStringList): TStringArray;
procedure ArrayToStringList(var AArray: TStringArray; var SL: TStringList);
function FindInArray(var AArray: TStringArray; AText: String; AStartIndex:
Integer = 0): Integer;
function FindAnyInArray(var AArray: TStringArray; const ATexts: array of String;
AStartIndex: Integer = 0): Integer;
procedure DeleteAllOccurrences(const SubStr: String; var Source: String;
const CensorMask: String = '');
procedure DeleteAllOccurrencesVL(const SubStrStart, SubStrEnd: String;
var Source: String; IncludeEnd: Boolean = True; const CensorMask: String = '');
function ReplaceStrings(const S: String; Old, New: array of String): String;
function NthIndexOf(const ASubStr, AStr: String; N: Integer): Integer;
function ReplaceFirst(const AStr, SubStr, NewSubStr: String;
FromEnd: Boolean = False): String;
function AddLineEndings(const AStr: String; ACount: Integer = 1;
PlusSpaces: Boolean = False; Prepend: Boolean = False): String;
const
LineEndings: array[0..2] of String = (#13#10,#13,#10);
implementation
function IsEmptyStr(const AStr: String): Boolean;
begin
Result := Astr.Trim([#7,#8,#9,#10,#11,#12,#13,#32,#255]) = EmptyStr;
end;
function Escape(const S: String; const ToEscape: String; const EscapeTo: String
): String;
begin
Result := StringReplace(S, ToEscape, EscapeTo, [rfReplaceAll,rfIgnoreCase]);
end;
function DeEscape(const S: String; const ToDeEscape: String;
const DeEscapeTo: String): String;
begin
Result := StringReplace(S, ToDeEscape, DeEscapeTo, [rfReplaceAll,rfIgnoreCase]);
end;
function QuoteAndEscape(const S: String; const ToEscape: String;
const EscapeTo: String; QuoteChar: Char): String;
begin
Result := QuoteChar + Escape(S, ToEscape, EscapeTo) + QuoteChar;
end;
function PrependEscapeChars(const S, Chars: String; EscChar: Char): String;
var
i: Integer;
begin
Result := S;
if Chars = EmptyStr then Exit;
if EscChar in [#0..#31] then Exit;
for i := S.Length-1 downto 0 do
if Chars.IndexOf(S.Chars[i]) >= 0 then
Result := Result.Insert(i, EscChar);
end;
function NoChainedSpaces(const S: String): String;
var
i: Integer;
begin
Result := S;
repeat
i := Result.IndexOf(' ');
if i >= 0 then
Result := Result.Remove(i, 1);
until i < 0;
Result := Result.Trim;
end;
function NowToString: String;
var
dt: TDateTime;
fs: TFormatSettings;
begin
fs := DefaultFormatSettings;
fs.DateSeparator := '-';
fs.TimeSeparator := '-';
dt := Now;
Result := Escape(DateTimeToStr(dt, fs), ' ', '-');
end;
function CountAWord(const AWord, AStr: String): Integer;
var
i,j: Integer;
begin
Result := 0;
j := -1;
repeat
i := AStr.IndexOf(AWord, j+1);
if i >= 0 then
begin
j := i;
Inc(Result);
end;
until i < 0;
end;
function StrForceLength(const AStr: String; ALen: Integer; APadChar: Char;
PadFromRight: Boolean = False): String;
begin
Result := AStr;
if AStr.Length < ALen then
begin
Result := EmptyStr;
case PadFromRight of
True: Result := AStr.PadRight(ALen, APadChar);
False: Result := AStr.PadLeft(ALen, APadChar);
end;
end
else if AStr.Length > ALen then
Result := AStr.Substring(0, ALen);
end;
function StringIsNumber(const AStr: String): Boolean;
begin
Result := True;
if Astr = EmptyStr then Exit(False);
try
if (AStr.ToDouble*0 = 0) then Exit;
except
Result := False;
end;
end;
function CharsAreDigit(const AStr: String; CharIndexes: array of Integer;
ZeroBasedIndexes: Boolean): Boolean;
var
i,j: Integer;
begin
Result := False;
j := 0;
if Astr = EmptyStr then Exit;
if Length(CharIndexes) = 0 then Exit;
if ZeroBasedIndexes then
Inc(j);
for i in CharIndexes do
if not IsNumber(AStr, i+j) then Exit;
Result := True;
end;
function CharsEqual(const AStr: String; AChar: Char;
CharIndexes: array of Integer): Boolean;
var
i: Integer;
begin
Result := False;
for i in CharIndexes do
if not (AStr.Chars[i-1] = AChar) then Exit;
Result := True;
end;
function NextNonDigit(const AStr: String; AIndex: Integer): Integer;
begin
Result := 0;
if AIndex < 1 then Exit;
if AIndex > AStr.Length-1 then Exit;
repeat
Inc(AIndex);
if not CharsAreDigit(AStr, [AIndex]) then
begin
Result := AIndex;
Break;
end;
until AIndex >= AStr.Length;
end;
function PrevNonDigit(const AStr: String; AIndex: Integer): Integer;
begin
Result := 0;
if AIndex <= 1 then Exit;
if AIndex > AStr.Length then Exit;
repeat
Dec(AIndex);
if not CharsAreDigit(AStr, [AIndex]) then
begin
Result := AIndex;
Break;
end;
until AIndex <= 1;
end;
procedure RemoveDupsInArray(var AArray: TStringArray);
var
TempArray: TStringArray;
i,j,l: Integer;
begin
if Length(AArray) < 2 then Exit;
for i := High(AArray) downto 0 do
for j := i-1 downto 0 do
if AArray[i].Equals(AArray[j]) then
begin
AArray[i] := EmptyStr;
Break;
end;
l := 0;
for i := 0 to High(AArray) do
if AArray[i] = EmptyStr then
Inc(l);
SetLength(TempArray, Length(AArray)-l);
j := 0;
for i := 0 to High(AArray) do
if AArray[i] <> EmptyStr then
begin
TempArray[j] := AArray[i];
Inc(j);
end;
AArray := nil;
AArray := TempArray;
end;
procedure NilIfEmptyArray(var AArray: TStringArray);
var
CanNil: Boolean;
i: Integer;
begin
CanNil := True;
for i := 0 to High(AArray) do
if AArray[i] <> EmptyStr then
begin
CanNil := False;
Break;
end;
if CanNil then
AArray := nil;
end;
function StringListToArray(var SL: TStringList): TStringArray;
var
i: Integer;
begin
Result := nil;
if not Assigned(SL) then Exit;
SetLength(Result, SL.Count);
for i := 0 to SL.Count-1 do
Result[i] := SL[i];
end;
procedure ArrayToStringList(var AArray: TStringArray; var SL: TStringList);
var
i: Integer;
begin
if not Assigned(SL) then Exit;
SL.Clear;
i := Length(AArray);
if i < 1 then Exit;
SL.Capacity := i;
for i := 0 to High(AArray) do
SL.Add(AArray[i]);
end;
function FindInArray(var AArray: TStringArray; AText: String; AStartIndex:
Integer): Integer;
var
i: Integer;
begin
Result := -1;
i := Length(AArray);
if i < 1 then Exit;
if AText = EmptyStr then Exit;
if i <= AStartIndex then Exit;
for i := AStartIndex to High(AArray) do
if AArray[i].IndexOf(AText) >= 0 then
begin
Result := i;
Break;
end;
end;
function FindAnyInArray(var AArray: TStringArray;
const ATexts: array of String; AStartIndex: Integer): Integer;
var
Indexes: array of Integer;
i,j: Integer;
begin
j := -1;
SetLength(Indexes, Length(ATexts));
for i := 0 to High(Indexes) do
Indexes[i] := FindInArray(AArray, ATexts[i], AStartIndex);
for i := 0 to High(Indexes) do
if Indexes[i] > -1 then
j := Indexes[i];
for i := 0 to High(Indexes) do
if (Indexes[i] > -1)
and (Indexes[i] < j)then
j := Indexes[i];
Result := j;
end;
procedure DeleteAllOccurrences(const SubStr: String; var Source: String;
const CensorMask: String);
begin
Source := Source.Replace(SubStr, CensorMask);
end;
procedure DeleteAllOccurrencesVL(const SubStrStart, SubStrEnd: String;
var Source: String; IncludeEnd: Boolean; const CensorMask: String);
var
i,j,k: Integer;
begin
j := 0;
repeat
i := Source.IndexOf(SubStrStart, j);
if i < 0 then Break;
j := i;
k := Source.IndexOf(SubStrEnd, i);
if k >= 0 then
begin
k := k - j;
if IncludeEnd then
k := k + SubStrEnd.Length;
Source := Source.Remove(i, k);
if not IsEmptyStr(CensorMask) then
Source := Source.Insert(i, CensorMask);
end;
until i < 0;
end;
function ReplaceStrings(const S: String; Old, New: array of String): String;
var
Flags: TReplaceFlags;
i: Integer;
begin
Result := S;
Flags := [rfReplaceAll,rfIgnoreCase];
if High(Old) <> High(New) then Exit;
for i := Low(Old) to High(Old) do
Result := Result.Replace(Old[i], New[i], Flags);
end;
function NthIndexOf(const ASubStr, AStr: String; N: Integer): Integer;
var
i,j: Integer;
begin
Result := -1;
if IsEmptyStr(ASubStr) or IsEmptyStr(AStr) then Exit;
if (N < 1) or (AStr.Length < N) then Exit;
i := 0;
j := 0;
repeat
i := AStr.IndexOf(ASubStr, i);
if i >= 0 then
begin
Inc(j);
if j = N then Exit(i);
Inc(i);
end;
until i < 0;
end;
function ReplaceFirst(const AStr, SubStr, NewSubStr: String;
FromEnd: Boolean): String;
var
i: Integer;
begin
Result := AStr;
if IsEmptyStr(SubStr) then Exit;
if FromEnd then
i := AStr.LastIndexOf(SubStr)
else
i := AStr.IndexOf(SubStr);
if i > -1 then
begin
Result := AStr.Remove(i,SubStr.Length);
Result := Result.Insert(i,NewSubStr);
end;
end;
function AddLineEndings(const AStr: String; ACount: Integer;
PlusSpaces: Boolean; Prepend: Boolean): String;
var
i: Integer;
begin
if ACount < 1 then Exit(AStr);
Result := EmptyStr;
for i:=0 to ACount-1 do
case PlusSpaces of
True: Result := Result+LineEnding+' ';
False: Result := Result+LineEnding;
end;
case Prepend of
True: Result := Result+AStr;
False: Result := AStr+Result;
end;
end;
end.