-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxstr.pas
119 lines (105 loc) · 2.61 KB
/
xstr.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
unit xstr;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
type
XONStr = packed record
private
const xstrInlineSize=6; // do not modify ... binary comptibility will be broken!
type
TXONStrType=(
xstNull=0, // Empty
xstInline, // Directly encoded in the record... up to xstrInlineSize chars
xstNative // Native FPC String
);
private
FType: Byte;
public
procedure Initialize;
procedure Finalize;
procedure SetString( Source: PChar; L: Cardinal);overload;
procedure SetString( const Source: String);overload;
function Length: Integer;
function GetAsString: String;
private
case TXONStrType of
xstInline: ( FLength: Byte; FChars: Array [01..xstrInlineSize] of Char);
xstNative: ( FStr: Pointer); // fpc string
end;
implementation
procedure XONStr.Initialize;inline;
begin
FType:=Ord(xstNull);
FStr:=nil; // set to zeros - just in case ;)
end;
procedure XONStr.Finalize;
begin
case TXONStrType(FType) of
xstNull: exit;
xstInline: FLength:=0;
xstNative: begin
String(FStr):='';
FStr:=nil
end;
end;
FType:=Ord(xstNull);
end;
function XONStr.Length:Integer;
begin
Result:=0;
case TXONStrType(FType) of
xstInline: Result:=FLength;
xstNative: Result:=system.Length(String(FStr));
end;
end;
procedure XONStr.SetString( Source: PChar; L: Cardinal);
var Dest: PChar;
begin
Finalize;
if L=0 then exit;
if L>xstrInlineSize then
begin
FType:=ord(xstNative);
FStr:=nil;
SetLength(String(FStr),L);
Dest:=@String(FStr)[1]
end
else
begin
FType:=ord(xstInline);
FLength:=L;
Dest:=@FChars[1];
end;
Move(Source^,Dest^,L);
end;
procedure XONStr.SetString( const Source: String);
var L: Integer;
begin
Finalize;
L:=System.Length(Source);
if L=0 then exit;
if L>xstrInlineSize then
begin
FType:=ord(xstNative);
FStr:=nil;
String(FStr):=Source;
end
else
begin
FType:=ord(xstInline);
FLength:=L;
Move(Source[1],FChars[1],L);
end;
end;
function XONStr.GetAsString: string;
begin
case TXONStrType(FType) of
xstNull: Result:='';
xstInline: begin
SetLength(Result,FLength);
Move(FChars,Result[1],FLength)
end;
xstNative: Result:=String(FStr)
end;
end;
end.