File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 36
36
; ;-------;;
37
37
38
38
(defgeneric make-rope (source)
39
- (:documentation " Create a new rope from a string." )
39
+ (:documentation " Create a new rope from a string, stream, or pathname ." )
40
40
(:method ((source rope))
41
41
source)
42
+ (:method ((source stream ))
43
+ (labels ((read-leaves (&optional acc)
44
+ (let* ((string (make-string *long-leaf* ))
45
+ (length (read-sequence string source))
46
+ (leaf (make-instance ' leaf :length length :string (subseq string 0 length ))))
47
+ (if (= *long-leaf* length )
48
+ (read-leaves (cons leaf acc))
49
+ (cons leaf acc)))))
50
+ (let ((leaves (nreverse (read-leaves))))
51
+ (merge-leaves leaves 0 (length leaves)))))
52
+ (:method ((source pathname ))
53
+ (with-open-file (s source)
54
+ (make-rope s)))
42
55
(:method ((source string ))
43
56
(let ((length (length source)))
44
57
(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