-
Notifications
You must be signed in to change notification settings - Fork 1
/
reading-writing.dylan
393 lines (327 loc) · 11.7 KB
/
reading-writing.dylan
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
module: sequence-stream
//
// Client reading methods
//
define method ext-peek
(stream :: <sequence-stream>, #key on-end-of-stream = unsupplied())
=> (elem-or-eof :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
peek(stream, on-end-of-stream: on-end-of-stream)
end method;
define method ext-read-element
(stream :: <sequence-stream>, #key on-end-of-stream = unsupplied())
=> (elem-or-eof :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
read-element(stream, on-end-of-stream: on-end-of-stream);
end method;
define method ext-unread-element
(stream :: <sequence-stream>, elem :: <object>)
=> (elem :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
if (stream.stream-position ~= stream.stream-unread-from)
cannot-unread-error(stream)
end if;
adjust-stream-position(stream, -1, grow: #f);
stream.stream-storage[stream.stream-position + stream.stream-start]
end method;
define method ext-read
(stream :: <sequence-stream>, n :: <integer>, #key on-end-of-stream = unsupplied())
=> (elements-or-eof :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
if (n = 0)
#[]
elseif (stream.stream-at-end?)
case
on-end-of-stream.supplied? => on-end-of-stream;
otherwise => eos-error(stream);
end case
else
let start-idx = stream.stream-start + stream.stream-position;
let desired-end-idx = start-idx + n;
let past-end-of-stream? = desired-end-idx > stream.stream-end;
let end-idx = min(desired-end-idx, stream.stream-end);
case
~past-end-of-stream? =>
read-stream-elements(stream, start: start-idx, end: end-idx);
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
let partial = read-stream-elements(stream, start: start-idx, end: end-idx);
incomplete-error(stream, partial);
end case
end if
end method;
define method ext-read-into!
(stream :: <sequence-stream>, n :: <integer>, coll :: <mutable-sequence>,
#key start :: <integer> = 0, on-end-of-stream = unsupplied())
=> (count-or-eof :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
let (coll-start, coll-limit, coll-next, coll-done?, coll-key, coll-elem,
coll-elem-setter) =
forward-iteration-protocol(coll);
// Skip to where we want to start inserting elements from the stream.
let coll-start =
for (coll-idx from 0 below start,
coll-iter = coll-start then coll-next(coll, coll-iter),
until: coll-done?(coll, coll-iter, coll-limit))
finally
coll-iter
end for;
// Start inserting elements from the stream and return results.
let total-read = 0;
block ()
for (read-count from 0 below n,
coll-iter = coll-start then coll-next(coll, coll-iter),
until: coll-done?(coll, coll-iter, coll-limit))
coll-elem-setter(read-element(stream), coll, coll-iter);
total-read := read-count;
end for;
total-read
exception (eos :: <end-of-stream-error>)
case
on-end-of-stream.supplied? =>
on-end-of-stream;
total-read = 0 =>
error(eos);
otherwise =>
let partial = copy-sequence(coll, start: start, end: start + total-read);
incomplete-error(stream, partial);
end case
end block;
end method;
define method ext-read-to
(stream :: <sequence-stream>, to-elem :: <object>,
#key on-end-of-stream = unsupplied(), test :: <function> = \==)
=> (elements-or-eof :: <object>, found? :: <boolean>)
check-stream-open(stream);
check-stream-readable(stream);
read-through(stream, to-elem, keep-term: #f, test: test,
on-end-of-stream: on-end-of-stream)
end method;
define method ext-read-through
(stream :: <sequence-stream>, to-elem :: <object>,
#key on-end-of-stream = unsupplied(), test :: <function> = \==)
=> (elements-or-eof :: <object>, found? :: <boolean>)
check-stream-open(stream);
check-stream-readable(stream);
read-through(stream, to-elem, keep-term: #t, test: test,
on-end-of-stream: on-end-of-stream)
end method;
define method ext-read-to-end (stream :: <sequence-stream>)
=> (elements :: <sequence>)
check-stream-open(stream);
check-stream-readable(stream);
let start-idx = stream.stream-position + stream.stream-start;
read-stream-elements(stream, start: start-idx, end: stream.stream-end)
end method;
define method ext-skip-through
(stream :: <sequence-stream>, to-elem :: <object>, #key test :: <function> = \==)
=> (found? :: <boolean>)
check-stream-open(stream);
check-stream-readable(stream);
skip-through(stream, to-elem, test: test).true?
end method;
define method ext-read-line
(stream :: <string-stream>, #key on-end-of-stream = unsupplied())
=> (string-or-eof :: <object>, newline? :: <boolean>)
check-stream-open(stream);
check-stream-readable(stream);
if (stream.stream-at-end?)
case
on-end-of-stream.supplied? => values(on-end-of-stream, #f);
otherwise => eos-error(stream);
end case
else
// Find line ending.
let start-idx = stream.stream-position + stream.stream-start;
let (found-idx, next-idx) = skip-through(stream, "\r\n", test: member?);
let end-idx = found-idx | next-idx;
let elements =
copy-sequence(stream.stream-storage, start: start-idx, end: end-idx);
// Skip over LF of CRLF.
when (found? & peek(stream, on-end-of-stream: #f) = '\n')
read-element(stream);
end when;
values(elements, found-idx.true?);
end if;
end method;
define method ext-read-line-into!
(stream :: <string-stream>, string :: <string>,
#key start :: <integer> = 0, grow? :: <boolean> = #f,
on-end-of-stream = unsupplied())
=> (string-or-eof :: <object>, newline? :: <boolean>)
let (line, found?) = ext-read-line(stream.outer-stream, on-end-of-stream: #f);
case
line =>
// Grow dest string if necessary.
let size-needed = start + line.size;
if (string.size < size-needed)
if (grow?)
let new-string = make(string.object-class, size: size-needed);
string := replace-subsequence!(new-string, string, end: string.size);
else
error("String not large enough for input");
end if;
end if;
// Copy into dest string. Would rather use replace-subsequence!, but
// that doesn't guarantee that string itself is altered.
for (i from start, c in line)
string[i] := c;
end for;
values(string, found?);
on-end-of-stream.supplied? =>
values(on-end-of-stream, #f);
otherwise =>
eos-error(stream);
end case;
end method;
define method ext-read-text
(stream :: <string-stream>, n :: <integer>, #key on-end-of-stream = unsupplied())
=> (elements-or-eof :: <object>)
let results = make(<string>, size: n);
let read-count = ext-read-text-into!(stream.outer-stream, n, results,
on-end-of-stream: #f);
case
read-count =>
if (read-count < results.size)
copy-sequence(results, end: read-count)
else
results
end if;
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
eos-error(stream);
end case;
end method;
define method ext-read-text-into!
(stream :: <string-stream>, n :: <integer>, string :: <string>,
#key start :: <integer> = 0, on-end-of-stream = unsupplied())
=> (count-or-eof :: <object>)
check-stream-open(stream);
check-stream-readable(stream);
let total-read = 0;
block ()
for (i from start below min(start + n, string.size))
let elem = read-element(stream);
if (elem == '\r')
elem := '\n';
if (peek(stream, on-end-of-stream: #f) == '\n')
read-element(stream);
end if;
end if;
string[i] := elem;
total-read := total-read + 1;
end for;
total-read
exception (eos :: <end-of-stream-error>)
case
total-read > 0 =>
total-read;
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
error(eos);
end case;
end block;
end method;
//
// Client writing methods
//
define method ext-write-element
(stream :: <sequence-stream>, elem :: <object>)
=> ()
check-stream-open(stream);
check-stream-writable(stream);
check-element-type(stream, elem);
if (stream.stream-at-end?)
replace-stream-elements(stream, vector(elem));
else
let idx = stream.stream-start + stream.stream-position;
stream.stream-storage[idx] := elem;
end if;
adjust-stream-position(stream, +1, grow: #f);
end method;
define method ext-write
(stream :: <sequence-stream>, coll :: <sequence>,
#key start: coll-start-idx :: <integer> = 0,
end: coll-end-idx :: <integer> = coll.size)
=> ()
check-stream-open(stream);
check-stream-writable(stream);
let (coll-start, coll-limit, coll-next, coll-done?, coll-key, coll-elem,
coll-elem-setter) =
forward-iteration-protocol(coll);
// Skip to where we want to start taking elements from the collection.
let coll-start =
for (coll-idx from 0 below coll-start-idx,
coll-iter = coll-start then coll-next(coll, coll-iter),
until: coll-done?(coll, coll-iter, coll-limit))
finally
coll-iter
end for;
// Take elements from the collection and put them in the stream.
let stream-start-idx = stream.stream-position + stream.stream-start;
let (coll-start, coll-remainder-start-idx) =
for (stream-idx from stream-start-idx below stream.stream-end,
coll-idx from coll-start-idx below coll-end-idx,
coll-iter = coll-start then coll-next(coll, coll-iter),
until: coll-done?(coll, coll-iter, coll-limit))
let elem = coll-elem(coll, coll-iter);
check-element-type(stream, elem);
stream.stream-storage[stream-idx] := elem;
finally
values(coll-iter, coll-idx);
end for;
// Deal with remainder that needs to be inserted. Check element types and
// collect up a sequence to pass to 'replace-stream-elements'.
let coll-remainder-size = coll-end-idx - coll-remainder-start-idx;
if (coll-remainder-size > 0)
let coll-remainder = make(<vector>, size: coll-remainder-size);
for (remainder-idx from 0,
coll-iter = coll-start then coll-next(coll, coll-iter),
until: coll-done?(coll, coll-iter, coll-limit))
let elem = coll-elem(coll, coll-iter);
check-element-type(stream, elem);
coll-remainder[remainder-idx] := elem;
end for;
replace-stream-elements(stream, coll-remainder);
end if;
// Reset stream position.
adjust-stream-position(stream, coll-end-idx - coll-start-idx, grow: #f);
end method;
define method ext-write
(stream :: <string-stream>, string :: <string>,
#key start: string-start-idx :: <integer> = 0,
end: string-end-idx :: <integer> = string.size)
=> ()
check-stream-open(stream);
check-stream-writable(stream);
let string-span = string-end-idx - string-start-idx;
if (string-span < string.size)
string := copy-sequence(string, start: string-start-idx, end: string-end-idx);
end if;
let stream-start-idx = stream.stream-position + stream.stream-start;
let stream-end-idx = stream-start-idx + string-span;
replace-stream-elements(stream, string, start: stream-start-idx, end: stream-end-idx);
adjust-stream-position(stream, string-span, grow: #f);
end method;
define sealed domain ext-write (<byte-string-stream>, <byte-string>);
define method ext-write-line
(stream :: <sequence-stream>, string :: <string>,
#key start: start-pos :: <integer> = 0,
end: end-pos :: <integer> = string.size)
=> ()
if (end-pos - start-pos < string.size)
string := copy-sequence(string, start: start-pos, end: end-pos);
end if;
ext-write(stream.outer-stream, add(string, '\n'))
end method;
define method ext-new-line (stream :: <sequence-stream>) => ()
ext-write-element(stream.outer-stream, '\n');
end method;