1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Text;
interface
uses
Boolean,
Byte,
Byte.Array_.Stream.Input,
Character,
Character.Encoding,
Character.String_,
Enumerator,
Integer._32,
Integer._32.Positive,
Iterator,
Memory.Pointer,
Stream.Input,
Stream.Output;
type
// TODO: TText currently is in a state of refactoring to reach the final state where Unicode is fully supported and TText.Encoding
// always correctly indicates the character encoding used in the TText contents. Currently quite some code still does not take
// this TText.Encoding into account and assumes it to be Windows-1252. Moreover, some functions are still byte oriented.
TTextCursor = class;
PText = ^TText;
TText = record
private
Value: TArray<TByte>;
FEncoding: TCharacterEncoding;
FCount: TInteger32;
function GetCharacter(Index: TInteger32): TAnsiCharacter;
procedure SetCharacter(Index: TInteger32; const Value: TAnsiCharacter);
function GetCodePoint(Index: TInteger32): TCharacterCodePoint;
function GetSize(): TInteger32;
function GetAddress(): TPointer;
function CalculateActualCount(): TInteger32; deprecated;
private
const
FirstCharacterIndex = {$IF Defined(NEXTGEN)}0{$ELSE}1{$ENDIF};
public
class function New(Encoding: TCharacterEncoding): TText; overload; static;
class function New(Encoding: TCharacterEncoding; Contents: PByte; Size: TPositiveInteger32): TText; overload; static;
class function New(Value: PAnsiCharacter): TText; overload; static;
class function New(Encoding: TCharacterEncoding; const Bytes: TArray<TByte>): TText; overload; static;
class function NewOfCharacter(Encoding: TCharacterEncoding; Character: TCharacterCodePoint; Count: TInteger32): TText; static;
class function NewRepeated(const Value: TText; Count: TInteger32): TText; static;
{$IF not Defined(NEXTGEN)}
class operator Implicit(const Operand: TAnsiString): TText;
class operator Implicit(const Operand: TWideString): TText;
class operator Implicit(const Operand: TText): TAnsiString;
class operator Implicit(const Operand: TText): TWideString;
{$ENDIF}
{$IF not Defined(CHARTA_PASCAL)}
class operator Implicit(const Operand: TUnicodeString): TText;
class operator Implicit(const Operand: TText): TUnicodeString;
{$ENDIF}
class operator Add(const Left: TText; const Right: TText): TText;
class operator Equal(const Left: TText; const Right: TText): TBoolean;
class operator NotEqual(const Left: TText; const Right: TText): TBoolean;
class operator GreaterThan(const Left: TText; const Right: TText): TBoolean;
class operator LessThan(const Left: TText; const Right: TText): TBoolean;
public
function IndexOf(const Subtext: TText; StartPosition: TInteger32 = 0): TInteger32;
function LastIndexOf(const Subtext: TText): TInteger32;
function IndexOfNot(const Characters: TCharacterSet; StartPosition: TInteger32 = 0): TInteger32;
function Contains(const Subtext: TText): TBoolean;
function IndexOfCharacter(Character: TCharacterCodePoint): TInteger32;
function ContainsCharacter(Character: TCharacterCodePoint): TBoolean;
function StartsWith(const Subtext: TText): TBoolean;
function EndsWith(const Subtext: TText): TBoolean;
function SubtextEquals(Position: TInteger32; const SubText: TText): TBoolean;
function Subtext(Position: TInteger32; Count: TInteger32): TText;
function SubtextTo(Position: TInteger32): TText;
function SubtextFrom(Position: TInteger32): TText;
function SubtextFromTo(FromPosition: TInteger32; ToPosition: TInteger32): TText;
function SubtextWithoutRight(Count: TInteger32): TText;
function Left(Count: TInteger32): TText;
function Right(Count: TInteger32): TText;
function PadLeft(PadCharacter: TCharacter; TargetCount: TInteger32): TText;
function PadRight(PadCharacter: TCharacter; TargetCount: TInteger32): TText;
function DetermineLineBreakOff(FirstCharacterIndex: TInteger32; LastCharacterIndex: TInteger32): TInteger32;
function Parse(const Delimiter: TText): TText;
function ParseOptional(const Delimiter: TText): TText;
function RemoveFromStart(Count: TInteger32): TText;
function ToUpperCase(): TText;
function ToLowerCase(): TText;
function Trim(): TText;
function ToByteArray(): TArray<TByte>;
function Replace(const Source: TText; const Target: TText): TText;
function CreateInputStream(): TInputStream;
procedure Write(Stream: TOutputStream); overload;
procedure Write(TargetEncoding: TCharacterEncoding; Stream: TOutputStream); overload;
procedure WriteNullTerminated(Stream: TOutputStream);
procedure ReadNullTerminated(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
procedure ReadNullTerminatedOrTillEnd(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
procedure Read(SourceEncoding: TCharacterEncoding; Stream: TInputStream; Count: TInteger32);
procedure ReadTillEnd(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
function CreateIterator(): TIterator<TCharacterCodePoint>;
function CreateCursor(): TTextCursor;
function GetEnumerator(): TEnumerator<TCharacterCodePoint>;
function Recode(Target: TCharacterEncoding): TText;
procedure CopyToNullTerminated(Destination: PAnsiCharacter);
property Count: TInteger32 read FCount;
property Size: TInteger32 read GetSize;
property Address: TPointer read GetAddress;
property Encoding: TCharacterEncoding read FEncoding;
// TODO: The implementation of Character[] should be replaced by the implementation of CodePoint[]
property Character[Index: TInteger32]: TAnsiCharacter read GetCharacter write SetCharacter; default;
property CodePoint[Index: TInteger32]: TCharacterCodePoint read GetCodePoint;
public
const
CarriageReturnLineFeed = #13#10;
LineFeed = #10;
class procedure AssureInitialized(); static;
end;
TTextCursor = class(TObject)
private
function GetAtEnd(): TBoolean;
protected
Value: TText;
ByteStream: TByteArrayInputStream;
public
constructor Create(Value: TText); virtual;
destructor Destroy(); override;
procedure MoveTo(Cursor: TTextCursor);
function Copy(): TTextCursor;
procedure Skip(Count: TInteger32);
function Read(): TCharacterCodePoint; overload;
function Read(Count: TInteger32): TText; overload;
property AtEnd: TBoolean read GetAtEnd;
end;
implementation
uses
Character.Iterator,
Character.Set_.Unicode,
Enumerator.Iterator,
Exception,
Exception.IndexOutOfBounds,
Mathematics,
Text.Collation,
Text.Stream.Output;
{ TText }
class operator TText.Add(const Left: TText; const Right: TText): TText;
var
Target: TTextOutputStream;
Encoding: TCharacterEncoding;
begin
if Left.Count = 0 then
Result := Right
else if Right.Count = 0 then
Result := Left
else
begin
if Left.Encoding.Equal(Right.Encoding) and Left.Encoding.IsConcatenatable() then
begin
Result.FEncoding := Left.Encoding;
Result.FCount := Left.Count + Right.Count;
SetLength(Result.Value, Length(Left.Value) + Length(Right.Value));
Move(Left.Value[0], Result.Value[0], Length(Left.Value));
Move(Right.Value[0], Result.Value[Length(Left.Value)], Length(Right.Value));
end
else
begin
Encoding := TCharacterEncoding.Utf8;
Target := TTextOutputStream.Create(Encoding, Left.Size + Right.Size);
Left.Write(Encoding, Target);
Right.Write(Encoding, Target);
Result := Target.GetValue();
Target.Free();
end;
end;
end;
function TText.GetAddress(): TPointer;
begin
if Count > 0 then
Result := @Value[0]
else
Result := nil;
end;
function TText.GetCharacter(Index: TInteger32): TAnsiCharacter;
var
CodePoint: TCharacterCodePoint;
begin
if Encoding.Equal(TCharacterEncoding.Windows1252) then
begin
if (Index >= 0) and (Index < Count) then
Result := TAnsiCharacter(Value[Index])
else
raise EIndexOutOfBounds.Create(Index);
end
else
begin
CodePoint := Self.CodePoint[Index];
if CodePoint < 128 then
Result := TAnsiCharacter(CodePoint)
else
raise EException.Create('Cannot convert code point to character');
end;
end;
function TText.GetCodePoint(Index: TInteger32): TCharacterCodePoint;
var
Cursor: TTextCursor;
i: TInteger32;
begin
if (Index >= 0) and (Index < Count) then
begin
Cursor := CreateCursor();
Result := $FFFFFFFF;
for i := 0 to Index do
Result := Cursor.Read();
Cursor.Free();
end
else
raise EIndexOutOfBounds.Create(Index);
end;
function TText.GetEnumerator(): TEnumerator<TCharacterCodePoint>;
begin
Result := TIteratorEnumerator<TCharacterCodePoint>.Create(CreateIterator());
end;
function TText.GetSize(): TInteger32;
begin
Result := Length(Value);
end;
class operator TText.GreaterThan(const Left: TText; const Right: TText): TBoolean;
begin
Result := TTextCollation.DefaultCaseSensitive.GreaterThan(Left, Right);
end;
function TText.CalculateActualCount(): TInteger32;
var
Stream: TInputStream;
begin
Result := 0;
Stream := CreateInputStream();
while not Stream.AtEnd() do
begin
Encoding.Decode(Stream);
Inc(Result);
end;
Stream.Free();
end;
{$IF not Defined(NEXTGEN)}
class operator TText.Implicit(const Operand: TAnsiString): TText;
begin
Result.FEncoding := TCharacterEncoding.Windows1252; // TODO: We should use the current Windows code page instead
Result.FCount := Length(Operand);
SetLength(Result.Value, Result.Count);
if Result.Count > 0 then
Move(Operand[FirstCharacterIndex], Result.Value[0], Result.Count);
end;
class operator TText.Implicit(const Operand: TText): TAnsiString;
var
AnsiValue: TText;
begin
if (Operand.Encoding <> nil) and (Operand.Encoding <> TCharacterEncoding.Windows1252) then
AnsiValue := Operand.Recode(TCharacterEncoding.Windows1252) // TODO: We should use the current Windows code page instead
else
AnsiValue := Operand;
SetLength(Result, AnsiValue.Count);
if AnsiValue.Count > 0 then
Move(AnsiValue.Value[0], Result[FirstCharacterIndex], AnsiValue.Count);
end;
class operator TText.Implicit(const Operand: TText): TWideString;
var
Utf16Le: TText;
begin
if (Operand.Encoding <> nil) and (Operand.Encoding <> TCharacterEncoding.Utf16LittleEndian) then
Utf16Le := Operand.Recode(TCharacterEncoding.Utf16LittleEndian)
else
Utf16Le := Operand;
SetLength(Result, Utf16Le.Size div 2);
if Utf16Le.Count > 0 then
Move(Utf16Le.Value[0], Result[FirstCharacterIndex], Utf16Le.Size);
end;
class operator TText.Implicit(const Operand: TWideString): TText;
begin
Result.FEncoding := TCharacterEncoding.Utf16LittleEndian;
Result.FCount := Length(Operand);
SetLength(Result.Value, Result.Count * 2);
if Result.Count > 0 then
Move(Operand[FirstCharacterIndex], Result.Value[0], Result.Size);
Result := Result.Recode(TCharacterEncoding.Utf8);
end;
{$ENDIF}
{$IF not Defined(CHARTA_PASCAL)}
class operator TText.Implicit(const Operand: TUnicodeString): TText;
begin
Result.FEncoding := TCharacterEncoding.Utf16LittleEndian;
Result.FCount := Length(Operand);
SetLength(Result.Value, Result.Count * 2);
if Result.Count > 0 then
Move(Operand[FirstCharacterIndex], Result.Value[0], Result.Size);
Result := Result.Recode(TCharacterEncoding.Utf8);
end;
class operator TText.Implicit(const Operand: TText): TUnicodeString;
var
Utf16Le: TText;
begin
if (Operand.Encoding <> nil) and (Operand.Encoding <> TCharacterEncoding.Utf16LittleEndian) then
Utf16Le := Operand.Recode(TCharacterEncoding.Utf16LittleEndian)
else
Utf16Le := Operand;
SetLength(Result, Utf16Le.Size div 2);
if Utf16Le.Count > 0 then
Move(Utf16Le.Value[0], Result[FirstCharacterIndex], Utf16Le.Size);
end;
{$ENDIF}
function TText.IndexOf(const Subtext: TText; StartPosition: TInteger32 = 0): TInteger32;
var
Index: TInteger32;
LastIndex: TInteger32;
begin
// TODO: This is a very simple search procedure and be improved upon by using something like the Boyer-More algorithm
Result := -1;
Index := StartPosition;
LastIndex := Count - Subtext.Count;
while (Result = -1) and (Index <= LastIndex) do
begin
if SubtextEquals(Index, Subtext) then
Result := Index
else
Inc(Index);
end;
end;
function TText.IndexOfCharacter(Character: TCharacterCodePoint): TInteger32;
var
Input: TTextCursor;
Index: TInteger32;
begin
Input := CreateCursor();
Result := -1;
Index := 0;
while (Result = -1) and not Input.AtEnd do
begin
if Input.Read() = Character then
Result := Index
else
Inc(Index);
end;
Input.Free();
end;
function TText.IndexOfNot(const Characters: TCharacterSet; StartPosition: TInteger32 = 0): TInteger32;
var
Index: TInteger32;
begin
Result := -1;
Index := StartPosition;
while (Result = -1) and (Index < Count) do
begin
if not (Self[Index] in Characters) then
Result := Index
else
Inc(Index);
end;
end;
function TText.LastIndexOf(const Subtext: TText): TInteger32;
begin
Result := Maximum(-1, Count - Subtext.Count);
while (Result >= 0) and (Self.Subtext(Result, Subtext.Count) <> Subtext) do
Dec(Result);
end;
class function TText.New(Encoding: TCharacterEncoding; Contents: PByte; Size: TPositiveInteger32): TText;
begin
SetLength(Result.Value, Size);
if Size > 0 then
Move(Contents^, Result.Value[0], Size);
Result.FEncoding := Encoding;
Result.FCount := Result.CalculateActualCount();
end;
class function TText.New(Value: PAnsiCharacter): TText;
var
Length: TInteger32;
begin
Length := 0;
while Value[Length] <> #0 do
Inc(Length);
SetLength(Result.Value, Length);
if Length > 0 then
Move(Value^, Result.Value[0], Length);
Result.FEncoding := TCharacterEncoding.Windows1252;
Result.FCount := Length;
end;
class function TText.New(Encoding: TCharacterEncoding; const Bytes: TArray<TByte>): TText;
begin
SetLength(Result.Value, Length(Bytes));
if Length(Bytes) > 0 then
Move(Bytes[0], Result.Value[0], Length(Bytes));
Result.FEncoding := Encoding;
Result.FCount := Result.CalculateActualCount();
end;
class function TText.NewOfCharacter(Encoding: TCharacterEncoding; Character: TCharacterCodePoint; Count: TInteger32): TText;
var
i: TInteger32;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, Count);
for i := 0 to Count - 1 do
Encoding.Encode(Target, Character);
Result := Target.GetValue();
Target.Free();
end;
class function TText.New(Encoding: TCharacterEncoding): TText;
begin
Result.FEncoding := Encoding;
Result.FCount := 0;
Result.Value := nil;
end;
class function TText.NewRepeated(const Value: TText; Count: TInteger32): TText;
var
i: TInteger32;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Value.Encoding, Count * Value.Size);
for i := 1 to Count do
Value.Write(Target);
Result := Target.GetValue();
Target.Free();
end;
class operator TText.NotEqual(const Left: TText; const Right: TText): TBoolean;
begin
Result := not TTextCollation.DefaultCaseSensitive.Equal(Left, Right);
end;
function TText.PadLeft(PadCharacter: TCharacter; TargetCount: TInteger32): TText;
begin
Result := Self;
if Result.Count < TargetCount then
Result := TText.NewRepeated(PadCharacter, TargetCount - Count) + Self;
end;
function TText.PadRight(PadCharacter: TCharacter; TargetCount: TInteger32): TText;
begin
Result := Self;
if Result.Count < TargetCount then
Result := Self + TText.NewRepeated(PadCharacter, TargetCount - Count);
end;
function TText.Parse(const Delimiter: TText): TText;
var
Index: TInteger32;
begin
Index := IndexOf(Delimiter);
if Index >= 0 then
begin
Result := Subtext(0, Index);
Self := Subtext(Index + Delimiter.Count, Count - Delimiter.Count - Index);
end
else
begin
Result := Self;
Self := '';
end;
end;
function TText.ParseOptional(const Delimiter: TText): TText;
var
Index: TInteger32;
begin
Index := IndexOf(Delimiter);
if Index >= 0 then
begin
Result := Subtext(0, Index);
Self := Subtext(Index + Delimiter.Count, Count - Delimiter.Count - Index);
end
else
Result := '';
end;
function TText.SubtextWithoutRight(Count: TInteger32): TText;
begin
Result := SubtextTo(Self.Count - Count - 1);
end;
procedure TText.Read(SourceEncoding: TCharacterEncoding; Stream: TInputStream; Count: TInteger32);
var
i: TInteger32;
CodePoint: TCharacterCodePoint;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, Count);
for i := 1 to Count do
begin
CodePoint := SourceEncoding.Decode(Stream);
Encoding.Encode(Target, CodePoint);
end;
Self := Target.GetValue();
Target.Free();
end;
function TText.Recode(Target: TCharacterEncoding): TText;
var
InputStream: TInputStream;
OutputStream: TTextOutputStream;
begin
if Encoding = Target then
Result := Self
else
begin
InputStream := CreateInputStream();
OutputStream := TTextOutputStream.Create(Target, Size);
while not InputStream.AtEnd() do
Target.Encode(OutputStream, Encoding.Decode(InputStream));
Result := OutputStream.GetValue();
OutputStream.Free();
InputStream.Free();
end;
end;
procedure TText.ReadNullTerminated(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
var
CodePoint: TCharacterCodePoint;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, 16);
CodePoint := SourceEncoding.Decode(Stream);
while CodePoint <> TUnicode.NULL do
begin
Encoding.Encode(Target, CodePoint);
CodePoint := SourceEncoding.Decode(Stream);
end;
Self := Target.GetValue();
Target.Free();
end;
procedure TText.ReadNullTerminatedOrTillEnd(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
var
CodePoint: TCharacterCodePoint;
Target: TTextOutputStream;
NullFound: TBoolean;
begin
Target := TTextOutputStream.Create(Encoding, 16);
NullFound := False;
while not NullFound and not Stream.AtEnd() do
begin
CodePoint := SourceEncoding.Decode(Stream);
NullFound := CodePoint = TUnicode.NULL;
if not NullFound then
Encoding.Encode(Target, CodePoint);
end;
Self := Target.GetValue();
Target.Free();
end;
procedure TText.ReadTillEnd(SourceEncoding: TCharacterEncoding; Stream: TInputStream);
var
CodePoint: TCharacterCodePoint;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, Stream.Available());
while not Stream.AtEnd() do
begin
CodePoint := SourceEncoding.Decode(Stream);
Encoding.Encode(Target, CodePoint);
end;
Self := Target.GetValue();
Target.Free();
end;
function TText.RemoveFromStart(Count: TInteger32): TText;
begin
Result := SubtextTo(Count - 1);
Self := SubtextFrom(Count);
end;
function TText.Replace(const Source: TText; const Target: TText): TText;
var
Low: TInteger32;
High: TInteger32;
begin
Result := '';
Low := 0;
repeat
High := IndexOf(Source, Low);
if High <> -1 then
begin
Result := Result + SubtextFromTo(Low, High - 1) + Target;
Low := High + Source.Count;
end;
until High = -1;
Result := Result + SubtextFrom(Low);
end;
function TText.Right(Count: TInteger32): TText;
begin
Result := SubtextFrom(Self.Count - Count);
end;
class procedure TText.AssureInitialized();
begin
TCharacterEncoding.AssureInitialized();
TTextCollation.AssureInitialized();
end;
function TText.Contains(const Subtext: TText): TBoolean;
begin
Result := IndexOf(Subtext) <> -1;
end;
function TText.ContainsCharacter(Character: TCharacterCodePoint): TBoolean;
begin
Result := IndexOfCharacter(Character) <> -1;
end;
procedure TText.CopyToNullTerminated(Destination: PAnsiCharacter);
begin
if Size > 0 then
Move(Value[0], Destination^, Size);
Destination[Size] := #0;
end;
function TText.CreateCursor(): TTextCursor;
begin
Result := TTextCursor.Create(Self);
end;
function TText.CreateInputStream(): TInputStream;
begin
Result := TByteArrayInputStream.Create(Value);
end;
function TText.CreateIterator(): TIterator<TCharacterCodePoint>;
var
Iterator: TCharacterIterator;
begin
Iterator := TCharacterIterator.Create(CreateInputStream(), Encoding);
Iterator.OwnsStream := True;
Result := Iterator;
end;
{
This function will try to find a point to break off a line given a maximum character index.
Currently walks back until it finds a space or the FirstCharacterIndex, and returns the index
before the space, meaning the last index that should be included in the line.
If no space can be found before the FirstCharacterIndex the maximum -1 is returned.
If a maximum greater or equal to the length of the text is given, the last valid index of the text is returned.
}
function TText.DetermineLineBreakOff(FirstCharacterIndex: TInteger32; LastCharacterIndex: TInteger32): TInteger32;
var
Index: TInteger32;
begin
if LastCharacterIndex >= Count then
Result := Count - 1
else
begin
Index := LastCharacterIndex;
while (Subtext(Index, 1) <> ' ') and (Index > FirstCharacterIndex) do
Dec(Index);
Result := Index - 1;
if Result <= FirstCharacterIndex then
Result := LastCharacterIndex - 1;
end;
end;
function TText.EndsWith(const Subtext: TText): TBoolean;
begin
Result := (Count >= Subtext.Count) and SubtextEquals(Count - Subtext.Count, Subtext);
end;
class operator TText.Equal(const Left: TText; const Right: TText): TBoolean;
begin
Result := TTextCollation.DefaultCaseSensitive.Equal(Left, Right);
end;
procedure TText.SetCharacter(Index: TInteger32; const Value: TAnsiCharacter);
var
Source: TTextCursor;
Target: TTextOutputStream;
i: TInteger32;
begin
if (Index >= 0) and (Index < Count) then
begin
Source := CreateCursor();
Target := TTextOutputStream.Create(Encoding, Size);
for i := 0 to Index - 1 do
Encoding.Encode(Target, Source.Read());
Source.Read();
Encoding.Encode(Target, Ord(Value));
for i := Index + 1 to Count - 1 do
Encoding.Encode(Target, Source.Read());
Self := Target.GetValue();
Source.Free();
Target.Free();
end
else
raise EIndexOutOfBounds.Create(Index);
end;
function TText.Left(Count: TInteger32): TText;
begin
Result := Subtext(0, Count);
end;
class operator TText.LessThan(const Left: TText; const Right: TText): TBoolean;
begin
Result := TTextCollation.DefaultCaseSensitive.LessThan(Left, Right);
end;
function TText.StartsWith(const Subtext: TText): TBoolean;
begin
Result := SubtextEquals(0, Subtext);
end;
function TText.Subtext(Position: TInteger32; Count: TInteger32): TText;
var
Characters: TTextCursor;
i: TInteger32;
OutputStream: TTextOutputStream;
begin
Characters := CreateCursor();
for i := 1 to Position do
Characters.Read();
OutputStream := TTextOutputStream.Create(Encoding, Count);
for i := 1 to Count do
Encoding.Encode(OutputStream, Characters.Read());
Result := OutputStream.GetValue();
OutputStream.Free();
Characters.Free();
end;
function TText.SubtextEquals(Position: TInteger32; const Subtext: TText): TBoolean;
var
Index: TInteger32;
Count: TInteger32;
Source: TTextCursor;
Target: TTextCursor;
i: TInteger32;
SourceCodePoint: TCharacterCodePoint;
TargetCodePoint: TCharacterCodePoint;
begin
Count := Subtext.Count;
Result := Position + Count <= Self.Count;
if Result then
begin
Target := Subtext.CreateCursor();
Source := CreateCursor();
for i := 0 to Position - 1 do
Source.Read();
Index := 0;
while Result and (Index < Count) do
begin
SourceCodePoint := Source.Read();
TargetCodePoint := Target.Read();
Result := SourceCodePoint = TargetCodePoint;
Inc(Index);
end;
Source.Free();
Target.Free();
end;
end;
function TText.SubtextFrom(Position: TInteger32): TText;
begin
Result := Subtext(Position, Count - Position);
end;
function TText.SubtextFromTo(FromPosition: TInteger32; ToPosition: TInteger32): TText;
begin
Result := Subtext(FromPosition, ToPosition - FromPosition + 1);
end;
function TText.SubtextTo(Position: TInteger32): TText;
begin
Result := Subtext(0, Position + 1);
end;
function TText.ToByteArray(): TArray<TByte>;
begin
Result := Value;
SetLength(Result, Length(Value)); // This assures a unique copy
end;
function TText.ToLowerCase(): TText;
var
Character: TCharacterCodePoint;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, Size);
for Character in Self do
Encoding.Encode(Target, CharacterToLowerCase(Character));
Result := Target.GetValue();
Target.Free();
end;
function TText.ToUpperCase(): TText;
var
Character: TCharacterCodePoint;
Target: TTextOutputStream;
begin
Target := TTextOutputStream.Create(Encoding, Size);
for Character in Self do
Encoding.Encode(Target, CharacterToUpperCase(Character));
Result := Target.GetValue();
Target.Free();
end;
function TText.Trim(): TText;
var
Leading: TInteger32;
Trailing: TInteger32;
begin
Leading := 0;
Trailing := Count - 1;
while (Leading <= Trailing) and (Self[Leading] <= ' ') do
Inc(Leading);
if Leading > Trailing then
Result := ''
else
begin
while Self[Trailing] <= ' ' do
Dec(Trailing);
Result := SubtextFromTo(Leading, Trailing);
end;
end;
procedure TText.Write(Stream: TOutputStream);
begin
if Length(Value) > 0 then
Stream.Write(Value[0], Length(Value));
end;
procedure TText.WriteNullTerminated(Stream: TOutputStream);
begin
if Length(Value) > 0 then
Stream.Write(Value[0], Length(Value));
Encoding.Encode(Stream, TUnicode.NULL);
end;
procedure TText.Write(TargetEncoding: TCharacterEncoding; Stream: TOutputStream);
var
Character: TCharacterCodePoint;
begin
if Count > 0 then
begin
if Encoding.Equal(TargetEncoding) then
Write(Stream)
else
begin
for Character in Self do
TargetEncoding.Encode(Stream, Character);
end;
end;
end;
{ TTextCursor }
function TTextCursor.Copy(): TTextCursor;
begin
Result := TTextCursor.Create(Value);
Result.ByteStream.Index := ByteStream.Index;
end;
constructor TTextCursor.Create(Value: TText);
begin
inherited Create();
Self.Value := Value;
ByteStream := TByteArrayInputStream.Create(Value.Value);
end;
destructor TTextCursor.Destroy();
begin
ByteStream.Free();
inherited Destroy();
end;
function TTextCursor.GetAtEnd(): TBoolean;
begin
Result := ByteStream.AtEnd();
end;
procedure TTextCursor.MoveTo(Cursor: TTextCursor);
begin
if ByteStream.Value = Cursor.ByteStream.Value then
ByteStream.Index := Cursor.ByteStream.Index
else
raise EException.Create('Imcompatible cursor');
end;
function TTextCursor.Read(): TCharacterCodePoint;
begin
Result := Value.Encoding.Decode(ByteStream);
end;
function TTextCursor.Read(Count: TInteger32): TText;
var
Start: TPositiveInteger32;
Index: TInteger32;
begin
Result.FEncoding := Value.Encoding;
Result.FCount := Count;
Start := ByteStream.Index;
for Index := 1 to Count do
Value.Encoding.Decode(ByteStream);
SetLength(Result.Value, ByteStream.Index - Start);
Move(ByteStream.Value[Start], Result.Value[0], Result.Size);
end;
procedure TTextCursor.Skip(Count: TInteger32);
var
Index: TInteger32;
begin
for Index := 1 to Count do
Value.Encoding.Decode(ByteStream);
end;
initialization
TText.AssureInitialized();
end.
|