diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-01-06 23:26:22 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-01-06 23:26:22 +0100 |
commit | 6448e4f22f2132aa7d277f30be134794531b5ae0 (patch) | |
tree | 52c57d3a040b80a4b77cdf8264cffd29dfa552b7 | |
parent | b04ca229dcc07d901e6826bbe638d14b8b51ecac (diff) |
par: piece-table: make and index
-rw-r--r-- | par/piece-table.scm | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/par/piece-table.scm b/par/piece-table.scm index d6a3c4e..6876541 100644 --- a/par/piece-table.scm +++ b/par/piece-table.scm @@ -17,18 +17,34 @@ ;; The piece table itself; ;; original and add are strings and pieces is a list of pieces (define-record-type <piece-table> - (make-piece-table original add pieces) + (%make-piece-table original add pieces) piece-table? (original piece-table-original set-piece-table-original!) (add piece-table-add set-piece-table-add!) (pieces piece-table-pieces set-piece-table-pieces!)) +(define %DEFAULT-ADD-BUFFER-LENGTH 1024) + +(define (make-piece-table original) + (%make-piece-table + original + (make-string %DEFAULT-ADD-BUFFER-LENGTH) + (list (make-piece original 0 (string-length original) 'normal)))) + (define (add-piece-table-piece! piece-table piece) - (cons piece (piece-table-pieces piece-table))) + (append (piece-table-pieces piece-table) (list piece))) (define (piece-table-index piece-table pos) - #f) + (let loop ((pieces (piece-table-pieces piece-table)) + (start 0)) + (let* ((piece (car pieces)) + (end (piece-length piece))) + (if (<= start pos (- end 1)) + (string-ref (piece-buffer piece) (+ start pos)) + (if (not (null? (cdr pieces))) + (loop (cdr pieces) (+ start end)) + (eof-object)))))) (define (piece-table-insert! piece-table pos char) #f) @@ -49,9 +65,7 @@ ;; From/to string -(define (string->piece-table string) - (make-piece-table string "" - (list (make-piece string 0 (string-length string) 'normal)))) +(define string->piece-table make-piece-table) (define (piece-table->string piece-table) #f) |