aboutsummaryrefslogtreecommitdiff
path: root/src/util.lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-07-10 17:49:07 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-07-10 21:46:35 -0700
commitb4733ff71f56d70c4a9909d11b37351c4c9c3a85 (patch)
treef080659bd1b21593ff70178f9320eb9532c8c690 /src/util.lisp
parentb3e8205199655d333d1bb18757b5b59510b3f8d0 (diff)
downloadconsfigurator-b4733ff71f56d70c4a9909d11b37351c4c9c3a85.tar.gz
LINES: split on CRLF too
Prompted by apt-get(8) output containing some lines ending CRLF. Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/util.lisp')
-rw-r--r--src/util.lisp25
1 files changed, 24 insertions, 1 deletions
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 <CR>, <LF> or <CR><LF>; <LF><CR> 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))