forked from ExaScience/cl-elprep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.lisp
425 lines (392 loc) · 19.7 KB
/
buffer.lisp
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
(in-package :elprep)
(in-simple-base-string-syntax)
(defconstant +buffer-chunk-size+ 1024
"Size of buffer chunks.")
(declaim (inline %make-buffer buffer-p))
(defstruct (buffer (:constructor %make-buffer ())
(:copier nil))
"A buffer can be used to create simple-base-strings that can grow arbitrarily large in size.
It is somewhat similar to adjustable strings in Common Lisp, but guarantees to allocate only as much as necessary,
and to never copy contents when growing. Buffers are also easy to reuse, and efficiently reuse already allocated memory.
The struct buffer has a default constructor %make-buffer, and a defaul predicate.
Accessor buffer-pos refers to the current active size of the buffer.
Accessor buffer-str refers to an array of string chunks.
Accessor buffer-hash-value refers to the hash value of the active contents of the buffer. If -1, the hash-value hasn't been computed yet."
(pos 0 :type fixnum)
(str #() :type simple-vector)
(hash-value -1 :type fixnum))
(setf (documentation '%make-buffer 'function)
"Default constructor for struct buffer."
(documentation 'buffer-p 'function)
"Default predicate for struct buffer."
(documentation 'buffer-pos 'function)
"The current active size of a buffer."
(documentation 'buffer-str 'function)
"An array of string chunks holding the contents of a buffer."
(documentation 'buffer-hash-value 'function)
"The hash-value for the active contents of a buffer. If -1, the hash-value hasn't been computed yet.")
(declaim (inline reinitialize-buffer))
(defun reinitialize-buffer (buf)
"Reset buffer-pos and buffer-hash-value, so this buffer can be reused."
(declare (buffer buf) #.*optimization*)
(setf (buffer-pos buf) 0)
(setf (buffer-hash-value buf) -1)
buf)
(declaim (inline buffer-emptyp))
(defun buffer-emptyp (buf)
"Returns true if the active size of the buffer is 0."
(declare (buffer buf) #.*optimization*)
(= (buffer-pos buf) 0))
(defun ensure-str (buf old-str n)
"Ensure that buffer-str holds enough chunks (internal)."
(declare (buffer buf) (simple-vector old-str) (fixnum n) #.*optimization*)
(let ((new-str (make-array n #+lispworks :single-thread #+lispworks t)))
(declare (simple-vector new-str))
(loop for i of-type fixnum below (length old-str)
do (setf (svref new-str i) (svref old-str i)))
(loop for i of-type fixnum from (length old-str) below (length new-str)
do (setf (svref new-str i)
(make-array +buffer-chunk-size+ :element-type 'base-char
#+lispworks :single-thread #+lispworks t)))
(setf (buffer-str buf) new-str)))
(declaim (inline ensure-chunk))
(defun ensure-chunk (buf hi)
"Ensure that buffer-str has enough chunks (internal)."
(declare (buffer buf) (fixnum hi) #.*optimization*)
(let ((str (buffer-str buf)))
(declare (simple-vector str))
(svref (if (< hi (length str)) str
(ensure-str buf str (the fixnum (1+ hi)))) hi)))
(defmethod print-object ((buf buffer) stream)
(print-unreadable-object (buf stream :type t :identity t)
(format stream ":POS ~S :STR ~S"
(buffer-pos buf)
(unless (zerop (length (buffer-str buf))) "..."))))
(declaim (inline buffer-push))
(defun buffer-push (buf char)
"Add a single character to a buffer."
(declare (buffer buf) (base-char char) #.*optimization*)
(let ((pos (buffer-pos buf)))
(declare (fixnum pos))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(let ((chunk (ensure-chunk buf hi)))
(declare (simple-base-string chunk))
(setf (schar chunk lo) char)
(setf (buffer-pos buf) (the fixnum (1+ pos)))))))
(declaim (notinline slow-buffer-extend))
(defun slow-buffer-extend (buf pos hi lo chunk string start end length)
"Add a simple-base-string to a buffer (internal slow path)."
(declare (buffer buf) (fixnum pos hi lo) (simple-base-string chunk)
(simple-base-string string) (fixnum start end length) #.*optimization*)
(loop with source of-type fixnum = start do
(loop for target of-type fixnum from lo below +buffer-chunk-size+ do
(setf (schar chunk target)
(schar string source))
(when (= (incf source) end)
(setf (buffer-pos buf) (the fixnum (+ pos length)))
(return-from slow-buffer-extend)))
(incf hi) (setq lo 0)
(setq chunk (ensure-chunk buf hi))))
(defun buffer-extend (buf string &optional (start 0) end)
"Add a base-string to a buffer."
(declare (buffer buf) (base-string string) (fixnum start) #.*optimization*)
(let* ((end (or end (length string)))
(length (the fixnum (- end start)))
(pos (buffer-pos buf)))
(declare (fixnum end length pos))
(multiple-value-bind (string offset) (unwrap-displaced-array string)
(declare (simple-base-string string) (fixnum offset))
(setq start (the fixnum (+ start offset))
end (the fixnum (+ end offset)))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(let ((chunk (ensure-chunk buf hi)))
(declare (simple-base-string chunk))
(if (<= (the fixnum (+ lo length)) +buffer-chunk-size+)
(loop for i of-type fixnum from start below end
for j of-type fixnum from lo do
(setf (schar chunk j)
(schar string i))
finally
(setf (buffer-pos buf) (the fixnum (+ pos length)))
(return (values)))
(slow-buffer-extend buf pos hi lo chunk string start end length)))))))
#+sbcl
(progn
(declaim (notinline slow-io-buffer-extend))
(defun slow-io-buffer-extend (buf pos hi lo chunk string start end length)
"Add a region of a buffered-ascii-input-stream buffer to a buffer (internal slow path)."
(declare (buffer buf) (fixnum pos hi lo) (simple-base-string chunk)
(fixnum start end length) #.*optimization*)
(with-buffer-dispatch string
(loop with source of-type fixnum = start do
(loop for target of-type fixnum from lo below +buffer-chunk-size+ do
(setf (schar chunk target)
(bchar string source))
(when (= (incf source) end)
(setf (buffer-pos buf) (the fixnum (+ pos length)))
(return-from slow-io-buffer-extend)))
(incf hi) (setq lo 0)
(setq chunk (ensure-chunk buf hi)))))
(defun io-buffer-extend (buf string &optional (start 0) end)
"Add a region of a buffered-ascii-input-stream buffer to a buffer."
(declare (buffer buf) (fixnum start) #.*optimization*)
(with-buffer-dispatch string
(let* ((end (or end (length string)))
(length (the fixnum (- end start)))
(pos (buffer-pos buf)))
(declare (fixnum end length pos))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(let ((chunk (ensure-chunk buf hi)))
(declare (simple-base-string chunk))
(if (<= (the fixnum (+ lo length)) +buffer-chunk-size+)
(loop for i of-type fixnum from start below end
for j of-type fixnum from lo do
(setf (schar chunk j)
(bchar string i))
finally
(setf (buffer-pos buf) (the fixnum (+ pos length)))
(return (values)))
(slow-io-buffer-extend buf pos hi lo chunk string start end length))))))))
(declaim (inline make-buffer))
(defun make-buffer (&optional initial-string)
"Create a buffer with an optional initial string"
(let ((buf (%make-buffer)))
(when initial-string (buffer-extend buf initial-string))
buf))
(defun write-buffer (buf stream)
"Write the active contents of a buffer to a stream"
(declare (buffer buf) (stream stream) #.*optimization*)
(let ((pos (buffer-pos buf))
(str (buffer-str buf)))
(declare (fixnum pos) (simple-vector str))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(loop for i of-type fixnum below hi do
(write-string (svref str i) stream :end +buffer-chunk-size+))
(when (> lo 0)
(write-string (svref str hi) stream :end lo))))
(values))
(defun buffer-copy (source target)
"Copy the active contents of one buffer to another."
(declare (buffer source target) #.*fixnum-optimization*)
(let ((pos (buffer-pos source))
(str (buffer-str source)))
(declare (fixnum pos) (simple-vector str))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(loop for i of-type fixnum below hi
do (buffer-extend target (svref str i) 0 +buffer-chunk-size+))
(when (> lo 0) (buffer-extend target (svref str hi) 0 lo))))
(values))
#+lispworks
(defun read-line-into-buffer (stream buf)
"Read a line from a stream into a buffer, after reinitializing it."
(declare (buffered-stream stream) (buffer buf) #.*fixnum-optimization*)
(reinitialize-buffer buf)
(loop (with-stream-input-buffer (buffer index limit) stream
(declare (simple-base-string buffer) (fixnum index limit))
(loop for end of-type fixnum from index below limit do
(when (char= (lw:sbchar buffer end) #\Newline)
(buffer-extend buf buffer index end)
(setq index (1+ end))
(return-from read-line-into-buffer buf))
finally
(buffer-extend buf buffer index limit)
(setq index limit)))
(unless (stream-fill-buffer stream)
(return-from read-line-into-buffer buf))))
#+sbcl
(defun read-line-into-buffer (stream buf)
"Read a line from a stream into a buffer, after reinitializing it."
(declare (buffered-ascii-input-stream stream) (buffer buf) #.*optimization*)
(reinitialize-buffer buf)
(with-ascii-stream-input-buffer buffer stream
(loop (let ((index (buffered-ascii-input-stream-index stream))
(limit (buffered-ascii-input-stream-limit stream)))
(declare (fixnum index limit))
(loop for end of-type fixnum from index below limit do
(when (char= (bchar buffer end) #\Newline)
(io-buffer-extend buf buffer index end)
(setf (buffered-ascii-input-stream-index stream) (the fixnum (1+ end)))
(return-from read-line-into-buffer buf))
finally
(io-buffer-extend buf buffer index limit)
(setf (buffered-ascii-input-stream-index stream) limit)))
(unless (stream-fill-buffer stream)
(return-from read-line-into-buffer buf)))))
(defun buffer-partition (buf separator &rest targets)
"Get substrings from a buffer and feed them to target buffers after reinitializing them;
separator is a character, like #\Tab;
targets is a property list with numbers as keys and buffers as values;
the targets need to be sorted by key;
for example (buffer-partition buf #\Tab 3 buf1 6 buf2)"
(declare (buffer buf) (base-char separator) (dynamic-extent targets) #.*optimization*)
(loop for (nil buffer) on targets by #'cddr do (reinitialize-buffer buffer))
(let ((current-target 0))
(declare (fixnum current-target))
(flet ((get-target-buf ()
(if targets
(when (= current-target (the fixnum (car targets)))
(pop targets)
(pop targets))
(return-from buffer-partition (values)))))
(declare (inline get-target-buf))
(let ((target-buf (get-target-buf)))
(declare ((or buffer null) target-buf))
(flet ((next-target ()
(incf current-target)
(setq target-buf (get-target-buf))))
(declare (inline next-target))
(let ((pos (buffer-pos buf))
(str (buffer-str buf)))
(declare (fixnum pos) (simple-vector str))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(loop for i of-type fixnum below hi
for chunk of-type simple-base-string = (svref str i)
for start of-type fixnum = 0 do
(loop for end of-type fixnum below +buffer-chunk-size+ do
(when (char= (schar chunk end) separator)
(when target-buf
(buffer-extend target-buf chunk start end))
(next-target)
(setq start (the fixnum (1+ end))))
finally
(when target-buf
(buffer-extend target-buf chunk start +buffer-chunk-size+))))
(when (> lo 0)
(loop with chunk of-type simple-base-string = (svref str hi)
with start of-type fixnum = 0
for end of-type fixnum below +buffer-chunk-size+ do
(when (char= (schar chunk end) separator)
(when target-buf
(buffer-extend target-buf chunk start end))
(next-target)
(setq start (the fixnum (1+ end))))
finally
(when target-buf
(buffer-extend target-buf chunk start lo))))))))))
(values))
(defun buffer-string (buf)
"Return a string representation of the active contents of a buffer.
Use this only for debugging. When writing to a stream, use write-buffer instead."
(declare (buffer buf) #.*optimization*)
(let ((pos (buffer-pos buf))
(str (buffer-str buf)))
(declare (fixnum pos) (simple-vector str))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(let ((result (make-array pos :element-type 'base-char
#+lispworks :single-thread #+lispworks t))
(target -1))
(declare (simple-base-string result) (fixnum target))
(loop for i of-type fixnum below hi
for chunk of-type simple-base-string = (svref str i) do
(loop for j of-type fixnum below +buffer-chunk-size+ do
(setf (schar result (incf target))
(schar chunk j))))
(when (> lo 0)
(loop with chunk of-type simple-base-string = (svref str hi)
for j of-type fixnum below lo do
(setf (schar result (incf target))
(schar chunk j))))
result))))
(defun buffer= (buf1 buf2)
"Compare the active contents of two buffers."
(declare (buffer buf1 buf2) #.*optimization*)
(or (eq buf1 buf2)
(let ((pos1 (buffer-pos buf1))
(str1 (buffer-str buf1))
(pos2 (buffer-pos buf2))
(str2 (buffer-str buf2)))
(declare (fixnum pos1 pos2) (simple-vector str1 str2))
(when (= pos1 pos2)
(multiple-value-bind (hi lo) (floor pos1 +buffer-chunk-size+)
(declare (fixnum hi lo))
(loop for i of-type fixnum below hi
for chunk1 of-type simple-base-string = (svref str1 i)
for chunk2 of-type simple-base-string = (svref str2 i) do
(loop for j of-type fixnum below +buffer-chunk-size+ do
(when (char/= (schar chunk1 j)
(schar chunk2 j))
(return-from buffer= nil))))
(when (> lo 0)
(loop with chunk1 of-type simple-base-string = (svref str1 hi)
with chunk2 of-type simple-base-string = (svref str2 hi)
for j of-type fixnum below lo do
(when (char/= (schar chunk1 j)
(schar chunk2 j))
(return-from buffer= nil)))))
t))))
(defun buffer-parse-integer (buf)
"Parse a buffer as an integer."
(declare (buffer buf) #.*optimization*)
(let ((pos (buffer-pos buf))
(str (buffer-str buf))
(sign +1)
(result 0))
(declare (fixnum pos) (simple-vector str) (fixnum sign) (integer result))
(flet ((update-result (char)
(declare (base-char char))
(assert (and (char<= #\0) (char<= #\9)))
(let ((digit (- (char-code char) #.(char-code #\0))))
(declare (fixnum digit))
(if (and (typep result 'fixnum) (< (the fixnum result) #.(floor most-positive-fixnum 10)))
(setq result (the fixnum (+ (the fixnum (* (the fixnum result) 10)) digit)))
(setq result (+ (* result 10) digit))))))
(declare (inline update-result))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(cond ((= hi 0)
(assert (> lo 0))
(let* ((chunk (svref str 0)) (char (schar chunk 0)) (start 0))
(declare (simple-base-string chunk) (base-char char) (fixnum start))
(cond ((char= char #\+) (setq start 1) (assert (> lo 1)))
((char= char #\-) (setq sign -1) (setq start 1) (assert (> lo 1))))
(loop for j of-type fixnum from start below lo
do (update-result (schar chunk j)))))
(t (let* ((chunk (svref str 0)) (char (schar chunk 0)) (start 0))
(declare (simple-base-string chunk) (base-char char) (fixnum start))
(cond ((char= char #\+) (setq start 1))
((char= char #\-) (setq sign -1) (setq start 1)))
(loop for j of-type fixnum from start below +buffer-chunk-size+
do (update-result (schar chunk j))))
(loop for i of-type fixnum from 1 below hi
for chunk of-type simple-base-string = (svref str i) do
(loop for j of-type fixnum below +buffer-chunk-size+
do (update-result (schar chunk j))))
(when (> lo 0)
(loop with chunk of-type simple-base-string = (svref str hi)
for j of-type fixnum below lo
do (update-result (schar chunk j))))))))
(* sign result)))
(declaim (inline rotate-1))
(defun rotate-1 (n)
"Rotate a fixnum by one position."
(declare (fixnum n) #.*optimization*)
(the fixnum (logior (ash n -1) (the fixnum (ash (logand n 1) #.(1- (integer-length most-positive-fixnum)))))))
(defun buffer-hash (buf)
"Get the hash code for a buffer; once a hash code is computed, the buffer shouldn't change anymore.
This can be used for hash tables, like in (make-hash-table :test #'buffer= :hash-function #'buffer-hash)."
(declare (buffer buf) #.*optimization*)
(let ((pos (buffer-pos buf))
(str (buffer-str buf))
(hash (buffer-hash-value buf)))
(declare (fixnum pos) (simple-vector str) (fixnum hash))
(if (> hash -1)
(return-from buffer-hash hash)
(setq hash 0))
(multiple-value-bind (hi lo) (floor pos +buffer-chunk-size+)
(declare (fixnum hi lo))
(loop for i of-type fixnum below hi
for chunk of-type simple-base-string = (svref str i) do
(loop for j of-type fixnum below +buffer-chunk-size+ do
(setq hash (logxor (rotate-1 hash) (char-code (schar chunk j))))))
(when (> lo 0)
(loop with chunk of-type simple-base-string = (svref str hi)
for j of-type fixnum below lo do
(setq hash (logxor (rotate-1 hash) (char-code (schar chunk j)))))))
(setf (buffer-hash-value buf) hash)))