Debian Codegraph @

%3a14/srfi-14.scm

raw symbols SCIP
1 ; Part of Scheme 48 1.9. See file COPYING for notices and license.
2
3 ; Authors: Mike Sperber, Robert Tansom
4
5 ; Copyright (c) 2005-2006 by Basis Technology Corporation.
6
7 ; This is basically a complete re-implementation, suitable for Unicode.
8
9 ; Some bits and pieces from Olin's reference implementation remain,
10 ; but none from the MIT Scheme code. For whatever remains, the
11 ; following copyright holds:
12
13 ; Copyright (c) 1994-2003 by Olin Shivers
14 ;
15 ; All rights reserved.
16 ;
17 ; Redistribution and use in source and binary forms, with or without
18 ; modification, are permitted provided that the following conditions
19 ; are met:
20 ; 1. Redistributions of source code must retain the above copyright
21 ; notice, this list of conditions and the following disclaimer.
22 ; 2. Redistributions in binary form must reproduce the above copyright
23 ; notice, this list of conditions and the following disclaimer in the
24 ; documentation and/or other materials provided with the distribution.
25 ; 3. The name of the authors may not be used to endorse or promote products
26 ; derived from this software without specific prior written permission.
27 ;
28 ; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
29 ; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 ; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 ; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 ; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 ; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 ; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 (define-record-type :char-set
40 (make-char-set simple i-list)
41 char-set?
42 ;; byte vector for the Latin-1 part
43 (simple char-set-simple
44 set-char-set-simple!)
45 ;; inversion list for the rest
46 (i-list char-set-i-list
47 set-char-set-i-list!))
48
49 (define-record-discloser :char-set
50 (lambda (cs)
51 (list 'char-set
52 (char-set-size cs))))
53
54 (define (make-char-set-immutable! char-set)
55 (make-immutable! char-set)
56 (make-immutable! (char-set-simple char-set)))
57 ; inversion lists are always immutable
58
59 ;;; "Simple Csets"---we use mutable byte vectors for the Latin-1 part
60
61 (define *simple-cset-boundary* 256)
62
63 (define (simple-char? c)
64 (< (char->scalar-value c) *simple-cset-boundary*))
65
66 (define (make-empty-simple-cset)
67 (make-byte-vector *simple-cset-boundary* 0))
68
69 (define (make-full-simple-cset)
70 (make-byte-vector *simple-cset-boundary* 1))
71
72 (define (copy-simple-cset s)
73 (byte-vector-copy s))
74
75 ; don't mistake these for abstractions
76 (define (simple-cset-code-not-member? s i) (zero? (byte-vector-ref s i)))
77 (define (simple-cset-code-member? s i) (not (simple-cset-code-not-member? s i)))
78 (define (simple-cset-ref s i) (byte-vector-ref s i))
79 (define (simple-cset-set! s i v) (byte-vector-set! s i v))
80 (define (simple-cset-remove-code! s i) (byte-vector-set! s i 0))
81 (define (simple-cset-adjoin-code! s i) (byte-vector-set! s i 1))
82
83 (define (simple-cset-contains? s char)
84 (simple-cset-code-member? s (char->scalar-value char)))
85
86 (define (simple-cset=? s1 s2)
87 (byte-vector=? s1 s2))
88
89 (define (simple-cset<=? s1 s2)
90 (or (eq? s1 s2)
91 (let loop ((i 0))
92 (if (>= i *simple-cset-boundary*)
93 #t
94 (and (<= (simple-cset-ref s1 i) (simple-cset-ref s2 i))
95 (loop (+ 1 i)))))))
96
97 (define (simple-cset-size s)
98 (let loop ((i 0) (size 0))
99 (if (>= i *simple-cset-boundary*)
100 size
101 (loop (+ 1 i) (+ size (simple-cset-ref s i))))))
102
103 (define (simple-cset-count pred s)
104 (let loop ((i 0) (count 0))
105 (if (>= i *simple-cset-boundary*)
106 count
107 (loop (+ 1 i)
108 (if (and (simple-cset-code-member? s i) (pred (scalar-value->char i)))
109 (+ count 1)
110 count)))))
111
112 (define (simple-cset-modify! set s chars)
113 (for-each (lambda (c) (set s (char->scalar-value c)))
114 chars)
115 s)
116
117 (define (simple-cset-modify set s chars)
118 (simple-cset-modify! set (copy-simple-cset s) chars))
119
120 (define (simple-cset-adjoin s . chars)
121 (simple-cset-modify simple-cset-adjoin-code! s chars))
122 (define (simple-cset-adjoin! s . chars)
123 (simple-cset-modify! simple-cset-adjoin-code! s chars))
124 (define (simple-cset-delete s . chars)
125 (simple-cset-modify simple-cset-remove-code! s chars))
126 (define (simple-cset-delete! s . chars)
127 (simple-cset-modify! simple-cset-remove-code! s chars))
128
129 ;;; If we represented char sets as a bit set, we could do the following
130 ;;; trick to pick the lowest bit out of the set:
131 ;;; (count-bits (xor (- cset 1) cset))
132 ;;; (But first mask out the bits already scanned by the cursor first.)
133
134 (define (simple-cset-cursor-next s cursor)
135 (let loop ((cur cursor))
136 (let ((cur (- cur 1)))
137 (if (or (< cur 0) (simple-cset-code-member? s cur))
138 cur
139 (loop cur)))))
140
141 (define (end-of-simple-cset? cursor)
142 (negative? cursor))
143
144 (define (simple-cset-cursor-ref cursor)
145 (scalar-value->char cursor))
146
147 (define (simple-cset-for-each proc s)
148 (let loop ((i 0))
149 (if (< i *simple-cset-boundary*)
150 (begin
151 (if (simple-cset-code-member? s i)
152 (proc (scalar-value->char i)))
153 (loop (+ 1 i))))))
154
155 (define (simple-cset-fold kons knil s)
156 (let loop ((i 0) (ans knil))
157 (if (>= i *simple-cset-boundary*)
158 ans
159 (loop (+ 1 i)
160 (if (simple-cset-code-not-member? s i)
161 ans
162 (kons (scalar-value->char i) ans))))))
163
164 (define (simple-cset-every? pred s)
165 (let loop ((i 0))
166 (cond
167 ((>= i *simple-cset-boundary*)
168 #t)
169 ((or (simple-cset-code-not-member? s i)
170 (pred (scalar-value->char i)))
171 (loop (+ 1 i)))
172 (else
173 #f))))
174
175 (define (simple-cset-any pred s)
176 (let loop ((i 0))
177 (cond
178 ((>= i *simple-cset-boundary*) #f)
179 ((and (simple-cset-code-member? s i)
180 (pred (scalar-value->char i))))
181 (else
182 (loop (+ 1 i))))))
183
184 (define (ucs-range->simple-cset lower upper)
185 (let ((s (make-empty-simple-cset)))
186 (let loop ((i lower))
187 (if (< i upper)
188 (begin
189 (simple-cset-adjoin-code! s i)
190 (loop (+ 1 i)))))
191 s))
192
193 ; Algebra
194
195 ; These do various "s[i] := s[i] op val" operations
196
197 (define (simple-cset-invert-code! s i v)
198 (simple-cset-set! s i (- 1 v)))
199
200 (define (simple-cset-and-code! s i v)
201 (if (zero? v)
202 (simple-cset-remove-code! s i)))
203 (define (simple-cset-or-code! s i v)
204 (if (not (zero? v))
205 (simple-cset-adjoin-code! s i)))
206 (define (simple-cset-minus-code! s i v)
207 (if (not (zero? v))
208 (simple-cset-remove-code! s i)))
209 (define (simple-cset-xor-code! s i v)
210 (if (not (zero? v))
211 (simple-cset-set! s i (- 1 (simple-cset-ref s i)))))
212
213 (define (simple-cset-complement s)
214 (simple-cset-complement! (copy-simple-cset s)))
215
216 (define (simple-cset-complement! s)
217 (byte-vector-iter (lambda (i v) (simple-cset-invert-code! s i v)) s)
218 s)
219
220 (define (simple-cset-op! s simple-csets code-op!)
221 (for-each (lambda (s2)
222 (let loop ((i 0))
223 (if (< i *simple-cset-boundary*)
224 (begin
225 (code-op! s i (simple-cset-ref s2 i))
226 (loop (+ 1 i))))))
227 simple-csets)
228 s)
229
230 (define (simple-cset-union! s1 . ss)
231 (simple-cset-op! s1 ss simple-cset-or-code!))
232
233 (define (simple-cset-union . ss)
234 (if (pair? ss)
235 (apply simple-cset-union!
236 (byte-vector-copy (car ss))
237 (cdr ss))
238 (make-empty-simple-cset)))
239
240 (define (simple-cset-intersection! s1 . ss)
241 (simple-cset-op! s1 ss simple-cset-and-code!))
242
243 (define (simple-cset-intersection . ss)
244 (if (pair? ss)
245 (apply simple-cset-intersection!
246 (byte-vector-copy (car ss))
247 (cdr ss))
248 (make-full-simple-cset)))
249
250 (define (simple-cset-difference! s1 . ss)
251 (simple-cset-op! s1 ss simple-cset-minus-code!))
252
253 (define (simple-cset-difference s1 . ss)
254 (if (pair? ss)
255 (apply simple-cset-difference! (copy-simple-cset s1) ss)
256 (copy-simple-cset s1)))
257
258 (define (simple-cset-xor! s1 . ss)
259 (simple-cset-op! s1 ss simple-cset-xor-code!))
260
261 (define (simple-cset-xor . ss)
262 (if (pair? ss)
263 (apply simple-cset-xor!
264 (byte-vector-copy (car ss))
265 (cdr ss))
266 (make-empty-simple-cset)))
267
268 (define (simple-cset-diff+intersection! s1 s2 . ss)
269 (byte-vector-iter (lambda (i v)
270 (cond
271 ((zero? v)
272 (simple-cset-remove-code! s2 i))
273 ((simple-cset-code-member? s2 i)
274 (simple-cset-remove-code! s1 i))))
275 s1)
276
277 (for-each (lambda (s)
278 (byte-vector-iter (lambda (i v)
279 (if (and (not (zero? v))
280 (simple-cset-code-member? s1 i))
281 (begin
282 (simple-cset-remove-code! s1 i)
283 (simple-cset-adjoin-code! s2 i))))
284 s))
285 ss)
286
287 (values s1 s2))
288
289
290
291 ; Compute (c + 37 c + 37^2 c + ...) modulo BOUND, with sleaze thrown
292 ; in to keep the intermediate values small. (We do the calculation
293 ; with just enough bits to represent BOUND, masking off high bits at
294 ; each step in calculation. If this screws up any important properties
295 ; of the hash function I'd like to hear about it. -Olin)
296
297 (define (simple-cset-hash s bound)
298 ;; The mask that will cover BOUND-1:
299 (let ((mask (let loop ((i #x10000)) ; Let's skip first 16 iterations, eh?
300 (if (>= i bound) (- i 1) (loop (+ i i))))))
301 (let loop ((i (- *simple-cset-boundary* 1)) (ans 0))
302 (if (< i 0)
303 (modulo ans bound)
304 (loop (- i 1)
305 (if (simple-cset-code-not-member? s i)
306 ans
307 (bitwise-and mask (+ (* 37 ans) i))))))))
308
309 ;;; Now for the real character sets
310
311 (define (make-empty-char-set)
312 (make-char-set (make-empty-simple-cset)
313 (make-empty-inversion-list *simple-cset-boundary* (+ 1 #x10ffff))))
314 (define (make-full-char-set)
315 (make-char-set (make-full-simple-cset)
316 (range->inversion-list *simple-cset-boundary* (+ 1 #x10ffff)
317 *simple-cset-boundary* (+ 1 #x10ffff))))
318
319 (define (char-set-copy cs)
320 (make-char-set (copy-simple-cset (char-set-simple cs))
321 (inversion-list-copy (char-set-i-list cs))))
322
323 ; n-ary version
324 (define (char-set= . rest)
325 (or (null? rest)
326 (let ((cs1 (car rest))
327 (rest (cdr rest)))
328 (let loop ((rest rest))
329 (or (not (pair? rest))
330 (and (char-set=/2 cs1 (car rest))
331 (loop (cdr rest))))))))
332
333 ; binary version
334 (define (char-set=/2 cs-1 cs-2)
335 (and (simple-cset=? (char-set-simple cs-1) (char-set-simple cs-2))
336 (inversion-list=? (char-set-i-list cs-1)
337 (char-set-i-list cs-2))))
338
339 ; n-ary
340 (define (char-set<= . rest)
341 (or (null? rest)
342 (let ((cs1 (car rest))
343 (rest (cdr rest)))
344 (let loop ((cs1 cs1) (rest rest))
345 (or (not (pair? rest))
346 (and (char-set<=/2 cs1 (car rest))
347 (loop (car rest) (cdr rest))))))))
348
349 ; binary
350 (define (char-set<=/2 cs-1 cs-2)
351 (and (simple-cset<=? (char-set-simple cs-1) (char-set-simple cs-2))
352 (inversion-list<=? (char-set-i-list cs-1)
353 (char-set-i-list cs-2))))
354
355 (define (inversion-list<=? i-list-1 i-list-2)
356 (inversion-list=? i-list-1
357 (inversion-list-intersection i-list-1 i-list-2)))
358
359 ;;; Hash
360
361 ; We follow Olin's reference implementation:
362 ;
363 ; If you keep BOUND small enough, the intermediate calculations will
364 ; always be fixnums. How small is dependent on the underlying Scheme system;
365 ; we use a default BOUND of 2^22 = 4194304, which should hack it in
366 ; Schemes that give you at least 29 signed bits for fixnums. The core
367 ; calculation that you don't want to overflow is, worst case,
368 ; (+ 65535 (* 37 (- bound 1)))
369 ; where 65535 is the max character code. Choose the default BOUND to be the
370 ; biggest power of two that won't cause this expression to fixnum overflow,
371 ; and everything will be copacetic.
372
373 (define char-set-hash
374 (opt-lambda (cs (bound 4194304))
375 (if (not (and (integer? bound)
376 (exact? bound)
377 (<= 0 bound)))
378 (assertion-violation 'char-set-hash "invalid bound" bound))
379 (let ((bound (if (zero? bound) 4194304 bound)))
380 (modulo (+ (simple-cset-hash (char-set-simple cs) bound)
381 (* 37 (inversion-list-hash (char-set-i-list cs) bound)))
382 bound))))
383
384 (define (char-set-contains? cs char)
385 (if (simple-char? char)
386 (simple-cset-contains? (char-set-simple cs) char)
387 (inversion-list-member? (char->scalar-value char)
388 (char-set-i-list cs))))
389
390 (define (char-set-size cs)
391 (+ (simple-cset-size (char-set-simple cs))
392 (inversion-list-size (char-set-i-list cs))))
393
394 (define (char-set-count pred cset)
395 (+ (simple-cset-count pred (char-set-simple cset))
396 (inversion-list-count pred (char-set-i-list cset))))
397
398 (define (inversion-list-count pred i-list)
399 (inversion-list-fold/done? (lambda (v count)
400 (if (pred (scalar-value->char v))
401 (+ 1 count)
402 count))
403 0
404 (lambda (v) #f)
405 i-list))
406
407 (define (make-char-set-char-op simple-cset-op inversion-list-op)
408 (lambda (cs . chars)
409 (call-with-values
410 (lambda () (partition-list simple-char? chars))
411 (lambda (simple-chars non-simple-chars)
412 (make-char-set (apply simple-cset-op (char-set-simple cs) simple-chars)
413 (apply inversion-list-op (char-set-i-list cs)
414 (map char->scalar-value non-simple-chars)))))))
415
416 (define (make-char-set-char-op! simple-cset-op! simple-cset-op
417 inversion-list-op)
418 (lambda (cs . chars)
419 (call-with-values
420 (lambda () (partition-list simple-char? chars))
421 (lambda (simple-chars non-simple-chars)
422 (if (null? non-simple-chars)
423 (apply simple-cset-op! (char-set-simple cs) simple-chars)
424 (begin
425 (set-char-set-simple! cs
426 (apply simple-cset-op (char-set-simple cs)
427 simple-chars))
428 (set-char-set-i-list! cs
429 (apply inversion-list-op (char-set-i-list cs)
430 (map char->scalar-value non-simple-chars)))))))
431 cs))
432
433 (define char-set-adjoin
434 (make-char-set-char-op simple-cset-adjoin inversion-list-adjoin))
435 (define char-set-adjoin!
436 (make-char-set-char-op! simple-cset-adjoin! simple-cset-adjoin
437 inversion-list-adjoin))
438 (define char-set-delete
439 (make-char-set-char-op simple-cset-delete inversion-list-remove))
440 (define char-set-delete!
441 (make-char-set-char-op! simple-cset-delete! simple-cset-delete
442 inversion-list-remove))
443
444 ;;; Cursors
445
446 ; A cursor is either an integer index into the mark vector (-1 for the
447 ; end-of-char-set cursor) as in the reference implementation, and an
448 ; inversion-list cursor otherwise.
449
450 (define (char-set-cursor cset)
451 (let ((simple-cursor
452 (simple-cset-cursor-next (char-set-simple cset)
453 *simple-cset-boundary*)))
454 (if (end-of-simple-cset? simple-cursor)
455 (inversion-list-cursor (char-set-i-list cset))
456 simple-cursor)))
457
458 (define (end-of-char-set? cursor)
459 (and (inversion-list-cursor? cursor)
460 (inversion-list-cursor-at-end? cursor)))
461
462 (define (char-set-ref cset cursor)
463 (if (number? cursor)
464 (simple-cset-cursor-ref cursor)
465 (scalar-value->char (inversion-list-cursor-ref cursor))))
466
467 (define (char-set-cursor-next cset cursor)
468 (cond
469 ((number? cursor)
470 (let ((next (simple-cset-cursor-next (char-set-simple cset) cursor)))
471 (if (end-of-simple-cset? next)
472 (inversion-list-cursor (char-set-i-list cset))
473 next)))
474 (else
475 (inversion-list-cursor-next (char-set-i-list cset) cursor))))
476
477 (define (char-set-for-each proc cs)
478 (simple-cset-for-each proc (char-set-simple cs))
479 (inversion-list-fold/done? (lambda (n _)
480 (proc (scalar-value->char n))
481 (unspecific))
482 #f
483 (lambda (_) #f)
484 (char-set-i-list cs)))
485
486 ; this is pretty inefficent
487 (define (char-set-map proc cs)
488 (let ((simple-cset (make-empty-simple-cset))
489 (other-scalar-values '()))
490
491 (define (adjoin! c)
492 (let ((c (proc c)))
493 (if (simple-char? c)
494 (simple-cset-adjoin! simple-cset c)
495 (set! other-scalar-values
496 (cons (char->scalar-value c) other-scalar-values)))))
497
498 (char-set-for-each adjoin! cs)
499
500 (make-char-set simple-cset
501 (apply numbers->inversion-list
502 *simple-cset-boundary* (+ 1 #x10ffff)
503 other-scalar-values))))
504
505 (define (char-set-fold kons knil cs)
506 (inversion-list-fold/done? (lambda (n v)
507 (kons (scalar-value->char n) v))
508 (simple-cset-fold kons knil (char-set-simple cs))
509 (lambda (_) #f)
510 (char-set-i-list cs)))
511
512 (define (char-set-every pred cs)
513 (and (simple-cset-every? pred (char-set-simple cs))
514 (inversion-list-fold/done? (lambda (n v)
515 (and v
516 (pred (scalar-value->char n))))
517 #t
518 not
519 (char-set-i-list cs))))
520
521 (define (char-set-any pred cs)
522 (or (simple-cset-any pred (char-set-simple cs))
523 (inversion-list-fold/done? (lambda (n v)
524 (or v
525 (pred (scalar-value->char n))))
526 #f
527 values
528 (char-set-i-list cs))))
529
530 (define (base-char-set maybe-base-cs)
531 (if maybe-base-cs
532 (char-set-copy maybe-base-cs)
533 (make-empty-char-set)))
534
535 (define char-set-unfold
536 (opt-lambda (p f g seed (maybe-base-cs #f))
537 (char-set-unfold! p f g seed
538 (base-char-set maybe-base-cs))))
539
540 (define (char-set-unfold! p f g seed base-cs)
541 (let loop ((seed seed) (cs base-cs))
542 (if (p seed) cs ; P says we are done.
543 (loop (g seed) ; Loop on (G SEED).
544 (char-set-adjoin! cs (f seed)))))) ; Add (F SEED) to set.
545
546 ; converting from and to lists
547
548 (define (char-set . chars)
549 (list->char-set chars))
550
551 (define list->char-set
552 (opt-lambda (chars (maybe-base-cs #f))
553 (list->char-set! chars
554 (base-char-set maybe-base-cs))))
555
556 (define (list->char-set! chars cs)
557 (for-each (lambda (c)
558 (char-set-adjoin! cs c))
559 chars)
560 cs)
561
562 (define (char-set->list cs)
563 (char-set-fold cons '() cs))
564
565 ; converting to and from strings
566
567 (define string->char-set
568 (opt-lambda (str (maybe-base-cs #f))
569 (string->char-set! str
570 (base-char-set maybe-base-cs))))
571
572 (define (string->char-set! str cs)
573 (do ((i (- (string-length str) 1) (- i 1)))
574 ((< i 0))
575 (char-set-adjoin! cs (string-ref str i)))
576 cs)
577
578 (define (char-set->string cs)
579 (let ((ans (make-string (char-set-size cs))))
580 (char-set-fold (lambda (ch i)
581 (string-set! ans i ch)
582 (+ i 1))
583 0
584 cs)
585 ans))
586
587 (define ucs-range->char-set
588 (opt-lambda (lower upper (error? #f) (maybe-base-cs #f))
589 (ucs-range->char-set! lower upper error?
590 (base-char-set maybe-base-cs))))
591
592 (define (ucs-range->char-set! lower upper error? base-cs)
593 (if (negative? lower)
594 (assertion-violation 'ucs-range->char-set! "negative lower bound" lower))
595 (if (> lower #x10ffff)
596 (assertion-violation 'ucs-range->char-set! "invalid lower bound" lower))
597 (if (negative? upper)
598 (assertion-violation 'ucs-range->char-set! "negative upper bound" upper))
599 (if (> upper #x110000)
600 (assertion-violation 'ucs-range->char-set! "invalid lower bound" upper))
601 (if (not (<= lower upper))
602 (assertion-violation 'ucs-range->char-set! "decreasing bounds" lower upper))
603
604 (let ((create-inversion-list
605 (lambda (lower upper)
606 (cond
607 ((and (>= lower #xD800)
608 (>= #xe000 upper))
609 (make-empty-inversion-list *simple-cset-boundary* (+ 1 #x10ffff)))
610 ((<= upper #xe000)
611 (range->inversion-list *simple-cset-boundary* (+ 1 #x10ffff)
612 lower (min #xd800 upper)))
613 ((>= lower #xd800)
614 (range->inversion-list *simple-cset-boundary* (+ 1 #x10ffff)
615 (max #xe000 lower) upper))
616 (else
617 ;; hole
618 (ranges->inversion-list *simple-cset-boundary* (+ 1 #x10ffff)
619 (cons lower #xd800)
620 (cons #xe000 upper)))))))
621 (char-set-union!
622 base-cs
623 (cond
624 ((>= lower *simple-cset-boundary*)
625 (make-char-set (make-empty-simple-cset)
626 (create-inversion-list lower upper)))
627 ((< upper *simple-cset-boundary*)
628 (make-char-set (ucs-range->simple-cset lower upper)
629 (make-empty-inversion-list *simple-cset-boundary* (+ 1 #x10ffff))))
630 (else
631 (make-char-set (ucs-range->simple-cset lower *simple-cset-boundary*)
632 (create-inversion-list *simple-cset-boundary* upper)))))))
633
634 (define char-set-filter
635 (opt-lambda (predicate domain (maybe-base-cs #f))
636 (char-set-filter! predicate
637 domain
638 (base-char-set maybe-base-cs))))
639
640 (define (char-set-filter! predicate domain base-cs)
641 (char-set-fold (lambda (ch _)
642 (if (predicate ch)
643 (char-set-adjoin! base-cs ch)))
644 (unspecific)
645 domain)
646 base-cs)
647
648 ; {string, char, char-set, char predicate} -> char-set
649
650 ; This is called ->CHAR-SET in the SRFI, but that's not a valid R5RS
651 ; identifier.
652
653 (define (x->char-set x)
654 (cond ((char-set? x) x)
655 ((string? x) (string->char-set x))
656 ((char? x) (char-set x))
657 (else (assertion-violation 'x->char-set "Not a charset, string or char."))))
658
659
660 ; Set algebra
661
662 (define *surrogate-complement-i-list*
663 (inversion-list-complement
664 (range->inversion-list *simple-cset-boundary* (+ 1 #x10ffff)
665 #xd800 #xe000)))
666
667 (define (char-set-complement cs)
668 (make-char-set (simple-cset-complement (char-set-simple cs))
669 (inversion-list-intersection
670 (inversion-list-complement (char-set-i-list cs))
671 *surrogate-complement-i-list*)))
672
673 (define (char-set-complement! cs)
674 (set-char-set-simple! cs
675 (simple-cset-complement! (char-set-simple cs)))
676 (set-char-set-i-list! cs
677 (inversion-list-intersection
678 (inversion-list-complement (char-set-i-list cs))
679 *surrogate-complement-i-list*))
680 cs)
681
682 (define (make-char-set-op! simple-cset-op! inversion-list-op)
683 (lambda (cset1 . csets)
684 (set-char-set-simple! cset1
685 (apply simple-cset-op!
686 (char-set-simple cset1)
687 (map char-set-simple csets)))
688 (set-char-set-i-list! cset1
689 (apply inversion-list-op
690 (char-set-i-list cset1)
691 (map char-set-i-list csets)))
692 cset1))
693
694 (define (make-char-set-op char-set-op! make-neutral)
695 (lambda csets
696 (if (pair? csets)
697 (apply char-set-op! (char-set-copy (car csets)) (cdr csets))
698 (make-neutral))))
699
700 (define char-set-union!
701 (make-char-set-op! simple-cset-union! inversion-list-union))
702 (define char-set-union
703 (make-char-set-op char-set-union! make-empty-char-set))
704
705 (define char-set-intersection!
706 (make-char-set-op! simple-cset-intersection! inversion-list-intersection))
707 (define char-set-intersection
708 (make-char-set-op char-set-intersection! make-full-char-set))
709
710 (define char-set-difference!
711 (make-char-set-op! simple-cset-difference! inversion-list-difference))
712
713 (define (char-set-difference cset1 . csets)
714 (apply char-set-difference! (char-set-copy cset1) csets))
715
716 ; copied from inversion-list.scm
717 (define (binary->n-ary proc/2)
718 (lambda (arg-1 . args)
719 (if (and (pair? args)
720 (null? (cdr args)))
721 (proc/2 arg-1 (car args))
722 (let loop ((args args)
723 (result arg-1))
724 (if (null? args)
725 result
726 (loop (cdr args) (proc/2 result (car args))))))))
727
728 (define inversion-list-xor
729 (binary->n-ary
730 (lambda (i-list-1 i-list-2)
731 (inversion-list-union (inversion-list-intersection
732 (inversion-list-complement i-list-1)
733 i-list-2)
734 (inversion-list-intersection
735 i-list-1
736 (inversion-list-complement i-list-2))))))
737
738 ; Really inefficient for things outside Latin-1
739 ; WHO NEEDS THIS NONSENSE, ANYWAY?
740 (define char-set-xor!
741 (make-char-set-op! simple-cset-xor! inversion-list-xor))
742
743 (define char-set-xor
744 (make-char-set-op char-set-xor! make-empty-char-set))
745
746 (define (char-set-diff+intersection! cs1 cs2 . csets)
747 (call-with-values
748 (lambda () (apply simple-cset-diff+intersection!
749 (char-set-simple cs1) (char-set-simple cs2)
750 (map char-set-simple csets)))
751 (lambda (simple-diff simple-intersection)
752 (set-char-set-simple! cs1 simple-diff)
753 (set-char-set-simple! cs2 simple-intersection)
754 (let ((i-list-1 (char-set-i-list cs1))
755 (i-list-2 (char-set-i-list cs2))
756 (i-list-rest (map char-set-i-list csets)))
757 (set-char-set-i-list! cs1
758 (apply inversion-list-difference
759 i-list-1 i-list-2
760 i-list-rest))
761 (set-char-set-i-list! cs2
762 (inversion-list-intersection
763 i-list-1
764 (apply inversion-list-union
765 i-list-2
766 i-list-rest)))
767 (values cs1 cs2)))))
768
769 (define (char-set-diff+intersection cs1 . csets)
770 (apply char-set-diff+intersection!
771 (char-set-copy cs1)
772 (make-empty-char-set)
773 csets))
774
775 ;; Byte vector utilities
776
777 (define (byte-vector-copy b)
778 (let* ((size (byte-vector-length b))
779 (result (make-byte-vector size 0)))
780 (copy-bytes! b 0 result 0 size)
781 result))
782
783 ;;; Apply P to each index and its char code in S: (P I VAL).
784 ;;; Used by the set-algebra ops.
785
786 (define (byte-vector-iter p s)
787 (let loop ((i (- (byte-vector-length s) 1)))
788 (if (>= i 0)
789 (begin
790 (p i (byte-vector-ref s i))
791 (loop (- i 1))))))
792
793 ;; Utility for srfi-14-base-char-sets.scm, which follows
794
795 ; The range vector is an even-sized vector with [lower, upper)
796 ; pairs.
797
798 (define (range-vector->char-set range-vector)
799 (let ((size (vector-length range-vector))
800 (simple-cset (make-empty-simple-cset)))
801
802 (let loop ((index 0) (ranges '()))
803 (if (>= index size)
804 (make-char-set simple-cset
805 (apply ranges->inversion-list
806 *simple-cset-boundary* (+ 1 #x10ffff)
807 ranges))
808 (let ((lower (vector-ref range-vector index))
809 (upper (vector-ref range-vector (+ 1 index))))
810
811 (define (fill-simple-cset! lower upper)
812 (let loop ((scalar-value lower))
813 (if (< scalar-value upper)
814 (begin
815 (simple-cset-adjoin-code! simple-cset scalar-value)
816 (loop (+ 1 scalar-value))))))
817
818
819 (cond
820 ((>= lower *simple-cset-boundary*)
821 (loop (+ 2 index) (cons (cons lower upper) ranges)))
822 ((< upper *simple-cset-boundary*)
823 (fill-simple-cset! lower upper)
824 (loop (+ 2 index) ranges))
825 (else
826 (fill-simple-cset! lower *simple-cset-boundary*)
827 (loop (+ 2 index)
828 (cons (cons *simple-cset-boundary* upper) ranges)))))))))