From 49f7232b4c580197413528e03b699ca519905fdc Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Fri, 12 Jan 2024 23:02:55 +0100 Subject: par: piece-table: fix delete and tests --- par/piece-table.scm | 38 +++++++++++++++++++------------------- tests/piece-table.scm | 10 +++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/par/piece-table.scm b/par/piece-table.scm index 1c2940f..c638a2d 100644 --- a/par/piece-table.scm +++ b/par/piece-table.scm @@ -181,26 +181,26 @@ ; TODO: SET-CDR for the win? ; - That would need other way to find the affected pieces and manipulate them. ; > we'll leave it for the future -(define (piece-table-delete! piece-table pos) +(define (piece-table-delete! piece-table from len) (let*-values (((idx rem) (piece-table-text-pos->piece-idx+remainder - piece-table (+ 1 pos))) ; TODO because of the <= - ((beg piece end) (list-ref-with-context - (piece-table-pieces piece-table) - idx))) - (let ((piece-len (piece-length piece))) - (cond - ((= piece-len 1) - (append! beg end)) - ((= rem piece-len) - (set-piece-length! piece (- piece-len 1))) - ((= rem 1) - (set-piece-start! piece (+ 1 (piece-start piece)))) - (else - (let-values (((first second) (split-piece! piece rem))) - (set-piece-length! first (- rem 1)) - (set-piece-table-pieces! - piece-table - (append! beg (list first second) end)))))))) + piece-table from)) ; TODO because of the <= + ((end-idx end-rem) (piece-table-text-pos->piece-idx+remainder + piece-table (+ from len))) + ((beg piece _) (list-ref-with-context + (piece-table-pieces piece-table) + idx)) + + ((_ end-piece end) (list-ref-with-context + (piece-table-pieces piece-table) + end-idx))) + (let*-values (((first second) (split-piece! piece rem)) + ((third fourth) (split-piece! end-piece end-rem))) + (set-piece-table-pieces! + piece-table + (append! beg + (remove (lambda (p) (= 0 (piece-length p))) + (list first fourth)) + end))))) diff --git a/tests/piece-table.scm b/tests/piece-table.scm index b8b6ff1..d6c1105 100644 --- a/tests/piece-table.scm +++ b/tests/piece-table.scm @@ -23,10 +23,10 @@ (test-begin "delete") (define table (make-piece-table "HOLA SOY EKAITZ")) - (piece-table-delete! table 4) + (piece-table-delete! table 4 1) (test-equal "HOLASOY EKAITZ" (piece-table->string table)) - (piece-table-delete! table 0) - (test-equal "OLA SOY EKAITZ" (piece-table->string table)) - (piece-table-delete! table 13) - (test-equal "OLA SOY EKAIT" (piece-table->string table)) + (piece-table-delete! table 0 1) + (test-equal "OLASOY EKAITZ" (piece-table->string table)) + (piece-table-delete! table 12 1) + (test-equal "OLASOY EKAIT" (piece-table->string table)) (test-end "delete") -- cgit v1.2.3