File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
1
(in-package # :rope)
2
2
3
3
(defparameter *short-leaf* 16 )
4
+ (defparameter *target-leaf* 80 )
4
5
(defparameter *long-leaf* 128 )
5
6
6
7
(defclass rope ()
36
37
; ;-------;;
37
38
38
39
(defgeneric make-rope (source)
39
- (:documentation " Create a new rope from a string." )
40
+ (:documentation " Create a new rope from a string, stream, or pathname ." )
40
41
(:method ((source rope))
41
42
source)
43
+ (:method ((source stream ))
44
+ (labels ((read-leaves (&optional acc)
45
+ (let* ((string (make-string *long-leaf* ))
46
+ (length (read-sequence string source))
47
+ (leaf (make-instance ' leaf :length length :string (subseq string 0 length ))))
48
+ (if (= *long-leaf* length )
49
+ (read-leaves (cons leaf acc))
50
+ (cons leaf acc)))))
51
+ (let ((leaves (nreverse (read-leaves))))
52
+ (merge-leaves leaves 0 (length leaves)))))
53
+ (:method ((source pathname ))
54
+ (with-open-file (s source)
55
+ (make-rope s)))
42
56
(:method ((source string ))
43
57
(let ((length (length source)))
44
58
(if (<= *long-leaf* length )
Original file line number Diff line number Diff line change @@ -45,3 +45,12 @@ can be done efficiently.")
45
45
(let* ((rope (rope :make-rope " 0123456789" )))
46
46
(is (string= #\1 (rope :index-rope rope 1 )))
47
47
(is (string= #\9 (rope :index-rope rope 9 )))))
48
+
49
+ (deftest read-files-and-streams ()
50
+ " Test reading a file to a rope."
51
+ (let* ((pathname (merge-pathnames " README.md" (asdf :system-source-directory :rope )))
52
+ (rope (rope :make-rope pathname )))
53
+ (is (string= (uiop :read-file-string pathname ) (rope :write-rope rope nil ))))
54
+ (let* ((stream (make-string-input-stream *string-2* ))
55
+ (rope (rope :make-rope stream )))
56
+ (is (string= *string-2* (rope :write-rope rope nil )))))
You can’t perform that action at this time.
0 commit comments