-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Strings.pas
1595 lines (1374 loc) · 45.3 KB
/
JPL.Strings.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
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
unit JPL.Strings;
{
Jacek Pazera
https://www.pazera-software.com
https://github.com/jackdp
Based on my old unit from 2000 for Borland Pascal 7.0
}
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
SysUtils, Types, JPL.Bytes;
const
CR = #13;
CRCR = #13#13;
LF = #10;
LFLF = #10#10;
CRLF = #13#10;
TAB = #9;
ENDL = sLineBreak;
ENDL2 = ENDL + ENDL;
ENDL3 = ENDL2 + ENDL;
ENDL4 = ENDL3 + ENDL;
DEG = '°';
type
TSpecialChars = record
const Tab = #9;
const Euro = '€';
const Copyright = '©';
const Reg = '®';
const Paragraph = '¶';
const Section = '§';
const Degree = '°';
const Sup2 = '²';
const Sup3 = '³';
const Integral = '∫';
const Micro = 'µ';
const PlusMinus = '±';
const Times = '×';
const Divide = '÷';
const OmegaBig = 'Ω';
const AlphaSmall = 'α';
const BetaSmall = 'β';
const GammaSmall = 'γ';
const DeltaSmall = 'δ';
const DeltaBig = 'Δ';
const PiSmall = 'π';
const PiBig = 'Π';
const SigmaBig = 'Σ';
const Bullet = '•';
const DashLong = '–';
const Trademark = '™';
end;
{$IFDEF DELPHI2009_OR_BELOW}
type
TArray<T> = array of T;
{$ENDIF}
function Rbs(const Directory: string): string; // Removes path delimiter from the end of directory name.
function Qs(const s: string): string; // Adds " at the beginning and end of the input string. See also EnsureBounds, AddBounds
function Capitalize(const s: string): string;
function FixFileName(const ShortFileName: string; ReplaceInvalidCharsWith: string = '_'; ReplaceQuotationMarks: Boolean = True): string;
function IsValidShortFileName(const ShortFileName: string): Boolean;
function IsValidLongFileName(const LongFileName: string): Boolean;
function FixFileNameSlashes(const FileName: string): string; deprecated {$IFDEF DCC}{$IF CompilerVersion > 19}'Use FixPathDelimiters instead'{$IFEND}{$ENDIF};
function FixPathDelimiters(const FileName: string): string;
function PadString(Text: string; i: integer; znak: Char = ' '): string;
function Pad(Text: string; Len: integer; PaddingChar: Char = ' '): string; overload;
function Pad(const x: integer; Len: integer; PaddingChar: Char = '0'): string; overload;
function Pad(const x: Int64; Len: integer; PaddingChar: Char = '0'): string; overload;
function PadRight(Text: string; Len: integer; PaddingChar: Char = ' '): string; overload;
function PadRight(const x: integer; Len: integer; PaddingChar: Char = ' '): string; overload;
function PadRight(const x: Int64; Len: integer; PaddingChar: Char = ' '): string; overload;
function UnquoteStr(s: string; bDoubleQuote: Boolean = True): string;
function IntToStrEx(const x: int64; c: Char = ' '): string; overload;
function IntToStrEx(const x: integer; c: Char = ' '): string; overload;
function IntToStrEx(const x: UInt64; c: Char = ' '): string; overload;
function GetLastCharIndex(const s: string; c: Char): integer;
function GetLastBIndex(const s: string): integer; deprecated {$IFDEF DCC}{$IF CompilerVersion > 19}'Use GetLastBackslashIndex instead'{$IFEND}{$ENDIF};
function GetLastBackslashIndex(const s: string): integer;
function DelFirstCharInString(s: string; ToDelete: Char): string;
function DelLastCharInString(s: string; ToDelete: Char): string;
function RemoveNum(const s: string): string;
//function AddSlashes(Text: string): string;
function RemoveSlashes(const Text: string): string;
function RemoveSpaces(const s: string): string;
function ReplaceSpecialChars(s: string; sc: Char = '_'): string;
function RemoveAll(const Text, ToRemove: string; IgnoreCase: Boolean = False): string; overload;
function RemoveAll(const Text: string; const StringsToRemove: array of string; IgnoreCase: Boolean = False): string; overload;
function RemoveNonLetters(s: string): string;
function ReplaceAll(const SrcStr, OldStr, NewStr: string; IgnoreCase: Boolean = False): string;
function ReplaceFirst(const SrcStr, OldStr, NewStr: string; IgnoreCase: Boolean = False): string;
function ReplaceDecimalSeparator(const FloatStr: string; NewSeparator: string = '.'): string;
function RemovePolishChars(s: string): string;
{$IFDEF DCC}
function IsBigLetterPL(c: Char): Boolean;
function IsSmallLetterPL(c: Char): Boolean;
function IsLetterPL(c: Char): Boolean;
{$ENDIF}
function IsBigLetter(const c: Char): Boolean;
function IsSmallLetter(const c: Char): Boolean;
function IsLetter(const c: Char): Boolean;
function IsNumber(const c: Char): Boolean;
function LastCharIsNumber(const s: string): Boolean;
function DistinctChars(s: string; IgnoreCase: Boolean = True): Boolean;
function MakeDistinctChars(s: string): string;
function CharCount(c: Char; s: string): integer;
function ReverseStr(const s: string): string;
function FillStrToLen(s: string; Len: integer; FillValue: Char = ' '): string;
function AnsiUpCase(zn: Char; Default: Char = #0): Char;
function AnsiLowCase(zn: Char; Default: Char = #0): Char;
function RemoveChars(const SrcStr, CharsToRemove: string; IgnoreCase: Boolean = False): string; overload;
function RemoveChars(const SrcStr: string; Chars: array of Char; IgnoreCase: Boolean = False): string; overload;
function LeaveOnlyChars(const SrcStr, CharsToLeave: string): string;
function RemoveNonDecimals(const s: string): string;
function CutStrBefore(s, CutBeforeText: string; IgnoreCase: Boolean = False): string;
function CutStrAfter(s, CutAfterText: string; IgnoreCase: Boolean = False; IncludeSearchText: Boolean = True): string;
function GetFileExt(fName: string; bRemoveFirstDot: Boolean = True): string;
function HtmlStringToStr(HTMLStr: string; IgnoreCase: Boolean = False): string; deprecated {$IFDEF DCC}{$IF CompilerVersion > 19}'Use ReplaceHtmlEntities instead'{$IFEND}{$ENDIF};
function ReplaceHtmlEntities(const AStr: string; IgnoreCase: Boolean = False): string;
function GetHref(const InStr: string): string;
function GetAnchorText(const InStr: string; bReplaceHtmlEntities: Boolean = True): string;
function GetFirstDigitIndex(const s: string): integer;
function GetFirstNonDigitIndex(const s: string): integer;
function MyDir(bExcludeTrailingPathDelim: Boolean = True): string; // Returns the ParamStr(0) directory
function StrRemove(const s, StringToRemove: string; IgnoreCase: Boolean = False): string;
function GetFileSizeString(const FileSize: Int64; BytesStr: string = ' bytes'): string;
function TrimUp(s: string): string;
function InsertNumSep(NumStr: string; Separator: string = ' '; NumBlockSize: integer = 3; MaxInsertions: integer = 255): string;
function CopyString(const s: string; Copies: integer = 2): string;
//procedure StrToList(LineToParse: string; var List: TStringList; Separator: string = ',');
{$IFDEF DELPHI2009_OR_BELOW}
procedure SplitStrToArray(s: string; var Arr: TStringDynArray; const EndLineStr: string = sLineBreak);
{$ELSE}
procedure SplitStrToArray(s: string; var Arr: TArray<string>; const EndLineStr: string = sLineBreak);
{$ENDIF}
// The SplitStrToArrayEx procedure uses the initial allocation of the size of the array and incrementing its size by the ArrDeltaSize value
// to avoid increasing the size of the array repeatedly by 1.
procedure SplitStrToArrayEx(s: string; var Arr: TStringDynArray; const EndLineStr: string = sLineBreak; ArrDeltaSize: WORD = 100);
function RemoveEmptyStrings(var Arr: TStringDynArray): integer; // Returns the number of removed empty strings
function SplitStr(const InStr: string; out LeftStr, RightStr: string; const Separator: string): Boolean; overload;
function SplitStr(const InStr: string; out LeftInt, RightInt: integer; const Separator: string): Boolean; overload;
// SplitText copied from JPL.CmdLineParser.pas
function SplitText(Text: string; MaxLen, Padding: integer; PadFirstLine: Boolean = False; PaddingChar: Char = ' '; ENDL: string = #13#10): string;
function TrimBounds(s: string; LeftBound, RightBound: string): string;
function IsBoundedWith(const s: string; LeftBound, RightBound: string; IgnoreCase: Boolean = False): Boolean;
function AddBounds(const s: string; LeftBound, RightBound: Char): string; overload;
function AddBounds(const s: string; LeftBound, RightBound: string): string; overload;
function AddBounds(const s: string; StringToBoundSeparator: string = ' '; BoundChar: Char = '-'; BoundLen: Integer = 16): string; overload;
function EnsureBounds(const s: string; LeftBound, RightBound: string): string;
function EnsureLeftBound(const s, BoundStr: string): string;
function EnsureRightBound(const s, BoundStr: string): string;
function GetRandomHexStr(Bytes: integer = 4; ByteSeparator: string = ''; bLowerCase: Boolean = False): string;
function GetRandomIntStr(Len: integer = 10): string;
// Case sensitive Pos
function PosCS(const substr, s: string; bCaseSensitive: Boolean = False): integer;
function TrimFromCharPosToEnd(const s: string; const AChar: Char): string;
function TrimFromStrPosToEnd(const Src, AStr: string): string;
function TrimFromEnd(const s: string; const StringToCut: string): string;
function TrimFromStart(const s: string; const StringToCut: string): string;
function TrimENDL(const s: string): string; // removes trailing sLineBreak (ENDL)
function TrimExtDot(const FileExtension: string): string;
function AddFileNameSuffix(const FileName, Suffix: string): string;
function AddFileNamePrefix(const FileName, Prefix: string): string;
function TrimFileExt(const FileName: string): string;
function BaseFileName(const FileName: string): string; // returns file name without path and extension
procedure SplitFileName(fName: string; out Dir, BaseFileName, Ext: string; bIncludePathDelimiter: Boolean = True; bRemoveDotFromExt: Boolean = False);
function PathIsAbsolute(const FileName: string): Boolean;
function GetDecimalSeparator: Char;
function SaveStringToFile(const FileName, Content: string; Encoding: TEncoding; const WriteBOM: Boolean = True):Boolean; overload;
function SaveStringToFile(const FileName, Content: string): Boolean; overload;
function LoadStringFromFile(const FileName: string; var s: string; Encoding: TEncoding; ForceEncoding: Boolean = False): Boolean; overload;
function LoadStringFromFile(const FileName: string; var s: string): Boolean; overload;
function StrInArray(const s: string; const Arr: array of string; const IgnoreCase: Boolean): Boolean;
function StringArrayToStr(Arr: TStringDynArray; ValueSeparator: string = ', '): string;
implementation
function StrInArray(const s: string; const Arr: array of string; const IgnoreCase: Boolean): Boolean;
var
i: integer;
us: string;
begin
Result := False;
if IgnoreCase then us := UpperCase(s);
for i := 0 to High(Arr) do
begin
if IgnoreCase then Result := UpperCase(Arr[i]) = us
else Result := Arr[i] = s;
if Result then Break;
end;
end;
function StringArrayToStr(Arr: TStringDynArray; ValueSeparator: string = ', '): string;
var
i: integer;
begin
Result := '';
if Length(Arr) = 0 then Exit;
for i := 0 to Length(Arr) - 1 do
Result := Result + Arr[i] + ValueSeparator;
Result := TrimFromEnd(Result, ValueSeparator);
end;
{$IFDEF FPC}
function SaveStringToFile(const FileName, Content: string; Encoding: TEncoding; const WriteBOM: Boolean = True): Boolean; overload;
var
BOM: TBytes = nil;
ByteContent: TBytes = nil;
begin
SetLength(BOM, 0);
if WriteBOM then BOM := Encoding.GetPreamble;
SetLength(ByteContent, 0);
{$IFDEF FPC320_OR_ABOVE}
if Length(BOM) > 0 then ConcatTBytes(BOM, Encoding.GetAnsiBytes(Content), ByteContent)
else ByteContent := Encoding.GetAnsiBytes(Content);
{$ELSE}
if Length(BOM) > 0 then ConcatTBytes(BOM, Encoding.GetBytes(UnicodeString(Content)), ByteContent)
else ByteContent := Encoding.GetBytes(UnicodeString(Content));
{$ENDIF}
Result := SaveBytesToFile(FileName, ByteContent);
end;
{$ELSE}
function SaveStringToFile(const FileName, Content: string; Encoding: TEncoding; const WriteBOM: Boolean = True): Boolean; overload;
var
BOM: TBytes {$IFDEF FPC}= nil{$ENDIF};
ByteContent: TBytes {$IFDEF FPC}= nil{$ENDIF};
begin
SetLength(BOM, 0);
if WriteBOM then BOM := Encoding.GetPreamble;
SetLength(ByteContent, 0);
if Length(BOM) > 0 then ConcatTBytes(BOM, Encoding.GetBytes(Content), ByteContent)
else ByteContent := Encoding.GetBytes(Content);
Result := SaveBytesToFile(FileName, ByteContent);
end;
{$ENDIF}
function SaveStringToFile(const FileName, Content: string): Boolean; overload;
begin
Result := SaveStringToFile(FileName, Content, TEncoding.Default);
end;
{$IFDEF FPC}
function LoadStringFromFile(const FileName: string; var s: string; Encoding: TEncoding;ForceEncoding: Boolean = False): Boolean; overload;
var
Bytes: TBytes = nil;
xBomLen: integer;
UserEncoding: TEncoding;
begin
Result := False;
if not FileExists(FileName) then Exit;
Result := GetFileContentAsBytes(FileName, Bytes);
if Result then
begin
UserEncoding := Encoding;
Encoding := nil;
// http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TEncoding.GetBufferEncoding
// https://www.freepascal.org/docs-html/rtl/sysutils/tencoding.getbufferencoding.html
// GetBufferEncoding detects encoding of Bytes, and assigns detected encoding to the Encoding param.
// So after GetBufferEncoding, the Encoding is NOT = nil
xBomLen := TEncoding.GetBufferEncoding(Bytes, Encoding, TEncoding.Default);
if ForceEncoding then s := UserEncoding.GetAnsiString(Bytes, xBomLen, Length(Bytes) - xBomLen)
else s := Encoding.GetAnsiString(Bytes, xBomLen, Length(Bytes) - xBomLen);
end;
end;
{$ELSE}
function LoadStringFromFile(const FileName: string; var s: string; Encoding: TEncoding; ForceEncoding: Boolean = False): Boolean; overload;
var
Bytes: TBytes;
xBomLen: integer;
UserEncoding: TEncoding;
begin
Result := False;
if not FileExists(FileName) then Exit;
Result := GetFileContentAsBytes(FileName, Bytes);
if Result then
begin
UserEncoding := Encoding;
Encoding := nil;
// http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TEncoding.GetBufferEncoding
// https://www.freepascal.org/docs-html/rtl/sysutils/tencoding.getbufferencoding.html
// GetBufferEncoding detects encoding of Bytes, and assigns detected encoding to the Encoding param.
// So after GetBufferEncoding, the Encoding is NOT = nil
xBomLen := TEncoding.GetBufferEncoding(Bytes, Encoding {$IFDEF DELPHIXE_OR_ABOVE}, TEncoding.Default{$ENDIF});
if ForceEncoding then s := UserEncoding.GetString(Bytes, xBomLen, Length(Bytes) - xBomLen)
else s := Encoding.GetString(Bytes, xBomLen, Length(Bytes) - xBomLen);
end;
end;
{$ENDIF}
function LoadStringFromFile(const FileName: string; var s: string): Boolean; overload;
begin
Result := LoadStringFromFile(FileName, s, TEncoding.Default);
end;
function GetDecimalSeparator: Char;
begin
{$IFDEF FPC}Result := FormatSettings.DecimalSeparator;{$ENDIF}
{$IFDEF DCC}
{$IFDEF DELPHIXE_OR_ABOVE}
Result := FormatSettings.DecimalSeparator;
{$ELSE}
Result := DecimalSeparator;
{$ENDIF}
{$ENDIF}
end;
function PathIsAbsolute(const FileName: string): Boolean;
begin
{$IFDEF MSWINDOWS}
Result := (Length(FileName) >= 3) and CharInSet(FileName[1], ['A'..'Z','a'..'z']) and (FileName[2] = ':') and CharInSet(FileName[3], ['/', '\']);
{$ELSE}
// UNIX / Linux
Result := (Length(FileName) > 0) and (FileName[1] = '/');
{$ENDIF}
end;
function SplitStr(const InStr: string; out LeftStr, RightStr: string; const Separator: string): Boolean;
var
xp: integer;
begin
Result := False;
xp := Pos(Separator, InStr);
if xp <= 0 then Exit;
LeftStr := Copy(InStr, 1, xp - 1);
RightStr := Copy(InStr, xp + Length(Separator), Length(InStr));
Result := True;
end;
function SplitStr(const InStr: string; out LeftInt, RightInt: integer; const Separator: string): Boolean; overload;
var
sLeft, sRight: string;
begin
Result := False;
if not SplitStr(InStr, sLeft, sRight, Separator) then Exit;
if not TryStrToInt(sLeft, LeftInt) then Exit;
if not TryStrToInt(sRight, RightInt) then Exit;
Result := True;
end;
function SplitText(Text: string; MaxLen, Padding: integer; PadFirstLine: Boolean = False; PaddingChar: Char = ' '; ENDL: string = #13#10): string;
const
Delims: TSysCharset = [
' ',
'(', ')', '[', ']', '{', '}', '<', '>',
{'`',} '~', '!', '@', '#', '$', '%', '^', '*', '-', {'_',} '+', '=',
{'''',} '"', '\', '|', ';', ':', ',', '.'
];
var
i, xDelimPos: integer;
s, sr, PaddingStr: string;
A: array of string;
procedure Add(Line: string);
begin
SetLength(A, Length(A) + 1);
A[Length(A) - 1] := Line;
end;
function DelimPos(InStr: string; SearchPosStart: integer): integer;
var
i: integer;
begin
Result := -1;
if (InStr = '') or (SearchPosStart < 1) or (SearchPosStart > Length(InStr)) then Exit;
for i := SearchPosStart downto 1 do
begin
if CharInSet(InStr[i], Delims) then
begin
Result := i;
Break;
end;
end;
end;
begin
if (Length(Text) <= Padding) or (MaxLen = 0) then
begin
Result := Text;
Exit;
end;
SetLength(A, 0);
//if Padding > MaxLen then exit;
if not PadFirstLine then
begin
xDelimPos := DelimPos(Text, MaxLen);
if xDelimPos < 0 then xDelimPos := MaxLen;
s := Copy(Text, 1, xDelimPos);
Add(s);
Text := Copy(Text, xDelimPos + 1, Length(Text));
end;
sr := '';
PaddingStr := StringOfChar(PaddingChar, Padding);
while Text <> '' do
begin
xDelimPos := DelimPos(Text, MaxLen - Padding);
if xDelimPos < 0 then xDelimPos := MaxLen;
s := PaddingStr + Trim( Copy(Text, 1, xDelimPos) );
Add(s);
Text := Copy(Text, xDelimPos + 1, Length(Text));
//Writeln(Text);
end;
for i := 0 to Length(A) - 1 do sr := sr + A[i] + ENDL;
sr := TrimRight(sr);
Result := sr;
end;
function Capitalize(const s: string): string;
var
i: integer;
bNeedUp: Boolean;
AChar: Char;
begin
if Pos(' ', s) = 0 then Exit(s);
bNeedUp := True;
Result := '';
for i := 1 to Length(s) do
begin
AChar := s[i];
if AChar = ' ' then
begin
bNeedUp := True;
Continue;
end;
if bNeedUp then AChar := UpCase(AChar);
Result := Result + AChar;
bNeedUp := False;
end;
end;
procedure SplitFileName(fName: string; out Dir, BaseFileName, Ext: string; bIncludePathDelimiter: Boolean = True; bRemoveDotFromExt: Boolean = False);
begin
Dir := ExtractFileDir(fName);
if bIncludePathDelimiter then Dir := IncludeTrailingPathDelimiter(Dir) else Dir := ExcludeTrailingPathDelimiter(Dir);
BaseFileName := ExtractFileName(fName);
BaseFileName := ChangeFileExt(BaseFileName, '');
Ext := GetFileExt(fName, bRemoveDotFromExt);
end;
function AddFileNameSuffix(const FileName, Suffix: string): string;
var
Dir, ShortName, Ext: string;
begin
if Suffix = '' then Exit(FileName);
SplitFileName(FileName, Dir, ShortName, Ext, False, False);
Result := Dir + PathDelim + ShortName + Suffix + Ext;
end;
function AddFileNamePrefix(const FileName, Prefix: string): string;
var
Dir, ShortName, Ext: string;
begin
if Prefix = '' then Exit(FileName);
SplitFileName(FileName, Dir, ShortName, Ext, False, False);
Result := Dir + PathDelim + Prefix + ShortName + Ext;
end;
function TrimFileExt(const FileName: string): string;
begin
Result := ChangeFileExt(FileName, '');
end;
function BaseFileName(const FileName: string): string;
begin
Result := ExtractFileName(ChangeFileExt(FileName, ''));
end;
function TrimENDL(const s: string): string;
begin
Result := TrimFromEnd(s, ENDL);
end;
function TrimExtDot(const FileExtension: string): string;
begin
Result := TrimFromStart(FileExtension, '.');
end;
function TrimFromStart(const s: string; const StringToCut: string): string;
begin
if Copy(s, 1, Length(StringToCut)) = StringToCut then Result := Copy(s, Length(StringToCut) + 1, Length(s))
else Result := s;
end;
function TrimFromEnd(const s: string; const StringToCut: string): string;
begin
if Copy(s, Length(s) - Length(StringToCut) + 1, Length(StringToCut)) = StringToCut then Result := Copy(s, 1, Length(s) - Length(StringToCut))
else Result := s;
end;
function TrimFromCharPosToEnd(const s: string; const AChar: Char): string;
var
xp: integer;
begin
xp := Pos(AChar, s);
if xp >= 1 then Result := Copy(s, 1, xp - 1)
else Result := s;
end;
function TrimFromStrPosToEnd(const Src, AStr: string): string;
var
xp: integer;
begin
xp := Pos(AStr, Src);
if xp >= 1 then Result := Copy(Src, 1, xp - 1)
else Result := Src;
end;
function PosCS(const substr, s: string; bCaseSensitive: Boolean = False): integer;
begin
if bCaseSensitive then Result := Pos(substr, s)
else Result := Pos(AnsiUpperCase(substr), AnsiUpperCase(s));
end;
function GetRandomHexStr(Bytes: integer = 4; ByteSeparator: string = ''; bLowerCase: Boolean = False): string;
var
i: integer;
bt: Byte;
begin
Result := '';
for i := 1 to Bytes do
begin
bt := Random(255);
Result := Result + IntToHex(bt, 2);
if i < Bytes then Result := Result + ByteSeparator;
end;
if bLowerCase then Result := LowerCase(Result);
end;
function GetRandomIntStr(Len: integer = 10): string;
const
Nums = '0123456789';
var
i, x: integer;
begin
Result := '';
for i := 1 to Len do
begin
x := Random(10);
Result := Result + Nums[x + 1];
end;
end;
function EnsureBounds(const s: string; LeftBound, RightBound: string): string;
var
x: integer;
begin
Result := s;
if Copy(Result, 1, Length(LeftBound)) <> LeftBound then Result := LeftBound + Result;
x := Length(RightBound);
if Copy(Result, Length(Result) - x + 1, x) <> RightBound then Result := Result + RightBound;
end;
function EnsureLeftBound(const s, BoundStr: string): string;
begin
Result := s;
if Copy(Result, 1, Length(BoundStr)) <> BoundStr then Result := BoundStr + Result;
end;
function EnsureRightBound(const s, BoundStr: string): string;
var
x: integer;
begin
Result := s;
x := Length(BoundStr);
if Copy(Result, Length(Result) - x + 1, x) <> BoundStr then Result := Result + BoundStr;
end;
function AddBounds(const s: string; LeftBound, RightBound: Char): string;
begin
Result := LeftBound + s + RightBound;
end;
function AddBounds(const s: string; LeftBound, RightBound: string): string;
begin
Result := LeftBound + s + RightBound;
end;
function AddBounds(const s: string; StringToBoundSeparator: string = ' '; BoundChar: Char = '-'; BoundLen: Integer = 16): string; overload;
var
sb: string;
begin
sb := StringOfChar(BoundChar, BoundLen);
Result := sb + StringToBoundSeparator + s + StringToBoundSeparator + sb;
end;
function IsBoundedWith(const s: string; LeftBound, RightBound: string; IgnoreCase: Boolean = False): Boolean;
var
sLeft, sRight: string;
begin
sLeft := Copy(s, 1, Length(LeftBound));
sRight := Copy(s, Length(s) - Length(RightBound) + 1, Length(RightBound));
if IgnoreCase then
Result := ( UpperCase(sLeft) = UpperCase(LeftBound) ) and ( UpperCase(sRight) = UpperCase(RightBound) )
else
Result := ( sLeft = LeftBound ) and (sRight = RightBound);
end;
function TrimBounds(s: string; LeftBound, RightBound: string): string;
begin
if Copy(s, 1, Length(LeftBound)) = LeftBound then s := Copy(s, 1 + Length(LeftBound), Length(s));
if Copy(s, Length(s) - Length(RightBound) + 1, Length(RightBound)) = RightBound then s := Copy(s, 1, Length(s) - Length(RightBound));
Result := s;
end;
{$IFDEF DELPHI2009_OR_BELOW}
procedure SplitStrToArray(s: string; var Arr: TStringDynArray; const EndLineStr: string = sLineBreak);
{$ELSE}
procedure SplitStrToArray(s: string; var Arr: TArray<string>; const EndLineStr: string = sLineBreak);
{$ENDIF}
var
x: integer;
begin
SetLength(Arr, 0);
if s = '' then Exit;
x := Pos(EndLineStr, s);
while x > 0 do
begin
SetLength(Arr, Length(Arr) + 1);
Arr[Length(Arr) - 1] := Copy(s, 1, x - 1);
s := Copy(s, x + Length(EndLineStr), Length(s));
x := Pos(EndLineStr, s);
end;
if s <> '' then
begin
SetLength(Arr, Length(Arr) + 1);
Arr[Length(Arr) - 1] := s;
end;
end;
// The SplitStrToArrayEx procedure uses the initial allocation of the size of the array and incrementing its size by the ArrDeltaSize value
// to avoid increasing the size of the array repeatedly by 1.
procedure SplitStrToArrayEx(s: string; var Arr: TStringDynArray; const EndLineStr: string = sLineBreak; ArrDeltaSize: WORD = 100);
var
x, xCount: integer;
begin
SetLength(Arr, 0);
if s = '' then Exit;
if ArrDeltaSize = 0 then ArrDeltaSize := 1; // must be greater than 0
xCount := 0;
x := Pos(EndLineStr, s);
while x > 0 do
begin
Inc(xCount);
if Length(Arr) = xCount - 1 then SetLength(Arr, Length(Arr) + ArrDeltaSize);
Arr[xCount - 1] := Copy(s, 1, x - 1);
s := Copy(s, x + Length(EndLineStr), Length(s));
x := Pos(EndLineStr, s);
end;
SetLength(Arr, xCount);
if s <> '' then
begin
SetLength(Arr, Length(Arr) + 1);
Arr[Length(Arr) - 1] := s;
end;
end;
function RemoveEmptyStrings(var Arr: TStringDynArray): integer;
var
A: TStringDynArray {$IFDEF FPC}= nil{$ENDIF};
i, Len, Ind: integer;
begin
Result := 0;
Len := Length(Arr);
if Len = 0 then Exit;
SetLength(A, Len);
Ind := 0;
for i := 0 to Len - 1 do
begin
if Arr[i] = '' then Inc(Result)
else
begin
A[Ind] := Arr[i];
Inc(Ind);
end;
end;
SetLength(A, Len - Result);
Arr := A;
end;
function CopyString(const s: string; Copies: integer = 2): string;
var
i: integer;
begin
Result := '';
for i := 1 to Copies do Result := Result + s;
end;
function InsertNumSep(NumStr: string; Separator: string = ' '; NumBlockSize: integer = 3; MaxInsertions: integer = 255): string;
var
s: string;
i, k, Len, LastDigitPos, xp: integer;
begin
s := NumStr;
LastDigitPos := Length(s);
xp := Pos('.', s);
if xp > 0 then LastDigitPos := xp - 1;
xp := Pos(GetDecimalSeparator, s);
if xp > 0 then LastDigitPos := xp - 1;
Len := LastDigitPos;
k := Len div NumBlockSize; // k - liczba wstawień separatora
for i := 1 to k do
begin
if i > MaxInsertions then Break;
Insert(Separator, s, Len - (i * NumBlockSize) + 1);
end;
s := TrimFromStart(s, Separator);
if Copy(s, 1, Length(Separator) + 1) = ('-' + Separator) then Delete(s, 2, Length(Separator)); // - 100 --> -100
Result := s;
end;
function TrimUp(s: string): string;
begin
Result := Trim(UpperCase(s));
end;
function GetFileSizeString(const FileSize: Int64; BytesStr: string = ' bytes'): string;
const
_KB = 1024;
_MB = _KB * 1024;
_GB = _MB * 1024;
var
fs: extended;
s: ShortString;
bNegative: Boolean;
xSize: Int64;
begin
xSize := FileSize;
bNegative := xSize < 0;
if bNegative then xSize := -xSize;
Result := IntToStr(xSize);
fs := xSize;
if fs < _KB then
begin
str(fs: 2: 0, s);
Result := string(s) + BytesStr;
end
else if (fs >= _KB) and (fs < _MB) then
begin
fs := fs / 1024;
str(fs: 2: 2, s);
Result := string(s) + ' KB';
end
else if (fs >= _MB) and (fs < _GB) then
begin
fs := (fs / 1024) / 1024;
str(fs: 2: 2, s);
Result := string(s) + ' MB';
end
else
begin
fs := (fs / 1024) / 1024 / 1024;
str(fs: 2: 2, s);
Result := string(s) + ' GB';
end;
if bNegative then Result := '-' + Result;
end;
function StrRemove(const s, StringToRemove: string; IgnoreCase: Boolean = False): string;
begin
Result := ReplaceAll(s, StringToRemove, '', IgnoreCase);
end;
function MyDir(bExcludeTrailingPathDelim: Boolean = True): string;
begin
Result := ExtractFileDir(ParamStr(0));
if bExcludeTrailingPathDelim then Result := ExcludeTrailingPathDelimiter(Result)
else Result := IncludeTrailingPathDelimiter(Result);
end;
function GetFirstNonDigitIndex(const s: string): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to Length(s) do
if not CharInSet(s[i], ['0'..'9']) then
begin
Result := i;
Break;
end;
end;
function GetFirstDigitIndex(const s: string): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to Length(s) do
if CharInSet(s[i], ['0'..'9']) then
begin
Result := i;
Break;
end;
end;
function GetHref(const InStr: string): string;
var
xp: integer;
sr: string;
begin
sr := InStr;
xp := Pos('href="', AnsiLowercase(sr));
if xp > 0 then
begin
sr := Copy(sr, xp + Length('href="'), Length(sr));
xp := Pos('"', sr);
if xp > 0 then sr := Copy(sr, 1, xp - 1);
end;
Result := sr;
end;
function GetAnchorText(const InStr: string; bReplaceHtmlEntities: Boolean = True): string;
var
xp: integer;
sr: string;
begin
sr := InStr;
xp := Pos('<a ', AnsiLowerCase(sr));
if xp > 0 then
begin
sr := Copy(sr, xp, Length(sr));
xp := Pos('>', sr);
if xp > 0 then
begin
sr := Copy(sr, xp + 1, Length(sr));
xp := Pos('</a>', AnsiLowerCase(sr));
if xp > 0 then sr := Copy(sr, 1, xp - 1);
end;
end;
if bReplaceHtmlEntities then sr := ReplaceHtmlEntities(sr);
Result := sr;
end;
function HtmlStringToStr(HTMLStr: string; IgnoreCase: Boolean = False): string;
begin
Result := ReplaceHtmlEntities(HTMLStr, IgnoreCase);
end;
function ReplaceHtmlEntities(const AStr: string; IgnoreCase: Boolean = False): string;
var
rf: TReplaceFlags;
s: string;
begin
// HTML entities are case sensitive!
rf := [rfReplaceAll];
if IgnoreCase then rf := rf + [rfIgnoreCase];
s := AStr;
s := StringReplace(s, '<', '<', rf);
s := StringReplace(s, '>', '>', rf);
s := StringReplace(s, '€', '€', rf);
s := StringReplace(s, '¢', '¢', rf);
s := StringReplace(s, '£', '£', rf);
s := StringReplace(s, '¥', '¥', rf);
s := StringReplace(s, '&', '&', rf);
s := StringReplace(s, '©', '©', rf);
s := StringReplace(s, '®', '®', rf);
s := StringReplace(s, '§', '§', rf);
s := StringReplace(s, '°', '°', rf);
s := StringReplace(s, '²', '²', rf);
s := StringReplace(s, '³', '³', rf);
s := StringReplace(s, '∫', '∫', rf);
s := StringReplace(s, 'µ', 'µ', rf);
s := StringReplace(s, '¶', '¶', rf);
s := StringReplace(s, '·', '·', rf);
s := StringReplace(s, '±', '±', rf);
s := StringReplace(s, '×', '×', rf);
s := StringReplace(s, '÷', '÷', rf);
s := StringReplace(s, 'Ω', 'Ω', rf);
s := StringReplace(s, 'α', 'α', rf);
s := StringReplace(s, 'β', 'β', rf);
s := StringReplace(s, 'γ', 'γ', rf);
s := StringReplace(s, 'Γ', 'Γ', rf);
s := StringReplace(s, 'δ', 'δ', rf);
s := StringReplace(s, 'Δ', 'Δ', rf);
s := StringReplace(s, 'π', 'π', rf);
s := StringReplace(s, 'Π', 'Π', rf);
s := StringReplace(s, 'Σ', 'Σ', rf);
s := StringReplace(s, '•', '•', rf);
s := StringReplace(s, '–', '–', rf);
s := StringReplace(s, '™', '™', rf);
s := StringReplace(s, '∘', '∘', rf); // ∘ / ∘
Result := s;
end;
function GetFileExt(fName: string; bRemoveFirstDot: Boolean = True): string;
begin
fName := ExtractFileExt(fName);
if bRemoveFirstDot then
if Copy(fName, 1, 1) = '.' then Delete(fName, 1, 1);
Result := fName;
end;
{$hints off}
// obcina łańcuch s po wystąpieniu w nim tekstu CutAfterText
function CutStrAfter(s, CutAfterText: string; IgnoreCase: Boolean = False; IncludeSearchText: Boolean = True): string;
var
xp: integer;
begin
if IgnoreCase then xp := Pos(AnsiUpperCase(CutAfterText), AnsiUpperCase(s))
else xp := Pos(CutAfterText, s);
if xp > 0 then
begin
s := Copy(s, 1, xp - 1);
if not IncludeSearchText then s := Copy(s, 1, Length(s) - Length(CutAfterText));
end;
Result := s;
end;
{$hints on}
{$hints off}
// obcina łańcuch s przed wystąpieniem w nim tekstu CutBeforeText
function CutStrBefore(s, CutBeforeText: string; IgnoreCase: Boolean = False): string;
var