(module rope (empty-rope current-maximum-leaf-length string->rope rope->string rope-length rope-depth rope rope? rope=? rope-null? rope-balanced? rope-balance rope-ref subrope rope-append rope-reverse rope-fold rope-for-each read-rope make-rope-iterator open-input-rope open-output-rope get-output-rope) (import scheme chicken srfi-1 srfi-13 ports extras) (require-library srfi-13) (include "rope.scm") (define (printer o p) (display "#" p)) (define-record-printer node printer) (define-record-printer leaf printer) (define (open-input-rope r) (make-input-port (make-rope-iterator r) (lambda () #t) void)) (define open-output-rope open-output-string) (define get-output-rope (case-lambda (() (get-output-rope (current-output-port))) ((port) (string->rope (get-output-string port))))) (define read-rope (case-lambda (() (read-rope (current-input-port) most-positive-fixnum)) ((port) (read-rope port most-positive-fixnum)) ((port len) (let ((mll (current-maximum-leaf-length))) (let lp ((i len) (a '())) (if (zero? i) (rope-concatenate (reverse a)) (let* ((part (read-string (min i mll) port)) (plen (string-length part))) (if (zero? plen) (lp 0 a) (lp (- i plen) (cons (make-leaf part plen) a)))))))))))