Menu

[r3204]: / branches / experimental / Src / TrunkSrc / UStrUtils.pas  Maximize  Restore  History

Download this file

808 lines (702 with data), 26.7 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2011-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Unicode string utility routines.
*
* Some of the routines are simply wrappers around SysUtils and StrUtils string
* routines with the purpose of (a) collecting them together in one place and
* (b) providing a standardised parameter order. Other routines are original.
}
unit UStrUtils;
interface
uses
// Delphi
Classes;
/// <summary>Checks if string Haystack contains string Needle. Case sensitive.
/// </summary>
function StrContainsStr(const Needle, Haystack: UnicodeString): Boolean;
/// <summary>Checks if string Haystack contains string Needle. Case
/// insensitive.</summary>
function StrContainsText(const Needle, Haystack: UnicodeString): Boolean;
/// <summary>Checks if string Str is present in string array Values. Case
/// insensitive.</summary>
function StrMatchText(const Str: string; const Values: array of string):
Boolean;
/// <summary>Returns index of string Needle in string Haystack or 0 if Needle
/// is not in Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: UnicodeString): Integer; overload;
/// <summary>Returns index of string Needle in string Haystack, beginning the
/// search at Offset. Returns 0 if Haystack is not found or if Offset is less
/// than 1 or greater than length of Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: UnicodeString; const Offset: Integer):
Integer; overload;
/// <summary>Returns index of last occurence of string Needle in string
/// Haystack or 0 if Needle is not in Haystack. Case sensitive.</summary>
function StrLastPos(const Needle, Haystack: UnicodeString): Integer;
/// <summary>Compares Left and Right strings, ignoring case. Returns 0 if both
/// strings are the same, -ve if Left is less than Right or +ve if Left is
/// greater than Right.</summary>
function StrCompareText(const Left, Right: UnicodeString): Integer;
/// <summary>Checks if Left and Right strings are equal. Case sensitive.
/// </summary>
function StrSameStr(const Left, Right: UnicodeString): Boolean;
/// <summary>Checks if Left and Right strings are equivalent when case is
/// ignored.</summary>
function StrSameText(const Left, Right: UnicodeString): Boolean;
/// <summary>Converts a string to upper case.</summary>
function StrToUpper(const Str: UnicodeString): UnicodeString;
/// <summary>Converts a string to lower case.</summary>
function StrToLower(const Str: UnicodeString): UnicodeString;
/// <summary>Returns a substring of Str starting at index StartIdx of length
/// Count.</summary>
/// <remarks>
/// <para>If Count specifies more than the available number of characters, the
/// substring from StartIdx to the end of the string is returned.</para>
/// <para>If StartIdx is beyond the end of Str the empty string is returned.
/// </para>
/// </remarks>
function StrSlice(const Str: UnicodeString; const StartIdx, Count: Integer):
UnicodeString;
/// <summary>Returns the leading characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceLeft(const Str: UnicodeString; const Count: Integer):
UnicodeString;
/// <summary>Returns the trailing characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceRight(const Str: UnicodeString; const Count: Integer):
UnicodeString;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// sensitive.</summary>
/// <remarks>False is returned if SubStr = ''.</remarks>
function StrStartsStr(const SubStr, Str: UnicodeString): Boolean;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// insensitive.</summary>
/// <remarks>False is returned if SubStr = ''.</remarks>
function StrStartsText(const SubStr, Str: UnicodeString): Boolean;
/// <summary>Replaces all occurences of FindStr in Str with ReplaceStr.
/// </summary>
function StrReplace(const Str, FindStr, ReplaceStr: UnicodeString):
UnicodeString;
/// <summary>Trims leading and trailing white space characters from a string.
/// </summary>
/// <remarks>White space is considered to be any character from #0..#32.
/// </remarks>
function StrTrim(const Str: UnicodeString): UnicodeString;
/// <summary>Trims leading white space characters from a string.</summary>
/// <remarks>White space is considered to be any character from #0..#32.
/// </remarks>
function StrTrimLeft(const Str: UnicodeString): UnicodeString;
/// <summary>Trims trailing white space characters from a string.</summary>
/// <remarks>White space is considered to be any character from #0..#32.
/// </remarks>
function StrTrimRight(const Str: UnicodeString): UnicodeString;
/// <summary>Trims leading and trailing characters C from string Str.</summary>
function StrTrimChars(const Str: UnicodeString; const C: Char): UnicodeString;
/// <summary>Trims leading characters C from string Str.</summary>
function StrTrimLeftChars(const Str: UnicodeString; const C: Char):
UnicodeString;
/// <summary>Trims trailing characters C from string Str.</summary>
function StrTrimRightChars(const Str: UnicodeString; const C: Char):
UnicodeString;
/// <summary>Ensures line breaks in a string are in Windows format (CRLF).
/// </summary>
/// <remarks>Converts both Unix (LF) and Macintosh (CR) to CRLF.</remarks>
function StrWindowsLineBreaks(const Str: UnicodeString): UnicodeString;
/// <summary>Ensures line breaks in a string are in Unix format (LF).
/// </summary>
/// <remarks>Converts both Windows (CRLF) and Macintosh (CR) to LF.</remarks>
function StrUnixLineBreaks(const Str: UnicodeString): UnicodeString;
/// <summary>Checks if the character at index Idx in Str is one of the
/// delimiter characters stored in Delims.</summary>
/// <remarks>Delims must contain only characters that take just one wide
/// character, i.e. they are from the basic multiligual plane.</remarks>
function StrIsDelimiter(const Delims, Str: UnicodeString; const Idx: Integer):
Boolean;
/// <summary>Returns total number of occurences of delimieters from Delim in
/// string Str.</summary>
/// <remarks>Delims must contain only characters that take just one wide
/// character, i.e. they are from the basic multiligual plane.</remarks>
function StrCountDelims(const Delims, Str: UnicodeString): Integer;
/// <summary>Returns position of last character in Str that matches any
/// character from Delims.</summary>
/// <remarks>Delims must contain only characters that take just one wide
/// character, i.e. they are from the basic multiligual plane.</remarks>
function StrLastDelimiterPos(const Delims, Str: UnicodeString): Integer;
/// <summary>Capitalises each word in string Str.</summary>
/// <remarks>Words are recognised only if separated by white space.</remarks>
function StrCapitaliseWords(const Str: UnicodeString): UnicodeString;
/// <summary>Compresses white space in string Str.</summary>
/// <remarks>Each sequence of one or more white space characters in Str is
/// replaced by a single space.</remarks>
function StrCompressWhiteSpace(const Str: UnicodeString): UnicodeString;
/// <summary>Removes all white space from string Str.</summary>
function StrStripWhiteSpace(const Str: UnicodeString): UnicodeString;
/// <summary>Checks if a stream contains white space.</summary>
function StrContainsWhiteSpace(const Str: UnicodeString): Boolean;
/// <summary>Encloses string Str in quotes specified by Quote iff Str contains
/// white space.</summary>
/// <remarks>Opening and closing quote are the same character.</remarks>
function StrQuoteSpaced(const Str: UnicodeString;
const Quote: Char = '"'): UnicodeString;
/// <summary>Joins all strings from a string list together into a single string
/// with each list element being separated by Delim. Empty string list elements
/// are included in the output string only if AllowEmpty is True.</summary>
function StrJoin(const SL: TStrings; const Delim: UnicodeString;
const AllowEmpty: Boolean = True): UnicodeString;
/// <summary>Splits string Str at delimiter Delim and records the components in
/// List. If TrimStrs is True spaces are trimmed from each component. If
/// AllowEmpty is True empty components are added to List.</summary>
/// <remarks>String components are trimmed before being checked for emptyness.
/// </remarks>
function StrExplode(Str: UnicodeString; const Delim: UnicodeString;
const List: TStrings; const AllowEmpty: Boolean = True;
const TrimStrs: Boolean = False): Integer;
/// <summary>Splits string Str at the first occurence of Delim setting Left to
/// the string preceding Delim and Right to the string following Delim.
/// Returns True if Delim was found in Str, False if not.</summary>
/// <remarks>Left and Right be empty if Delim is found at the start or end of
/// Str respectively.</remarks>
function StrSplit(const Str: UnicodeString; const Delim: UnicodeString;
out Left, Right: UnicodeString): Boolean;
/// <summary>Returns the portion of string Str preceding the first occurence of
/// string Str and removes the returned string and Delim from Str.</summary>
/// <remarks>The empty string is returned if Str is empty or begins with Delim.
/// Str can be set to the empty string if Delim ends the string of if Str was
/// empty when the routine was called.</remarks>
function StrPop(var Str: string; const Delim: string): string;
/// <summary>Word wraps text Str to form lines of maximum length MaxLen and
/// offsets each line using spaces to form a left margin of size given by
/// Margin.</summary>
/// <remarks>Output lines are separated by CRLF.</remarks>
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: Integer):
UnicodeString; overload;
/// <summary>Word wraps each paragraph of text in Paras so that each line of a
/// paragraph has lines of maximum length MaxLineLen and is offset by the
/// number of spaces gvien by Margin. Blanks lines are used to separate
/// output paragraphs iff SeparateParas is true.</summary>
/// <remarks>Output lines are separated by CRLF.</remarks>
function StrWrap(const Paras: TStrings; const MaxLineLen, Margin: Integer;
const SeparateParas: Boolean): UnicodeString; overload;
/// <summary>Checks in string Str forms a valid sentence and, if not, adds a
/// full stop.</summary>
/// <remarks>Trailing white space is ignored when detecting full stops etc.
/// </remarks>
function StrMakeSentence(const Str: UnicodeString): UnicodeString;
/// <summary>Returns one of two strings, TrueStr or FalseStr, depending on
/// value of given Condition.</summary>
function StrIf(const Condition: Boolean; const TrueStr, FalseStr: string):
string;
implementation
uses
// Delphi
SysUtils, StrUtils, Character,
// Project
UConsts;
{ Internal helper routines }
/// <summary>Checks if character at position Idx in Str can be trimmed from
/// string.</summary>
/// <remarks>Character can be trimmed if it's not part of a multi-character
/// sequence and TrimFn returns True for the character.</remarks>
function IsTrimmableChar(const Str: UnicodeString; const Idx: Integer;
const TrimFn: TFunc<Char,Boolean>): Boolean;
begin
Result := (ByteType(Str, Idx) = mbSingleByte) and TrimFn(Str[Idx]);
end;
/// <summary>Trims characters from both ends of a string.</summary>
/// <remarks>Anonymous function TrimFn determines whether a character is to be
/// trimmed.</remarks>
function InternalTrim(const Str: UnicodeString;
const TrimFn: TFunc<Char,Boolean>): UnicodeString;
var
TextStart: Integer; // position of start of non-space text
TextEnd: Integer; // position of end of non-space text
begin
TextEnd := Length(Str);
TextStart := 1;
while (TextStart <= TextEnd)
and IsTrimmableChar(Str, TextStart, TrimFn) do
Inc(TextStart);
if TextStart > TextEnd then
Exit('');
while IsTrimmableChar(Str, TextEnd, TrimFn) do
Dec(TextEnd);
Result := Copy(Str, TextStart, TextEnd - TextStart + 1);
end;
/// <summary>Trims characters from the left hand end of a string.</summary>
/// <remarks>Anonymous function TrimFn determines whether a character is to be
/// trimmed.</remarks>
function InternalTrimLeft(const Str: UnicodeString;
const TrimFn: TFunc<Char,Boolean>): UnicodeString;
var
TextStart: Integer; // position of start of non-space text
begin
TextStart := 1;
while (TextStart <= Length(Str))
and IsTrimmableChar(Str, TextStart, TrimFn) do
Inc(TextStart);
Result := Copy(Str, TextStart, MaxInt);
end;
/// <summary>Trims characters from the right hand end of a string.</summary>
/// <remarks>Anonymous function TrimFn determines whether a character is to be
/// trimmed.</remarks>
function InternalTrimRight(const Str: UnicodeString;
const TrimFn: TFunc<Char,Boolean>): UnicodeString;
var
TextEnd: Integer; // position of end of non-space text
begin
TextEnd := Length(Str);
while (TextEnd > 0) and IsTrimmableChar(Str, TextEnd, TrimFn) do
Dec(TextEnd);
Result := Copy(Str, 1, TextEnd);
end;
{ Public routines }
function StrCapitaliseWords(const Str: UnicodeString): UnicodeString;
var
Idx: Integer; // loops through each character in string
WantCapital: Boolean; // flag indicating whether captial letter required
begin
Result := Str;
WantCapital := True;
for Idx := 1 to Length(Str) do
begin
if TCharacter.IsLetter(Result[Idx]) then
begin
if WantCapital then
Result[Idx] := TCharacter.ToUpper(Result[Idx]);
WantCapital := False;
end
else
WantCapital := TCharacter.IsWhiteSpace(Result[Idx]);
end;
end;
function StrCompareText(const Left, Right: UnicodeString): Integer;
begin
Result := SysUtils.AnsiCompareText(Left, Right);
end;
function StrCompressWhiteSpace(const Str: UnicodeString): UnicodeString;
var
Idx: Integer; // loops thru all characters in string
ResCount: Integer; // counts number of characters in result string
PRes: PChar; // pointer to characters in result string
begin
// Set length of result to length of source string and set pointer to it
SetLength(Result, Length(Str));
PRes := PChar(Result);
// Reset count of characters in result string
ResCount := 0;
// Loop thru characters of source string
Idx := 1;
while Idx <= Length(Str) do
begin
if TCharacter.IsWhiteSpace(Str[Idx]) then
begin
// Current char is white space: replace by space char and count it
PRes^ := ' ';
Inc(PRes);
Inc(ResCount);
// Skip past any following white space
Inc(Idx);
while TCharacter.IsWhiteSpace(Str[Idx]) do
Inc(Idx);
end
else
begin
// Current char is not white space: copy it literally and count it
PRes^ := Str[Idx];
Inc(PRes);
Inc(ResCount);
Inc(Idx);
end;
end;
// Reduce length of result string if it is shorter than source string
if ResCount < Length(Str) then
SetLength(Result, ResCount);
end;
function StrContainsStr(const Needle, Haystack: UnicodeString): Boolean;
begin
Result := StrUtils.AnsiContainsStr(Haystack, Needle);
end;
function StrContainsText(const Needle, Haystack: UnicodeString): Boolean;
begin
Result := StrUtils.AnsiContainsText(Haystack, Needle);
end;
function StrMatchText(const Str: string; const Values: array of string):
Boolean;
begin
Result := StrUtils.MatchText(Str, Values);
end;
function StrContainsWhiteSpace(const Str: UnicodeString): Boolean;
var
Ch: Char; // scans through Str
begin
Result := False;
for Ch in Str do
if TCharacter.IsWhiteSpace(Ch) then
Exit(True);
end;
function StrCountDelims(const Delims, Str: UnicodeString): Integer;
var
Idx: Integer; //loops thru all characters in string
begin
Result := 0;
for Idx := 1 to Length(Str) do
if StrIsDelimiter(Delims, Str, Idx) then
Inc(Result);
end;
function StrExplode(Str: UnicodeString; const Delim: UnicodeString;
const List: TStrings; const AllowEmpty: Boolean = True;
const TrimStrs: Boolean = False): Integer;
var
Head: UnicodeString; // head of Str before delimiter
Remainder: UnicodeString; // remaining unconsumed part of string
// ---------------------------------------------------------------------------
procedure ProcessHead;
{Modifies Head string as necessary and adds to list if required.
}
begin
if TrimStrs then
Head := StrTrim(Head);
if (Head <> '') or AllowEmpty then
List.Add(Head)
end;
// ---------------------------------------------------------------------------
begin
// Clear the list
List.Clear;
// Check we have some entries in the string
if Str <> '' then
begin
// Repeatedly split string until we have no more entries
while StrSplit(Str, Delim, Head, Remainder) do
begin
ProcessHead;
// Go round again with remainder of string
Str := Remainder;
end;
// Deal with Head after last delimiter, if any
ProcessHead;
end;
Result := List.Count;
end;
function StrIsDelimiter(const Delims, Str: UnicodeString; const Idx: Integer):
Boolean;
begin
Result := SysUtils.IsDelimiter(Delims, Str, Idx);
end;
function StrJoin(const SL: TStrings; const Delim: UnicodeString;
const AllowEmpty: Boolean = True): UnicodeString;
var
Idx: Integer; // loops thru all items in string list
FirstItem: Boolean; // flag true until first item has been added to result
begin
Result := '';
FirstItem := True;
for Idx := 0 to Pred(SL.Count) do
begin
if (SL[Idx] <> '') or AllowEmpty then
begin
if FirstItem then
begin
Result := SL[Idx];
FirstItem := False;
end
else
Result := Result + Delim + SL[Idx];
end;
end;
end;
function StrLastDelimiterPos(const Delims, Str: UnicodeString): Integer;
begin
Result := SysUtils.LastDelimiter(Delims, Str);
end;
function StrLastPos(const Needle, Haystack: UnicodeString): Integer;
var
Idx: Integer; // an index of Needle in Haystack
begin
Result := 0;
Idx := StrPos(Needle, Haystack);
if Idx = 0 then
Exit;
while Idx > 0 do
begin
Result := Idx;
Idx := StrPos(Needle, Haystack, Idx + 1);
end;
end;
function StrMakeSentence(const Str: UnicodeString): UnicodeString;
resourcestring
// characters that can close sentence
sSentenceClosers = '.!?';
sFullStop = '.';
var
Sentence: UnicodeString; // sentence text without trailing spaces
TrailingWhiteSpace: UnicodeString; // records trailing white space from Str
begin
Sentence := StrTrimRight(Str);
TrailingWhiteSpace := StrSliceRight(Str, Length(Str) - Length(Sentence));
if Sentence = '' then
Exit(TrailingWhiteSpace);
if not StrIsDelimiter(sSentenceClosers, Sentence, Length(Sentence)) then
Sentence := Sentence + sFullStop;
Result := Sentence + TrailingWhiteSpace;
end;
function StrPos(const Needle, Haystack: UnicodeString): Integer;
begin
Result := SysUtils.AnsiPos(Needle, Haystack);
end;
function StrPos(const Needle, Haystack: UnicodeString; const Offset: Integer):
Integer;
begin
Result := StrUtils.PosEx(Needle, Haystack, Offset);
end;
function StrQuoteSpaced(const Str: UnicodeString;
const Quote: Char = '"'): UnicodeString;
begin
if StrContainsWhiteSpace(Str) then
Result := Quote + Str + Quote
else
Result := Str;
end;
function StrReplace(const Str, FindStr, ReplaceStr: UnicodeString):
UnicodeString;
begin
Result := StrUtils.AnsiReplaceStr(Str, FindStr, ReplaceStr);
end;
function StrSameStr(const Left, Right: UnicodeString): Boolean;
begin
Result := SysUtils.AnsiSameStr(Left, Right);
end;
function StrSameText(const Left, Right: UnicodeString): Boolean;
begin
Result := SysUtils.AnsiSameText(Left, Right);
end;
function StrSlice(const Str: UnicodeString; const StartIdx, Count: Integer):
UnicodeString;
begin
Result := StrUtils.AnsiMidStr(Str, StartIdx, Count);
end;
function StrSliceLeft(const Str: UnicodeString; const Count: Integer):
UnicodeString;
begin
Result := StrUtils.AnsiLeftStr(Str, Count);
end;
function StrSliceRight(const Str: UnicodeString; const Count: Integer):
UnicodeString;
begin
Result := StrUtils.AnsiRightStr(Str, Count);
end;
function StrSplit(const Str: UnicodeString; const Delim: UnicodeString;
out Left, Right: UnicodeString): Boolean;
var
DelimPos: Integer; // position of delimiter in source string
begin
// Find position of first occurence of delimiter in string
DelimPos := StrPos(Delim, Str);
if DelimPos > 0 then
begin
// Delimiter found: split string at delimiter
Left := Copy(Str, 1, DelimPos - 1);
Right := Copy(Str, DelimPos + Length(Delim), MaxInt);
Result := True;
end
else
begin
// Delimiter not found: set Left to whole string
Left := Str;
Right := '';
Result := False;
end;
end;
function StrPop(var Str: string; const Delim: string): string;
var
Right: string; // part of Str to right of Delim
begin
StrSplit(Str, Delim, Result, Right);
Str := Right;
end;
function StrStartsStr(const SubStr, Str: UnicodeString): Boolean;
begin
if SubStr = '' then
Exit(False);
Result := StrUtils.AnsiStartsStr(SubStr, Str);
end;
function StrStartsText(const SubStr, Str: UnicodeString): Boolean;
begin
if SubStr = '' then
Exit(False);
Result := StrUtils.AnsiStartsText(SubStr, Str);
end;
function StrStripWhiteSpace(const Str: UnicodeString): UnicodeString;
var
Idx: Integer; // loops thru all characters in string
ResCount: Integer; // counts number of characters in result string
PRes: PChar; // pointer to characters in result string
begin
// Set length of result to length of source string and set pointer to it
SetLength(Result, Length(Str));
PRes := PChar(Result);
// Reset count of characters in result string
ResCount := 0;
// Loop thru characters of source string
Idx := 1;
while Idx <= Length(Str) do
begin
if not TCharacter.IsWhiteSpace(Str[Idx]) then
begin
// Character is not white space: copy to result string
PRes^ := Str[Idx];
Inc(ResCount);
Inc(PRes);
end;
Inc(Idx);
end;
// Reduce length of result string if it is shorter than source string
if ResCount < Length(Str) then
SetLength(Result, ResCount);
end;
function StrToLower(const Str: UnicodeString): UnicodeString;
begin
Result := SysUtils.AnsiLowerCase(Str);
end;
function StrToUpper(const Str: UnicodeString): UnicodeString;
begin
Result := SysUtils.AnsiUpperCase(Str);
end;
function StrTrim(const Str: UnicodeString): UnicodeString;
begin
Result := InternalTrim(Str, TCharacter.IsWhiteSpace);
end;
function StrTrimChars(const Str: UnicodeString; const C: Char): UnicodeString;
begin
Result := InternalTrim(
Str,
function (Ch: Char): Boolean begin Result := Ch = C; end
);
end;
function StrTrimLeft(const Str: UnicodeString): UnicodeString;
begin
Result := InternalTrimLeft(Str, TCharacter.IsWhiteSpace);
end;
function StrTrimLeftChars(const Str: UnicodeString; const C: Char):
UnicodeString;
begin
Result := InternalTrimLeft(
Str,
function (Ch: Char): Boolean begin Result := Ch = C; end
);
end;
function StrTrimRight(const Str: UnicodeString): UnicodeString;
begin
Result := InternalTrimRight(Str, TCharacter.IsWhiteSpace);
end;
function StrTrimRightChars(const Str: UnicodeString; const C: Char):
UnicodeString; overload;
begin
Result := InternalTrimRight(
Str,
function (Ch: Char): Boolean begin Result := Ch = C; end
);
end;
function StrUnixLineBreaks(const Str: UnicodeString): UnicodeString;
begin
// Replace any CRLF (MSDOS/Windows) line ends with LF
Result := StrReplace(Str, CRLF, LF);
// Replace any remaining CR (Mac) line ends with LF
Result := StrReplace(Result, CR, LF);
end;
function StrWindowsLineBreaks(const Str: UnicodeString): UnicodeString;
begin
// First convert to Unix to get rid of all CR characters (CRs could come from
// existing Windows CRLF or from Max CR)
Result := StrUnixLineBreaks(Str);
// Now have only LFs - convert them all to CRLFs
Result := StrReplace(Result, LF, CRLF);
end;
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: Integer):
UnicodeString;
var
Word: UnicodeString; // next word in input Str
Line: UnicodeString; // current output line
Words: TStringList; // list of words in input Str
I: Integer; // loops thru all words in input Str
// -------------------------------------------------------------------------
/// Adds a line of text to output, offseting line by Margin spaces
procedure AddLine(const Line: string);
begin
if Result <> '' then // not first line: insert new line
Result := Result + EOL;
Result := Result + StringOfChar(' ', Margin) + Line;
end;
// -------------------------------------------------------------------------
begin
// Get all words in Str
Words := TStringList.Create;
try
StrExplode(Str, ' ', Words);
Result := '';
Line := '';
// Loop for each word in Str
for I := 0 to Pred(Words.Count) do
begin
Word := Words[I];
if Length(Line) + Length(Word) + 1 <= MaxLen then
begin
// Word fits on current line: add it
if Line = '' then
Line := Word // 1st word on line
else
Line := Line + ' ' + Word;
end
else
begin
// Word doesn't fit on line
AddLine(Line);
Line := Word; // store word as first on next line
end;
end;
if Line <> '' then
// Residual line after end of loop: add to output
AddLine(Line);
finally
Words.Free;
end;
end;
function StrWrap(const Paras: TStrings; const MaxLineLen, Margin: Integer;
const SeparateParas: Boolean): UnicodeString; overload;
var
Para: string;
SB: TStringBuilder;
begin
SB := TStringBuilder.Create;
try
for Para in Paras do
begin
SB.AppendLine(StrTrimRight(StrWrap(Para, MaxLineLen, Margin)));
if SeparateParas then
SB.AppendLine;
end;
Result := SB.ToString;
finally
SB.Free;
end;
end;
function StrIf(const Condition: Boolean; const TrueStr, FalseStr: string):
string;
begin
if Condition then
Result := TrueStr
else
Result := FalseStr;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.