-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwires.pas
More file actions
86 lines (74 loc) · 1.88 KB
/
wires.pas
File metadata and controls
86 lines (74 loc) · 1.88 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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit wires;
// A mostly rigid string
// This is a ShortString/UTF8String hybrid.
// It uses a ShortString until you hit 255 characters, at which point it starts using a regular UTF8String.
interface
uses
unicode, utf8;
type
TWire = record
private
const
kSafeThreshold = High(ShortString) - High(TUTF8SequenceLength) + 1;
var
FShortString: ShortString;
FLongString: UTF8String;
function GetAsString(): UTF8String; inline;
function GetIsEmpty(): Boolean; inline;
public
procedure Init(); inline;
procedure Append(const Codepoint: TUnicodeCodepoint); inline;
property AsString: UTF8String read GetAsString;
property IsEmpty: Boolean read GetIsEmpty;
end;
operator = (const Op1: TWire; const Op2: UTF8String): Boolean; inline;
implementation
procedure TWire.Init();
begin
FShortString := '';
FLongString := '';
end;
procedure TWire.Append(const Codepoint: TUnicodeCodepoint);
var
AsUTF8: TUTF8Sequence;
begin
AsUTF8 := CodepointToUTF8(CodePoint);
if (Length(FShortString) < kSafeThreshold) then
begin
{$PUSH}
{$RANGECHECKS OFF}
FShortString := FShortString + AsUTF8.AsString;
{$POP}
end
else
begin
FLongString := FLongString + AsUTF8.AsString;
end;
end;
function TWire.GetAsString(): UTF8String;
begin
Result := FShortString + FLongString;
end;
function TWire.GetIsEmpty(): Boolean;
begin
Result := FShortString[0] = #0;
end;
operator = (const Op1: TWire; const Op2: UTF8String): Boolean; inline;
begin
if (Length(Op2) < Length(Op1.FShortString)) then
begin
Result := False;
end
else
if (Length(Op1.FShortString) < Op1.kSafeThreshold) then
begin
Result := Op1.FShortString = Op2;
end
else
begin
Result := (Op1.FShortString + Op1.FLongString) = Op2;
end;
end;
end.