# Strlines

Extract a group of lines from a string by their line numbers.

`(strlines a b x)` extracts the lines between line number a and
line number b from the string x, and returns them as a new
string.

The order of a and b is irrelevant; the extracted text will always
be a proper substring of x, that is, the line with the smallest number
will come first.

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
require a and b to be posps.

Out of bounds conditions. If the larger line number is past the end of the
text, the full text is obtained. In other words, (strlines 0 100000 x) is
likely to just be x except for very large strings. If both line numbers
are past the end of the text, the empty string is returned.

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
exactly when x ends with a newline. In the out-of-bounds case where both
indices are too large, the empty string is returned so there are no
newlines.

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.

### Definitions and Theorems

**Function: **strlines

(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)))))))

**Theorem: **stringp-of-strlines

(defthm stringp-of-strlines
(stringp (strlines a b x))
:rule-classes :type-prescription)