Add a prefix to every line in a string.
(prefix-lines x prefix) builds a new string by adding
(prefix-lines "hello world goodbye world" " ** ")
Would create the following result:
" ** hello world ** goodbye world"
This is sometimes useful for indenting blobs of text when you are trying to pretty-print things. The operation is fairly efficient: we cons everything into a character list and then coerce it back into a string at the end.
Function:
(defun prefix-lines-aux (n x xl acc prefix) (declare (xargs :guard (and (natp n) (stringp x) (natp xl) (<= n xl) (= xl (length x)) (stringp prefix)))) (let ((n (lnfix n)) (xl (lnfix xl))) (if (mbe :logic (zp (- xl n)) :exec (int= n xl)) acc (let* ((char (char x n)) (acc (cons char acc)) (acc (if (eql char #\Newline) (revappend-chars prefix acc) acc))) (prefix-lines-aux (+ 1 n) x xl acc prefix)))))
Function:
(defun prefix-lines (x prefix) (declare (xargs :guard (and (stringp x) (stringp prefix)))) (let* ((acc (revappend-chars prefix nil)) (rchars (prefix-lines-aux 0 x (length x) acc prefix))) (rchars-to-string rchars)))
Theorem:
(defthm character-listp-of-prefix-lines-aux (implies (and (natp n) (stringp x) (<= n xl) (= xl (length x)) (stringp rprefix) (character-listp acc)) (character-listp (prefix-lines-aux n x xl acc prefix))))
Theorem:
(defthm stringp-of-prefix-lines (stringp (prefix-lines x prefix)) :rule-classes :type-prescription)