forked from CPsoftBE/BackupOfCromis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cromis.StringUtils.pas
564 lines (481 loc) · 15 KB
/
Cromis.StringUtils.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
(*
* This software is distributed under BSD license.
*
* Copyright (c) 2006-2009 Iztok Kacin, Cromis ([email protected]).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of the Iztok Kacin nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* =============================================================================
* String utilities that are not found in delphi already. Support for
* both ansi and widestring.
* =============================================================================
* 15/08/2006:
* - Initial implementation.
* 10/01/2008
* - optimization (Copy - > Move)
* - changed string to AnsiString for future compatibility with Tiburion
* 03/10/2008
* - D2009 compatible
* 25/10/2009
* - D2010 compatible
* 09/12/2010
* - Added TTextTokenizer
* 30/1/2012
* - XE2 compatible (hs)
* 16/2/2012
* - TTextTokenizer made unicode compatible
* 21/1/2013
* - Compiler defines adjusted to support XE3 and newer
* =============================================================================
*)
unit Cromis.StringUtils;
interface
uses
SysUtils, StrUtils, Classes,
// unicode support
Cromis.Unicode;
const
NumChars = '1234567890+-';
// string searching functions (ansi and wide)
function PosBack(const SubStr, S: astring): Integer; overload;
function PosBack(const SubStr, S: ustring): Integer; overload;
function PosAfter(const SubStr, S: ustring; Offset: Integer = 1): Integer; overload;
function PosAfter(const SubStr, S: astring; Offset: Integer = 1): Integer; overload;
// string token counting funcions (ansi and wide)
function CountSubStrings(const SubStr, S: astring): Integer; overload;
function CountSubStrings(const SubStr, S: ustring): Integer; overload;
// string extracting functions (ansi and wide)
function StrAfter(const BegPattern, S: astring): astring; overload;
function StrAfter(const BegPattern, S: ustring): ustring; overload;
function StrBefore(const EndPattern, S: astring): astring; overload;
function StrBefore(const EndPattern, S: ustring): ustring; overload;
function StrBetween(const BegPattern, EndPattern, S: astring): astring; overload;
function StrBetween(const BegPattern, EndPattern, S: ustring): ustring; overload;
// string replace function (ansi and wide)
function AnsiReplaceChars(Chars: array of achar; NewChar: achar; S: astring): astring;
function WideReplaceChars(Chars: array of uchar; NewChar: uchar; S: ustring): ustring;
// string manipulation functions (ansi and wide)
function PadLeft(const S: astring; const MaxLen: Integer; const PadChar: achar = #32): astring; overload;
function PadLeft(const S: ustring; const MaxLen: Integer; const PadChar: uchar = #32): ustring; overload;
function PadRight(const S: astring; const MaxLen: Integer; const PadChar: achar = #32): astring; overload;
function PadRight(const S: ustring; const MaxLen: Integer; const PadChar: uchar = #32): ustring; overload;
// string test functions (ansi and wide)
function StrConsistsOfNumberChars(S: astring): Boolean; overload;
function StrConsistsOfNumberChars(S: ustring): Boolean; overload;
function StrIsNumber(S: astring): Boolean; overload;
function StrIsNumber(S: ustring): Boolean; overload;
type
TTokens = array of ustring;
TTextTokenizer = class
private
FTokens: TTokens;
FDelimiters: array of ustring;
public
constructor Create;
procedure Tokenize(const Text: ustring);
procedure AddDelimiters(const Delimiters: array of ustring);
property Tokens: TTokens read FTokens;
end;
implementation
// -----------------------------------------------------------------------------------------
// string searching functions (ansi and wide)
// -----------------------------------------------------------------------------------------
function PosBack(const SubStr, S: astring): Integer;
var
LastStr: astring;
LastPos: Integer;
begin
Result := Pos(SubStr, S);
LastPos := Result;
LastStr := S;
while LastPos > 0 do
begin
LastStr := Copy(LastStr, LastPos + 1, Length(LastStr) - LastPos);
LastPos := Pos(SubStr, LastStr);
// set the result
if LastPos > 0 then
Result := Result + LastPos;
end;
end;
function PosBack(const SubStr, S: ustring): Integer;
var
LastStr: ustring;
LastPos: Integer;
begin
Result := Pos(SubStr, S);
LastPos := Result;
LastStr := S;
while LastPos > 0 do
begin
LastStr := Copy(LastStr, LastPos + 1, Length(LastStr) - LastPos);
LastPos := Pos(SubStr, LastStr);
// set the result
if LastPos > 0 then
Result := Result + LastPos;
end;
end;
function PosAfter(const SubStr, S: ustring; Offset: Integer): Integer;
var
BegPos: Integer;
begin
Result := 0;
if Offset <= Length(S) then
begin
BegPos := Pos(SubStr, Copy(S, Offset, Length(S) - Offset + 1));
if BegPos > 0 then
Result := BegPos + Offset - 1;
end;
end;
function PosAfter(const SubStr, S: astring; Offset: Integer): Integer;
var
BegPos: Integer;
begin
Result := 0;
if Offset <= Length(S) then
begin
BegPos := Pos(SubStr, Copy(S, Offset, Length(S) - Offset + 1));
if BegPos > 0 then
Result := BegPos + Offset - 1;
end;
end;
// -----------------------------------------------------------------------------------------
// string token counting funcions (ansi and wide)
// -----------------------------------------------------------------------------------------
function CountSubStrings(const SubStr, S: astring): Integer;
var
PrevPos: Integer;
begin
Result := 0;
PrevPos := 0;
repeat
PrevPos := PosAfter(SubStr, S, PrevPos + 1);
if PrevPos > 0 then
Inc(Result);
until (PrevPos = 0);
end;
function CountSubStrings(const SubStr, S: ustring): Integer;
var
PrevPos: Integer;
begin
Result := 0;
PrevPos := 0;
repeat
PrevPos := PosAfter(SubStr, S, PrevPos + 1);
if PrevPos > 0 then
Inc(Result);
until (PrevPos = 0);
end;
// -----------------------------------------------------------------------------------------
// string extracting functions (ansi and wide)
// -----------------------------------------------------------------------------------------
function StrBefore(const EndPattern, S: astring): astring; overload;
var
EndPos: Integer;
begin
Result := '';
EndPos := Pos(EndPattern, S);
if EndPos > 1 then
begin
SetLength(Result, EndPos - 1);
Move(S[1], Result[1], SizeOf(achar) * Length(Result));
end;
end;
function StrBefore(const EndPattern, S: ustring): ustring; overload;
var
EndPos: Integer;
begin
Result := '';
EndPos := Pos(EndPattern, S);
if EndPos > 1 then
begin
SetLength(Result, EndPos - 1);
Move(S[1], Result[1], SizeOf(uchar) * Length(Result));
end;
end;
function StrAfter(const BegPattern, S: astring): astring; overload;
var
BegPos: Integer;
begin
Result := '';
BegPos := Pos(BegPattern, S) + Length(BegPattern);
if (BegPos <= Length(S)) and (BegPos > Length(BegPattern)) then
begin
SetLength(Result, Length(S) - BegPos + 1);
Move(S[BegPos], Result[1], SizeOf(achar) * Length(Result));
end;
end;
function StrAfter(const BegPattern, S: ustring): ustring; overload;
var
BegPos: Integer;
begin
Result := '';
BegPos := Pos(BegPattern, S) + Length(BegPattern);
if (BegPos <= Length(S)) and (BegPos > Length(BegPattern)) then
begin
SetLength(Result, Length(S) - BegPos + 1);
Move(S[BegPos], Result[1], SizeOf(uchar) * Length(Result));
end;
end;
function StrBetween(const BegPattern, EndPattern, S: astring): astring; overload;
var
BegPos: Integer;
EndPos: Integer;
begin
BegPos := PosAfter(BegPattern, S, 1) + Length(BegPattern);
EndPos := PosAfter(EndPattern, S, BegPos + 1);
Result := '';
if (EndPos > BegPos) and (BegPos > Length(BegPattern)) then
begin
// move the string to the result
SetLength(Result, EndPos - BegPos);
Move(S[BegPos], Result[1], SizeOf(achar) * Length(Result));
end;
end;
function StrBetween(const BegPattern, EndPattern, S: ustring): ustring; overload;
var
BegPos: Integer;
EndPos: Integer;
begin
BegPos := PosAfter(BegPattern, S, 1) + Length(BegPattern);
EndPos := PosAfter(EndPattern, S, BegPos + 1);
Result := '';
if (EndPos > BegPos) and (BegPos > Length(BegPattern)) then
begin
// move the string to the result
SetLength(Result, EndPos - BegPos);
Move(S[BegPos], Result[1], SizeOf(uchar) * Length(Result));
end;
end;
// -----------------------------------------------------------------------------------------
// string test functions (ansi and wide)
// -----------------------------------------------------------------------------------------
function StrConsistsOfNumberChars(S: astring): Boolean; overload;
var
I: Integer;
begin
Result := True;
for I := 1 to Length(S) do
begin
if (Pos(S[I], AnsiString(NumChars)) = 0) and
not (S[I] = achar({$IF CompilerVersion >= 22}Formatsettings.{$IFEND}DecimalSeparator)) then
begin
Result := False;
Exit;
end;
end;
end;
function StrConsistsOfNumberChars(S: ustring): Boolean; overload;
var
I: Integer;
begin
Result := True;
for I := 1 to Length(S) do
begin
if (Pos(S[I], NumChars) = 0) and
not (S[I] = uchar({$IF CompilerVersion >= 22}Formatsettings.{$IFEND}DecimalSeparator)) then
begin
Result := False;
Exit;
end;
end;
end;
function StrIsNumber(S: astring): Boolean;
var
I: Integer;
begin
Result := True;
for I := 1 to Length(S) do
begin
if I = 1 then
begin
if not (Ord(S[I]) in [48..57]) and not (Ord(S[I]) in [43, 45]) then
begin
Result := False;
Exit;
end;
end
else
begin
if not (Ord(S[I]) in [48..57]) and
not (S[I] = achar({$IF CompilerVersion >= 22}Formatsettings.{$IFEND}DecimalSeparator)) then
begin
Result := False;
Exit;
end;
end;
end;
end;
function StrIsNumber(S: ustring): Boolean;
var
I: Integer;
begin
Result := True;
for I := 1 to Length(S) do
begin
if I = 1 then
begin
if not (Ord(S[I]) in [48..57]) and not (Ord(S[I]) in [43, 45]) then
begin
Result := False;
Exit;
end;
end
else
begin
if not (Ord(S[I]) in [48..57]) and
not (S[I] = uchar({$IF CompilerVersion >= 22}Formatsettings.{$IFEND}DecimalSeparator)) then
begin
Result := False;
Exit;
end;
end;
end;
end;
// -----------------------------------------------------------------------------------------
// string replace function (ansi and wide)
// -----------------------------------------------------------------------------------------
function AnsiReplaceChars(Chars: array of achar; NewChar: achar; S: astring): astring;
var
I: Integer;
function CharInArray(TestChar: achar): Boolean;
var
K: Integer;
begin
Result := False;
for K := 0 to Length(Chars) - 1 do
begin
if TestChar = Chars[I] then
begin
Result := True;
Exit;
end;
end;
end;
begin
for I := 1 to Length(S) do
if CharInArray(S[I]) then
S[I] := NewChar;
Result := S;
end;
function WideReplaceChars(Chars: array of uchar; NewChar: uchar; S: ustring): ustring;
var
I: Integer;
function CharInArray(TestChar: uchar): Boolean;
var
K: Integer;
begin
Result := False;
for K := 0 to Length(Chars) - 1 do
begin
if TestChar = Chars[I] then
begin
Result := True;
Exit;
end;
end;
end;
begin
for I := 1 to Length(S) do
if CharInArray(S[I]) then
S[I] := NewChar;
Result := S;
end;
// -----------------------------------------------------------------------------------------
// string manipulation functions (ansi and wide)
// -----------------------------------------------------------------------------------------
function PadLeft(const S: astring; const MaxLen: Integer; const PadChar: achar): astring;
begin
Result := StringOfChar(PadChar, MaxLen - Length(S)) + S;
end;
function PadLeft(const S: ustring; const MaxLen: Integer; const PadChar: uchar): ustring;
begin
Result := StringOfChar(PadChar, MaxLen - Length(S)) + S;
end;
function PadRight(const S: astring; const MaxLen: Integer; const PadChar: achar): astring;
begin
Result := S + StringOfChar(PadChar, MaxLen - Length(S));
end;
function PadRight(const S: ustring; const MaxLen: Integer; const PadChar: uchar): ustring;
begin
Result := S + StringOfChar(PadChar, MaxLen - Length(S));
end;
{ TTextTokenizer }
procedure TTextTokenizer.AddDelimiters(const Delimiters: array of ustring);
var
I: Integer;
begin
if Length(Delimiters) > 0 then
begin
SetLength(FDelimiters, Length(Delimiters));
for I := 0 to Length(Delimiters) - 1 do
FDelimiters[I] := Delimiters[I];
end;
end;
constructor TTextTokenizer.Create;
begin
SetLength(FTokens, 0);
SetLength(FDelimiters, 0);
end;
procedure TTextTokenizer.Tokenize(const Text: ustring);
var
I, K: Integer;
Counter: Integer;
NewToken: ustring;
Position: Integer;
CurrToken: ustring;
begin
SetLength(FTokens, 100);
CurrToken := '';
Counter := 0;
for I := 1 to Length(Text) do
begin
CurrToken := CurrToken + Text[I];
for K := 0 to Length(FDelimiters) - 1 do
begin
Position := Pos(FDelimiters[K], CurrToken);
if Position > 0 then
begin
NewToken := Copy(CurrToken, 1, Position - 1);
if NewToken <> '' then
begin
if Counter > Length(FTokens) then
SetLength(FTokens, Length(FTokens) * 2);
FTokens[Counter] := Trim(NewToken);
Inc(Counter)
end;
CurrToken := '';
end;
end;
end;
if CurrToken <> '' then
begin
if Counter > Length(FTokens) then
SetLength(FTokens, Length(FTokens) * 2);
FTokens[Counter] := Trim(CurrToken);
Inc(Counter)
end;
SetLength(FTokens, Counter);
end;
end.