(define (void . args) (if #f #t #f)) (define string-copy! (case-lambda ((s1 s2) (string-copy! s1 0 s2 0 (string-length s2))) ((s1 i s2) (string-copy! s1 i s2 0 (string-length s2))) ((s1 i s2 s) (string-copy! s1 i s2 s (string-length s2))) ((s1 i s2 s e) (do ((i i (+ i 1)) (s s (+ s 1))) ((= s e) s1) (string-set! s1 i (string-ref s2 s)))))) (define string-reverse (case-lambda ((s1) (string-reverse s1 0 (string-length s1))) ((s1 s) (string-reverse s1 s (string-length s1))) ((s1 s e) (let* ((len (- e s)) (str (make-string len))) (do ((s s (+ s 1)) (e (- len 1) (- e 1))) ((< e 0) str) (string-set! str e (string-ref s1 s))))))) (define (fold f a l) (if (null? l) a (fold f (f (car l) a) (cdr l)))) (define (split-at l n) (let lp ((a '()) (l l) (n n)) (if (zero? n) (values (reverse a) l) (lp (cons (car l) a) (cdr l) (- n 1))))) (define (take l n) (call-with-values (lambda () (split-at l n)) (lambda (l r) l))) (define (find p l) (and (not (null? l)) (let ((v (car l))) (if (p v) v (find p (cdr l)))))) (include "rope.scm") (define read-rope (case-lambda (() (read-rope (current-input-port) +inf.0)) ((port) (read-rope port +inf.0)) ((port len) (let ((mll (current-maximum-leaf-length))) (let lp ((n 0) (a '())) (if (>= n len) (rope-concatenate (reverse a)) (let ((len (min len mll))) (let ((s (open-output-string))) (let ll ((i 0)) (if (>= i len) (lp (+ n i) (cons (make-leaf (get-output-string s)) a)) (let ((c (read-char port))) (if (eof-object? c) (ll +inf.0) (begin (display c s) (ll (+ i 1)))))))))))))))