ce-tokens: parse out \'s used for escaping tok

This commit is contained in:
xfnw 2023-11-11 18:13:16 -05:00
parent f3641ab1e3
commit 81400a9ad2

15
ce.cl
View File

@ -88,21 +88,22 @@
(subseq buffer 0 in) (subseq buffer 0 in)
(nthcdr (1+ out) buffer)))) (nthcdr (1+ out) buffer))))
(defun ce-tokens (str tok &optional (len (length str)) (defun ce-tokens (str tok &optional (len (length str)) (sta 0) (cur 0)
(sta 0) (cur 0) (bs nil) (out nil)) (bs nil) (out nil) (rep (pregexp (pregexp-quote
(format nil "\\~c" tok)))) (ins (format nil "~c" tok)))
"tokenize of str at character tok. respects backslashes for escaping." "tokenize of str at character tok. respects backslashes for escaping."
(if (< cur len) (if (< cur len)
(let ((c (char str cur))) (let ((c (char str cur)))
(if bs (if bs
; there was previously a backslash, ignore this char ; last char was a backslash, ignore this one
(ce-tokens str tok len sta (1+ cur) nil out) (ce-tokens str tok len sta (1+ cur) nil out rep ins)
(if (char= tok c) (if (char= tok c)
(ce-tokens str tok len (1+ cur) (1+ cur) nil (cons (ce-tokens str tok len (1+ cur) (1+ cur) nil (cons
(subseq str sta cur) out)) (pregexp-replace* rep (subseq str sta cur) ins) out) rep ins)
(ce-tokens str tok len sta (1+ cur) (char= #\\ c) out)))) (ce-tokens str tok len sta (1+ cur) (char= #\\ c) out rep ins))))
(reverse (if (= sta cur) (reverse (if (= sta cur)
out ; there was a trailing delimiter, ignore it out ; there was a trailing delimiter, ignore it
(cons (subseq str sta cur) out))))) (cons (pregexp-replace* rep (subseq str sta cur) ins) out)))))
; TODO: possibly flatten the region here instead of nearly ; TODO: possibly flatten the region here instead of nearly
; every command needing ce-mod to get proper numbers? ; every command needing ce-mod to get proper numbers?