Skip to content

Commit c094750

Browse files
committed
substr-rope
1 parent 3f4076e commit c094750

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ With [Ultralisp](https://ultralisp.org/) installed:
2222
Convert a string to a rope with `make-rope`, and turn it back into a
2323
string with `write-rope` passing `nil` as the output.
2424

25+
You can also pass an input stream or pathname to `make-rope` to
26+
efficiently read files.
27+
2528
```lisp
2629
(let* ((rope (rope:make-rope "Immutable Ropes for Common Lisp"))
2730
(super (rope:insert-rope rope 10 "Super "))
@@ -32,7 +35,7 @@ string with `write-rope` passing `nil` as the output.
3235
(rope:write-rope superer nil)))
3336
```
3437

35-
Split a rope at an index
38+
Split a rope at an index:
3639

3740
```lisp
3841
(let ((rope (rope:make-rope "Immutable Super Ropes for Common Lisp")))
@@ -56,6 +59,21 @@ Concatenate ropes together:
5659

5760
![Concatenated Rope](screenshots/concat.png)
5861

62+
Kill a segment of a rope:
63+
64+
```lisp
65+
(let ((rope (rope:make-rope "Immutable Ropes for Common Lisp")))
66+
(rope:write-rope (rope:kill-rope rope 20 27) t))
67+
```
68+
69+
Get chars or strings at a position:
70+
71+
```lisp
72+
(let ((rope (rope:make-rope "Immutable Ropes for Common Lisp")))
73+
(print (rope:index-rope rope 2))
74+
(print (rope:substr-rope rope 10 15)))
75+
```
76+
5977
# Dev Utils
6078

6179
If you want to generate graphs as shown above, you will need to

package.lisp

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
(:export
44
#:rope ; CLASS
55
#:rope-length ; ACCESSOR
6-
#:rope-depth ; ACCESSOR
7-
#:branch ; CLASS
8-
#:branch-left ; ACCESSOR
9-
#:branch-right ; ACCESSOR
10-
#:leaf ; CLASS
11-
#:leaf-string ; ACCESSOR
126
#:make-rope ; CONSTRUCTOR
137
#:write-rope ; FUNCTION
148
#:prepend-rope ; FUNCTION
159
#:append-rope ; FUNCTION
1610
#:insert-rope ; FUNCTION
1711
#:index-rope ; FUNCTION
12+
#:substr-rope ; FUNCTION
1813
#:concat-rope ; FUNCTION
1914
#:split-rope ; FUNCTION
2015
#:kill-rope ; FUNCTION

rope.lisp

+8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@
180180
(index-rope (branch-left rope) index)
181181
(index-rope (branch-right rope) (- index weight))))))
182182

183+
(defun substr-rope (rope from &optional to)
184+
"Get a substring out of a rope."
185+
(multiple-value-bind (ante _) (split-rope rope (or to (rope-length rope)))
186+
(declare (ignore _))
187+
(multiple-value-bind (_ post) (split-rope ante from)
188+
(declare (ignore _))
189+
(write-rope post nil))))
190+
183191
;;--------;;
184192
;; Concat ;;
185193
;;--------;;

test/basic.lisp

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ can be done efficiently.")
4141
(is (string= "Hello, super rope!" (rope:write-rope super nil)))))
4242

4343
(deftest index-rope ()
44-
"Test accessing characters by index"
44+
"Test accessing characters and strings by index"
4545
(let* ((rope (rope:make-rope "0123456789")))
4646
(is (string= #\1 (rope:index-rope rope 1)))
47-
(is (string= #\9 (rope:index-rope rope 9)))))
47+
(is (string= #\9 (rope:index-rope rope 9))))
48+
(let ((rope (rope:make-rope "Immutable Ropes for Common Lisp")))
49+
(is (string= "Ropes" (rope:substr-rope rope 10 15)))))
50+
4851

4952
(deftest read-files-and-streams ()
5053
"Test reading a file to a rope."

0 commit comments

Comments
 (0)