-
Notifications
You must be signed in to change notification settings - Fork 5
/
BufStreamReader.pas
531 lines (442 loc) · 13.5 KB
/
BufStreamReader.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
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
// Copyright 2015 Asbjørn Heid
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
unit BufStreamReader;
interface
uses
System.SysUtils, System.Classes,
{$IFNDEF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
RegularExpr,
{$ENDIF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
BufStream;
type
BufferedStreamReaderOption = (BufferedStreamReaderOwnsSource);
BufferedStreamReaderOptions = set of BufferedStreamReaderOption;
/// <summary>
/// <para>
/// A simple text reader for streams.
/// </para>
/// <remarks>
/// <para>
/// NOTE: Always use the Stream property for accessing
/// the underlying source stream. Failure to do so will result in pain.
/// </para>
/// <para>
/// The reader relies on the BufferedStream for parsing.
/// If the source stream is not a BufferedStream the source will be
/// automatically wrapped by a BufferedStream instance.
/// </para>
/// </remarks>
/// </summary>
BufferedStreamReader = class
strict private
FOwnsSourceStream: boolean;
FBufferedStream: BufferedStream;
FEncoding: TEncoding;
FSourceEndOfStream: boolean;
FEndOfStream: boolean;
procedure FillBufferedData;
procedure ConsumeBufferedData(const Size: integer);
function GetStream: TStream; inline;
function GetBufferedData: PByte; inline;
function GetBufferedDataLength: integer; inline;
property BufferedData: PByte read GetBufferedData;
property BufferedDataLength: integer read GetBufferedDataLength;
public
/// <summary>
/// Creates a bufferd stream reader instance.
/// </summary>
/// <param name="SourceStream">
/// Stream to read text from. If the SourceStream is not a BufferedStream
/// it will be wrapped, in which case you should use the Stream property
/// if you need to read additional data after reading some text.
/// </param>
/// <param name="Encoding">
/// Encoding of the text. Note, BOM is not detected automatically.
/// </param>
constructor Create(const SourceStream: TStream;
const Encoding: TEncoding;
const Options: BufferedStreamReaderOptions = []);
destructor Destroy; override;
/// <summary>
/// <para>
/// Reads up to Count characters from the source stream. Returns an empty
/// array if there's an error decoding the characters.
/// </para>
/// </summary>
function ReadChars(const CharCount: integer): TCharArray;
/// <summary>
/// <para>
/// Reads a single line of text from the source stream.
/// Line breaks detected are LF, CR and CRLF.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns an empty string.
/// </para>
/// </summary>
function ReadLine: string;
/// <summary>
/// <para>
/// Reads text from the source stream until a delimiter is found or
/// the end of the source stream is reached.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns an empty string.
/// </para>
/// </summary>
function ReadUntil(const Delimiter: UInt8): string; overload;
{$IFNDEF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
/// <summary>
/// <para>
/// Reads text from the source stream until the delimiter
/// regular expression matches.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns an empty string.
/// </para>
/// </summary>
function ReadUntil(const DelimiterExpr: RegEx): string; overload;
{$ENDIF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
/// <summary>
/// <para>
/// Reads text from the source stream until a text delimiter is found or
/// the end of the source stream is reached. The delimiter is encoded using
/// the current Encoding, and the encoded delimiter is used for matching.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns an empty string.
/// </para>
/// </summary>
function ReadUntil(const Delimiter: string): string; overload;
/// <summary>
/// <para>
/// Reads any remaining text from the source stream.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns an empty string.
/// </para>
/// </summary>
function ReadToEnd: string;
/// <summary>
/// <para>
/// Returns the next byte from the source stream
/// without consuming it.
/// </para>
/// <para>
/// If no more data can be read from the source stream, it
/// returns -1.
/// </para>
/// </summary>
function Peek: integer;
/// <summary>
/// <para>
/// Encoding of the text to be read.
/// </para>
/// </summary>
property Encoding: TEncoding read FEncoding write FEncoding;
/// <summary>
/// The buffered stream. Use this if you need to read aditional
/// (possibly binary) data after reading text.
/// </summary>
property Stream: TStream read GetStream;
property OwnsSourceStream: boolean read FOwnsSourceStream;
/// <summary>
/// True if the end of the source stream was detected during the previous
/// read operation.
/// </summary>
property EndOfStream: boolean read FEndOfStream;
end;
implementation
uses
EncodingHelper;
{ BufferedStreamReader }
procedure BufferedStreamReader.ConsumeBufferedData(const Size: integer);
begin
FBufferedStream.ConsumeBuffer(Size);
FEndOfStream := FSourceEndOfStream and (BufferedDataLength <= 0);
end;
constructor BufferedStreamReader.Create(const SourceStream: TStream;
const Encoding: TEncoding; const Options: BufferedStreamReaderOptions);
var
opts: BufferedStreamOptions;
begin
inherited Create;
if (SourceStream is BufferedStream) then
begin
FBufferedStream := BufferedStream(SourceStream);
FOwnsSourceStream := BufferedStreamReaderOwnsSource in Options;
end
else
begin
FOwnsSourceStream := True;
opts := [];
if (BufferedStreamReaderOwnsSource in Options) then
Include(opts, BufferedStreamOwnsSource);
FBufferedStream := BufferedStream.Create(SourceStream, opts);
end;
FEncoding := Encoding;
end;
destructor BufferedStreamReader.Destroy;
begin
if (FOwnsSourceStream) then
FBufferedStream.Free;
FBufferedStream := nil;
inherited;
end;
procedure BufferedStreamReader.FillBufferedData;
begin
FSourceEndOfStream := not FBufferedStream.FillBuffer;
FEndOfStream := FSourceEndOfStream and (BufferedDataLength <= 0);
end;
function BufferedStreamReader.GetBufferedData: PByte;
begin
result := FBufferedStream.BufferedData;
end;
function BufferedStreamReader.GetBufferedDataLength: integer;
begin
result := FBufferedStream.BufferedDataLength;
end;
function BufferedStreamReader.GetStream: TStream;
begin
result := FBufferedStream;
end;
function BufferedStreamReader.Peek: integer;
begin
result := -1;
FEndOfStream := False;
while (BufferedDataLength < 1) and (not FEndOfStream) do
begin
FillBufferedData;
end;
if (FEndOfStream) then
exit;
result := BufferedData^;
end;
function BufferedStreamReader.ReadChars(const CharCount: integer): TCharArray;
var
curIndex, charIndex, curCharLen, outputCharCount: integer;
maxCharLength: integer;
gotChar: boolean;
begin
result := nil;
FEndOfStream := False;
if (Encoding.IsSingleByte) then
begin
while (BufferedDataLength < CharCount) and (not FEndOfStream) do
begin
FillBufferedData;
end;
result := Encoding.GetChars(BufferedData, 0, CharCount);
ConsumeBufferedData(CharCount);
exit;
end;
maxCharLength := Encoding.GetMaxByteCount(1);
curIndex := 0;
charIndex := 0;
outputCharCount := 0;
while True do
begin
if (curIndex + 1 > BufferedDataLength) and (not FEndOfStream) then
FillBufferedData;
if (curIndex >= BufferedDataLength) then
begin
curIndex := BufferedDataLength;
charIndex := curIndex;
break;
end;
curCharLen := curIndex - charIndex + 1;
gotChar := Encoding.GetCharCount(BufferedData, charIndex, curCharLen) > 0;
if (gotChar) then
begin
charIndex := curIndex + 1;
outputCharCount := outputCharCount + 1;
end
else if (curCharLen >= maxCharLength) then
begin
// something is wrong
// buffer start is probably in the middle of a character or similar
exit;
end;
if (outputCharCount >= CharCount) then
break;
curIndex := curIndex + 1;
end;
if (charIndex = 0) then
exit;
result := Encoding.GetChars(BufferedData, 0, charIndex);
ConsumeBufferedData(charIndex);
end;
function BufferedStreamReader.ReadLine: string;
var
curIndex, postLineBreakIndex: integer;
begin
FEndOfStream := False;
curIndex := 0;
postLineBreakIndex := -1;
while True do
begin
if (curIndex + 2 > BufferedDataLength) and (not FEndOfStream) then
FillBufferedData;
if (curIndex >= BufferedDataLength) then
begin
curIndex := BufferedDataLength;
postLineBreakIndex := curIndex;
break;
end;
if (BufferedData[curIndex] = 10) then
begin
postLineBreakIndex := curIndex + 1;
break;
end
else if (BufferedData[curIndex] = 13) then
begin
if (curIndex + 1 < BufferedDataLength) and (BufferedData[curIndex+1] = 10) then
postLineBreakIndex := curIndex + 2
else
postLineBreakIndex := curIndex + 1;
break;
end;
curIndex := curIndex + 1;
end;
result := Encoding.GetString(BufferedData, 0, curIndex);
ConsumeBufferedData(postLineBreakIndex);
end;
function BufferedStreamReader.ReadToEnd: string;
begin
FEndOfStream := False;
while (not FSourceEndOfStream) do
begin
FillBufferedData;
end;
result := Encoding.GetString(BufferedData, 0, BufferedDataLength);
ConsumeBufferedData(BufferedDataLength);
end;
{$IFNDEF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
function BufferedStreamReader.ReadUntil(const DelimiterExpr: RegEx): string;
var
curIndex, postDelimiterIndex: integer;
match: RegExMatch;
begin
FEndOfStream := False;
curIndex := 0;
postDelimiterIndex := -1;
while True do
begin
if (curIndex >= BufferedDataLength) and (not EndOfStream) then
FillBufferedData;
// the latter checks if FillBufferedData managed to read anything at all
if (BufferedDataLength <= 0) or (curIndex >= BufferedDataLength) then
begin
curIndex := 0;
postDelimiterIndex := 0;
break;
end;
match := DelimiterExpr.Match(BufferedData, BufferedDataLength);
if (match) then
begin
curIndex := match.Offset;
postDelimiterIndex := match.Offset + match.Length;
break;
end;
if (FSourceEndOfStream) then
begin
curIndex := BufferedDataLength;
postDelimiterIndex := curIndex;
break;
end;
curIndex := BufferedDataLength;
end;
result := Encoding.GetString(BufferedData, 0, curIndex);
ConsumeBufferedData(postDelimiterIndex);
end;
{$ENDIF BUFFEREDSTREAMREADER_NO_REGULAREXPR}
function BufferedStreamReader.ReadUntil(const Delimiter: string): string;
var
encodedDelimiter: TBytes;
curIndex, matchIndex, postDelimiterIndex: integer;
begin
if (Delimiter = '') then
begin
result := ReadToEnd;
exit;
end;
FEndOfStream := False;
curIndex := 0;
matchIndex := 0;
postDelimiterIndex := -1;
encodedDelimiter := Encoding.GetBytes(Delimiter);
// TODO - perhaps some better algorithm than the naive scan
while True do
begin
if (curIndex + 1 > BufferedDataLength) and (not FEndOfStream) then
FillBufferedData;
if (curIndex >= BufferedDataLength) then
begin
curIndex := BufferedDataLength;
postDelimiterIndex := curIndex;
break;
end;
if (BufferedData[curIndex] = encodedDelimiter[matchIndex]) then
begin
matchIndex := matchIndex + 1;
if (matchIndex >= Length(encodedDelimiter)) then
begin
postDelimiterIndex := curIndex + 1;
curIndex := postDelimiterIndex - matchIndex;
break;
end;
end
else
begin
// reset curIndex in case we've restarted the matching
curIndex := curIndex - matchIndex;
matchIndex := 0;
end;
curIndex := curIndex + 1;
end;
result := Encoding.GetString(BufferedData, 0, curIndex);
ConsumeBufferedData(postDelimiterIndex);
end;
function BufferedStreamReader.ReadUntil(const Delimiter: UInt8): string;
var
curIndex, postDelimiterIndex: integer;
begin
FEndOfStream := False;
curIndex := 0;
postDelimiterIndex := -1;
while True do
begin
if (curIndex + 1 > BufferedDataLength) and (not FEndOfStream) then
FillBufferedData;
if (curIndex >= BufferedDataLength) then
begin
curIndex := BufferedDataLength;
postDelimiterIndex := curIndex;
break;
end;
if (BufferedData[curIndex] = Delimiter) then
begin
postDelimiterIndex := curIndex + 1;
break;
end;
curIndex := curIndex + 1;
end;
result := Encoding.GetString(BufferedData, 0, curIndex);
ConsumeBufferedData(postDelimiterIndex);
end;
end.