-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompressedString.pas
More file actions
221 lines (177 loc) · 5.61 KB
/
CompressedString.pas
File metadata and controls
221 lines (177 loc) · 5.61 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
unit CompressedString;
interface
uses
System.SysUtils,
System.Classes,
System.ZLib;
// 1.1 - Added toInteger and Length ( original length )
// 1.0 - First release - Thanks to https://github.com/havrlisan for lots of help and making the debugger visualizer!
Const
zStringVersion = 1.1;
type
zString = record
strict private
FCompressedData: TBytes;
FCompressed: Boolean;
FOriginalLength: longint;
function GetValue: string;
function GetLength: longint;
procedure SetValue(const Value: string);
public
class operator Implicit(const Value: string): zString;
class operator Implicit(const Value: zString): string;
class operator Equal(const A: zString; const B: zString): Boolean;
class operator Equal(const A: zString; const B: string): Boolean;
class operator Equal(const A: string; const B: zString): Boolean;
class operator NotEqual(Const A: zString; const B: zString): Boolean;
class operator NotEqual(const A: zString; const B: string): Boolean;
class operator NotEqual(const A: string; const B: zString): Boolean;
class operator Add(const A, B: zString): zString;
class operator Add(const A: zString; const B: string): zString;
class operator Add(const A: string; const B: zString): zString;
class operator GreaterThan(const A: zString; const B: zString): Boolean;
class operator GreaterThan(const A: zString; const B: string): Boolean;
class operator GreaterThan(const A: string; const B: zString): Boolean;
class operator LessThan(const A: zString; const B: zString): Boolean;
class operator LessThan(const A: zString; const B: string): Boolean;
class operator LessThan(const A: string; const B: zString): Boolean;
class function CompressString(const Value: string): TBytes; static;
class function DecompressString(const CompressedData: TBytes): string; static;
constructor Create(const Value: string);
procedure Assign(const Value: zString);
function CompressionPercentage: Double;
function ToDouble: Double;
function ToInteger: Integer;
property Value: string read GetValue write SetValue;
property CompressSize: longint read GetLength;
property isCompressed: Boolean read FCompressed;
property Length: longint read FOriginalLength;
end;
implementation
class function zString.CompressString(const Value: string): TBytes;
begin
Result := ZCompressStr(Value, TZCompressionLevel.zcMax); // default is max because why else?
end;
class function zString.DecompressString(const CompressedData: TBytes): string;
begin
Result := ZDecompressStr(CompressedData);
end;
procedure zString.SetValue(const Value: string);
begin
FOriginalLength:= Value.Length;
FCompressedData := CompressString(Value);
If System.Length(FCompressedData) > Value.Length then
begin
FCompressedData := TEncoding.Unicode.GetBytes(Value);
FCompressed := false;
end
else
FCompressed := true;
end;
function zString.GetValue: string;
begin
if FCompressed then
Result := DecompressString(FCompressedData)
else
Result := TEncoding.Unicode.GetString(FCompressedData);
end;
function zString.GetLength: longint;
begin
Result := System.Length(FCompressedData);
end;
class operator zString.Implicit(const Value: string): zString;
begin
Result.SetValue(Value);
end;
class operator zString.Implicit(const Value: zString): string;
begin
Result := Value.GetValue;
end;
class operator zString.Equal(const A: zString; const B: zString): Boolean;
begin
Result := A.GetValue = B;
end;
class operator zString.Equal(const A: zString; const B: string): Boolean;
begin
Result := A.GetValue = B;
end;
class operator zString.Equal(const A: string; const B: zString): Boolean;
begin
Result := A = B.GetValue;
end;
class operator zString.NotEqual(const A: zString; const B: zString): Boolean;
begin
Result := A.GetValue <> B;
end;
class operator zString.NotEqual(const A: zString; const B: string): Boolean;
begin
Result := A.GetValue <> B;
end;
class operator zString.NotEqual(const A: string; const B: zString): Boolean;
begin
Result := A <> B.GetValue;
end;
class operator zString.Add(const A, B: zString): zString;
begin
Result := zString(A.GetValue + B.GetValue);
end;
class operator zString.Add(const A: zString; const B: string): zString;
begin
Result := zString(A.GetValue + B);
end;
class operator zString.Add(const A: string; const B: zString): zString;
begin
Result := zString(A + B.GetValue);
end;
class operator zString.GreaterThan(const A, B: zString): Boolean;
begin
Result := A.GetValue > B.GetValue;
end;
class operator zString.GreaterThan(const A: zString; const B: string): Boolean;
begin
Result := A.GetValue > B;
end;
class operator zString.GreaterThan(const A: string; const B: zString): Boolean;
begin
Result := A > B.GetValue;
end;
class operator zString.LessThan(const A, B: zString): Boolean;
begin
Result := A.GetValue < B.GetValue;
end;
class operator zString.LessThan(const A: zString; const B: string): Boolean;
begin
Result := A.GetValue < B;
end;
class operator zString.LessThan(const A: string; const B: zString): Boolean;
begin
Result := A < B.GetValue;
end;
constructor zString.Create(const Value: string);
begin
SetValue(Value);
end;
procedure zString.Assign(const Value: zString);
begin
FCompressedData := Value.FCompressedData;
end;
function zString.ToDouble: Double;
begin
Result := StrToFloat(GetValue);
end;
function zString.ToInteger: Integer;
begin
Result := StrToInt(GetValue);
end;
function zString.CompressionPercentage: Double;
var
OriginalSize: integer;
CompressedSize: integer;
begin
OriginalSize := System.Length(GetValue);
CompressedSize := System.Length(FCompressedData);
if OriginalSize = 0 then
Exit(0.0);
Result := ((OriginalSize - CompressedSize) / OriginalSize) * 100;
end;
end.