• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
      • Std/lists
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
        • Pretty-printing
        • Printtree
        • Base64
        • Charset-p
        • Strtok!
        • Cases
        • Concatenation
        • Html-encoding
        • Character-kinds
        • Substrings
        • Strtok
        • Equivalences
        • Url-encoding
        • Lines
          • Strlines
          • Prefix-lines
            • Strline
          • Ordering
          • Numbers
          • Pad-trim
          • Coercion
          • Std/strings-extensions
          • Std/strings/digit-to-char
          • Substitution
          • Symbols
        • Std/io
        • Std/osets
        • Std/system
        • Std/basic
        • Std/typed-lists
        • Std/bitsets
        • Std/testing
        • Std/typed-alists
        • Std/stobjs
        • Std-extensions
      • Proof-automation
      • Macro-libraries
      • ACL2
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Testing-utilities
      • Math
    • Lines

    Prefix-lines

    Add a prefix to every line in a string.

    (prefix-lines x prefix) builds a new string by adding prefix to the start of every line in the string x. The start of x is regarded as the start of a line, and also gets the prefix. For instance,

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

    Definitions and Theorems

    Function: prefix-lines-aux

    (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: prefix-lines

    (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: character-listp-of-prefix-lines-aux

    (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: stringp-of-prefix-lines

    (defthm stringp-of-prefix-lines
            (stringp (prefix-lines x prefix))
            :rule-classes :type-prescription)