-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJclStrings.pas
More file actions
5418 lines (4955 loc) · 144 KB
/
Copy pathJclStrings.pas
File metadata and controls
5418 lines (4955 loc) · 144 KB
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
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclStrings.pas. }
{ }
{ The Initial Developer of the Original Code is Marcel van Brakel. }
{ Portions created by Marcel van Brakel are Copyright (C) Marcel van Brakel. All rights reserved. }
{ }
{ Contributor(s): }
{ Alexander Radchenko }
{ Andreas Hausladen (ahuser) }
{ Anthony Steele }
{ Azret Botash }
{ Barry Kelly }
{ Huanlin Tsai }
{ Jack N.A. Bakker }
{ Jean-Fabien Connault (cycocrew) }
{ John C Molyneux }
{ Kiriakos Vlahos }
{ Leonard Wennekers }
{ Marcel Bestebroer }
{ Martin Kimmings }
{ Martin Kubecka }
{ Massimo Maria Ghisalberti }
{ Matthias Thoma (mthoma) }
{ Michael Winter }
{ Nick Hodges }
{ Olivier Sannier (obones) }
{ Pelle F. S. Liljendal }
{ Petr Vones (pvones) }
{ Rik Barker (rikbarker) }
{ Robert Lee }
{ Robert Marquardt (marquardt) }
{ Robert Rossmair (rrossmair) }
{ Andreas Schmidt }
{ Sean Farrow (sfarrow) }
{ }
{**************************************************************************************************}
{ }
{ Various character and string routines (searching, testing and transforming) }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
unit JclStrings;
{$I jcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF HAS_UNITSCOPE}
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF MSWINDOWS}
{$IFDEF UNICODE_RTL_DATABASE}
System.Character,
{$ENDIF UNICODE_RTL_DATABASE}
System.Classes, System.SysUtils,
{$ELSE ~HAS_UNITSCOPE}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF MSWINDOWS}
{$IFDEF UNICODE_RTL_DATABASE}
Character,
{$ENDIF UNICODE_RTL_DATABASE}
Classes, SysUtils,
{$ENDIF ~HAS_UNITSCOPE}
JclAnsiStrings,
JclWideStrings,
JclBase;
// Exceptions
type
EJclStringError = class(EJclError);
// Character constants and sets
const
// Misc. often used character definitions
NativeNull = Char(#0);
NativeSoh = Char(#1);
NativeStx = Char(#2);
NativeEtx = Char(#3);
NativeEot = Char(#4);
NativeEnq = Char(#5);
NativeAck = Char(#6);
NativeBell = Char(#7);
NativeBackspace = Char(#8);
NativeTab = Char(#9);
NativeLineFeed = JclBase.NativeLineFeed;
NativeVerticalTab = Char(#11);
NativeFormFeed = Char(#12);
NativeCarriageReturn = JclBase.NativeCarriageReturn;
NativeCrLf = JclBase.NativeCrLf;
NativeSo = Char(#14);
NativeSi = Char(#15);
NativeDle = Char(#16);
NativeDc1 = Char(#17);
NativeDc2 = Char(#18);
NativeDc3 = Char(#19);
NativeDc4 = Char(#20);
NativeNak = Char(#21);
NativeSyn = Char(#22);
NativeEtb = Char(#23);
NativeCan = Char(#24);
NativeEm = Char(#25);
NativeEndOfFile = Char(#26);
NativeEscape = Char(#27);
NativeFs = Char(#28);
NativeGs = Char(#29);
NativeRs = Char(#30);
NativeUs = Char(#31);
NativeSpace = Char(' ');
NativeComma = Char(',');
NativeBackslash = Char('\');
NativeForwardSlash = Char('/');
NativeDoubleQuote = Char('"');
NativeSingleQuote = Char('''');
NativeLineBreak = JclBase.NativeLineBreak;
const
// CharType return values
C1_UPPER = $0001; // Uppercase
C1_LOWER = $0002; // Lowercase
C1_DIGIT = $0004; // Decimal digits
C1_SPACE = $0008; // Space characters
C1_PUNCT = $0010; // Punctuation
C1_CNTRL = $0020; // Control characters
C1_BLANK = $0040; // Blank characters
C1_XDIGIT = $0080; // Hexadecimal digits
C1_ALPHA = $0100; // Any linguistic character: alphabetic, syllabary, or ideographic
{$IFDEF MSWINDOWS}
{$IFDEF SUPPORTS_EXTSYM}
{$EXTERNALSYM C1_UPPER}
{$EXTERNALSYM C1_LOWER}
{$EXTERNALSYM C1_DIGIT}
{$EXTERNALSYM C1_SPACE}
{$EXTERNALSYM C1_PUNCT}
{$EXTERNALSYM C1_CNTRL}
{$EXTERNALSYM C1_BLANK}
{$EXTERNALSYM C1_XDIGIT}
{$EXTERNALSYM C1_ALPHA}
{$ENDIF SUPPORTS_EXTSYM}
{$ENDIF MSWINDOWS}
type
TCharValidator = function(const C: Char): Boolean;
function ArrayContainsChar(const Chars: array of Char; const C: Char): Boolean; overload;
function ArrayContainsChar(const Chars: array of Char; const C: Char; out Index: SizeInt): Boolean; overload;
// String Test Routines
function StrIsAlpha(const S: string): Boolean;
function StrIsAlphaNum(const S: string): Boolean;
function StrIsAlphaNumUnderscore(const S: string): Boolean;
function StrContainsChars(const S: string; const Chars: TCharValidator; CheckAll: Boolean): Boolean; overload;
function StrContainsChars(const S: string; const Chars: array of Char; CheckAll: Boolean): Boolean; overload;
function StrConsistsOfNumberChars(const S: string): Boolean;
function StrIsDigit(const S: string): Boolean;
function StrIsSubset(const S: string; const ValidChars: TCharValidator): Boolean; overload;
function StrIsSubset(const S: string; const ValidChars: array of Char): Boolean; overload;
function StrSame(const S1, S2: string; CaseSensitive: Boolean = False): Boolean;
// String Transformation Routines
function StrCenter(const S: string; L: SizeInt; C: Char = ' '): string;
function StrCharPosLower(const S: string; CharPos: SizeInt): string;
function StrCharPosUpper(const S: string; CharPos: SizeInt): string;
function StrDoubleQuote(const S: string): string;
function StrEnsureNoPrefix(const Prefix, Text: string): string;
function StrEnsureNoSuffix(const Suffix, Text: string): string;
function StrEnsurePrefix(const Prefix, Text: string): string;
function StrEnsureSuffix(const Suffix, Text: string): string;
function StrEscapedToString(const S: string): string;
function StrLower(const S: string): string;
procedure StrLowerInPlace(var S: string);
procedure StrLowerBuff(S: PChar);
procedure StrMove(var Dest: string; const Source: string; const ToIndex,
FromIndex, Count: SizeInt);
function StrPadLeft(const S: string; Len: SizeInt; C: Char = NativeSpace): string;
function StrPadRight(const S: string; Len: SizeInt; C: Char = NativeSpace): string;
function StrProper(const S: string): string;
procedure StrProperBuff(S: PChar);
function StrQuote(const S: string; C: Char): string;
function StrRemoveChars(const S: string; const Chars: TCharValidator): string; overload;
function StrRemoveChars(const S: string; const Chars: array of Char): string; overload;
function StrRemoveLeadingChars(const S: string; const Chars: TCharValidator): string; overload;
function StrRemoveLeadingChars(const S: string; const Chars: array of Char): string; overload;
function StrRemoveEndChars(const S: string; const Chars: TCharValidator): string; overload;
function StrRemoveEndChars(const S: string; const Chars: array of Char): string; overload;
function StrKeepChars(const S: string; const Chars: TCharValidator): string; overload;
function StrKeepChars(const S: string; const Chars: array of Char): string; overload;
procedure StrReplace(var S: string; const Search, Replace: string; Flags: TReplaceFlags = []);
function StrReplaceChar(const S: string; const Source, Replace: Char): string;
function StrReplaceChars(const S: string; const Chars: TCharValidator; Replace: Char): string; overload;
function StrReplaceChars(const S: string; const Chars: array of Char; Replace: Char): string; overload;
function StrReplaceButChars(const S: string; const Chars: TCharValidator; Replace: Char): string; overload;
function StrReplaceButChars(const S: string; const Chars: array of Char; Replace: Char): string; overload;
function StrRepeat(const S: string; Count: SizeInt): string;
function StrRepeatLength(const S: string; L: SizeInt): string;
function StrReverse(const S: string): string;
procedure StrReverseInPlace(var S: string);
function StrSingleQuote(const S: string): string;
procedure StrSkipChars(var S: PChar; const Chars: TCharValidator); overload;
procedure StrSkipChars(var S: PChar; const Chars: array of Char); overload;
procedure StrSkipChars(const S: string; var Index: SizeInt; const Chars: TCharValidator); overload;
procedure StrSkipChars(const S: string; var Index: SizeInt; const Chars: array of Char); overload;
function StrSmartCase(const S: string; const Delimiters: TCharValidator): string; overload;
function StrSmartCase(const S: string; const Delimiters: array of Char): string; overload;
function StrStringToEscaped(const S: string): string;
function StrStripNonNumberChars(const S: string): string;
function StrToHex(const Source: string): string;
function StrTrimCharLeft(const S: string; C: Char): string;
function StrTrimCharsLeft(const S: string; const Chars: TCharValidator): string; overload;
function StrTrimCharsLeft(const S: string; const Chars: array of Char): string; overload;
function StrTrimCharRight(const S: string; C: Char): string;
function StrTrimCharsRight(const S: string; const Chars: TCharValidator): string; overload;
function StrTrimCharsRight(const S: string; const Chars: array of Char): string; overload;
function StrTrimQuotes(const S: string): string;
function StrUpper(const S: string): string;
procedure StrUpperInPlace(var S: string);
procedure StrUpperBuff(S: PChar);
// String Management
procedure StrAddRef(var S: string);
procedure StrDecRef(var S: string);
function StrLength(const S: string): SizeInt;
function StrRefCount(const S: string): SizeInt;
// String Search and Replace Routines
function StrCharCount(const S: string; C: Char): SizeInt; overload;
function StrCharsCount(const S: string; const Chars: TCharValidator): SizeInt; overload;
function StrCharsCount(const S: string; const Chars: array of Char): SizeInt; overload;
function StrStrCount(const S, SubS: string): SizeInt;
function StrCompare(const S1, S2: string; CaseSensitive: Boolean = False): SizeInt;
function StrCompareRange(const S1, S2: string; Index, Count: SizeInt; CaseSensitive: Boolean = True): SizeInt;
function StrCompareRangeEx(const S1, S2: string; Index, Count: SizeInt; CaseSensitive: Boolean): SizeInt;
procedure StrFillChar(var S; Count: SizeInt; C: Char);
function StrRepeatChar(C: Char; Count: SizeInt): string;
function StrFind(const Substr, S: string; const Index: SizeInt = 1): SizeInt;
function StrHasPrefix(const S: string; const Prefixes: array of string): Boolean;
function StrHasSuffix(const S: string; const Suffixes: array of string): Boolean;
function StrIndex(const S: string; const List: array of string; CaseSensitive: Boolean = False): SizeInt;
function StrIHasPrefix(const S: string; const Prefixes: array of string): Boolean;
function StrIHasSuffix(const S: string; const Suffixes: array of string): Boolean;
function StrILastPos(const SubStr, S: string): SizeInt;
function StrIPos(const SubStr, S: string): SizeInt;
function StrIPrefixIndex(const S: string; const Prefixes: array of string): SizeInt;
function StrIsOneOf(const S: string; const List: array of string): Boolean;
function StrISuffixIndex(const S: string; const Suffixes: array of string): SizeInt;
function StrLastPos(const SubStr, S: string): SizeInt;
function StrMatch(const Substr, S: string; Index: SizeInt = 1): SizeInt;
function StrMatches(const Substr, S: string; const Index: SizeInt = 1): Boolean;
function StrNIPos(const S, SubStr: string; N: SizeInt): SizeInt;
function StrNPos(const S, SubStr: string; N: SizeInt): SizeInt;
function StrPrefixIndex(const S: string; const Prefixes: array of string): SizeInt;
function StrSearch(const Substr, S: string; const Index: SizeInt = 1): SizeInt;
function StrSuffixIndex(const S: string; const Suffixes: array of string): SizeInt;
// String Extraction
/// Returns the string after SubStr
function StrAfter(const SubStr, S: string): string;
/// Returns the String before SubStr
function StrBefore(const SubStr, S: string): string;
/// Splits a string at SubStr, returns true when SubStr is found, Left contains the
/// string before the SubStr and Right the string behind SubStr
function StrSplit(const SubStr, S: string;var Left, Right : string): boolean;
/// Returns the string between Start and Stop
function StrBetween(const S: string; const Start, Stop: Char): string;
/// Returns all but rightmost N characters of the string
function StrChopRight(const S: string; N: SizeInt): string;{$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
/// Returns the left Count characters of the string
function StrLeft(const S: string; Count: SizeInt): string; {$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
/// Returns the string starting from position Start for the Count Characters
function StrMid(const S: string; Start, Count: SizeInt): string; {$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
/// Returns the string starting from position N to the end
function StrRestOf(const S: string; N: SizeInt): string;{$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
/// Returns the right Count characters of the string
function StrRight(const S: string; Count: SizeInt): string;{$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
// Character Test Routines
function CharEqualNoCase(const C1, C2: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsAlpha(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsAlphaNum(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsBlank(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsControl(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsDelete(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsDigit(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsFracDigit(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsHexDigit(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsLower(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsNumberChar(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
function CharIsNumber(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} {$IFDEF COMPILER16_UP} inline; {$ENDIF} {$ENDIF}
function CharIsPrintable(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsPunctuation(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsReturn(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsSpace(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsUpper(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsValidIdentifierLetter(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsWhiteSpace(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharIsWildcard(const C: Char): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharType(const C: Char): Word;
// Character Transformation Routines
function CharHex(const C: Char): Byte;
function CharLower(const C: Char): Char; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharUpper(const C: Char): Char; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharToggleCase(const C: Char): Char;
// Character Search and Replace
function CharPos(const S: string; const C: Char; const Index: SizeInt = 1): SizeInt;
function CharLastPos(const S: string; const C: Char; const Index: SizeInt = 1): SizeInt;
function CharIPos(const S: string; C: Char; const Index: SizeInt = 1): SizeInt;
function CharReplace(var S: string; const Search, Replace: Char): SizeInt;
// PCharVector
type
PCharVector = ^PChar;
function StringsToPCharVector(var Dest: PCharVector; const Source: TStrings): PCharVector;
function PCharVectorCount(Source: PCharVector): SizeInt;
procedure PCharVectorToStrings(const Dest: TStrings; Source: PCharVector);
procedure FreePCharVector(var Dest: PCharVector);
// MultiSz Routines
type
PMultiSz = PChar;
PAnsiMultiSz = JclAnsiStrings.PAnsiMultiSz;
PWideMultiSz = JclWideStrings.PWideMultiSz;
TAnsiStrings = JclAnsiStrings.TJclAnsiStrings;
TWideStrings = JclWideStrings.TJclWideStrings;
TAnsiStringList = JclAnsiStrings.TJclAnsiStringList;
TWideStringList = JclWideStrings.TJclWideStringList;
function StringsToMultiSz(var Dest: PMultiSz; const Source: TStrings): PMultiSz;
procedure MultiSzToStrings(const Dest: TStrings; const Source: PMultiSz);
function MultiSzLength(const Source: PMultiSz): SizeInt;
procedure AllocateMultiSz(var Dest: PMultiSz; Len: SizeInt);
procedure FreeMultiSz(var Dest: PMultiSz);
function MultiSzDup(const Source: PMultiSz): PMultiSz;
function AnsiStringsToAnsiMultiSz(var Dest: PAnsiMultiSz; const Source: TAnsiStrings): PAnsiMultiSz;
{$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure AnsiMultiSzToAnsiStrings(const Dest: TAnsiStrings; const Source: PAnsiMultiSz); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function AnsiMultiSzLength(const Source: PAnsiMultiSz): SizeInt; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure AllocateAnsiMultiSz(var Dest: PAnsiMultiSz; Len: SizeInt); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure FreeAnsiMultiSz(var Dest: PAnsiMultiSz); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function AnsiMultiSzDup(const Source: PAnsiMultiSz): PAnsiMultiSz; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function WideStringsToWideMultiSz(var Dest: PWideMultiSz; const Source: TWideStrings): PWideMultiSz;
{$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure WideMultiSzToWideStrings(const Dest: TWideStrings; const Source: PWideMultiSz); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function WideMultiSzLength(const Source: PWideMultiSz): SizeInt; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure AllocateWideMultiSz(var Dest: PWideMultiSz; Len: SizeInt); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure FreeWideMultiSz(var Dest: PWideMultiSz); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function WideMultiSzDup(const Source: PWideMultiSz): PWideMultiSz; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
// TStrings Manipulation
procedure StrIToStrings(S, Sep: string; const List: TStrings; const AllowEmptyString: Boolean = True);
procedure StrToStrings(S, Sep: string; const List: TStrings; const AllowEmptyString: Boolean = True);
function StringsToStr(const List: TStrings; const Sep: string; const AllowEmptyString: Boolean = True): string; overload;
function StringsToStr(const List: TStrings; const Sep: string; const NumberOfItems: SizeInt; const AllowEmptyString:
Boolean = True): string; overload;
procedure TrimStrings(const List: TStrings; DeleteIfEmpty: Boolean = True);
procedure TrimStringsRight(const List: TStrings; DeleteIfEmpty: Boolean = True);
procedure TrimStringsLeft(const List: TStrings; DeleteIfEmpty: Boolean = True);
function AddStringToStrings(const S: string; Strings: TStrings; const Unique: Boolean): Boolean;
// Miscellaneous
// (OF) moved to JclSysUtils
// function BooleanToStr(B: Boolean): string;
// AnsiString here because it is binary data
function FileToString(const FileName: string): {$IFDEF COMPILER12_UP}RawByteString{$ELSE}AnsiString{$ENDIF};
procedure StringToFile(const FileName: string; const Contents: {$IFDEF COMPILER12_UP}RawByteString{$ELSE}AnsiString{$ENDIF};
Append: Boolean = False);
function StrToken(var S: string; Separator: Char): string;
procedure StrTokens(const S: string; const List: TStrings);
procedure StrTokenToStrings(S: string; Separator: Char; const List: TStrings);
function StrWord(const S: string; var Index: SizeInt; out Word: string): Boolean; overload;
function StrWord(var S: PChar; out Word: string): Boolean; overload;
function StrIdent(const S: string; var Index: SizeInt; out Ident: string): Boolean; overload;
function StrIdent(var S: PChar; out Ident: string): Boolean; overload;
function StrToFloatSafe(const S: string): Float;
function StrToIntSafe(const S: string): Integer;
procedure StrNormIndex(const StrLen: SizeInt; var Index: SizeInt; var Count: SizeInt); overload;
function ArrayOf(List: TStrings): TDynStringArray; overload;
type
FormatException = class(EJclError);
ArgumentException = class(EJclError);
ArgumentNullException = class(EJclError);
ArgumentOutOfRangeException = class(EJclError);
IToString = interface
['{C4ABABB4-1029-46E7-B5FA-99800F130C05}']
function ToString: string;
end;
TCharDynArray = array of Char;
// The TStringBuilder class is a Delphi implementation of the .NET
// System.Text.StringBuilder.
// It is zero based and the methods that have a TObject argument (Append, Insert,
// AppendFormat) are limited to IToString implementors or Delphi 2009+ RTL.
// This class is not threadsafe. Any instance of TStringBuilder should not
// be used in different threads at the same time.
TJclStringBuilder = class(TInterfacedObject, IToString)
private
FChars: TCharDynArray;
FLength: SizeInt;
FMaxCapacity: SizeInt;
function GetCapacity: SizeInt;
procedure SetCapacity(const Value: SizeInt);
function GetChars(Index: SizeInt): Char;
procedure SetChars(Index: SizeInt; const Value: Char);
procedure Set_Length(const Value: SizeInt);
protected
function AppendPChar(Value: PChar; Count: SizeInt; RepeatCount: SizeInt = 1): TJclStringBuilder;
function InsertPChar(Index: SizeInt; Value: PChar; Count: SizeInt; RepeatCount: SizeInt = 1): TJclStringBuilder;
public
constructor Create(const Value: string; Capacity: SizeInt = 16); overload;
constructor Create(Capacity: SizeInt = 16; MaxCapacity: SizeInt = MaxInt); overload;
constructor Create(const Value: string; StartIndex, Length, Capacity: SizeInt); overload;
function Append(const Value: string): TJclStringBuilder; overload;
function Append(const Value: string; StartIndex, Length: SizeInt): TJclStringBuilder; overload;
function Append(Value: Boolean): TJclStringBuilder; overload;
function Append(Value: Char; RepeatCount: SizeInt = 1): TJclStringBuilder; overload;
function Append(const Value: array of Char): TJclStringBuilder; overload;
function Append(const Value: array of Char; StartIndex, Length: SizeInt): TJclStringBuilder; overload;
function Append(Value: Cardinal): TJclStringBuilder; overload;
function Append(Value: Integer): TJclStringBuilder; overload;
function Append(Value: Double): TJclStringBuilder; overload;
function Append(Value: Int64): TJclStringBuilder; overload;
function Append(Obj: TObject): TJclStringBuilder; overload;
function AppendFormat(const Fmt: string; const Args: array of const): TJclStringBuilder; overload;
function AppendFormat(const Fmt: string; Arg0: Variant): TJclStringBuilder; overload;
function AppendFormat(const Fmt: string; Arg0, Arg1: Variant): TJclStringBuilder; overload;
function AppendFormat(const Fmt: string; Arg0, Arg1, Arg2: Variant): TJclStringBuilder; overload;
function Insert(Index: SizeInt; const Value: string; Count: SizeInt = 1): TJclStringBuilder; overload;
function Insert(Index: SizeInt; Value: Boolean): TJclStringBuilder; overload;
function Insert(Index: SizeInt; const Value: array of Char): TJclStringBuilder; overload;
function Insert(Index: SizeInt; const Value: array of Char; StartIndex, Length: SizeInt): TJclStringBuilder;
overload;
function Insert(Index: SizeInt; Value: Cardinal): TJclStringBuilder; overload;
function Insert(Index: SizeInt; Value: Integer): TJclStringBuilder; overload;
function Insert(Index: SizeInt; Value: Double): TJclStringBuilder; overload;
function Insert(Index: SizeInt; Value: Int64): TJclStringBuilder; overload;
function Insert(Index: SizeInt; Obj: TObject): TJclStringBuilder; overload;
function Replace(OldChar, NewChar: Char; StartIndex: SizeInt = 0; Count: SizeInt = -1): TJclStringBuilder;
overload;
function Replace(OldValue, NewValue: string; StartIndex: SizeInt = 0; Count: SizeInt = -1): TJclStringBuilder;
overload;
function Remove(StartIndex, Length: SizeInt): TJclStringBuilder;
function EnsureCapacity(Capacity: SizeInt): SizeInt;
procedure Clear;
{ IToString }
function ToString: string; {$IFDEF RTL200_UP} override; {$ENDIF RTL200_UP}
property __Chars__[Index: SizeInt]: Char read GetChars write SetChars; default;
property Chars: TCharDynArray read FChars;
property Length: SizeInt read FLength write Set_Length;
property Capacity: SizeInt read GetCapacity write SetCapacity;
property MaxCapacity: SizeInt read FMaxCapacity;
end;
{$IFDEF RTL200_UP}
TStringBuilder = {$IFDEF HAS_UNITSCOPE}System.{$ENDIF}SysUtils.TStringBuilder;
{$ELSE ~RTL200_UP}
TStringBuilder = TJclStringBuilder;
{$ENDIF ~RTL200_UP}
// DotNetFormat() uses the .NET format style: "{argX}"
function DotNetFormat(const Fmt: string; const Args: array of const): string; overload;
function DotNetFormat(const Fmt: string; const Arg0: Variant): string; overload;
function DotNetFormat(const Fmt: string; const Arg0, Arg1: Variant): string; overload;
function DotNetFormat(const Fmt: string; const Arg0, Arg1, Arg2: Variant): string; overload;
// TJclTabSet
type
TJclTabSet = class (TInterfacedObject, IToString)
private
FData: TObject;
function GetCount: SizeInt;
function GetStops(Index: SizeInt): SizeInt;
function GetTabWidth: SizeInt;
function GetZeroBased: Boolean;
procedure SetStops(Index, Value: SizeInt);
procedure SetTabWidth(Value: SizeInt);
procedure SetZeroBased(Value: Boolean);
protected
function FindStop(Column: SizeInt): SizeInt;
function InternalTabStops: TDynSizeIntArray;
function InternalTabWidth: SizeInt;
procedure RemoveAt(Index: SizeInt);
public
constructor Create; overload;
constructor Create(Data: TObject); overload;
constructor Create(TabWidth: SizeInt); overload;
constructor Create(const Tabstops: array of SizeInt; ZeroBased: Boolean); overload;
constructor Create(const Tabstops: array of SizeInt; ZeroBased: Boolean; TabWidth: SizeInt); overload;
destructor Destroy; override;
// cloning and referencing
function Clone: TJclTabSet;
function NewReference: TJclTabSet;
// Tab stops manipulation
function Add(Column: SizeInt): SizeInt;
function Delete(Column: SizeInt): SizeInt;
// Usage
function Expand(const S: string): string; overload;
function Expand(const S: string; Column: SizeInt): string; overload;
procedure OptimalFillInfo(StartColumn, TargetColumn: SizeInt; out TabsNeeded, SpacesNeeded: SizeInt);
function Optimize(const S: string): string; overload;
function Optimize(const S: string; Column: SizeInt): string; overload;
function StartColumn: SizeInt;
function TabFrom(Column: SizeInt): SizeInt;
function UpdatePosition(const S: string): SizeInt; overload;
function UpdatePosition(const S: string; Column: SizeInt): SizeInt; overload;
function UpdatePosition(const S: string; var Column, Line: SizeInt): SizeInt; overload;
{ IToString }
function ToString: string; overload; {$IFDEF RTL200_UP} override; {$ENDIF RTL200_UP}
// Conversions
function ToString(FormattingOptions: SizeInt): string; {$IFDEF RTL200_UP} reintroduce; {$ENDIF RTL200_UP} overload;
class function FromString(const S: string): TJclTabSet; {$IFDEF SUPPORTS_STATIC} static; {$ENDIF SUPPORTS_STATIC}
// Properties
property ActualTabWidth: SizeInt read InternalTabWidth;
property Count: SizeInt read GetCount;
property TabStops[Index: SizeInt]: SizeInt read GetStops write SetStops; default;
property TabWidth: SizeInt read GetTabWidth write SetTabWidth;
property ZeroBased: Boolean read GetZeroBased write SetZeroBased;
end;
// Formatting constants
const
TabSetFormatting_SurroundStopsWithBrackets = 1;
TabSetFormatting_EmptyBracketsIfNoStops = 2;
TabSetFormatting_NoTabStops = 4;
TabSetFormatting_NoTabWidth = 8;
TabSetFormatting_AutoTabWidth = 16;
// common combinations
TabSetFormatting_Default = 0;
TabSetFormatting_AlwaysUseBrackets = TabSetFormatting_SurroundStopsWithBrackets or
TabSetFormatting_EmptyBracketsIfNoStops;
TabSetFormatting_Full = TabSetFormatting_AlwaysUseBrackets or TabSetFormatting_AutoTabWidth;
// aliases
TabSetFormatting_StopsOnly = TabSetFormatting_NoTabWidth;
TabSetFormatting_TabWidthOnly = TabSetFormatting_NoTabStops;
TabSetFormatting_StopsWithoutBracketsAndTabWidth = TabSetFormatting_Default;
// Tab expansion routines
function StrExpandTabs(S: string): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
function StrExpandTabs(S: string; TabWidth: SizeInt): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
function StrExpandTabs(S: string; TabSet: TJclTabSet): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
// Tab optimization routines
function StrOptimizeTabs(S: string): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
function StrOptimizeTabs(S: string; TabWidth: SizeInt): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
function StrOptimizeTabs(S: string; TabSet: TJclTabSet): string; {$IFDEF SUPPORTS_INLINE}inline; {$ENDIF} overload;
// move to JclBase?
type
NullReferenceException = class(EJclError)
public
constructor Create; overload;
end;
procedure StrResetLength(var S: WideString); overload;
procedure StrResetLength(var S: AnsiString); overload;
procedure StrResetLength(S: TJclStringBuilder); overload;
{$IFDEF SUPPORTS_UNICODE_STRING}
procedure StrResetLength(var S: UnicodeString); overload;
{$ENDIF SUPPORTS_UNICODE_STRING}
// natural comparison functions
function CompareNaturalStr(const S1, S2: string): SizeInt;
function CompareNaturalText(const S1, S2: string): SizeInt;
{$IFNDEF UNICODE_RTL_DATABASE}
// internal structures published to make function inlining working
const
MaxStrCharCount = Ord(High(Char)) + 1; // # of chars in one set
StrLoOffset = MaxStrCharCount * 0; // offset to lower case chars
StrUpOffset = MaxStrCharCount * 1; // offset to upper case chars
StrReOffset = MaxStrCharCount * 2; // offset to reverse case chars
StrCaseMapSize = MaxStrCharCount * 3; // # of chars is a table
var
StrCaseMap: array [0..StrCaseMapSize - 1] of Char; // case mappings
StrCaseMapReady: Boolean = False; // true if case map exists
StrCharTypes: array [Char] of Word;
{$ENDIF ~UNICODE_RTL_DATABASE}
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\common';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
uses
{$IFDEF HAS_UNIT_LIBC}
Libc,
{$ENDIF HAS_UNIT_LIBC}
{$IFDEF SUPPORTS_UNICODE}
{$IFDEF HAS_UNITSCOPE}
System.StrUtils,
{$ELSE ~HAS_UNITSCOPE}
StrUtils,
{$ENDIF ~HAS_UNITSCOPE}
{$ENDIF SUPPORTS_UNICODE}
JclLogic, JclResources, JclStreams, JclSynch, JclSysUtils;
//=== Internal ===============================================================
type
TStrRec = packed record
RefCount: Integer;
Length: Integer;
end;
PStrRec = ^TStrRec;
{$IFNDEF UNICODE_RTL_DATABASE}
procedure LoadCharTypes;
var
CurrChar: Char;
CurrType: Word;
begin
for CurrChar := Low(CurrChar) to High(CurrChar) do
begin
{$IFDEF MSWINDOWS}
CurrType := 0;
GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, @CurrChar, 1, CurrType);
{$DEFINE CHAR_TYPES_INITIALIZED}
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
CurrType := 0;
if isupper(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_UPPER;
if islower(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_LOWER;
if isdigit(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_DIGIT;
if isspace(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_SPACE;
if ispunct(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_PUNCT;
if iscntrl(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_CNTRL;
if isblank(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_BLANK;
if isxdigit(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_XDIGIT;
if isalpha(Byte(CurrChar)) <> 0 then
CurrType := CurrType or C1_ALPHA;
{$DEFINE CHAR_TYPES_INITIALIZED}
{$ENDIF LINUX}
StrCharTypes[CurrChar] := CurrType;
{$IFNDEF CHAR_TYPES_INITIALIZED}
Implement case map initialization here
{$ENDIF ~CHAR_TYPES_INITIALIZED}
end;
end;
procedure LoadCaseMap;
var
CurrChar, UpCaseChar, LoCaseChar, ReCaseChar: Char;
begin
if not StrCaseMapReady then
begin
for CurrChar := Low(Char) to High(Char) do
begin
{$IFDEF MSWINDOWS}
LoCaseChar := CurrChar;
UpCaseChar := CurrChar;
{$IFDEF HAS_UNITSCOPE}Winapi.{$ENDIF}Windows.CharLowerBuff(@LoCaseChar, 1);
{$IFDEF HAS_UNITSCOPE}Winapi.{$ENDIF}Windows.CharUpperBuff(@UpCaseChar, 1);
{$DEFINE CASE_MAP_INITIALIZED}
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
LoCaseChar := Char(tolower(Byte(CurrChar)));
UpCaseChar := Char(toupper(Byte(CurrChar)));
{$DEFINE CASE_MAP_INITIALIZED}
{$ENDIF LINUX}
{$IFNDEF CASE_MAP_INITIALIZED}
Implement case map initialization here
{$ENDIF ~CASE_MAP_INITIALIZED}
if CharIsUpper(CurrChar) then
ReCaseChar := LoCaseChar
else
if CharIsLower(CurrChar) then
ReCaseChar := UpCaseChar
else
ReCaseChar := CurrChar;
StrCaseMap[Ord(CurrChar) + StrLoOffset] := LoCaseChar;
StrCaseMap[Ord(CurrChar) + StrUpOffset] := UpCaseChar;
StrCaseMap[Ord(CurrChar) + StrReOffset] := ReCaseChar;
end;
StrCaseMapReady := True;
end;
end;
// Uppercases or Lowercases a give string depending on the
// passed offset. (UpOffset or LoOffset)
procedure StrCase(var Str: string; const Offset: SizeInt);
var
P: PChar;
I, L: SizeInt;
begin
L := Length(Str);
if L > 0 then
begin
UniqueString(Str);
P := PChar(Str);
for I := 1 to L do
begin
P^ := StrCaseMap[Offset + Ord(P^)];
Inc(P);
end;
end;
end;
// Internal utility function
// Uppercases or Lowercases a give null terminated string depending on the
// passed offset. (UpOffset or LoOffset)
procedure StrCaseBuff(S: PChar; const Offset: SizeInt);
var
C: Char;
begin
if S <> nil then
begin
repeat
C := S^;
S^ := StrCaseMap[Offset + Ord(C)];
Inc(S);
until C = #0;
end;
end;
{$ENDIF ~UNICODE_RTL_DATABASE}
function StrEndW(Str: PWideChar): PWideChar;
begin
Result := Str;
while Result^ <> #0 do
Inc(Result);
end;
function ArrayContainsChar(const Chars: array of Char; const C: Char): Boolean;
var
idx: SizeInt;
begin
Result := ArrayContainsChar(Chars, C, idx);
end;
function ArrayContainsChar(const Chars: array of Char; const C: Char; out Index: SizeInt): Boolean;
{ optimized version for sorted arrays
var
I, L, H: SizeInt;
begin
L := Low(Chars);
H := High(Chars);
while L <= H do
begin
I := (L + H) div 2;
if C = Chars[I] then
begin
Result := True;
Exit;
end
else
if C < Chars[I] then
H := I - 1
else
// C > Chars[I]
L := I + 1;
end;
Result := False;
end;}
begin
Index := High(Chars);
while (Index >= Low(Chars)) and (Chars[Index] <> C) do
Dec(Index);
Result := Index >= Low(Chars);
end;
// String Test Routines
function StrIsAlpha(const S: string): Boolean;
var
I: SizeInt;
begin
Result := S <> '';
for I := 1 to Length(S) do
begin
if not CharIsAlpha(S[I]) then
begin
Result := False;
Exit;
end;
end;
end;
function StrIsAlphaNum(const S: string): Boolean;
var
I: SizeInt;
begin
Result := S <> '';
for I := 1 to Length(S) do
begin
if not CharIsAlphaNum(S[I]) then
begin
Result := False;
Exit;
end;
end;
end;
function StrConsistsofNumberChars(const S: string): Boolean;
var
I: SizeInt;
begin
Result := S <> '';
for I := 1 to Length(S) do
begin
if not CharIsNumberChar(S[I]) then
begin
Result := False;
Exit;
end;
end;
end;
function StrContainsChars(const S: string; const Chars: TCharValidator; CheckAll: Boolean): Boolean;
var
I: SizeInt;
begin
Result := False;
if CheckAll then
begin
// this will not work with the current definition of the validator. The validator would need to check each character
// it requires against the string (which is currently not provided to the Validator). The current implementation of
// CheckAll will check if all characters in S will be accepted by the provided Validator, which is wrong and incon-
// sistent with the documentation and the array-based overload.
for I := 1 to Length(S) do
begin
Result := Chars(S[I]);
if not Result then
Break;
end;
end
else
begin
for I := 1 to Length(S) do
begin
Result := Chars(S[I]);
if Result then
Break;
end;
end;
end;
function StrContainsChars(const S: string; const Chars: array of Char; CheckAll: Boolean): Boolean;
var
I: SizeInt;
begin
if CheckAll then
begin
Result := True;
I := High(Chars);
while (I >= 0) and Result do
begin
Result := CharPos(S, Chars[I]) > 0;
Dec(I);
end;
end
else
begin
Result := False;
for I := 1 to Length(S) do
begin
Result := ArrayContainsChar(Chars, S[I]);
if Result then
Break;
end;
end;
end;
function StrIsAlphaNumUnderscore(const S: string): Boolean;
var
I: SizeInt;
C: Char;
begin
for I := 1 to Length(S) do
begin
C := S[I];
if not (CharIsAlphaNum(C) or (C = '_')) then
begin
Result := False;
Exit;
end;
end;
Result := Length(S) > 0;
end;
function StrIsDigit(const S: string): Boolean;
var
I: SizeInt;
begin
Result := S <> '';
for I := 1 to Length(S) do
begin
if not CharIsDigit(S[I]) then
begin
Result := False;
Exit;
end;
end;
end;
function StrIsSubset(const S: string; const ValidChars: TCharValidator): Boolean;
var
I: SizeInt;
begin
for I := 1 to Length(S) do
begin
Result := ValidChars(S[I]);
if not Result then
Exit;
end;
Result := Length(S) > 0;
end;
function StrIsSubset(const S: string; const ValidChars: array of Char): Boolean;
var
I: SizeInt;
begin
for I := 1 to Length(S) do
begin
Result := ArrayContainsChar(ValidChars, S[I]);
if not Result then
Exit;
end;
Result := Length(S) > 0;
end;
function StrSame(const S1, S2: string; CaseSensitive: Boolean): Boolean;
begin
Result := StrCompare(S1, S2, CaseSensitive) = 0;
end;
//=== String Transformation Routines =========================================
function StrCenter(const S: string; L: SizeInt; C: Char = ' '): string;
begin
if Length(S) < L then
begin
Result := StringOfChar(C, (L - Length(S)) div 2) + S;