Extract a group of lines from a string by their line numbers.
(strlines a b x) extracts the lines between line number
The order of
Note that we consider the first line of the string to be 1, not 0. This is
intended to agree with the convention followed by many text editors, where the
first line in a file is regarded as line 1 instead of line 0. Accordingly, we
Out of bounds conditions. If the larger line number is past the end of the
text, the full text is obtained. In other words,
Newline behavior. When both line numbers are in range and do not refer to
the last line in the string, the returned string will have a newline after
every line. If the last line is to be included, then it will have a newline
Efficiency. This function should be much faster than calling strline repeatedly and concatenating the resulting lines. Basically it figures out where the text to extract should start and end, then extracts it all as a single chunk.
(defun strlines (a b x) (declare (type integer a) (type integer b) (type string x) (xargs :guard (and (posp a) (posp b) (stringp x)))) (let* ((x (mbe :logic (if (stringp x) x "") :exec x)) (xl (length x))) (mv-let (a b) (if (<= a b) (mv a b) (mv b a)) (let ((start (go-to-line x 0 xl 1 a))) (if (not start) "" (let ((end (go-to-line x start xl a (+ 1 b)))) (subseq x start end)))))))
(defthm stringp-of-strlines (stringp (strlines a b x)) :rule-classes :type-prescription)