Plaster
New
List
Login
common-lisp
default
anonymous
2021.02.05 10:28:44
(defun %justify-text-OLD (text &key (cols 80) (stream *standard-output*) (prefix "") (separator #\space) omit-first-prefix (start-column 0)) (declare (type simple-string text prefix) (type fixnum cols)) ;;(check-type text simple-string) (let ((len (length text)) (i 0) (last-word-start 0) ;;(line-start 0) (column start-column)) (declare (type fixnum len i last-word-start column)) (flet ((write-word (final) (if (< column cols) (progn (write-string text stream :start last-word-start :end i) (when (< column (- cols 2)) (when (not final) (write-char separator stream)) (incf column))) (progn (write-char #\newline stream) (if prefix (progn (write-string prefix stream) (setf column (length prefix))) (setf column 0)) (write-string text stream :start last-word-start :end i) (write-char separator stream) (incf column (+ (- i last-word-start) 2)))))) (when (and prefix (not omit-first-prefix)) (write-string prefix stream) (setf column (length prefix))) (loop :while (< i len) :do (cond ((char= (aref text i) #\Newline) (write-word nil) (write-char #\newline stream) (if prefix (progn (write-string prefix stream) (setf column (length prefix))) (setf column 0)) (incf i) (setf last-word-start i)) ((char= (aref text i) separator) (write-word nil) (incf i) ;; eat multiple spaces (loop :while (and (< i len) (char= (aref text i) separator)) :do (incf i)) (setf last-word-start i)) (t (incf i) (incf column)))) (when (< last-word-start i) (write-word t)))))
Raw
Annotate
Repaste
Edit