forked from kami-soft/ProtoBufGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrBuffer.pas
390 lines (346 loc) · 8.14 KB
/
StrBuffer.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
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
// author Marat Shaymardanov, Tomsk 2001, 2013
//
// You can freely use this code in any project
// if sending any postcards with postage stamp to my address:
// Frunze 131/1, 56, Russia, Tomsk, 634021
//
// The buffer for strings.
// The main purpose of the rapid format of long string.
// Features:
// 1. Minimize the handling of the memory manager.
// 2. One-time allocation of the specified size
// 3. The class is not multi-thread safe
unit StrBuffer;
interface
uses
Classes,
SysUtils;
const
MaxBuffSize = Maxint div 16;
SBufferIndexError = 'Buffer index out of bounds (%d)';
SBufferCapacityError = 'Buffer capacity out of bounds (%d)';
SBufferCountError = 'Buffer count out of bounds (%d)';
type
{
If you do not have enough space in the string than
is taken a piece of memory twice the size
and copies the data in this chunk of memory
}
TStrBuffer = class
private
FCount: Integer;
FCapacity: Integer;
FBuff: PAnsiChar;
protected
class procedure Error(const Msg: string; Data: Integer);
public
constructor Create;
destructor Destroy; override;
procedure SaveToStream(Stream: TStream);
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
procedure LoadFromStream(Stream: TStream);
procedure Clear;
procedure Add(const aValue: AnsiString); overload;
procedure SetCapacity(NewCapacity: Integer);
function GetText: AnsiString;
function GetCount: Integer;
end;
PSegment = ^TSegment;
TSegment = record
Next: PSegment;
Size: Integer;
Count: Integer;
Data: array [0 .. 0] of AnsiChar;
end;
{ add memory done by segments }
TSegmentBuffer = class
private
FCount: Integer;
FCapacity: Integer;
FFirst: PSegment;
FLast: PSegment;
protected
class procedure Error(const Msg: string; Data: Integer);
public
constructor Create;
destructor Destroy; override;
procedure SaveToStream(Stream: TStream);
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
procedure LoadFromStream(Stream: TStream);
procedure Clear;
procedure AddSegment(aSize: Integer);
procedure Add(const aValue: AnsiChar); overload;
procedure Add(const aValue: AnsiString); overload;
procedure Add(const aValue: PAnsiChar; aCnt: Integer); overload;
function GetText: AnsiString;
function GetCount: Integer;
property Text: AnsiString read GetText;
end;
implementation
{$RANGECHECKS OFF}
{ TStrBuffer }
class procedure TStrBuffer.Error(const Msg: string; Data: Integer);
{ function ReturnAddr: Pointer; // this is not cross-platform
asm
MOV EAX,[EBP+4]
end; }
begin
raise EListError.CreateFmt(Msg, [Data]) { at ReturnAddr };
end;
constructor TStrBuffer.Create;
begin
inherited Create;
FCount := 0;
FCapacity := 0;
FBuff := nil;
end;
destructor TStrBuffer.Destroy;
begin
Clear;
inherited;
end;
procedure TStrBuffer.Clear;
begin
FCount := 0;
SetCapacity(0);
end;
procedure TStrBuffer.Add(const aValue: AnsiString);
var
cnt, delta: Integer;
begin
cnt := Length(aValue);
if FCount + cnt > FCapacity then
begin
delta := FCapacity div 2;
if delta < cnt then
delta := cnt * 2;
SetCapacity(FCapacity + delta);
end;
System.Move(Pointer(aValue)^, PAnsiChar(FBuff + FCount)^, cnt);
Inc(FCount, cnt);
end;
function TStrBuffer.GetCount: Integer;
begin
result := FCount;
end;
function TStrBuffer.GetText: AnsiString;
begin
SetLength(result, FCount);
System.Move(FBuff^, Pointer(result)^, FCount);
end;
procedure TStrBuffer.SetCapacity(NewCapacity: Integer);
begin
if (NewCapacity < FCount) or (NewCapacity > MaxBuffSize) then
Error(SBufferCapacityError, NewCapacity);
if NewCapacity <> FCapacity then
begin
ReallocMem(FBuff, NewCapacity);
FCapacity := NewCapacity;
end;
end;
procedure TStrBuffer.SaveToStream(Stream: TStream);
begin
Stream.WriteBuffer(FBuff, FCount);
end;
procedure TStrBuffer.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TStrBuffer.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TStrBuffer.LoadFromStream(Stream: TStream);
var
Size: Integer;
s: AnsiString;
begin
Clear;
Size := Stream.Size - Stream.Position;
SetString(s, nil, Size);
Stream.Read(Pointer(s)^, Size);
Add(s);
end;
{ TSegmentBuffer }
class procedure TSegmentBuffer.Error(const Msg: string; Data: Integer);
{function ReturnAddr: Pointer;
asm
MOV EAX,[EBP+4]
end;}
begin
raise EListError.CreateFmt(Msg, [Data]);// at ReturnAddr;
end;
constructor TSegmentBuffer.Create;
begin
inherited Create;
FCount := 0;
FFirst := AllocMem(4096 + SizeOf(TSegment));
with FFirst^ do
begin
Next := nil;
Size := 4096;
Count := 0;
end;
FLast := FFirst;
end;
destructor TSegmentBuffer.Destroy;
begin
Clear;
FreeMem(FFirst, FFirst^.Size);
inherited Destroy;
end;
procedure TSegmentBuffer.Clear;
var
p1, p2: PSegment;
begin
p1 := FFirst;
while p1 <> FLast do
begin
p2 := p1;
p1 := p1^.Next;
FreeMem(p2, p2^.Size);
end;
FFirst := FLast;
FFirst^.Count := 0;
FCount := 0;
end;
procedure TSegmentBuffer.AddSegment(aSize: Integer);
var
segment: PSegment;
begin
segment := AllocMem(aSize + SizeOf(TSegment) - SizeOf(AnsiChar));
with segment^ do
begin
Next := nil;
Size := aSize;
Count := 0;
end;
FLast^.Next := segment;
FLast := segment;
Inc(FCapacity, aSize);
end;
function TSegmentBuffer.GetCount: Integer;
begin
result := FCount;
end;
function TSegmentBuffer.GetText: AnsiString;
var
p: PAnsiChar;
segment: PSegment;
len: Integer;
begin
SetString(result, nil, FCount);
p := Pointer(result);
segment := FFirst;
while segment <> nil do
begin
len := segment^.Count;
System.Move(segment^.Data, p^, len);
Inc(p, len);
segment := segment^.Next;
end;
end;
procedure TSegmentBuffer.Add(const aValue: PAnsiChar; aCnt: Integer);
var
p: PAnsiChar;
tmp: Integer;
begin
p := aValue;
// define size of unused memory in current buffer segment
tmp := FLast^.Size - FLast^.Count;
// if you do not have enough space in the buffer then copy the "unused" bytes
// and reduce current segment
if aCnt > tmp then
begin
System.Move(p^, FLast^.Data[FLast^.Count], tmp);
Inc(FLast^.Count, tmp);
Inc(FCount, tmp);
Inc(p, tmp);
Dec(aCnt, tmp);
// add another segment of the larger buffer size
tmp := FLast^.Size;
if tmp < aCnt then
tmp := aCnt;
AddSegment(tmp * 2);
end;
if aCnt > 0 then
begin
Move(p^, FLast^.Data[FLast^.Count], aCnt);
Inc(FCount, aCnt);
Inc(FLast^.Count, aCnt);
end;
end;
procedure TSegmentBuffer.Add(const aValue: AnsiString);
var
len: Integer;
begin
len := Length(aValue);
if len > 0 then
Add(@aValue[1], len);
end;
procedure TSegmentBuffer.Add(const aValue: AnsiChar);
begin
Add(@aValue, 1);
end;
procedure TSegmentBuffer.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TSegmentBuffer.SaveToStream(Stream: TStream);
var
segment: PSegment;
begin
segment := FFirst;
while segment <> nil do
begin
Stream.WriteBuffer(segment^.Data[0], segment^.Count);
segment := segment^.Next;
end;
end;
procedure TSegmentBuffer.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TSegmentBuffer.LoadFromStream(Stream: TStream);
var
Size: Integer;
s: AnsiString;
begin
Clear;
Size := Stream.Size - Stream.Position;
SetString(s, nil, Size);
Stream.Read(Pointer(s)^, Size);
Add(s);
end;
{$RANGECHECKS ON}
end.