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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
|
Unit ACLStringUtility;
{$mode objfpc}{$H+}
Interface
Uses
Classes;
const
CharTAB = chr(9);
CharCR = chr(13);
CharLF = chr(10);
CharSingleQuote = '''';
CharDoubleQuote = '"';
EndLine = CharCR + CharLF;
StrTAB = CharTAB;
StrCR = CharCR;
StrLF = CharLF;
StrCRLF = StrCR + StrLF;
StrSingleQuote = CharSingleQuote;
StrDoubleQuote = CharDoubleQuote;
type
TSetOfChars = set of char;
TCharMatchFunction = function( const a: char ): boolean;
TSerializableStringList = class(TObject)
private
stringList: TStrings;
public
constructor Create;
destructor Destroy; override;
function getCount : LongInt;
function get(const anIndex : LongInt) : String;
function getSerializedString : String;
procedure add(const aString : String);
procedure readValuesFromSerializedString(const aSerializedString : String);
end;
// Returns true if c is a digit 0..9
Function IsDigit( const c: char ): boolean;
// Returns true if c is not a digit
Function IsNonDigit( const c: char ): boolean;
// Returns true if c is an alphabetic character a..z A..Z
Function IsAlpha( const c: char ): boolean;
// Returns true if s is only spaces (or empty)
Function IsSpaces( const s: string ): boolean;
// ---------------------- Numeric conversions ---------------------------------------
// Converts a hex string to a longint
// May be upper or lower case
// Does not allow a sign
// Is not forgiving as StrToInt: all characters
// must be valid hex chars.
function HexToInt( s: string ): longint;
// Given a string with a number on the end, increments that
// number by one.
// If there is no number it adds a one.
// If the number is left zero padded then the result is similarly
// padded
Function IncrementNumberedString( StartString: string ): string;
// ---------------------- Pascal String Utilities ---------------------------------------
Function CaseInsensitivePos( const a: string;
const b: string ): longint;
// Looks for occurrences of QuoteChar and inserts a duplicate
Function InsertDuplicateChars( const S: string;
const QuoteChar: char ): string;
// Returns index of SubString in S, case-insensitve
Function FindSubstring( const SubString: string;
const S: string ): integer;
// Returns characters at the front of S that match according
// to a given function... for example, IsDigit, IsNonDigit, IsAlpha
Function MatchLeadingChars(
const S: string;
MatchFunction: TCharMatchFunction ): string;
// Same as MatchLeadingChars, but removes the matching chars from S
Function ExtractLeadingChars(
Var S: string;
MatchFunction: TCharMatchFunction ): string;
// Case insensitive compare
Function StringsSame( const a, b: string ): boolean;
// Quoting
// Note: these functions do not check for existing quotes within
// the string, they only add or delete characters at the end.
// Returns S in single quotes
Function StrQuote( const s: string ): string;
// Returns S without single quotes
Function StrUnQuote( const s: string ): string;
// Returns S in double quotes,
// with any double quotes in S duplicated
Function StrFullDoubleQuote( const s: string ): string;
// Returns S without double quotes
Function StrUnDoubleQuote( const s: string ): string;
// Returns aString enclosed in single quotes
Function StrInSingleQuotes(const aString : String) : String;
// Returns aString enclosed in double quotes
Function StrInDoubleQuotes(const aString : String) : String;
//
// Substitutes given character - placing all occurences of AFind char.
Function SubstituteChar( const S: string; const Find: Char; const Replace: Char ): string;
// Returns the count rightmost chars of S
Function StrRight( const S:string; const count:integer ):string;
// Returns the remainder of S starting at start
Function StrRightFrom( const S:string; const start:integer ):string;
// Returns the count leftmost chars of S
Function StrLeft( const S:string; const count:integer ):string;
// Returns S minus count characters from the right
Function StrLeftWithout( const S:string; const count:integer ):string;
// Returns S with leftCount chars removed from the left and
// rightCount chars removed from the right.
Function StrRemoveEnds( const S:string; const leftCount:integer; const rightCount:integer ):string;
// Produces a string from n padded on the left with 0's
// to width chars
Function StrLeft0Pad( const n: integer; const width: integer ): string;
// Returns true if s starts with start (case insensitive)
Function StrStarts( const start: string; const s: string ): boolean;
// Returns true if s ends with endstr (case insensitive)
Function StrEnds( const endStr: string; const s: string ): boolean;
// Returns first word from S
Function StrFirstWord( const S: String ): string;
// prefixes all occurences of one of the chars in aReceiver with
// anEscape char if the escapeChar itself is found, then it is doubled
Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String;
// Trims punctuation characters from start and end of s
// such as braces, periods, commas.
procedure TrimPunctuation( var s: string );
// Returns true if S contains a URL. MAY MODIFY CONTENTS OF S
function CheckAndEncodeURL( var s: string ): boolean;
// ------------ Seperated value utilities ---------------------
// Returns the next item starting at Index. Spaces are the separator,
// unless the item is quoted with QuoteChar, in which case it looks for
// a closing quote. Occurrences of the QuoteChar in the item itself,
// can be escaped with a duplicate, e.g. "He said ""bok"""
Procedure GetNextQuotedValue(
const S: string;
var Index: longint;
var Value: string;
const QuoteChar: char );
procedure GetNextValue(
const S: String;
var Index: longint;
var Value: string;
const Seperator: char );
// Extract all fields in a String delimited by whitespace (blank or tab).
// use double quotes if you need blanks in the strings
Procedure StrExtractStringsQuoted(Var aResult: TStrings; const aReceiver: String);
// Extract all fields in a String given a set of delimiter characters and
// an optional escape character usable to escape field delimits.
// Example:
// StrExtractStrings('1x2x3\x4', 'x', '\') ->
// returns 4 strings: '1', '', '2' and '3x4'
procedure StrExtractStrings(var aResult : TStrings; const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char);
// Removes and returns the first value in a separated
// value list (removes quotes if found)
Function ExtractNextValue(
var S: string;
const Separator: string ): string;
Function ExtractNextValueNoTrim(
var S: string;
const Separator: string ): string;
// Parses a line of the form
// key = value into it's components
Procedure ParseConfigLine( const S: string;
var KeyName: string;
var KeyValue: string );
// Removes spaces around the separator in the given string
Procedure RemoveSeparatorSpaces( var S: string; const Separator:string );
// ------------- Lists of strings, and strings as lists -----------
// Adds NewValue to S as a separated list
Procedure AddToListString( Var S: string;
const NewValue: string;
const Separator: string );
Function ListToString( List: TStrings;
const Separator: string ): string;
procedure StringToList( S: String;
List: TStrings;
const Separator: string );
// Reverse the given list. It must be set to not sorted
Procedure ReverseList( TheList: TStrings );
// Sort the given list into reverse alphabetical order
//Procedure ReverseSortList( TheList: TStringList );
// Find the given string in the given list, using
// case insensitive searching (and trimming)
// returns -1 if not found
Function FindStringInList( const TheString: string;
TheList:TStrings ):longint;
Procedure MergeStringLists( Dest: TStringList;
AdditionalList: TStringList );
// ---------------------- PCHAR Utilities ---------------------------------------
function StrNPas( const ps: PChar; const Length: integer ): String;
// Returns a - b
Function PCharDiff( const a: PChar; const b: Pchar ): longword;
// trims spaces and carriage returns of the end of Text
procedure TrimWhitespace( Text: PChar );
function TrimChars( const s: string;
chars: TSetOfChars ): string;
// Concatenates a pascal string onto a PCHar string
// Resizes if needed
procedure StrPCat( Var Dest: PChar;
const StringToAdd: string );
// Trim endlines (#10 or #13) off the end of
// the given string.
Procedure TrimEndLines( const S: PChar );
// Allocates enough memory for a copy of s as a PChar
// and copies s to it.
Function StrDupPas( const s: string ): PChar;
// Returns a copy of the first n chars of s
Function StrNDup( const s: PChar; const n: integer ): PChar;
// Returns a copy of the first line starting at lineStart
Function CopyFirstLine( const lineStart: PChar ): PChar;
// Returns next line p points to
Function NextLine( const p: PChar): PChar;
// Concatentate AddText to Text. Reallocate and expand
// Text if necessary. This is a size-safe StrCat
Procedure AddAndResize( Var Text: PChar;
const AddText: PChar );
// Copy Source to Dest. Reallocate and expand
// Dest if necessary. This is a size-safe StrCopy
Procedure StrCopyAndResize( Var Dest: PChar;
const Source: PChar );
// Return "True" or "False"
Function BoolToStr( const b: boolean ): string;
// Return true if param matches the form
// /Flag:value
// dash (-) can be used instead of slash (/)
// colon can be omitted
function MatchValueParam( const Param: string;
const Flag: string;
var Value: string ): boolean;
// Return true if param matches the form
// /Flag
// dash (-) can be used instead of slash (/)
function MatchFlagParam( const Param: string; const Flag: string ): boolean;
// returns true if the String starts with the provided one
// this is case INsensitive
function StrStartsWithIgnoringCase(const aReceiver: String; const aStartString: String): Boolean;
// returns true if the String ends with the provided one
// this is case INsensitive
function StrEndsWithIgnoringCase(const aReceiver: String; const anEndString: String): Boolean;
function StrIsEmptyOrSpaces(const AText: string): boolean;
implementation
Uses
SysUtils
,nvUtilities
;
// ---------------------- Pascal String Utilities ---------------------------------------
Procedure SkipChar( const S: string;
Var index: longint;
const C: Char );
begin
while Index <= Length( S ) do
begin
if S[ Index ] <> C then
break;
inc( Index );
end;
end;
Procedure GetNextQuotedValue(
const S: string;
var Index: longint;
var Value: string;
const QuoteChar: char );
begin
Value := '';
SkipChar( S, Index, ' ' );
if Index > Length( S ) then
exit;
if S[ Index ] <> QuoteChar then
begin
// not quoted, go to next space
while Index <= Length( S ) do
begin
if S[ Index ] = ' ' then
break;
Value := Value + S[ Index ];
inc( Index );
end;
// skip following spaces
SkipChar( S, Index, ' ' );
exit;
end;
// quoted string
inc( Index ); // skip opening quote
while Index <= Length( S ) do
begin
if S[ Index ] = QuoteChar then
begin
inc( index ); // skip closing quote
if Index > Length( S ) then
break; // done
if S[ Index ] <> QuoteChar then
break; // done
// escaped quote e.g "" so we do want it.
end;
Value := Value + S[ Index ];
inc( Index );
end;
SkipChar( S, Index, ' ' );
end;
Function InsertDuplicateChars( const S: string;
const QuoteChar: char ): string;
var
i: integer;
begin
Result := '';
for i := 1 to Length( S ) do
begin
Result := Result + S[ i ];
if S[ i ] = QuoteChar then
Result := Result + QuoteChar; // insert duplicate
end;
end;
Function FindSubstring( const SubString: string;
const S: string ): integer;
begin
Result := Pos( Uppercase( SubString ),
Uppercase( S ) );
end;
Function MatchLeadingChars(
const S: string;
MatchFunction: TCharMatchFunction ): string;
var
i: integer;
TheChar: char;
begin
Result:= '';
i := 1;
while i <= Length( S ) do
begin
TheChar:= S[ i ];
if not MatchFunction( TheChar ) then
// found a non matching char. Stop looking
break;
Result:= Result + TheChar;
inc( i );
end;
end;
Function ExtractLeadingChars(
Var S: string;
MatchFunction: TCharMatchFunction ): string;
begin
Result := MatchLeadingChars( s, MatchFunction );
if Length( Result ) > 0 then
// remove matching section from string
Delete( S, 1, Length( Result ) );
end;
// Hex conversion: sheer extravagance. Conversion from
// a hex digit char to int is done by creating a lookup table
// in advance.
var
MapHexDigitToInt: array[ Chr( 0 ) .. Chr( 255 ) ] of longint;
procedure InitHexDigitMap;
var
c: char;
IntValue: longint;
begin
for c := Chr( 0 ) to Chr( 255 ) do
begin
IntValue := -1;
if ( c >= '0' )
and ( c <= '9' ) then
IntValue := Ord( c ) - Ord( '0' );
if ( Upcase( c ) >= 'A' )
and ( Upcase( c ) <= 'F' ) then
IntValue := 10 + Ord( Upcase( c ) ) - Ord( 'A' );
MapHexDigitToInt[ c ] := IntValue;
end;
end;
function HexDigitToInt( c: char ): longint;
begin
Result := MapHexDigitToInt[ c ];
if Result = -1 then
raise EConvertError.Create( 'Invalid hex char: ' + c );
end;
function HexToInt( s: string ): longint;
var
i: integer;
begin
if Length( s ) = 0 then
raise EConvertError.Create( 'No chars in hex string' );
Result := 0;
for i:= 1 to Length( s ) do
begin
Result := Result shl 4;
inc( Result, HexDigitToInt( s[ i ] ) );
end;
end;
Function StringsSame( const a, b: string ): boolean;
begin
Result:= CompareText( a, b ) = 0;
end;
// Returns S in single quotes
Function StrQuote( const s: string ): string;
begin
Result := StrSingleQuote + s + StrSingleQuote;
end;
// Returns S without double quotes
Function StrUnQuote( const s: string ): string;
begin
Result := S;
if S = '' then
exit;
if Result[ 1 ] = StrSingleQuote then
Delete( Result, 1, 1 );
if Result = '' then
exit;
if Result[ Length( Result ) ] = StrSingleQuote then
Delete( Result, Length( Result ), 1 );
end;
Function StrFullDoubleQuote( const s: string ): string;
begin
Result := StrDoubleQuote
+ InsertDuplicateChars( s, '"' )
+ StrDoubleQuote;
end;
// Returns S without double quotes
Function StrUnDoubleQuote( const s: string ): string;
begin
Result := S;
if S = '' then
exit;
if Result[ 1 ] = StrDoubleQuote then
Delete( Result, 1, 1 );
if Result = '' then
exit;
if Result[ Length( Result ) ] = StrDoubleQuote then
Delete( Result, Length( Result ), 1 );
end;
Function StrInSingleQuotes(const aString : String) : String;
begin
Result := StrSingleQuote + aString + StrSingleQuote;
end;
Function StrInDoubleQuotes(const aString : String) : String;
begin
Result := StrDoubleQuote + aString + StrDoubleQuote;
end;
Function SubstituteChar( const S: string; const Find: Char; const Replace: Char ): string;
Var
i: longint;
Begin
Result:= S;
for i:=1 to length( S ) do
if Result[ i ] = Find then
Result[ i ]:= Replace;
End;
Function StrRight( const S:string; const count:integer ):string;
Begin
if count>=length(s) then
begin
Result:=S;
end
else
begin
Result:=copy( S, length( s )-count+1, count );
end;
end;
Function StrLeft( const S:string; const count:integer ):string;
Begin
if count>=length(s) then
Result:=S
else
Result:=copy( S, 1, count );
end;
// Returns S minus count characters from the right
Function StrLeftWithout( const S:string; const count:integer ):string;
Begin
Result:= copy( S, 1, length( S )-count );
End;
Function StrRemoveEnds( const S:string; const leftCount:integer; const rightCount:integer ):string;
Begin
Result:= S;
Delete( Result, 1, leftCount );
Delete( Result, length( S )-rightCount, rightCount );
End;
Function StrRightFrom( const S:string; const start:integer ):string;
Begin
Result:= copy( S, start, length( S )-start+1 );
end;
Procedure ParseConfigLine( const S: string;
var keyName: string;
var keyValue: string );
Var
line: String;
EqualsPos: longint;
Begin
KeyName:= '';
KeyValue:= '';
line:= trim( S );
EqualsPos:= Pos( '=', line );
if ( EqualsPos>0 ) then
begin
KeyName:= line;
Delete( KeyName, EqualsPos, length( KeyName )-EqualsPos+1 );
KeyName:= Trim( KeyName );
KeyValue:= line;
Delete( KeyValue, 1, EqualsPos );
KeyValue:= Trim( KeyValue );
end;
end;
Function ExtractNextValueNoTrim( var S: string;
const Separator: string ): string;
Var
SeparatorPos: integer;
Begin
SeparatorPos := Pos( Separator, S );
if SeparatorPos > 0 then
begin
Result := Copy( S, 1, SeparatorPos-1 );
Delete( S, 1, SeparatorPos + length( Separator ) - 1 );
end
else
begin
Result := S;
S := '';
end;
end;
Function ExtractNextValue( var S: string;
const Separator: string ): string;
begin
Result := ExtractNextValueNoTrim( S, Separator );
Result := trim( Result );
// Remove quotes if present
if Result <> '' then
if Result[ 1 ] = StrDoubleQuote then
Delete( Result, 1, 1 );
if Result <> '' then
if Result[ length( Result ) ] = StrDoubleQuote then
Delete( Result, length( Result ), 1 );
end;
procedure GetNextValue( const S: String;
Var Index: longint;
Var Value: String;
const Seperator: Char );
var
NextSeperatorPosition: longint;
StringLen: longint;
begin
Value := '';
StringLen := Length( S );
if Index > StringLen then
exit;
NextSeperatorPosition := Index;
while NextSeperatorPosition < StringLen do
begin
if S[ NextSeperatorPosition ] = Seperator then
break;
inc( NextSeperatorPosition );
end;
if NextSeperatorPosition < StringLen then
begin
Value := Copy( S,
Index,
NextSeperatorPosition - Index );
Index := NextSeperatorPosition + 1;
end
else
begin
Value := Copy( S,
Index,
StringLen - Index + 1 );
Index := StringLen + 1;
end;
TrimRight( Value );
end;
Procedure StrExtractStringsQuoted(Var aResult: TStrings; const aReceiver: String);
Var
tmpState : (WHITESPACE, INSIDE, START_QUOTE, INSIDE_QUOTED, INSIDE_QUOTED_START_QUOTE);
tmpCurrentParsePosition : Integer;
tmpCurrentChar : Char;
tmpPart : String;
Begin
if (length(aReceiver) < 1) then exit;
tmpState := WHITESPACE;
tmpPart := '';
tmpCurrentParsePosition := 1;
for tmpCurrentParsePosition:=1 to length(aReceiver) do
begin
tmpCurrentChar := aReceiver[tmpCurrentParsePosition];
Case tmpCurrentChar of
' ', StrTAB :
begin
Case tmpState of
WHITESPACE :
begin
// nothing
end;
INSIDE :
begin
aResult.add(tmpPart);
tmpPart := '';
tmpState := WHITESPACE;
end;
INSIDE_QUOTED :
begin
tmpPart := tmpPart + tmpCurrentChar;
end;
START_QUOTE :
begin
tmpPart := tmpPart + tmpCurrentChar;
tmpState := INSIDE_QUOTED;
end;
INSIDE_QUOTED_START_QUOTE :
begin
aResult.add(tmpPart);
tmpPart := '';
tmpState := WHITESPACE;
end;
end;
end;
StrDoubleQuote :
begin
Case tmpState of
WHITESPACE :
begin
tmpState := START_QUOTE;
end;
INSIDE :
begin
aResult.add(tmpPart);
tmpPart := '';
tmpState := START_QUOTE;
end;
INSIDE_QUOTED :
begin
tmpState := INSIDE_QUOTED_START_QUOTE;
end;
START_QUOTE :
begin
tmpState := INSIDE_QUOTED_START_QUOTE;
end;
INSIDE_QUOTED_START_QUOTE :
begin
tmpPart := tmpPart + tmpCurrentChar;
tmpState := INSIDE_QUOTED;
end;
end;
end;
else
begin
Case tmpState of
WHITESPACE :
begin
tmpPart := tmpPart + tmpCurrentChar;
tmpState := INSIDE;
end;
INSIDE, INSIDE_QUOTED :
begin
tmpPart := tmpPart + tmpCurrentChar;
end;
START_QUOTE :
begin
tmpPart := tmpPart + tmpCurrentChar;
tmpState := INSIDE_QUOTED;
end;
INSIDE_QUOTED_START_QUOTE :
begin
aResult.add(tmpPart);
tmpPart := tmpCurrentChar;
tmpState := INSIDE;
end;
end;
end;
end;
end;
Case tmpState of
WHITESPACE, START_QUOTE : {nothing to do};
INSIDE, INSIDE_QUOTED, INSIDE_QUOTED_START_QUOTE :
begin
aResult.add(tmpPart);
end;
end;
end;
Procedure PrivateStrExtractStrings( Var aResult: TStrings;
const aReceiver: String;
const aSetOfChars: TSetOfChars;
const anEscapeChar: char;
const anIgnoreEmptyFlag : boolean);
Var
i : Integer;
tmpChar,tmpNextChar : Char;
tmpPart: String;
Begin
if (length(aReceiver) < 1) then exit;
tmpPart := '';
i := 1;
while i <= length(aReceiver) do
begin
tmpChar := aReceiver[i];
if i < length(aReceiver) then
tmpNextChar := aReceiver[i+1]
else
tmpNextChar := #0;
if (tmpChar = anEscapeChar) and (tmpNextChar = anEscapeChar) then
begin
tmpPart := tmpPart + anEscapeChar;
i := i + 2;
end
else
if (tmpChar = anEscapeChar) and (tmpNextChar in aSetOfChars) then
begin
tmpPart := tmpPart + tmpNextChar;
i := i + 2;
end
else
begin
if (tmpChar in aSetOfChars) then
begin
if (NOT anIgnoreEmptyFlag) OR ('' <> tmpPart) then
aResult.add(tmpPart);
tmpPart := '';
i := i + 1;
end
else
begin
tmpPart := tmpPart + tmpChar;
i := i + 1;
end;
end; { if/else }
end;
if (NOT anIgnoreEmptyFlag) OR ('' <> tmpPart) then
begin
aResult.add(tmpPart);
end;
end;
procedure StrExtractStrings(Var aResult: TStrings; Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char);
begin
PrivateStrExtractStrings(aResult, aReceiver, aSetOfChars, anEscapeChar, false);
end;
Function IsDigit( const c: char ): boolean;
Begin
Result:=( c>='0' ) and ( c<='9' );
End;
Function IsNonDigit( const c: char ): boolean;
Begin
Result:=( c<'0' ) or ( c>'9' );
End;
Function IsAlpha( const c: char ): boolean;
var
UppercaseC: char;
Begin
UppercaseC := UpCase( c );
Result := ( UppercaseC >= 'A' ) and ( UppercaseC <= 'Z' );
end;
// Returns true if s is only spaces (or empty)
Function IsSpaces( const s: string ): boolean;
var
i: longint;
Begin
for i := 1 to length( s ) do
begin
if s[ i ] <> ' ' then
begin
result := false;
exit;
end;
end;
result := true;
end;
Function StrLeft0Pad( const n: integer; const width: integer ): string;
Begin
Result:= IntToStr( n );
while length( Result )<width do
Result:= '0' +Result;
End;
// Returns true if s starts with start
Function StrStarts( const start: string; const s: string ): boolean;
Var
i: integer;
Begin
Result:= false;
if length( start ) > length( s ) then
exit;
for i:= 1 to length( start ) do
if UpCase( s[ i ] ) <> UpCase( start[ i ] ) then
exit;
Result:= true;
End;
// Returns true if s ends with endstr (case insensitive)
Function StrEnds( const endStr: string; const s: string ): boolean;
Var
i, j: integer;
Begin
Result:= false;
if Length( s ) < length( endStr ) then
exit;
j:= Length( s );
for i:= length( endstr ) downto 1 do
begin
if UpCase( s[ j ] ) <> UpCase( endStr[ i ] ) then
exit;
dec( j );
end;
Result:= true;
End;
Procedure RemoveSeparatorSpaces( var S: string;
const Separator:string );
Var
SeparatorPos:integer;
NewString: string;
Begin
NewString := '';
while S <> '' do
begin
SeparatorPos := pos( Separator, S );
if SeparatorPos > 0 then
begin
NewString := NewString
+ trim( copy( S, 1, SeparatorPos - 1 ) )
+ Separator;
Delete( S, 1, SeparatorPos );
end
else
begin
NewString := NewString + trim( S );
S := '';
end;
end;
S := NewString;
End;
Procedure AddToListString( Var S: string;
const NewValue: string;
const Separator: string );
Begin
if trim( S )<>'' then
S:=S+Separator;
S:=S+NewValue;
End;
Function ListToString( List: TStrings;
const Separator: string ): string;
Var
i: longint;
Begin
Result:= '';
for i:= 0 to List.Count - 1 do
AddToListString( Result, List[ i ], Separator );
End;
procedure StringToList( S: String;
List: TStrings;
const Separator: string );
var
Item: string;
begin
List.Clear;
while S <> '' do
begin
Item:= ExtractNextValue( S, Separator );
List.Add( Item );
end;
end;
Function StrFirstWord( const S: String ): string;
Var
SpacePos: longint;
temp: string;
Begin
temp:= trimleft( S );
SpacePos:= pos( ' ', temp );
if SpacePos>0 then
Result:= Copy( temp, 1, SpacePos-1 )
else
Result:= temp;
End;
Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String;
Var
i : Integer;
tmpChar : Char;
Begin
Result := '';
for i := 1 To length(aReceiver) do
begin
tmpChar := aReceiver[i];
if (tmpChar = anEscapeChar) or (tmpChar IN aSetOfChars) then
result := result + anEscapeChar + tmpChar
else
result := result + tmpChar;
end;
end;
const
StartPunctuationChars: set of char =
[ '(', '[', '{', '<', '''', '"' ];
EndPunctuationChars: set of char =
[ ')', ']', '}', '>', '''', '"', '.', ',', ':', ';', '!', '?' ];
procedure TrimPunctuation( var s: string );
var
ChangesMade: boolean;
c: Char;
begin
while Length(s) > 0 do
begin
ChangesMade := false;
c := s[1];
if c in StartPunctuationChars then
begin
ChangesMade := true;
Delete(s, 1, 1);
end;
if Length(s) = 0 then
exit;
c := s[Length(s)];
if c in EndPunctuationChars then
begin
ChangesMade := true;
Delete(s, Length(s), 1);
end;
if not ChangesMade then
exit; // done
end;
end;
function IsDomainName( const s: string; StartingAt: longint ): boolean;
var
DotPos: longint;
t: string;
begin
Result := false;
t := Copy(s, StartingAt+1, Length(s));
// must be a dot in the domain...
DotPos := pos('.', t);
if DotPos = 0 then
// nope
exit;
// must be some text between start and dot,
// and between dot and end
// ie. a.b not .b or a.
if DotPos = Length(t) then
// no b;
exit;
Result := true;
end;
function IsEmailAddress( const s: string ): boolean;
var
AtPos: longint;
SecondAtPos: longint;
begin
result := false;
// must be a @...
AtPos := pos('@', s);
if AtPos = 0 then
// no @
exit;
if AtPos = 1 then
// can't be the first char though
exit;
// there is? There must be only one though...
SecondAtPos := LastDelimiter('@', s);
if (SecondAtPos <> AtPos) then
// there's a second @
exit;
Result := IsDomainName( s, AtPos + 1 );
end;
function CheckAndEncodeURL( var s: string ): boolean;
// simple userfriendly routine
function StartsWith(const s:string; const text: string): boolean;
begin
Result := pos(text, s) = 1;
end;
begin
if StartsWith(s, 'www.') then
begin
if not IsDomainName( s, 4 ) then
exit;
Insert('http://', s, 1);
Result := true;
exit;
end;
if StartsWith(s, 'ftp.') then
begin
if not IsDomainName( s, 4 ) then
exit;
Insert('ftp://', s, 1);
Result := true;
exit;
end;
if StartsWith(s, 'http://' )
or StartsWith(s, 'https://' )
or StartsWith(s, 'ftp://' )
or StartsWith(s, 'mailto:' )
or StartsWith(s, 'news:' ) then
begin
Result := true;
exit;
end;
if IsEmailAddress( s ) then
begin
Insert('mailto:', s, 1);
Result := true;
exit;
end;
Result := false;
end;
Function IncrementNumberedString( StartString: string ): string;
Var
Number: string;
NewNumber: string;
i: integer;
begin
// Extract any digits at the end of the string
i:= length( StartString );
Number:= '';
while i>0 do
begin
if isDigit( StartString[i] ) then
begin
Number:= StartString[i] + Number;
i:= i - 1;
end
else
break;
end;
if Number<>'' then
begin
// Found a numeric bit to play with
// Copy the first part
Result:= StrLeftWithout( StartString, length( Number ) );
NewNumber:= StrLeft0Pad( StrToInt( Number ) + 1,
length( Number ) );
Result:= Result + NewNumber;
end
else
// No build number, add a 1
Result:= StartString + '1';
end;
Procedure ReverseList( TheList:TStrings );
Var
TempList: TStringList;
i: integer;
Begin
TempList:= TStringList.Create;
for i:=TheList.count-1 downto 0 do
begin
TempList.AddObject( TheList.Strings[i],
TheList.Objects[i] );
end;
TheList.Assign( TempList );
TempList.Destroy;
end;
Function FindStringInList( const TheString: string;
TheList:TStrings ): longint;
Var
i: longint;
Begin
for i:=0 to TheList.count-1 do
begin
if StringsSame( TheString, TheList[ i ] ) then
begin
// found
Result:=i;
exit;
end;
end;
Result:=-1;
End;
Procedure MergeStringLists( Dest: TStringList;
AdditionalList: TStringList );
var
i: integer;
s: string;
begin
for i:= 0 to AdditionalList.Count - 1 do
begin
s:= AdditionalList[ i ];
if FindStringInList( s, Dest ) = -1 then
Dest.AddObject( s, AdditionalList.Objects[ i ] );
end;
end;
// ---------------------- PCHAR Utilities ---------------------------------------
function StrNPas( const Ps: PChar; const Length: integer ): String;
var
i: integer;
begin
Result:= '';
i:= 0;
while ( Ps[ i ] <> #0 ) and ( i < Length ) do
begin
Result:= Result + Ps[ i ];
inc( i );
end;
end;
Function PCharDiff( const a: PChar; const b: Pchar ): longword;
begin
Result:= longword( a ) - longword( b );
end;
Procedure CheckPCharSize( Var Text: PChar;
const NeededSize: longword );
var
temp: PChar;
NewBufferSize: longword;
begin
if ( NeededSize + 1 ) // + 1 to allow for null terminator
> StrBufSize( Text ) then
begin
// allocate new buffer, double the size...
NewBufferSize:= StrBufSize( Text ) * 2;
// or if that's not enough...
if NewBufferSize < ( NeededSize + 1 ) then
// double what we are going to need
NewBufferSize:= NeededSize * 2;
temp:= StrAlloc( NewBufferSize );
// copy string to new buffer
StrCopy( temp, Text );
StrDispose( Text );
Text:= temp;
end;
end;
Procedure AddAndResize( Var Text: PChar; const AddText: PChar );
var
s: string;
s1, s2: string;
begin
//CheckPCharSize( Text,
// strlen( Text )
// + strlen( AddText ) );
//StrCat( Text, AddText );
s1 := Text;
s2 := AddText;
s := s1 + s2;
StrDispose(Text);
Text := StrAlloc(length(s) + 1);
StrPCopy(Text, s);
end;
Procedure StrCopyAndResize( Var Dest: PChar;
const Source: PChar );
begin
CheckPCharSize( Dest, StrLen( Source ) );
StrCopy( Dest, Source );
end;
// trims spaces and carriage returns of the end of Text
procedure TrimWhitespace( Text: PChar );
var
P: PChar;
IsWhitespace: boolean;
TheChar: Char;
begin
P:= Text + StrLen( Text );
while P > Text do
begin
dec( P );
TheChar:= P^;
IsWhitespace:= TheChar in [ ' ', #13, #10, #9 ];
if not IsWhiteSpace then
// done
break;
P[ 0 ]:= #0; // Do no use P^ :=
end;
end;
function TrimChars( const s: string;
chars: TSetOfChars ): string;
var
i: longint;
j: longint;
begin
i := 1;
while i < Length( s ) do
if s[ i ] in chars then
inc( i )
else
break;
j := Length( s );
while j > i do
if s[ j ] in chars then
dec( j )
else
break;
result := Copy( s, i, j - i + 1 );
end;
procedure StrPCat( Var Dest: PChar;
const StringToAdd: string );
var
Index: longint;
DestP: PChar;
begin
CheckPCharSize( Dest,
StrLen( Dest )
+ longword( Length( StringToAdd ) ) );
DestP:= Dest + StrLen( Dest );
for Index:= 1 to Length( StringToAdd ) do
begin
DestP[ 0 ]:= StringToAdd[ Index ]; // do not use DestP^ :=
inc( DestP );
end;
DestP[ 0 ]:= #0; // Do not use DestP^ := #0; Under Sibyl at least, this writes *** 2 NULL BYTES!!! ***
end;
Procedure TrimEndLines( const S: PChar );
var
StringIndex: integer;
begin
StringIndex:= strlen( S );
while StringIndex > 0 do
begin
dec( StringIndex );
if S[ StringIndex ] in [ #10, #13 ] then
begin
S[ StringIndex ]:= #0
end
else
break;
end;
end;
Function StrDupPas( const s: string ): PChar;
Begin
Result:=StrAlloc( length( s )+1 );
StrPCopy( Result, S );
// Result^:=s;
End;
// Returns a copy of the first n chars of s
Function StrNDup( const s: PChar; const n: integer ): PChar;
Begin
Result:= StrAlloc( n+1 );
Result[ n ]:= '6';
StrLCopy( Result, s, n );
End;
// Returns a copy of the first line starting at lineStart
Function CopyFirstLine( const lineStart: PChar ): PChar;
Var
lineEnd: PChar;
lineLength: integer;
Begin
// look for an end of line
lineEnd:= strpos( lineStart, EndLine );
if lineEnd <> nil then
begin
// found, line length is difference between line end position and start of line
lineLength:= longword( lineEnd )-longword( lineStart ); // ugly but how else can it be done?
Result:= StrNDup( lineStart, lineLength );
exit;
end;
// no eol found, return copy of remainder of string
Result:= StrNew( lineStart );
end;
// Returns next line p points to
Function NextLine( const p: PChar): PChar;
Var
lineEnd: PChar;
Begin
// look for an end of line
lineEnd:=strpos( p, EndLine );
if lineEnd<>nil then
begin
// Advance the linestart over the eol
Result:=lineEnd+length( EndLine );
exit;
end;
// no eol found, return pointer to null term
Result:=p+strlen( p );
end;
Function CaseInsensitivePos( const a: string; const b: string ): longint;
begin
Result := Pos( UpperCase( a ), Uppercase( b ) );
end;
Function BoolToStr( const b: boolean ): string;
begin
if b then
Result := 'True'
else
Result := 'False';
end;
// Return true if param matches the form
// /Flag:value
// dash (-) can be used instead of slash (/)
// colon can be omitted
function MatchValueParam( const Param: string;
const Flag: string;
var Value: string ): boolean;
begin
Result := false;
if Param = '' then
exit;
if ( Param[ 1 ] <> '/' )
and ( Param[ 1 ] <> '-' ) then
exit;
if not StringsSame( Copy( Param, 2, Length( Flag ) ),
Flag ) then
exit;
Result := true;
Value := StrRightFrom( Param, 2 + Length( Flag ) );
if Value <> '' then
if Value[ 1 ] = ':' then
Delete( Value, 1, 1 );
end;
// Return true if param matches the form
// /Flag
// dash (-) can be used instead of slash (/)
function MatchFlagParam( const Param: string;
const Flag: string ): boolean;
begin
Result := false;
if Param = '' then
exit;
if ( Param[ 1 ] <> '/' )
and ( Param[ 1 ] <> '-' ) then
exit;
Result := StringsSame( StrRightFrom( Param, 2 ),
Flag );
end;
function StrStartsWithIgnoringCase(const aReceiver: String; const aStartString: String): Boolean;
var
tmpStringPos : integer;
tmpStartStringLength : integer;
begin
tmpStartStringLength := Length(aStartString);
if Length(aReceiver) < tmpStartStringLength then
begin
result := false;
exit;
end;
for tmpStringPos := 1 to tmpStartStringLength do
begin
if UpCase(aReceiver[tmpStringPos]) <> UpCase(aStartString[tmpStringPos]) then
begin
result := false;
exit;
end;
end;
result := true;
end;
Function StrEndsWithIgnoringCase(const aReceiver: String; const anEndString: String): Boolean;
Var
tmpStringPos : Longint;
tmpMatchPos : Longint;
Begin
tmpStringPos := length(aReceiver);
tmpMatchPos := length(anEndString);
if tmpMatchPos > tmpStringPos then
begin
result := false;
exit;
end;
while tmpMatchPos > 0 do
begin
if upcase(aReceiver[tmpStringPos]) <> upcase(anEndString[tmpMatchPos]) then
begin
result := false;
exit;
end;
dec(tmpMatchPos);
dec(tmpStringPos);
end;
result := true;
end;
function StrIsEmptyOrSpaces(const AText: string): boolean;
begin
Result := Trim(AText) = '';
end;
{ TSerializableStringList }
constructor TSerializableStringList.Create;
begin
LogEvent(LogObjConstDest, 'TSerializableStringList createdestroy');
inherited Create;
stringList := TStringList.Create;
end;
destructor TSerializableStringList.Destroy;
begin
LogEvent(LogObjConstDest, 'TSerializableStringList destroy');
stringList.Free;
inherited Destroy;
end;
function TSerializableStringList.getCount: LongInt;
begin
Result := stringlist.Count;
end;
function TSerializableStringList.get(const anIndex: LongInt): String;
begin
Result := stringList[anIndex];
end;
function TSerializableStringList.getSerializedString: String;
var
i: Integer;
begin
Result := '';
for i := 0 to stringList.count-1 do
begin
if (i > 0) then result := result + '&';
Result := Result + StrEscapeAllCharsBy(stringList[i], ['&'], '\');
end;
end;
procedure TSerializableStringList.add(const aString: String);
begin
stringList.add(aString);
end;
procedure TSerializableStringList.readValuesFromSerializedString(const aSerializedString: String);
begin
if length(aSerializedString) < 1 then
exit;
LogEvent(LogObjConstDest, 'readValuesFromSerializedString');
stringList.Clear;
LogEvent(LogObjConstDest, 'readValuesFromSerializedString clear done');
StrExtractStrings(stringList, aSerializedString, ['&'], '\');
end;
initialization
InitHexDigitMap;
End.
|