From b4733ff71f56d70c4a9909d11b37351c4c9c3a85 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 10 Jul 2021 17:49:07 -0700 Subject: LINES: split on CRLF too Prompted by apt-get(8) output containing some lines ending CRLF. Signed-off-by: Sean Whitton --- src/util.lisp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/util.lisp') diff --git a/src/util.lisp b/src/util.lisp index 7dadfe9..d772364 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -41,7 +41,30 @@ (values)) (defun lines (text) - (split-string (stripln text) :separator '(#\Newline #\Return))) + (with-input-from-string (stream text) + (let (bolp buffer) + (flet ((reset () + (setq bolp t + buffer (make-array 0 :fill-pointer 0 + :element-type 'character)))) + ;; Split on either , or ; would mean split + ;; with a blank line in between. Drop a single trailing blank line. + (loop initially (reset) + for char = (read-char stream nil nil) + if char + if (member char '(#\Return #\Newline) :test #'char=) + collect buffer + and do (reset) + (when (char= char #\Return) + (when-let ((next (peek-char nil stream nil nil))) + (when (char= next #\Newline) + (read-char stream)))) + else do (setq bolp nil) + (vector-push-extend char buffer) + end + else + unless bolp collect buffer end + and do (loop-finish)))))) (defun unlines (lines) (format nil "~{~A~%~}" lines)) -- cgit v1.2.3